xref: /openbmc/linux/net/ceph/osd_client.c (revision f628d799)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a4ce40a9SAlex Elder 
33d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h>
43d14c5d2SYehuda Sadeh 
53d14c5d2SYehuda Sadeh #include <linux/module.h>
63d14c5d2SYehuda Sadeh #include <linux/err.h>
73d14c5d2SYehuda Sadeh #include <linux/highmem.h>
83d14c5d2SYehuda Sadeh #include <linux/mm.h>
93d14c5d2SYehuda Sadeh #include <linux/pagemap.h>
103d14c5d2SYehuda Sadeh #include <linux/slab.h>
113d14c5d2SYehuda Sadeh #include <linux/uaccess.h>
123d14c5d2SYehuda Sadeh #ifdef CONFIG_BLOCK
133d14c5d2SYehuda Sadeh #include <linux/bio.h>
143d14c5d2SYehuda Sadeh #endif
153d14c5d2SYehuda Sadeh 
168cb441c0SIlya Dryomov #include <linux/ceph/ceph_features.h>
173d14c5d2SYehuda Sadeh #include <linux/ceph/libceph.h>
183d14c5d2SYehuda Sadeh #include <linux/ceph/osd_client.h>
193d14c5d2SYehuda Sadeh #include <linux/ceph/messenger.h>
203d14c5d2SYehuda Sadeh #include <linux/ceph/decode.h>
213d14c5d2SYehuda Sadeh #include <linux/ceph/auth.h>
223d14c5d2SYehuda Sadeh #include <linux/ceph/pagelist.h>
2308c1ac50SIlya Dryomov #include <linux/ceph/striper.h>
243d14c5d2SYehuda Sadeh 
253d14c5d2SYehuda Sadeh #define OSD_OPREPLY_FRONT_LEN	512
263d14c5d2SYehuda Sadeh 
275522ae0bSAlex Elder static struct kmem_cache	*ceph_osd_request_cache;
285522ae0bSAlex Elder 
293d14c5d2SYehuda Sadeh static const struct ceph_connection_operations osd_con_ops;
303d14c5d2SYehuda Sadeh 
313d14c5d2SYehuda Sadeh /*
323d14c5d2SYehuda Sadeh  * Implement client access to distributed object storage cluster.
333d14c5d2SYehuda Sadeh  *
343d14c5d2SYehuda Sadeh  * All data objects are stored within a cluster/cloud of OSDs, or
353d14c5d2SYehuda Sadeh  * "object storage devices."  (Note that Ceph OSDs have _nothing_ to
363d14c5d2SYehuda Sadeh  * do with the T10 OSD extensions to SCSI.)  Ceph OSDs are simply
373d14c5d2SYehuda Sadeh  * remote daemons serving up and coordinating consistent and safe
383d14c5d2SYehuda Sadeh  * access to storage.
393d14c5d2SYehuda Sadeh  *
403d14c5d2SYehuda Sadeh  * Cluster membership and the mapping of data objects onto storage devices
413d14c5d2SYehuda Sadeh  * are described by the osd map.
423d14c5d2SYehuda Sadeh  *
433d14c5d2SYehuda Sadeh  * We keep track of pending OSD requests (read, write), resubmit
443d14c5d2SYehuda Sadeh  * requests to different OSDs when the cluster topology/data layout
453d14c5d2SYehuda Sadeh  * change, or retry the affected requests when the communications
463d14c5d2SYehuda Sadeh  * channel with an OSD is reset.
473d14c5d2SYehuda Sadeh  */
483d14c5d2SYehuda Sadeh 
495aea3dcdSIlya Dryomov static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req);
505aea3dcdSIlya Dryomov static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req);
51922dab61SIlya Dryomov static void link_linger(struct ceph_osd *osd,
52922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq);
53922dab61SIlya Dryomov static void unlink_linger(struct ceph_osd *osd,
54922dab61SIlya Dryomov 			  struct ceph_osd_linger_request *lreq);
55a02a946dSIlya Dryomov static void clear_backoffs(struct ceph_osd *osd);
565aea3dcdSIlya Dryomov 
575aea3dcdSIlya Dryomov #if 1
585aea3dcdSIlya Dryomov static inline bool rwsem_is_wrlocked(struct rw_semaphore *sem)
595aea3dcdSIlya Dryomov {
605aea3dcdSIlya Dryomov 	bool wrlocked = true;
615aea3dcdSIlya Dryomov 
625aea3dcdSIlya Dryomov 	if (unlikely(down_read_trylock(sem))) {
635aea3dcdSIlya Dryomov 		wrlocked = false;
645aea3dcdSIlya Dryomov 		up_read(sem);
655aea3dcdSIlya Dryomov 	}
665aea3dcdSIlya Dryomov 
675aea3dcdSIlya Dryomov 	return wrlocked;
685aea3dcdSIlya Dryomov }
695aea3dcdSIlya Dryomov static inline void verify_osdc_locked(struct ceph_osd_client *osdc)
705aea3dcdSIlya Dryomov {
715aea3dcdSIlya Dryomov 	WARN_ON(!rwsem_is_locked(&osdc->lock));
725aea3dcdSIlya Dryomov }
735aea3dcdSIlya Dryomov static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc)
745aea3dcdSIlya Dryomov {
755aea3dcdSIlya Dryomov 	WARN_ON(!rwsem_is_wrlocked(&osdc->lock));
765aea3dcdSIlya Dryomov }
775aea3dcdSIlya Dryomov static inline void verify_osd_locked(struct ceph_osd *osd)
785aea3dcdSIlya Dryomov {
795aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
805aea3dcdSIlya Dryomov 
815aea3dcdSIlya Dryomov 	WARN_ON(!(mutex_is_locked(&osd->lock) &&
825aea3dcdSIlya Dryomov 		  rwsem_is_locked(&osdc->lock)) &&
835aea3dcdSIlya Dryomov 		!rwsem_is_wrlocked(&osdc->lock));
845aea3dcdSIlya Dryomov }
85922dab61SIlya Dryomov static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq)
86922dab61SIlya Dryomov {
87922dab61SIlya Dryomov 	WARN_ON(!mutex_is_locked(&lreq->lock));
88922dab61SIlya Dryomov }
895aea3dcdSIlya Dryomov #else
905aea3dcdSIlya Dryomov static inline void verify_osdc_locked(struct ceph_osd_client *osdc) { }
915aea3dcdSIlya Dryomov static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) { }
925aea3dcdSIlya Dryomov static inline void verify_osd_locked(struct ceph_osd *osd) { }
93922dab61SIlya Dryomov static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) { }
945aea3dcdSIlya Dryomov #endif
955aea3dcdSIlya Dryomov 
963d14c5d2SYehuda Sadeh /*
973d14c5d2SYehuda Sadeh  * calculate the mapping of a file extent onto an object, and fill out the
983d14c5d2SYehuda Sadeh  * request accordingly.  shorten extent as necessary if it crosses an
993d14c5d2SYehuda Sadeh  * object boundary.
1003d14c5d2SYehuda Sadeh  *
1013d14c5d2SYehuda Sadeh  * fill osd op in request message.
1023d14c5d2SYehuda Sadeh  */
103dbe0fc41SAlex Elder static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
104a19dadfbSAlex Elder 			u64 *objnum, u64 *objoff, u64 *objlen)
1053d14c5d2SYehuda Sadeh {
10660e56f13SAlex Elder 	u64 orig_len = *plen;
107dccbf080SIlya Dryomov 	u32 xlen;
1083d14c5d2SYehuda Sadeh 
10960e56f13SAlex Elder 	/* object extent? */
110dccbf080SIlya Dryomov 	ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
111dccbf080SIlya Dryomov 					  objoff, &xlen);
112dccbf080SIlya Dryomov 	*objlen = xlen;
11375d1c941SAlex Elder 	if (*objlen < orig_len) {
11475d1c941SAlex Elder 		*plen = *objlen;
11560e56f13SAlex Elder 		dout(" skipping last %llu, final file extent %llu~%llu\n",
11660e56f13SAlex Elder 		     orig_len - *plen, off, *plen);
11760e56f13SAlex Elder 	}
11860e56f13SAlex Elder 
11975d1c941SAlex Elder 	dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
1203ff5f385SAlex Elder 	return 0;
1213d14c5d2SYehuda Sadeh }
1223d14c5d2SYehuda Sadeh 
123c54d47bfSAlex Elder static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
124c54d47bfSAlex Elder {
125c54d47bfSAlex Elder 	memset(osd_data, 0, sizeof (*osd_data));
126c54d47bfSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
127c54d47bfSAlex Elder }
128c54d47bfSAlex Elder 
12989486833SIlya Dryomov /*
13089486833SIlya Dryomov  * Consumes @pages if @own_pages is true.
13189486833SIlya Dryomov  */
132a4ce40a9SAlex Elder static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
13343bfe5deSAlex Elder 			struct page **pages, u64 length, u32 alignment,
13443bfe5deSAlex Elder 			bool pages_from_pool, bool own_pages)
13543bfe5deSAlex Elder {
13643bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
13743bfe5deSAlex Elder 	osd_data->pages = pages;
13843bfe5deSAlex Elder 	osd_data->length = length;
13943bfe5deSAlex Elder 	osd_data->alignment = alignment;
14043bfe5deSAlex Elder 	osd_data->pages_from_pool = pages_from_pool;
14143bfe5deSAlex Elder 	osd_data->own_pages = own_pages;
14243bfe5deSAlex Elder }
14343bfe5deSAlex Elder 
14489486833SIlya Dryomov /*
14589486833SIlya Dryomov  * Consumes a ref on @pagelist.
14689486833SIlya Dryomov  */
147a4ce40a9SAlex Elder static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
14843bfe5deSAlex Elder 			struct ceph_pagelist *pagelist)
14943bfe5deSAlex Elder {
15043bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
15143bfe5deSAlex Elder 	osd_data->pagelist = pagelist;
15243bfe5deSAlex Elder }
15343bfe5deSAlex Elder 
15443bfe5deSAlex Elder #ifdef CONFIG_BLOCK
155a4ce40a9SAlex Elder static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
1565359a17dSIlya Dryomov 				   struct ceph_bio_iter *bio_pos,
1575359a17dSIlya Dryomov 				   u32 bio_length)
15843bfe5deSAlex Elder {
15943bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
1605359a17dSIlya Dryomov 	osd_data->bio_pos = *bio_pos;
16143bfe5deSAlex Elder 	osd_data->bio_length = bio_length;
16243bfe5deSAlex Elder }
16343bfe5deSAlex Elder #endif /* CONFIG_BLOCK */
16443bfe5deSAlex Elder 
165b9e281c2SIlya Dryomov static void ceph_osd_data_bvecs_init(struct ceph_osd_data *osd_data,
1660010f705SIlya Dryomov 				     struct ceph_bvec_iter *bvec_pos,
1670010f705SIlya Dryomov 				     u32 num_bvecs)
168b9e281c2SIlya Dryomov {
169b9e281c2SIlya Dryomov 	osd_data->type = CEPH_OSD_DATA_TYPE_BVECS;
170b9e281c2SIlya Dryomov 	osd_data->bvec_pos = *bvec_pos;
1710010f705SIlya Dryomov 	osd_data->num_bvecs = num_bvecs;
172b9e281c2SIlya Dryomov }
173b9e281c2SIlya Dryomov 
17449719778SAlex Elder static struct ceph_osd_data *
17549719778SAlex Elder osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
17649719778SAlex Elder {
17749719778SAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
17849719778SAlex Elder 
17949719778SAlex Elder 	return &osd_req->r_ops[which].raw_data_in;
18049719778SAlex Elder }
18149719778SAlex Elder 
182a4ce40a9SAlex Elder struct ceph_osd_data *
183a4ce40a9SAlex Elder osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
184406e2c9fSAlex Elder 			unsigned int which)
185a4ce40a9SAlex Elder {
186863c7eb5SAlex Elder 	return osd_req_op_data(osd_req, which, extent, osd_data);
187a4ce40a9SAlex Elder }
188a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data);
189a4ce40a9SAlex Elder 
19049719778SAlex Elder void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
19149719778SAlex Elder 			unsigned int which, struct page **pages,
19249719778SAlex Elder 			u64 length, u32 alignment,
19349719778SAlex Elder 			bool pages_from_pool, bool own_pages)
19449719778SAlex Elder {
19549719778SAlex Elder 	struct ceph_osd_data *osd_data;
19649719778SAlex Elder 
19749719778SAlex Elder 	osd_data = osd_req_op_raw_data_in(osd_req, which);
19849719778SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
19949719778SAlex Elder 				pages_from_pool, own_pages);
20049719778SAlex Elder }
20149719778SAlex Elder EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
20249719778SAlex Elder 
203a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
204406e2c9fSAlex Elder 			unsigned int which, struct page **pages,
205406e2c9fSAlex Elder 			u64 length, u32 alignment,
206a4ce40a9SAlex Elder 			bool pages_from_pool, bool own_pages)
207a4ce40a9SAlex Elder {
208a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
209a4ce40a9SAlex Elder 
210863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
211a4ce40a9SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
212a4ce40a9SAlex Elder 				pages_from_pool, own_pages);
213a4ce40a9SAlex Elder }
214a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
215a4ce40a9SAlex Elder 
216a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
217406e2c9fSAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
218a4ce40a9SAlex Elder {
219a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
220a4ce40a9SAlex Elder 
221863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
222a4ce40a9SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
223a4ce40a9SAlex Elder }
224a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
225a4ce40a9SAlex Elder 
226a4ce40a9SAlex Elder #ifdef CONFIG_BLOCK
227a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
2285359a17dSIlya Dryomov 				    unsigned int which,
2295359a17dSIlya Dryomov 				    struct ceph_bio_iter *bio_pos,
2305359a17dSIlya Dryomov 				    u32 bio_length)
231a4ce40a9SAlex Elder {
232a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
233863c7eb5SAlex Elder 
234863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
2355359a17dSIlya Dryomov 	ceph_osd_data_bio_init(osd_data, bio_pos, bio_length);
236a4ce40a9SAlex Elder }
237a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
238a4ce40a9SAlex Elder #endif /* CONFIG_BLOCK */
239a4ce40a9SAlex Elder 
2400010f705SIlya Dryomov void osd_req_op_extent_osd_data_bvecs(struct ceph_osd_request *osd_req,
2410010f705SIlya Dryomov 				      unsigned int which,
2420010f705SIlya Dryomov 				      struct bio_vec *bvecs, u32 num_bvecs,
2430010f705SIlya Dryomov 				      u32 bytes)
2440010f705SIlya Dryomov {
2450010f705SIlya Dryomov 	struct ceph_osd_data *osd_data;
2460010f705SIlya Dryomov 	struct ceph_bvec_iter it = {
2470010f705SIlya Dryomov 		.bvecs = bvecs,
2480010f705SIlya Dryomov 		.iter = { .bi_size = bytes },
2490010f705SIlya Dryomov 	};
2500010f705SIlya Dryomov 
2510010f705SIlya Dryomov 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
2520010f705SIlya Dryomov 	ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
2530010f705SIlya Dryomov }
2540010f705SIlya Dryomov EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvecs);
2550010f705SIlya Dryomov 
256b9e281c2SIlya Dryomov void osd_req_op_extent_osd_data_bvec_pos(struct ceph_osd_request *osd_req,
257b9e281c2SIlya Dryomov 					 unsigned int which,
258b9e281c2SIlya Dryomov 					 struct ceph_bvec_iter *bvec_pos)
259b9e281c2SIlya Dryomov {
260b9e281c2SIlya Dryomov 	struct ceph_osd_data *osd_data;
261b9e281c2SIlya Dryomov 
262b9e281c2SIlya Dryomov 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
2630010f705SIlya Dryomov 	ceph_osd_data_bvecs_init(osd_data, bvec_pos, 0);
264b9e281c2SIlya Dryomov }
265b9e281c2SIlya Dryomov EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvec_pos);
266b9e281c2SIlya Dryomov 
267a4ce40a9SAlex Elder static void osd_req_op_cls_request_info_pagelist(
268a4ce40a9SAlex Elder 			struct ceph_osd_request *osd_req,
269a4ce40a9SAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
270a4ce40a9SAlex Elder {
271a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
272a4ce40a9SAlex Elder 
273863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_info);
274a4ce40a9SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
275a4ce40a9SAlex Elder }
276a4ce40a9SAlex Elder 
27704017e29SAlex Elder void osd_req_op_cls_request_data_pagelist(
27804017e29SAlex Elder 			struct ceph_osd_request *osd_req,
27904017e29SAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
28004017e29SAlex Elder {
28104017e29SAlex Elder 	struct ceph_osd_data *osd_data;
28204017e29SAlex Elder 
283863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
28404017e29SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
285bb873b53SIlya Dryomov 	osd_req->r_ops[which].cls.indata_len += pagelist->length;
286bb873b53SIlya Dryomov 	osd_req->r_ops[which].indata_len += pagelist->length;
28704017e29SAlex Elder }
28804017e29SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
28904017e29SAlex Elder 
2906c57b554SAlex Elder void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
2916c57b554SAlex Elder 			unsigned int which, struct page **pages, u64 length,
2926c57b554SAlex Elder 			u32 alignment, bool pages_from_pool, bool own_pages)
2936c57b554SAlex Elder {
2946c57b554SAlex Elder 	struct ceph_osd_data *osd_data;
2956c57b554SAlex Elder 
2966c57b554SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
2976c57b554SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
2986c57b554SAlex Elder 				pages_from_pool, own_pages);
299bb873b53SIlya Dryomov 	osd_req->r_ops[which].cls.indata_len += length;
300bb873b53SIlya Dryomov 	osd_req->r_ops[which].indata_len += length;
3016c57b554SAlex Elder }
3026c57b554SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
3036c57b554SAlex Elder 
304b9e281c2SIlya Dryomov void osd_req_op_cls_request_data_bvecs(struct ceph_osd_request *osd_req,
305b9e281c2SIlya Dryomov 				       unsigned int which,
3060010f705SIlya Dryomov 				       struct bio_vec *bvecs, u32 num_bvecs,
3070010f705SIlya Dryomov 				       u32 bytes)
308b9e281c2SIlya Dryomov {
309b9e281c2SIlya Dryomov 	struct ceph_osd_data *osd_data;
310b9e281c2SIlya Dryomov 	struct ceph_bvec_iter it = {
311b9e281c2SIlya Dryomov 		.bvecs = bvecs,
312b9e281c2SIlya Dryomov 		.iter = { .bi_size = bytes },
313b9e281c2SIlya Dryomov 	};
314b9e281c2SIlya Dryomov 
315b9e281c2SIlya Dryomov 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
3160010f705SIlya Dryomov 	ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
317b9e281c2SIlya Dryomov 	osd_req->r_ops[which].cls.indata_len += bytes;
318b9e281c2SIlya Dryomov 	osd_req->r_ops[which].indata_len += bytes;
319b9e281c2SIlya Dryomov }
320b9e281c2SIlya Dryomov EXPORT_SYMBOL(osd_req_op_cls_request_data_bvecs);
321b9e281c2SIlya Dryomov 
322a4ce40a9SAlex Elder void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
323a4ce40a9SAlex Elder 			unsigned int which, struct page **pages, u64 length,
324a4ce40a9SAlex Elder 			u32 alignment, bool pages_from_pool, bool own_pages)
325a4ce40a9SAlex Elder {
326a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
327a4ce40a9SAlex Elder 
328863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, response_data);
329a4ce40a9SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
330a4ce40a9SAlex Elder 				pages_from_pool, own_pages);
331a4ce40a9SAlex Elder }
332a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
333a4ce40a9SAlex Elder 
33423c08a9cSAlex Elder static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
33523c08a9cSAlex Elder {
33623c08a9cSAlex Elder 	switch (osd_data->type) {
33723c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_NONE:
33823c08a9cSAlex Elder 		return 0;
33923c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_PAGES:
34023c08a9cSAlex Elder 		return osd_data->length;
34123c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_PAGELIST:
34223c08a9cSAlex Elder 		return (u64)osd_data->pagelist->length;
34323c08a9cSAlex Elder #ifdef CONFIG_BLOCK
34423c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_BIO:
34523c08a9cSAlex Elder 		return (u64)osd_data->bio_length;
34623c08a9cSAlex Elder #endif /* CONFIG_BLOCK */
347b9e281c2SIlya Dryomov 	case CEPH_OSD_DATA_TYPE_BVECS:
348b9e281c2SIlya Dryomov 		return osd_data->bvec_pos.iter.bi_size;
34923c08a9cSAlex Elder 	default:
35023c08a9cSAlex Elder 		WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
35123c08a9cSAlex Elder 		return 0;
35223c08a9cSAlex Elder 	}
35323c08a9cSAlex Elder }
35423c08a9cSAlex Elder 
355c54d47bfSAlex Elder static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
356c54d47bfSAlex Elder {
3575476492fSAlex Elder 	if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
358c54d47bfSAlex Elder 		int num_pages;
359c54d47bfSAlex Elder 
360c54d47bfSAlex Elder 		num_pages = calc_pages_for((u64)osd_data->alignment,
361c54d47bfSAlex Elder 						(u64)osd_data->length);
362c54d47bfSAlex Elder 		ceph_release_page_vector(osd_data->pages, num_pages);
36389486833SIlya Dryomov 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
36489486833SIlya Dryomov 		ceph_pagelist_release(osd_data->pagelist);
365c54d47bfSAlex Elder 	}
3665476492fSAlex Elder 	ceph_osd_data_init(osd_data);
3675476492fSAlex Elder }
3685476492fSAlex Elder 
3695476492fSAlex Elder static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
3705476492fSAlex Elder 			unsigned int which)
3715476492fSAlex Elder {
3725476492fSAlex Elder 	struct ceph_osd_req_op *op;
3735476492fSAlex Elder 
3745476492fSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
3755476492fSAlex Elder 	op = &osd_req->r_ops[which];
3765476492fSAlex Elder 
3775476492fSAlex Elder 	switch (op->op) {
3785476492fSAlex Elder 	case CEPH_OSD_OP_READ:
379*f628d799SJeff Layton 	case CEPH_OSD_OP_SPARSE_READ:
3805476492fSAlex Elder 	case CEPH_OSD_OP_WRITE:
381e30b7577SIlya Dryomov 	case CEPH_OSD_OP_WRITEFULL:
382a679e50fSJeff Layton 		kfree(op->extent.sparse_ext);
3835476492fSAlex Elder 		ceph_osd_data_release(&op->extent.osd_data);
3845476492fSAlex Elder 		break;
3855476492fSAlex Elder 	case CEPH_OSD_OP_CALL:
3865476492fSAlex Elder 		ceph_osd_data_release(&op->cls.request_info);
38704017e29SAlex Elder 		ceph_osd_data_release(&op->cls.request_data);
3885476492fSAlex Elder 		ceph_osd_data_release(&op->cls.response_data);
3895476492fSAlex Elder 		break;
390d74b50beSYan, Zheng 	case CEPH_OSD_OP_SETXATTR:
391d74b50beSYan, Zheng 	case CEPH_OSD_OP_CMPXATTR:
392d74b50beSYan, Zheng 		ceph_osd_data_release(&op->xattr.osd_data);
393d74b50beSYan, Zheng 		break;
39466ba609fSYan, Zheng 	case CEPH_OSD_OP_STAT:
39566ba609fSYan, Zheng 		ceph_osd_data_release(&op->raw_data_in);
39666ba609fSYan, Zheng 		break;
397922dab61SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY_ACK:
398922dab61SIlya Dryomov 		ceph_osd_data_release(&op->notify_ack.request_data);
399922dab61SIlya Dryomov 		break;
40019079203SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY:
40119079203SIlya Dryomov 		ceph_osd_data_release(&op->notify.request_data);
40219079203SIlya Dryomov 		ceph_osd_data_release(&op->notify.response_data);
40319079203SIlya Dryomov 		break;
404a4ed38d7SDouglas Fuller 	case CEPH_OSD_OP_LIST_WATCHERS:
405a4ed38d7SDouglas Fuller 		ceph_osd_data_release(&op->list_watchers.response_data);
406a4ed38d7SDouglas Fuller 		break;
40778beb0ffSLuis Henriques 	case CEPH_OSD_OP_COPY_FROM2:
40823ddf9beSLuis Henriques 		ceph_osd_data_release(&op->copy_from.osd_data);
40923ddf9beSLuis Henriques 		break;
4105476492fSAlex Elder 	default:
4115476492fSAlex Elder 		break;
4125476492fSAlex Elder 	}
413c54d47bfSAlex Elder }
414c54d47bfSAlex Elder 
4153d14c5d2SYehuda Sadeh /*
41663244fa1SIlya Dryomov  * Assumes @t is zero-initialized.
41763244fa1SIlya Dryomov  */
41863244fa1SIlya Dryomov static void target_init(struct ceph_osd_request_target *t)
41963244fa1SIlya Dryomov {
42063244fa1SIlya Dryomov 	ceph_oid_init(&t->base_oid);
42163244fa1SIlya Dryomov 	ceph_oloc_init(&t->base_oloc);
42263244fa1SIlya Dryomov 	ceph_oid_init(&t->target_oid);
42363244fa1SIlya Dryomov 	ceph_oloc_init(&t->target_oloc);
42463244fa1SIlya Dryomov 
42563244fa1SIlya Dryomov 	ceph_osds_init(&t->acting);
42663244fa1SIlya Dryomov 	ceph_osds_init(&t->up);
42763244fa1SIlya Dryomov 	t->size = -1;
42863244fa1SIlya Dryomov 	t->min_size = -1;
42963244fa1SIlya Dryomov 
43063244fa1SIlya Dryomov 	t->osd = CEPH_HOMELESS_OSD;
43163244fa1SIlya Dryomov }
43263244fa1SIlya Dryomov 
433922dab61SIlya Dryomov static void target_copy(struct ceph_osd_request_target *dest,
434922dab61SIlya Dryomov 			const struct ceph_osd_request_target *src)
435922dab61SIlya Dryomov {
436922dab61SIlya Dryomov 	ceph_oid_copy(&dest->base_oid, &src->base_oid);
437922dab61SIlya Dryomov 	ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
438922dab61SIlya Dryomov 	ceph_oid_copy(&dest->target_oid, &src->target_oid);
439922dab61SIlya Dryomov 	ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
440922dab61SIlya Dryomov 
441922dab61SIlya Dryomov 	dest->pgid = src->pgid; /* struct */
442dc98ff72SIlya Dryomov 	dest->spgid = src->spgid; /* struct */
443922dab61SIlya Dryomov 	dest->pg_num = src->pg_num;
444922dab61SIlya Dryomov 	dest->pg_num_mask = src->pg_num_mask;
445922dab61SIlya Dryomov 	ceph_osds_copy(&dest->acting, &src->acting);
446922dab61SIlya Dryomov 	ceph_osds_copy(&dest->up, &src->up);
447922dab61SIlya Dryomov 	dest->size = src->size;
448922dab61SIlya Dryomov 	dest->min_size = src->min_size;
449922dab61SIlya Dryomov 	dest->sort_bitwise = src->sort_bitwise;
4502f3fead6SIlya Dryomov 	dest->recovery_deletes = src->recovery_deletes;
451922dab61SIlya Dryomov 
452922dab61SIlya Dryomov 	dest->flags = src->flags;
4537ed286f3SIlya Dryomov 	dest->used_replica = src->used_replica;
454922dab61SIlya Dryomov 	dest->paused = src->paused;
455922dab61SIlya Dryomov 
45604c7d789SIlya Dryomov 	dest->epoch = src->epoch;
457dc93e0e2SIlya Dryomov 	dest->last_force_resend = src->last_force_resend;
458dc93e0e2SIlya Dryomov 
459922dab61SIlya Dryomov 	dest->osd = src->osd;
460922dab61SIlya Dryomov }
461922dab61SIlya Dryomov 
46263244fa1SIlya Dryomov static void target_destroy(struct ceph_osd_request_target *t)
46363244fa1SIlya Dryomov {
46463244fa1SIlya Dryomov 	ceph_oid_destroy(&t->base_oid);
46530c156d9SYan, Zheng 	ceph_oloc_destroy(&t->base_oloc);
46663244fa1SIlya Dryomov 	ceph_oid_destroy(&t->target_oid);
46730c156d9SYan, Zheng 	ceph_oloc_destroy(&t->target_oloc);
46863244fa1SIlya Dryomov }
46963244fa1SIlya Dryomov 
47063244fa1SIlya Dryomov /*
4713d14c5d2SYehuda Sadeh  * requests
4723d14c5d2SYehuda Sadeh  */
4733540bfdbSIlya Dryomov static void request_release_checks(struct ceph_osd_request *req)
4743540bfdbSIlya Dryomov {
4753540bfdbSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&req->r_node));
4764609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
47794e85771SIlya Dryomov 	WARN_ON(!list_empty(&req->r_private_item));
4783540bfdbSIlya Dryomov 	WARN_ON(req->r_osd);
4793540bfdbSIlya Dryomov }
4803540bfdbSIlya Dryomov 
4819e94af20SIlya Dryomov static void ceph_osdc_release_request(struct kref *kref)
4823d14c5d2SYehuda Sadeh {
4839e94af20SIlya Dryomov 	struct ceph_osd_request *req = container_of(kref,
4849e94af20SIlya Dryomov 					    struct ceph_osd_request, r_kref);
4855476492fSAlex Elder 	unsigned int which;
4863d14c5d2SYehuda Sadeh 
4879e94af20SIlya Dryomov 	dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
4889e94af20SIlya Dryomov 	     req->r_request, req->r_reply);
4893540bfdbSIlya Dryomov 	request_release_checks(req);
4909e94af20SIlya Dryomov 
4913d14c5d2SYehuda Sadeh 	if (req->r_request)
4923d14c5d2SYehuda Sadeh 		ceph_msg_put(req->r_request);
4935aea3dcdSIlya Dryomov 	if (req->r_reply)
494ab8cb34aSAlex Elder 		ceph_msg_put(req->r_reply);
4950fff87ecSAlex Elder 
4965476492fSAlex Elder 	for (which = 0; which < req->r_num_ops; which++)
4975476492fSAlex Elder 		osd_req_op_data_release(req, which);
4980fff87ecSAlex Elder 
499a66dd383SIlya Dryomov 	target_destroy(&req->r_t);
5003d14c5d2SYehuda Sadeh 	ceph_put_snap_context(req->r_snapc);
501d30291b9SIlya Dryomov 
5023d14c5d2SYehuda Sadeh 	if (req->r_mempool)
5033d14c5d2SYehuda Sadeh 		mempool_free(req, req->r_osdc->req_mempool);
5043f1af42aSIlya Dryomov 	else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
5055522ae0bSAlex Elder 		kmem_cache_free(ceph_osd_request_cache, req);
5063f1af42aSIlya Dryomov 	else
5073f1af42aSIlya Dryomov 		kfree(req);
5083d14c5d2SYehuda Sadeh }
5099e94af20SIlya Dryomov 
5109e94af20SIlya Dryomov void ceph_osdc_get_request(struct ceph_osd_request *req)
5119e94af20SIlya Dryomov {
5129e94af20SIlya Dryomov 	dout("%s %p (was %d)\n", __func__, req,
5132c935bc5SPeter Zijlstra 	     kref_read(&req->r_kref));
5149e94af20SIlya Dryomov 	kref_get(&req->r_kref);
5159e94af20SIlya Dryomov }
5169e94af20SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_get_request);
5179e94af20SIlya Dryomov 
5189e94af20SIlya Dryomov void ceph_osdc_put_request(struct ceph_osd_request *req)
5199e94af20SIlya Dryomov {
5203ed97d63SIlya Dryomov 	if (req) {
5219e94af20SIlya Dryomov 		dout("%s %p (was %d)\n", __func__, req,
5222c935bc5SPeter Zijlstra 		     kref_read(&req->r_kref));
5239e94af20SIlya Dryomov 		kref_put(&req->r_kref, ceph_osdc_release_request);
5249e94af20SIlya Dryomov 	}
5253ed97d63SIlya Dryomov }
5269e94af20SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_put_request);
5273d14c5d2SYehuda Sadeh 
5283540bfdbSIlya Dryomov static void request_init(struct ceph_osd_request *req)
5293540bfdbSIlya Dryomov {
530042f6498SJeff Layton 	/* req only, each op is zeroed in osd_req_op_init() */
5313540bfdbSIlya Dryomov 	memset(req, 0, sizeof(*req));
5323540bfdbSIlya Dryomov 
5333540bfdbSIlya Dryomov 	kref_init(&req->r_kref);
5343540bfdbSIlya Dryomov 	init_completion(&req->r_completion);
5353540bfdbSIlya Dryomov 	RB_CLEAR_NODE(&req->r_node);
5364609245eSIlya Dryomov 	RB_CLEAR_NODE(&req->r_mc_node);
53794e85771SIlya Dryomov 	INIT_LIST_HEAD(&req->r_private_item);
5383540bfdbSIlya Dryomov 
5393540bfdbSIlya Dryomov 	target_init(&req->r_t);
5403540bfdbSIlya Dryomov }
5413540bfdbSIlya Dryomov 
5423d14c5d2SYehuda Sadeh struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
5433d14c5d2SYehuda Sadeh 					       struct ceph_snap_context *snapc,
5441b83bef2SSage Weil 					       unsigned int num_ops,
5453d14c5d2SYehuda Sadeh 					       bool use_mempool,
54654a54007SAlex Elder 					       gfp_t gfp_flags)
5473d14c5d2SYehuda Sadeh {
5483d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
5493d14c5d2SYehuda Sadeh 
5503d14c5d2SYehuda Sadeh 	if (use_mempool) {
5513f1af42aSIlya Dryomov 		BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
5523d14c5d2SYehuda Sadeh 		req = mempool_alloc(osdc->req_mempool, gfp_flags);
5533f1af42aSIlya Dryomov 	} else if (num_ops <= CEPH_OSD_SLAB_OPS) {
5543f1af42aSIlya Dryomov 		req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
5553d14c5d2SYehuda Sadeh 	} else {
5563f1af42aSIlya Dryomov 		BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
557acafe7e3SKees Cook 		req = kmalloc(struct_size(req, r_ops, num_ops), gfp_flags);
5583d14c5d2SYehuda Sadeh 	}
5593f1af42aSIlya Dryomov 	if (unlikely(!req))
5603d14c5d2SYehuda Sadeh 		return NULL;
5613d14c5d2SYehuda Sadeh 
5623540bfdbSIlya Dryomov 	request_init(req);
5633d14c5d2SYehuda Sadeh 	req->r_osdc = osdc;
5643d14c5d2SYehuda Sadeh 	req->r_mempool = use_mempool;
56579528734SAlex Elder 	req->r_num_ops = num_ops;
56684127282SIlya Dryomov 	req->r_snapid = CEPH_NOSNAP;
56784127282SIlya Dryomov 	req->r_snapc = ceph_get_snap_context(snapc);
5683d14c5d2SYehuda Sadeh 
56913d1ad16SIlya Dryomov 	dout("%s req %p\n", __func__, req);
57013d1ad16SIlya Dryomov 	return req;
5713f1af42aSIlya Dryomov }
57213d1ad16SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_alloc_request);
5733f1af42aSIlya Dryomov 
5742e59ffd1SIlya Dryomov static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc)
57530c156d9SYan, Zheng {
57630c156d9SYan, Zheng 	return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
57730c156d9SYan, Zheng }
57830c156d9SYan, Zheng 
5790d9c1ab3SIlya Dryomov static int __ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp,
5800d9c1ab3SIlya Dryomov 				      int num_request_data_items,
5810d9c1ab3SIlya Dryomov 				      int num_reply_data_items)
58213d1ad16SIlya Dryomov {
58313d1ad16SIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
58413d1ad16SIlya Dryomov 	struct ceph_msg *msg;
58513d1ad16SIlya Dryomov 	int msg_size;
5863d14c5d2SYehuda Sadeh 
5870d9c1ab3SIlya Dryomov 	WARN_ON(req->r_request || req->r_reply);
588d30291b9SIlya Dryomov 	WARN_ON(ceph_oid_empty(&req->r_base_oid));
58930c156d9SYan, Zheng 	WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
590d30291b9SIlya Dryomov 
59113d1ad16SIlya Dryomov 	/* create request message */
5928cb441c0SIlya Dryomov 	msg_size = CEPH_ENCODING_START_BLK_LEN +
5938cb441c0SIlya Dryomov 			CEPH_PGID_ENCODING_LEN + 1; /* spgid */
5948cb441c0SIlya Dryomov 	msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */
5958cb441c0SIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
5968cb441c0SIlya Dryomov 			sizeof(struct ceph_osd_reqid); /* reqid */
5978cb441c0SIlya Dryomov 	msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */
5988cb441c0SIlya Dryomov 	msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */
59930c156d9SYan, Zheng 	msg_size += CEPH_ENCODING_START_BLK_LEN +
60030c156d9SYan, Zheng 			ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
60113d1ad16SIlya Dryomov 	msg_size += 4 + req->r_base_oid.name_len; /* oid */
60213d1ad16SIlya Dryomov 	msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
603ae458f5aSIlya Dryomov 	msg_size += 8; /* snapid */
604ae458f5aSIlya Dryomov 	msg_size += 8; /* snap_seq */
60513d1ad16SIlya Dryomov 	msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
6068cb441c0SIlya Dryomov 	msg_size += 4 + 8; /* retry_attempt, features */
607ae458f5aSIlya Dryomov 
60813d1ad16SIlya Dryomov 	if (req->r_mempool)
6090d9c1ab3SIlya Dryomov 		msg = ceph_msgpool_get(&osdc->msgpool_op, msg_size,
6100d9c1ab3SIlya Dryomov 				       num_request_data_items);
6113d14c5d2SYehuda Sadeh 	else
6120d9c1ab3SIlya Dryomov 		msg = ceph_msg_new2(CEPH_MSG_OSD_OP, msg_size,
6130d9c1ab3SIlya Dryomov 				    num_request_data_items, gfp, true);
61413d1ad16SIlya Dryomov 	if (!msg)
61513d1ad16SIlya Dryomov 		return -ENOMEM;
6163d14c5d2SYehuda Sadeh 
6173d14c5d2SYehuda Sadeh 	memset(msg->front.iov_base, 0, msg->front.iov_len);
6183d14c5d2SYehuda Sadeh 	req->r_request = msg;
6193d14c5d2SYehuda Sadeh 
62013d1ad16SIlya Dryomov 	/* create reply message */
62113d1ad16SIlya Dryomov 	msg_size = OSD_OPREPLY_FRONT_LEN;
622711da55dSIlya Dryomov 	msg_size += req->r_base_oid.name_len;
623711da55dSIlya Dryomov 	msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
62413d1ad16SIlya Dryomov 
62513d1ad16SIlya Dryomov 	if (req->r_mempool)
6260d9c1ab3SIlya Dryomov 		msg = ceph_msgpool_get(&osdc->msgpool_op_reply, msg_size,
6270d9c1ab3SIlya Dryomov 				       num_reply_data_items);
62813d1ad16SIlya Dryomov 	else
6290d9c1ab3SIlya Dryomov 		msg = ceph_msg_new2(CEPH_MSG_OSD_OPREPLY, msg_size,
6300d9c1ab3SIlya Dryomov 				    num_reply_data_items, gfp, true);
63113d1ad16SIlya Dryomov 	if (!msg)
63213d1ad16SIlya Dryomov 		return -ENOMEM;
63313d1ad16SIlya Dryomov 
63413d1ad16SIlya Dryomov 	req->r_reply = msg;
63513d1ad16SIlya Dryomov 
63613d1ad16SIlya Dryomov 	return 0;
63713d1ad16SIlya Dryomov }
6383d14c5d2SYehuda Sadeh 
639a8dd0a37SAlex Elder static bool osd_req_opcode_valid(u16 opcode)
640a8dd0a37SAlex Elder {
641a8dd0a37SAlex Elder 	switch (opcode) {
64270b5bfa3SIlya Dryomov #define GENERATE_CASE(op, opcode, str)	case CEPH_OSD_OP_##op: return true;
64370b5bfa3SIlya Dryomov __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
64470b5bfa3SIlya Dryomov #undef GENERATE_CASE
645a8dd0a37SAlex Elder 	default:
646a8dd0a37SAlex Elder 		return false;
647a8dd0a37SAlex Elder 	}
648a8dd0a37SAlex Elder }
649a8dd0a37SAlex Elder 
6500d9c1ab3SIlya Dryomov static void get_num_data_items(struct ceph_osd_request *req,
6510d9c1ab3SIlya Dryomov 			       int *num_request_data_items,
6520d9c1ab3SIlya Dryomov 			       int *num_reply_data_items)
6530d9c1ab3SIlya Dryomov {
6540d9c1ab3SIlya Dryomov 	struct ceph_osd_req_op *op;
6550d9c1ab3SIlya Dryomov 
6560d9c1ab3SIlya Dryomov 	*num_request_data_items = 0;
6570d9c1ab3SIlya Dryomov 	*num_reply_data_items = 0;
6580d9c1ab3SIlya Dryomov 
6590d9c1ab3SIlya Dryomov 	for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
6600d9c1ab3SIlya Dryomov 		switch (op->op) {
6610d9c1ab3SIlya Dryomov 		/* request */
6620d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_WRITE:
6630d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_WRITEFULL:
6640d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_SETXATTR:
6650d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_CMPXATTR:
6660d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY_ACK:
66778beb0ffSLuis Henriques 		case CEPH_OSD_OP_COPY_FROM2:
6680d9c1ab3SIlya Dryomov 			*num_request_data_items += 1;
6690d9c1ab3SIlya Dryomov 			break;
6700d9c1ab3SIlya Dryomov 
6710d9c1ab3SIlya Dryomov 		/* reply */
6720d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_STAT:
6730d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_READ:
674*f628d799SJeff Layton 		case CEPH_OSD_OP_SPARSE_READ:
6750d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_LIST_WATCHERS:
6760d9c1ab3SIlya Dryomov 			*num_reply_data_items += 1;
6770d9c1ab3SIlya Dryomov 			break;
6780d9c1ab3SIlya Dryomov 
6790d9c1ab3SIlya Dryomov 		/* both */
6800d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY:
6810d9c1ab3SIlya Dryomov 			*num_request_data_items += 1;
6820d9c1ab3SIlya Dryomov 			*num_reply_data_items += 1;
6830d9c1ab3SIlya Dryomov 			break;
6840d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_CALL:
6850d9c1ab3SIlya Dryomov 			*num_request_data_items += 2;
6860d9c1ab3SIlya Dryomov 			*num_reply_data_items += 1;
6870d9c1ab3SIlya Dryomov 			break;
6880d9c1ab3SIlya Dryomov 
6890d9c1ab3SIlya Dryomov 		default:
6900d9c1ab3SIlya Dryomov 			WARN_ON(!osd_req_opcode_valid(op->op));
6910d9c1ab3SIlya Dryomov 			break;
6920d9c1ab3SIlya Dryomov 		}
6930d9c1ab3SIlya Dryomov 	}
6940d9c1ab3SIlya Dryomov }
6950d9c1ab3SIlya Dryomov 
6960d9c1ab3SIlya Dryomov /*
6970d9c1ab3SIlya Dryomov  * oid, oloc and OSD op opcode(s) must be filled in before this function
6980d9c1ab3SIlya Dryomov  * is called.
6990d9c1ab3SIlya Dryomov  */
7000d9c1ab3SIlya Dryomov int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
7010d9c1ab3SIlya Dryomov {
7020d9c1ab3SIlya Dryomov 	int num_request_data_items, num_reply_data_items;
7030d9c1ab3SIlya Dryomov 
7040d9c1ab3SIlya Dryomov 	get_num_data_items(req, &num_request_data_items, &num_reply_data_items);
7050d9c1ab3SIlya Dryomov 	return __ceph_osdc_alloc_messages(req, gfp, num_request_data_items,
7060d9c1ab3SIlya Dryomov 					  num_reply_data_items);
7070d9c1ab3SIlya Dryomov }
7080d9c1ab3SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_alloc_messages);
7090d9c1ab3SIlya Dryomov 
71033803f33SAlex Elder /*
71133803f33SAlex Elder  * This is an osd op init function for opcodes that have no data or
71233803f33SAlex Elder  * other information associated with them.  It also serves as a
71333803f33SAlex Elder  * common init routine for all the other init functions, below.
71433803f33SAlex Elder  */
715042f6498SJeff Layton struct ceph_osd_req_op *
716042f6498SJeff Layton osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
717144cba14SYan, Zheng 		 u16 opcode, u32 flags)
71833803f33SAlex Elder {
719c99d2d4aSAlex Elder 	struct ceph_osd_req_op *op;
720c99d2d4aSAlex Elder 
721c99d2d4aSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
72233803f33SAlex Elder 	BUG_ON(!osd_req_opcode_valid(opcode));
72333803f33SAlex Elder 
724c99d2d4aSAlex Elder 	op = &osd_req->r_ops[which];
72533803f33SAlex Elder 	memset(op, 0, sizeof (*op));
72633803f33SAlex Elder 	op->op = opcode;
727144cba14SYan, Zheng 	op->flags = flags;
728c99d2d4aSAlex Elder 
729c99d2d4aSAlex Elder 	return op;
73033803f33SAlex Elder }
73149719778SAlex Elder EXPORT_SYMBOL(osd_req_op_init);
73249719778SAlex Elder 
733c99d2d4aSAlex Elder void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
734c99d2d4aSAlex Elder 				unsigned int which, u16 opcode,
73533803f33SAlex Elder 				u64 offset, u64 length,
73633803f33SAlex Elder 				u64 truncate_size, u32 truncate_seq)
73733803f33SAlex Elder {
738042f6498SJeff Layton 	struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which,
739144cba14SYan, Zheng 						     opcode, 0);
74033803f33SAlex Elder 	size_t payload_len = 0;
74133803f33SAlex Elder 
742ad7a60deSLi Wang 	BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
743e30b7577SIlya Dryomov 	       opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
744*f628d799SJeff Layton 	       opcode != CEPH_OSD_OP_TRUNCATE && opcode != CEPH_OSD_OP_SPARSE_READ);
74533803f33SAlex Elder 
74633803f33SAlex Elder 	op->extent.offset = offset;
74733803f33SAlex Elder 	op->extent.length = length;
74833803f33SAlex Elder 	op->extent.truncate_size = truncate_size;
74933803f33SAlex Elder 	op->extent.truncate_seq = truncate_seq;
750e30b7577SIlya Dryomov 	if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
75133803f33SAlex Elder 		payload_len += length;
75233803f33SAlex Elder 
753de2aa102SIlya Dryomov 	op->indata_len = payload_len;
75433803f33SAlex Elder }
75533803f33SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_init);
75633803f33SAlex Elder 
757c99d2d4aSAlex Elder void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
758c99d2d4aSAlex Elder 				unsigned int which, u64 length)
759e5975c7cSAlex Elder {
760c99d2d4aSAlex Elder 	struct ceph_osd_req_op *op;
761c99d2d4aSAlex Elder 	u64 previous;
762c99d2d4aSAlex Elder 
763c99d2d4aSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
764c99d2d4aSAlex Elder 	op = &osd_req->r_ops[which];
765c99d2d4aSAlex Elder 	previous = op->extent.length;
766e5975c7cSAlex Elder 
767e5975c7cSAlex Elder 	if (length == previous)
768e5975c7cSAlex Elder 		return;		/* Nothing to do */
769e5975c7cSAlex Elder 	BUG_ON(length > previous);
770e5975c7cSAlex Elder 
771e5975c7cSAlex Elder 	op->extent.length = length;
772d641df81SYan, Zheng 	if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
773de2aa102SIlya Dryomov 		op->indata_len -= previous - length;
774e5975c7cSAlex Elder }
775e5975c7cSAlex Elder EXPORT_SYMBOL(osd_req_op_extent_update);
776e5975c7cSAlex Elder 
7772c63f49aSYan, Zheng void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
7782c63f49aSYan, Zheng 				unsigned int which, u64 offset_inc)
7792c63f49aSYan, Zheng {
7802c63f49aSYan, Zheng 	struct ceph_osd_req_op *op, *prev_op;
7812c63f49aSYan, Zheng 
7822c63f49aSYan, Zheng 	BUG_ON(which + 1 >= osd_req->r_num_ops);
7832c63f49aSYan, Zheng 
7842c63f49aSYan, Zheng 	prev_op = &osd_req->r_ops[which];
785042f6498SJeff Layton 	op = osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
7862c63f49aSYan, Zheng 	/* dup previous one */
7872c63f49aSYan, Zheng 	op->indata_len = prev_op->indata_len;
7882c63f49aSYan, Zheng 	op->outdata_len = prev_op->outdata_len;
7892c63f49aSYan, Zheng 	op->extent = prev_op->extent;
7902c63f49aSYan, Zheng 	/* adjust offset */
7912c63f49aSYan, Zheng 	op->extent.offset += offset_inc;
7922c63f49aSYan, Zheng 	op->extent.length -= offset_inc;
7932c63f49aSYan, Zheng 
7942c63f49aSYan, Zheng 	if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
7952c63f49aSYan, Zheng 		op->indata_len -= offset_inc;
7962c63f49aSYan, Zheng }
7972c63f49aSYan, Zheng EXPORT_SYMBOL(osd_req_op_extent_dup_last);
7982c63f49aSYan, Zheng 
799fe943d50SChengguang Xu int osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
80024639ce5SIlya Dryomov 			const char *class, const char *method)
80133803f33SAlex Elder {
80224639ce5SIlya Dryomov 	struct ceph_osd_req_op *op;
8035f562df5SAlex Elder 	struct ceph_pagelist *pagelist;
80433803f33SAlex Elder 	size_t payload_len = 0;
80533803f33SAlex Elder 	size_t size;
8064766815bSDavid Disseldorp 	int ret;
80733803f33SAlex Elder 
808042f6498SJeff Layton 	op = osd_req_op_init(osd_req, which, CEPH_OSD_OP_CALL, 0);
80933803f33SAlex Elder 
81033165d47SIlya Dryomov 	pagelist = ceph_pagelist_alloc(GFP_NOFS);
811fe943d50SChengguang Xu 	if (!pagelist)
812fe943d50SChengguang Xu 		return -ENOMEM;
813fe943d50SChengguang Xu 
81433803f33SAlex Elder 	op->cls.class_name = class;
81533803f33SAlex Elder 	size = strlen(class);
81633803f33SAlex Elder 	BUG_ON(size > (size_t) U8_MAX);
81733803f33SAlex Elder 	op->cls.class_len = size;
8184766815bSDavid Disseldorp 	ret = ceph_pagelist_append(pagelist, class, size);
8194766815bSDavid Disseldorp 	if (ret)
8204766815bSDavid Disseldorp 		goto err_pagelist_free;
82133803f33SAlex Elder 	payload_len += size;
82233803f33SAlex Elder 
82333803f33SAlex Elder 	op->cls.method_name = method;
82433803f33SAlex Elder 	size = strlen(method);
82533803f33SAlex Elder 	BUG_ON(size > (size_t) U8_MAX);
82633803f33SAlex Elder 	op->cls.method_len = size;
8274766815bSDavid Disseldorp 	ret = ceph_pagelist_append(pagelist, method, size);
8284766815bSDavid Disseldorp 	if (ret)
8294766815bSDavid Disseldorp 		goto err_pagelist_free;
83033803f33SAlex Elder 	payload_len += size;
83133803f33SAlex Elder 
832a4ce40a9SAlex Elder 	osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
833de2aa102SIlya Dryomov 	op->indata_len = payload_len;
834fe943d50SChengguang Xu 	return 0;
8354766815bSDavid Disseldorp 
8364766815bSDavid Disseldorp err_pagelist_free:
8374766815bSDavid Disseldorp 	ceph_pagelist_release(pagelist);
8384766815bSDavid Disseldorp 	return ret;
83933803f33SAlex Elder }
84033803f33SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_init);
8418c042b0dSAlex Elder 
842d74b50beSYan, Zheng int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
843d74b50beSYan, Zheng 			  u16 opcode, const char *name, const void *value,
844d74b50beSYan, Zheng 			  size_t size, u8 cmp_op, u8 cmp_mode)
845d74b50beSYan, Zheng {
846042f6498SJeff Layton 	struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which,
847144cba14SYan, Zheng 						     opcode, 0);
848d74b50beSYan, Zheng 	struct ceph_pagelist *pagelist;
849d74b50beSYan, Zheng 	size_t payload_len;
8504766815bSDavid Disseldorp 	int ret;
851d74b50beSYan, Zheng 
852d74b50beSYan, Zheng 	BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
853d74b50beSYan, Zheng 
85433165d47SIlya Dryomov 	pagelist = ceph_pagelist_alloc(GFP_NOFS);
855d74b50beSYan, Zheng 	if (!pagelist)
856d74b50beSYan, Zheng 		return -ENOMEM;
857d74b50beSYan, Zheng 
858d74b50beSYan, Zheng 	payload_len = strlen(name);
859d74b50beSYan, Zheng 	op->xattr.name_len = payload_len;
8604766815bSDavid Disseldorp 	ret = ceph_pagelist_append(pagelist, name, payload_len);
8614766815bSDavid Disseldorp 	if (ret)
8624766815bSDavid Disseldorp 		goto err_pagelist_free;
863d74b50beSYan, Zheng 
864d74b50beSYan, Zheng 	op->xattr.value_len = size;
8654766815bSDavid Disseldorp 	ret = ceph_pagelist_append(pagelist, value, size);
8664766815bSDavid Disseldorp 	if (ret)
8674766815bSDavid Disseldorp 		goto err_pagelist_free;
868d74b50beSYan, Zheng 	payload_len += size;
869d74b50beSYan, Zheng 
870d74b50beSYan, Zheng 	op->xattr.cmp_op = cmp_op;
871d74b50beSYan, Zheng 	op->xattr.cmp_mode = cmp_mode;
872d74b50beSYan, Zheng 
873d74b50beSYan, Zheng 	ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
874de2aa102SIlya Dryomov 	op->indata_len = payload_len;
875d74b50beSYan, Zheng 	return 0;
8764766815bSDavid Disseldorp 
8774766815bSDavid Disseldorp err_pagelist_free:
8784766815bSDavid Disseldorp 	ceph_pagelist_release(pagelist);
8794766815bSDavid Disseldorp 	return ret;
880d74b50beSYan, Zheng }
881d74b50beSYan, Zheng EXPORT_SYMBOL(osd_req_op_xattr_init);
882d74b50beSYan, Zheng 
883922dab61SIlya Dryomov /*
884922dab61SIlya Dryomov  * @watch_opcode: CEPH_OSD_WATCH_OP_*
885922dab61SIlya Dryomov  */
886922dab61SIlya Dryomov static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
88775dbb685SIlya Dryomov 				  u8 watch_opcode, u64 cookie, u32 gen)
88833803f33SAlex Elder {
889922dab61SIlya Dryomov 	struct ceph_osd_req_op *op;
89033803f33SAlex Elder 
891042f6498SJeff Layton 	op = osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
89233803f33SAlex Elder 	op->watch.cookie = cookie;
893922dab61SIlya Dryomov 	op->watch.op = watch_opcode;
89475dbb685SIlya Dryomov 	op->watch.gen = gen;
89575dbb685SIlya Dryomov }
89675dbb685SIlya Dryomov 
89775dbb685SIlya Dryomov /*
89875dbb685SIlya Dryomov  * prot_ver, timeout and notify payload (may be empty) should already be
89975dbb685SIlya Dryomov  * encoded in @request_pl
90075dbb685SIlya Dryomov  */
90175dbb685SIlya Dryomov static void osd_req_op_notify_init(struct ceph_osd_request *req, int which,
90275dbb685SIlya Dryomov 				   u64 cookie, struct ceph_pagelist *request_pl)
90375dbb685SIlya Dryomov {
90475dbb685SIlya Dryomov 	struct ceph_osd_req_op *op;
90575dbb685SIlya Dryomov 
90675dbb685SIlya Dryomov 	op = osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
90775dbb685SIlya Dryomov 	op->notify.cookie = cookie;
90875dbb685SIlya Dryomov 
90975dbb685SIlya Dryomov 	ceph_osd_data_pagelist_init(&op->notify.request_data, request_pl);
91075dbb685SIlya Dryomov 	op->indata_len = request_pl->length;
91133803f33SAlex Elder }
91233803f33SAlex Elder 
913d3798accSIlya Dryomov /*
914d3798accSIlya Dryomov  * @flags: CEPH_OSD_OP_ALLOC_HINT_FLAG_*
915d3798accSIlya Dryomov  */
916c647b8a8SIlya Dryomov void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
917c647b8a8SIlya Dryomov 				unsigned int which,
918c647b8a8SIlya Dryomov 				u64 expected_object_size,
919d3798accSIlya Dryomov 				u64 expected_write_size,
920d3798accSIlya Dryomov 				u32 flags)
921c647b8a8SIlya Dryomov {
922042f6498SJeff Layton 	struct ceph_osd_req_op *op;
923c647b8a8SIlya Dryomov 
924042f6498SJeff Layton 	op = osd_req_op_init(osd_req, which, CEPH_OSD_OP_SETALLOCHINT, 0);
925c647b8a8SIlya Dryomov 	op->alloc_hint.expected_object_size = expected_object_size;
926c647b8a8SIlya Dryomov 	op->alloc_hint.expected_write_size = expected_write_size;
927d3798accSIlya Dryomov 	op->alloc_hint.flags = flags;
928c647b8a8SIlya Dryomov 
929c647b8a8SIlya Dryomov 	/*
930c647b8a8SIlya Dryomov 	 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
931c647b8a8SIlya Dryomov 	 * not worth a feature bit.  Set FAILOK per-op flag to make
932c647b8a8SIlya Dryomov 	 * sure older osds don't trip over an unsupported opcode.
933c647b8a8SIlya Dryomov 	 */
934c647b8a8SIlya Dryomov 	op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
935c647b8a8SIlya Dryomov }
936c647b8a8SIlya Dryomov EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
937c647b8a8SIlya Dryomov 
93890af3602SAlex Elder static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
939ec9123c5SAlex Elder 				struct ceph_osd_data *osd_data)
940ec9123c5SAlex Elder {
941ec9123c5SAlex Elder 	u64 length = ceph_osd_data_length(osd_data);
942ec9123c5SAlex Elder 
943ec9123c5SAlex Elder 	if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
944ec9123c5SAlex Elder 		BUG_ON(length > (u64) SIZE_MAX);
945ec9123c5SAlex Elder 		if (length)
94690af3602SAlex Elder 			ceph_msg_data_add_pages(msg, osd_data->pages,
947e8862740SIlya Dryomov 					length, osd_data->alignment, false);
948ec9123c5SAlex Elder 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
949ec9123c5SAlex Elder 		BUG_ON(!length);
95090af3602SAlex Elder 		ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
951ec9123c5SAlex Elder #ifdef CONFIG_BLOCK
952ec9123c5SAlex Elder 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
9535359a17dSIlya Dryomov 		ceph_msg_data_add_bio(msg, &osd_data->bio_pos, length);
954ec9123c5SAlex Elder #endif
955b9e281c2SIlya Dryomov 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_BVECS) {
956b9e281c2SIlya Dryomov 		ceph_msg_data_add_bvecs(msg, &osd_data->bvec_pos);
957ec9123c5SAlex Elder 	} else {
958ec9123c5SAlex Elder 		BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
959ec9123c5SAlex Elder 	}
960ec9123c5SAlex Elder }
961ec9123c5SAlex Elder 
962bb873b53SIlya Dryomov static u32 osd_req_encode_op(struct ceph_osd_op *dst,
963bb873b53SIlya Dryomov 			     const struct ceph_osd_req_op *src)
9643d14c5d2SYehuda Sadeh {
965065a68f9SAlex Elder 	switch (src->op) {
966fbfab539SAlex Elder 	case CEPH_OSD_OP_STAT:
967fbfab539SAlex Elder 		break;
9683d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_READ:
969*f628d799SJeff Layton 	case CEPH_OSD_OP_SPARSE_READ:
9703d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_WRITE:
971e30b7577SIlya Dryomov 	case CEPH_OSD_OP_WRITEFULL:
972ad7a60deSLi Wang 	case CEPH_OSD_OP_ZERO:
973ad7a60deSLi Wang 	case CEPH_OSD_OP_TRUNCATE:
974175face2SAlex Elder 		dst->extent.offset = cpu_to_le64(src->extent.offset);
975175face2SAlex Elder 		dst->extent.length = cpu_to_le64(src->extent.length);
9763d14c5d2SYehuda Sadeh 		dst->extent.truncate_size =
9773d14c5d2SYehuda Sadeh 			cpu_to_le64(src->extent.truncate_size);
9783d14c5d2SYehuda Sadeh 		dst->extent.truncate_seq =
9793d14c5d2SYehuda Sadeh 			cpu_to_le32(src->extent.truncate_seq);
9803d14c5d2SYehuda Sadeh 		break;
9813d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_CALL:
9823d14c5d2SYehuda Sadeh 		dst->cls.class_len = src->cls.class_len;
9833d14c5d2SYehuda Sadeh 		dst->cls.method_len = src->cls.method_len;
984bb873b53SIlya Dryomov 		dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
9853d14c5d2SYehuda Sadeh 		break;
986a40c4f10SYehuda Sadeh 	case CEPH_OSD_OP_WATCH:
987a40c4f10SYehuda Sadeh 		dst->watch.cookie = cpu_to_le64(src->watch.cookie);
988922dab61SIlya Dryomov 		dst->watch.ver = cpu_to_le64(0);
989922dab61SIlya Dryomov 		dst->watch.op = src->watch.op;
990922dab61SIlya Dryomov 		dst->watch.gen = cpu_to_le32(src->watch.gen);
991922dab61SIlya Dryomov 		break;
992922dab61SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY_ACK:
993a40c4f10SYehuda Sadeh 		break;
99419079203SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY:
99519079203SIlya Dryomov 		dst->notify.cookie = cpu_to_le64(src->notify.cookie);
99619079203SIlya Dryomov 		break;
997a4ed38d7SDouglas Fuller 	case CEPH_OSD_OP_LIST_WATCHERS:
998a4ed38d7SDouglas Fuller 		break;
999c647b8a8SIlya Dryomov 	case CEPH_OSD_OP_SETALLOCHINT:
1000c647b8a8SIlya Dryomov 		dst->alloc_hint.expected_object_size =
1001c647b8a8SIlya Dryomov 		    cpu_to_le64(src->alloc_hint.expected_object_size);
1002c647b8a8SIlya Dryomov 		dst->alloc_hint.expected_write_size =
1003c647b8a8SIlya Dryomov 		    cpu_to_le64(src->alloc_hint.expected_write_size);
1004d3798accSIlya Dryomov 		dst->alloc_hint.flags = cpu_to_le32(src->alloc_hint.flags);
1005c647b8a8SIlya Dryomov 		break;
1006d74b50beSYan, Zheng 	case CEPH_OSD_OP_SETXATTR:
1007d74b50beSYan, Zheng 	case CEPH_OSD_OP_CMPXATTR:
1008d74b50beSYan, Zheng 		dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
1009d74b50beSYan, Zheng 		dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
1010d74b50beSYan, Zheng 		dst->xattr.cmp_op = src->xattr.cmp_op;
1011d74b50beSYan, Zheng 		dst->xattr.cmp_mode = src->xattr.cmp_mode;
1012d74b50beSYan, Zheng 		break;
1013864e9197SYan, Zheng 	case CEPH_OSD_OP_CREATE:
1014864e9197SYan, Zheng 	case CEPH_OSD_OP_DELETE:
1015864e9197SYan, Zheng 		break;
101678beb0ffSLuis Henriques 	case CEPH_OSD_OP_COPY_FROM2:
101723ddf9beSLuis Henriques 		dst->copy_from.snapid = cpu_to_le64(src->copy_from.snapid);
101823ddf9beSLuis Henriques 		dst->copy_from.src_version =
101923ddf9beSLuis Henriques 			cpu_to_le64(src->copy_from.src_version);
102023ddf9beSLuis Henriques 		dst->copy_from.flags = src->copy_from.flags;
102123ddf9beSLuis Henriques 		dst->copy_from.src_fadvise_flags =
102223ddf9beSLuis Henriques 			cpu_to_le32(src->copy_from.src_fadvise_flags);
102323ddf9beSLuis Henriques 		break;
10243d14c5d2SYehuda Sadeh 	default:
10254c46459cSAlex Elder 		pr_err("unsupported osd opcode %s\n",
10268f63ca2dSAlex Elder 			ceph_osd_op_name(src->op));
10274c46459cSAlex Elder 		WARN_ON(1);
1028a8dd0a37SAlex Elder 
1029a8dd0a37SAlex Elder 		return 0;
10303d14c5d2SYehuda Sadeh 	}
10317b25bf5fSIlya Dryomov 
1032a8dd0a37SAlex Elder 	dst->op = cpu_to_le16(src->op);
10337b25bf5fSIlya Dryomov 	dst->flags = cpu_to_le32(src->flags);
1034de2aa102SIlya Dryomov 	dst->payload_len = cpu_to_le32(src->indata_len);
1035175face2SAlex Elder 
1036bb873b53SIlya Dryomov 	return src->indata_len;
10373d14c5d2SYehuda Sadeh }
10383d14c5d2SYehuda Sadeh 
10393d14c5d2SYehuda Sadeh /*
10403d14c5d2SYehuda Sadeh  * build new request AND message, calculate layout, and adjust file
10413d14c5d2SYehuda Sadeh  * extent as needed.
10423d14c5d2SYehuda Sadeh  *
10433d14c5d2SYehuda Sadeh  * if the file was recently truncated, we include information about its
10443d14c5d2SYehuda Sadeh  * old and new size so that the object can be updated appropriately.  (we
10453d14c5d2SYehuda Sadeh  * avoid synchronously deleting truncated objects because it's slow.)
10463d14c5d2SYehuda Sadeh  */
10473d14c5d2SYehuda Sadeh struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
10483d14c5d2SYehuda Sadeh 					       struct ceph_file_layout *layout,
10493d14c5d2SYehuda Sadeh 					       struct ceph_vino vino,
1050715e4cd4SYan, Zheng 					       u64 off, u64 *plen,
1051715e4cd4SYan, Zheng 					       unsigned int which, int num_ops,
10523d14c5d2SYehuda Sadeh 					       int opcode, int flags,
10533d14c5d2SYehuda Sadeh 					       struct ceph_snap_context *snapc,
10543d14c5d2SYehuda Sadeh 					       u32 truncate_seq,
10553d14c5d2SYehuda Sadeh 					       u64 truncate_size,
1056153e5167SAlex Elder 					       bool use_mempool)
10573d14c5d2SYehuda Sadeh {
10583d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
105975d1c941SAlex Elder 	u64 objnum = 0;
106075d1c941SAlex Elder 	u64 objoff = 0;
106175d1c941SAlex Elder 	u64 objlen = 0;
10626816282dSSage Weil 	int r;
10633d14c5d2SYehuda Sadeh 
1064ad7a60deSLi Wang 	BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
1065864e9197SYan, Zheng 	       opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
1066*f628d799SJeff Layton 	       opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE &&
1067*f628d799SJeff Layton 	       opcode != CEPH_OSD_OP_SPARSE_READ);
10683d14c5d2SYehuda Sadeh 
1069acead002SAlex Elder 	req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
1070ae7ca4a3SAlex Elder 					GFP_NOFS);
107113d1ad16SIlya Dryomov 	if (!req) {
107213d1ad16SIlya Dryomov 		r = -ENOMEM;
107313d1ad16SIlya Dryomov 		goto fail;
107413d1ad16SIlya Dryomov 	}
107579528734SAlex Elder 
10763d14c5d2SYehuda Sadeh 	/* calculate max write size */
1077a19dadfbSAlex Elder 	r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
107813d1ad16SIlya Dryomov 	if (r)
107913d1ad16SIlya Dryomov 		goto fail;
1080a19dadfbSAlex Elder 
1081864e9197SYan, Zheng 	if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
1082144cba14SYan, Zheng 		osd_req_op_init(req, which, opcode, 0);
1083864e9197SYan, Zheng 	} else {
10847627151eSYan, Zheng 		u32 object_size = layout->object_size;
1085864e9197SYan, Zheng 		u32 object_base = off - objoff;
1086ccca4e37SYan, Zheng 		if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
1087d18d1e28SAlex Elder 			if (truncate_size <= object_base) {
1088d18d1e28SAlex Elder 				truncate_size = 0;
1089d18d1e28SAlex Elder 			} else {
1090d18d1e28SAlex Elder 				truncate_size -= object_base;
1091d18d1e28SAlex Elder 				if (truncate_size > object_size)
1092d18d1e28SAlex Elder 					truncate_size = object_size;
1093d18d1e28SAlex Elder 			}
1094ccca4e37SYan, Zheng 		}
1095715e4cd4SYan, Zheng 		osd_req_op_extent_init(req, which, opcode, objoff, objlen,
1096b0270324SAlex Elder 				       truncate_size, truncate_seq);
1097864e9197SYan, Zheng 	}
1098d18d1e28SAlex Elder 
10997627151eSYan, Zheng 	req->r_base_oloc.pool = layout->pool_id;
110030c156d9SYan, Zheng 	req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
1101d30291b9SIlya Dryomov 	ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
110222d2cfdfSIlya Dryomov 	req->r_flags = flags | osdc->client->options->read_from_replica;
1103dbe0fc41SAlex Elder 
1104bb873b53SIlya Dryomov 	req->r_snapid = vino.snap;
1105bb873b53SIlya Dryomov 	if (flags & CEPH_OSD_FLAG_WRITE)
1106bb873b53SIlya Dryomov 		req->r_data_offset = off;
1107bb873b53SIlya Dryomov 
11080d9c1ab3SIlya Dryomov 	if (num_ops > 1)
11090d9c1ab3SIlya Dryomov 		/*
11100d9c1ab3SIlya Dryomov 		 * This is a special case for ceph_writepages_start(), but it
11110d9c1ab3SIlya Dryomov 		 * also covers ceph_uninline_data().  If more multi-op request
11120d9c1ab3SIlya Dryomov 		 * use cases emerge, we will need a separate helper.
11130d9c1ab3SIlya Dryomov 		 */
11140d9c1ab3SIlya Dryomov 		r = __ceph_osdc_alloc_messages(req, GFP_NOFS, num_ops, 0);
11150d9c1ab3SIlya Dryomov 	else
111613d1ad16SIlya Dryomov 		r = ceph_osdc_alloc_messages(req, GFP_NOFS);
111713d1ad16SIlya Dryomov 	if (r)
111813d1ad16SIlya Dryomov 		goto fail;
111913d1ad16SIlya Dryomov 
11203d14c5d2SYehuda Sadeh 	return req;
112113d1ad16SIlya Dryomov 
112213d1ad16SIlya Dryomov fail:
112313d1ad16SIlya Dryomov 	ceph_osdc_put_request(req);
112413d1ad16SIlya Dryomov 	return ERR_PTR(r);
11253d14c5d2SYehuda Sadeh }
11263d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_new_request);
11273d14c5d2SYehuda Sadeh 
1128a679e50fSJeff Layton int __ceph_alloc_sparse_ext_map(struct ceph_osd_req_op *op, int cnt)
1129a679e50fSJeff Layton {
1130a679e50fSJeff Layton 	op->extent.sparse_ext_cnt = cnt;
1131a679e50fSJeff Layton 	op->extent.sparse_ext = kmalloc_array(cnt,
1132a679e50fSJeff Layton 					      sizeof(*op->extent.sparse_ext),
1133a679e50fSJeff Layton 					      GFP_NOFS);
1134a679e50fSJeff Layton 	if (!op->extent.sparse_ext)
1135a679e50fSJeff Layton 		return -ENOMEM;
1136a679e50fSJeff Layton 	return 0;
1137a679e50fSJeff Layton }
1138a679e50fSJeff Layton EXPORT_SYMBOL(__ceph_alloc_sparse_ext_map);
1139a679e50fSJeff Layton 
11403d14c5d2SYehuda Sadeh /*
11413d14c5d2SYehuda Sadeh  * We keep osd requests in an rbtree, sorted by ->r_tid.
11423d14c5d2SYehuda Sadeh  */
1143fcd00b68SIlya Dryomov DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
11444609245eSIlya Dryomov DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
11453d14c5d2SYehuda Sadeh 
114666850df5SIlya Dryomov /*
114766850df5SIlya Dryomov  * Call @fn on each OSD request as long as @fn returns 0.
114866850df5SIlya Dryomov  */
114966850df5SIlya Dryomov static void for_each_request(struct ceph_osd_client *osdc,
115066850df5SIlya Dryomov 			int (*fn)(struct ceph_osd_request *req, void *arg),
115166850df5SIlya Dryomov 			void *arg)
115266850df5SIlya Dryomov {
115366850df5SIlya Dryomov 	struct rb_node *n, *p;
115466850df5SIlya Dryomov 
115566850df5SIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
115666850df5SIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
115766850df5SIlya Dryomov 
115866850df5SIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; ) {
115966850df5SIlya Dryomov 			struct ceph_osd_request *req =
116066850df5SIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
116166850df5SIlya Dryomov 
116266850df5SIlya Dryomov 			p = rb_next(p);
116366850df5SIlya Dryomov 			if (fn(req, arg))
116466850df5SIlya Dryomov 				return;
116566850df5SIlya Dryomov 		}
116666850df5SIlya Dryomov 	}
116766850df5SIlya Dryomov 
116866850df5SIlya Dryomov 	for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
116966850df5SIlya Dryomov 		struct ceph_osd_request *req =
117066850df5SIlya Dryomov 		    rb_entry(p, struct ceph_osd_request, r_node);
117166850df5SIlya Dryomov 
117266850df5SIlya Dryomov 		p = rb_next(p);
117366850df5SIlya Dryomov 		if (fn(req, arg))
117466850df5SIlya Dryomov 			return;
117566850df5SIlya Dryomov 	}
117666850df5SIlya Dryomov }
117766850df5SIlya Dryomov 
11780247a0cfSIlya Dryomov static bool osd_homeless(struct ceph_osd *osd)
11790247a0cfSIlya Dryomov {
11800247a0cfSIlya Dryomov 	return osd->o_osd == CEPH_HOMELESS_OSD;
11810247a0cfSIlya Dryomov }
11820247a0cfSIlya Dryomov 
11835aea3dcdSIlya Dryomov static bool osd_registered(struct ceph_osd *osd)
11843d14c5d2SYehuda Sadeh {
11855aea3dcdSIlya Dryomov 	verify_osdc_locked(osd->o_osdc);
11863d14c5d2SYehuda Sadeh 
11875aea3dcdSIlya Dryomov 	return !RB_EMPTY_NODE(&osd->o_node);
11883d14c5d2SYehuda Sadeh }
11893d14c5d2SYehuda Sadeh 
11903d14c5d2SYehuda Sadeh /*
11910247a0cfSIlya Dryomov  * Assumes @osd is zero-initialized.
11920247a0cfSIlya Dryomov  */
11930247a0cfSIlya Dryomov static void osd_init(struct ceph_osd *osd)
11940247a0cfSIlya Dryomov {
119502113a0fSElena Reshetova 	refcount_set(&osd->o_ref, 1);
11960247a0cfSIlya Dryomov 	RB_CLEAR_NODE(&osd->o_node);
119708b8a044SJeff Layton 	spin_lock_init(&osd->o_requests_lock);
11985aea3dcdSIlya Dryomov 	osd->o_requests = RB_ROOT;
1199922dab61SIlya Dryomov 	osd->o_linger_requests = RB_ROOT;
1200a02a946dSIlya Dryomov 	osd->o_backoff_mappings = RB_ROOT;
1201a02a946dSIlya Dryomov 	osd->o_backoffs_by_id = RB_ROOT;
12020247a0cfSIlya Dryomov 	INIT_LIST_HEAD(&osd->o_osd_lru);
12030247a0cfSIlya Dryomov 	INIT_LIST_HEAD(&osd->o_keepalive_item);
12040247a0cfSIlya Dryomov 	osd->o_incarnation = 1;
12055aea3dcdSIlya Dryomov 	mutex_init(&osd->lock);
12060247a0cfSIlya Dryomov }
12070247a0cfSIlya Dryomov 
1208*f628d799SJeff Layton static void ceph_init_sparse_read(struct ceph_sparse_read *sr)
1209*f628d799SJeff Layton {
1210*f628d799SJeff Layton 	kfree(sr->sr_extent);
1211*f628d799SJeff Layton 	memset(sr, '\0', sizeof(*sr));
1212*f628d799SJeff Layton 	sr->sr_state = CEPH_SPARSE_READ_HDR;
1213*f628d799SJeff Layton }
1214*f628d799SJeff Layton 
12150247a0cfSIlya Dryomov static void osd_cleanup(struct ceph_osd *osd)
12160247a0cfSIlya Dryomov {
12170247a0cfSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
12185aea3dcdSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
1219922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
1220a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
1221a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
12220247a0cfSIlya Dryomov 	WARN_ON(!list_empty(&osd->o_osd_lru));
12230247a0cfSIlya Dryomov 	WARN_ON(!list_empty(&osd->o_keepalive_item));
12240247a0cfSIlya Dryomov 
1225*f628d799SJeff Layton 	ceph_init_sparse_read(&osd->o_sparse_read);
1226*f628d799SJeff Layton 
12270247a0cfSIlya Dryomov 	if (osd->o_auth.authorizer) {
12280247a0cfSIlya Dryomov 		WARN_ON(osd_homeless(osd));
12290247a0cfSIlya Dryomov 		ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
12300247a0cfSIlya Dryomov 	}
12310247a0cfSIlya Dryomov }
12320247a0cfSIlya Dryomov 
12330247a0cfSIlya Dryomov /*
12343d14c5d2SYehuda Sadeh  * Track open sessions with osds.
12353d14c5d2SYehuda Sadeh  */
1236e10006f8SAlex Elder static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
12373d14c5d2SYehuda Sadeh {
12383d14c5d2SYehuda Sadeh 	struct ceph_osd *osd;
12393d14c5d2SYehuda Sadeh 
12400247a0cfSIlya Dryomov 	WARN_ON(onum == CEPH_HOMELESS_OSD);
12410247a0cfSIlya Dryomov 
12427a28f59bSIlya Dryomov 	osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
12430247a0cfSIlya Dryomov 	osd_init(osd);
12443d14c5d2SYehuda Sadeh 	osd->o_osdc = osdc;
1245e10006f8SAlex Elder 	osd->o_osd = onum;
1246*f628d799SJeff Layton 	osd->o_sparse_op_idx = -1;
1247*f628d799SJeff Layton 
1248*f628d799SJeff Layton 	ceph_init_sparse_read(&osd->o_sparse_read);
12493d14c5d2SYehuda Sadeh 
1250b7a9e5ddSSage Weil 	ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
12513d14c5d2SYehuda Sadeh 
12523d14c5d2SYehuda Sadeh 	return osd;
12533d14c5d2SYehuda Sadeh }
12543d14c5d2SYehuda Sadeh 
12553d14c5d2SYehuda Sadeh static struct ceph_osd *get_osd(struct ceph_osd *osd)
12563d14c5d2SYehuda Sadeh {
125702113a0fSElena Reshetova 	if (refcount_inc_not_zero(&osd->o_ref)) {
125802113a0fSElena Reshetova 		dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
125902113a0fSElena Reshetova 		     refcount_read(&osd->o_ref));
12603d14c5d2SYehuda Sadeh 		return osd;
12613d14c5d2SYehuda Sadeh 	} else {
12623d14c5d2SYehuda Sadeh 		dout("get_osd %p FAIL\n", osd);
12633d14c5d2SYehuda Sadeh 		return NULL;
12643d14c5d2SYehuda Sadeh 	}
12653d14c5d2SYehuda Sadeh }
12663d14c5d2SYehuda Sadeh 
12673d14c5d2SYehuda Sadeh static void put_osd(struct ceph_osd *osd)
12683d14c5d2SYehuda Sadeh {
126902113a0fSElena Reshetova 	dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
127002113a0fSElena Reshetova 	     refcount_read(&osd->o_ref) - 1);
127102113a0fSElena Reshetova 	if (refcount_dec_and_test(&osd->o_ref)) {
12720247a0cfSIlya Dryomov 		osd_cleanup(osd);
12733d14c5d2SYehuda Sadeh 		kfree(osd);
12743d14c5d2SYehuda Sadeh 	}
12753d14c5d2SYehuda Sadeh }
12763d14c5d2SYehuda Sadeh 
1277fcd00b68SIlya Dryomov DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1278fcd00b68SIlya Dryomov 
12799dd2845cSIlya Dryomov static void __move_osd_to_lru(struct ceph_osd *osd)
12803d14c5d2SYehuda Sadeh {
12819dd2845cSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
12829dd2845cSIlya Dryomov 
12839dd2845cSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
12843d14c5d2SYehuda Sadeh 	BUG_ON(!list_empty(&osd->o_osd_lru));
1285bbf37ec3SIlya Dryomov 
12869dd2845cSIlya Dryomov 	spin_lock(&osdc->osd_lru_lock);
12873d14c5d2SYehuda Sadeh 	list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
12889dd2845cSIlya Dryomov 	spin_unlock(&osdc->osd_lru_lock);
12899dd2845cSIlya Dryomov 
1290a319bf56SIlya Dryomov 	osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
12913d14c5d2SYehuda Sadeh }
12923d14c5d2SYehuda Sadeh 
12939dd2845cSIlya Dryomov static void maybe_move_osd_to_lru(struct ceph_osd *osd)
1294bbf37ec3SIlya Dryomov {
12955aea3dcdSIlya Dryomov 	if (RB_EMPTY_ROOT(&osd->o_requests) &&
1296922dab61SIlya Dryomov 	    RB_EMPTY_ROOT(&osd->o_linger_requests))
12979dd2845cSIlya Dryomov 		__move_osd_to_lru(osd);
1298bbf37ec3SIlya Dryomov }
1299bbf37ec3SIlya Dryomov 
13003d14c5d2SYehuda Sadeh static void __remove_osd_from_lru(struct ceph_osd *osd)
13013d14c5d2SYehuda Sadeh {
13029dd2845cSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
13039dd2845cSIlya Dryomov 
13049dd2845cSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
13059dd2845cSIlya Dryomov 
13069dd2845cSIlya Dryomov 	spin_lock(&osdc->osd_lru_lock);
13073d14c5d2SYehuda Sadeh 	if (!list_empty(&osd->o_osd_lru))
13083d14c5d2SYehuda Sadeh 		list_del_init(&osd->o_osd_lru);
13099dd2845cSIlya Dryomov 	spin_unlock(&osdc->osd_lru_lock);
13103d14c5d2SYehuda Sadeh }
13113d14c5d2SYehuda Sadeh 
13123d14c5d2SYehuda Sadeh /*
13135aea3dcdSIlya Dryomov  * Close the connection and assign any leftover requests to the
13145aea3dcdSIlya Dryomov  * homeless session.
13155aea3dcdSIlya Dryomov  */
13165aea3dcdSIlya Dryomov static void close_osd(struct ceph_osd *osd)
13175aea3dcdSIlya Dryomov {
13185aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
13195aea3dcdSIlya Dryomov 	struct rb_node *n;
13205aea3dcdSIlya Dryomov 
13215aea3dcdSIlya Dryomov 	verify_osdc_wrlocked(osdc);
13225aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
13235aea3dcdSIlya Dryomov 
13245aea3dcdSIlya Dryomov 	ceph_con_close(&osd->o_con);
13255aea3dcdSIlya Dryomov 
13265aea3dcdSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
13275aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
13285aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
13295aea3dcdSIlya Dryomov 
13305aea3dcdSIlya Dryomov 		n = rb_next(n); /* unlink_request() */
13315aea3dcdSIlya Dryomov 
13325aea3dcdSIlya Dryomov 		dout(" reassigning req %p tid %llu\n", req, req->r_tid);
13335aea3dcdSIlya Dryomov 		unlink_request(osd, req);
13345aea3dcdSIlya Dryomov 		link_request(&osdc->homeless_osd, req);
13355aea3dcdSIlya Dryomov 	}
1336922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; ) {
1337922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
1338922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
1339922dab61SIlya Dryomov 
1340922dab61SIlya Dryomov 		n = rb_next(n); /* unlink_linger() */
1341922dab61SIlya Dryomov 
1342922dab61SIlya Dryomov 		dout(" reassigning lreq %p linger_id %llu\n", lreq,
1343922dab61SIlya Dryomov 		     lreq->linger_id);
1344922dab61SIlya Dryomov 		unlink_linger(osd, lreq);
1345922dab61SIlya Dryomov 		link_linger(&osdc->homeless_osd, lreq);
1346922dab61SIlya Dryomov 	}
1347a02a946dSIlya Dryomov 	clear_backoffs(osd);
13485aea3dcdSIlya Dryomov 
13495aea3dcdSIlya Dryomov 	__remove_osd_from_lru(osd);
13505aea3dcdSIlya Dryomov 	erase_osd(&osdc->osds, osd);
13515aea3dcdSIlya Dryomov 	put_osd(osd);
13525aea3dcdSIlya Dryomov }
13535aea3dcdSIlya Dryomov 
13545aea3dcdSIlya Dryomov /*
13553d14c5d2SYehuda Sadeh  * reset osd connect
13563d14c5d2SYehuda Sadeh  */
13575aea3dcdSIlya Dryomov static int reopen_osd(struct ceph_osd *osd)
13583d14c5d2SYehuda Sadeh {
1359c3acb181SAlex Elder 	struct ceph_entity_addr *peer_addr;
13603d14c5d2SYehuda Sadeh 
13615aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
13625aea3dcdSIlya Dryomov 
13635aea3dcdSIlya Dryomov 	if (RB_EMPTY_ROOT(&osd->o_requests) &&
1364922dab61SIlya Dryomov 	    RB_EMPTY_ROOT(&osd->o_linger_requests)) {
13655aea3dcdSIlya Dryomov 		close_osd(osd);
1366c3acb181SAlex Elder 		return -ENODEV;
1367c3acb181SAlex Elder 	}
1368c3acb181SAlex Elder 
13695aea3dcdSIlya Dryomov 	peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
1370c3acb181SAlex Elder 	if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
13713d14c5d2SYehuda Sadeh 			!ceph_con_opened(&osd->o_con)) {
13725aea3dcdSIlya Dryomov 		struct rb_node *n;
1373c3acb181SAlex Elder 
13743d14c5d2SYehuda Sadeh 		dout("osd addr hasn't changed and connection never opened, "
13750b4af2e8SIlya Dryomov 		     "letting msgr retry\n");
13763d14c5d2SYehuda Sadeh 		/* touch each r_stamp for handle_timeout()'s benfit */
13775aea3dcdSIlya Dryomov 		for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
13785aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
13795aea3dcdSIlya Dryomov 			    rb_entry(n, struct ceph_osd_request, r_node);
13803d14c5d2SYehuda Sadeh 			req->r_stamp = jiffies;
13815aea3dcdSIlya Dryomov 		}
1382c3acb181SAlex Elder 
1383c3acb181SAlex Elder 		return -EAGAIN;
13843d14c5d2SYehuda Sadeh 	}
1385c3acb181SAlex Elder 
1386c3acb181SAlex Elder 	ceph_con_close(&osd->o_con);
1387c3acb181SAlex Elder 	ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1388c3acb181SAlex Elder 	osd->o_incarnation++;
1389c3acb181SAlex Elder 
1390c3acb181SAlex Elder 	return 0;
13913d14c5d2SYehuda Sadeh }
13923d14c5d2SYehuda Sadeh 
13935aea3dcdSIlya Dryomov static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
13945aea3dcdSIlya Dryomov 					  bool wrlocked)
13953d14c5d2SYehuda Sadeh {
13965aea3dcdSIlya Dryomov 	struct ceph_osd *osd;
13975aea3dcdSIlya Dryomov 
13985aea3dcdSIlya Dryomov 	if (wrlocked)
13995aea3dcdSIlya Dryomov 		verify_osdc_wrlocked(osdc);
14005aea3dcdSIlya Dryomov 	else
14015aea3dcdSIlya Dryomov 		verify_osdc_locked(osdc);
14025aea3dcdSIlya Dryomov 
14035aea3dcdSIlya Dryomov 	if (o != CEPH_HOMELESS_OSD)
14045aea3dcdSIlya Dryomov 		osd = lookup_osd(&osdc->osds, o);
14055aea3dcdSIlya Dryomov 	else
14065aea3dcdSIlya Dryomov 		osd = &osdc->homeless_osd;
14075aea3dcdSIlya Dryomov 	if (!osd) {
14085aea3dcdSIlya Dryomov 		if (!wrlocked)
14095aea3dcdSIlya Dryomov 			return ERR_PTR(-EAGAIN);
14105aea3dcdSIlya Dryomov 
14115aea3dcdSIlya Dryomov 		osd = create_osd(osdc, o);
14125aea3dcdSIlya Dryomov 		insert_osd(&osdc->osds, osd);
14135aea3dcdSIlya Dryomov 		ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
14145aea3dcdSIlya Dryomov 			      &osdc->osdmap->osd_addr[osd->o_osd]);
14155aea3dcdSIlya Dryomov 	}
14165aea3dcdSIlya Dryomov 
14175aea3dcdSIlya Dryomov 	dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
14185aea3dcdSIlya Dryomov 	return osd;
1419a40c4f10SYehuda Sadeh }
1420a40c4f10SYehuda Sadeh 
14213d14c5d2SYehuda Sadeh /*
14225aea3dcdSIlya Dryomov  * Create request <-> OSD session relation.
14235aea3dcdSIlya Dryomov  *
14245aea3dcdSIlya Dryomov  * @req has to be assigned a tid, @osd may be homeless.
14253d14c5d2SYehuda Sadeh  */
14265aea3dcdSIlya Dryomov static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
14273d14c5d2SYehuda Sadeh {
14285aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
14295aea3dcdSIlya Dryomov 	WARN_ON(!req->r_tid || req->r_osd);
14305aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
143135f9f8a0SSage Weil 	     req, req->r_tid);
14325aea3dcdSIlya Dryomov 
14335aea3dcdSIlya Dryomov 	if (!osd_homeless(osd))
14345aea3dcdSIlya Dryomov 		__remove_osd_from_lru(osd);
14355aea3dcdSIlya Dryomov 	else
14365aea3dcdSIlya Dryomov 		atomic_inc(&osd->o_osdc->num_homeless);
14375aea3dcdSIlya Dryomov 
14385aea3dcdSIlya Dryomov 	get_osd(osd);
143908b8a044SJeff Layton 	spin_lock(&osd->o_requests_lock);
14405aea3dcdSIlya Dryomov 	insert_request(&osd->o_requests, req);
144108b8a044SJeff Layton 	spin_unlock(&osd->o_requests_lock);
14425aea3dcdSIlya Dryomov 	req->r_osd = osd;
144335f9f8a0SSage Weil }
144435f9f8a0SSage Weil 
14455aea3dcdSIlya Dryomov static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
14463d14c5d2SYehuda Sadeh {
14475aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
14485aea3dcdSIlya Dryomov 	WARN_ON(req->r_osd != osd);
14495aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
14505aea3dcdSIlya Dryomov 	     req, req->r_tid);
14515aea3dcdSIlya Dryomov 
14525aea3dcdSIlya Dryomov 	req->r_osd = NULL;
145308b8a044SJeff Layton 	spin_lock(&osd->o_requests_lock);
14545aea3dcdSIlya Dryomov 	erase_request(&osd->o_requests, req);
145508b8a044SJeff Layton 	spin_unlock(&osd->o_requests_lock);
14565aea3dcdSIlya Dryomov 	put_osd(osd);
14575aea3dcdSIlya Dryomov 
14585aea3dcdSIlya Dryomov 	if (!osd_homeless(osd))
14595aea3dcdSIlya Dryomov 		maybe_move_osd_to_lru(osd);
14605aea3dcdSIlya Dryomov 	else
14615aea3dcdSIlya Dryomov 		atomic_dec(&osd->o_osdc->num_homeless);
14623d14c5d2SYehuda Sadeh }
14633d14c5d2SYehuda Sadeh 
146463244fa1SIlya Dryomov static bool __pool_full(struct ceph_pg_pool_info *pi)
146563244fa1SIlya Dryomov {
146663244fa1SIlya Dryomov 	return pi->flags & CEPH_POOL_FLAG_FULL;
146763244fa1SIlya Dryomov }
146863244fa1SIlya Dryomov 
146942c1b124SIlya Dryomov static bool have_pool_full(struct ceph_osd_client *osdc)
147042c1b124SIlya Dryomov {
147142c1b124SIlya Dryomov 	struct rb_node *n;
147242c1b124SIlya Dryomov 
147342c1b124SIlya Dryomov 	for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
147442c1b124SIlya Dryomov 		struct ceph_pg_pool_info *pi =
147542c1b124SIlya Dryomov 		    rb_entry(n, struct ceph_pg_pool_info, node);
147642c1b124SIlya Dryomov 
147742c1b124SIlya Dryomov 		if (__pool_full(pi))
147842c1b124SIlya Dryomov 			return true;
147942c1b124SIlya Dryomov 	}
148042c1b124SIlya Dryomov 
148142c1b124SIlya Dryomov 	return false;
148242c1b124SIlya Dryomov }
148342c1b124SIlya Dryomov 
14845aea3dcdSIlya Dryomov static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
14855aea3dcdSIlya Dryomov {
14865aea3dcdSIlya Dryomov 	struct ceph_pg_pool_info *pi;
14875aea3dcdSIlya Dryomov 
14885aea3dcdSIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
14895aea3dcdSIlya Dryomov 	if (!pi)
14905aea3dcdSIlya Dryomov 		return false;
14915aea3dcdSIlya Dryomov 
14925aea3dcdSIlya Dryomov 	return __pool_full(pi);
14935aea3dcdSIlya Dryomov }
14945aea3dcdSIlya Dryomov 
14953d14c5d2SYehuda Sadeh /*
1496d29adb34SJosh Durgin  * Returns whether a request should be blocked from being sent
1497d29adb34SJosh Durgin  * based on the current osdmap and osd_client settings.
1498d29adb34SJosh Durgin  */
149963244fa1SIlya Dryomov static bool target_should_be_paused(struct ceph_osd_client *osdc,
150063244fa1SIlya Dryomov 				    const struct ceph_osd_request_target *t,
150163244fa1SIlya Dryomov 				    struct ceph_pg_pool_info *pi)
150263244fa1SIlya Dryomov {
1503b7ec35b3SIlya Dryomov 	bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1504b7ec35b3SIlya Dryomov 	bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1505b7ec35b3SIlya Dryomov 		       ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
150663244fa1SIlya Dryomov 		       __pool_full(pi);
150763244fa1SIlya Dryomov 
15086d637a54SIlya Dryomov 	WARN_ON(pi->id != t->target_oloc.pool);
150958eb7932SJeff Layton 	return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
151058eb7932SJeff Layton 	       ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
151158eb7932SJeff Layton 	       (osdc->osdmap->epoch < osdc->epoch_barrier);
151263244fa1SIlya Dryomov }
151363244fa1SIlya Dryomov 
1514117d96a0SIlya Dryomov static int pick_random_replica(const struct ceph_osds *acting)
1515117d96a0SIlya Dryomov {
15168032bf12SJason A. Donenfeld 	int i = get_random_u32_below(acting->size);
1517117d96a0SIlya Dryomov 
1518117d96a0SIlya Dryomov 	dout("%s picked osd%d, primary osd%d\n", __func__,
1519117d96a0SIlya Dryomov 	     acting->osds[i], acting->primary);
1520117d96a0SIlya Dryomov 	return i;
1521117d96a0SIlya Dryomov }
1522117d96a0SIlya Dryomov 
1523117d96a0SIlya Dryomov /*
1524117d96a0SIlya Dryomov  * Picks the closest replica based on client's location given by
1525117d96a0SIlya Dryomov  * crush_location option.  Prefers the primary if the locality is
1526117d96a0SIlya Dryomov  * the same.
1527117d96a0SIlya Dryomov  */
1528117d96a0SIlya Dryomov static int pick_closest_replica(struct ceph_osd_client *osdc,
1529117d96a0SIlya Dryomov 				const struct ceph_osds *acting)
1530117d96a0SIlya Dryomov {
1531117d96a0SIlya Dryomov 	struct ceph_options *opt = osdc->client->options;
1532117d96a0SIlya Dryomov 	int best_i, best_locality;
1533117d96a0SIlya Dryomov 	int i = 0, locality;
1534117d96a0SIlya Dryomov 
1535117d96a0SIlya Dryomov 	do {
1536117d96a0SIlya Dryomov 		locality = ceph_get_crush_locality(osdc->osdmap,
1537117d96a0SIlya Dryomov 						   acting->osds[i],
1538117d96a0SIlya Dryomov 						   &opt->crush_locs);
1539117d96a0SIlya Dryomov 		if (i == 0 ||
1540117d96a0SIlya Dryomov 		    (locality >= 0 && best_locality < 0) ||
1541117d96a0SIlya Dryomov 		    (locality >= 0 && best_locality >= 0 &&
1542117d96a0SIlya Dryomov 		     locality < best_locality)) {
1543117d96a0SIlya Dryomov 			best_i = i;
1544117d96a0SIlya Dryomov 			best_locality = locality;
1545117d96a0SIlya Dryomov 		}
1546117d96a0SIlya Dryomov 	} while (++i < acting->size);
1547117d96a0SIlya Dryomov 
1548117d96a0SIlya Dryomov 	dout("%s picked osd%d with locality %d, primary osd%d\n", __func__,
1549117d96a0SIlya Dryomov 	     acting->osds[best_i], best_locality, acting->primary);
1550117d96a0SIlya Dryomov 	return best_i;
1551117d96a0SIlya Dryomov }
1552117d96a0SIlya Dryomov 
155363244fa1SIlya Dryomov enum calc_target_result {
155463244fa1SIlya Dryomov 	CALC_TARGET_NO_ACTION = 0,
155563244fa1SIlya Dryomov 	CALC_TARGET_NEED_RESEND,
155663244fa1SIlya Dryomov 	CALC_TARGET_POOL_DNE,
155763244fa1SIlya Dryomov };
155863244fa1SIlya Dryomov 
155963244fa1SIlya Dryomov static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
156063244fa1SIlya Dryomov 					   struct ceph_osd_request_target *t,
156163244fa1SIlya Dryomov 					   bool any_change)
156263244fa1SIlya Dryomov {
156363244fa1SIlya Dryomov 	struct ceph_pg_pool_info *pi;
156463244fa1SIlya Dryomov 	struct ceph_pg pgid, last_pgid;
156563244fa1SIlya Dryomov 	struct ceph_osds up, acting;
1566117d96a0SIlya Dryomov 	bool is_read = t->flags & CEPH_OSD_FLAG_READ;
1567117d96a0SIlya Dryomov 	bool is_write = t->flags & CEPH_OSD_FLAG_WRITE;
156863244fa1SIlya Dryomov 	bool force_resend = false;
156984ed45dfSIlya Dryomov 	bool unpaused = false;
1570a5613724SIlya Dryomov 	bool legacy_change = false;
15717de030d6SIlya Dryomov 	bool split = false;
1572b7ec35b3SIlya Dryomov 	bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
1573ae78dd81SIlya Dryomov 	bool recovery_deletes = ceph_osdmap_flag(osdc,
1574ae78dd81SIlya Dryomov 						 CEPH_OSDMAP_RECOVERY_DELETES);
157563244fa1SIlya Dryomov 	enum calc_target_result ct_res;
157663244fa1SIlya Dryomov 
157704c7d789SIlya Dryomov 	t->epoch = osdc->osdmap->epoch;
157863244fa1SIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
157963244fa1SIlya Dryomov 	if (!pi) {
158063244fa1SIlya Dryomov 		t->osd = CEPH_HOMELESS_OSD;
158163244fa1SIlya Dryomov 		ct_res = CALC_TARGET_POOL_DNE;
158263244fa1SIlya Dryomov 		goto out;
158363244fa1SIlya Dryomov 	}
158463244fa1SIlya Dryomov 
158563244fa1SIlya Dryomov 	if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1586dc93e0e2SIlya Dryomov 		if (t->last_force_resend < pi->last_force_request_resend) {
1587dc93e0e2SIlya Dryomov 			t->last_force_resend = pi->last_force_request_resend;
158863244fa1SIlya Dryomov 			force_resend = true;
1589dc93e0e2SIlya Dryomov 		} else if (t->last_force_resend == 0) {
159063244fa1SIlya Dryomov 			force_resend = true;
159163244fa1SIlya Dryomov 		}
159263244fa1SIlya Dryomov 	}
159363244fa1SIlya Dryomov 
1594db098ec4SIlya Dryomov 	/* apply tiering */
1595db098ec4SIlya Dryomov 	ceph_oid_copy(&t->target_oid, &t->base_oid);
1596db098ec4SIlya Dryomov 	ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1597db098ec4SIlya Dryomov 	if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1598117d96a0SIlya Dryomov 		if (is_read && pi->read_tier >= 0)
159963244fa1SIlya Dryomov 			t->target_oloc.pool = pi->read_tier;
1600117d96a0SIlya Dryomov 		if (is_write && pi->write_tier >= 0)
160163244fa1SIlya Dryomov 			t->target_oloc.pool = pi->write_tier;
16026d637a54SIlya Dryomov 
16036d637a54SIlya Dryomov 		pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
16046d637a54SIlya Dryomov 		if (!pi) {
16056d637a54SIlya Dryomov 			t->osd = CEPH_HOMELESS_OSD;
16066d637a54SIlya Dryomov 			ct_res = CALC_TARGET_POOL_DNE;
16076d637a54SIlya Dryomov 			goto out;
16086d637a54SIlya Dryomov 		}
160963244fa1SIlya Dryomov 	}
161063244fa1SIlya Dryomov 
1611a86f009fSIlya Dryomov 	__ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc, &pgid);
161263244fa1SIlya Dryomov 	last_pgid.pool = pgid.pool;
161363244fa1SIlya Dryomov 	last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
161463244fa1SIlya Dryomov 
1615df28152dSIlya Dryomov 	ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
161663244fa1SIlya Dryomov 	if (any_change &&
161763244fa1SIlya Dryomov 	    ceph_is_new_interval(&t->acting,
161863244fa1SIlya Dryomov 				 &acting,
161963244fa1SIlya Dryomov 				 &t->up,
162063244fa1SIlya Dryomov 				 &up,
162163244fa1SIlya Dryomov 				 t->size,
162263244fa1SIlya Dryomov 				 pi->size,
162363244fa1SIlya Dryomov 				 t->min_size,
162463244fa1SIlya Dryomov 				 pi->min_size,
162563244fa1SIlya Dryomov 				 t->pg_num,
162663244fa1SIlya Dryomov 				 pi->pg_num,
162763244fa1SIlya Dryomov 				 t->sort_bitwise,
162863244fa1SIlya Dryomov 				 sort_bitwise,
1629ae78dd81SIlya Dryomov 				 t->recovery_deletes,
1630ae78dd81SIlya Dryomov 				 recovery_deletes,
163163244fa1SIlya Dryomov 				 &last_pgid))
163263244fa1SIlya Dryomov 		force_resend = true;
163363244fa1SIlya Dryomov 
163463244fa1SIlya Dryomov 	if (t->paused && !target_should_be_paused(osdc, t, pi)) {
163563244fa1SIlya Dryomov 		t->paused = false;
163684ed45dfSIlya Dryomov 		unpaused = true;
163763244fa1SIlya Dryomov 	}
163884ed45dfSIlya Dryomov 	legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
1639117d96a0SIlya Dryomov 			ceph_osds_changed(&t->acting, &acting,
1640117d96a0SIlya Dryomov 					  t->used_replica || any_change);
16417de030d6SIlya Dryomov 	if (t->pg_num)
16427de030d6SIlya Dryomov 		split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
164363244fa1SIlya Dryomov 
16447de030d6SIlya Dryomov 	if (legacy_change || force_resend || split) {
164563244fa1SIlya Dryomov 		t->pgid = pgid; /* struct */
1646df28152dSIlya Dryomov 		ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
164763244fa1SIlya Dryomov 		ceph_osds_copy(&t->acting, &acting);
164863244fa1SIlya Dryomov 		ceph_osds_copy(&t->up, &up);
164963244fa1SIlya Dryomov 		t->size = pi->size;
165063244fa1SIlya Dryomov 		t->min_size = pi->min_size;
165163244fa1SIlya Dryomov 		t->pg_num = pi->pg_num;
165263244fa1SIlya Dryomov 		t->pg_num_mask = pi->pg_num_mask;
165363244fa1SIlya Dryomov 		t->sort_bitwise = sort_bitwise;
1654ae78dd81SIlya Dryomov 		t->recovery_deletes = recovery_deletes;
165563244fa1SIlya Dryomov 
1656117d96a0SIlya Dryomov 		if ((t->flags & (CEPH_OSD_FLAG_BALANCE_READS |
1657117d96a0SIlya Dryomov 				 CEPH_OSD_FLAG_LOCALIZE_READS)) &&
1658117d96a0SIlya Dryomov 		    !is_write && pi->type == CEPH_POOL_TYPE_REP &&
1659117d96a0SIlya Dryomov 		    acting.size > 1) {
1660117d96a0SIlya Dryomov 			int pos;
1661117d96a0SIlya Dryomov 
1662117d96a0SIlya Dryomov 			WARN_ON(!is_read || acting.osds[0] != acting.primary);
1663117d96a0SIlya Dryomov 			if (t->flags & CEPH_OSD_FLAG_BALANCE_READS) {
1664117d96a0SIlya Dryomov 				pos = pick_random_replica(&acting);
1665117d96a0SIlya Dryomov 			} else {
1666117d96a0SIlya Dryomov 				pos = pick_closest_replica(osdc, &acting);
1667117d96a0SIlya Dryomov 			}
1668117d96a0SIlya Dryomov 			t->osd = acting.osds[pos];
1669117d96a0SIlya Dryomov 			t->used_replica = pos > 0;
1670117d96a0SIlya Dryomov 		} else {
167163244fa1SIlya Dryomov 			t->osd = acting.primary;
1672117d96a0SIlya Dryomov 			t->used_replica = false;
1673117d96a0SIlya Dryomov 		}
167463244fa1SIlya Dryomov 	}
167563244fa1SIlya Dryomov 
1676a5613724SIlya Dryomov 	if (unpaused || legacy_change || force_resend || split)
167784ed45dfSIlya Dryomov 		ct_res = CALC_TARGET_NEED_RESEND;
167884ed45dfSIlya Dryomov 	else
167984ed45dfSIlya Dryomov 		ct_res = CALC_TARGET_NO_ACTION;
168084ed45dfSIlya Dryomov 
168163244fa1SIlya Dryomov out:
1682a5613724SIlya Dryomov 	dout("%s t %p -> %d%d%d%d ct_res %d osd%d\n", __func__, t, unpaused,
1683a5613724SIlya Dryomov 	     legacy_change, force_resend, split, ct_res, t->osd);
168463244fa1SIlya Dryomov 	return ct_res;
168563244fa1SIlya Dryomov }
168663244fa1SIlya Dryomov 
1687a02a946dSIlya Dryomov static struct ceph_spg_mapping *alloc_spg_mapping(void)
1688a02a946dSIlya Dryomov {
1689a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
1690a02a946dSIlya Dryomov 
1691a02a946dSIlya Dryomov 	spg = kmalloc(sizeof(*spg), GFP_NOIO);
1692a02a946dSIlya Dryomov 	if (!spg)
1693a02a946dSIlya Dryomov 		return NULL;
1694a02a946dSIlya Dryomov 
1695a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&spg->node);
1696a02a946dSIlya Dryomov 	spg->backoffs = RB_ROOT;
1697a02a946dSIlya Dryomov 	return spg;
1698a02a946dSIlya Dryomov }
1699a02a946dSIlya Dryomov 
1700a02a946dSIlya Dryomov static void free_spg_mapping(struct ceph_spg_mapping *spg)
1701a02a946dSIlya Dryomov {
1702a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&spg->node));
1703a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
1704a02a946dSIlya Dryomov 
1705a02a946dSIlya Dryomov 	kfree(spg);
1706a02a946dSIlya Dryomov }
1707a02a946dSIlya Dryomov 
1708a02a946dSIlya Dryomov /*
1709a02a946dSIlya Dryomov  * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
1710a02a946dSIlya Dryomov  * ceph_pg_mapping.  Used to track OSD backoffs -- a backoff [range] is
1711a02a946dSIlya Dryomov  * defined only within a specific spgid; it does not pass anything to
1712a02a946dSIlya Dryomov  * children on split, or to another primary.
1713a02a946dSIlya Dryomov  */
1714a02a946dSIlya Dryomov DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
1715a02a946dSIlya Dryomov 		 RB_BYPTR, const struct ceph_spg *, node)
1716a02a946dSIlya Dryomov 
1717a02a946dSIlya Dryomov static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
1718a02a946dSIlya Dryomov {
1719a02a946dSIlya Dryomov 	return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
1720a02a946dSIlya Dryomov }
1721a02a946dSIlya Dryomov 
1722a02a946dSIlya Dryomov static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
1723a02a946dSIlya Dryomov 				   void **pkey, size_t *pkey_len)
1724a02a946dSIlya Dryomov {
1725a02a946dSIlya Dryomov 	if (hoid->key_len) {
1726a02a946dSIlya Dryomov 		*pkey = hoid->key;
1727a02a946dSIlya Dryomov 		*pkey_len = hoid->key_len;
1728a02a946dSIlya Dryomov 	} else {
1729a02a946dSIlya Dryomov 		*pkey = hoid->oid;
1730a02a946dSIlya Dryomov 		*pkey_len = hoid->oid_len;
1731a02a946dSIlya Dryomov 	}
1732a02a946dSIlya Dryomov }
1733a02a946dSIlya Dryomov 
1734a02a946dSIlya Dryomov static int compare_names(const void *name1, size_t name1_len,
1735a02a946dSIlya Dryomov 			 const void *name2, size_t name2_len)
1736a02a946dSIlya Dryomov {
1737a02a946dSIlya Dryomov 	int ret;
1738a02a946dSIlya Dryomov 
1739a02a946dSIlya Dryomov 	ret = memcmp(name1, name2, min(name1_len, name2_len));
1740a02a946dSIlya Dryomov 	if (!ret) {
1741a02a946dSIlya Dryomov 		if (name1_len < name2_len)
1742a02a946dSIlya Dryomov 			ret = -1;
1743a02a946dSIlya Dryomov 		else if (name1_len > name2_len)
1744a02a946dSIlya Dryomov 			ret = 1;
1745a02a946dSIlya Dryomov 	}
1746a02a946dSIlya Dryomov 	return ret;
1747a02a946dSIlya Dryomov }
1748a02a946dSIlya Dryomov 
1749a02a946dSIlya Dryomov static int hoid_compare(const struct ceph_hobject_id *lhs,
1750a02a946dSIlya Dryomov 			const struct ceph_hobject_id *rhs)
1751a02a946dSIlya Dryomov {
1752a02a946dSIlya Dryomov 	void *effective_key1, *effective_key2;
1753a02a946dSIlya Dryomov 	size_t effective_key1_len, effective_key2_len;
1754a02a946dSIlya Dryomov 	int ret;
1755a02a946dSIlya Dryomov 
1756a02a946dSIlya Dryomov 	if (lhs->is_max < rhs->is_max)
1757a02a946dSIlya Dryomov 		return -1;
1758a02a946dSIlya Dryomov 	if (lhs->is_max > rhs->is_max)
1759a02a946dSIlya Dryomov 		return 1;
1760a02a946dSIlya Dryomov 
1761a02a946dSIlya Dryomov 	if (lhs->pool < rhs->pool)
1762a02a946dSIlya Dryomov 		return -1;
1763a02a946dSIlya Dryomov 	if (lhs->pool > rhs->pool)
1764a02a946dSIlya Dryomov 		return 1;
1765a02a946dSIlya Dryomov 
1766a02a946dSIlya Dryomov 	if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
1767a02a946dSIlya Dryomov 		return -1;
1768a02a946dSIlya Dryomov 	if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
1769a02a946dSIlya Dryomov 		return 1;
1770a02a946dSIlya Dryomov 
1771a02a946dSIlya Dryomov 	ret = compare_names(lhs->nspace, lhs->nspace_len,
1772a02a946dSIlya Dryomov 			    rhs->nspace, rhs->nspace_len);
1773a02a946dSIlya Dryomov 	if (ret)
1774a02a946dSIlya Dryomov 		return ret;
1775a02a946dSIlya Dryomov 
1776a02a946dSIlya Dryomov 	hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
1777a02a946dSIlya Dryomov 	hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
1778a02a946dSIlya Dryomov 	ret = compare_names(effective_key1, effective_key1_len,
1779a02a946dSIlya Dryomov 			    effective_key2, effective_key2_len);
1780a02a946dSIlya Dryomov 	if (ret)
1781a02a946dSIlya Dryomov 		return ret;
1782a02a946dSIlya Dryomov 
1783a02a946dSIlya Dryomov 	ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
1784a02a946dSIlya Dryomov 	if (ret)
1785a02a946dSIlya Dryomov 		return ret;
1786a02a946dSIlya Dryomov 
1787a02a946dSIlya Dryomov 	if (lhs->snapid < rhs->snapid)
1788a02a946dSIlya Dryomov 		return -1;
1789a02a946dSIlya Dryomov 	if (lhs->snapid > rhs->snapid)
1790a02a946dSIlya Dryomov 		return 1;
1791a02a946dSIlya Dryomov 
1792a02a946dSIlya Dryomov 	return 0;
1793a02a946dSIlya Dryomov }
1794a02a946dSIlya Dryomov 
1795a02a946dSIlya Dryomov /*
1796a02a946dSIlya Dryomov  * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
1797a02a946dSIlya Dryomov  * compat stuff here.
1798a02a946dSIlya Dryomov  *
1799a02a946dSIlya Dryomov  * Assumes @hoid is zero-initialized.
1800a02a946dSIlya Dryomov  */
1801a02a946dSIlya Dryomov static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
1802a02a946dSIlya Dryomov {
1803a02a946dSIlya Dryomov 	u8 struct_v;
1804a02a946dSIlya Dryomov 	u32 struct_len;
1805a02a946dSIlya Dryomov 	int ret;
1806a02a946dSIlya Dryomov 
1807a02a946dSIlya Dryomov 	ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
1808a02a946dSIlya Dryomov 				  &struct_len);
1809a02a946dSIlya Dryomov 	if (ret)
1810a02a946dSIlya Dryomov 		return ret;
1811a02a946dSIlya Dryomov 
1812a02a946dSIlya Dryomov 	if (struct_v < 4) {
1813a02a946dSIlya Dryomov 		pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
1814a02a946dSIlya Dryomov 		goto e_inval;
1815a02a946dSIlya Dryomov 	}
1816a02a946dSIlya Dryomov 
1817a02a946dSIlya Dryomov 	hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
1818a02a946dSIlya Dryomov 						GFP_NOIO);
1819a02a946dSIlya Dryomov 	if (IS_ERR(hoid->key)) {
1820a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->key);
1821a02a946dSIlya Dryomov 		hoid->key = NULL;
1822a02a946dSIlya Dryomov 		return ret;
1823a02a946dSIlya Dryomov 	}
1824a02a946dSIlya Dryomov 
1825a02a946dSIlya Dryomov 	hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
1826a02a946dSIlya Dryomov 						GFP_NOIO);
1827a02a946dSIlya Dryomov 	if (IS_ERR(hoid->oid)) {
1828a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->oid);
1829a02a946dSIlya Dryomov 		hoid->oid = NULL;
1830a02a946dSIlya Dryomov 		return ret;
1831a02a946dSIlya Dryomov 	}
1832a02a946dSIlya Dryomov 
1833a02a946dSIlya Dryomov 	ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
1834a02a946dSIlya Dryomov 	ceph_decode_32_safe(p, end, hoid->hash, e_inval);
1835a02a946dSIlya Dryomov 	ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
1836a02a946dSIlya Dryomov 
1837a02a946dSIlya Dryomov 	hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
1838a02a946dSIlya Dryomov 						   GFP_NOIO);
1839a02a946dSIlya Dryomov 	if (IS_ERR(hoid->nspace)) {
1840a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->nspace);
1841a02a946dSIlya Dryomov 		hoid->nspace = NULL;
1842a02a946dSIlya Dryomov 		return ret;
1843a02a946dSIlya Dryomov 	}
1844a02a946dSIlya Dryomov 
1845a02a946dSIlya Dryomov 	ceph_decode_64_safe(p, end, hoid->pool, e_inval);
1846a02a946dSIlya Dryomov 
1847a02a946dSIlya Dryomov 	ceph_hoid_build_hash_cache(hoid);
1848a02a946dSIlya Dryomov 	return 0;
1849a02a946dSIlya Dryomov 
1850a02a946dSIlya Dryomov e_inval:
1851a02a946dSIlya Dryomov 	return -EINVAL;
1852a02a946dSIlya Dryomov }
1853a02a946dSIlya Dryomov 
1854a02a946dSIlya Dryomov static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
1855a02a946dSIlya Dryomov {
1856a02a946dSIlya Dryomov 	return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
1857a02a946dSIlya Dryomov 	       4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
1858a02a946dSIlya Dryomov }
1859a02a946dSIlya Dryomov 
1860a02a946dSIlya Dryomov static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
1861a02a946dSIlya Dryomov {
1862a02a946dSIlya Dryomov 	ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
1863a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->key, hoid->key_len);
1864a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
1865a02a946dSIlya Dryomov 	ceph_encode_64(p, hoid->snapid);
1866a02a946dSIlya Dryomov 	ceph_encode_32(p, hoid->hash);
1867a02a946dSIlya Dryomov 	ceph_encode_8(p, hoid->is_max);
1868a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
1869a02a946dSIlya Dryomov 	ceph_encode_64(p, hoid->pool);
1870a02a946dSIlya Dryomov }
1871a02a946dSIlya Dryomov 
1872a02a946dSIlya Dryomov static void free_hoid(struct ceph_hobject_id *hoid)
1873a02a946dSIlya Dryomov {
1874a02a946dSIlya Dryomov 	if (hoid) {
1875a02a946dSIlya Dryomov 		kfree(hoid->key);
1876a02a946dSIlya Dryomov 		kfree(hoid->oid);
1877a02a946dSIlya Dryomov 		kfree(hoid->nspace);
1878a02a946dSIlya Dryomov 		kfree(hoid);
1879a02a946dSIlya Dryomov 	}
1880a02a946dSIlya Dryomov }
1881a02a946dSIlya Dryomov 
1882a02a946dSIlya Dryomov static struct ceph_osd_backoff *alloc_backoff(void)
1883a02a946dSIlya Dryomov {
1884a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
1885a02a946dSIlya Dryomov 
1886a02a946dSIlya Dryomov 	backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
1887a02a946dSIlya Dryomov 	if (!backoff)
1888a02a946dSIlya Dryomov 		return NULL;
1889a02a946dSIlya Dryomov 
1890a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&backoff->spg_node);
1891a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&backoff->id_node);
1892a02a946dSIlya Dryomov 	return backoff;
1893a02a946dSIlya Dryomov }
1894a02a946dSIlya Dryomov 
1895a02a946dSIlya Dryomov static void free_backoff(struct ceph_osd_backoff *backoff)
1896a02a946dSIlya Dryomov {
1897a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
1898a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
1899a02a946dSIlya Dryomov 
1900a02a946dSIlya Dryomov 	free_hoid(backoff->begin);
1901a02a946dSIlya Dryomov 	free_hoid(backoff->end);
1902a02a946dSIlya Dryomov 	kfree(backoff);
1903a02a946dSIlya Dryomov }
1904a02a946dSIlya Dryomov 
1905a02a946dSIlya Dryomov /*
1906a02a946dSIlya Dryomov  * Within a specific spgid, backoffs are managed by ->begin hoid.
1907a02a946dSIlya Dryomov  */
1908a02a946dSIlya Dryomov DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
1909a02a946dSIlya Dryomov 			RB_BYVAL, spg_node);
1910a02a946dSIlya Dryomov 
1911a02a946dSIlya Dryomov static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
1912a02a946dSIlya Dryomov 					    const struct ceph_hobject_id *hoid)
1913a02a946dSIlya Dryomov {
1914a02a946dSIlya Dryomov 	struct rb_node *n = root->rb_node;
1915a02a946dSIlya Dryomov 
1916a02a946dSIlya Dryomov 	while (n) {
1917a02a946dSIlya Dryomov 		struct ceph_osd_backoff *cur =
1918a02a946dSIlya Dryomov 		    rb_entry(n, struct ceph_osd_backoff, spg_node);
1919a02a946dSIlya Dryomov 		int cmp;
1920a02a946dSIlya Dryomov 
1921a02a946dSIlya Dryomov 		cmp = hoid_compare(hoid, cur->begin);
1922a02a946dSIlya Dryomov 		if (cmp < 0) {
1923a02a946dSIlya Dryomov 			n = n->rb_left;
1924a02a946dSIlya Dryomov 		} else if (cmp > 0) {
1925a02a946dSIlya Dryomov 			if (hoid_compare(hoid, cur->end) < 0)
1926a02a946dSIlya Dryomov 				return cur;
1927a02a946dSIlya Dryomov 
1928a02a946dSIlya Dryomov 			n = n->rb_right;
1929a02a946dSIlya Dryomov 		} else {
1930a02a946dSIlya Dryomov 			return cur;
1931a02a946dSIlya Dryomov 		}
1932a02a946dSIlya Dryomov 	}
1933a02a946dSIlya Dryomov 
1934a02a946dSIlya Dryomov 	return NULL;
1935a02a946dSIlya Dryomov }
1936a02a946dSIlya Dryomov 
1937a02a946dSIlya Dryomov /*
1938a02a946dSIlya Dryomov  * Each backoff has a unique id within its OSD session.
1939a02a946dSIlya Dryomov  */
1940a02a946dSIlya Dryomov DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
1941a02a946dSIlya Dryomov 
1942a02a946dSIlya Dryomov static void clear_backoffs(struct ceph_osd *osd)
1943a02a946dSIlya Dryomov {
1944a02a946dSIlya Dryomov 	while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
1945a02a946dSIlya Dryomov 		struct ceph_spg_mapping *spg =
1946a02a946dSIlya Dryomov 		    rb_entry(rb_first(&osd->o_backoff_mappings),
1947a02a946dSIlya Dryomov 			     struct ceph_spg_mapping, node);
1948a02a946dSIlya Dryomov 
1949a02a946dSIlya Dryomov 		while (!RB_EMPTY_ROOT(&spg->backoffs)) {
1950a02a946dSIlya Dryomov 			struct ceph_osd_backoff *backoff =
1951a02a946dSIlya Dryomov 			    rb_entry(rb_first(&spg->backoffs),
1952a02a946dSIlya Dryomov 				     struct ceph_osd_backoff, spg_node);
1953a02a946dSIlya Dryomov 
1954a02a946dSIlya Dryomov 			erase_backoff(&spg->backoffs, backoff);
1955a02a946dSIlya Dryomov 			erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
1956a02a946dSIlya Dryomov 			free_backoff(backoff);
1957a02a946dSIlya Dryomov 		}
1958a02a946dSIlya Dryomov 		erase_spg_mapping(&osd->o_backoff_mappings, spg);
1959a02a946dSIlya Dryomov 		free_spg_mapping(spg);
1960a02a946dSIlya Dryomov 	}
1961a02a946dSIlya Dryomov }
1962a02a946dSIlya Dryomov 
1963a02a946dSIlya Dryomov /*
1964a02a946dSIlya Dryomov  * Set up a temporary, non-owning view into @t.
1965a02a946dSIlya Dryomov  */
1966a02a946dSIlya Dryomov static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
1967a02a946dSIlya Dryomov 				  const struct ceph_osd_request_target *t)
1968a02a946dSIlya Dryomov {
1969a02a946dSIlya Dryomov 	hoid->key = NULL;
1970a02a946dSIlya Dryomov 	hoid->key_len = 0;
1971a02a946dSIlya Dryomov 	hoid->oid = t->target_oid.name;
1972a02a946dSIlya Dryomov 	hoid->oid_len = t->target_oid.name_len;
1973a02a946dSIlya Dryomov 	hoid->snapid = CEPH_NOSNAP;
1974a02a946dSIlya Dryomov 	hoid->hash = t->pgid.seed;
1975a02a946dSIlya Dryomov 	hoid->is_max = false;
1976a02a946dSIlya Dryomov 	if (t->target_oloc.pool_ns) {
1977a02a946dSIlya Dryomov 		hoid->nspace = t->target_oloc.pool_ns->str;
1978a02a946dSIlya Dryomov 		hoid->nspace_len = t->target_oloc.pool_ns->len;
1979a02a946dSIlya Dryomov 	} else {
1980a02a946dSIlya Dryomov 		hoid->nspace = NULL;
1981a02a946dSIlya Dryomov 		hoid->nspace_len = 0;
1982a02a946dSIlya Dryomov 	}
1983a02a946dSIlya Dryomov 	hoid->pool = t->target_oloc.pool;
1984a02a946dSIlya Dryomov 	ceph_hoid_build_hash_cache(hoid);
1985a02a946dSIlya Dryomov }
1986a02a946dSIlya Dryomov 
1987a02a946dSIlya Dryomov static bool should_plug_request(struct ceph_osd_request *req)
1988a02a946dSIlya Dryomov {
1989a02a946dSIlya Dryomov 	struct ceph_osd *osd = req->r_osd;
1990a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
1991a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
1992a02a946dSIlya Dryomov 	struct ceph_hobject_id hoid;
1993a02a946dSIlya Dryomov 
1994a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
1995a02a946dSIlya Dryomov 	if (!spg)
1996a02a946dSIlya Dryomov 		return false;
1997a02a946dSIlya Dryomov 
1998a02a946dSIlya Dryomov 	hoid_fill_from_target(&hoid, &req->r_t);
1999a02a946dSIlya Dryomov 	backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
2000a02a946dSIlya Dryomov 	if (!backoff)
2001a02a946dSIlya Dryomov 		return false;
2002a02a946dSIlya Dryomov 
2003a02a946dSIlya Dryomov 	dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
2004a02a946dSIlya Dryomov 	     __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
2005a02a946dSIlya Dryomov 	     backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
2006a02a946dSIlya Dryomov 	return true;
2007a02a946dSIlya Dryomov }
2008a02a946dSIlya Dryomov 
20090d9c1ab3SIlya Dryomov /*
20100d9c1ab3SIlya Dryomov  * Keep get_num_data_items() in sync with this function.
20110d9c1ab3SIlya Dryomov  */
201298c4bfe9SIlya Dryomov static void setup_request_data(struct ceph_osd_request *req)
20133d14c5d2SYehuda Sadeh {
201498c4bfe9SIlya Dryomov 	struct ceph_msg *request_msg = req->r_request;
201598c4bfe9SIlya Dryomov 	struct ceph_msg *reply_msg = req->r_reply;
201698c4bfe9SIlya Dryomov 	struct ceph_osd_req_op *op;
20173d14c5d2SYehuda Sadeh 
201898c4bfe9SIlya Dryomov 	if (req->r_request->num_data_items || req->r_reply->num_data_items)
2019bb873b53SIlya Dryomov 		return;
20203d14c5d2SYehuda Sadeh 
202198c4bfe9SIlya Dryomov 	WARN_ON(request_msg->data_length || reply_msg->data_length);
202298c4bfe9SIlya Dryomov 	for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
2023bb873b53SIlya Dryomov 		switch (op->op) {
2024bb873b53SIlya Dryomov 		/* request */
2025bb873b53SIlya Dryomov 		case CEPH_OSD_OP_WRITE:
2026bb873b53SIlya Dryomov 		case CEPH_OSD_OP_WRITEFULL:
2027bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->extent.length);
202898c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
202998c4bfe9SIlya Dryomov 					       &op->extent.osd_data);
2030bb873b53SIlya Dryomov 			break;
2031bb873b53SIlya Dryomov 		case CEPH_OSD_OP_SETXATTR:
2032bb873b53SIlya Dryomov 		case CEPH_OSD_OP_CMPXATTR:
2033bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->xattr.name_len +
2034bb873b53SIlya Dryomov 						  op->xattr.value_len);
203598c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
203698c4bfe9SIlya Dryomov 					       &op->xattr.osd_data);
2037bb873b53SIlya Dryomov 			break;
2038922dab61SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY_ACK:
203998c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
2040922dab61SIlya Dryomov 					       &op->notify_ack.request_data);
2041922dab61SIlya Dryomov 			break;
204278beb0ffSLuis Henriques 		case CEPH_OSD_OP_COPY_FROM2:
204323ddf9beSLuis Henriques 			ceph_osdc_msg_data_add(request_msg,
204423ddf9beSLuis Henriques 					       &op->copy_from.osd_data);
204523ddf9beSLuis Henriques 			break;
2046bb873b53SIlya Dryomov 
2047bb873b53SIlya Dryomov 		/* reply */
2048bb873b53SIlya Dryomov 		case CEPH_OSD_OP_STAT:
204998c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
2050bb873b53SIlya Dryomov 					       &op->raw_data_in);
2051bb873b53SIlya Dryomov 			break;
2052bb873b53SIlya Dryomov 		case CEPH_OSD_OP_READ:
2053*f628d799SJeff Layton 		case CEPH_OSD_OP_SPARSE_READ:
205498c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
2055bb873b53SIlya Dryomov 					       &op->extent.osd_data);
2056bb873b53SIlya Dryomov 			break;
2057a4ed38d7SDouglas Fuller 		case CEPH_OSD_OP_LIST_WATCHERS:
205898c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
2059a4ed38d7SDouglas Fuller 					       &op->list_watchers.response_data);
2060a4ed38d7SDouglas Fuller 			break;
2061bb873b53SIlya Dryomov 
2062bb873b53SIlya Dryomov 		/* both */
2063bb873b53SIlya Dryomov 		case CEPH_OSD_OP_CALL:
2064bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->cls.class_len +
2065bb873b53SIlya Dryomov 						  op->cls.method_len +
2066bb873b53SIlya Dryomov 						  op->cls.indata_len);
206798c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
206898c4bfe9SIlya Dryomov 					       &op->cls.request_info);
2069bb873b53SIlya Dryomov 			/* optional, can be NONE */
207098c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
207198c4bfe9SIlya Dryomov 					       &op->cls.request_data);
2072bb873b53SIlya Dryomov 			/* optional, can be NONE */
207398c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
2074bb873b53SIlya Dryomov 					       &op->cls.response_data);
2075bb873b53SIlya Dryomov 			break;
207619079203SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY:
207798c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
207819079203SIlya Dryomov 					       &op->notify.request_data);
207998c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
208019079203SIlya Dryomov 					       &op->notify.response_data);
208119079203SIlya Dryomov 			break;
2082bb873b53SIlya Dryomov 		}
2083bb873b53SIlya Dryomov 	}
2084bb873b53SIlya Dryomov }
2085bb873b53SIlya Dryomov 
20862e59ffd1SIlya Dryomov static void encode_pgid(void **p, const struct ceph_pg *pgid)
20872e59ffd1SIlya Dryomov {
20882e59ffd1SIlya Dryomov 	ceph_encode_8(p, 1);
20892e59ffd1SIlya Dryomov 	ceph_encode_64(p, pgid->pool);
20902e59ffd1SIlya Dryomov 	ceph_encode_32(p, pgid->seed);
20912e59ffd1SIlya Dryomov 	ceph_encode_32(p, -1); /* preferred */
20922e59ffd1SIlya Dryomov }
20932e59ffd1SIlya Dryomov 
20948cb441c0SIlya Dryomov static void encode_spgid(void **p, const struct ceph_spg *spgid)
20958cb441c0SIlya Dryomov {
20968cb441c0SIlya Dryomov 	ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
20978cb441c0SIlya Dryomov 	encode_pgid(p, &spgid->pgid);
20988cb441c0SIlya Dryomov 	ceph_encode_8(p, spgid->shard);
20998cb441c0SIlya Dryomov }
21008cb441c0SIlya Dryomov 
21012e59ffd1SIlya Dryomov static void encode_oloc(void **p, void *end,
21022e59ffd1SIlya Dryomov 			const struct ceph_object_locator *oloc)
21032e59ffd1SIlya Dryomov {
21042e59ffd1SIlya Dryomov 	ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
21052e59ffd1SIlya Dryomov 	ceph_encode_64(p, oloc->pool);
21062e59ffd1SIlya Dryomov 	ceph_encode_32(p, -1); /* preferred */
21072e59ffd1SIlya Dryomov 	ceph_encode_32(p, 0);  /* key len */
21082e59ffd1SIlya Dryomov 	if (oloc->pool_ns)
21092e59ffd1SIlya Dryomov 		ceph_encode_string(p, end, oloc->pool_ns->str,
21102e59ffd1SIlya Dryomov 				   oloc->pool_ns->len);
21112e59ffd1SIlya Dryomov 	else
21122e59ffd1SIlya Dryomov 		ceph_encode_32(p, 0);
21132e59ffd1SIlya Dryomov }
21142e59ffd1SIlya Dryomov 
21158cb441c0SIlya Dryomov static void encode_request_partial(struct ceph_osd_request *req,
21168cb441c0SIlya Dryomov 				   struct ceph_msg *msg)
2117bb873b53SIlya Dryomov {
2118bb873b53SIlya Dryomov 	void *p = msg->front.iov_base;
2119bb873b53SIlya Dryomov 	void *const end = p + msg->front_alloc_len;
2120bb873b53SIlya Dryomov 	u32 data_len = 0;
2121bb873b53SIlya Dryomov 	int i;
2122bb873b53SIlya Dryomov 
2123bb873b53SIlya Dryomov 	if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
2124bb873b53SIlya Dryomov 		/* snapshots aren't writeable */
2125bb873b53SIlya Dryomov 		WARN_ON(req->r_snapid != CEPH_NOSNAP);
2126bb873b53SIlya Dryomov 	} else {
2127bb873b53SIlya Dryomov 		WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
2128bb873b53SIlya Dryomov 			req->r_data_offset || req->r_snapc);
2129bb873b53SIlya Dryomov 	}
2130bb873b53SIlya Dryomov 
213198c4bfe9SIlya Dryomov 	setup_request_data(req);
2132bb873b53SIlya Dryomov 
21338cb441c0SIlya Dryomov 	encode_spgid(&p, &req->r_t.spgid); /* actual spg */
21348cb441c0SIlya Dryomov 	ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
2135bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
2136bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_flags);
21378cb441c0SIlya Dryomov 
21388cb441c0SIlya Dryomov 	/* reqid */
21398cb441c0SIlya Dryomov 	ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
21408cb441c0SIlya Dryomov 	memset(p, 0, sizeof(struct ceph_osd_reqid));
21418cb441c0SIlya Dryomov 	p += sizeof(struct ceph_osd_reqid);
21428cb441c0SIlya Dryomov 
21438cb441c0SIlya Dryomov 	/* trace */
21448cb441c0SIlya Dryomov 	memset(p, 0, sizeof(struct ceph_blkin_trace_info));
21458cb441c0SIlya Dryomov 	p += sizeof(struct ceph_blkin_trace_info);
21468cb441c0SIlya Dryomov 
21478cb441c0SIlya Dryomov 	ceph_encode_32(&p, 0); /* client_inc, always 0 */
2148fac02ddfSArnd Bergmann 	ceph_encode_timespec64(p, &req->r_mtime);
2149bb873b53SIlya Dryomov 	p += sizeof(struct ceph_timespec);
2150aa26d662SJeff Layton 
21512e59ffd1SIlya Dryomov 	encode_oloc(&p, end, &req->r_t.target_oloc);
21522e59ffd1SIlya Dryomov 	ceph_encode_string(&p, end, req->r_t.target_oid.name,
21532e59ffd1SIlya Dryomov 			   req->r_t.target_oid.name_len);
2154bb873b53SIlya Dryomov 
2155bb873b53SIlya Dryomov 	/* ops, can imply data */
2156bb873b53SIlya Dryomov 	ceph_encode_16(&p, req->r_num_ops);
2157bb873b53SIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
2158bb873b53SIlya Dryomov 		data_len += osd_req_encode_op(p, &req->r_ops[i]);
2159bb873b53SIlya Dryomov 		p += sizeof(struct ceph_osd_op);
2160bb873b53SIlya Dryomov 	}
2161bb873b53SIlya Dryomov 
2162bb873b53SIlya Dryomov 	ceph_encode_64(&p, req->r_snapid); /* snapid */
2163bb873b53SIlya Dryomov 	if (req->r_snapc) {
2164bb873b53SIlya Dryomov 		ceph_encode_64(&p, req->r_snapc->seq);
2165bb873b53SIlya Dryomov 		ceph_encode_32(&p, req->r_snapc->num_snaps);
2166bb873b53SIlya Dryomov 		for (i = 0; i < req->r_snapc->num_snaps; i++)
2167bb873b53SIlya Dryomov 			ceph_encode_64(&p, req->r_snapc->snaps[i]);
2168bb873b53SIlya Dryomov 	} else {
2169bb873b53SIlya Dryomov 		ceph_encode_64(&p, 0); /* snap_seq */
2170bb873b53SIlya Dryomov 		ceph_encode_32(&p, 0); /* snaps len */
2171bb873b53SIlya Dryomov 	}
2172bb873b53SIlya Dryomov 
2173bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
2174986e8989SIlya Dryomov 	BUG_ON(p > end - 8); /* space for features */
2175bb873b53SIlya Dryomov 
21768cb441c0SIlya Dryomov 	msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
21778cb441c0SIlya Dryomov 	/* front_len is finalized in encode_request_finish() */
2178986e8989SIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
2179986e8989SIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2180bb873b53SIlya Dryomov 	msg->hdr.data_len = cpu_to_le32(data_len);
2181bb873b53SIlya Dryomov 	/*
2182bb873b53SIlya Dryomov 	 * The header "data_off" is a hint to the receiver allowing it
2183bb873b53SIlya Dryomov 	 * to align received data into its buffers such that there's no
2184bb873b53SIlya Dryomov 	 * need to re-copy it before writing it to disk (direct I/O).
2185bb873b53SIlya Dryomov 	 */
2186bb873b53SIlya Dryomov 	msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
2187bb873b53SIlya Dryomov 
21888cb441c0SIlya Dryomov 	dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
21898cb441c0SIlya Dryomov 	     req->r_t.target_oid.name, req->r_t.target_oid.name_len);
21908cb441c0SIlya Dryomov }
21918cb441c0SIlya Dryomov 
21928cb441c0SIlya Dryomov static void encode_request_finish(struct ceph_msg *msg)
21938cb441c0SIlya Dryomov {
21948cb441c0SIlya Dryomov 	void *p = msg->front.iov_base;
2195986e8989SIlya Dryomov 	void *const partial_end = p + msg->front.iov_len;
21968cb441c0SIlya Dryomov 	void *const end = p + msg->front_alloc_len;
21978cb441c0SIlya Dryomov 
21988cb441c0SIlya Dryomov 	if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
21998cb441c0SIlya Dryomov 		/* luminous OSD -- encode features and be done */
2200986e8989SIlya Dryomov 		p = partial_end;
22018cb441c0SIlya Dryomov 		ceph_encode_64(&p, msg->con->peer_features);
22028cb441c0SIlya Dryomov 	} else {
22038cb441c0SIlya Dryomov 		struct {
22048cb441c0SIlya Dryomov 			char spgid[CEPH_ENCODING_START_BLK_LEN +
22058cb441c0SIlya Dryomov 				   CEPH_PGID_ENCODING_LEN + 1];
22068cb441c0SIlya Dryomov 			__le32 hash;
22078cb441c0SIlya Dryomov 			__le32 epoch;
22088cb441c0SIlya Dryomov 			__le32 flags;
22098cb441c0SIlya Dryomov 			char reqid[CEPH_ENCODING_START_BLK_LEN +
22108cb441c0SIlya Dryomov 				   sizeof(struct ceph_osd_reqid)];
22118cb441c0SIlya Dryomov 			char trace[sizeof(struct ceph_blkin_trace_info)];
22128cb441c0SIlya Dryomov 			__le32 client_inc;
22138cb441c0SIlya Dryomov 			struct ceph_timespec mtime;
22148cb441c0SIlya Dryomov 		} __packed head;
22158cb441c0SIlya Dryomov 		struct ceph_pg pgid;
22168cb441c0SIlya Dryomov 		void *oloc, *oid, *tail;
22178cb441c0SIlya Dryomov 		int oloc_len, oid_len, tail_len;
22188cb441c0SIlya Dryomov 		int len;
22198cb441c0SIlya Dryomov 
22208cb441c0SIlya Dryomov 		/*
22218cb441c0SIlya Dryomov 		 * Pre-luminous OSD -- reencode v8 into v4 using @head
22228cb441c0SIlya Dryomov 		 * as a temporary buffer.  Encode the raw PG; the rest
22238cb441c0SIlya Dryomov 		 * is just a matter of moving oloc, oid and tail blobs
22248cb441c0SIlya Dryomov 		 * around.
22258cb441c0SIlya Dryomov 		 */
22268cb441c0SIlya Dryomov 		memcpy(&head, p, sizeof(head));
22278cb441c0SIlya Dryomov 		p += sizeof(head);
22288cb441c0SIlya Dryomov 
22298cb441c0SIlya Dryomov 		oloc = p;
22308cb441c0SIlya Dryomov 		p += CEPH_ENCODING_START_BLK_LEN;
22318cb441c0SIlya Dryomov 		pgid.pool = ceph_decode_64(&p);
22328cb441c0SIlya Dryomov 		p += 4 + 4; /* preferred, key len */
22338cb441c0SIlya Dryomov 		len = ceph_decode_32(&p);
22348cb441c0SIlya Dryomov 		p += len;   /* nspace */
22358cb441c0SIlya Dryomov 		oloc_len = p - oloc;
22368cb441c0SIlya Dryomov 
22378cb441c0SIlya Dryomov 		oid = p;
22388cb441c0SIlya Dryomov 		len = ceph_decode_32(&p);
22398cb441c0SIlya Dryomov 		p += len;
22408cb441c0SIlya Dryomov 		oid_len = p - oid;
22418cb441c0SIlya Dryomov 
22428cb441c0SIlya Dryomov 		tail = p;
2243986e8989SIlya Dryomov 		tail_len = partial_end - p;
22448cb441c0SIlya Dryomov 
22458cb441c0SIlya Dryomov 		p = msg->front.iov_base;
22468cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
22478cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
22488cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
22498cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
22508cb441c0SIlya Dryomov 
22518cb441c0SIlya Dryomov 		/* reassert_version */
22528cb441c0SIlya Dryomov 		memset(p, 0, sizeof(struct ceph_eversion));
22538cb441c0SIlya Dryomov 		p += sizeof(struct ceph_eversion);
22548cb441c0SIlya Dryomov 
22558cb441c0SIlya Dryomov 		BUG_ON(p >= oloc);
22568cb441c0SIlya Dryomov 		memmove(p, oloc, oloc_len);
22578cb441c0SIlya Dryomov 		p += oloc_len;
22588cb441c0SIlya Dryomov 
22598cb441c0SIlya Dryomov 		pgid.seed = le32_to_cpu(head.hash);
22608cb441c0SIlya Dryomov 		encode_pgid(&p, &pgid); /* raw pg */
22618cb441c0SIlya Dryomov 
22628cb441c0SIlya Dryomov 		BUG_ON(p >= oid);
22638cb441c0SIlya Dryomov 		memmove(p, oid, oid_len);
22648cb441c0SIlya Dryomov 		p += oid_len;
22658cb441c0SIlya Dryomov 
22668cb441c0SIlya Dryomov 		/* tail -- ops, snapid, snapc, retry_attempt */
22678cb441c0SIlya Dryomov 		BUG_ON(p >= tail);
22688cb441c0SIlya Dryomov 		memmove(p, tail, tail_len);
22698cb441c0SIlya Dryomov 		p += tail_len;
22708cb441c0SIlya Dryomov 
22718cb441c0SIlya Dryomov 		msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
22728cb441c0SIlya Dryomov 	}
22738cb441c0SIlya Dryomov 
22748cb441c0SIlya Dryomov 	BUG_ON(p > end);
22758cb441c0SIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
22768cb441c0SIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
22778cb441c0SIlya Dryomov 
22788cb441c0SIlya Dryomov 	dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
22798cb441c0SIlya Dryomov 	     le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
22808cb441c0SIlya Dryomov 	     le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
22818cb441c0SIlya Dryomov 	     le16_to_cpu(msg->hdr.version));
2282bb873b53SIlya Dryomov }
2283bb873b53SIlya Dryomov 
2284bb873b53SIlya Dryomov /*
2285bb873b53SIlya Dryomov  * @req has to be assigned a tid and registered.
2286bb873b53SIlya Dryomov  */
2287bb873b53SIlya Dryomov static void send_request(struct ceph_osd_request *req)
2288bb873b53SIlya Dryomov {
2289bb873b53SIlya Dryomov 	struct ceph_osd *osd = req->r_osd;
2290bb873b53SIlya Dryomov 
22915aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
2292bb873b53SIlya Dryomov 	WARN_ON(osd->o_osd != req->r_t.osd);
2293bb873b53SIlya Dryomov 
2294a02a946dSIlya Dryomov 	/* backoff? */
2295a02a946dSIlya Dryomov 	if (should_plug_request(req))
2296a02a946dSIlya Dryomov 		return;
2297a02a946dSIlya Dryomov 
22985aea3dcdSIlya Dryomov 	/*
22995aea3dcdSIlya Dryomov 	 * We may have a previously queued request message hanging
23005aea3dcdSIlya Dryomov 	 * around.  Cancel it to avoid corrupting the msgr.
23015aea3dcdSIlya Dryomov 	 */
23025aea3dcdSIlya Dryomov 	if (req->r_sent)
23035aea3dcdSIlya Dryomov 		ceph_msg_revoke(req->r_request);
23045aea3dcdSIlya Dryomov 
2305bb873b53SIlya Dryomov 	req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
2306bb873b53SIlya Dryomov 	if (req->r_attempts)
2307bb873b53SIlya Dryomov 		req->r_flags |= CEPH_OSD_FLAG_RETRY;
2308bb873b53SIlya Dryomov 	else
2309bb873b53SIlya Dryomov 		WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
2310bb873b53SIlya Dryomov 
23118cb441c0SIlya Dryomov 	encode_request_partial(req, req->r_request);
2312bb873b53SIlya Dryomov 
231304c7d789SIlya 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",
2314bb873b53SIlya Dryomov 	     __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
2315dc98ff72SIlya Dryomov 	     req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
231604c7d789SIlya Dryomov 	     req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
231704c7d789SIlya Dryomov 	     req->r_attempts);
2318bb873b53SIlya Dryomov 
2319bb873b53SIlya Dryomov 	req->r_t.paused = false;
23203d14c5d2SYehuda Sadeh 	req->r_stamp = jiffies;
2321bb873b53SIlya Dryomov 	req->r_attempts++;
23223d14c5d2SYehuda Sadeh 
2323bb873b53SIlya Dryomov 	req->r_sent = osd->o_incarnation;
2324bb873b53SIlya Dryomov 	req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
2325bb873b53SIlya Dryomov 	ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
23263d14c5d2SYehuda Sadeh }
23273d14c5d2SYehuda Sadeh 
232842c1b124SIlya Dryomov static void maybe_request_map(struct ceph_osd_client *osdc)
232942c1b124SIlya Dryomov {
233042c1b124SIlya Dryomov 	bool continuous = false;
233142c1b124SIlya Dryomov 
23325aea3dcdSIlya Dryomov 	verify_osdc_locked(osdc);
233342c1b124SIlya Dryomov 	WARN_ON(!osdc->osdmap->epoch);
233442c1b124SIlya Dryomov 
2335b7ec35b3SIlya Dryomov 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2336b7ec35b3SIlya Dryomov 	    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
2337b7ec35b3SIlya Dryomov 	    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
233842c1b124SIlya Dryomov 		dout("%s osdc %p continuous\n", __func__, osdc);
233942c1b124SIlya Dryomov 		continuous = true;
234042c1b124SIlya Dryomov 	} else {
234142c1b124SIlya Dryomov 		dout("%s osdc %p onetime\n", __func__, osdc);
234242c1b124SIlya Dryomov 	}
234342c1b124SIlya Dryomov 
234442c1b124SIlya Dryomov 	if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
234542c1b124SIlya Dryomov 			       osdc->osdmap->epoch + 1, continuous))
234642c1b124SIlya Dryomov 		ceph_monc_renew_subs(&osdc->client->monc);
234742c1b124SIlya Dryomov }
234842c1b124SIlya Dryomov 
2349a1f4020aSJeff Layton static void complete_request(struct ceph_osd_request *req, int err);
23504609245eSIlya Dryomov static void send_map_check(struct ceph_osd_request *req);
23514609245eSIlya Dryomov 
23525aea3dcdSIlya Dryomov static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
23530bbfdfe8SIlya Dryomov {
23545aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
23555aea3dcdSIlya Dryomov 	struct ceph_osd *osd;
23564609245eSIlya Dryomov 	enum calc_target_result ct_res;
235766850df5SIlya Dryomov 	int err = 0;
23585aea3dcdSIlya Dryomov 	bool need_send = false;
23595aea3dcdSIlya Dryomov 	bool promoted = false;
23600bbfdfe8SIlya Dryomov 
2361b18b9550SIlya Dryomov 	WARN_ON(req->r_tid);
23625aea3dcdSIlya Dryomov 	dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
23635aea3dcdSIlya Dryomov 
23645aea3dcdSIlya Dryomov again:
23658edf84baSIlya Dryomov 	ct_res = calc_target(osdc, &req->r_t, false);
23664609245eSIlya Dryomov 	if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
23674609245eSIlya Dryomov 		goto promote;
23684609245eSIlya Dryomov 
23695aea3dcdSIlya Dryomov 	osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
23705aea3dcdSIlya Dryomov 	if (IS_ERR(osd)) {
23715aea3dcdSIlya Dryomov 		WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
23725aea3dcdSIlya Dryomov 		goto promote;
23735aea3dcdSIlya Dryomov 	}
23745aea3dcdSIlya Dryomov 
237566850df5SIlya Dryomov 	if (osdc->abort_err) {
237666850df5SIlya Dryomov 		dout("req %p abort_err %d\n", req, osdc->abort_err);
237766850df5SIlya Dryomov 		err = osdc->abort_err;
237866850df5SIlya Dryomov 	} else if (osdc->osdmap->epoch < osdc->epoch_barrier) {
237958eb7932SJeff Layton 		dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
238058eb7932SJeff Layton 		     osdc->epoch_barrier);
238158eb7932SJeff Layton 		req->r_t.paused = true;
238258eb7932SJeff Layton 		maybe_request_map(osdc);
238358eb7932SJeff Layton 	} else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2384b7ec35b3SIlya Dryomov 		   ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
23855aea3dcdSIlya Dryomov 		dout("req %p pausewr\n", req);
23865aea3dcdSIlya Dryomov 		req->r_t.paused = true;
23875aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
23885aea3dcdSIlya Dryomov 	} else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
2389b7ec35b3SIlya Dryomov 		   ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
23905aea3dcdSIlya Dryomov 		dout("req %p pauserd\n", req);
23915aea3dcdSIlya Dryomov 		req->r_t.paused = true;
23925aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
23935aea3dcdSIlya Dryomov 	} else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
23945aea3dcdSIlya Dryomov 		   !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
23955aea3dcdSIlya Dryomov 				     CEPH_OSD_FLAG_FULL_FORCE)) &&
2396b7ec35b3SIlya Dryomov 		   (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
23975aea3dcdSIlya Dryomov 		    pool_full(osdc, req->r_t.base_oloc.pool))) {
23985aea3dcdSIlya Dryomov 		dout("req %p full/pool_full\n", req);
239902b2f549SDongsheng Yang 		if (ceph_test_opt(osdc->client, ABORT_ON_FULL)) {
240029e87820SIlya Dryomov 			err = -ENOSPC;
240129e87820SIlya Dryomov 		} else {
2402dc9b0dc4SIlya Dryomov 			if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL))
2403dc9b0dc4SIlya Dryomov 				pr_warn_ratelimited("cluster is full (osdmap FULL)\n");
2404dc9b0dc4SIlya Dryomov 			else
2405dc9b0dc4SIlya Dryomov 				pr_warn_ratelimited("pool %lld is full or reached quota\n",
2406dc9b0dc4SIlya Dryomov 						    req->r_t.base_oloc.pool);
24075aea3dcdSIlya Dryomov 			req->r_t.paused = true;
24085aea3dcdSIlya Dryomov 			maybe_request_map(osdc);
240929e87820SIlya Dryomov 		}
24105aea3dcdSIlya Dryomov 	} else if (!osd_homeless(osd)) {
24115aea3dcdSIlya Dryomov 		need_send = true;
24120bbfdfe8SIlya Dryomov 	} else {
24135aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
24140bbfdfe8SIlya Dryomov 	}
24150bbfdfe8SIlya Dryomov 
24165aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
24175aea3dcdSIlya Dryomov 	/*
24185aea3dcdSIlya Dryomov 	 * Assign the tid atomically with send_request() to protect
24195aea3dcdSIlya Dryomov 	 * multiple writes to the same object from racing with each
24205aea3dcdSIlya Dryomov 	 * other, resulting in out of order ops on the OSDs.
24215aea3dcdSIlya Dryomov 	 */
24225aea3dcdSIlya Dryomov 	req->r_tid = atomic64_inc_return(&osdc->last_tid);
24235aea3dcdSIlya Dryomov 	link_request(osd, req);
24245aea3dcdSIlya Dryomov 	if (need_send)
24255aea3dcdSIlya Dryomov 		send_request(req);
242666850df5SIlya Dryomov 	else if (err)
242766850df5SIlya Dryomov 		complete_request(req, err);
24285aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
24295aea3dcdSIlya Dryomov 
24306001567cSIlya Dryomov 	if (!err && ct_res == CALC_TARGET_POOL_DNE)
24314609245eSIlya Dryomov 		send_map_check(req);
24324609245eSIlya Dryomov 
24335aea3dcdSIlya Dryomov 	if (promoted)
24345aea3dcdSIlya Dryomov 		downgrade_write(&osdc->lock);
24355aea3dcdSIlya Dryomov 	return;
24365aea3dcdSIlya Dryomov 
24375aea3dcdSIlya Dryomov promote:
24385aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
24395aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
24405aea3dcdSIlya Dryomov 	wrlocked = true;
24415aea3dcdSIlya Dryomov 	promoted = true;
24425aea3dcdSIlya Dryomov 	goto again;
24430bbfdfe8SIlya Dryomov }
24440bbfdfe8SIlya Dryomov 
24455aea3dcdSIlya Dryomov static void account_request(struct ceph_osd_request *req)
24465aea3dcdSIlya Dryomov {
244754ea0046SIlya Dryomov 	WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
2448b18b9550SIlya Dryomov 	WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
24495aea3dcdSIlya Dryomov 
2450b18b9550SIlya Dryomov 	req->r_flags |= CEPH_OSD_FLAG_ONDISK;
245122d2cfdfSIlya Dryomov 	atomic_inc(&req->r_osdc->num_requests);
24527cc5e38fSIlya Dryomov 
24537cc5e38fSIlya Dryomov 	req->r_start_stamp = jiffies;
245497e27aaaSXiubo Li 	req->r_start_latency = ktime_get();
24555aea3dcdSIlya Dryomov }
24565aea3dcdSIlya Dryomov 
24575aea3dcdSIlya Dryomov static void submit_request(struct ceph_osd_request *req, bool wrlocked)
24585aea3dcdSIlya Dryomov {
24595aea3dcdSIlya Dryomov 	ceph_osdc_get_request(req);
24605aea3dcdSIlya Dryomov 	account_request(req);
24615aea3dcdSIlya Dryomov 	__submit_request(req, wrlocked);
24625aea3dcdSIlya Dryomov }
24635aea3dcdSIlya Dryomov 
246445ee2c1dSIlya Dryomov static void finish_request(struct ceph_osd_request *req)
24655aea3dcdSIlya Dryomov {
24665aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
24675aea3dcdSIlya Dryomov 
24684609245eSIlya Dryomov 	WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
246904c7d789SIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
247004c7d789SIlya Dryomov 
247197e27aaaSXiubo Li 	req->r_end_latency = ktime_get();
247297e27aaaSXiubo Li 
2473*f628d799SJeff Layton 	if (req->r_osd) {
2474*f628d799SJeff Layton 		ceph_init_sparse_read(&req->r_osd->o_sparse_read);
247504c7d789SIlya Dryomov 		unlink_request(req->r_osd, req);
2476*f628d799SJeff Layton 	}
24775aea3dcdSIlya Dryomov 	atomic_dec(&osdc->num_requests);
24785aea3dcdSIlya Dryomov 
24795aea3dcdSIlya Dryomov 	/*
24805aea3dcdSIlya Dryomov 	 * If an OSD has failed or returned and a request has been sent
24815aea3dcdSIlya Dryomov 	 * twice, it's possible to get a reply and end up here while the
24825aea3dcdSIlya Dryomov 	 * request message is queued for delivery.  We will ignore the
24835aea3dcdSIlya Dryomov 	 * reply, so not a big deal, but better to try and catch it.
24845aea3dcdSIlya Dryomov 	 */
24855aea3dcdSIlya Dryomov 	ceph_msg_revoke(req->r_request);
24865aea3dcdSIlya Dryomov 	ceph_msg_revoke_incoming(req->r_reply);
24875aea3dcdSIlya Dryomov }
24885aea3dcdSIlya Dryomov 
2489fe5da05eSIlya Dryomov static void __complete_request(struct ceph_osd_request *req)
2490fe5da05eSIlya Dryomov {
2491d75f773cSSakari Ailus 	dout("%s req %p tid %llu cb %ps result %d\n", __func__, req,
2492b18b9550SIlya Dryomov 	     req->r_tid, req->r_callback, req->r_result);
249326df726bSIlya Dryomov 
249426df726bSIlya Dryomov 	if (req->r_callback)
2495fe5da05eSIlya Dryomov 		req->r_callback(req);
249626df726bSIlya Dryomov 	complete_all(&req->r_completion);
249726df726bSIlya Dryomov 	ceph_osdc_put_request(req);
2498b18b9550SIlya Dryomov }
2499fe5da05eSIlya Dryomov 
250088bc1922SIlya Dryomov static void complete_request_workfn(struct work_struct *work)
250188bc1922SIlya Dryomov {
250288bc1922SIlya Dryomov 	struct ceph_osd_request *req =
250388bc1922SIlya Dryomov 	    container_of(work, struct ceph_osd_request, r_complete_work);
250488bc1922SIlya Dryomov 
250588bc1922SIlya Dryomov 	__complete_request(req);
25060bbfdfe8SIlya Dryomov }
25070bbfdfe8SIlya Dryomov 
25084609245eSIlya Dryomov /*
2509b18b9550SIlya Dryomov  * This is open-coded in handle_reply().
25104609245eSIlya Dryomov  */
25114609245eSIlya Dryomov static void complete_request(struct ceph_osd_request *req, int err)
25124609245eSIlya Dryomov {
25134609245eSIlya Dryomov 	dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
25144609245eSIlya Dryomov 
25154609245eSIlya Dryomov 	req->r_result = err;
251645ee2c1dSIlya Dryomov 	finish_request(req);
251788bc1922SIlya Dryomov 
251888bc1922SIlya Dryomov 	INIT_WORK(&req->r_complete_work, complete_request_workfn);
251988bc1922SIlya Dryomov 	queue_work(req->r_osdc->completion_wq, &req->r_complete_work);
25204609245eSIlya Dryomov }
25214609245eSIlya Dryomov 
25224609245eSIlya Dryomov static void cancel_map_check(struct ceph_osd_request *req)
25234609245eSIlya Dryomov {
25244609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
25254609245eSIlya Dryomov 	struct ceph_osd_request *lookup_req;
25264609245eSIlya Dryomov 
25274609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
25284609245eSIlya Dryomov 
25294609245eSIlya Dryomov 	lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
25304609245eSIlya Dryomov 	if (!lookup_req)
25314609245eSIlya Dryomov 		return;
25324609245eSIlya Dryomov 
25334609245eSIlya Dryomov 	WARN_ON(lookup_req != req);
25344609245eSIlya Dryomov 	erase_request_mc(&osdc->map_checks, req);
25354609245eSIlya Dryomov 	ceph_osdc_put_request(req);
25364609245eSIlya Dryomov }
25374609245eSIlya Dryomov 
25385aea3dcdSIlya Dryomov static void cancel_request(struct ceph_osd_request *req)
25395aea3dcdSIlya Dryomov {
25405aea3dcdSIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
25415aea3dcdSIlya Dryomov 
25424609245eSIlya Dryomov 	cancel_map_check(req);
254345ee2c1dSIlya Dryomov 	finish_request(req);
2544b18b9550SIlya Dryomov 	complete_all(&req->r_completion);
2545c297eb42SIlya Dryomov 	ceph_osdc_put_request(req);
25465aea3dcdSIlya Dryomov }
25475aea3dcdSIlya Dryomov 
25487cc5e38fSIlya Dryomov static void abort_request(struct ceph_osd_request *req, int err)
25497cc5e38fSIlya Dryomov {
25507cc5e38fSIlya Dryomov 	dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
25517cc5e38fSIlya Dryomov 
25527cc5e38fSIlya Dryomov 	cancel_map_check(req);
25537cc5e38fSIlya Dryomov 	complete_request(req, err);
25547cc5e38fSIlya Dryomov }
25557cc5e38fSIlya Dryomov 
255666850df5SIlya Dryomov static int abort_fn(struct ceph_osd_request *req, void *arg)
255766850df5SIlya Dryomov {
255866850df5SIlya Dryomov 	int err = *(int *)arg;
255966850df5SIlya Dryomov 
256066850df5SIlya Dryomov 	abort_request(req, err);
256166850df5SIlya Dryomov 	return 0; /* continue iteration */
256266850df5SIlya Dryomov }
256366850df5SIlya Dryomov 
256466850df5SIlya Dryomov /*
256566850df5SIlya Dryomov  * Abort all in-flight requests with @err and arrange for all future
256666850df5SIlya Dryomov  * requests to be failed immediately.
256766850df5SIlya Dryomov  */
256866850df5SIlya Dryomov void ceph_osdc_abort_requests(struct ceph_osd_client *osdc, int err)
256966850df5SIlya Dryomov {
257066850df5SIlya Dryomov 	dout("%s osdc %p err %d\n", __func__, osdc, err);
257166850df5SIlya Dryomov 	down_write(&osdc->lock);
257266850df5SIlya Dryomov 	for_each_request(osdc, abort_fn, &err);
257366850df5SIlya Dryomov 	osdc->abort_err = err;
257466850df5SIlya Dryomov 	up_write(&osdc->lock);
257566850df5SIlya Dryomov }
257666850df5SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_abort_requests);
257766850df5SIlya Dryomov 
25782cef0ba8SYan, Zheng void ceph_osdc_clear_abort_err(struct ceph_osd_client *osdc)
25792cef0ba8SYan, Zheng {
25802cef0ba8SYan, Zheng 	down_write(&osdc->lock);
25812cef0ba8SYan, Zheng 	osdc->abort_err = 0;
25822cef0ba8SYan, Zheng 	up_write(&osdc->lock);
25832cef0ba8SYan, Zheng }
25842cef0ba8SYan, Zheng EXPORT_SYMBOL(ceph_osdc_clear_abort_err);
25852cef0ba8SYan, Zheng 
258658eb7932SJeff Layton static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
258758eb7932SJeff Layton {
258858eb7932SJeff Layton 	if (likely(eb > osdc->epoch_barrier)) {
258958eb7932SJeff Layton 		dout("updating epoch_barrier from %u to %u\n",
259058eb7932SJeff Layton 				osdc->epoch_barrier, eb);
259158eb7932SJeff Layton 		osdc->epoch_barrier = eb;
259258eb7932SJeff Layton 		/* Request map if we're not to the barrier yet */
259358eb7932SJeff Layton 		if (eb > osdc->osdmap->epoch)
259458eb7932SJeff Layton 			maybe_request_map(osdc);
259558eb7932SJeff Layton 	}
259658eb7932SJeff Layton }
259758eb7932SJeff Layton 
259858eb7932SJeff Layton void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
259958eb7932SJeff Layton {
260058eb7932SJeff Layton 	down_read(&osdc->lock);
260158eb7932SJeff Layton 	if (unlikely(eb > osdc->epoch_barrier)) {
260258eb7932SJeff Layton 		up_read(&osdc->lock);
260358eb7932SJeff Layton 		down_write(&osdc->lock);
260458eb7932SJeff Layton 		update_epoch_barrier(osdc, eb);
260558eb7932SJeff Layton 		up_write(&osdc->lock);
260658eb7932SJeff Layton 	} else {
260758eb7932SJeff Layton 		up_read(&osdc->lock);
260858eb7932SJeff Layton 	}
260958eb7932SJeff Layton }
261058eb7932SJeff Layton EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
261158eb7932SJeff Layton 
2612fc36d0a4SJeff Layton /*
26134eea0fefSIlya Dryomov  * We can end up releasing caps as a result of abort_request().
26144eea0fefSIlya Dryomov  * In that case, we probably want to ensure that the cap release message
26154eea0fefSIlya Dryomov  * has an updated epoch barrier in it, so set the epoch barrier prior to
26164eea0fefSIlya Dryomov  * aborting the first request.
26174eea0fefSIlya Dryomov  */
26184eea0fefSIlya Dryomov static int abort_on_full_fn(struct ceph_osd_request *req, void *arg)
26194eea0fefSIlya Dryomov {
26204eea0fefSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
26214eea0fefSIlya Dryomov 	bool *victims = arg;
26224eea0fefSIlya Dryomov 
2623c843d13cSIlya Dryomov 	if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
26244eea0fefSIlya Dryomov 	    (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2625690f951dSIlya Dryomov 	     pool_full(osdc, req->r_t.base_oloc.pool))) {
26264eea0fefSIlya Dryomov 		if (!*victims) {
26274eea0fefSIlya Dryomov 			update_epoch_barrier(osdc, osdc->osdmap->epoch);
26284eea0fefSIlya Dryomov 			*victims = true;
26294eea0fefSIlya Dryomov 		}
26304eea0fefSIlya Dryomov 		abort_request(req, -ENOSPC);
26314eea0fefSIlya Dryomov 	}
26324eea0fefSIlya Dryomov 
26334eea0fefSIlya Dryomov 	return 0; /* continue iteration */
26344eea0fefSIlya Dryomov }
26354eea0fefSIlya Dryomov 
26364eea0fefSIlya Dryomov /*
2637fc36d0a4SJeff Layton  * Drop all pending requests that are stalled waiting on a full condition to
263858eb7932SJeff Layton  * clear, and complete them with ENOSPC as the return code. Set the
263958eb7932SJeff Layton  * osdc->epoch_barrier to the latest map epoch that we've seen if any were
264058eb7932SJeff Layton  * cancelled.
2641fc36d0a4SJeff Layton  */
2642fc36d0a4SJeff Layton static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
2643fc36d0a4SJeff Layton {
264458eb7932SJeff Layton 	bool victims = false;
2645fc36d0a4SJeff Layton 
264602b2f549SDongsheng Yang 	if (ceph_test_opt(osdc->client, ABORT_ON_FULL) &&
2647c843d13cSIlya Dryomov 	    (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || have_pool_full(osdc)))
26484eea0fefSIlya Dryomov 		for_each_request(osdc, abort_on_full_fn, &victims);
2649fc36d0a4SJeff Layton }
2650fc36d0a4SJeff Layton 
26514609245eSIlya Dryomov static void check_pool_dne(struct ceph_osd_request *req)
26524609245eSIlya Dryomov {
26534609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
26544609245eSIlya Dryomov 	struct ceph_osdmap *map = osdc->osdmap;
26554609245eSIlya Dryomov 
26564609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
26574609245eSIlya Dryomov 	WARN_ON(!map->epoch);
26584609245eSIlya Dryomov 
26594609245eSIlya Dryomov 	if (req->r_attempts) {
26604609245eSIlya Dryomov 		/*
26614609245eSIlya Dryomov 		 * We sent a request earlier, which means that
26624609245eSIlya Dryomov 		 * previously the pool existed, and now it does not
26634609245eSIlya Dryomov 		 * (i.e., it was deleted).
26644609245eSIlya Dryomov 		 */
26654609245eSIlya Dryomov 		req->r_map_dne_bound = map->epoch;
26664609245eSIlya Dryomov 		dout("%s req %p tid %llu pool disappeared\n", __func__, req,
26674609245eSIlya Dryomov 		     req->r_tid);
26684609245eSIlya Dryomov 	} else {
26694609245eSIlya Dryomov 		dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
26704609245eSIlya Dryomov 		     req, req->r_tid, req->r_map_dne_bound, map->epoch);
26714609245eSIlya Dryomov 	}
26724609245eSIlya Dryomov 
26734609245eSIlya Dryomov 	if (req->r_map_dne_bound) {
26744609245eSIlya Dryomov 		if (map->epoch >= req->r_map_dne_bound) {
26754609245eSIlya Dryomov 			/* we had a new enough map */
26764609245eSIlya Dryomov 			pr_info_ratelimited("tid %llu pool does not exist\n",
26774609245eSIlya Dryomov 					    req->r_tid);
26784609245eSIlya Dryomov 			complete_request(req, -ENOENT);
26794609245eSIlya Dryomov 		}
26804609245eSIlya Dryomov 	} else {
26814609245eSIlya Dryomov 		send_map_check(req);
26824609245eSIlya Dryomov 	}
26834609245eSIlya Dryomov }
26844609245eSIlya Dryomov 
26854609245eSIlya Dryomov static void map_check_cb(struct ceph_mon_generic_request *greq)
26864609245eSIlya Dryomov {
26874609245eSIlya Dryomov 	struct ceph_osd_client *osdc = &greq->monc->client->osdc;
26884609245eSIlya Dryomov 	struct ceph_osd_request *req;
26894609245eSIlya Dryomov 	u64 tid = greq->private_data;
26904609245eSIlya Dryomov 
26914609245eSIlya Dryomov 	WARN_ON(greq->result || !greq->u.newest);
26924609245eSIlya Dryomov 
26934609245eSIlya Dryomov 	down_write(&osdc->lock);
26944609245eSIlya Dryomov 	req = lookup_request_mc(&osdc->map_checks, tid);
26954609245eSIlya Dryomov 	if (!req) {
26964609245eSIlya Dryomov 		dout("%s tid %llu dne\n", __func__, tid);
26974609245eSIlya Dryomov 		goto out_unlock;
26984609245eSIlya Dryomov 	}
26994609245eSIlya Dryomov 
27004609245eSIlya Dryomov 	dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
27014609245eSIlya Dryomov 	     req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
27024609245eSIlya Dryomov 	if (!req->r_map_dne_bound)
27034609245eSIlya Dryomov 		req->r_map_dne_bound = greq->u.newest;
27044609245eSIlya Dryomov 	erase_request_mc(&osdc->map_checks, req);
27054609245eSIlya Dryomov 	check_pool_dne(req);
27064609245eSIlya Dryomov 
27074609245eSIlya Dryomov 	ceph_osdc_put_request(req);
27084609245eSIlya Dryomov out_unlock:
27094609245eSIlya Dryomov 	up_write(&osdc->lock);
27104609245eSIlya Dryomov }
27114609245eSIlya Dryomov 
27124609245eSIlya Dryomov static void send_map_check(struct ceph_osd_request *req)
27134609245eSIlya Dryomov {
27144609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
27154609245eSIlya Dryomov 	struct ceph_osd_request *lookup_req;
27164609245eSIlya Dryomov 	int ret;
27174609245eSIlya Dryomov 
27184609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
27194609245eSIlya Dryomov 
27204609245eSIlya Dryomov 	lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
27214609245eSIlya Dryomov 	if (lookup_req) {
27224609245eSIlya Dryomov 		WARN_ON(lookup_req != req);
27234609245eSIlya Dryomov 		return;
27244609245eSIlya Dryomov 	}
27254609245eSIlya Dryomov 
27264609245eSIlya Dryomov 	ceph_osdc_get_request(req);
27274609245eSIlya Dryomov 	insert_request_mc(&osdc->map_checks, req);
27284609245eSIlya Dryomov 	ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
27294609245eSIlya Dryomov 					  map_check_cb, req->r_tid);
27304609245eSIlya Dryomov 	WARN_ON(ret);
27314609245eSIlya Dryomov }
27324609245eSIlya Dryomov 
27330bbfdfe8SIlya Dryomov /*
2734922dab61SIlya Dryomov  * lingering requests, watch/notify v2 infrastructure
2735922dab61SIlya Dryomov  */
2736922dab61SIlya Dryomov static void linger_release(struct kref *kref)
2737922dab61SIlya Dryomov {
2738922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq =
2739922dab61SIlya Dryomov 	    container_of(kref, struct ceph_osd_linger_request, kref);
2740922dab61SIlya Dryomov 
2741922dab61SIlya Dryomov 	dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2742922dab61SIlya Dryomov 	     lreq->reg_req, lreq->ping_req);
2743922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2744922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
27454609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
2746922dab61SIlya Dryomov 	WARN_ON(!list_empty(&lreq->scan_item));
2747b07d3c4bSIlya Dryomov 	WARN_ON(!list_empty(&lreq->pending_lworks));
2748922dab61SIlya Dryomov 	WARN_ON(lreq->osd);
2749922dab61SIlya Dryomov 
275075dbb685SIlya Dryomov 	if (lreq->request_pl)
275175dbb685SIlya Dryomov 		ceph_pagelist_release(lreq->request_pl);
275275dbb685SIlya Dryomov 	if (lreq->notify_id_pages)
275375dbb685SIlya Dryomov 		ceph_release_page_vector(lreq->notify_id_pages, 1);
275475dbb685SIlya Dryomov 
2755922dab61SIlya Dryomov 	ceph_osdc_put_request(lreq->reg_req);
2756922dab61SIlya Dryomov 	ceph_osdc_put_request(lreq->ping_req);
2757922dab61SIlya Dryomov 	target_destroy(&lreq->t);
2758922dab61SIlya Dryomov 	kfree(lreq);
2759922dab61SIlya Dryomov }
2760922dab61SIlya Dryomov 
2761922dab61SIlya Dryomov static void linger_put(struct ceph_osd_linger_request *lreq)
2762922dab61SIlya Dryomov {
2763922dab61SIlya Dryomov 	if (lreq)
2764922dab61SIlya Dryomov 		kref_put(&lreq->kref, linger_release);
2765922dab61SIlya Dryomov }
2766922dab61SIlya Dryomov 
2767922dab61SIlya Dryomov static struct ceph_osd_linger_request *
2768922dab61SIlya Dryomov linger_get(struct ceph_osd_linger_request *lreq)
2769922dab61SIlya Dryomov {
2770922dab61SIlya Dryomov 	kref_get(&lreq->kref);
2771922dab61SIlya Dryomov 	return lreq;
2772922dab61SIlya Dryomov }
2773922dab61SIlya Dryomov 
2774922dab61SIlya Dryomov static struct ceph_osd_linger_request *
2775922dab61SIlya Dryomov linger_alloc(struct ceph_osd_client *osdc)
2776922dab61SIlya Dryomov {
2777922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
2778922dab61SIlya Dryomov 
2779922dab61SIlya Dryomov 	lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2780922dab61SIlya Dryomov 	if (!lreq)
2781922dab61SIlya Dryomov 		return NULL;
2782922dab61SIlya Dryomov 
2783922dab61SIlya Dryomov 	kref_init(&lreq->kref);
2784922dab61SIlya Dryomov 	mutex_init(&lreq->lock);
2785922dab61SIlya Dryomov 	RB_CLEAR_NODE(&lreq->node);
2786922dab61SIlya Dryomov 	RB_CLEAR_NODE(&lreq->osdc_node);
27874609245eSIlya Dryomov 	RB_CLEAR_NODE(&lreq->mc_node);
2788922dab61SIlya Dryomov 	INIT_LIST_HEAD(&lreq->scan_item);
2789b07d3c4bSIlya Dryomov 	INIT_LIST_HEAD(&lreq->pending_lworks);
2790922dab61SIlya Dryomov 	init_completion(&lreq->reg_commit_wait);
279119079203SIlya Dryomov 	init_completion(&lreq->notify_finish_wait);
2792922dab61SIlya Dryomov 
2793922dab61SIlya Dryomov 	lreq->osdc = osdc;
2794922dab61SIlya Dryomov 	target_init(&lreq->t);
2795922dab61SIlya Dryomov 
2796922dab61SIlya Dryomov 	dout("%s lreq %p\n", __func__, lreq);
2797922dab61SIlya Dryomov 	return lreq;
2798922dab61SIlya Dryomov }
2799922dab61SIlya Dryomov 
2800922dab61SIlya Dryomov DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2801922dab61SIlya Dryomov DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
28024609245eSIlya Dryomov DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
2803922dab61SIlya Dryomov 
2804922dab61SIlya Dryomov /*
2805922dab61SIlya Dryomov  * Create linger request <-> OSD session relation.
2806922dab61SIlya Dryomov  *
2807922dab61SIlya Dryomov  * @lreq has to be registered, @osd may be homeless.
2808922dab61SIlya Dryomov  */
2809922dab61SIlya Dryomov static void link_linger(struct ceph_osd *osd,
2810922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq)
2811922dab61SIlya Dryomov {
2812922dab61SIlya Dryomov 	verify_osd_locked(osd);
2813922dab61SIlya Dryomov 	WARN_ON(!lreq->linger_id || lreq->osd);
2814922dab61SIlya Dryomov 	dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2815922dab61SIlya Dryomov 	     osd->o_osd, lreq, lreq->linger_id);
2816922dab61SIlya Dryomov 
2817922dab61SIlya Dryomov 	if (!osd_homeless(osd))
2818922dab61SIlya Dryomov 		__remove_osd_from_lru(osd);
2819922dab61SIlya Dryomov 	else
2820922dab61SIlya Dryomov 		atomic_inc(&osd->o_osdc->num_homeless);
2821922dab61SIlya Dryomov 
2822922dab61SIlya Dryomov 	get_osd(osd);
2823922dab61SIlya Dryomov 	insert_linger(&osd->o_linger_requests, lreq);
2824922dab61SIlya Dryomov 	lreq->osd = osd;
2825922dab61SIlya Dryomov }
2826922dab61SIlya Dryomov 
2827922dab61SIlya Dryomov static void unlink_linger(struct ceph_osd *osd,
2828922dab61SIlya Dryomov 			  struct ceph_osd_linger_request *lreq)
2829922dab61SIlya Dryomov {
2830922dab61SIlya Dryomov 	verify_osd_locked(osd);
2831922dab61SIlya Dryomov 	WARN_ON(lreq->osd != osd);
2832922dab61SIlya Dryomov 	dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2833922dab61SIlya Dryomov 	     osd->o_osd, lreq, lreq->linger_id);
2834922dab61SIlya Dryomov 
2835922dab61SIlya Dryomov 	lreq->osd = NULL;
2836922dab61SIlya Dryomov 	erase_linger(&osd->o_linger_requests, lreq);
2837922dab61SIlya Dryomov 	put_osd(osd);
2838922dab61SIlya Dryomov 
2839922dab61SIlya Dryomov 	if (!osd_homeless(osd))
2840922dab61SIlya Dryomov 		maybe_move_osd_to_lru(osd);
2841922dab61SIlya Dryomov 	else
2842922dab61SIlya Dryomov 		atomic_dec(&osd->o_osdc->num_homeless);
2843922dab61SIlya Dryomov }
2844922dab61SIlya Dryomov 
2845922dab61SIlya Dryomov static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2846922dab61SIlya Dryomov {
2847922dab61SIlya Dryomov 	verify_osdc_locked(lreq->osdc);
2848922dab61SIlya Dryomov 
2849922dab61SIlya Dryomov 	return !RB_EMPTY_NODE(&lreq->osdc_node);
2850922dab61SIlya Dryomov }
2851922dab61SIlya Dryomov 
2852922dab61SIlya Dryomov static bool linger_registered(struct ceph_osd_linger_request *lreq)
2853922dab61SIlya Dryomov {
2854922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2855922dab61SIlya Dryomov 	bool registered;
2856922dab61SIlya Dryomov 
2857922dab61SIlya Dryomov 	down_read(&osdc->lock);
2858922dab61SIlya Dryomov 	registered = __linger_registered(lreq);
2859922dab61SIlya Dryomov 	up_read(&osdc->lock);
2860922dab61SIlya Dryomov 
2861922dab61SIlya Dryomov 	return registered;
2862922dab61SIlya Dryomov }
2863922dab61SIlya Dryomov 
2864922dab61SIlya Dryomov static void linger_register(struct ceph_osd_linger_request *lreq)
2865922dab61SIlya Dryomov {
2866922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2867922dab61SIlya Dryomov 
2868922dab61SIlya Dryomov 	verify_osdc_wrlocked(osdc);
2869922dab61SIlya Dryomov 	WARN_ON(lreq->linger_id);
2870922dab61SIlya Dryomov 
2871922dab61SIlya Dryomov 	linger_get(lreq);
2872922dab61SIlya Dryomov 	lreq->linger_id = ++osdc->last_linger_id;
2873922dab61SIlya Dryomov 	insert_linger_osdc(&osdc->linger_requests, lreq);
2874922dab61SIlya Dryomov }
2875922dab61SIlya Dryomov 
2876922dab61SIlya Dryomov static void linger_unregister(struct ceph_osd_linger_request *lreq)
2877922dab61SIlya Dryomov {
2878922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2879922dab61SIlya Dryomov 
2880922dab61SIlya Dryomov 	verify_osdc_wrlocked(osdc);
2881922dab61SIlya Dryomov 
2882922dab61SIlya Dryomov 	erase_linger_osdc(&osdc->linger_requests, lreq);
2883922dab61SIlya Dryomov 	linger_put(lreq);
2884922dab61SIlya Dryomov }
2885922dab61SIlya Dryomov 
2886922dab61SIlya Dryomov static void cancel_linger_request(struct ceph_osd_request *req)
2887922dab61SIlya Dryomov {
2888922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2889922dab61SIlya Dryomov 
2890922dab61SIlya Dryomov 	WARN_ON(!req->r_linger);
2891922dab61SIlya Dryomov 	cancel_request(req);
2892922dab61SIlya Dryomov 	linger_put(lreq);
2893922dab61SIlya Dryomov }
2894922dab61SIlya Dryomov 
2895922dab61SIlya Dryomov struct linger_work {
2896922dab61SIlya Dryomov 	struct work_struct work;
2897922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
2898b07d3c4bSIlya Dryomov 	struct list_head pending_item;
2899b07d3c4bSIlya Dryomov 	unsigned long queued_stamp;
2900922dab61SIlya Dryomov 
2901922dab61SIlya Dryomov 	union {
2902922dab61SIlya Dryomov 		struct {
2903922dab61SIlya Dryomov 			u64 notify_id;
2904922dab61SIlya Dryomov 			u64 notifier_id;
2905922dab61SIlya Dryomov 			void *payload; /* points into @msg front */
2906922dab61SIlya Dryomov 			size_t payload_len;
2907922dab61SIlya Dryomov 
2908922dab61SIlya Dryomov 			struct ceph_msg *msg; /* for ceph_msg_put() */
2909922dab61SIlya Dryomov 		} notify;
2910922dab61SIlya Dryomov 		struct {
2911922dab61SIlya Dryomov 			int err;
2912922dab61SIlya Dryomov 		} error;
2913922dab61SIlya Dryomov 	};
2914922dab61SIlya Dryomov };
2915922dab61SIlya Dryomov 
2916922dab61SIlya Dryomov static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2917922dab61SIlya Dryomov 				       work_func_t workfn)
2918922dab61SIlya Dryomov {
2919922dab61SIlya Dryomov 	struct linger_work *lwork;
2920922dab61SIlya Dryomov 
2921922dab61SIlya Dryomov 	lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2922922dab61SIlya Dryomov 	if (!lwork)
2923922dab61SIlya Dryomov 		return NULL;
2924922dab61SIlya Dryomov 
2925922dab61SIlya Dryomov 	INIT_WORK(&lwork->work, workfn);
2926b07d3c4bSIlya Dryomov 	INIT_LIST_HEAD(&lwork->pending_item);
2927922dab61SIlya Dryomov 	lwork->lreq = linger_get(lreq);
2928922dab61SIlya Dryomov 
2929922dab61SIlya Dryomov 	return lwork;
2930922dab61SIlya Dryomov }
2931922dab61SIlya Dryomov 
2932922dab61SIlya Dryomov static void lwork_free(struct linger_work *lwork)
2933922dab61SIlya Dryomov {
2934922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2935922dab61SIlya Dryomov 
2936b07d3c4bSIlya Dryomov 	mutex_lock(&lreq->lock);
2937b07d3c4bSIlya Dryomov 	list_del(&lwork->pending_item);
2938b07d3c4bSIlya Dryomov 	mutex_unlock(&lreq->lock);
2939b07d3c4bSIlya Dryomov 
2940922dab61SIlya Dryomov 	linger_put(lreq);
2941922dab61SIlya Dryomov 	kfree(lwork);
2942922dab61SIlya Dryomov }
2943922dab61SIlya Dryomov 
2944922dab61SIlya Dryomov static void lwork_queue(struct linger_work *lwork)
2945922dab61SIlya Dryomov {
2946922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2947922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2948922dab61SIlya Dryomov 
2949922dab61SIlya Dryomov 	verify_lreq_locked(lreq);
2950b07d3c4bSIlya Dryomov 	WARN_ON(!list_empty(&lwork->pending_item));
2951b07d3c4bSIlya Dryomov 
2952b07d3c4bSIlya Dryomov 	lwork->queued_stamp = jiffies;
2953b07d3c4bSIlya Dryomov 	list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
2954922dab61SIlya Dryomov 	queue_work(osdc->notify_wq, &lwork->work);
2955922dab61SIlya Dryomov }
2956922dab61SIlya Dryomov 
2957922dab61SIlya Dryomov static void do_watch_notify(struct work_struct *w)
2958922dab61SIlya Dryomov {
2959922dab61SIlya Dryomov 	struct linger_work *lwork = container_of(w, struct linger_work, work);
2960922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2961922dab61SIlya Dryomov 
2962922dab61SIlya Dryomov 	if (!linger_registered(lreq)) {
2963922dab61SIlya Dryomov 		dout("%s lreq %p not registered\n", __func__, lreq);
2964922dab61SIlya Dryomov 		goto out;
2965922dab61SIlya Dryomov 	}
2966922dab61SIlya Dryomov 
296719079203SIlya Dryomov 	WARN_ON(!lreq->is_watch);
2968922dab61SIlya Dryomov 	dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2969922dab61SIlya Dryomov 	     __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2970922dab61SIlya Dryomov 	     lwork->notify.payload_len);
2971922dab61SIlya Dryomov 	lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2972922dab61SIlya Dryomov 		  lwork->notify.notifier_id, lwork->notify.payload,
2973922dab61SIlya Dryomov 		  lwork->notify.payload_len);
2974922dab61SIlya Dryomov 
2975922dab61SIlya Dryomov out:
2976922dab61SIlya Dryomov 	ceph_msg_put(lwork->notify.msg);
2977922dab61SIlya Dryomov 	lwork_free(lwork);
2978922dab61SIlya Dryomov }
2979922dab61SIlya Dryomov 
2980922dab61SIlya Dryomov static void do_watch_error(struct work_struct *w)
2981922dab61SIlya Dryomov {
2982922dab61SIlya Dryomov 	struct linger_work *lwork = container_of(w, struct linger_work, work);
2983922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2984922dab61SIlya Dryomov 
2985922dab61SIlya Dryomov 	if (!linger_registered(lreq)) {
2986922dab61SIlya Dryomov 		dout("%s lreq %p not registered\n", __func__, lreq);
2987922dab61SIlya Dryomov 		goto out;
2988922dab61SIlya Dryomov 	}
2989922dab61SIlya Dryomov 
2990922dab61SIlya Dryomov 	dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2991922dab61SIlya Dryomov 	lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2992922dab61SIlya Dryomov 
2993922dab61SIlya Dryomov out:
2994922dab61SIlya Dryomov 	lwork_free(lwork);
2995922dab61SIlya Dryomov }
2996922dab61SIlya Dryomov 
2997922dab61SIlya Dryomov static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2998922dab61SIlya Dryomov {
2999922dab61SIlya Dryomov 	struct linger_work *lwork;
3000922dab61SIlya Dryomov 
3001922dab61SIlya Dryomov 	lwork = lwork_alloc(lreq, do_watch_error);
3002922dab61SIlya Dryomov 	if (!lwork) {
3003922dab61SIlya Dryomov 		pr_err("failed to allocate error-lwork\n");
3004922dab61SIlya Dryomov 		return;
3005922dab61SIlya Dryomov 	}
3006922dab61SIlya Dryomov 
3007922dab61SIlya Dryomov 	lwork->error.err = lreq->last_error;
3008922dab61SIlya Dryomov 	lwork_queue(lwork);
3009922dab61SIlya Dryomov }
3010922dab61SIlya Dryomov 
3011922dab61SIlya Dryomov static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
3012922dab61SIlya Dryomov 				       int result)
3013922dab61SIlya Dryomov {
3014922dab61SIlya Dryomov 	if (!completion_done(&lreq->reg_commit_wait)) {
3015922dab61SIlya Dryomov 		lreq->reg_commit_error = (result <= 0 ? result : 0);
3016922dab61SIlya Dryomov 		complete_all(&lreq->reg_commit_wait);
3017922dab61SIlya Dryomov 	}
3018922dab61SIlya Dryomov }
3019922dab61SIlya Dryomov 
3020922dab61SIlya Dryomov static void linger_commit_cb(struct ceph_osd_request *req)
3021922dab61SIlya Dryomov {
3022922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
3023922dab61SIlya Dryomov 
3024922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
302575dbb685SIlya Dryomov 	if (req != lreq->reg_req) {
302675dbb685SIlya Dryomov 		dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n",
302775dbb685SIlya Dryomov 		     __func__, lreq, lreq->linger_id, req, lreq->reg_req);
302875dbb685SIlya Dryomov 		goto out;
302975dbb685SIlya Dryomov 	}
303075dbb685SIlya Dryomov 
3031922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
3032922dab61SIlya Dryomov 	     lreq->linger_id, req->r_result);
3033922dab61SIlya Dryomov 	linger_reg_commit_complete(lreq, req->r_result);
3034922dab61SIlya Dryomov 	lreq->committed = true;
3035922dab61SIlya Dryomov 
303619079203SIlya Dryomov 	if (!lreq->is_watch) {
303719079203SIlya Dryomov 		struct ceph_osd_data *osd_data =
303819079203SIlya Dryomov 		    osd_req_op_data(req, 0, notify, response_data);
303919079203SIlya Dryomov 		void *p = page_address(osd_data->pages[0]);
304019079203SIlya Dryomov 
304119079203SIlya Dryomov 		WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
304219079203SIlya Dryomov 			osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
304319079203SIlya Dryomov 
304419079203SIlya Dryomov 		/* make note of the notify_id */
304519079203SIlya Dryomov 		if (req->r_ops[0].outdata_len >= sizeof(u64)) {
304619079203SIlya Dryomov 			lreq->notify_id = ceph_decode_64(&p);
304719079203SIlya Dryomov 			dout("lreq %p notify_id %llu\n", lreq,
304819079203SIlya Dryomov 			     lreq->notify_id);
304919079203SIlya Dryomov 		} else {
305019079203SIlya Dryomov 			dout("lreq %p no notify_id\n", lreq);
305119079203SIlya Dryomov 		}
305219079203SIlya Dryomov 	}
305319079203SIlya Dryomov 
305475dbb685SIlya Dryomov out:
3055922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
3056922dab61SIlya Dryomov 	linger_put(lreq);
3057922dab61SIlya Dryomov }
3058922dab61SIlya Dryomov 
3059922dab61SIlya Dryomov static int normalize_watch_error(int err)
3060922dab61SIlya Dryomov {
3061922dab61SIlya Dryomov 	/*
3062922dab61SIlya Dryomov 	 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
3063922dab61SIlya Dryomov 	 * notification and a failure to reconnect because we raced with
3064922dab61SIlya Dryomov 	 * the delete appear the same to the user.
3065922dab61SIlya Dryomov 	 */
3066922dab61SIlya Dryomov 	if (err == -ENOENT)
3067922dab61SIlya Dryomov 		err = -ENOTCONN;
3068922dab61SIlya Dryomov 
3069922dab61SIlya Dryomov 	return err;
3070922dab61SIlya Dryomov }
3071922dab61SIlya Dryomov 
3072922dab61SIlya Dryomov static void linger_reconnect_cb(struct ceph_osd_request *req)
3073922dab61SIlya Dryomov {
3074922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
3075922dab61SIlya Dryomov 
3076922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
307775dbb685SIlya Dryomov 	if (req != lreq->reg_req) {
307875dbb685SIlya Dryomov 		dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n",
307975dbb685SIlya Dryomov 		     __func__, lreq, lreq->linger_id, req, lreq->reg_req);
308075dbb685SIlya Dryomov 		goto out;
308175dbb685SIlya Dryomov 	}
308275dbb685SIlya Dryomov 
3083922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
3084922dab61SIlya Dryomov 	     lreq, lreq->linger_id, req->r_result, lreq->last_error);
3085922dab61SIlya Dryomov 	if (req->r_result < 0) {
3086922dab61SIlya Dryomov 		if (!lreq->last_error) {
3087922dab61SIlya Dryomov 			lreq->last_error = normalize_watch_error(req->r_result);
3088922dab61SIlya Dryomov 			queue_watch_error(lreq);
3089922dab61SIlya Dryomov 		}
3090922dab61SIlya Dryomov 	}
3091922dab61SIlya Dryomov 
309275dbb685SIlya Dryomov out:
3093922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
3094922dab61SIlya Dryomov 	linger_put(lreq);
3095922dab61SIlya Dryomov }
3096922dab61SIlya Dryomov 
3097922dab61SIlya Dryomov static void send_linger(struct ceph_osd_linger_request *lreq)
3098922dab61SIlya Dryomov {
309975dbb685SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
310075dbb685SIlya Dryomov 	struct ceph_osd_request *req;
310175dbb685SIlya Dryomov 	int ret;
3102922dab61SIlya Dryomov 
310375dbb685SIlya Dryomov 	verify_osdc_wrlocked(osdc);
310475dbb685SIlya Dryomov 	mutex_lock(&lreq->lock);
3105922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3106922dab61SIlya Dryomov 
310775dbb685SIlya Dryomov 	if (lreq->reg_req) {
310875dbb685SIlya Dryomov 		if (lreq->reg_req->r_osd)
310975dbb685SIlya Dryomov 			cancel_linger_request(lreq->reg_req);
311075dbb685SIlya Dryomov 		ceph_osdc_put_request(lreq->reg_req);
311175dbb685SIlya Dryomov 	}
3112922dab61SIlya Dryomov 
311375dbb685SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, true, GFP_NOIO);
311475dbb685SIlya Dryomov 	BUG_ON(!req);
311575dbb685SIlya Dryomov 
31165133ba8fSIlya Dryomov 	target_copy(&req->r_t, &lreq->t);
3117922dab61SIlya Dryomov 	req->r_mtime = lreq->mtime;
3118922dab61SIlya Dryomov 
311919079203SIlya Dryomov 	if (lreq->is_watch && lreq->committed) {
312075dbb685SIlya Dryomov 		osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_RECONNECT,
312175dbb685SIlya Dryomov 				      lreq->linger_id, ++lreq->register_gen);
3122922dab61SIlya Dryomov 		dout("lreq %p reconnect register_gen %u\n", lreq,
312375dbb685SIlya Dryomov 		     req->r_ops[0].watch.gen);
3124922dab61SIlya Dryomov 		req->r_callback = linger_reconnect_cb;
3125922dab61SIlya Dryomov 	} else {
312675dbb685SIlya Dryomov 		if (lreq->is_watch) {
312775dbb685SIlya Dryomov 			osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_WATCH,
312875dbb685SIlya Dryomov 					      lreq->linger_id, 0);
312975dbb685SIlya Dryomov 		} else {
313019079203SIlya Dryomov 			lreq->notify_id = 0;
313175dbb685SIlya Dryomov 
313275dbb685SIlya Dryomov 			refcount_inc(&lreq->request_pl->refcnt);
313375dbb685SIlya Dryomov 			osd_req_op_notify_init(req, 0, lreq->linger_id,
313475dbb685SIlya Dryomov 					       lreq->request_pl);
313575dbb685SIlya Dryomov 			ceph_osd_data_pages_init(
313675dbb685SIlya Dryomov 			    osd_req_op_data(req, 0, notify, response_data),
313775dbb685SIlya Dryomov 			    lreq->notify_id_pages, PAGE_SIZE, 0, false, false);
313875dbb685SIlya Dryomov 		}
3139922dab61SIlya Dryomov 		dout("lreq %p register\n", lreq);
3140922dab61SIlya Dryomov 		req->r_callback = linger_commit_cb;
3141922dab61SIlya Dryomov 	}
314275dbb685SIlya Dryomov 
314375dbb685SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
314475dbb685SIlya Dryomov 	BUG_ON(ret);
3145922dab61SIlya Dryomov 
3146922dab61SIlya Dryomov 	req->r_priv = linger_get(lreq);
3147922dab61SIlya Dryomov 	req->r_linger = true;
314875dbb685SIlya Dryomov 	lreq->reg_req = req;
314975dbb685SIlya Dryomov 	mutex_unlock(&lreq->lock);
3150922dab61SIlya Dryomov 
3151922dab61SIlya Dryomov 	submit_request(req, true);
3152922dab61SIlya Dryomov }
3153922dab61SIlya Dryomov 
3154922dab61SIlya Dryomov static void linger_ping_cb(struct ceph_osd_request *req)
3155922dab61SIlya Dryomov {
3156922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
3157922dab61SIlya Dryomov 
3158922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
315975dbb685SIlya Dryomov 	if (req != lreq->ping_req) {
316075dbb685SIlya Dryomov 		dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n",
316175dbb685SIlya Dryomov 		     __func__, lreq, lreq->linger_id, req, lreq->ping_req);
316275dbb685SIlya Dryomov 		goto out;
316375dbb685SIlya Dryomov 	}
316475dbb685SIlya Dryomov 
3165922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
3166922dab61SIlya Dryomov 	     __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
3167922dab61SIlya Dryomov 	     lreq->last_error);
3168922dab61SIlya Dryomov 	if (lreq->register_gen == req->r_ops[0].watch.gen) {
3169b07d3c4bSIlya Dryomov 		if (!req->r_result) {
3170b07d3c4bSIlya Dryomov 			lreq->watch_valid_thru = lreq->ping_sent;
3171b07d3c4bSIlya Dryomov 		} else if (!lreq->last_error) {
3172922dab61SIlya Dryomov 			lreq->last_error = normalize_watch_error(req->r_result);
3173922dab61SIlya Dryomov 			queue_watch_error(lreq);
3174922dab61SIlya Dryomov 		}
3175922dab61SIlya Dryomov 	} else {
3176922dab61SIlya Dryomov 		dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
3177922dab61SIlya Dryomov 		     lreq->register_gen, req->r_ops[0].watch.gen);
3178922dab61SIlya Dryomov 	}
3179922dab61SIlya Dryomov 
318075dbb685SIlya Dryomov out:
3181922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
3182922dab61SIlya Dryomov 	linger_put(lreq);
3183922dab61SIlya Dryomov }
3184922dab61SIlya Dryomov 
3185922dab61SIlya Dryomov static void send_linger_ping(struct ceph_osd_linger_request *lreq)
3186922dab61SIlya Dryomov {
3187922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
318875dbb685SIlya Dryomov 	struct ceph_osd_request *req;
318975dbb685SIlya Dryomov 	int ret;
3190922dab61SIlya Dryomov 
3191b7ec35b3SIlya Dryomov 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
3192922dab61SIlya Dryomov 		dout("%s PAUSERD\n", __func__);
3193922dab61SIlya Dryomov 		return;
3194922dab61SIlya Dryomov 	}
3195922dab61SIlya Dryomov 
3196922dab61SIlya Dryomov 	lreq->ping_sent = jiffies;
3197922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
3198922dab61SIlya Dryomov 	     __func__, lreq, lreq->linger_id, lreq->ping_sent,
3199922dab61SIlya Dryomov 	     lreq->register_gen);
3200922dab61SIlya Dryomov 
320175dbb685SIlya Dryomov 	if (lreq->ping_req) {
320275dbb685SIlya Dryomov 		if (lreq->ping_req->r_osd)
320375dbb685SIlya Dryomov 			cancel_linger_request(lreq->ping_req);
320475dbb685SIlya Dryomov 		ceph_osdc_put_request(lreq->ping_req);
320575dbb685SIlya Dryomov 	}
3206922dab61SIlya Dryomov 
320775dbb685SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, true, GFP_NOIO);
320875dbb685SIlya Dryomov 	BUG_ON(!req);
320975dbb685SIlya Dryomov 
3210922dab61SIlya Dryomov 	target_copy(&req->r_t, &lreq->t);
321175dbb685SIlya Dryomov 	osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_PING, lreq->linger_id,
321275dbb685SIlya Dryomov 			      lreq->register_gen);
3213922dab61SIlya Dryomov 	req->r_callback = linger_ping_cb;
321475dbb685SIlya Dryomov 
321575dbb685SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
321675dbb685SIlya Dryomov 	BUG_ON(ret);
321775dbb685SIlya Dryomov 
3218922dab61SIlya Dryomov 	req->r_priv = linger_get(lreq);
3219922dab61SIlya Dryomov 	req->r_linger = true;
322075dbb685SIlya Dryomov 	lreq->ping_req = req;
3221922dab61SIlya Dryomov 
3222922dab61SIlya Dryomov 	ceph_osdc_get_request(req);
3223922dab61SIlya Dryomov 	account_request(req);
3224922dab61SIlya Dryomov 	req->r_tid = atomic64_inc_return(&osdc->last_tid);
3225922dab61SIlya Dryomov 	link_request(lreq->osd, req);
3226922dab61SIlya Dryomov 	send_request(req);
3227922dab61SIlya Dryomov }
3228922dab61SIlya Dryomov 
3229922dab61SIlya Dryomov static void linger_submit(struct ceph_osd_linger_request *lreq)
3230922dab61SIlya Dryomov {
3231922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3232922dab61SIlya Dryomov 	struct ceph_osd *osd;
3233922dab61SIlya Dryomov 
323481c65213SIlya Dryomov 	down_write(&osdc->lock);
323581c65213SIlya Dryomov 	linger_register(lreq);
323681c65213SIlya Dryomov 
32378edf84baSIlya Dryomov 	calc_target(osdc, &lreq->t, false);
3238922dab61SIlya Dryomov 	osd = lookup_create_osd(osdc, lreq->t.osd, true);
3239922dab61SIlya Dryomov 	link_linger(osd, lreq);
3240922dab61SIlya Dryomov 
3241922dab61SIlya Dryomov 	send_linger(lreq);
324281c65213SIlya Dryomov 	up_write(&osdc->lock);
3243922dab61SIlya Dryomov }
3244922dab61SIlya Dryomov 
32454609245eSIlya Dryomov static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
32464609245eSIlya Dryomov {
32474609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
32484609245eSIlya Dryomov 	struct ceph_osd_linger_request *lookup_lreq;
32494609245eSIlya Dryomov 
32504609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
32514609245eSIlya Dryomov 
32524609245eSIlya Dryomov 	lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
32534609245eSIlya Dryomov 				       lreq->linger_id);
32544609245eSIlya Dryomov 	if (!lookup_lreq)
32554609245eSIlya Dryomov 		return;
32564609245eSIlya Dryomov 
32574609245eSIlya Dryomov 	WARN_ON(lookup_lreq != lreq);
32584609245eSIlya Dryomov 	erase_linger_mc(&osdc->linger_map_checks, lreq);
32594609245eSIlya Dryomov 	linger_put(lreq);
32604609245eSIlya Dryomov }
32614609245eSIlya Dryomov 
3262922dab61SIlya Dryomov /*
3263922dab61SIlya Dryomov  * @lreq has to be both registered and linked.
3264922dab61SIlya Dryomov  */
3265922dab61SIlya Dryomov static void __linger_cancel(struct ceph_osd_linger_request *lreq)
3266922dab61SIlya Dryomov {
326775dbb685SIlya Dryomov 	if (lreq->ping_req && lreq->ping_req->r_osd)
3268922dab61SIlya Dryomov 		cancel_linger_request(lreq->ping_req);
326975dbb685SIlya Dryomov 	if (lreq->reg_req && lreq->reg_req->r_osd)
3270922dab61SIlya Dryomov 		cancel_linger_request(lreq->reg_req);
32714609245eSIlya Dryomov 	cancel_linger_map_check(lreq);
3272922dab61SIlya Dryomov 	unlink_linger(lreq->osd, lreq);
3273922dab61SIlya Dryomov 	linger_unregister(lreq);
3274922dab61SIlya Dryomov }
3275922dab61SIlya Dryomov 
3276922dab61SIlya Dryomov static void linger_cancel(struct ceph_osd_linger_request *lreq)
3277922dab61SIlya Dryomov {
3278922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3279922dab61SIlya Dryomov 
3280922dab61SIlya Dryomov 	down_write(&osdc->lock);
3281922dab61SIlya Dryomov 	if (__linger_registered(lreq))
3282922dab61SIlya Dryomov 		__linger_cancel(lreq);
3283922dab61SIlya Dryomov 	up_write(&osdc->lock);
3284922dab61SIlya Dryomov }
3285922dab61SIlya Dryomov 
32864609245eSIlya Dryomov static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
32874609245eSIlya Dryomov 
32884609245eSIlya Dryomov static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
32894609245eSIlya Dryomov {
32904609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
32914609245eSIlya Dryomov 	struct ceph_osdmap *map = osdc->osdmap;
32924609245eSIlya Dryomov 
32934609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
32944609245eSIlya Dryomov 	WARN_ON(!map->epoch);
32954609245eSIlya Dryomov 
32964609245eSIlya Dryomov 	if (lreq->register_gen) {
32974609245eSIlya Dryomov 		lreq->map_dne_bound = map->epoch;
32984609245eSIlya Dryomov 		dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
32994609245eSIlya Dryomov 		     lreq, lreq->linger_id);
33004609245eSIlya Dryomov 	} else {
33014609245eSIlya Dryomov 		dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
33024609245eSIlya Dryomov 		     __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
33034609245eSIlya Dryomov 		     map->epoch);
33044609245eSIlya Dryomov 	}
33054609245eSIlya Dryomov 
33064609245eSIlya Dryomov 	if (lreq->map_dne_bound) {
33074609245eSIlya Dryomov 		if (map->epoch >= lreq->map_dne_bound) {
33084609245eSIlya Dryomov 			/* we had a new enough map */
33094609245eSIlya Dryomov 			pr_info("linger_id %llu pool does not exist\n",
33104609245eSIlya Dryomov 				lreq->linger_id);
33114609245eSIlya Dryomov 			linger_reg_commit_complete(lreq, -ENOENT);
33124609245eSIlya Dryomov 			__linger_cancel(lreq);
33134609245eSIlya Dryomov 		}
33144609245eSIlya Dryomov 	} else {
33154609245eSIlya Dryomov 		send_linger_map_check(lreq);
33164609245eSIlya Dryomov 	}
33174609245eSIlya Dryomov }
33184609245eSIlya Dryomov 
33194609245eSIlya Dryomov static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
33204609245eSIlya Dryomov {
33214609245eSIlya Dryomov 	struct ceph_osd_client *osdc = &greq->monc->client->osdc;
33224609245eSIlya Dryomov 	struct ceph_osd_linger_request *lreq;
33234609245eSIlya Dryomov 	u64 linger_id = greq->private_data;
33244609245eSIlya Dryomov 
33254609245eSIlya Dryomov 	WARN_ON(greq->result || !greq->u.newest);
33264609245eSIlya Dryomov 
33274609245eSIlya Dryomov 	down_write(&osdc->lock);
33284609245eSIlya Dryomov 	lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
33294609245eSIlya Dryomov 	if (!lreq) {
33304609245eSIlya Dryomov 		dout("%s linger_id %llu dne\n", __func__, linger_id);
33314609245eSIlya Dryomov 		goto out_unlock;
33324609245eSIlya Dryomov 	}
33334609245eSIlya Dryomov 
33344609245eSIlya Dryomov 	dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
33354609245eSIlya Dryomov 	     __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
33364609245eSIlya Dryomov 	     greq->u.newest);
33374609245eSIlya Dryomov 	if (!lreq->map_dne_bound)
33384609245eSIlya Dryomov 		lreq->map_dne_bound = greq->u.newest;
33394609245eSIlya Dryomov 	erase_linger_mc(&osdc->linger_map_checks, lreq);
33404609245eSIlya Dryomov 	check_linger_pool_dne(lreq);
33414609245eSIlya Dryomov 
33424609245eSIlya Dryomov 	linger_put(lreq);
33434609245eSIlya Dryomov out_unlock:
33444609245eSIlya Dryomov 	up_write(&osdc->lock);
33454609245eSIlya Dryomov }
33464609245eSIlya Dryomov 
33474609245eSIlya Dryomov static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
33484609245eSIlya Dryomov {
33494609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
33504609245eSIlya Dryomov 	struct ceph_osd_linger_request *lookup_lreq;
33514609245eSIlya Dryomov 	int ret;
33524609245eSIlya Dryomov 
33534609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
33544609245eSIlya Dryomov 
33554609245eSIlya Dryomov 	lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
33564609245eSIlya Dryomov 				       lreq->linger_id);
33574609245eSIlya Dryomov 	if (lookup_lreq) {
33584609245eSIlya Dryomov 		WARN_ON(lookup_lreq != lreq);
33594609245eSIlya Dryomov 		return;
33604609245eSIlya Dryomov 	}
33614609245eSIlya Dryomov 
33624609245eSIlya Dryomov 	linger_get(lreq);
33634609245eSIlya Dryomov 	insert_linger_mc(&osdc->linger_map_checks, lreq);
33644609245eSIlya Dryomov 	ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
33654609245eSIlya Dryomov 					  linger_map_check_cb, lreq->linger_id);
33664609245eSIlya Dryomov 	WARN_ON(ret);
33674609245eSIlya Dryomov }
33684609245eSIlya Dryomov 
3369922dab61SIlya Dryomov static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
3370922dab61SIlya Dryomov {
3371922dab61SIlya Dryomov 	int ret;
3372922dab61SIlya Dryomov 
3373922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3374e6e28432SIlya Dryomov 	ret = wait_for_completion_killable(&lreq->reg_commit_wait);
3375922dab61SIlya Dryomov 	return ret ?: lreq->reg_commit_error;
3376922dab61SIlya Dryomov }
3377922dab61SIlya Dryomov 
3378e6e28432SIlya Dryomov static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq,
3379e6e28432SIlya Dryomov 				     unsigned long timeout)
338019079203SIlya Dryomov {
3381e6e28432SIlya Dryomov 	long left;
338219079203SIlya Dryomov 
338319079203SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3384e6e28432SIlya Dryomov 	left = wait_for_completion_killable_timeout(&lreq->notify_finish_wait,
3385e6e28432SIlya Dryomov 						ceph_timeout_jiffies(timeout));
3386e6e28432SIlya Dryomov 	if (left <= 0)
3387e6e28432SIlya Dryomov 		left = left ?: -ETIMEDOUT;
3388e6e28432SIlya Dryomov 	else
3389e6e28432SIlya Dryomov 		left = lreq->notify_finish_error; /* completed */
3390e6e28432SIlya Dryomov 
3391e6e28432SIlya Dryomov 	return left;
339219079203SIlya Dryomov }
339319079203SIlya Dryomov 
3394922dab61SIlya Dryomov /*
3395fbca9635SIlya Dryomov  * Timeout callback, called every N seconds.  When 1 or more OSD
3396fbca9635SIlya Dryomov  * requests has been active for more than N seconds, we send a keepalive
3397fbca9635SIlya Dryomov  * (tag + timestamp) to its OSD to ensure any communications channel
3398fbca9635SIlya Dryomov  * reset is detected.
33993d14c5d2SYehuda Sadeh  */
34003d14c5d2SYehuda Sadeh static void handle_timeout(struct work_struct *work)
34013d14c5d2SYehuda Sadeh {
34023d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc =
34033d14c5d2SYehuda Sadeh 		container_of(work, struct ceph_osd_client, timeout_work.work);
3404a319bf56SIlya Dryomov 	struct ceph_options *opts = osdc->client->options;
34055aea3dcdSIlya Dryomov 	unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
34067cc5e38fSIlya Dryomov 	unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
34075aea3dcdSIlya Dryomov 	LIST_HEAD(slow_osds);
34085aea3dcdSIlya Dryomov 	struct rb_node *n, *p;
34093d14c5d2SYehuda Sadeh 
34105aea3dcdSIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
34115aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
34123d14c5d2SYehuda Sadeh 
34133d14c5d2SYehuda Sadeh 	/*
34143d14c5d2SYehuda Sadeh 	 * ping osds that are a bit slow.  this ensures that if there
34153d14c5d2SYehuda Sadeh 	 * is a break in the TCP connection we will notice, and reopen
34163d14c5d2SYehuda Sadeh 	 * a connection with that osd (from the fault callback).
34173d14c5d2SYehuda Sadeh 	 */
34185aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
34195aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
34205aea3dcdSIlya Dryomov 		bool found = false;
34213d14c5d2SYehuda Sadeh 
34227cc5e38fSIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; ) {
34235aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
34245aea3dcdSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
34255aea3dcdSIlya Dryomov 
34267cc5e38fSIlya Dryomov 			p = rb_next(p); /* abort_request() */
34277cc5e38fSIlya Dryomov 
34285aea3dcdSIlya Dryomov 			if (time_before(req->r_stamp, cutoff)) {
34295aea3dcdSIlya Dryomov 				dout(" req %p tid %llu on osd%d is laggy\n",
34305aea3dcdSIlya Dryomov 				     req, req->r_tid, osd->o_osd);
34315aea3dcdSIlya Dryomov 				found = true;
34325aea3dcdSIlya Dryomov 			}
34337cc5e38fSIlya Dryomov 			if (opts->osd_request_timeout &&
34347cc5e38fSIlya Dryomov 			    time_before(req->r_start_stamp, expiry_cutoff)) {
34357cc5e38fSIlya Dryomov 				pr_err_ratelimited("tid %llu on osd%d timeout\n",
34367cc5e38fSIlya Dryomov 				       req->r_tid, osd->o_osd);
34377cc5e38fSIlya Dryomov 				abort_request(req, -ETIMEDOUT);
34387cc5e38fSIlya Dryomov 			}
34395aea3dcdSIlya Dryomov 		}
3440922dab61SIlya Dryomov 		for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
3441922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq =
3442922dab61SIlya Dryomov 			    rb_entry(p, struct ceph_osd_linger_request, node);
3443922dab61SIlya Dryomov 
3444922dab61SIlya Dryomov 			dout(" lreq %p linger_id %llu is served by osd%d\n",
3445922dab61SIlya Dryomov 			     lreq, lreq->linger_id, osd->o_osd);
3446922dab61SIlya Dryomov 			found = true;
3447922dab61SIlya Dryomov 
3448922dab61SIlya Dryomov 			mutex_lock(&lreq->lock);
344919079203SIlya Dryomov 			if (lreq->is_watch && lreq->committed && !lreq->last_error)
3450922dab61SIlya Dryomov 				send_linger_ping(lreq);
3451922dab61SIlya Dryomov 			mutex_unlock(&lreq->lock);
3452922dab61SIlya Dryomov 		}
34535aea3dcdSIlya Dryomov 
34545aea3dcdSIlya Dryomov 		if (found)
34553d14c5d2SYehuda Sadeh 			list_move_tail(&osd->o_keepalive_item, &slow_osds);
34563d14c5d2SYehuda Sadeh 	}
34575aea3dcdSIlya Dryomov 
34587cc5e38fSIlya Dryomov 	if (opts->osd_request_timeout) {
34597cc5e38fSIlya Dryomov 		for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
34607cc5e38fSIlya Dryomov 			struct ceph_osd_request *req =
34617cc5e38fSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
34627cc5e38fSIlya Dryomov 
34637cc5e38fSIlya Dryomov 			p = rb_next(p); /* abort_request() */
34647cc5e38fSIlya Dryomov 
34657cc5e38fSIlya Dryomov 			if (time_before(req->r_start_stamp, expiry_cutoff)) {
34667cc5e38fSIlya Dryomov 				pr_err_ratelimited("tid %llu on osd%d timeout\n",
34677cc5e38fSIlya Dryomov 				       req->r_tid, osdc->homeless_osd.o_osd);
34687cc5e38fSIlya Dryomov 				abort_request(req, -ETIMEDOUT);
34697cc5e38fSIlya Dryomov 			}
34707cc5e38fSIlya Dryomov 		}
34717cc5e38fSIlya Dryomov 	}
34727cc5e38fSIlya Dryomov 
34735aea3dcdSIlya Dryomov 	if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
34745aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
34755aea3dcdSIlya Dryomov 
34763d14c5d2SYehuda Sadeh 	while (!list_empty(&slow_osds)) {
34775aea3dcdSIlya Dryomov 		struct ceph_osd *osd = list_first_entry(&slow_osds,
34785aea3dcdSIlya Dryomov 							struct ceph_osd,
34793d14c5d2SYehuda Sadeh 							o_keepalive_item);
34803d14c5d2SYehuda Sadeh 		list_del_init(&osd->o_keepalive_item);
34813d14c5d2SYehuda Sadeh 		ceph_con_keepalive(&osd->o_con);
34823d14c5d2SYehuda Sadeh 	}
34833d14c5d2SYehuda Sadeh 
34845aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
3485fbca9635SIlya Dryomov 	schedule_delayed_work(&osdc->timeout_work,
3486fbca9635SIlya Dryomov 			      osdc->client->options->osd_keepalive_timeout);
34873d14c5d2SYehuda Sadeh }
34883d14c5d2SYehuda Sadeh 
34893d14c5d2SYehuda Sadeh static void handle_osds_timeout(struct work_struct *work)
34903d14c5d2SYehuda Sadeh {
34913d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc =
34923d14c5d2SYehuda Sadeh 		container_of(work, struct ceph_osd_client,
34933d14c5d2SYehuda Sadeh 			     osds_timeout_work.work);
3494a319bf56SIlya Dryomov 	unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
349542a2c09fSIlya Dryomov 	struct ceph_osd *osd, *nosd;
34963d14c5d2SYehuda Sadeh 
349742a2c09fSIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
34985aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
349942a2c09fSIlya Dryomov 	list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
350042a2c09fSIlya Dryomov 		if (time_before(jiffies, osd->lru_ttl))
350142a2c09fSIlya Dryomov 			break;
350242a2c09fSIlya Dryomov 
35035aea3dcdSIlya Dryomov 		WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
3504922dab61SIlya Dryomov 		WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
35055aea3dcdSIlya Dryomov 		close_osd(osd);
350642a2c09fSIlya Dryomov 	}
350742a2c09fSIlya Dryomov 
35085aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
35093d14c5d2SYehuda Sadeh 	schedule_delayed_work(&osdc->osds_timeout_work,
35103d14c5d2SYehuda Sadeh 			      round_jiffies_relative(delay));
35113d14c5d2SYehuda Sadeh }
35123d14c5d2SYehuda Sadeh 
3513205ee118SIlya Dryomov static int ceph_oloc_decode(void **p, void *end,
3514205ee118SIlya Dryomov 			    struct ceph_object_locator *oloc)
3515205ee118SIlya Dryomov {
3516205ee118SIlya Dryomov 	u8 struct_v, struct_cv;
3517205ee118SIlya Dryomov 	u32 len;
3518205ee118SIlya Dryomov 	void *struct_end;
3519205ee118SIlya Dryomov 	int ret = 0;
3520205ee118SIlya Dryomov 
3521205ee118SIlya Dryomov 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3522205ee118SIlya Dryomov 	struct_v = ceph_decode_8(p);
3523205ee118SIlya Dryomov 	struct_cv = ceph_decode_8(p);
3524205ee118SIlya Dryomov 	if (struct_v < 3) {
3525205ee118SIlya Dryomov 		pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
3526205ee118SIlya Dryomov 			struct_v, struct_cv);
3527205ee118SIlya Dryomov 		goto e_inval;
3528205ee118SIlya Dryomov 	}
3529205ee118SIlya Dryomov 	if (struct_cv > 6) {
3530205ee118SIlya Dryomov 		pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
3531205ee118SIlya Dryomov 			struct_v, struct_cv);
3532205ee118SIlya Dryomov 		goto e_inval;
3533205ee118SIlya Dryomov 	}
3534205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3535205ee118SIlya Dryomov 	ceph_decode_need(p, end, len, e_inval);
3536205ee118SIlya Dryomov 	struct_end = *p + len;
3537205ee118SIlya Dryomov 
3538205ee118SIlya Dryomov 	oloc->pool = ceph_decode_64(p);
3539205ee118SIlya Dryomov 	*p += 4; /* skip preferred */
3540205ee118SIlya Dryomov 
3541205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3542205ee118SIlya Dryomov 	if (len > 0) {
3543205ee118SIlya Dryomov 		pr_warn("ceph_object_locator::key is set\n");
3544205ee118SIlya Dryomov 		goto e_inval;
3545205ee118SIlya Dryomov 	}
3546205ee118SIlya Dryomov 
3547205ee118SIlya Dryomov 	if (struct_v >= 5) {
3548cd08e0a2SYan, Zheng 		bool changed = false;
3549cd08e0a2SYan, Zheng 
3550205ee118SIlya Dryomov 		len = ceph_decode_32(p);
3551205ee118SIlya Dryomov 		if (len > 0) {
355230c156d9SYan, Zheng 			ceph_decode_need(p, end, len, e_inval);
3553cd08e0a2SYan, Zheng 			if (!oloc->pool_ns ||
3554cd08e0a2SYan, Zheng 			    ceph_compare_string(oloc->pool_ns, *p, len))
3555cd08e0a2SYan, Zheng 				changed = true;
355630c156d9SYan, Zheng 			*p += len;
3557cd08e0a2SYan, Zheng 		} else {
3558cd08e0a2SYan, Zheng 			if (oloc->pool_ns)
3559cd08e0a2SYan, Zheng 				changed = true;
3560cd08e0a2SYan, Zheng 		}
3561cd08e0a2SYan, Zheng 		if (changed) {
3562cd08e0a2SYan, Zheng 			/* redirect changes namespace */
3563cd08e0a2SYan, Zheng 			pr_warn("ceph_object_locator::nspace is changed\n");
3564cd08e0a2SYan, Zheng 			goto e_inval;
3565205ee118SIlya Dryomov 		}
3566205ee118SIlya Dryomov 	}
3567205ee118SIlya Dryomov 
3568205ee118SIlya Dryomov 	if (struct_v >= 6) {
3569205ee118SIlya Dryomov 		s64 hash = ceph_decode_64(p);
3570205ee118SIlya Dryomov 		if (hash != -1) {
3571205ee118SIlya Dryomov 			pr_warn("ceph_object_locator::hash is set\n");
3572205ee118SIlya Dryomov 			goto e_inval;
3573205ee118SIlya Dryomov 		}
3574205ee118SIlya Dryomov 	}
3575205ee118SIlya Dryomov 
3576205ee118SIlya Dryomov 	/* skip the rest */
3577205ee118SIlya Dryomov 	*p = struct_end;
3578205ee118SIlya Dryomov out:
3579205ee118SIlya Dryomov 	return ret;
3580205ee118SIlya Dryomov 
3581205ee118SIlya Dryomov e_inval:
3582205ee118SIlya Dryomov 	ret = -EINVAL;
3583205ee118SIlya Dryomov 	goto out;
3584205ee118SIlya Dryomov }
3585205ee118SIlya Dryomov 
3586205ee118SIlya Dryomov static int ceph_redirect_decode(void **p, void *end,
3587205ee118SIlya Dryomov 				struct ceph_request_redirect *redir)
3588205ee118SIlya Dryomov {
3589205ee118SIlya Dryomov 	u8 struct_v, struct_cv;
3590205ee118SIlya Dryomov 	u32 len;
3591205ee118SIlya Dryomov 	void *struct_end;
3592205ee118SIlya Dryomov 	int ret;
3593205ee118SIlya Dryomov 
3594205ee118SIlya Dryomov 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3595205ee118SIlya Dryomov 	struct_v = ceph_decode_8(p);
3596205ee118SIlya Dryomov 	struct_cv = ceph_decode_8(p);
3597205ee118SIlya Dryomov 	if (struct_cv > 1) {
3598205ee118SIlya Dryomov 		pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
3599205ee118SIlya Dryomov 			struct_v, struct_cv);
3600205ee118SIlya Dryomov 		goto e_inval;
3601205ee118SIlya Dryomov 	}
3602205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3603205ee118SIlya Dryomov 	ceph_decode_need(p, end, len, e_inval);
3604205ee118SIlya Dryomov 	struct_end = *p + len;
3605205ee118SIlya Dryomov 
3606205ee118SIlya Dryomov 	ret = ceph_oloc_decode(p, end, &redir->oloc);
3607205ee118SIlya Dryomov 	if (ret)
3608205ee118SIlya Dryomov 		goto out;
3609205ee118SIlya Dryomov 
3610205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3611205ee118SIlya Dryomov 	if (len > 0) {
3612205ee118SIlya Dryomov 		pr_warn("ceph_request_redirect::object_name is set\n");
3613205ee118SIlya Dryomov 		goto e_inval;
3614205ee118SIlya Dryomov 	}
3615205ee118SIlya Dryomov 
3616205ee118SIlya Dryomov 	/* skip the rest */
3617205ee118SIlya Dryomov 	*p = struct_end;
3618205ee118SIlya Dryomov out:
3619205ee118SIlya Dryomov 	return ret;
3620205ee118SIlya Dryomov 
3621205ee118SIlya Dryomov e_inval:
3622205ee118SIlya Dryomov 	ret = -EINVAL;
3623205ee118SIlya Dryomov 	goto out;
3624205ee118SIlya Dryomov }
3625205ee118SIlya Dryomov 
3626fe5da05eSIlya Dryomov struct MOSDOpReply {
3627fe5da05eSIlya Dryomov 	struct ceph_pg pgid;
3628fe5da05eSIlya Dryomov 	u64 flags;
3629fe5da05eSIlya Dryomov 	int result;
3630fe5da05eSIlya Dryomov 	u32 epoch;
3631fe5da05eSIlya Dryomov 	int num_ops;
3632fe5da05eSIlya Dryomov 	u32 outdata_len[CEPH_OSD_MAX_OPS];
3633fe5da05eSIlya Dryomov 	s32 rval[CEPH_OSD_MAX_OPS];
3634fe5da05eSIlya Dryomov 	int retry_attempt;
3635fe5da05eSIlya Dryomov 	struct ceph_eversion replay_version;
3636fe5da05eSIlya Dryomov 	u64 user_version;
3637fe5da05eSIlya Dryomov 	struct ceph_request_redirect redirect;
3638fe5da05eSIlya Dryomov };
363925845472SSage Weil 
3640fe5da05eSIlya Dryomov static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
36413d14c5d2SYehuda Sadeh {
3642fe5da05eSIlya Dryomov 	void *p = msg->front.iov_base;
3643fe5da05eSIlya Dryomov 	void *const end = p + msg->front.iov_len;
3644fe5da05eSIlya Dryomov 	u16 version = le16_to_cpu(msg->hdr.version);
3645fe5da05eSIlya Dryomov 	struct ceph_eversion bad_replay_version;
3646b0b31a8fSIlya Dryomov 	u8 decode_redir;
3647fe5da05eSIlya Dryomov 	u32 len;
3648fe5da05eSIlya Dryomov 	int ret;
3649fe5da05eSIlya Dryomov 	int i;
36503d14c5d2SYehuda Sadeh 
3651fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, len, e_inval);
3652fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, len, e_inval);
3653fe5da05eSIlya Dryomov 	p += len; /* skip oid */
36541b83bef2SSage Weil 
3655fe5da05eSIlya Dryomov 	ret = ceph_decode_pgid(&p, end, &m->pgid);
3656fe5da05eSIlya Dryomov 	if (ret)
3657fe5da05eSIlya Dryomov 		return ret;
36581b83bef2SSage Weil 
3659fe5da05eSIlya Dryomov 	ceph_decode_64_safe(&p, end, m->flags, e_inval);
3660fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->result, e_inval);
3661fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
3662fe5da05eSIlya Dryomov 	memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
3663fe5da05eSIlya Dryomov 	p += sizeof(bad_replay_version);
3664fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->epoch, e_inval);
36651b83bef2SSage Weil 
3666fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
3667fe5da05eSIlya Dryomov 	if (m->num_ops > ARRAY_SIZE(m->outdata_len))
3668fe5da05eSIlya Dryomov 		goto e_inval;
36691b83bef2SSage Weil 
3670fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
3671fe5da05eSIlya Dryomov 			 e_inval);
3672fe5da05eSIlya Dryomov 	for (i = 0; i < m->num_ops; i++) {
36731b83bef2SSage Weil 		struct ceph_osd_op *op = p;
36741b83bef2SSage Weil 
3675fe5da05eSIlya Dryomov 		m->outdata_len[i] = le32_to_cpu(op->payload_len);
36761b83bef2SSage Weil 		p += sizeof(*op);
36771b83bef2SSage Weil 	}
3678fe5da05eSIlya Dryomov 
3679fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
3680fe5da05eSIlya Dryomov 	for (i = 0; i < m->num_ops; i++)
3681fe5da05eSIlya Dryomov 		ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
3682fe5da05eSIlya Dryomov 
3683fe5da05eSIlya Dryomov 	if (version >= 5) {
3684fe5da05eSIlya Dryomov 		ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
3685fe5da05eSIlya Dryomov 		memcpy(&m->replay_version, p, sizeof(m->replay_version));
3686fe5da05eSIlya Dryomov 		p += sizeof(m->replay_version);
3687fe5da05eSIlya Dryomov 		ceph_decode_64_safe(&p, end, m->user_version, e_inval);
3688fe5da05eSIlya Dryomov 	} else {
3689fe5da05eSIlya Dryomov 		m->replay_version = bad_replay_version; /* struct */
3690fe5da05eSIlya Dryomov 		m->user_version = le64_to_cpu(m->replay_version.version);
36911b83bef2SSage Weil 	}
36921b83bef2SSage Weil 
3693fe5da05eSIlya Dryomov 	if (version >= 6) {
3694fe5da05eSIlya Dryomov 		if (version >= 7)
3695fe5da05eSIlya Dryomov 			ceph_decode_8_safe(&p, end, decode_redir, e_inval);
3696b0b31a8fSIlya Dryomov 		else
3697b0b31a8fSIlya Dryomov 			decode_redir = 1;
3698b0b31a8fSIlya Dryomov 	} else {
3699b0b31a8fSIlya Dryomov 		decode_redir = 0;
3700b0b31a8fSIlya Dryomov 	}
3701b0b31a8fSIlya Dryomov 
3702b0b31a8fSIlya Dryomov 	if (decode_redir) {
3703fe5da05eSIlya Dryomov 		ret = ceph_redirect_decode(&p, end, &m->redirect);
3704fe5da05eSIlya Dryomov 		if (ret)
3705fe5da05eSIlya Dryomov 			return ret;
3706205ee118SIlya Dryomov 	} else {
3707fe5da05eSIlya Dryomov 		ceph_oloc_init(&m->redirect.oloc);
3708205ee118SIlya Dryomov 	}
3709205ee118SIlya Dryomov 
3710fe5da05eSIlya Dryomov 	return 0;
3711205ee118SIlya Dryomov 
3712fe5da05eSIlya Dryomov e_inval:
3713fe5da05eSIlya Dryomov 	return -EINVAL;
3714fe5da05eSIlya Dryomov }
3715fe5da05eSIlya Dryomov 
3716fe5da05eSIlya Dryomov /*
3717b18b9550SIlya Dryomov  * Handle MOSDOpReply.  Set ->r_result and call the callback if it is
3718b18b9550SIlya Dryomov  * specified.
3719fe5da05eSIlya Dryomov  */
37205aea3dcdSIlya Dryomov static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
3721fe5da05eSIlya Dryomov {
37225aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
3723fe5da05eSIlya Dryomov 	struct ceph_osd_request *req;
3724fe5da05eSIlya Dryomov 	struct MOSDOpReply m;
3725fe5da05eSIlya Dryomov 	u64 tid = le64_to_cpu(msg->hdr.tid);
3726fe5da05eSIlya Dryomov 	u32 data_len = 0;
3727fe5da05eSIlya Dryomov 	int ret;
3728fe5da05eSIlya Dryomov 	int i;
3729fe5da05eSIlya Dryomov 
3730fe5da05eSIlya Dryomov 	dout("%s msg %p tid %llu\n", __func__, msg, tid);
3731fe5da05eSIlya Dryomov 
37325aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
37335aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
37345aea3dcdSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
37355aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
3736fe5da05eSIlya Dryomov 	}
37375aea3dcdSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
37385aea3dcdSIlya Dryomov 
37395aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
37405aea3dcdSIlya Dryomov 	req = lookup_request(&osd->o_requests, tid);
37415aea3dcdSIlya Dryomov 	if (!req) {
37425aea3dcdSIlya Dryomov 		dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
37435aea3dcdSIlya Dryomov 		goto out_unlock_session;
37445aea3dcdSIlya Dryomov 	}
3745fe5da05eSIlya Dryomov 
3746cd08e0a2SYan, Zheng 	m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
3747fe5da05eSIlya Dryomov 	ret = decode_MOSDOpReply(msg, &m);
3748cd08e0a2SYan, Zheng 	m.redirect.oloc.pool_ns = NULL;
3749fe5da05eSIlya Dryomov 	if (ret) {
3750fe5da05eSIlya Dryomov 		pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
3751fe5da05eSIlya Dryomov 		       req->r_tid, ret);
3752fe5da05eSIlya Dryomov 		ceph_msg_dump(msg);
3753fe5da05eSIlya Dryomov 		goto fail_request;
3754fe5da05eSIlya Dryomov 	}
3755fe5da05eSIlya Dryomov 	dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
3756fe5da05eSIlya Dryomov 	     __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
3757fe5da05eSIlya Dryomov 	     m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
3758fe5da05eSIlya Dryomov 	     le64_to_cpu(m.replay_version.version), m.user_version);
3759fe5da05eSIlya Dryomov 
3760fe5da05eSIlya Dryomov 	if (m.retry_attempt >= 0) {
3761fe5da05eSIlya Dryomov 		if (m.retry_attempt != req->r_attempts - 1) {
3762fe5da05eSIlya Dryomov 			dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
3763fe5da05eSIlya Dryomov 			     req, req->r_tid, m.retry_attempt,
3764fe5da05eSIlya Dryomov 			     req->r_attempts - 1);
37655aea3dcdSIlya Dryomov 			goto out_unlock_session;
3766fe5da05eSIlya Dryomov 		}
3767fe5da05eSIlya Dryomov 	} else {
3768fe5da05eSIlya Dryomov 		WARN_ON(1); /* MOSDOpReply v4 is assumed */
3769fe5da05eSIlya Dryomov 	}
3770fe5da05eSIlya Dryomov 
3771fe5da05eSIlya Dryomov 	if (!ceph_oloc_empty(&m.redirect.oloc)) {
3772fe5da05eSIlya Dryomov 		dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
3773fe5da05eSIlya Dryomov 		     m.redirect.oloc.pool);
37745aea3dcdSIlya Dryomov 		unlink_request(osd, req);
37755aea3dcdSIlya Dryomov 		mutex_unlock(&osd->lock);
3776205ee118SIlya Dryomov 
377730c156d9SYan, Zheng 		/*
377830c156d9SYan, Zheng 		 * Not ceph_oloc_copy() - changing pool_ns is not
377930c156d9SYan, Zheng 		 * supported.
378030c156d9SYan, Zheng 		 */
378130c156d9SYan, Zheng 		req->r_t.target_oloc.pool = m.redirect.oloc.pool;
3782890bd0f8SJerry Lee 		req->r_flags |= CEPH_OSD_FLAG_REDIRECTED |
3783890bd0f8SJerry Lee 				CEPH_OSD_FLAG_IGNORE_OVERLAY |
3784890bd0f8SJerry Lee 				CEPH_OSD_FLAG_IGNORE_CACHE;
37855aea3dcdSIlya Dryomov 		req->r_tid = 0;
37865aea3dcdSIlya Dryomov 		__submit_request(req, false);
37875aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
3788205ee118SIlya Dryomov 	}
3789205ee118SIlya Dryomov 
3790117d96a0SIlya Dryomov 	if (m.result == -EAGAIN) {
3791117d96a0SIlya Dryomov 		dout("req %p tid %llu EAGAIN\n", req, req->r_tid);
3792117d96a0SIlya Dryomov 		unlink_request(osd, req);
3793117d96a0SIlya Dryomov 		mutex_unlock(&osd->lock);
3794117d96a0SIlya Dryomov 
3795117d96a0SIlya Dryomov 		/*
3796117d96a0SIlya Dryomov 		 * The object is missing on the replica or not (yet)
3797117d96a0SIlya Dryomov 		 * readable.  Clear pgid to force a resend to the primary
3798117d96a0SIlya Dryomov 		 * via legacy_change.
3799117d96a0SIlya Dryomov 		 */
3800117d96a0SIlya Dryomov 		req->r_t.pgid.pool = 0;
3801117d96a0SIlya Dryomov 		req->r_t.pgid.seed = 0;
3802117d96a0SIlya Dryomov 		WARN_ON(!req->r_t.used_replica);
3803117d96a0SIlya Dryomov 		req->r_flags &= ~(CEPH_OSD_FLAG_BALANCE_READS |
3804117d96a0SIlya Dryomov 				  CEPH_OSD_FLAG_LOCALIZE_READS);
3805117d96a0SIlya Dryomov 		req->r_tid = 0;
3806117d96a0SIlya Dryomov 		__submit_request(req, false);
3807117d96a0SIlya Dryomov 		goto out_unlock_osdc;
3808117d96a0SIlya Dryomov 	}
3809117d96a0SIlya Dryomov 
3810fe5da05eSIlya Dryomov 	if (m.num_ops != req->r_num_ops) {
3811fe5da05eSIlya Dryomov 		pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
3812fe5da05eSIlya Dryomov 		       req->r_num_ops, req->r_tid);
3813fe5da05eSIlya Dryomov 		goto fail_request;
3814fe5da05eSIlya Dryomov 	}
3815fe5da05eSIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
3816fe5da05eSIlya Dryomov 		dout(" req %p tid %llu op %d rval %d len %u\n", req,
3817fe5da05eSIlya Dryomov 		     req->r_tid, i, m.rval[i], m.outdata_len[i]);
3818fe5da05eSIlya Dryomov 		req->r_ops[i].rval = m.rval[i];
3819fe5da05eSIlya Dryomov 		req->r_ops[i].outdata_len = m.outdata_len[i];
3820fe5da05eSIlya Dryomov 		data_len += m.outdata_len[i];
3821fe5da05eSIlya Dryomov 	}
3822fe5da05eSIlya Dryomov 	if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3823fe5da05eSIlya Dryomov 		pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3824fe5da05eSIlya Dryomov 		       le32_to_cpu(msg->hdr.data_len), req->r_tid);
3825fe5da05eSIlya Dryomov 		goto fail_request;
3826fe5da05eSIlya Dryomov 	}
3827b18b9550SIlya Dryomov 	dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3828b18b9550SIlya Dryomov 	     req, req->r_tid, m.result, data_len);
38293d14c5d2SYehuda Sadeh 
3830b18b9550SIlya Dryomov 	/*
3831b18b9550SIlya Dryomov 	 * Since we only ever request ONDISK, we should only ever get
3832b18b9550SIlya Dryomov 	 * one (type of) reply back.
3833b18b9550SIlya Dryomov 	 */
3834b18b9550SIlya Dryomov 	WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3835fe5da05eSIlya Dryomov 	req->r_result = m.result ?: data_len;
383645ee2c1dSIlya Dryomov 	finish_request(req);
38375aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
38385aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
38393d14c5d2SYehuda Sadeh 
3840fe5da05eSIlya Dryomov 	__complete_request(req);
38413d14c5d2SYehuda Sadeh 	return;
3842fe5da05eSIlya Dryomov 
3843fe5da05eSIlya Dryomov fail_request:
38444609245eSIlya Dryomov 	complete_request(req, -EIO);
38455aea3dcdSIlya Dryomov out_unlock_session:
38465aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
38475aea3dcdSIlya Dryomov out_unlock_osdc:
38485aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
38493d14c5d2SYehuda Sadeh }
38503d14c5d2SYehuda Sadeh 
385142c1b124SIlya Dryomov static void set_pool_was_full(struct ceph_osd_client *osdc)
385242c1b124SIlya Dryomov {
385342c1b124SIlya Dryomov 	struct rb_node *n;
385442c1b124SIlya Dryomov 
385542c1b124SIlya Dryomov 	for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
385642c1b124SIlya Dryomov 		struct ceph_pg_pool_info *pi =
385742c1b124SIlya Dryomov 		    rb_entry(n, struct ceph_pg_pool_info, node);
385842c1b124SIlya Dryomov 
385942c1b124SIlya Dryomov 		pi->was_full = __pool_full(pi);
386042c1b124SIlya Dryomov 	}
386142c1b124SIlya Dryomov }
386242c1b124SIlya Dryomov 
38635aea3dcdSIlya Dryomov static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
38643d14c5d2SYehuda Sadeh {
38655aea3dcdSIlya Dryomov 	struct ceph_pg_pool_info *pi;
38663d14c5d2SYehuda Sadeh 
38675aea3dcdSIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
38685aea3dcdSIlya Dryomov 	if (!pi)
38695aea3dcdSIlya Dryomov 		return false;
38703d14c5d2SYehuda Sadeh 
38715aea3dcdSIlya Dryomov 	return pi->was_full && !__pool_full(pi);
38723d14c5d2SYehuda Sadeh }
38733d14c5d2SYehuda Sadeh 
3874922dab61SIlya Dryomov static enum calc_target_result
3875922dab61SIlya Dryomov recalc_linger_target(struct ceph_osd_linger_request *lreq)
3876922dab61SIlya Dryomov {
3877922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3878922dab61SIlya Dryomov 	enum calc_target_result ct_res;
3879922dab61SIlya Dryomov 
38808edf84baSIlya Dryomov 	ct_res = calc_target(osdc, &lreq->t, true);
3881922dab61SIlya Dryomov 	if (ct_res == CALC_TARGET_NEED_RESEND) {
3882922dab61SIlya Dryomov 		struct ceph_osd *osd;
3883922dab61SIlya Dryomov 
3884922dab61SIlya Dryomov 		osd = lookup_create_osd(osdc, lreq->t.osd, true);
3885922dab61SIlya Dryomov 		if (osd != lreq->osd) {
3886922dab61SIlya Dryomov 			unlink_linger(lreq->osd, lreq);
3887922dab61SIlya Dryomov 			link_linger(osd, lreq);
3888922dab61SIlya Dryomov 		}
3889922dab61SIlya Dryomov 	}
3890922dab61SIlya Dryomov 
3891922dab61SIlya Dryomov 	return ct_res;
3892922dab61SIlya Dryomov }
3893922dab61SIlya Dryomov 
38943d14c5d2SYehuda Sadeh /*
38955aea3dcdSIlya Dryomov  * Requeue requests whose mapping to an OSD has changed.
38963d14c5d2SYehuda Sadeh  */
38975aea3dcdSIlya Dryomov static void scan_requests(struct ceph_osd *osd,
38985aea3dcdSIlya Dryomov 			  bool force_resend,
38995aea3dcdSIlya Dryomov 			  bool cleared_full,
39005aea3dcdSIlya Dryomov 			  bool check_pool_cleared_full,
39015aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
39025aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
39033d14c5d2SYehuda Sadeh {
39045aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
39055aea3dcdSIlya Dryomov 	struct rb_node *n;
39065aea3dcdSIlya Dryomov 	bool force_resend_writes;
39073d14c5d2SYehuda Sadeh 
3908922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; ) {
3909922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
3910922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
3911922dab61SIlya Dryomov 		enum calc_target_result ct_res;
3912922dab61SIlya Dryomov 
3913922dab61SIlya Dryomov 		n = rb_next(n); /* recalc_linger_target() */
3914922dab61SIlya Dryomov 
3915922dab61SIlya Dryomov 		dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3916922dab61SIlya Dryomov 		     lreq->linger_id);
3917922dab61SIlya Dryomov 		ct_res = recalc_linger_target(lreq);
3918922dab61SIlya Dryomov 		switch (ct_res) {
3919922dab61SIlya Dryomov 		case CALC_TARGET_NO_ACTION:
3920922dab61SIlya Dryomov 			force_resend_writes = cleared_full ||
3921922dab61SIlya Dryomov 			    (check_pool_cleared_full &&
3922922dab61SIlya Dryomov 			     pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3923922dab61SIlya Dryomov 			if (!force_resend && !force_resend_writes)
3924922dab61SIlya Dryomov 				break;
3925922dab61SIlya Dryomov 
3926df561f66SGustavo A. R. Silva 			fallthrough;
3927922dab61SIlya Dryomov 		case CALC_TARGET_NEED_RESEND:
39284609245eSIlya Dryomov 			cancel_linger_map_check(lreq);
3929922dab61SIlya Dryomov 			/*
3930922dab61SIlya Dryomov 			 * scan_requests() for the previous epoch(s)
3931922dab61SIlya Dryomov 			 * may have already added it to the list, since
3932922dab61SIlya Dryomov 			 * it's not unlinked here.
3933922dab61SIlya Dryomov 			 */
3934922dab61SIlya Dryomov 			if (list_empty(&lreq->scan_item))
3935922dab61SIlya Dryomov 				list_add_tail(&lreq->scan_item, need_resend_linger);
3936922dab61SIlya Dryomov 			break;
3937922dab61SIlya Dryomov 		case CALC_TARGET_POOL_DNE:
3938a10bcb19SIlya Dryomov 			list_del_init(&lreq->scan_item);
39394609245eSIlya Dryomov 			check_linger_pool_dne(lreq);
3940922dab61SIlya Dryomov 			break;
3941922dab61SIlya Dryomov 		}
3942922dab61SIlya Dryomov 	}
3943922dab61SIlya Dryomov 
39445aea3dcdSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
39455aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
39465aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
39475aea3dcdSIlya Dryomov 		enum calc_target_result ct_res;
3948ab60b16dSAlex Elder 
39494609245eSIlya Dryomov 		n = rb_next(n); /* unlink_request(), check_pool_dne() */
3950ab60b16dSAlex Elder 
39515aea3dcdSIlya Dryomov 		dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
39528edf84baSIlya Dryomov 		ct_res = calc_target(osdc, &req->r_t, false);
39535aea3dcdSIlya Dryomov 		switch (ct_res) {
39545aea3dcdSIlya Dryomov 		case CALC_TARGET_NO_ACTION:
39555aea3dcdSIlya Dryomov 			force_resend_writes = cleared_full ||
39565aea3dcdSIlya Dryomov 			    (check_pool_cleared_full &&
39575aea3dcdSIlya Dryomov 			     pool_cleared_full(osdc, req->r_t.base_oloc.pool));
39585aea3dcdSIlya Dryomov 			if (!force_resend &&
39595aea3dcdSIlya Dryomov 			    (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
39605aea3dcdSIlya Dryomov 			     !force_resend_writes))
39615aea3dcdSIlya Dryomov 				break;
3962a40c4f10SYehuda Sadeh 
3963df561f66SGustavo A. R. Silva 			fallthrough;
39645aea3dcdSIlya Dryomov 		case CALC_TARGET_NEED_RESEND:
39654609245eSIlya Dryomov 			cancel_map_check(req);
39665aea3dcdSIlya Dryomov 			unlink_request(osd, req);
39675aea3dcdSIlya Dryomov 			insert_request(need_resend, req);
39685aea3dcdSIlya Dryomov 			break;
39695aea3dcdSIlya Dryomov 		case CALC_TARGET_POOL_DNE:
39704609245eSIlya Dryomov 			check_pool_dne(req);
39715aea3dcdSIlya Dryomov 			break;
3972a40c4f10SYehuda Sadeh 		}
39733d14c5d2SYehuda Sadeh 	}
39743d14c5d2SYehuda Sadeh }
39756f6c7006SSage Weil 
397642c1b124SIlya Dryomov static int handle_one_map(struct ceph_osd_client *osdc,
39775aea3dcdSIlya Dryomov 			  void *p, void *end, bool incremental,
39785aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
39795aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
398042c1b124SIlya Dryomov {
398142c1b124SIlya Dryomov 	struct ceph_osdmap *newmap;
398242c1b124SIlya Dryomov 	struct rb_node *n;
398342c1b124SIlya Dryomov 	bool skipped_map = false;
398442c1b124SIlya Dryomov 	bool was_full;
398542c1b124SIlya Dryomov 
3986b7ec35b3SIlya Dryomov 	was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
398742c1b124SIlya Dryomov 	set_pool_was_full(osdc);
398842c1b124SIlya Dryomov 
398942c1b124SIlya Dryomov 	if (incremental)
3990cd1a677cSIlya Dryomov 		newmap = osdmap_apply_incremental(&p, end,
3991cd1a677cSIlya Dryomov 						  ceph_msgr2(osdc->client),
3992cd1a677cSIlya Dryomov 						  osdc->osdmap);
399342c1b124SIlya Dryomov 	else
3994cd1a677cSIlya Dryomov 		newmap = ceph_osdmap_decode(&p, end, ceph_msgr2(osdc->client));
399542c1b124SIlya Dryomov 	if (IS_ERR(newmap))
399642c1b124SIlya Dryomov 		return PTR_ERR(newmap);
399742c1b124SIlya Dryomov 
399842c1b124SIlya Dryomov 	if (newmap != osdc->osdmap) {
399942c1b124SIlya Dryomov 		/*
400042c1b124SIlya Dryomov 		 * Preserve ->was_full before destroying the old map.
400142c1b124SIlya Dryomov 		 * For pools that weren't in the old map, ->was_full
400242c1b124SIlya Dryomov 		 * should be false.
400342c1b124SIlya Dryomov 		 */
400442c1b124SIlya Dryomov 		for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
400542c1b124SIlya Dryomov 			struct ceph_pg_pool_info *pi =
400642c1b124SIlya Dryomov 			    rb_entry(n, struct ceph_pg_pool_info, node);
400742c1b124SIlya Dryomov 			struct ceph_pg_pool_info *old_pi;
400842c1b124SIlya Dryomov 
400942c1b124SIlya Dryomov 			old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
401042c1b124SIlya Dryomov 			if (old_pi)
401142c1b124SIlya Dryomov 				pi->was_full = old_pi->was_full;
401242c1b124SIlya Dryomov 			else
401342c1b124SIlya Dryomov 				WARN_ON(pi->was_full);
401442c1b124SIlya Dryomov 		}
401542c1b124SIlya Dryomov 
401642c1b124SIlya Dryomov 		if (osdc->osdmap->epoch &&
401742c1b124SIlya Dryomov 		    osdc->osdmap->epoch + 1 < newmap->epoch) {
401842c1b124SIlya Dryomov 			WARN_ON(incremental);
401942c1b124SIlya Dryomov 			skipped_map = true;
402042c1b124SIlya Dryomov 		}
402142c1b124SIlya Dryomov 
402242c1b124SIlya Dryomov 		ceph_osdmap_destroy(osdc->osdmap);
402342c1b124SIlya Dryomov 		osdc->osdmap = newmap;
402442c1b124SIlya Dryomov 	}
402542c1b124SIlya Dryomov 
4026b7ec35b3SIlya Dryomov 	was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
40275aea3dcdSIlya Dryomov 	scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
40285aea3dcdSIlya Dryomov 		      need_resend, need_resend_linger);
40295aea3dcdSIlya Dryomov 
40305aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; ) {
40315aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
40325aea3dcdSIlya Dryomov 
40335aea3dcdSIlya Dryomov 		n = rb_next(n); /* close_osd() */
40345aea3dcdSIlya Dryomov 
40355aea3dcdSIlya Dryomov 		scan_requests(osd, skipped_map, was_full, true, need_resend,
40365aea3dcdSIlya Dryomov 			      need_resend_linger);
40375aea3dcdSIlya Dryomov 		if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
40385aea3dcdSIlya Dryomov 		    memcmp(&osd->o_con.peer_addr,
40395aea3dcdSIlya Dryomov 			   ceph_osd_addr(osdc->osdmap, osd->o_osd),
40405aea3dcdSIlya Dryomov 			   sizeof(struct ceph_entity_addr)))
40415aea3dcdSIlya Dryomov 			close_osd(osd);
40425aea3dcdSIlya Dryomov 	}
404342c1b124SIlya Dryomov 
404442c1b124SIlya Dryomov 	return 0;
404542c1b124SIlya Dryomov }
40466f6c7006SSage Weil 
40475aea3dcdSIlya Dryomov static void kick_requests(struct ceph_osd_client *osdc,
40485aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
40495aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
40505aea3dcdSIlya Dryomov {
4051922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq, *nlreq;
405204c7d789SIlya Dryomov 	enum calc_target_result ct_res;
40535aea3dcdSIlya Dryomov 	struct rb_node *n;
40545aea3dcdSIlya Dryomov 
405504c7d789SIlya Dryomov 	/* make sure need_resend targets reflect latest map */
405604c7d789SIlya Dryomov 	for (n = rb_first(need_resend); n; ) {
405704c7d789SIlya Dryomov 		struct ceph_osd_request *req =
405804c7d789SIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
405904c7d789SIlya Dryomov 
406004c7d789SIlya Dryomov 		n = rb_next(n);
406104c7d789SIlya Dryomov 
406204c7d789SIlya Dryomov 		if (req->r_t.epoch < osdc->osdmap->epoch) {
40638edf84baSIlya Dryomov 			ct_res = calc_target(osdc, &req->r_t, false);
406404c7d789SIlya Dryomov 			if (ct_res == CALC_TARGET_POOL_DNE) {
406504c7d789SIlya Dryomov 				erase_request(need_resend, req);
406604c7d789SIlya Dryomov 				check_pool_dne(req);
406704c7d789SIlya Dryomov 			}
406804c7d789SIlya Dryomov 		}
406904c7d789SIlya Dryomov 	}
407004c7d789SIlya Dryomov 
40715aea3dcdSIlya Dryomov 	for (n = rb_first(need_resend); n; ) {
40725aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
40735aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
40745aea3dcdSIlya Dryomov 		struct ceph_osd *osd;
40755aea3dcdSIlya Dryomov 
40765aea3dcdSIlya Dryomov 		n = rb_next(n);
40775aea3dcdSIlya Dryomov 		erase_request(need_resend, req); /* before link_request() */
40785aea3dcdSIlya Dryomov 
40795aea3dcdSIlya Dryomov 		osd = lookup_create_osd(osdc, req->r_t.osd, true);
40805aea3dcdSIlya Dryomov 		link_request(osd, req);
40815aea3dcdSIlya Dryomov 		if (!req->r_linger) {
40825aea3dcdSIlya Dryomov 			if (!osd_homeless(osd) && !req->r_t.paused)
40835aea3dcdSIlya Dryomov 				send_request(req);
4084922dab61SIlya Dryomov 		} else {
4085922dab61SIlya Dryomov 			cancel_linger_request(req);
40865aea3dcdSIlya Dryomov 		}
40875aea3dcdSIlya Dryomov 	}
4088922dab61SIlya Dryomov 
4089922dab61SIlya Dryomov 	list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
4090922dab61SIlya Dryomov 		if (!osd_homeless(lreq->osd))
4091922dab61SIlya Dryomov 			send_linger(lreq);
4092922dab61SIlya Dryomov 
4093922dab61SIlya Dryomov 		list_del_init(&lreq->scan_item);
4094922dab61SIlya Dryomov 	}
40955aea3dcdSIlya Dryomov }
40965aea3dcdSIlya Dryomov 
40973d14c5d2SYehuda Sadeh /*
40983d14c5d2SYehuda Sadeh  * Process updated osd map.
40993d14c5d2SYehuda Sadeh  *
41003d14c5d2SYehuda Sadeh  * The message contains any number of incremental and full maps, normally
41013d14c5d2SYehuda Sadeh  * indicating some sort of topology change in the cluster.  Kick requests
41023d14c5d2SYehuda Sadeh  * off to different OSDs as needed.
41033d14c5d2SYehuda Sadeh  */
41043d14c5d2SYehuda Sadeh void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
41053d14c5d2SYehuda Sadeh {
410642c1b124SIlya Dryomov 	void *p = msg->front.iov_base;
410742c1b124SIlya Dryomov 	void *const end = p + msg->front.iov_len;
41083d14c5d2SYehuda Sadeh 	u32 nr_maps, maplen;
41093d14c5d2SYehuda Sadeh 	u32 epoch;
41103d14c5d2SYehuda Sadeh 	struct ceph_fsid fsid;
41115aea3dcdSIlya Dryomov 	struct rb_root need_resend = RB_ROOT;
41125aea3dcdSIlya Dryomov 	LIST_HEAD(need_resend_linger);
411342c1b124SIlya Dryomov 	bool handled_incremental = false;
411442c1b124SIlya Dryomov 	bool was_pauserd, was_pausewr;
411542c1b124SIlya Dryomov 	bool pauserd, pausewr;
411642c1b124SIlya Dryomov 	int err;
41173d14c5d2SYehuda Sadeh 
411842c1b124SIlya Dryomov 	dout("%s have %u\n", __func__, osdc->osdmap->epoch);
41195aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
41203d14c5d2SYehuda Sadeh 
41213d14c5d2SYehuda Sadeh 	/* verify fsid */
41223d14c5d2SYehuda Sadeh 	ceph_decode_need(&p, end, sizeof(fsid), bad);
41233d14c5d2SYehuda Sadeh 	ceph_decode_copy(&p, &fsid, sizeof(fsid));
41243d14c5d2SYehuda Sadeh 	if (ceph_check_fsid(osdc->client, &fsid) < 0)
412542c1b124SIlya Dryomov 		goto bad;
41263d14c5d2SYehuda Sadeh 
4127b7ec35b3SIlya Dryomov 	was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
4128b7ec35b3SIlya Dryomov 	was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
4129b7ec35b3SIlya Dryomov 		      ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
413042c1b124SIlya Dryomov 		      have_pool_full(osdc);
41319a1ea2dbSJosh Durgin 
41323d14c5d2SYehuda Sadeh 	/* incremental maps */
41333d14c5d2SYehuda Sadeh 	ceph_decode_32_safe(&p, end, nr_maps, bad);
41343d14c5d2SYehuda Sadeh 	dout(" %d inc maps\n", nr_maps);
41353d14c5d2SYehuda Sadeh 	while (nr_maps > 0) {
41363d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
41373d14c5d2SYehuda Sadeh 		epoch = ceph_decode_32(&p);
41383d14c5d2SYehuda Sadeh 		maplen = ceph_decode_32(&p);
41393d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, maplen, bad);
414042c1b124SIlya Dryomov 		if (osdc->osdmap->epoch &&
414142c1b124SIlya Dryomov 		    osdc->osdmap->epoch + 1 == epoch) {
41423d14c5d2SYehuda Sadeh 			dout("applying incremental map %u len %d\n",
41433d14c5d2SYehuda Sadeh 			     epoch, maplen);
41445aea3dcdSIlya Dryomov 			err = handle_one_map(osdc, p, p + maplen, true,
41455aea3dcdSIlya Dryomov 					     &need_resend, &need_resend_linger);
414642c1b124SIlya Dryomov 			if (err)
41473d14c5d2SYehuda Sadeh 				goto bad;
414842c1b124SIlya Dryomov 			handled_incremental = true;
41493d14c5d2SYehuda Sadeh 		} else {
41503d14c5d2SYehuda Sadeh 			dout("ignoring incremental map %u len %d\n",
41513d14c5d2SYehuda Sadeh 			     epoch, maplen);
41523d14c5d2SYehuda Sadeh 		}
415342c1b124SIlya Dryomov 		p += maplen;
41543d14c5d2SYehuda Sadeh 		nr_maps--;
41553d14c5d2SYehuda Sadeh 	}
415642c1b124SIlya Dryomov 	if (handled_incremental)
41573d14c5d2SYehuda Sadeh 		goto done;
41583d14c5d2SYehuda Sadeh 
41593d14c5d2SYehuda Sadeh 	/* full maps */
41603d14c5d2SYehuda Sadeh 	ceph_decode_32_safe(&p, end, nr_maps, bad);
41613d14c5d2SYehuda Sadeh 	dout(" %d full maps\n", nr_maps);
41623d14c5d2SYehuda Sadeh 	while (nr_maps) {
41633d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
41643d14c5d2SYehuda Sadeh 		epoch = ceph_decode_32(&p);
41653d14c5d2SYehuda Sadeh 		maplen = ceph_decode_32(&p);
41663d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, maplen, bad);
41673d14c5d2SYehuda Sadeh 		if (nr_maps > 1) {
41683d14c5d2SYehuda Sadeh 			dout("skipping non-latest full map %u len %d\n",
41693d14c5d2SYehuda Sadeh 			     epoch, maplen);
4170e5253a7bSIlya Dryomov 		} else if (osdc->osdmap->epoch >= epoch) {
41713d14c5d2SYehuda Sadeh 			dout("skipping full map %u len %d, "
41723d14c5d2SYehuda Sadeh 			     "older than our %u\n", epoch, maplen,
41733d14c5d2SYehuda Sadeh 			     osdc->osdmap->epoch);
41743d14c5d2SYehuda Sadeh 		} else {
41753d14c5d2SYehuda Sadeh 			dout("taking full map %u len %d\n", epoch, maplen);
41765aea3dcdSIlya Dryomov 			err = handle_one_map(osdc, p, p + maplen, false,
41775aea3dcdSIlya Dryomov 					     &need_resend, &need_resend_linger);
417842c1b124SIlya Dryomov 			if (err)
41793d14c5d2SYehuda Sadeh 				goto bad;
41803d14c5d2SYehuda Sadeh 		}
41813d14c5d2SYehuda Sadeh 		p += maplen;
41823d14c5d2SYehuda Sadeh 		nr_maps--;
41833d14c5d2SYehuda Sadeh 	}
41843d14c5d2SYehuda Sadeh 
41853d14c5d2SYehuda Sadeh done:
4186cd634fb6SSage Weil 	/*
4187cd634fb6SSage Weil 	 * subscribe to subsequent osdmap updates if full to ensure
4188cd634fb6SSage Weil 	 * we find out when we are no longer full and stop returning
4189cd634fb6SSage Weil 	 * ENOSPC.
4190cd634fb6SSage Weil 	 */
4191b7ec35b3SIlya Dryomov 	pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
4192b7ec35b3SIlya Dryomov 	pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
4193b7ec35b3SIlya Dryomov 		  ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
419442c1b124SIlya Dryomov 		  have_pool_full(osdc);
419558eb7932SJeff Layton 	if (was_pauserd || was_pausewr || pauserd || pausewr ||
419658eb7932SJeff Layton 	    osdc->osdmap->epoch < osdc->epoch_barrier)
419742c1b124SIlya Dryomov 		maybe_request_map(osdc);
4198cd634fb6SSage Weil 
41995aea3dcdSIlya Dryomov 	kick_requests(osdc, &need_resend, &need_resend_linger);
420042c1b124SIlya Dryomov 
4201fc36d0a4SJeff Layton 	ceph_osdc_abort_on_full(osdc);
420242c1b124SIlya Dryomov 	ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
420342c1b124SIlya Dryomov 			  osdc->osdmap->epoch);
42045aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
42053d14c5d2SYehuda Sadeh 	wake_up_all(&osdc->client->auth_wq);
42063d14c5d2SYehuda Sadeh 	return;
42073d14c5d2SYehuda Sadeh 
42083d14c5d2SYehuda Sadeh bad:
42093d14c5d2SYehuda Sadeh 	pr_err("osdc handle_map corrupt msg\n");
42103d14c5d2SYehuda Sadeh 	ceph_msg_dump(msg);
42115aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
42125aea3dcdSIlya Dryomov }
42135aea3dcdSIlya Dryomov 
42145aea3dcdSIlya Dryomov /*
42155aea3dcdSIlya Dryomov  * Resubmit requests pending on the given osd.
42165aea3dcdSIlya Dryomov  */
42175aea3dcdSIlya Dryomov static void kick_osd_requests(struct ceph_osd *osd)
42185aea3dcdSIlya Dryomov {
42195aea3dcdSIlya Dryomov 	struct rb_node *n;
42205aea3dcdSIlya Dryomov 
4221a02a946dSIlya Dryomov 	clear_backoffs(osd);
4222a02a946dSIlya Dryomov 
4223922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
42245aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
42255aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
42265aea3dcdSIlya Dryomov 
4227922dab61SIlya Dryomov 		n = rb_next(n); /* cancel_linger_request() */
4228922dab61SIlya Dryomov 
42295aea3dcdSIlya Dryomov 		if (!req->r_linger) {
42305aea3dcdSIlya Dryomov 			if (!req->r_t.paused)
42315aea3dcdSIlya Dryomov 				send_request(req);
4232922dab61SIlya Dryomov 		} else {
4233922dab61SIlya Dryomov 			cancel_linger_request(req);
42345aea3dcdSIlya Dryomov 		}
42355aea3dcdSIlya Dryomov 	}
4236922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
4237922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
4238922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
4239922dab61SIlya Dryomov 
4240922dab61SIlya Dryomov 		send_linger(lreq);
4241922dab61SIlya Dryomov 	}
42425aea3dcdSIlya Dryomov }
42435aea3dcdSIlya Dryomov 
42445aea3dcdSIlya Dryomov /*
42455aea3dcdSIlya Dryomov  * If the osd connection drops, we need to resubmit all requests.
42465aea3dcdSIlya Dryomov  */
42475aea3dcdSIlya Dryomov static void osd_fault(struct ceph_connection *con)
42485aea3dcdSIlya Dryomov {
42495aea3dcdSIlya Dryomov 	struct ceph_osd *osd = con->private;
42505aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
42515aea3dcdSIlya Dryomov 
42525aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
42535aea3dcdSIlya Dryomov 
42545aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
42555aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
42565aea3dcdSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
42575aea3dcdSIlya Dryomov 		goto out_unlock;
42585aea3dcdSIlya Dryomov 	}
42595aea3dcdSIlya Dryomov 
42605aea3dcdSIlya Dryomov 	if (!reopen_osd(osd))
42615aea3dcdSIlya Dryomov 		kick_osd_requests(osd);
42625aea3dcdSIlya Dryomov 	maybe_request_map(osdc);
42635aea3dcdSIlya Dryomov 
42645aea3dcdSIlya Dryomov out_unlock:
42655aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
42663d14c5d2SYehuda Sadeh }
42673d14c5d2SYehuda Sadeh 
4268a02a946dSIlya Dryomov struct MOSDBackoff {
4269a02a946dSIlya Dryomov 	struct ceph_spg spgid;
4270a02a946dSIlya Dryomov 	u32 map_epoch;
4271a02a946dSIlya Dryomov 	u8 op;
4272a02a946dSIlya Dryomov 	u64 id;
4273a02a946dSIlya Dryomov 	struct ceph_hobject_id *begin;
4274a02a946dSIlya Dryomov 	struct ceph_hobject_id *end;
4275a02a946dSIlya Dryomov };
4276a02a946dSIlya Dryomov 
4277a02a946dSIlya Dryomov static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m)
4278a02a946dSIlya Dryomov {
4279a02a946dSIlya Dryomov 	void *p = msg->front.iov_base;
4280a02a946dSIlya Dryomov 	void *const end = p + msg->front.iov_len;
4281a02a946dSIlya Dryomov 	u8 struct_v;
4282a02a946dSIlya Dryomov 	u32 struct_len;
4283a02a946dSIlya Dryomov 	int ret;
4284a02a946dSIlya Dryomov 
4285a02a946dSIlya Dryomov 	ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len);
4286a02a946dSIlya Dryomov 	if (ret)
4287a02a946dSIlya Dryomov 		return ret;
4288a02a946dSIlya Dryomov 
4289a02a946dSIlya Dryomov 	ret = ceph_decode_pgid(&p, end, &m->spgid.pgid);
4290a02a946dSIlya Dryomov 	if (ret)
4291a02a946dSIlya Dryomov 		return ret;
4292a02a946dSIlya Dryomov 
4293a02a946dSIlya Dryomov 	ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval);
4294a02a946dSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->map_epoch, e_inval);
4295a02a946dSIlya Dryomov 	ceph_decode_8_safe(&p, end, m->op, e_inval);
4296a02a946dSIlya Dryomov 	ceph_decode_64_safe(&p, end, m->id, e_inval);
4297a02a946dSIlya Dryomov 
4298a02a946dSIlya Dryomov 	m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO);
4299a02a946dSIlya Dryomov 	if (!m->begin)
4300a02a946dSIlya Dryomov 		return -ENOMEM;
4301a02a946dSIlya Dryomov 
4302a02a946dSIlya Dryomov 	ret = decode_hoid(&p, end, m->begin);
4303a02a946dSIlya Dryomov 	if (ret) {
4304a02a946dSIlya Dryomov 		free_hoid(m->begin);
4305a02a946dSIlya Dryomov 		return ret;
4306a02a946dSIlya Dryomov 	}
4307a02a946dSIlya Dryomov 
4308a02a946dSIlya Dryomov 	m->end = kzalloc(sizeof(*m->end), GFP_NOIO);
4309a02a946dSIlya Dryomov 	if (!m->end) {
4310a02a946dSIlya Dryomov 		free_hoid(m->begin);
4311a02a946dSIlya Dryomov 		return -ENOMEM;
4312a02a946dSIlya Dryomov 	}
4313a02a946dSIlya Dryomov 
4314a02a946dSIlya Dryomov 	ret = decode_hoid(&p, end, m->end);
4315a02a946dSIlya Dryomov 	if (ret) {
4316a02a946dSIlya Dryomov 		free_hoid(m->begin);
4317a02a946dSIlya Dryomov 		free_hoid(m->end);
4318a02a946dSIlya Dryomov 		return ret;
4319a02a946dSIlya Dryomov 	}
4320a02a946dSIlya Dryomov 
4321a02a946dSIlya Dryomov 	return 0;
4322a02a946dSIlya Dryomov 
4323a02a946dSIlya Dryomov e_inval:
4324a02a946dSIlya Dryomov 	return -EINVAL;
4325a02a946dSIlya Dryomov }
4326a02a946dSIlya Dryomov 
4327a02a946dSIlya Dryomov static struct ceph_msg *create_backoff_message(
4328a02a946dSIlya Dryomov 				const struct ceph_osd_backoff *backoff,
4329a02a946dSIlya Dryomov 				u32 map_epoch)
4330a02a946dSIlya Dryomov {
4331a02a946dSIlya Dryomov 	struct ceph_msg *msg;
4332a02a946dSIlya Dryomov 	void *p, *end;
4333a02a946dSIlya Dryomov 	int msg_size;
4334a02a946dSIlya Dryomov 
4335a02a946dSIlya Dryomov 	msg_size = CEPH_ENCODING_START_BLK_LEN +
4336a02a946dSIlya Dryomov 			CEPH_PGID_ENCODING_LEN + 1; /* spgid */
4337a02a946dSIlya Dryomov 	msg_size += 4 + 1 + 8; /* map_epoch, op, id */
4338a02a946dSIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
4339a02a946dSIlya Dryomov 			hoid_encoding_size(backoff->begin);
4340a02a946dSIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
4341a02a946dSIlya Dryomov 			hoid_encoding_size(backoff->end);
4342a02a946dSIlya Dryomov 
4343a02a946dSIlya Dryomov 	msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true);
4344a02a946dSIlya Dryomov 	if (!msg)
4345a02a946dSIlya Dryomov 		return NULL;
4346a02a946dSIlya Dryomov 
4347a02a946dSIlya Dryomov 	p = msg->front.iov_base;
4348a02a946dSIlya Dryomov 	end = p + msg->front_alloc_len;
4349a02a946dSIlya Dryomov 
4350a02a946dSIlya Dryomov 	encode_spgid(&p, &backoff->spgid);
4351a02a946dSIlya Dryomov 	ceph_encode_32(&p, map_epoch);
4352a02a946dSIlya Dryomov 	ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK);
4353a02a946dSIlya Dryomov 	ceph_encode_64(&p, backoff->id);
4354a02a946dSIlya Dryomov 	encode_hoid(&p, end, backoff->begin);
4355a02a946dSIlya Dryomov 	encode_hoid(&p, end, backoff->end);
4356a02a946dSIlya Dryomov 	BUG_ON(p != end);
4357a02a946dSIlya Dryomov 
4358a02a946dSIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
4359a02a946dSIlya Dryomov 	msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */
4360a02a946dSIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
4361a02a946dSIlya Dryomov 
4362a02a946dSIlya Dryomov 	return msg;
4363a02a946dSIlya Dryomov }
4364a02a946dSIlya Dryomov 
4365a02a946dSIlya Dryomov static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m)
4366a02a946dSIlya Dryomov {
4367a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
4368a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
4369a02a946dSIlya Dryomov 	struct ceph_msg *msg;
4370a02a946dSIlya Dryomov 
4371a02a946dSIlya Dryomov 	dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4372a02a946dSIlya Dryomov 	     m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4373a02a946dSIlya Dryomov 
4374a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid);
4375a02a946dSIlya Dryomov 	if (!spg) {
4376a02a946dSIlya Dryomov 		spg = alloc_spg_mapping();
4377a02a946dSIlya Dryomov 		if (!spg) {
4378a02a946dSIlya Dryomov 			pr_err("%s failed to allocate spg\n", __func__);
4379a02a946dSIlya Dryomov 			return;
4380a02a946dSIlya Dryomov 		}
4381a02a946dSIlya Dryomov 		spg->spgid = m->spgid; /* struct */
4382a02a946dSIlya Dryomov 		insert_spg_mapping(&osd->o_backoff_mappings, spg);
4383a02a946dSIlya Dryomov 	}
4384a02a946dSIlya Dryomov 
4385a02a946dSIlya Dryomov 	backoff = alloc_backoff();
4386a02a946dSIlya Dryomov 	if (!backoff) {
4387a02a946dSIlya Dryomov 		pr_err("%s failed to allocate backoff\n", __func__);
4388a02a946dSIlya Dryomov 		return;
4389a02a946dSIlya Dryomov 	}
4390a02a946dSIlya Dryomov 	backoff->spgid = m->spgid; /* struct */
4391a02a946dSIlya Dryomov 	backoff->id = m->id;
4392a02a946dSIlya Dryomov 	backoff->begin = m->begin;
4393a02a946dSIlya Dryomov 	m->begin = NULL; /* backoff now owns this */
4394a02a946dSIlya Dryomov 	backoff->end = m->end;
4395a02a946dSIlya Dryomov 	m->end = NULL;   /* ditto */
4396a02a946dSIlya Dryomov 
4397a02a946dSIlya Dryomov 	insert_backoff(&spg->backoffs, backoff);
4398a02a946dSIlya Dryomov 	insert_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4399a02a946dSIlya Dryomov 
4400a02a946dSIlya Dryomov 	/*
4401a02a946dSIlya Dryomov 	 * Ack with original backoff's epoch so that the OSD can
4402a02a946dSIlya Dryomov 	 * discard this if there was a PG split.
4403a02a946dSIlya Dryomov 	 */
4404a02a946dSIlya Dryomov 	msg = create_backoff_message(backoff, m->map_epoch);
4405a02a946dSIlya Dryomov 	if (!msg) {
4406a02a946dSIlya Dryomov 		pr_err("%s failed to allocate msg\n", __func__);
4407a02a946dSIlya Dryomov 		return;
4408a02a946dSIlya Dryomov 	}
4409a02a946dSIlya Dryomov 	ceph_con_send(&osd->o_con, msg);
4410a02a946dSIlya Dryomov }
4411a02a946dSIlya Dryomov 
4412a02a946dSIlya Dryomov static bool target_contained_by(const struct ceph_osd_request_target *t,
4413a02a946dSIlya Dryomov 				const struct ceph_hobject_id *begin,
4414a02a946dSIlya Dryomov 				const struct ceph_hobject_id *end)
4415a02a946dSIlya Dryomov {
4416a02a946dSIlya Dryomov 	struct ceph_hobject_id hoid;
4417a02a946dSIlya Dryomov 	int cmp;
4418a02a946dSIlya Dryomov 
4419a02a946dSIlya Dryomov 	hoid_fill_from_target(&hoid, t);
4420a02a946dSIlya Dryomov 	cmp = hoid_compare(&hoid, begin);
4421a02a946dSIlya Dryomov 	return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0);
4422a02a946dSIlya Dryomov }
4423a02a946dSIlya Dryomov 
4424a02a946dSIlya Dryomov static void handle_backoff_unblock(struct ceph_osd *osd,
4425a02a946dSIlya Dryomov 				   const struct MOSDBackoff *m)
4426a02a946dSIlya Dryomov {
4427a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
4428a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
4429a02a946dSIlya Dryomov 	struct rb_node *n;
4430a02a946dSIlya Dryomov 
4431a02a946dSIlya Dryomov 	dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4432a02a946dSIlya Dryomov 	     m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4433a02a946dSIlya Dryomov 
4434a02a946dSIlya Dryomov 	backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id);
4435a02a946dSIlya Dryomov 	if (!backoff) {
4436a02a946dSIlya Dryomov 		pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n",
4437a02a946dSIlya Dryomov 		       __func__, osd->o_osd, m->spgid.pgid.pool,
4438a02a946dSIlya Dryomov 		       m->spgid.pgid.seed, m->spgid.shard, m->id);
4439a02a946dSIlya Dryomov 		return;
4440a02a946dSIlya Dryomov 	}
4441a02a946dSIlya Dryomov 
4442a02a946dSIlya Dryomov 	if (hoid_compare(backoff->begin, m->begin) &&
4443a02a946dSIlya Dryomov 	    hoid_compare(backoff->end, m->end)) {
4444a02a946dSIlya Dryomov 		pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n",
4445a02a946dSIlya Dryomov 		       __func__, osd->o_osd, m->spgid.pgid.pool,
4446a02a946dSIlya Dryomov 		       m->spgid.pgid.seed, m->spgid.shard, m->id);
4447a02a946dSIlya Dryomov 		/* unblock it anyway... */
4448a02a946dSIlya Dryomov 	}
4449a02a946dSIlya Dryomov 
4450a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid);
4451a02a946dSIlya Dryomov 	BUG_ON(!spg);
4452a02a946dSIlya Dryomov 
4453a02a946dSIlya Dryomov 	erase_backoff(&spg->backoffs, backoff);
4454a02a946dSIlya Dryomov 	erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4455a02a946dSIlya Dryomov 	free_backoff(backoff);
4456a02a946dSIlya Dryomov 
4457a02a946dSIlya Dryomov 	if (RB_EMPTY_ROOT(&spg->backoffs)) {
4458a02a946dSIlya Dryomov 		erase_spg_mapping(&osd->o_backoff_mappings, spg);
4459a02a946dSIlya Dryomov 		free_spg_mapping(spg);
4460a02a946dSIlya Dryomov 	}
4461a02a946dSIlya Dryomov 
4462a02a946dSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
4463a02a946dSIlya Dryomov 		struct ceph_osd_request *req =
4464a02a946dSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
4465a02a946dSIlya Dryomov 
4466a02a946dSIlya Dryomov 		if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) {
4467a02a946dSIlya Dryomov 			/*
4468a02a946dSIlya Dryomov 			 * Match against @m, not @backoff -- the PG may
4469a02a946dSIlya Dryomov 			 * have split on the OSD.
4470a02a946dSIlya Dryomov 			 */
4471a02a946dSIlya Dryomov 			if (target_contained_by(&req->r_t, m->begin, m->end)) {
4472a02a946dSIlya Dryomov 				/*
4473a02a946dSIlya Dryomov 				 * If no other installed backoff applies,
4474a02a946dSIlya Dryomov 				 * resend.
4475a02a946dSIlya Dryomov 				 */
4476a02a946dSIlya Dryomov 				send_request(req);
4477a02a946dSIlya Dryomov 			}
4478a02a946dSIlya Dryomov 		}
4479a02a946dSIlya Dryomov 	}
4480a02a946dSIlya Dryomov }
4481a02a946dSIlya Dryomov 
4482a02a946dSIlya Dryomov static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg)
4483a02a946dSIlya Dryomov {
4484a02a946dSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
4485a02a946dSIlya Dryomov 	struct MOSDBackoff m;
4486a02a946dSIlya Dryomov 	int ret;
4487a02a946dSIlya Dryomov 
4488a02a946dSIlya Dryomov 	down_read(&osdc->lock);
4489a02a946dSIlya Dryomov 	if (!osd_registered(osd)) {
4490a02a946dSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
4491a02a946dSIlya Dryomov 		up_read(&osdc->lock);
4492a02a946dSIlya Dryomov 		return;
4493a02a946dSIlya Dryomov 	}
4494a02a946dSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
4495a02a946dSIlya Dryomov 
4496a02a946dSIlya Dryomov 	mutex_lock(&osd->lock);
4497a02a946dSIlya Dryomov 	ret = decode_MOSDBackoff(msg, &m);
4498a02a946dSIlya Dryomov 	if (ret) {
4499a02a946dSIlya Dryomov 		pr_err("failed to decode MOSDBackoff: %d\n", ret);
4500a02a946dSIlya Dryomov 		ceph_msg_dump(msg);
4501a02a946dSIlya Dryomov 		goto out_unlock;
4502a02a946dSIlya Dryomov 	}
4503a02a946dSIlya Dryomov 
4504a02a946dSIlya Dryomov 	switch (m.op) {
4505a02a946dSIlya Dryomov 	case CEPH_OSD_BACKOFF_OP_BLOCK:
4506a02a946dSIlya Dryomov 		handle_backoff_block(osd, &m);
4507a02a946dSIlya Dryomov 		break;
4508a02a946dSIlya Dryomov 	case CEPH_OSD_BACKOFF_OP_UNBLOCK:
4509a02a946dSIlya Dryomov 		handle_backoff_unblock(osd, &m);
4510a02a946dSIlya Dryomov 		break;
4511a02a946dSIlya Dryomov 	default:
4512a02a946dSIlya Dryomov 		pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op);
4513a02a946dSIlya Dryomov 	}
4514a02a946dSIlya Dryomov 
4515a02a946dSIlya Dryomov 	free_hoid(m.begin);
4516a02a946dSIlya Dryomov 	free_hoid(m.end);
4517a02a946dSIlya Dryomov 
4518a02a946dSIlya Dryomov out_unlock:
4519a02a946dSIlya Dryomov 	mutex_unlock(&osd->lock);
4520a02a946dSIlya Dryomov 	up_read(&osdc->lock);
4521a02a946dSIlya Dryomov }
4522a02a946dSIlya Dryomov 
45233d14c5d2SYehuda Sadeh /*
4524a40c4f10SYehuda Sadeh  * Process osd watch notifications
4525a40c4f10SYehuda Sadeh  */
45263c663bbdSAlex Elder static void handle_watch_notify(struct ceph_osd_client *osdc,
45273c663bbdSAlex Elder 				struct ceph_msg *msg)
4528a40c4f10SYehuda Sadeh {
4529922dab61SIlya Dryomov 	void *p = msg->front.iov_base;
4530922dab61SIlya Dryomov 	void *const end = p + msg->front.iov_len;
4531922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
4532922dab61SIlya Dryomov 	struct linger_work *lwork;
4533922dab61SIlya Dryomov 	u8 proto_ver, opcode;
4534922dab61SIlya Dryomov 	u64 cookie, notify_id;
4535922dab61SIlya Dryomov 	u64 notifier_id = 0;
453619079203SIlya Dryomov 	s32 return_code = 0;
4537922dab61SIlya Dryomov 	void *payload = NULL;
4538922dab61SIlya Dryomov 	u32 payload_len = 0;
4539a40c4f10SYehuda Sadeh 
4540a40c4f10SYehuda Sadeh 	ceph_decode_8_safe(&p, end, proto_ver, bad);
4541a40c4f10SYehuda Sadeh 	ceph_decode_8_safe(&p, end, opcode, bad);
4542a40c4f10SYehuda Sadeh 	ceph_decode_64_safe(&p, end, cookie, bad);
4543922dab61SIlya Dryomov 	p += 8; /* skip ver */
4544a40c4f10SYehuda Sadeh 	ceph_decode_64_safe(&p, end, notify_id, bad);
4545a40c4f10SYehuda Sadeh 
4546922dab61SIlya Dryomov 	if (proto_ver >= 1) {
4547922dab61SIlya Dryomov 		ceph_decode_32_safe(&p, end, payload_len, bad);
4548922dab61SIlya Dryomov 		ceph_decode_need(&p, end, payload_len, bad);
4549922dab61SIlya Dryomov 		payload = p;
4550922dab61SIlya Dryomov 		p += payload_len;
4551a40c4f10SYehuda Sadeh 	}
4552a40c4f10SYehuda Sadeh 
4553922dab61SIlya Dryomov 	if (le16_to_cpu(msg->hdr.version) >= 2)
455419079203SIlya Dryomov 		ceph_decode_32_safe(&p, end, return_code, bad);
4555922dab61SIlya Dryomov 
4556922dab61SIlya Dryomov 	if (le16_to_cpu(msg->hdr.version) >= 3)
4557922dab61SIlya Dryomov 		ceph_decode_64_safe(&p, end, notifier_id, bad);
4558922dab61SIlya Dryomov 
4559922dab61SIlya Dryomov 	down_read(&osdc->lock);
4560922dab61SIlya Dryomov 	lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
4561922dab61SIlya Dryomov 	if (!lreq) {
4562922dab61SIlya Dryomov 		dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
4563922dab61SIlya Dryomov 		     cookie);
4564922dab61SIlya Dryomov 		goto out_unlock_osdc;
4565922dab61SIlya Dryomov 	}
4566922dab61SIlya Dryomov 
4567922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
456819079203SIlya Dryomov 	dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
456919079203SIlya Dryomov 	     opcode, cookie, lreq, lreq->is_watch);
4570922dab61SIlya Dryomov 	if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
4571922dab61SIlya Dryomov 		if (!lreq->last_error) {
4572922dab61SIlya Dryomov 			lreq->last_error = -ENOTCONN;
4573922dab61SIlya Dryomov 			queue_watch_error(lreq);
4574922dab61SIlya Dryomov 		}
457519079203SIlya Dryomov 	} else if (!lreq->is_watch) {
457619079203SIlya Dryomov 		/* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
457719079203SIlya Dryomov 		if (lreq->notify_id && lreq->notify_id != notify_id) {
457819079203SIlya Dryomov 			dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
457919079203SIlya Dryomov 			     lreq->notify_id, notify_id);
458019079203SIlya Dryomov 		} else if (!completion_done(&lreq->notify_finish_wait)) {
458119079203SIlya Dryomov 			struct ceph_msg_data *data =
45820d9c1ab3SIlya Dryomov 			    msg->num_data_items ? &msg->data[0] : NULL;
458319079203SIlya Dryomov 
458419079203SIlya Dryomov 			if (data) {
458519079203SIlya Dryomov 				if (lreq->preply_pages) {
458619079203SIlya Dryomov 					WARN_ON(data->type !=
458719079203SIlya Dryomov 							CEPH_MSG_DATA_PAGES);
458819079203SIlya Dryomov 					*lreq->preply_pages = data->pages;
458919079203SIlya Dryomov 					*lreq->preply_len = data->length;
4590e8862740SIlya Dryomov 					data->own_pages = false;
459119079203SIlya Dryomov 				}
459219079203SIlya Dryomov 			}
459319079203SIlya Dryomov 			lreq->notify_finish_error = return_code;
459419079203SIlya Dryomov 			complete_all(&lreq->notify_finish_wait);
459519079203SIlya Dryomov 		}
4596922dab61SIlya Dryomov 	} else {
4597922dab61SIlya Dryomov 		/* CEPH_WATCH_EVENT_NOTIFY */
4598922dab61SIlya Dryomov 		lwork = lwork_alloc(lreq, do_watch_notify);
4599922dab61SIlya Dryomov 		if (!lwork) {
4600922dab61SIlya Dryomov 			pr_err("failed to allocate notify-lwork\n");
4601922dab61SIlya Dryomov 			goto out_unlock_lreq;
4602922dab61SIlya Dryomov 		}
4603922dab61SIlya Dryomov 
4604922dab61SIlya Dryomov 		lwork->notify.notify_id = notify_id;
4605922dab61SIlya Dryomov 		lwork->notify.notifier_id = notifier_id;
4606922dab61SIlya Dryomov 		lwork->notify.payload = payload;
4607922dab61SIlya Dryomov 		lwork->notify.payload_len = payload_len;
4608922dab61SIlya Dryomov 		lwork->notify.msg = ceph_msg_get(msg);
4609922dab61SIlya Dryomov 		lwork_queue(lwork);
4610922dab61SIlya Dryomov 	}
4611922dab61SIlya Dryomov 
4612922dab61SIlya Dryomov out_unlock_lreq:
4613922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
4614922dab61SIlya Dryomov out_unlock_osdc:
4615922dab61SIlya Dryomov 	up_read(&osdc->lock);
4616a40c4f10SYehuda Sadeh 	return;
4617a40c4f10SYehuda Sadeh 
4618a40c4f10SYehuda Sadeh bad:
4619a40c4f10SYehuda Sadeh 	pr_err("osdc handle_watch_notify corrupt msg\n");
4620a40c4f10SYehuda Sadeh }
4621a40c4f10SYehuda Sadeh 
4622a40c4f10SYehuda Sadeh /*
46233d14c5d2SYehuda Sadeh  * Register request, send initial attempt.
46243d14c5d2SYehuda Sadeh  */
4625a8af0d68SJeff Layton void ceph_osdc_start_request(struct ceph_osd_client *osdc,
4626a8af0d68SJeff Layton 			     struct ceph_osd_request *req)
46273d14c5d2SYehuda Sadeh {
46285aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
46295aea3dcdSIlya Dryomov 	submit_request(req, false);
46305aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
46313d14c5d2SYehuda Sadeh }
46323d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_start_request);
46333d14c5d2SYehuda Sadeh 
46343d14c5d2SYehuda Sadeh /*
4635d0bb883cSIlya Dryomov  * Unregister request.  If @req was registered, it isn't completed:
4636d0bb883cSIlya Dryomov  * r_result isn't set and __complete_request() isn't invoked.
4637d0bb883cSIlya Dryomov  *
4638d0bb883cSIlya Dryomov  * If @req wasn't registered, this call may have raced with
4639d0bb883cSIlya Dryomov  * handle_reply(), in which case r_result would already be set and
4640d0bb883cSIlya Dryomov  * __complete_request() would be getting invoked, possibly even
4641d0bb883cSIlya Dryomov  * concurrently with this call.
4642c9f9b93dSIlya Dryomov  */
4643c9f9b93dSIlya Dryomov void ceph_osdc_cancel_request(struct ceph_osd_request *req)
4644c9f9b93dSIlya Dryomov {
4645c9f9b93dSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
4646c9f9b93dSIlya Dryomov 
46475aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
46485aea3dcdSIlya Dryomov 	if (req->r_osd)
46495aea3dcdSIlya Dryomov 		cancel_request(req);
46505aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
4651c9f9b93dSIlya Dryomov }
4652c9f9b93dSIlya Dryomov EXPORT_SYMBOL(ceph_osdc_cancel_request);
4653c9f9b93dSIlya Dryomov 
4654c9f9b93dSIlya Dryomov /*
465542b06965SIlya Dryomov  * @timeout: in jiffies, 0 means "wait forever"
465642b06965SIlya Dryomov  */
465742b06965SIlya Dryomov static int wait_request_timeout(struct ceph_osd_request *req,
465842b06965SIlya Dryomov 				unsigned long timeout)
465942b06965SIlya Dryomov {
466042b06965SIlya Dryomov 	long left;
466142b06965SIlya Dryomov 
466242b06965SIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
46630e76abf2SYan, Zheng 	left = wait_for_completion_killable_timeout(&req->r_completion,
466442b06965SIlya Dryomov 						ceph_timeout_jiffies(timeout));
466542b06965SIlya Dryomov 	if (left <= 0) {
466642b06965SIlya Dryomov 		left = left ?: -ETIMEDOUT;
466742b06965SIlya Dryomov 		ceph_osdc_cancel_request(req);
466842b06965SIlya Dryomov 	} else {
466942b06965SIlya Dryomov 		left = req->r_result; /* completed */
467042b06965SIlya Dryomov 	}
467142b06965SIlya Dryomov 
467242b06965SIlya Dryomov 	return left;
467342b06965SIlya Dryomov }
467442b06965SIlya Dryomov 
467542b06965SIlya Dryomov /*
46763d14c5d2SYehuda Sadeh  * wait for a request to complete
46773d14c5d2SYehuda Sadeh  */
46783d14c5d2SYehuda Sadeh int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
46793d14c5d2SYehuda Sadeh 			   struct ceph_osd_request *req)
46803d14c5d2SYehuda Sadeh {
468142b06965SIlya Dryomov 	return wait_request_timeout(req, 0);
46823d14c5d2SYehuda Sadeh }
46833d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_wait_request);
46843d14c5d2SYehuda Sadeh 
46853d14c5d2SYehuda Sadeh /*
46863d14c5d2SYehuda Sadeh  * sync - wait for all in-flight requests to flush.  avoid starvation.
46873d14c5d2SYehuda Sadeh  */
46883d14c5d2SYehuda Sadeh void ceph_osdc_sync(struct ceph_osd_client *osdc)
46893d14c5d2SYehuda Sadeh {
46905aea3dcdSIlya Dryomov 	struct rb_node *n, *p;
46915aea3dcdSIlya Dryomov 	u64 last_tid = atomic64_read(&osdc->last_tid);
46923d14c5d2SYehuda Sadeh 
46935aea3dcdSIlya Dryomov again:
46945aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
46955aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
46965aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
46975aea3dcdSIlya Dryomov 
46985aea3dcdSIlya Dryomov 		mutex_lock(&osd->lock);
46995aea3dcdSIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
47005aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
47015aea3dcdSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
47025aea3dcdSIlya Dryomov 
47033d14c5d2SYehuda Sadeh 			if (req->r_tid > last_tid)
47043d14c5d2SYehuda Sadeh 				break;
47053d14c5d2SYehuda Sadeh 
47065aea3dcdSIlya Dryomov 			if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
47073d14c5d2SYehuda Sadeh 				continue;
47083d14c5d2SYehuda Sadeh 
47093d14c5d2SYehuda Sadeh 			ceph_osdc_get_request(req);
47105aea3dcdSIlya Dryomov 			mutex_unlock(&osd->lock);
47115aea3dcdSIlya Dryomov 			up_read(&osdc->lock);
47125aea3dcdSIlya Dryomov 			dout("%s waiting on req %p tid %llu last_tid %llu\n",
47135aea3dcdSIlya Dryomov 			     __func__, req, req->r_tid, last_tid);
4714b18b9550SIlya Dryomov 			wait_for_completion(&req->r_completion);
47153d14c5d2SYehuda Sadeh 			ceph_osdc_put_request(req);
47165aea3dcdSIlya Dryomov 			goto again;
47173d14c5d2SYehuda Sadeh 		}
47185aea3dcdSIlya Dryomov 
47195aea3dcdSIlya Dryomov 		mutex_unlock(&osd->lock);
47205aea3dcdSIlya Dryomov 	}
47215aea3dcdSIlya Dryomov 
47225aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
47235aea3dcdSIlya Dryomov 	dout("%s done last_tid %llu\n", __func__, last_tid);
47243d14c5d2SYehuda Sadeh }
47253d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_sync);
47263d14c5d2SYehuda Sadeh 
4727922dab61SIlya Dryomov /*
4728922dab61SIlya Dryomov  * Returns a handle, caller owns a ref.
4729922dab61SIlya Dryomov  */
4730922dab61SIlya Dryomov struct ceph_osd_linger_request *
4731922dab61SIlya Dryomov ceph_osdc_watch(struct ceph_osd_client *osdc,
4732922dab61SIlya Dryomov 		struct ceph_object_id *oid,
4733922dab61SIlya Dryomov 		struct ceph_object_locator *oloc,
4734922dab61SIlya Dryomov 		rados_watchcb2_t wcb,
4735922dab61SIlya Dryomov 		rados_watcherrcb_t errcb,
4736922dab61SIlya Dryomov 		void *data)
4737922dab61SIlya Dryomov {
4738922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
4739922dab61SIlya Dryomov 	int ret;
4740922dab61SIlya Dryomov 
4741922dab61SIlya Dryomov 	lreq = linger_alloc(osdc);
4742922dab61SIlya Dryomov 	if (!lreq)
4743922dab61SIlya Dryomov 		return ERR_PTR(-ENOMEM);
4744922dab61SIlya Dryomov 
474519079203SIlya Dryomov 	lreq->is_watch = true;
4746922dab61SIlya Dryomov 	lreq->wcb = wcb;
4747922dab61SIlya Dryomov 	lreq->errcb = errcb;
4748922dab61SIlya Dryomov 	lreq->data = data;
4749b07d3c4bSIlya Dryomov 	lreq->watch_valid_thru = jiffies;
4750922dab61SIlya Dryomov 
4751922dab61SIlya Dryomov 	ceph_oid_copy(&lreq->t.base_oid, oid);
4752922dab61SIlya Dryomov 	ceph_oloc_copy(&lreq->t.base_oloc, oloc);
475354ea0046SIlya Dryomov 	lreq->t.flags = CEPH_OSD_FLAG_WRITE;
4754fac02ddfSArnd Bergmann 	ktime_get_real_ts64(&lreq->mtime);
4755922dab61SIlya Dryomov 
475681c65213SIlya Dryomov 	linger_submit(lreq);
4757922dab61SIlya Dryomov 	ret = linger_reg_commit_wait(lreq);
4758922dab61SIlya Dryomov 	if (ret) {
4759922dab61SIlya Dryomov 		linger_cancel(lreq);
4760922dab61SIlya Dryomov 		goto err_put_lreq;
4761922dab61SIlya Dryomov 	}
4762922dab61SIlya Dryomov 
4763922dab61SIlya Dryomov 	return lreq;
4764922dab61SIlya Dryomov 
4765922dab61SIlya Dryomov err_put_lreq:
4766922dab61SIlya Dryomov 	linger_put(lreq);
4767922dab61SIlya Dryomov 	return ERR_PTR(ret);
4768922dab61SIlya Dryomov }
4769922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_watch);
4770922dab61SIlya Dryomov 
4771922dab61SIlya Dryomov /*
4772922dab61SIlya Dryomov  * Releases a ref.
4773922dab61SIlya Dryomov  *
4774922dab61SIlya Dryomov  * Times out after mount_timeout to preserve rbd unmap behaviour
4775922dab61SIlya Dryomov  * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
4776922dab61SIlya Dryomov  * with mount_timeout").
4777922dab61SIlya Dryomov  */
4778922dab61SIlya Dryomov int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
4779922dab61SIlya Dryomov 		      struct ceph_osd_linger_request *lreq)
4780922dab61SIlya Dryomov {
4781922dab61SIlya Dryomov 	struct ceph_options *opts = osdc->client->options;
4782922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4783922dab61SIlya Dryomov 	int ret;
4784922dab61SIlya Dryomov 
4785922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4786922dab61SIlya Dryomov 	if (!req)
4787922dab61SIlya Dryomov 		return -ENOMEM;
4788922dab61SIlya Dryomov 
4789922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4790922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
479154ea0046SIlya Dryomov 	req->r_flags = CEPH_OSD_FLAG_WRITE;
4792fac02ddfSArnd Bergmann 	ktime_get_real_ts64(&req->r_mtime);
479375dbb685SIlya Dryomov 	osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_UNWATCH,
479475dbb685SIlya Dryomov 			      lreq->linger_id, 0);
4795922dab61SIlya Dryomov 
4796922dab61SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4797922dab61SIlya Dryomov 	if (ret)
4798922dab61SIlya Dryomov 		goto out_put_req;
4799922dab61SIlya Dryomov 
4800a8af0d68SJeff Layton 	ceph_osdc_start_request(osdc, req);
4801922dab61SIlya Dryomov 	linger_cancel(lreq);
4802922dab61SIlya Dryomov 	linger_put(lreq);
4803922dab61SIlya Dryomov 	ret = wait_request_timeout(req, opts->mount_timeout);
4804922dab61SIlya Dryomov 
4805922dab61SIlya Dryomov out_put_req:
4806922dab61SIlya Dryomov 	ceph_osdc_put_request(req);
4807922dab61SIlya Dryomov 	return ret;
4808922dab61SIlya Dryomov }
4809922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_unwatch);
4810922dab61SIlya Dryomov 
4811922dab61SIlya Dryomov static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
4812922dab61SIlya Dryomov 				      u64 notify_id, u64 cookie, void *payload,
48136d54228fSIlya Dryomov 				      u32 payload_len)
4814922dab61SIlya Dryomov {
4815922dab61SIlya Dryomov 	struct ceph_osd_req_op *op;
4816922dab61SIlya Dryomov 	struct ceph_pagelist *pl;
4817922dab61SIlya Dryomov 	int ret;
4818922dab61SIlya Dryomov 
4819042f6498SJeff Layton 	op = osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
4820922dab61SIlya Dryomov 
482133165d47SIlya Dryomov 	pl = ceph_pagelist_alloc(GFP_NOIO);
4822922dab61SIlya Dryomov 	if (!pl)
4823922dab61SIlya Dryomov 		return -ENOMEM;
4824922dab61SIlya Dryomov 
4825922dab61SIlya Dryomov 	ret = ceph_pagelist_encode_64(pl, notify_id);
4826922dab61SIlya Dryomov 	ret |= ceph_pagelist_encode_64(pl, cookie);
4827922dab61SIlya Dryomov 	if (payload) {
4828922dab61SIlya Dryomov 		ret |= ceph_pagelist_encode_32(pl, payload_len);
4829922dab61SIlya Dryomov 		ret |= ceph_pagelist_append(pl, payload, payload_len);
4830922dab61SIlya Dryomov 	} else {
4831922dab61SIlya Dryomov 		ret |= ceph_pagelist_encode_32(pl, 0);
4832922dab61SIlya Dryomov 	}
4833922dab61SIlya Dryomov 	if (ret) {
4834922dab61SIlya Dryomov 		ceph_pagelist_release(pl);
4835922dab61SIlya Dryomov 		return -ENOMEM;
4836922dab61SIlya Dryomov 	}
4837922dab61SIlya Dryomov 
4838922dab61SIlya Dryomov 	ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
4839922dab61SIlya Dryomov 	op->indata_len = pl->length;
4840922dab61SIlya Dryomov 	return 0;
4841922dab61SIlya Dryomov }
4842922dab61SIlya Dryomov 
4843922dab61SIlya Dryomov int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
4844922dab61SIlya Dryomov 			 struct ceph_object_id *oid,
4845922dab61SIlya Dryomov 			 struct ceph_object_locator *oloc,
4846922dab61SIlya Dryomov 			 u64 notify_id,
4847922dab61SIlya Dryomov 			 u64 cookie,
4848922dab61SIlya Dryomov 			 void *payload,
48496d54228fSIlya Dryomov 			 u32 payload_len)
4850922dab61SIlya Dryomov {
4851922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4852922dab61SIlya Dryomov 	int ret;
4853922dab61SIlya Dryomov 
4854922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4855922dab61SIlya Dryomov 	if (!req)
4856922dab61SIlya Dryomov 		return -ENOMEM;
4857922dab61SIlya Dryomov 
4858922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, oid);
4859922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, oloc);
4860922dab61SIlya Dryomov 	req->r_flags = CEPH_OSD_FLAG_READ;
4861922dab61SIlya Dryomov 
486226f887e0SIlya Dryomov 	ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
486326f887e0SIlya Dryomov 					 payload_len);
4864922dab61SIlya Dryomov 	if (ret)
4865922dab61SIlya Dryomov 		goto out_put_req;
4866922dab61SIlya Dryomov 
486726f887e0SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4868922dab61SIlya Dryomov 	if (ret)
4869922dab61SIlya Dryomov 		goto out_put_req;
4870922dab61SIlya Dryomov 
4871a8af0d68SJeff Layton 	ceph_osdc_start_request(osdc, req);
4872922dab61SIlya Dryomov 	ret = ceph_osdc_wait_request(osdc, req);
4873922dab61SIlya Dryomov 
4874922dab61SIlya Dryomov out_put_req:
4875922dab61SIlya Dryomov 	ceph_osdc_put_request(req);
4876922dab61SIlya Dryomov 	return ret;
4877922dab61SIlya Dryomov }
4878922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_notify_ack);
4879922dab61SIlya Dryomov 
488019079203SIlya Dryomov /*
488119079203SIlya Dryomov  * @timeout: in seconds
488219079203SIlya Dryomov  *
488319079203SIlya Dryomov  * @preply_{pages,len} are initialized both on success and error.
488419079203SIlya Dryomov  * The caller is responsible for:
488519079203SIlya Dryomov  *
488619079203SIlya Dryomov  *     ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
488719079203SIlya Dryomov  */
488819079203SIlya Dryomov int ceph_osdc_notify(struct ceph_osd_client *osdc,
488919079203SIlya Dryomov 		     struct ceph_object_id *oid,
489019079203SIlya Dryomov 		     struct ceph_object_locator *oloc,
489119079203SIlya Dryomov 		     void *payload,
48926d54228fSIlya Dryomov 		     u32 payload_len,
489319079203SIlya Dryomov 		     u32 timeout,
489419079203SIlya Dryomov 		     struct page ***preply_pages,
489519079203SIlya Dryomov 		     size_t *preply_len)
489619079203SIlya Dryomov {
489719079203SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
489819079203SIlya Dryomov 	int ret;
489919079203SIlya Dryomov 
490019079203SIlya Dryomov 	WARN_ON(!timeout);
490119079203SIlya Dryomov 	if (preply_pages) {
490219079203SIlya Dryomov 		*preply_pages = NULL;
490319079203SIlya Dryomov 		*preply_len = 0;
490419079203SIlya Dryomov 	}
490519079203SIlya Dryomov 
490619079203SIlya Dryomov 	lreq = linger_alloc(osdc);
490719079203SIlya Dryomov 	if (!lreq)
490819079203SIlya Dryomov 		return -ENOMEM;
490919079203SIlya Dryomov 
491075dbb685SIlya Dryomov 	lreq->request_pl = ceph_pagelist_alloc(GFP_NOIO);
491175dbb685SIlya Dryomov 	if (!lreq->request_pl) {
491275dbb685SIlya Dryomov 		ret = -ENOMEM;
491375dbb685SIlya Dryomov 		goto out_put_lreq;
491475dbb685SIlya Dryomov 	}
491575dbb685SIlya Dryomov 
491675dbb685SIlya Dryomov 	ret = ceph_pagelist_encode_32(lreq->request_pl, 1); /* prot_ver */
491775dbb685SIlya Dryomov 	ret |= ceph_pagelist_encode_32(lreq->request_pl, timeout);
491875dbb685SIlya Dryomov 	ret |= ceph_pagelist_encode_32(lreq->request_pl, payload_len);
491975dbb685SIlya Dryomov 	ret |= ceph_pagelist_append(lreq->request_pl, payload, payload_len);
492075dbb685SIlya Dryomov 	if (ret) {
492175dbb685SIlya Dryomov 		ret = -ENOMEM;
492275dbb685SIlya Dryomov 		goto out_put_lreq;
492375dbb685SIlya Dryomov 	}
492475dbb685SIlya Dryomov 
492575dbb685SIlya Dryomov 	/* for notify_id */
492675dbb685SIlya Dryomov 	lreq->notify_id_pages = ceph_alloc_page_vector(1, GFP_NOIO);
492775dbb685SIlya Dryomov 	if (IS_ERR(lreq->notify_id_pages)) {
492875dbb685SIlya Dryomov 		ret = PTR_ERR(lreq->notify_id_pages);
492975dbb685SIlya Dryomov 		lreq->notify_id_pages = NULL;
493075dbb685SIlya Dryomov 		goto out_put_lreq;
493175dbb685SIlya Dryomov 	}
493275dbb685SIlya Dryomov 
493319079203SIlya Dryomov 	lreq->preply_pages = preply_pages;
493419079203SIlya Dryomov 	lreq->preply_len = preply_len;
493519079203SIlya Dryomov 
493619079203SIlya Dryomov 	ceph_oid_copy(&lreq->t.base_oid, oid);
493719079203SIlya Dryomov 	ceph_oloc_copy(&lreq->t.base_oloc, oloc);
493819079203SIlya Dryomov 	lreq->t.flags = CEPH_OSD_FLAG_READ;
493919079203SIlya Dryomov 
494081c65213SIlya Dryomov 	linger_submit(lreq);
494119079203SIlya Dryomov 	ret = linger_reg_commit_wait(lreq);
494219079203SIlya Dryomov 	if (!ret)
4943e6e28432SIlya Dryomov 		ret = linger_notify_finish_wait(lreq,
4944e6e28432SIlya Dryomov 				 msecs_to_jiffies(2 * timeout * MSEC_PER_SEC));
494519079203SIlya Dryomov 	else
494619079203SIlya Dryomov 		dout("lreq %p failed to initiate notify %d\n", lreq, ret);
494719079203SIlya Dryomov 
494819079203SIlya Dryomov 	linger_cancel(lreq);
494919079203SIlya Dryomov out_put_lreq:
495019079203SIlya Dryomov 	linger_put(lreq);
495119079203SIlya Dryomov 	return ret;
495219079203SIlya Dryomov }
495319079203SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_notify);
495419079203SIlya Dryomov 
49553d14c5d2SYehuda Sadeh /*
4956b07d3c4bSIlya Dryomov  * Return the number of milliseconds since the watch was last
4957b07d3c4bSIlya Dryomov  * confirmed, or an error.  If there is an error, the watch is no
4958b07d3c4bSIlya Dryomov  * longer valid, and should be destroyed with ceph_osdc_unwatch().
4959b07d3c4bSIlya Dryomov  */
4960b07d3c4bSIlya Dryomov int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
4961b07d3c4bSIlya Dryomov 			  struct ceph_osd_linger_request *lreq)
4962b07d3c4bSIlya Dryomov {
4963b07d3c4bSIlya Dryomov 	unsigned long stamp, age;
4964b07d3c4bSIlya Dryomov 	int ret;
4965b07d3c4bSIlya Dryomov 
4966b07d3c4bSIlya Dryomov 	down_read(&osdc->lock);
4967b07d3c4bSIlya Dryomov 	mutex_lock(&lreq->lock);
4968b07d3c4bSIlya Dryomov 	stamp = lreq->watch_valid_thru;
4969b07d3c4bSIlya Dryomov 	if (!list_empty(&lreq->pending_lworks)) {
4970b07d3c4bSIlya Dryomov 		struct linger_work *lwork =
4971b07d3c4bSIlya Dryomov 		    list_first_entry(&lreq->pending_lworks,
4972b07d3c4bSIlya Dryomov 				     struct linger_work,
4973b07d3c4bSIlya Dryomov 				     pending_item);
4974b07d3c4bSIlya Dryomov 
4975b07d3c4bSIlya Dryomov 		if (time_before(lwork->queued_stamp, stamp))
4976b07d3c4bSIlya Dryomov 			stamp = lwork->queued_stamp;
4977b07d3c4bSIlya Dryomov 	}
4978b07d3c4bSIlya Dryomov 	age = jiffies - stamp;
4979b07d3c4bSIlya Dryomov 	dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
4980b07d3c4bSIlya Dryomov 	     lreq, lreq->linger_id, age, lreq->last_error);
4981b07d3c4bSIlya Dryomov 	/* we are truncating to msecs, so return a safe upper bound */
4982b07d3c4bSIlya Dryomov 	ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
4983b07d3c4bSIlya Dryomov 
4984b07d3c4bSIlya Dryomov 	mutex_unlock(&lreq->lock);
4985b07d3c4bSIlya Dryomov 	up_read(&osdc->lock);
4986b07d3c4bSIlya Dryomov 	return ret;
4987b07d3c4bSIlya Dryomov }
4988b07d3c4bSIlya Dryomov 
4989a4ed38d7SDouglas Fuller static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
4990a4ed38d7SDouglas Fuller {
4991a4ed38d7SDouglas Fuller 	u8 struct_v;
4992a4ed38d7SDouglas Fuller 	u32 struct_len;
4993a4ed38d7SDouglas Fuller 	int ret;
4994a4ed38d7SDouglas Fuller 
4995a4ed38d7SDouglas Fuller 	ret = ceph_start_decoding(p, end, 2, "watch_item_t",
4996a4ed38d7SDouglas Fuller 				  &struct_v, &struct_len);
4997a4ed38d7SDouglas Fuller 	if (ret)
499851fc7ab4SJeff Layton 		goto bad;
4999a4ed38d7SDouglas Fuller 
500051fc7ab4SJeff Layton 	ret = -EINVAL;
500151fc7ab4SJeff Layton 	ceph_decode_copy_safe(p, end, &item->name, sizeof(item->name), bad);
500251fc7ab4SJeff Layton 	ceph_decode_64_safe(p, end, item->cookie, bad);
500351fc7ab4SJeff Layton 	ceph_decode_skip_32(p, end, bad); /* skip timeout seconds */
500451fc7ab4SJeff Layton 
5005a4ed38d7SDouglas Fuller 	if (struct_v >= 2) {
500651fc7ab4SJeff Layton 		ret = ceph_decode_entity_addr(p, end, &item->addr);
500751fc7ab4SJeff Layton 		if (ret)
500851fc7ab4SJeff Layton 			goto bad;
500951fc7ab4SJeff Layton 	} else {
501051fc7ab4SJeff Layton 		ret = 0;
5011a4ed38d7SDouglas Fuller 	}
5012a4ed38d7SDouglas Fuller 
5013a4ed38d7SDouglas Fuller 	dout("%s %s%llu cookie %llu addr %s\n", __func__,
5014a4ed38d7SDouglas Fuller 	     ENTITY_NAME(item->name), item->cookie,
5015b726ec97SJeff Layton 	     ceph_pr_addr(&item->addr));
501651fc7ab4SJeff Layton bad:
501751fc7ab4SJeff Layton 	return ret;
5018a4ed38d7SDouglas Fuller }
5019a4ed38d7SDouglas Fuller 
5020a4ed38d7SDouglas Fuller static int decode_watchers(void **p, void *end,
5021a4ed38d7SDouglas Fuller 			   struct ceph_watch_item **watchers,
5022a4ed38d7SDouglas Fuller 			   u32 *num_watchers)
5023a4ed38d7SDouglas Fuller {
5024a4ed38d7SDouglas Fuller 	u8 struct_v;
5025a4ed38d7SDouglas Fuller 	u32 struct_len;
5026a4ed38d7SDouglas Fuller 	int i;
5027a4ed38d7SDouglas Fuller 	int ret;
5028a4ed38d7SDouglas Fuller 
5029a4ed38d7SDouglas Fuller 	ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
5030a4ed38d7SDouglas Fuller 				  &struct_v, &struct_len);
5031a4ed38d7SDouglas Fuller 	if (ret)
5032a4ed38d7SDouglas Fuller 		return ret;
5033a4ed38d7SDouglas Fuller 
5034a4ed38d7SDouglas Fuller 	*num_watchers = ceph_decode_32(p);
5035a4ed38d7SDouglas Fuller 	*watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
5036a4ed38d7SDouglas Fuller 	if (!*watchers)
5037a4ed38d7SDouglas Fuller 		return -ENOMEM;
5038a4ed38d7SDouglas Fuller 
5039a4ed38d7SDouglas Fuller 	for (i = 0; i < *num_watchers; i++) {
5040a4ed38d7SDouglas Fuller 		ret = decode_watcher(p, end, *watchers + i);
5041a4ed38d7SDouglas Fuller 		if (ret) {
5042a4ed38d7SDouglas Fuller 			kfree(*watchers);
5043a4ed38d7SDouglas Fuller 			return ret;
5044a4ed38d7SDouglas Fuller 		}
5045a4ed38d7SDouglas Fuller 	}
5046a4ed38d7SDouglas Fuller 
5047a4ed38d7SDouglas Fuller 	return 0;
5048a4ed38d7SDouglas Fuller }
5049a4ed38d7SDouglas Fuller 
5050a4ed38d7SDouglas Fuller /*
5051a4ed38d7SDouglas Fuller  * On success, the caller is responsible for:
5052a4ed38d7SDouglas Fuller  *
5053a4ed38d7SDouglas Fuller  *     kfree(watchers);
5054a4ed38d7SDouglas Fuller  */
5055a4ed38d7SDouglas Fuller int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
5056a4ed38d7SDouglas Fuller 			    struct ceph_object_id *oid,
5057a4ed38d7SDouglas Fuller 			    struct ceph_object_locator *oloc,
5058a4ed38d7SDouglas Fuller 			    struct ceph_watch_item **watchers,
5059a4ed38d7SDouglas Fuller 			    u32 *num_watchers)
5060a4ed38d7SDouglas Fuller {
5061a4ed38d7SDouglas Fuller 	struct ceph_osd_request *req;
5062a4ed38d7SDouglas Fuller 	struct page **pages;
5063a4ed38d7SDouglas Fuller 	int ret;
5064a4ed38d7SDouglas Fuller 
5065a4ed38d7SDouglas Fuller 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
5066a4ed38d7SDouglas Fuller 	if (!req)
5067a4ed38d7SDouglas Fuller 		return -ENOMEM;
5068a4ed38d7SDouglas Fuller 
5069a4ed38d7SDouglas Fuller 	ceph_oid_copy(&req->r_base_oid, oid);
5070a4ed38d7SDouglas Fuller 	ceph_oloc_copy(&req->r_base_oloc, oloc);
5071a4ed38d7SDouglas Fuller 	req->r_flags = CEPH_OSD_FLAG_READ;
5072a4ed38d7SDouglas Fuller 
5073a4ed38d7SDouglas Fuller 	pages = ceph_alloc_page_vector(1, GFP_NOIO);
5074a4ed38d7SDouglas Fuller 	if (IS_ERR(pages)) {
5075a4ed38d7SDouglas Fuller 		ret = PTR_ERR(pages);
5076a4ed38d7SDouglas Fuller 		goto out_put_req;
5077a4ed38d7SDouglas Fuller 	}
5078a4ed38d7SDouglas Fuller 
5079a4ed38d7SDouglas Fuller 	osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
5080a4ed38d7SDouglas Fuller 	ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
5081a4ed38d7SDouglas Fuller 						 response_data),
5082a4ed38d7SDouglas Fuller 				 pages, PAGE_SIZE, 0, false, true);
5083a4ed38d7SDouglas Fuller 
508426f887e0SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
508526f887e0SIlya Dryomov 	if (ret)
508626f887e0SIlya Dryomov 		goto out_put_req;
508726f887e0SIlya Dryomov 
5088a8af0d68SJeff Layton 	ceph_osdc_start_request(osdc, req);
5089a4ed38d7SDouglas Fuller 	ret = ceph_osdc_wait_request(osdc, req);
5090a4ed38d7SDouglas Fuller 	if (ret >= 0) {
5091a4ed38d7SDouglas Fuller 		void *p = page_address(pages[0]);
5092a4ed38d7SDouglas Fuller 		void *const end = p + req->r_ops[0].outdata_len;
5093a4ed38d7SDouglas Fuller 
5094a4ed38d7SDouglas Fuller 		ret = decode_watchers(&p, end, watchers, num_watchers);
5095a4ed38d7SDouglas Fuller 	}
5096a4ed38d7SDouglas Fuller 
5097a4ed38d7SDouglas Fuller out_put_req:
5098a4ed38d7SDouglas Fuller 	ceph_osdc_put_request(req);
5099a4ed38d7SDouglas Fuller 	return ret;
5100a4ed38d7SDouglas Fuller }
5101a4ed38d7SDouglas Fuller EXPORT_SYMBOL(ceph_osdc_list_watchers);
5102a4ed38d7SDouglas Fuller 
5103b07d3c4bSIlya Dryomov /*
5104dd935f44SJosh Durgin  * Call all pending notify callbacks - for use after a watch is
5105dd935f44SJosh Durgin  * unregistered, to make sure no more callbacks for it will be invoked
5106dd935f44SJosh Durgin  */
5107f6479449Sstephen hemminger void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
5108dd935f44SJosh Durgin {
510999d16943SIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
5110dd935f44SJosh Durgin 	flush_workqueue(osdc->notify_wq);
5111dd935f44SJosh Durgin }
5112dd935f44SJosh Durgin EXPORT_SYMBOL(ceph_osdc_flush_notifies);
5113dd935f44SJosh Durgin 
51147cca78c9SIlya Dryomov void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
51157cca78c9SIlya Dryomov {
51167cca78c9SIlya Dryomov 	down_read(&osdc->lock);
51177cca78c9SIlya Dryomov 	maybe_request_map(osdc);
51187cca78c9SIlya Dryomov 	up_read(&osdc->lock);
51197cca78c9SIlya Dryomov }
51207cca78c9SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
5121dd935f44SJosh Durgin 
5122dd935f44SJosh Durgin /*
5123428a7158SDouglas Fuller  * Execute an OSD class method on an object.
5124428a7158SDouglas Fuller  *
5125428a7158SDouglas Fuller  * @flags: CEPH_OSD_FLAG_*
51262544a020SIlya Dryomov  * @resp_len: in/out param for reply length
5127428a7158SDouglas Fuller  */
5128428a7158SDouglas Fuller int ceph_osdc_call(struct ceph_osd_client *osdc,
5129428a7158SDouglas Fuller 		   struct ceph_object_id *oid,
5130428a7158SDouglas Fuller 		   struct ceph_object_locator *oloc,
5131428a7158SDouglas Fuller 		   const char *class, const char *method,
5132428a7158SDouglas Fuller 		   unsigned int flags,
5133428a7158SDouglas Fuller 		   struct page *req_page, size_t req_len,
513468ada915SIlya Dryomov 		   struct page **resp_pages, size_t *resp_len)
5135428a7158SDouglas Fuller {
5136428a7158SDouglas Fuller 	struct ceph_osd_request *req;
5137428a7158SDouglas Fuller 	int ret;
5138428a7158SDouglas Fuller 
513968ada915SIlya Dryomov 	if (req_len > PAGE_SIZE)
51402544a020SIlya Dryomov 		return -E2BIG;
51412544a020SIlya Dryomov 
5142428a7158SDouglas Fuller 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
5143428a7158SDouglas Fuller 	if (!req)
5144428a7158SDouglas Fuller 		return -ENOMEM;
5145428a7158SDouglas Fuller 
5146428a7158SDouglas Fuller 	ceph_oid_copy(&req->r_base_oid, oid);
5147428a7158SDouglas Fuller 	ceph_oloc_copy(&req->r_base_oloc, oloc);
5148428a7158SDouglas Fuller 	req->r_flags = flags;
5149428a7158SDouglas Fuller 
515024639ce5SIlya Dryomov 	ret = osd_req_op_cls_init(req, 0, class, method);
5151fe943d50SChengguang Xu 	if (ret)
5152fe943d50SChengguang Xu 		goto out_put_req;
5153fe943d50SChengguang Xu 
5154428a7158SDouglas Fuller 	if (req_page)
5155428a7158SDouglas Fuller 		osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
5156428a7158SDouglas Fuller 						  0, false, false);
515768ada915SIlya Dryomov 	if (resp_pages)
515868ada915SIlya Dryomov 		osd_req_op_cls_response_data_pages(req, 0, resp_pages,
51592544a020SIlya Dryomov 						   *resp_len, 0, false, false);
5160428a7158SDouglas Fuller 
516126f887e0SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
516226f887e0SIlya Dryomov 	if (ret)
516326f887e0SIlya Dryomov 		goto out_put_req;
516426f887e0SIlya Dryomov 
5165a8af0d68SJeff Layton 	ceph_osdc_start_request(osdc, req);
5166428a7158SDouglas Fuller 	ret = ceph_osdc_wait_request(osdc, req);
5167428a7158SDouglas Fuller 	if (ret >= 0) {
5168428a7158SDouglas Fuller 		ret = req->r_ops[0].rval;
516968ada915SIlya Dryomov 		if (resp_pages)
5170428a7158SDouglas Fuller 			*resp_len = req->r_ops[0].outdata_len;
5171428a7158SDouglas Fuller 	}
5172428a7158SDouglas Fuller 
5173428a7158SDouglas Fuller out_put_req:
5174428a7158SDouglas Fuller 	ceph_osdc_put_request(req);
5175428a7158SDouglas Fuller 	return ret;
5176428a7158SDouglas Fuller }
5177428a7158SDouglas Fuller EXPORT_SYMBOL(ceph_osdc_call);
5178428a7158SDouglas Fuller 
5179428a7158SDouglas Fuller /*
5180120a75eaSYan, Zheng  * reset all osd connections
5181120a75eaSYan, Zheng  */
5182120a75eaSYan, Zheng void ceph_osdc_reopen_osds(struct ceph_osd_client *osdc)
5183120a75eaSYan, Zheng {
5184120a75eaSYan, Zheng 	struct rb_node *n;
5185120a75eaSYan, Zheng 
5186120a75eaSYan, Zheng 	down_write(&osdc->lock);
5187120a75eaSYan, Zheng 	for (n = rb_first(&osdc->osds); n; ) {
5188120a75eaSYan, Zheng 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
5189120a75eaSYan, Zheng 
5190120a75eaSYan, Zheng 		n = rb_next(n);
5191120a75eaSYan, Zheng 		if (!reopen_osd(osd))
5192120a75eaSYan, Zheng 			kick_osd_requests(osd);
5193120a75eaSYan, Zheng 	}
5194120a75eaSYan, Zheng 	up_write(&osdc->lock);
5195120a75eaSYan, Zheng }
5196120a75eaSYan, Zheng 
5197120a75eaSYan, Zheng /*
51983d14c5d2SYehuda Sadeh  * init, shutdown
51993d14c5d2SYehuda Sadeh  */
52003d14c5d2SYehuda Sadeh int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
52013d14c5d2SYehuda Sadeh {
52023d14c5d2SYehuda Sadeh 	int err;
52033d14c5d2SYehuda Sadeh 
52043d14c5d2SYehuda Sadeh 	dout("init\n");
52053d14c5d2SYehuda Sadeh 	osdc->client = client;
52065aea3dcdSIlya Dryomov 	init_rwsem(&osdc->lock);
52073d14c5d2SYehuda Sadeh 	osdc->osds = RB_ROOT;
52083d14c5d2SYehuda Sadeh 	INIT_LIST_HEAD(&osdc->osd_lru);
52099dd2845cSIlya Dryomov 	spin_lock_init(&osdc->osd_lru_lock);
52105aea3dcdSIlya Dryomov 	osd_init(&osdc->homeless_osd);
52115aea3dcdSIlya Dryomov 	osdc->homeless_osd.o_osdc = osdc;
52125aea3dcdSIlya Dryomov 	osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
5213264048afSIlya Dryomov 	osdc->last_linger_id = CEPH_LINGER_ID_START;
5214922dab61SIlya Dryomov 	osdc->linger_requests = RB_ROOT;
52154609245eSIlya Dryomov 	osdc->map_checks = RB_ROOT;
52164609245eSIlya Dryomov 	osdc->linger_map_checks = RB_ROOT;
52173d14c5d2SYehuda Sadeh 	INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
52183d14c5d2SYehuda Sadeh 	INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
52193d14c5d2SYehuda Sadeh 
52203d14c5d2SYehuda Sadeh 	err = -ENOMEM;
5221e5253a7bSIlya Dryomov 	osdc->osdmap = ceph_osdmap_alloc();
5222e5253a7bSIlya Dryomov 	if (!osdc->osdmap)
5223e5253a7bSIlya Dryomov 		goto out;
5224e5253a7bSIlya Dryomov 
52259e767adbSIlya Dryomov 	osdc->req_mempool = mempool_create_slab_pool(10,
52269e767adbSIlya Dryomov 						     ceph_osd_request_cache);
52273d14c5d2SYehuda Sadeh 	if (!osdc->req_mempool)
5228e5253a7bSIlya Dryomov 		goto out_map;
52293d14c5d2SYehuda Sadeh 
5230d50b409fSSage Weil 	err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
52310d9c1ab3SIlya Dryomov 				PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10, "osd_op");
52323d14c5d2SYehuda Sadeh 	if (err < 0)
52333d14c5d2SYehuda Sadeh 		goto out_mempool;
5234d50b409fSSage Weil 	err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
52350d9c1ab3SIlya Dryomov 				PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10,
52360d9c1ab3SIlya Dryomov 				"osd_op_reply");
52373d14c5d2SYehuda Sadeh 	if (err < 0)
52383d14c5d2SYehuda Sadeh 		goto out_msgpool;
5239a40c4f10SYehuda Sadeh 
5240dbcae088SDan Carpenter 	err = -ENOMEM;
5241a40c4f10SYehuda Sadeh 	osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
5242dbcae088SDan Carpenter 	if (!osdc->notify_wq)
5243c172ec5cSIlya Dryomov 		goto out_msgpool_reply;
5244c172ec5cSIlya Dryomov 
524588bc1922SIlya Dryomov 	osdc->completion_wq = create_singlethread_workqueue("ceph-completion");
524688bc1922SIlya Dryomov 	if (!osdc->completion_wq)
524788bc1922SIlya Dryomov 		goto out_notify_wq;
524888bc1922SIlya Dryomov 
5249fbca9635SIlya Dryomov 	schedule_delayed_work(&osdc->timeout_work,
5250fbca9635SIlya Dryomov 			      osdc->client->options->osd_keepalive_timeout);
5251b37ee1b9SIlya Dryomov 	schedule_delayed_work(&osdc->osds_timeout_work,
5252b37ee1b9SIlya Dryomov 	    round_jiffies_relative(osdc->client->options->osd_idle_ttl));
5253b37ee1b9SIlya Dryomov 
52543d14c5d2SYehuda Sadeh 	return 0;
52553d14c5d2SYehuda Sadeh 
525688bc1922SIlya Dryomov out_notify_wq:
525788bc1922SIlya Dryomov 	destroy_workqueue(osdc->notify_wq);
5258c172ec5cSIlya Dryomov out_msgpool_reply:
5259c172ec5cSIlya Dryomov 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
52603d14c5d2SYehuda Sadeh out_msgpool:
52613d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op);
52623d14c5d2SYehuda Sadeh out_mempool:
52633d14c5d2SYehuda Sadeh 	mempool_destroy(osdc->req_mempool);
5264e5253a7bSIlya Dryomov out_map:
5265e5253a7bSIlya Dryomov 	ceph_osdmap_destroy(osdc->osdmap);
52663d14c5d2SYehuda Sadeh out:
52673d14c5d2SYehuda Sadeh 	return err;
52683d14c5d2SYehuda Sadeh }
52693d14c5d2SYehuda Sadeh 
52703d14c5d2SYehuda Sadeh void ceph_osdc_stop(struct ceph_osd_client *osdc)
52713d14c5d2SYehuda Sadeh {
527288bc1922SIlya Dryomov 	destroy_workqueue(osdc->completion_wq);
5273a40c4f10SYehuda Sadeh 	destroy_workqueue(osdc->notify_wq);
52743d14c5d2SYehuda Sadeh 	cancel_delayed_work_sync(&osdc->timeout_work);
52753d14c5d2SYehuda Sadeh 	cancel_delayed_work_sync(&osdc->osds_timeout_work);
527642a2c09fSIlya Dryomov 
52775aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
527842a2c09fSIlya Dryomov 	while (!RB_EMPTY_ROOT(&osdc->osds)) {
527942a2c09fSIlya Dryomov 		struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
528042a2c09fSIlya Dryomov 						struct ceph_osd, o_node);
52815aea3dcdSIlya Dryomov 		close_osd(osd);
528242a2c09fSIlya Dryomov 	}
52835aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
528402113a0fSElena Reshetova 	WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
52855aea3dcdSIlya Dryomov 	osd_cleanup(&osdc->homeless_osd);
52865aea3dcdSIlya Dryomov 
52875aea3dcdSIlya Dryomov 	WARN_ON(!list_empty(&osdc->osd_lru));
5288922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
52894609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
52904609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
52915aea3dcdSIlya Dryomov 	WARN_ON(atomic_read(&osdc->num_requests));
52925aea3dcdSIlya Dryomov 	WARN_ON(atomic_read(&osdc->num_homeless));
529342a2c09fSIlya Dryomov 
52943d14c5d2SYehuda Sadeh 	ceph_osdmap_destroy(osdc->osdmap);
52953d14c5d2SYehuda Sadeh 	mempool_destroy(osdc->req_mempool);
52963d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op);
52973d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
52983d14c5d2SYehuda Sadeh }
52993d14c5d2SYehuda Sadeh 
5300aca39d9eSLuís Henriques int osd_req_op_copy_from_init(struct ceph_osd_request *req,
530123ddf9beSLuis Henriques 			      u64 src_snapid, u64 src_version,
530223ddf9beSLuis Henriques 			      struct ceph_object_id *src_oid,
530323ddf9beSLuis Henriques 			      struct ceph_object_locator *src_oloc,
530423ddf9beSLuis Henriques 			      u32 src_fadvise_flags,
530523ddf9beSLuis Henriques 			      u32 dst_fadvise_flags,
530678beb0ffSLuis Henriques 			      u32 truncate_seq, u64 truncate_size,
530723ddf9beSLuis Henriques 			      u8 copy_from_flags)
530823ddf9beSLuis Henriques {
530923ddf9beSLuis Henriques 	struct ceph_osd_req_op *op;
531023ddf9beSLuis Henriques 	struct page **pages;
531123ddf9beSLuis Henriques 	void *p, *end;
531223ddf9beSLuis Henriques 
531323ddf9beSLuis Henriques 	pages = ceph_alloc_page_vector(1, GFP_KERNEL);
531423ddf9beSLuis Henriques 	if (IS_ERR(pages))
531523ddf9beSLuis Henriques 		return PTR_ERR(pages);
531623ddf9beSLuis Henriques 
5317042f6498SJeff Layton 	op = osd_req_op_init(req, 0, CEPH_OSD_OP_COPY_FROM2,
531878beb0ffSLuis Henriques 			     dst_fadvise_flags);
531923ddf9beSLuis Henriques 	op->copy_from.snapid = src_snapid;
532023ddf9beSLuis Henriques 	op->copy_from.src_version = src_version;
532123ddf9beSLuis Henriques 	op->copy_from.flags = copy_from_flags;
532223ddf9beSLuis Henriques 	op->copy_from.src_fadvise_flags = src_fadvise_flags;
532323ddf9beSLuis Henriques 
532423ddf9beSLuis Henriques 	p = page_address(pages[0]);
532523ddf9beSLuis Henriques 	end = p + PAGE_SIZE;
532623ddf9beSLuis Henriques 	ceph_encode_string(&p, end, src_oid->name, src_oid->name_len);
532723ddf9beSLuis Henriques 	encode_oloc(&p, end, src_oloc);
532878beb0ffSLuis Henriques 	ceph_encode_32(&p, truncate_seq);
532978beb0ffSLuis Henriques 	ceph_encode_64(&p, truncate_size);
533023ddf9beSLuis Henriques 	op->indata_len = PAGE_SIZE - (end - p);
533123ddf9beSLuis Henriques 
533223ddf9beSLuis Henriques 	ceph_osd_data_pages_init(&op->copy_from.osd_data, pages,
533323ddf9beSLuis Henriques 				 op->indata_len, 0, false, true);
533423ddf9beSLuis Henriques 	return 0;
533523ddf9beSLuis Henriques }
5336aca39d9eSLuís Henriques EXPORT_SYMBOL(osd_req_op_copy_from_init);
533723ddf9beSLuis Henriques 
533857a35dfbSChengguang Xu int __init ceph_osdc_setup(void)
53395522ae0bSAlex Elder {
53403f1af42aSIlya Dryomov 	size_t size = sizeof(struct ceph_osd_request) +
53413f1af42aSIlya Dryomov 	    CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
53423f1af42aSIlya Dryomov 
53435522ae0bSAlex Elder 	BUG_ON(ceph_osd_request_cache);
53443f1af42aSIlya Dryomov 	ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
53453f1af42aSIlya Dryomov 						   0, 0, NULL);
53465522ae0bSAlex Elder 
53475522ae0bSAlex Elder 	return ceph_osd_request_cache ? 0 : -ENOMEM;
53485522ae0bSAlex Elder }
53495522ae0bSAlex Elder 
53505522ae0bSAlex Elder void ceph_osdc_cleanup(void)
53515522ae0bSAlex Elder {
53525522ae0bSAlex Elder 	BUG_ON(!ceph_osd_request_cache);
53535522ae0bSAlex Elder 	kmem_cache_destroy(ceph_osd_request_cache);
53545522ae0bSAlex Elder 	ceph_osd_request_cache = NULL;
53555522ae0bSAlex Elder }
53565522ae0bSAlex Elder 
53573d14c5d2SYehuda Sadeh /*
53583d14c5d2SYehuda Sadeh  * handle incoming message
53593d14c5d2SYehuda Sadeh  */
53604972cf60SIlya Dryomov static void osd_dispatch(struct ceph_connection *con, struct ceph_msg *msg)
53613d14c5d2SYehuda Sadeh {
53623d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
53635aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
53643d14c5d2SYehuda Sadeh 	int type = le16_to_cpu(msg->hdr.type);
53653d14c5d2SYehuda Sadeh 
53663d14c5d2SYehuda Sadeh 	switch (type) {
53673d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP:
53683d14c5d2SYehuda Sadeh 		ceph_osdc_handle_map(osdc, msg);
53693d14c5d2SYehuda Sadeh 		break;
53703d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY:
53715aea3dcdSIlya Dryomov 		handle_reply(osd, msg);
53723d14c5d2SYehuda Sadeh 		break;
5373a02a946dSIlya Dryomov 	case CEPH_MSG_OSD_BACKOFF:
5374a02a946dSIlya Dryomov 		handle_backoff(osd, msg);
5375a02a946dSIlya Dryomov 		break;
5376a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY:
5377a40c4f10SYehuda Sadeh 		handle_watch_notify(osdc, msg);
5378a40c4f10SYehuda Sadeh 		break;
53793d14c5d2SYehuda Sadeh 
53803d14c5d2SYehuda Sadeh 	default:
53813d14c5d2SYehuda Sadeh 		pr_err("received unknown message type %d %s\n", type,
53823d14c5d2SYehuda Sadeh 		       ceph_msg_type_name(type));
53833d14c5d2SYehuda Sadeh 	}
53845aea3dcdSIlya Dryomov 
53853d14c5d2SYehuda Sadeh 	ceph_msg_put(msg);
53863d14c5d2SYehuda Sadeh }
53873d14c5d2SYehuda Sadeh 
5388*f628d799SJeff Layton /* How much sparse data was requested? */
5389*f628d799SJeff Layton static u64 sparse_data_requested(struct ceph_osd_request *req)
5390*f628d799SJeff Layton {
5391*f628d799SJeff Layton 	u64 len = 0;
5392*f628d799SJeff Layton 
5393*f628d799SJeff Layton 	if (req->r_flags & CEPH_OSD_FLAG_READ) {
5394*f628d799SJeff Layton 		int i;
5395*f628d799SJeff Layton 
5396*f628d799SJeff Layton 		for (i = 0; i < req->r_num_ops; ++i) {
5397*f628d799SJeff Layton 			struct ceph_osd_req_op *op = &req->r_ops[i];
5398*f628d799SJeff Layton 
5399*f628d799SJeff Layton 			if (op->op == CEPH_OSD_OP_SPARSE_READ)
5400*f628d799SJeff Layton 				len += op->extent.length;
5401*f628d799SJeff Layton 		}
5402*f628d799SJeff Layton 	}
5403*f628d799SJeff Layton 	return len;
5404*f628d799SJeff Layton }
5405*f628d799SJeff Layton 
54063d14c5d2SYehuda Sadeh /*
5407d15f9d69SIlya Dryomov  * Lookup and return message for incoming reply.  Don't try to do
5408d15f9d69SIlya Dryomov  * anything about a larger than preallocated data portion of the
5409d15f9d69SIlya Dryomov  * message at the moment - for now, just skip the message.
54103d14c5d2SYehuda Sadeh  */
54113d14c5d2SYehuda Sadeh static struct ceph_msg *get_reply(struct ceph_connection *con,
54123d14c5d2SYehuda Sadeh 				  struct ceph_msg_header *hdr,
54133d14c5d2SYehuda Sadeh 				  int *skip)
54143d14c5d2SYehuda Sadeh {
54153d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
54163d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = osd->o_osdc;
54175aea3dcdSIlya Dryomov 	struct ceph_msg *m = NULL;
54183d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
54193f0a4ac5SIlya Dryomov 	int front_len = le32_to_cpu(hdr->front_len);
54203d14c5d2SYehuda Sadeh 	int data_len = le32_to_cpu(hdr->data_len);
54215aea3dcdSIlya Dryomov 	u64 tid = le64_to_cpu(hdr->tid);
5422*f628d799SJeff Layton 	u64 srlen;
54233d14c5d2SYehuda Sadeh 
54245aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
54255aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
54265aea3dcdSIlya Dryomov 		dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
54275aea3dcdSIlya Dryomov 		*skip = 1;
54285aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
54295aea3dcdSIlya Dryomov 	}
54305aea3dcdSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
54315aea3dcdSIlya Dryomov 
54325aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
54335aea3dcdSIlya Dryomov 	req = lookup_request(&osd->o_requests, tid);
54343d14c5d2SYehuda Sadeh 	if (!req) {
5435cd8140c6SIlya Dryomov 		dout("%s osd%d tid %llu unknown, skipping\n", __func__,
5436cd8140c6SIlya Dryomov 		     osd->o_osd, tid);
5437d15f9d69SIlya Dryomov 		*skip = 1;
54385aea3dcdSIlya Dryomov 		goto out_unlock_session;
54393d14c5d2SYehuda Sadeh 	}
54403d14c5d2SYehuda Sadeh 
54418921d114SAlex Elder 	ceph_msg_revoke_incoming(req->r_reply);
54423d14c5d2SYehuda Sadeh 
5443f2be82b0SIlya Dryomov 	if (front_len > req->r_reply->front_alloc_len) {
5444d15f9d69SIlya Dryomov 		pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
5445d15f9d69SIlya Dryomov 			__func__, osd->o_osd, req->r_tid, front_len,
5446d15f9d69SIlya Dryomov 			req->r_reply->front_alloc_len);
54473f0a4ac5SIlya Dryomov 		m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
54483f0a4ac5SIlya Dryomov 				 false);
54493d14c5d2SYehuda Sadeh 		if (!m)
54505aea3dcdSIlya Dryomov 			goto out_unlock_session;
54513d14c5d2SYehuda Sadeh 		ceph_msg_put(req->r_reply);
54523d14c5d2SYehuda Sadeh 		req->r_reply = m;
54533d14c5d2SYehuda Sadeh 	}
54543d14c5d2SYehuda Sadeh 
5455*f628d799SJeff Layton 	srlen = sparse_data_requested(req);
5456*f628d799SJeff Layton 	if (!srlen && data_len > req->r_reply->data_length) {
5457d15f9d69SIlya Dryomov 		pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
5458d15f9d69SIlya Dryomov 			__func__, osd->o_osd, req->r_tid, data_len,
5459d15f9d69SIlya Dryomov 			req->r_reply->data_length);
54603d14c5d2SYehuda Sadeh 		m = NULL;
5461d15f9d69SIlya Dryomov 		*skip = 1;
54625aea3dcdSIlya Dryomov 		goto out_unlock_session;
54633d14c5d2SYehuda Sadeh 	}
5464d15f9d69SIlya Dryomov 
5465d15f9d69SIlya Dryomov 	m = ceph_msg_get(req->r_reply);
5466*f628d799SJeff Layton 	m->sparse_read = (bool)srlen;
5467*f628d799SJeff Layton 
54683d14c5d2SYehuda Sadeh 	dout("get_reply tid %lld %p\n", tid, m);
54693d14c5d2SYehuda Sadeh 
54705aea3dcdSIlya Dryomov out_unlock_session:
54715aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
54725aea3dcdSIlya Dryomov out_unlock_osdc:
54735aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
54743d14c5d2SYehuda Sadeh 	return m;
54753d14c5d2SYehuda Sadeh }
54763d14c5d2SYehuda Sadeh 
547719079203SIlya Dryomov static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
547819079203SIlya Dryomov {
547919079203SIlya Dryomov 	struct ceph_msg *m;
548019079203SIlya Dryomov 	int type = le16_to_cpu(hdr->type);
548119079203SIlya Dryomov 	u32 front_len = le32_to_cpu(hdr->front_len);
548219079203SIlya Dryomov 	u32 data_len = le32_to_cpu(hdr->data_len);
548319079203SIlya Dryomov 
54840d9c1ab3SIlya Dryomov 	m = ceph_msg_new2(type, front_len, 1, GFP_NOIO, false);
548519079203SIlya Dryomov 	if (!m)
548619079203SIlya Dryomov 		return NULL;
548719079203SIlya Dryomov 
548819079203SIlya Dryomov 	if (data_len) {
548919079203SIlya Dryomov 		struct page **pages;
549019079203SIlya Dryomov 
549119079203SIlya Dryomov 		pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
549219079203SIlya Dryomov 					       GFP_NOIO);
5493c22e853aSWei Yongjun 		if (IS_ERR(pages)) {
549419079203SIlya Dryomov 			ceph_msg_put(m);
549519079203SIlya Dryomov 			return NULL;
549619079203SIlya Dryomov 		}
549719079203SIlya Dryomov 
5498e8862740SIlya Dryomov 		ceph_msg_data_add_pages(m, pages, data_len, 0, true);
549919079203SIlya Dryomov 	}
550019079203SIlya Dryomov 
550119079203SIlya Dryomov 	return m;
550219079203SIlya Dryomov }
550319079203SIlya Dryomov 
55044972cf60SIlya Dryomov static struct ceph_msg *osd_alloc_msg(struct ceph_connection *con,
55053d14c5d2SYehuda Sadeh 				      struct ceph_msg_header *hdr,
55063d14c5d2SYehuda Sadeh 				      int *skip)
55073d14c5d2SYehuda Sadeh {
55083d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
55093d14c5d2SYehuda Sadeh 	int type = le16_to_cpu(hdr->type);
55103d14c5d2SYehuda Sadeh 
55111c20f2d2SAlex Elder 	*skip = 0;
55123d14c5d2SYehuda Sadeh 	switch (type) {
55133d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP:
5514a02a946dSIlya Dryomov 	case CEPH_MSG_OSD_BACKOFF:
5515a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY:
551619079203SIlya Dryomov 		return alloc_msg_with_page_vector(hdr);
55173d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY:
55183d14c5d2SYehuda Sadeh 		return get_reply(con, hdr, skip);
55193d14c5d2SYehuda Sadeh 	default:
55205aea3dcdSIlya Dryomov 		pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
55215aea3dcdSIlya Dryomov 			osd->o_osd, type);
55223d14c5d2SYehuda Sadeh 		*skip = 1;
55233d14c5d2SYehuda Sadeh 		return NULL;
55243d14c5d2SYehuda Sadeh 	}
55253d14c5d2SYehuda Sadeh }
55263d14c5d2SYehuda Sadeh 
55273d14c5d2SYehuda Sadeh /*
55283d14c5d2SYehuda Sadeh  * Wrappers to refcount containing ceph_osd struct
55293d14c5d2SYehuda Sadeh  */
55304972cf60SIlya Dryomov static struct ceph_connection *osd_get_con(struct ceph_connection *con)
55313d14c5d2SYehuda Sadeh {
55323d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
55333d14c5d2SYehuda Sadeh 	if (get_osd(osd))
55343d14c5d2SYehuda Sadeh 		return con;
55353d14c5d2SYehuda Sadeh 	return NULL;
55363d14c5d2SYehuda Sadeh }
55373d14c5d2SYehuda Sadeh 
55384972cf60SIlya Dryomov static void osd_put_con(struct ceph_connection *con)
55393d14c5d2SYehuda Sadeh {
55403d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
55413d14c5d2SYehuda Sadeh 	put_osd(osd);
55423d14c5d2SYehuda Sadeh }
55433d14c5d2SYehuda Sadeh 
55443d14c5d2SYehuda Sadeh /*
55453d14c5d2SYehuda Sadeh  * authentication
55463d14c5d2SYehuda Sadeh  */
5547cd1a677cSIlya Dryomov 
5548a3530df3SAlex Elder /*
5549a3530df3SAlex Elder  * Note: returned pointer is the address of a structure that's
5550a3530df3SAlex Elder  * managed separately.  Caller must *not* attempt to free it.
5551a3530df3SAlex Elder  */
55524972cf60SIlya Dryomov static struct ceph_auth_handshake *
55534972cf60SIlya Dryomov osd_get_authorizer(struct ceph_connection *con, int *proto, int force_new)
55543d14c5d2SYehuda Sadeh {
55553d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
55563d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
55573d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
555874f1869fSAlex Elder 	struct ceph_auth_handshake *auth = &o->o_auth;
5559ce287162SIlya Dryomov 	int ret;
55603d14c5d2SYehuda Sadeh 
5561ce287162SIlya Dryomov 	ret = __ceph_auth_get_authorizer(ac, auth, CEPH_ENTITY_TYPE_OSD,
5562ce287162SIlya Dryomov 					 force_new, proto, NULL, NULL);
55633d14c5d2SYehuda Sadeh 	if (ret)
5564a3530df3SAlex Elder 		return ERR_PTR(ret);
556574f1869fSAlex Elder 
5566a3530df3SAlex Elder 	return auth;
55673d14c5d2SYehuda Sadeh }
55683d14c5d2SYehuda Sadeh 
55694972cf60SIlya Dryomov static int osd_add_authorizer_challenge(struct ceph_connection *con,
55706daca13dSIlya Dryomov 				    void *challenge_buf, int challenge_buf_len)
55716daca13dSIlya Dryomov {
55726daca13dSIlya Dryomov 	struct ceph_osd *o = con->private;
55736daca13dSIlya Dryomov 	struct ceph_osd_client *osdc = o->o_osdc;
55746daca13dSIlya Dryomov 	struct ceph_auth_client *ac = osdc->client->monc.auth;
55756daca13dSIlya Dryomov 
55766daca13dSIlya Dryomov 	return ceph_auth_add_authorizer_challenge(ac, o->o_auth.authorizer,
55776daca13dSIlya Dryomov 					    challenge_buf, challenge_buf_len);
55786daca13dSIlya Dryomov }
55793d14c5d2SYehuda Sadeh 
55804972cf60SIlya Dryomov static int osd_verify_authorizer_reply(struct ceph_connection *con)
55813d14c5d2SYehuda Sadeh {
55823d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
55833d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
55843d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
5585285ea34fSIlya Dryomov 	struct ceph_auth_handshake *auth = &o->o_auth;
55863d14c5d2SYehuda Sadeh 
5587285ea34fSIlya Dryomov 	return ceph_auth_verify_authorizer_reply(ac, auth->authorizer,
5588285ea34fSIlya Dryomov 		auth->authorizer_reply_buf, auth->authorizer_reply_buf_len,
5589285ea34fSIlya Dryomov 		NULL, NULL, NULL, NULL);
55903d14c5d2SYehuda Sadeh }
55913d14c5d2SYehuda Sadeh 
55924972cf60SIlya Dryomov static int osd_invalidate_authorizer(struct ceph_connection *con)
55933d14c5d2SYehuda Sadeh {
55943d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
55953d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
55963d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
55973d14c5d2SYehuda Sadeh 
559827859f97SSage Weil 	ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
55993d14c5d2SYehuda Sadeh 	return ceph_monc_validate_auth(&osdc->client->monc);
56003d14c5d2SYehuda Sadeh }
56013d14c5d2SYehuda Sadeh 
5602cd1a677cSIlya Dryomov static int osd_get_auth_request(struct ceph_connection *con,
5603cd1a677cSIlya Dryomov 				void *buf, int *buf_len,
5604cd1a677cSIlya Dryomov 				void **authorizer, int *authorizer_len)
5605cd1a677cSIlya Dryomov {
5606cd1a677cSIlya Dryomov 	struct ceph_osd *o = con->private;
5607cd1a677cSIlya Dryomov 	struct ceph_auth_client *ac = o->o_osdc->client->monc.auth;
5608cd1a677cSIlya Dryomov 	struct ceph_auth_handshake *auth = &o->o_auth;
5609cd1a677cSIlya Dryomov 	int ret;
5610cd1a677cSIlya Dryomov 
5611cd1a677cSIlya Dryomov 	ret = ceph_auth_get_authorizer(ac, auth, CEPH_ENTITY_TYPE_OSD,
5612cd1a677cSIlya Dryomov 				       buf, buf_len);
5613cd1a677cSIlya Dryomov 	if (ret)
5614cd1a677cSIlya Dryomov 		return ret;
5615cd1a677cSIlya Dryomov 
5616cd1a677cSIlya Dryomov 	*authorizer = auth->authorizer_buf;
5617cd1a677cSIlya Dryomov 	*authorizer_len = auth->authorizer_buf_len;
5618cd1a677cSIlya Dryomov 	return 0;
5619cd1a677cSIlya Dryomov }
5620cd1a677cSIlya Dryomov 
5621cd1a677cSIlya Dryomov static int osd_handle_auth_reply_more(struct ceph_connection *con,
5622cd1a677cSIlya Dryomov 				      void *reply, int reply_len,
5623cd1a677cSIlya Dryomov 				      void *buf, int *buf_len,
5624cd1a677cSIlya Dryomov 				      void **authorizer, int *authorizer_len)
5625cd1a677cSIlya Dryomov {
5626cd1a677cSIlya Dryomov 	struct ceph_osd *o = con->private;
5627cd1a677cSIlya Dryomov 	struct ceph_auth_client *ac = o->o_osdc->client->monc.auth;
5628cd1a677cSIlya Dryomov 	struct ceph_auth_handshake *auth = &o->o_auth;
5629cd1a677cSIlya Dryomov 	int ret;
5630cd1a677cSIlya Dryomov 
5631cd1a677cSIlya Dryomov 	ret = ceph_auth_handle_svc_reply_more(ac, auth, reply, reply_len,
5632cd1a677cSIlya Dryomov 					      buf, buf_len);
5633cd1a677cSIlya Dryomov 	if (ret)
5634cd1a677cSIlya Dryomov 		return ret;
5635cd1a677cSIlya Dryomov 
5636cd1a677cSIlya Dryomov 	*authorizer = auth->authorizer_buf;
5637cd1a677cSIlya Dryomov 	*authorizer_len = auth->authorizer_buf_len;
5638cd1a677cSIlya Dryomov 	return 0;
5639cd1a677cSIlya Dryomov }
5640cd1a677cSIlya Dryomov 
5641cd1a677cSIlya Dryomov static int osd_handle_auth_done(struct ceph_connection *con,
5642cd1a677cSIlya Dryomov 				u64 global_id, void *reply, int reply_len,
5643cd1a677cSIlya Dryomov 				u8 *session_key, int *session_key_len,
5644cd1a677cSIlya Dryomov 				u8 *con_secret, int *con_secret_len)
5645cd1a677cSIlya Dryomov {
5646cd1a677cSIlya Dryomov 	struct ceph_osd *o = con->private;
5647cd1a677cSIlya Dryomov 	struct ceph_auth_client *ac = o->o_osdc->client->monc.auth;
5648cd1a677cSIlya Dryomov 	struct ceph_auth_handshake *auth = &o->o_auth;
5649cd1a677cSIlya Dryomov 
5650cd1a677cSIlya Dryomov 	return ceph_auth_handle_svc_reply_done(ac, auth, reply, reply_len,
5651cd1a677cSIlya Dryomov 					       session_key, session_key_len,
5652cd1a677cSIlya Dryomov 					       con_secret, con_secret_len);
5653cd1a677cSIlya Dryomov }
5654cd1a677cSIlya Dryomov 
5655cd1a677cSIlya Dryomov static int osd_handle_auth_bad_method(struct ceph_connection *con,
5656cd1a677cSIlya Dryomov 				      int used_proto, int result,
5657cd1a677cSIlya Dryomov 				      const int *allowed_protos, int proto_cnt,
5658cd1a677cSIlya Dryomov 				      const int *allowed_modes, int mode_cnt)
5659cd1a677cSIlya Dryomov {
5660cd1a677cSIlya Dryomov 	struct ceph_osd *o = con->private;
5661cd1a677cSIlya Dryomov 	struct ceph_mon_client *monc = &o->o_osdc->client->monc;
5662cd1a677cSIlya Dryomov 	int ret;
5663cd1a677cSIlya Dryomov 
5664cd1a677cSIlya Dryomov 	if (ceph_auth_handle_bad_authorizer(monc->auth, CEPH_ENTITY_TYPE_OSD,
5665cd1a677cSIlya Dryomov 					    used_proto, result,
5666cd1a677cSIlya Dryomov 					    allowed_protos, proto_cnt,
5667cd1a677cSIlya Dryomov 					    allowed_modes, mode_cnt)) {
5668cd1a677cSIlya Dryomov 		ret = ceph_monc_validate_auth(monc);
5669cd1a677cSIlya Dryomov 		if (ret)
5670cd1a677cSIlya Dryomov 			return ret;
5671cd1a677cSIlya Dryomov 	}
5672cd1a677cSIlya Dryomov 
5673cd1a677cSIlya Dryomov 	return -EACCES;
5674cd1a677cSIlya Dryomov }
5675cd1a677cSIlya Dryomov 
56768cb441c0SIlya Dryomov static void osd_reencode_message(struct ceph_msg *msg)
56778cb441c0SIlya Dryomov {
5678914902afSIlya Dryomov 	int type = le16_to_cpu(msg->hdr.type);
5679914902afSIlya Dryomov 
5680914902afSIlya Dryomov 	if (type == CEPH_MSG_OSD_OP)
56818cb441c0SIlya Dryomov 		encode_request_finish(msg);
56828cb441c0SIlya Dryomov }
56838cb441c0SIlya Dryomov 
568479dbd1baSIlya Dryomov static int osd_sign_message(struct ceph_msg *msg)
568533d07337SYan, Zheng {
568679dbd1baSIlya Dryomov 	struct ceph_osd *o = msg->con->private;
568733d07337SYan, Zheng 	struct ceph_auth_handshake *auth = &o->o_auth;
568879dbd1baSIlya Dryomov 
568933d07337SYan, Zheng 	return ceph_auth_sign_message(auth, msg);
569033d07337SYan, Zheng }
569133d07337SYan, Zheng 
569279dbd1baSIlya Dryomov static int osd_check_message_signature(struct ceph_msg *msg)
569333d07337SYan, Zheng {
569479dbd1baSIlya Dryomov 	struct ceph_osd *o = msg->con->private;
569533d07337SYan, Zheng 	struct ceph_auth_handshake *auth = &o->o_auth;
569679dbd1baSIlya Dryomov 
569733d07337SYan, Zheng 	return ceph_auth_check_message_signature(auth, msg);
569833d07337SYan, Zheng }
569933d07337SYan, Zheng 
5700*f628d799SJeff Layton static void advance_cursor(struct ceph_msg_data_cursor *cursor, size_t len,
5701*f628d799SJeff Layton 			   bool zero)
5702*f628d799SJeff Layton {
5703*f628d799SJeff Layton 	while (len) {
5704*f628d799SJeff Layton 		struct page *page;
5705*f628d799SJeff Layton 		size_t poff, plen;
5706*f628d799SJeff Layton 
5707*f628d799SJeff Layton 		page = ceph_msg_data_next(cursor, &poff, &plen);
5708*f628d799SJeff Layton 		if (plen > len)
5709*f628d799SJeff Layton 			plen = len;
5710*f628d799SJeff Layton 		if (zero)
5711*f628d799SJeff Layton 			zero_user_segment(page, poff, poff + plen);
5712*f628d799SJeff Layton 		len -= plen;
5713*f628d799SJeff Layton 		ceph_msg_data_advance(cursor, plen);
5714*f628d799SJeff Layton 	}
5715*f628d799SJeff Layton }
5716*f628d799SJeff Layton 
5717*f628d799SJeff Layton static int prep_next_sparse_read(struct ceph_connection *con,
5718*f628d799SJeff Layton 				 struct ceph_msg_data_cursor *cursor)
5719*f628d799SJeff Layton {
5720*f628d799SJeff Layton 	struct ceph_osd *o = con->private;
5721*f628d799SJeff Layton 	struct ceph_sparse_read *sr = &o->o_sparse_read;
5722*f628d799SJeff Layton 	struct ceph_osd_request *req;
5723*f628d799SJeff Layton 	struct ceph_osd_req_op *op;
5724*f628d799SJeff Layton 
5725*f628d799SJeff Layton 	spin_lock(&o->o_requests_lock);
5726*f628d799SJeff Layton 	req = lookup_request(&o->o_requests, le64_to_cpu(con->in_msg->hdr.tid));
5727*f628d799SJeff Layton 	if (!req) {
5728*f628d799SJeff Layton 		spin_unlock(&o->o_requests_lock);
5729*f628d799SJeff Layton 		return -EBADR;
5730*f628d799SJeff Layton 	}
5731*f628d799SJeff Layton 
5732*f628d799SJeff Layton 	if (o->o_sparse_op_idx < 0) {
5733*f628d799SJeff Layton 		u64 srlen = sparse_data_requested(req);
5734*f628d799SJeff Layton 
5735*f628d799SJeff Layton 		dout("%s: [%d] starting new sparse read req. srlen=0x%llx\n",
5736*f628d799SJeff Layton 		     __func__, o->o_osd, srlen);
5737*f628d799SJeff Layton 		ceph_msg_data_cursor_init(cursor, con->in_msg, srlen);
5738*f628d799SJeff Layton 	} else {
5739*f628d799SJeff Layton 		u64 end;
5740*f628d799SJeff Layton 
5741*f628d799SJeff Layton 		op = &req->r_ops[o->o_sparse_op_idx];
5742*f628d799SJeff Layton 
5743*f628d799SJeff Layton 		WARN_ON_ONCE(op->extent.sparse_ext);
5744*f628d799SJeff Layton 
5745*f628d799SJeff Layton 		/* hand back buffer we took earlier */
5746*f628d799SJeff Layton 		op->extent.sparse_ext = sr->sr_extent;
5747*f628d799SJeff Layton 		sr->sr_extent = NULL;
5748*f628d799SJeff Layton 		op->extent.sparse_ext_cnt = sr->sr_count;
5749*f628d799SJeff Layton 		sr->sr_ext_len = 0;
5750*f628d799SJeff Layton 		dout("%s: [%d] completed extent array len %d cursor->resid %zd\n",
5751*f628d799SJeff Layton 		     __func__, o->o_osd, op->extent.sparse_ext_cnt, cursor->resid);
5752*f628d799SJeff Layton 		/* Advance to end of data for this operation */
5753*f628d799SJeff Layton 		end = ceph_sparse_ext_map_end(op);
5754*f628d799SJeff Layton 		if (end < sr->sr_req_len)
5755*f628d799SJeff Layton 			advance_cursor(cursor, sr->sr_req_len - end, false);
5756*f628d799SJeff Layton 	}
5757*f628d799SJeff Layton 
5758*f628d799SJeff Layton 	ceph_init_sparse_read(sr);
5759*f628d799SJeff Layton 
5760*f628d799SJeff Layton 	/* find next op in this request (if any) */
5761*f628d799SJeff Layton 	while (++o->o_sparse_op_idx < req->r_num_ops) {
5762*f628d799SJeff Layton 		op = &req->r_ops[o->o_sparse_op_idx];
5763*f628d799SJeff Layton 		if (op->op == CEPH_OSD_OP_SPARSE_READ)
5764*f628d799SJeff Layton 			goto found;
5765*f628d799SJeff Layton 	}
5766*f628d799SJeff Layton 
5767*f628d799SJeff Layton 	/* reset for next sparse read request */
5768*f628d799SJeff Layton 	spin_unlock(&o->o_requests_lock);
5769*f628d799SJeff Layton 	o->o_sparse_op_idx = -1;
5770*f628d799SJeff Layton 	return 0;
5771*f628d799SJeff Layton found:
5772*f628d799SJeff Layton 	sr->sr_req_off = op->extent.offset;
5773*f628d799SJeff Layton 	sr->sr_req_len = op->extent.length;
5774*f628d799SJeff Layton 	sr->sr_pos = sr->sr_req_off;
5775*f628d799SJeff Layton 	dout("%s: [%d] new sparse read op at idx %d 0x%llx~0x%llx\n", __func__,
5776*f628d799SJeff Layton 	     o->o_osd, o->o_sparse_op_idx, sr->sr_req_off, sr->sr_req_len);
5777*f628d799SJeff Layton 
5778*f628d799SJeff Layton 	/* hand off request's sparse extent map buffer */
5779*f628d799SJeff Layton 	sr->sr_ext_len = op->extent.sparse_ext_cnt;
5780*f628d799SJeff Layton 	op->extent.sparse_ext_cnt = 0;
5781*f628d799SJeff Layton 	sr->sr_extent = op->extent.sparse_ext;
5782*f628d799SJeff Layton 	op->extent.sparse_ext = NULL;
5783*f628d799SJeff Layton 
5784*f628d799SJeff Layton 	spin_unlock(&o->o_requests_lock);
5785*f628d799SJeff Layton 	return 1;
5786*f628d799SJeff Layton }
5787*f628d799SJeff Layton 
5788*f628d799SJeff Layton #ifdef __BIG_ENDIAN
5789*f628d799SJeff Layton static inline void convert_extent_map(struct ceph_sparse_read *sr)
5790*f628d799SJeff Layton {
5791*f628d799SJeff Layton 	int i;
5792*f628d799SJeff Layton 
5793*f628d799SJeff Layton 	for (i = 0; i < sr->sr_count; i++) {
5794*f628d799SJeff Layton 		struct ceph_sparse_extent *ext = &sr->sr_extent[i];
5795*f628d799SJeff Layton 
5796*f628d799SJeff Layton 		ext->off = le64_to_cpu((__force __le64)ext->off);
5797*f628d799SJeff Layton 		ext->len = le64_to_cpu((__force __le64)ext->len);
5798*f628d799SJeff Layton 	}
5799*f628d799SJeff Layton }
5800*f628d799SJeff Layton #else
5801*f628d799SJeff Layton static inline void convert_extent_map(struct ceph_sparse_read *sr)
5802*f628d799SJeff Layton {
5803*f628d799SJeff Layton }
5804*f628d799SJeff Layton #endif
5805*f628d799SJeff Layton 
5806*f628d799SJeff Layton #define MAX_EXTENTS 4096
5807*f628d799SJeff Layton 
5808*f628d799SJeff Layton static int osd_sparse_read(struct ceph_connection *con,
5809*f628d799SJeff Layton 			   struct ceph_msg_data_cursor *cursor,
5810*f628d799SJeff Layton 			   char **pbuf)
5811*f628d799SJeff Layton {
5812*f628d799SJeff Layton 	struct ceph_osd *o = con->private;
5813*f628d799SJeff Layton 	struct ceph_sparse_read *sr = &o->o_sparse_read;
5814*f628d799SJeff Layton 	u32 count = sr->sr_count;
5815*f628d799SJeff Layton 	u64 eoff, elen;
5816*f628d799SJeff Layton 	int ret;
5817*f628d799SJeff Layton 
5818*f628d799SJeff Layton 	switch (sr->sr_state) {
5819*f628d799SJeff Layton 	case CEPH_SPARSE_READ_HDR:
5820*f628d799SJeff Layton next_op:
5821*f628d799SJeff Layton 		ret = prep_next_sparse_read(con, cursor);
5822*f628d799SJeff Layton 		if (ret <= 0)
5823*f628d799SJeff Layton 			return ret;
5824*f628d799SJeff Layton 
5825*f628d799SJeff Layton 		/* number of extents */
5826*f628d799SJeff Layton 		ret = sizeof(sr->sr_count);
5827*f628d799SJeff Layton 		*pbuf = (char *)&sr->sr_count;
5828*f628d799SJeff Layton 		sr->sr_state = CEPH_SPARSE_READ_EXTENTS;
5829*f628d799SJeff Layton 		break;
5830*f628d799SJeff Layton 	case CEPH_SPARSE_READ_EXTENTS:
5831*f628d799SJeff Layton 		/* Convert sr_count to host-endian */
5832*f628d799SJeff Layton 		count = le32_to_cpu((__force __le32)sr->sr_count);
5833*f628d799SJeff Layton 		sr->sr_count = count;
5834*f628d799SJeff Layton 		dout("[%d] got %u extents\n", o->o_osd, count);
5835*f628d799SJeff Layton 
5836*f628d799SJeff Layton 		if (count > 0) {
5837*f628d799SJeff Layton 			if (!sr->sr_extent || count > sr->sr_ext_len) {
5838*f628d799SJeff Layton 				/*
5839*f628d799SJeff Layton 				 * Apply a hard cap to the number of extents.
5840*f628d799SJeff Layton 				 * If we have more, assume something is wrong.
5841*f628d799SJeff Layton 				 */
5842*f628d799SJeff Layton 				if (count > MAX_EXTENTS) {
5843*f628d799SJeff Layton 					dout("%s: OSD returned 0x%x extents in a single reply!\n",
5844*f628d799SJeff Layton 					     __func__, count);
5845*f628d799SJeff Layton 					return -EREMOTEIO;
5846*f628d799SJeff Layton 				}
5847*f628d799SJeff Layton 
5848*f628d799SJeff Layton 				/* no extent array provided, or too short */
5849*f628d799SJeff Layton 				kfree(sr->sr_extent);
5850*f628d799SJeff Layton 				sr->sr_extent = kmalloc_array(count,
5851*f628d799SJeff Layton 							      sizeof(*sr->sr_extent),
5852*f628d799SJeff Layton 							      GFP_NOIO);
5853*f628d799SJeff Layton 				if (!sr->sr_extent)
5854*f628d799SJeff Layton 					return -ENOMEM;
5855*f628d799SJeff Layton 				sr->sr_ext_len = count;
5856*f628d799SJeff Layton 			}
5857*f628d799SJeff Layton 			ret = count * sizeof(*sr->sr_extent);
5858*f628d799SJeff Layton 			*pbuf = (char *)sr->sr_extent;
5859*f628d799SJeff Layton 			sr->sr_state = CEPH_SPARSE_READ_DATA_LEN;
5860*f628d799SJeff Layton 			break;
5861*f628d799SJeff Layton 		}
5862*f628d799SJeff Layton 		/* No extents? Read data len */
5863*f628d799SJeff Layton 		fallthrough;
5864*f628d799SJeff Layton 	case CEPH_SPARSE_READ_DATA_LEN:
5865*f628d799SJeff Layton 		convert_extent_map(sr);
5866*f628d799SJeff Layton 		ret = sizeof(sr->sr_datalen);
5867*f628d799SJeff Layton 		*pbuf = (char *)&sr->sr_datalen;
5868*f628d799SJeff Layton 		sr->sr_state = CEPH_SPARSE_READ_DATA;
5869*f628d799SJeff Layton 		break;
5870*f628d799SJeff Layton 	case CEPH_SPARSE_READ_DATA:
5871*f628d799SJeff Layton 		if (sr->sr_index >= count) {
5872*f628d799SJeff Layton 			sr->sr_state = CEPH_SPARSE_READ_HDR;
5873*f628d799SJeff Layton 			goto next_op;
5874*f628d799SJeff Layton 		}
5875*f628d799SJeff Layton 
5876*f628d799SJeff Layton 		eoff = sr->sr_extent[sr->sr_index].off;
5877*f628d799SJeff Layton 		elen = sr->sr_extent[sr->sr_index].len;
5878*f628d799SJeff Layton 
5879*f628d799SJeff Layton 		dout("[%d] ext %d off 0x%llx len 0x%llx\n",
5880*f628d799SJeff Layton 		     o->o_osd, sr->sr_index, eoff, elen);
5881*f628d799SJeff Layton 
5882*f628d799SJeff Layton 		if (elen > INT_MAX) {
5883*f628d799SJeff Layton 			dout("Sparse read extent length too long (0x%llx)\n",
5884*f628d799SJeff Layton 			     elen);
5885*f628d799SJeff Layton 			return -EREMOTEIO;
5886*f628d799SJeff Layton 		}
5887*f628d799SJeff Layton 
5888*f628d799SJeff Layton 		/* zero out anything from sr_pos to start of extent */
5889*f628d799SJeff Layton 		if (sr->sr_pos < eoff)
5890*f628d799SJeff Layton 			advance_cursor(cursor, eoff - sr->sr_pos, true);
5891*f628d799SJeff Layton 
5892*f628d799SJeff Layton 		/* Set position to end of extent */
5893*f628d799SJeff Layton 		sr->sr_pos = eoff + elen;
5894*f628d799SJeff Layton 
5895*f628d799SJeff Layton 		/* send back the new length and nullify the ptr */
5896*f628d799SJeff Layton 		cursor->sr_resid = elen;
5897*f628d799SJeff Layton 		ret = elen;
5898*f628d799SJeff Layton 		*pbuf = NULL;
5899*f628d799SJeff Layton 
5900*f628d799SJeff Layton 		/* Bump the array index */
5901*f628d799SJeff Layton 		++sr->sr_index;
5902*f628d799SJeff Layton 		break;
5903*f628d799SJeff Layton 	}
5904*f628d799SJeff Layton 	return ret;
5905*f628d799SJeff Layton }
5906*f628d799SJeff Layton 
59073d14c5d2SYehuda Sadeh static const struct ceph_connection_operations osd_con_ops = {
59084972cf60SIlya Dryomov 	.get = osd_get_con,
59094972cf60SIlya Dryomov 	.put = osd_put_con,
5910*f628d799SJeff Layton 	.sparse_read = osd_sparse_read,
59114972cf60SIlya Dryomov 	.alloc_msg = osd_alloc_msg,
59124972cf60SIlya Dryomov 	.dispatch = osd_dispatch,
59134972cf60SIlya Dryomov 	.fault = osd_fault,
59148cb441c0SIlya Dryomov 	.reencode_message = osd_reencode_message,
59154972cf60SIlya Dryomov 	.get_authorizer = osd_get_authorizer,
59164972cf60SIlya Dryomov 	.add_authorizer_challenge = osd_add_authorizer_challenge,
59174972cf60SIlya Dryomov 	.verify_authorizer_reply = osd_verify_authorizer_reply,
59184972cf60SIlya Dryomov 	.invalidate_authorizer = osd_invalidate_authorizer,
591979dbd1baSIlya Dryomov 	.sign_message = osd_sign_message,
592079dbd1baSIlya Dryomov 	.check_message_signature = osd_check_message_signature,
5921cd1a677cSIlya Dryomov 	.get_auth_request = osd_get_auth_request,
5922cd1a677cSIlya Dryomov 	.handle_auth_reply_more = osd_handle_auth_reply_more,
5923cd1a677cSIlya Dryomov 	.handle_auth_done = osd_handle_auth_done,
5924cd1a677cSIlya Dryomov 	.handle_auth_bad_method = osd_handle_auth_bad_method,
59253d14c5d2SYehuda Sadeh };
5926