xref: /openbmc/linux/net/ceph/osd_client.c (revision 2cef0ba8)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a4ce40a9SAlex Elder 
33d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h>
43d14c5d2SYehuda Sadeh 
53d14c5d2SYehuda Sadeh #include <linux/module.h>
63d14c5d2SYehuda Sadeh #include <linux/err.h>
73d14c5d2SYehuda Sadeh #include <linux/highmem.h>
83d14c5d2SYehuda Sadeh #include <linux/mm.h>
93d14c5d2SYehuda Sadeh #include <linux/pagemap.h>
103d14c5d2SYehuda Sadeh #include <linux/slab.h>
113d14c5d2SYehuda Sadeh #include <linux/uaccess.h>
123d14c5d2SYehuda Sadeh #ifdef CONFIG_BLOCK
133d14c5d2SYehuda Sadeh #include <linux/bio.h>
143d14c5d2SYehuda Sadeh #endif
153d14c5d2SYehuda Sadeh 
168cb441c0SIlya Dryomov #include <linux/ceph/ceph_features.h>
173d14c5d2SYehuda Sadeh #include <linux/ceph/libceph.h>
183d14c5d2SYehuda Sadeh #include <linux/ceph/osd_client.h>
193d14c5d2SYehuda Sadeh #include <linux/ceph/messenger.h>
203d14c5d2SYehuda Sadeh #include <linux/ceph/decode.h>
213d14c5d2SYehuda Sadeh #include <linux/ceph/auth.h>
223d14c5d2SYehuda Sadeh #include <linux/ceph/pagelist.h>
2308c1ac50SIlya Dryomov #include <linux/ceph/striper.h>
243d14c5d2SYehuda Sadeh 
253d14c5d2SYehuda Sadeh #define OSD_OPREPLY_FRONT_LEN	512
263d14c5d2SYehuda Sadeh 
275522ae0bSAlex Elder static struct kmem_cache	*ceph_osd_request_cache;
285522ae0bSAlex Elder 
293d14c5d2SYehuda Sadeh static const struct ceph_connection_operations osd_con_ops;
303d14c5d2SYehuda Sadeh 
313d14c5d2SYehuda Sadeh /*
323d14c5d2SYehuda Sadeh  * Implement client access to distributed object storage cluster.
333d14c5d2SYehuda Sadeh  *
343d14c5d2SYehuda Sadeh  * All data objects are stored within a cluster/cloud of OSDs, or
353d14c5d2SYehuda Sadeh  * "object storage devices."  (Note that Ceph OSDs have _nothing_ to
363d14c5d2SYehuda Sadeh  * do with the T10 OSD extensions to SCSI.)  Ceph OSDs are simply
373d14c5d2SYehuda Sadeh  * remote daemons serving up and coordinating consistent and safe
383d14c5d2SYehuda Sadeh  * access to storage.
393d14c5d2SYehuda Sadeh  *
403d14c5d2SYehuda Sadeh  * Cluster membership and the mapping of data objects onto storage devices
413d14c5d2SYehuda Sadeh  * are described by the osd map.
423d14c5d2SYehuda Sadeh  *
433d14c5d2SYehuda Sadeh  * We keep track of pending OSD requests (read, write), resubmit
443d14c5d2SYehuda Sadeh  * requests to different OSDs when the cluster topology/data layout
453d14c5d2SYehuda Sadeh  * change, or retry the affected requests when the communications
463d14c5d2SYehuda Sadeh  * channel with an OSD is reset.
473d14c5d2SYehuda Sadeh  */
483d14c5d2SYehuda Sadeh 
495aea3dcdSIlya Dryomov static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req);
505aea3dcdSIlya Dryomov static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req);
51922dab61SIlya Dryomov static void link_linger(struct ceph_osd *osd,
52922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq);
53922dab61SIlya Dryomov static void unlink_linger(struct ceph_osd *osd,
54922dab61SIlya Dryomov 			  struct ceph_osd_linger_request *lreq);
55a02a946dSIlya Dryomov static void clear_backoffs(struct ceph_osd *osd);
565aea3dcdSIlya Dryomov 
575aea3dcdSIlya Dryomov #if 1
585aea3dcdSIlya Dryomov static inline bool rwsem_is_wrlocked(struct rw_semaphore *sem)
595aea3dcdSIlya Dryomov {
605aea3dcdSIlya Dryomov 	bool wrlocked = true;
615aea3dcdSIlya Dryomov 
625aea3dcdSIlya Dryomov 	if (unlikely(down_read_trylock(sem))) {
635aea3dcdSIlya Dryomov 		wrlocked = false;
645aea3dcdSIlya Dryomov 		up_read(sem);
655aea3dcdSIlya Dryomov 	}
665aea3dcdSIlya Dryomov 
675aea3dcdSIlya Dryomov 	return wrlocked;
685aea3dcdSIlya Dryomov }
695aea3dcdSIlya Dryomov static inline void verify_osdc_locked(struct ceph_osd_client *osdc)
705aea3dcdSIlya Dryomov {
715aea3dcdSIlya Dryomov 	WARN_ON(!rwsem_is_locked(&osdc->lock));
725aea3dcdSIlya Dryomov }
735aea3dcdSIlya Dryomov static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc)
745aea3dcdSIlya Dryomov {
755aea3dcdSIlya Dryomov 	WARN_ON(!rwsem_is_wrlocked(&osdc->lock));
765aea3dcdSIlya Dryomov }
775aea3dcdSIlya Dryomov static inline void verify_osd_locked(struct ceph_osd *osd)
785aea3dcdSIlya Dryomov {
795aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
805aea3dcdSIlya Dryomov 
815aea3dcdSIlya Dryomov 	WARN_ON(!(mutex_is_locked(&osd->lock) &&
825aea3dcdSIlya Dryomov 		  rwsem_is_locked(&osdc->lock)) &&
835aea3dcdSIlya Dryomov 		!rwsem_is_wrlocked(&osdc->lock));
845aea3dcdSIlya Dryomov }
85922dab61SIlya Dryomov static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq)
86922dab61SIlya Dryomov {
87922dab61SIlya Dryomov 	WARN_ON(!mutex_is_locked(&lreq->lock));
88922dab61SIlya Dryomov }
895aea3dcdSIlya Dryomov #else
905aea3dcdSIlya Dryomov static inline void verify_osdc_locked(struct ceph_osd_client *osdc) { }
915aea3dcdSIlya Dryomov static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) { }
925aea3dcdSIlya Dryomov static inline void verify_osd_locked(struct ceph_osd *osd) { }
93922dab61SIlya Dryomov static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) { }
945aea3dcdSIlya Dryomov #endif
955aea3dcdSIlya Dryomov 
963d14c5d2SYehuda Sadeh /*
973d14c5d2SYehuda Sadeh  * calculate the mapping of a file extent onto an object, and fill out the
983d14c5d2SYehuda Sadeh  * request accordingly.  shorten extent as necessary if it crosses an
993d14c5d2SYehuda Sadeh  * object boundary.
1003d14c5d2SYehuda Sadeh  *
1013d14c5d2SYehuda Sadeh  * fill osd op in request message.
1023d14c5d2SYehuda Sadeh  */
103dbe0fc41SAlex Elder static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
104a19dadfbSAlex Elder 			u64 *objnum, u64 *objoff, u64 *objlen)
1053d14c5d2SYehuda Sadeh {
10660e56f13SAlex Elder 	u64 orig_len = *plen;
107dccbf080SIlya Dryomov 	u32 xlen;
1083d14c5d2SYehuda Sadeh 
10960e56f13SAlex Elder 	/* object extent? */
110dccbf080SIlya Dryomov 	ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
111dccbf080SIlya Dryomov 					  objoff, &xlen);
112dccbf080SIlya Dryomov 	*objlen = xlen;
11375d1c941SAlex Elder 	if (*objlen < orig_len) {
11475d1c941SAlex Elder 		*plen = *objlen;
11560e56f13SAlex Elder 		dout(" skipping last %llu, final file extent %llu~%llu\n",
11660e56f13SAlex Elder 		     orig_len - *plen, off, *plen);
11760e56f13SAlex Elder 	}
11860e56f13SAlex Elder 
11975d1c941SAlex Elder 	dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
1203ff5f385SAlex Elder 	return 0;
1213d14c5d2SYehuda Sadeh }
1223d14c5d2SYehuda Sadeh 
123c54d47bfSAlex Elder static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
124c54d47bfSAlex Elder {
125c54d47bfSAlex Elder 	memset(osd_data, 0, sizeof (*osd_data));
126c54d47bfSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
127c54d47bfSAlex Elder }
128c54d47bfSAlex Elder 
12989486833SIlya Dryomov /*
13089486833SIlya Dryomov  * Consumes @pages if @own_pages is true.
13189486833SIlya Dryomov  */
132a4ce40a9SAlex Elder static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
13343bfe5deSAlex Elder 			struct page **pages, u64 length, u32 alignment,
13443bfe5deSAlex Elder 			bool pages_from_pool, bool own_pages)
13543bfe5deSAlex Elder {
13643bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
13743bfe5deSAlex Elder 	osd_data->pages = pages;
13843bfe5deSAlex Elder 	osd_data->length = length;
13943bfe5deSAlex Elder 	osd_data->alignment = alignment;
14043bfe5deSAlex Elder 	osd_data->pages_from_pool = pages_from_pool;
14143bfe5deSAlex Elder 	osd_data->own_pages = own_pages;
14243bfe5deSAlex Elder }
14343bfe5deSAlex Elder 
14489486833SIlya Dryomov /*
14589486833SIlya Dryomov  * Consumes a ref on @pagelist.
14689486833SIlya Dryomov  */
147a4ce40a9SAlex Elder static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
14843bfe5deSAlex Elder 			struct ceph_pagelist *pagelist)
14943bfe5deSAlex Elder {
15043bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
15143bfe5deSAlex Elder 	osd_data->pagelist = pagelist;
15243bfe5deSAlex Elder }
15343bfe5deSAlex Elder 
15443bfe5deSAlex Elder #ifdef CONFIG_BLOCK
155a4ce40a9SAlex Elder static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
1565359a17dSIlya Dryomov 				   struct ceph_bio_iter *bio_pos,
1575359a17dSIlya Dryomov 				   u32 bio_length)
15843bfe5deSAlex Elder {
15943bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
1605359a17dSIlya Dryomov 	osd_data->bio_pos = *bio_pos;
16143bfe5deSAlex Elder 	osd_data->bio_length = bio_length;
16243bfe5deSAlex Elder }
16343bfe5deSAlex Elder #endif /* CONFIG_BLOCK */
16443bfe5deSAlex Elder 
165b9e281c2SIlya Dryomov static void ceph_osd_data_bvecs_init(struct ceph_osd_data *osd_data,
1660010f705SIlya Dryomov 				     struct ceph_bvec_iter *bvec_pos,
1670010f705SIlya Dryomov 				     u32 num_bvecs)
168b9e281c2SIlya Dryomov {
169b9e281c2SIlya Dryomov 	osd_data->type = CEPH_OSD_DATA_TYPE_BVECS;
170b9e281c2SIlya Dryomov 	osd_data->bvec_pos = *bvec_pos;
1710010f705SIlya Dryomov 	osd_data->num_bvecs = num_bvecs;
172b9e281c2SIlya Dryomov }
173b9e281c2SIlya Dryomov 
17449719778SAlex Elder static struct ceph_osd_data *
17549719778SAlex Elder osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
17649719778SAlex Elder {
17749719778SAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
17849719778SAlex Elder 
17949719778SAlex Elder 	return &osd_req->r_ops[which].raw_data_in;
18049719778SAlex Elder }
18149719778SAlex Elder 
182a4ce40a9SAlex Elder struct ceph_osd_data *
183a4ce40a9SAlex Elder osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
184406e2c9fSAlex Elder 			unsigned int which)
185a4ce40a9SAlex Elder {
186863c7eb5SAlex Elder 	return osd_req_op_data(osd_req, which, extent, osd_data);
187a4ce40a9SAlex Elder }
188a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data);
189a4ce40a9SAlex Elder 
19049719778SAlex Elder void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
19149719778SAlex Elder 			unsigned int which, struct page **pages,
19249719778SAlex Elder 			u64 length, u32 alignment,
19349719778SAlex Elder 			bool pages_from_pool, bool own_pages)
19449719778SAlex Elder {
19549719778SAlex Elder 	struct ceph_osd_data *osd_data;
19649719778SAlex Elder 
19749719778SAlex Elder 	osd_data = osd_req_op_raw_data_in(osd_req, which);
19849719778SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
19949719778SAlex Elder 				pages_from_pool, own_pages);
20049719778SAlex Elder }
20149719778SAlex Elder EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
20249719778SAlex Elder 
203a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
204406e2c9fSAlex Elder 			unsigned int which, struct page **pages,
205406e2c9fSAlex Elder 			u64 length, u32 alignment,
206a4ce40a9SAlex Elder 			bool pages_from_pool, bool own_pages)
207a4ce40a9SAlex Elder {
208a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
209a4ce40a9SAlex Elder 
210863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
211a4ce40a9SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
212a4ce40a9SAlex Elder 				pages_from_pool, own_pages);
213a4ce40a9SAlex Elder }
214a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
215a4ce40a9SAlex Elder 
216a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
217406e2c9fSAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
218a4ce40a9SAlex Elder {
219a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
220a4ce40a9SAlex Elder 
221863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
222a4ce40a9SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
223a4ce40a9SAlex Elder }
224a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
225a4ce40a9SAlex Elder 
226a4ce40a9SAlex Elder #ifdef CONFIG_BLOCK
227a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
2285359a17dSIlya Dryomov 				    unsigned int which,
2295359a17dSIlya Dryomov 				    struct ceph_bio_iter *bio_pos,
2305359a17dSIlya Dryomov 				    u32 bio_length)
231a4ce40a9SAlex Elder {
232a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
233863c7eb5SAlex Elder 
234863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
2355359a17dSIlya Dryomov 	ceph_osd_data_bio_init(osd_data, bio_pos, bio_length);
236a4ce40a9SAlex Elder }
237a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
238a4ce40a9SAlex Elder #endif /* CONFIG_BLOCK */
239a4ce40a9SAlex Elder 
2400010f705SIlya Dryomov void osd_req_op_extent_osd_data_bvecs(struct ceph_osd_request *osd_req,
2410010f705SIlya Dryomov 				      unsigned int which,
2420010f705SIlya Dryomov 				      struct bio_vec *bvecs, u32 num_bvecs,
2430010f705SIlya Dryomov 				      u32 bytes)
2440010f705SIlya Dryomov {
2450010f705SIlya Dryomov 	struct ceph_osd_data *osd_data;
2460010f705SIlya Dryomov 	struct ceph_bvec_iter it = {
2470010f705SIlya Dryomov 		.bvecs = bvecs,
2480010f705SIlya Dryomov 		.iter = { .bi_size = bytes },
2490010f705SIlya Dryomov 	};
2500010f705SIlya Dryomov 
2510010f705SIlya Dryomov 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
2520010f705SIlya Dryomov 	ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
2530010f705SIlya Dryomov }
2540010f705SIlya Dryomov EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvecs);
2550010f705SIlya Dryomov 
256b9e281c2SIlya Dryomov void osd_req_op_extent_osd_data_bvec_pos(struct ceph_osd_request *osd_req,
257b9e281c2SIlya Dryomov 					 unsigned int which,
258b9e281c2SIlya Dryomov 					 struct ceph_bvec_iter *bvec_pos)
259b9e281c2SIlya Dryomov {
260b9e281c2SIlya Dryomov 	struct ceph_osd_data *osd_data;
261b9e281c2SIlya Dryomov 
262b9e281c2SIlya Dryomov 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
2630010f705SIlya Dryomov 	ceph_osd_data_bvecs_init(osd_data, bvec_pos, 0);
264b9e281c2SIlya Dryomov }
265b9e281c2SIlya Dryomov EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvec_pos);
266b9e281c2SIlya Dryomov 
267a4ce40a9SAlex Elder static void osd_req_op_cls_request_info_pagelist(
268a4ce40a9SAlex Elder 			struct ceph_osd_request *osd_req,
269a4ce40a9SAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
270a4ce40a9SAlex Elder {
271a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
272a4ce40a9SAlex Elder 
273863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_info);
274a4ce40a9SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
275a4ce40a9SAlex Elder }
276a4ce40a9SAlex Elder 
27704017e29SAlex Elder void osd_req_op_cls_request_data_pagelist(
27804017e29SAlex Elder 			struct ceph_osd_request *osd_req,
27904017e29SAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
28004017e29SAlex Elder {
28104017e29SAlex Elder 	struct ceph_osd_data *osd_data;
28204017e29SAlex Elder 
283863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
28404017e29SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
285bb873b53SIlya Dryomov 	osd_req->r_ops[which].cls.indata_len += pagelist->length;
286bb873b53SIlya Dryomov 	osd_req->r_ops[which].indata_len += pagelist->length;
28704017e29SAlex Elder }
28804017e29SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
28904017e29SAlex Elder 
2906c57b554SAlex Elder void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
2916c57b554SAlex Elder 			unsigned int which, struct page **pages, u64 length,
2926c57b554SAlex Elder 			u32 alignment, bool pages_from_pool, bool own_pages)
2936c57b554SAlex Elder {
2946c57b554SAlex Elder 	struct ceph_osd_data *osd_data;
2956c57b554SAlex Elder 
2966c57b554SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
2976c57b554SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
2986c57b554SAlex Elder 				pages_from_pool, own_pages);
299bb873b53SIlya Dryomov 	osd_req->r_ops[which].cls.indata_len += length;
300bb873b53SIlya Dryomov 	osd_req->r_ops[which].indata_len += length;
3016c57b554SAlex Elder }
3026c57b554SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
3036c57b554SAlex Elder 
304b9e281c2SIlya Dryomov void osd_req_op_cls_request_data_bvecs(struct ceph_osd_request *osd_req,
305b9e281c2SIlya Dryomov 				       unsigned int which,
3060010f705SIlya Dryomov 				       struct bio_vec *bvecs, u32 num_bvecs,
3070010f705SIlya Dryomov 				       u32 bytes)
308b9e281c2SIlya Dryomov {
309b9e281c2SIlya Dryomov 	struct ceph_osd_data *osd_data;
310b9e281c2SIlya Dryomov 	struct ceph_bvec_iter it = {
311b9e281c2SIlya Dryomov 		.bvecs = bvecs,
312b9e281c2SIlya Dryomov 		.iter = { .bi_size = bytes },
313b9e281c2SIlya Dryomov 	};
314b9e281c2SIlya Dryomov 
315b9e281c2SIlya Dryomov 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
3160010f705SIlya Dryomov 	ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
317b9e281c2SIlya Dryomov 	osd_req->r_ops[which].cls.indata_len += bytes;
318b9e281c2SIlya Dryomov 	osd_req->r_ops[which].indata_len += bytes;
319b9e281c2SIlya Dryomov }
320b9e281c2SIlya Dryomov EXPORT_SYMBOL(osd_req_op_cls_request_data_bvecs);
321b9e281c2SIlya Dryomov 
322a4ce40a9SAlex Elder void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
323a4ce40a9SAlex Elder 			unsigned int which, struct page **pages, u64 length,
324a4ce40a9SAlex Elder 			u32 alignment, bool pages_from_pool, bool own_pages)
325a4ce40a9SAlex Elder {
326a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
327a4ce40a9SAlex Elder 
328863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, response_data);
329a4ce40a9SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
330a4ce40a9SAlex Elder 				pages_from_pool, own_pages);
331a4ce40a9SAlex Elder }
332a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
333a4ce40a9SAlex Elder 
33423c08a9cSAlex Elder static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
33523c08a9cSAlex Elder {
33623c08a9cSAlex Elder 	switch (osd_data->type) {
33723c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_NONE:
33823c08a9cSAlex Elder 		return 0;
33923c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_PAGES:
34023c08a9cSAlex Elder 		return osd_data->length;
34123c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_PAGELIST:
34223c08a9cSAlex Elder 		return (u64)osd_data->pagelist->length;
34323c08a9cSAlex Elder #ifdef CONFIG_BLOCK
34423c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_BIO:
34523c08a9cSAlex Elder 		return (u64)osd_data->bio_length;
34623c08a9cSAlex Elder #endif /* CONFIG_BLOCK */
347b9e281c2SIlya Dryomov 	case CEPH_OSD_DATA_TYPE_BVECS:
348b9e281c2SIlya Dryomov 		return osd_data->bvec_pos.iter.bi_size;
34923c08a9cSAlex Elder 	default:
35023c08a9cSAlex Elder 		WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
35123c08a9cSAlex Elder 		return 0;
35223c08a9cSAlex Elder 	}
35323c08a9cSAlex Elder }
35423c08a9cSAlex Elder 
355c54d47bfSAlex Elder static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
356c54d47bfSAlex Elder {
3575476492fSAlex Elder 	if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
358c54d47bfSAlex Elder 		int num_pages;
359c54d47bfSAlex Elder 
360c54d47bfSAlex Elder 		num_pages = calc_pages_for((u64)osd_data->alignment,
361c54d47bfSAlex Elder 						(u64)osd_data->length);
362c54d47bfSAlex Elder 		ceph_release_page_vector(osd_data->pages, num_pages);
36389486833SIlya Dryomov 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
36489486833SIlya Dryomov 		ceph_pagelist_release(osd_data->pagelist);
365c54d47bfSAlex Elder 	}
3665476492fSAlex Elder 	ceph_osd_data_init(osd_data);
3675476492fSAlex Elder }
3685476492fSAlex Elder 
3695476492fSAlex Elder static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
3705476492fSAlex Elder 			unsigned int which)
3715476492fSAlex Elder {
3725476492fSAlex Elder 	struct ceph_osd_req_op *op;
3735476492fSAlex Elder 
3745476492fSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
3755476492fSAlex Elder 	op = &osd_req->r_ops[which];
3765476492fSAlex Elder 
3775476492fSAlex Elder 	switch (op->op) {
3785476492fSAlex Elder 	case CEPH_OSD_OP_READ:
3795476492fSAlex Elder 	case CEPH_OSD_OP_WRITE:
380e30b7577SIlya Dryomov 	case CEPH_OSD_OP_WRITEFULL:
3815476492fSAlex Elder 		ceph_osd_data_release(&op->extent.osd_data);
3825476492fSAlex Elder 		break;
3835476492fSAlex Elder 	case CEPH_OSD_OP_CALL:
3845476492fSAlex Elder 		ceph_osd_data_release(&op->cls.request_info);
38504017e29SAlex Elder 		ceph_osd_data_release(&op->cls.request_data);
3865476492fSAlex Elder 		ceph_osd_data_release(&op->cls.response_data);
3875476492fSAlex Elder 		break;
388d74b50beSYan, Zheng 	case CEPH_OSD_OP_SETXATTR:
389d74b50beSYan, Zheng 	case CEPH_OSD_OP_CMPXATTR:
390d74b50beSYan, Zheng 		ceph_osd_data_release(&op->xattr.osd_data);
391d74b50beSYan, Zheng 		break;
39266ba609fSYan, Zheng 	case CEPH_OSD_OP_STAT:
39366ba609fSYan, Zheng 		ceph_osd_data_release(&op->raw_data_in);
39466ba609fSYan, Zheng 		break;
395922dab61SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY_ACK:
396922dab61SIlya Dryomov 		ceph_osd_data_release(&op->notify_ack.request_data);
397922dab61SIlya Dryomov 		break;
39819079203SIlya Dryomov 	case CEPH_OSD_OP_NOTIFY:
39919079203SIlya Dryomov 		ceph_osd_data_release(&op->notify.request_data);
40019079203SIlya Dryomov 		ceph_osd_data_release(&op->notify.response_data);
40119079203SIlya Dryomov 		break;
402a4ed38d7SDouglas Fuller 	case CEPH_OSD_OP_LIST_WATCHERS:
403a4ed38d7SDouglas Fuller 		ceph_osd_data_release(&op->list_watchers.response_data);
404a4ed38d7SDouglas Fuller 		break;
40523ddf9beSLuis Henriques 	case CEPH_OSD_OP_COPY_FROM:
40623ddf9beSLuis Henriques 		ceph_osd_data_release(&op->copy_from.osd_data);
40723ddf9beSLuis Henriques 		break;
4085476492fSAlex Elder 	default:
4095476492fSAlex Elder 		break;
4105476492fSAlex Elder 	}
411c54d47bfSAlex Elder }
412c54d47bfSAlex Elder 
4133d14c5d2SYehuda Sadeh /*
41463244fa1SIlya Dryomov  * Assumes @t is zero-initialized.
41563244fa1SIlya Dryomov  */
41663244fa1SIlya Dryomov static void target_init(struct ceph_osd_request_target *t)
41763244fa1SIlya Dryomov {
41863244fa1SIlya Dryomov 	ceph_oid_init(&t->base_oid);
41963244fa1SIlya Dryomov 	ceph_oloc_init(&t->base_oloc);
42063244fa1SIlya Dryomov 	ceph_oid_init(&t->target_oid);
42163244fa1SIlya Dryomov 	ceph_oloc_init(&t->target_oloc);
42263244fa1SIlya Dryomov 
42363244fa1SIlya Dryomov 	ceph_osds_init(&t->acting);
42463244fa1SIlya Dryomov 	ceph_osds_init(&t->up);
42563244fa1SIlya Dryomov 	t->size = -1;
42663244fa1SIlya Dryomov 	t->min_size = -1;
42763244fa1SIlya Dryomov 
42863244fa1SIlya Dryomov 	t->osd = CEPH_HOMELESS_OSD;
42963244fa1SIlya Dryomov }
43063244fa1SIlya Dryomov 
431922dab61SIlya Dryomov static void target_copy(struct ceph_osd_request_target *dest,
432922dab61SIlya Dryomov 			const struct ceph_osd_request_target *src)
433922dab61SIlya Dryomov {
434922dab61SIlya Dryomov 	ceph_oid_copy(&dest->base_oid, &src->base_oid);
435922dab61SIlya Dryomov 	ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
436922dab61SIlya Dryomov 	ceph_oid_copy(&dest->target_oid, &src->target_oid);
437922dab61SIlya Dryomov 	ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
438922dab61SIlya Dryomov 
439922dab61SIlya Dryomov 	dest->pgid = src->pgid; /* struct */
440dc98ff72SIlya Dryomov 	dest->spgid = src->spgid; /* struct */
441922dab61SIlya Dryomov 	dest->pg_num = src->pg_num;
442922dab61SIlya Dryomov 	dest->pg_num_mask = src->pg_num_mask;
443922dab61SIlya Dryomov 	ceph_osds_copy(&dest->acting, &src->acting);
444922dab61SIlya Dryomov 	ceph_osds_copy(&dest->up, &src->up);
445922dab61SIlya Dryomov 	dest->size = src->size;
446922dab61SIlya Dryomov 	dest->min_size = src->min_size;
447922dab61SIlya Dryomov 	dest->sort_bitwise = src->sort_bitwise;
448922dab61SIlya Dryomov 
449922dab61SIlya Dryomov 	dest->flags = src->flags;
450922dab61SIlya Dryomov 	dest->paused = src->paused;
451922dab61SIlya Dryomov 
45204c7d789SIlya Dryomov 	dest->epoch = src->epoch;
453dc93e0e2SIlya Dryomov 	dest->last_force_resend = src->last_force_resend;
454dc93e0e2SIlya Dryomov 
455922dab61SIlya Dryomov 	dest->osd = src->osd;
456922dab61SIlya Dryomov }
457922dab61SIlya Dryomov 
45863244fa1SIlya Dryomov static void target_destroy(struct ceph_osd_request_target *t)
45963244fa1SIlya Dryomov {
46063244fa1SIlya Dryomov 	ceph_oid_destroy(&t->base_oid);
46130c156d9SYan, Zheng 	ceph_oloc_destroy(&t->base_oloc);
46263244fa1SIlya Dryomov 	ceph_oid_destroy(&t->target_oid);
46330c156d9SYan, Zheng 	ceph_oloc_destroy(&t->target_oloc);
46463244fa1SIlya Dryomov }
46563244fa1SIlya Dryomov 
46663244fa1SIlya Dryomov /*
4673d14c5d2SYehuda Sadeh  * requests
4683d14c5d2SYehuda Sadeh  */
4693540bfdbSIlya Dryomov static void request_release_checks(struct ceph_osd_request *req)
4703540bfdbSIlya Dryomov {
4713540bfdbSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&req->r_node));
4724609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
47394e85771SIlya Dryomov 	WARN_ON(!list_empty(&req->r_private_item));
4743540bfdbSIlya Dryomov 	WARN_ON(req->r_osd);
4753540bfdbSIlya Dryomov }
4763540bfdbSIlya Dryomov 
4779e94af20SIlya Dryomov static void ceph_osdc_release_request(struct kref *kref)
4783d14c5d2SYehuda Sadeh {
4799e94af20SIlya Dryomov 	struct ceph_osd_request *req = container_of(kref,
4809e94af20SIlya Dryomov 					    struct ceph_osd_request, r_kref);
4815476492fSAlex Elder 	unsigned int which;
4823d14c5d2SYehuda Sadeh 
4839e94af20SIlya Dryomov 	dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
4849e94af20SIlya Dryomov 	     req->r_request, req->r_reply);
4853540bfdbSIlya Dryomov 	request_release_checks(req);
4869e94af20SIlya Dryomov 
4873d14c5d2SYehuda Sadeh 	if (req->r_request)
4883d14c5d2SYehuda Sadeh 		ceph_msg_put(req->r_request);
4895aea3dcdSIlya Dryomov 	if (req->r_reply)
490ab8cb34aSAlex Elder 		ceph_msg_put(req->r_reply);
4910fff87ecSAlex Elder 
4925476492fSAlex Elder 	for (which = 0; which < req->r_num_ops; which++)
4935476492fSAlex Elder 		osd_req_op_data_release(req, which);
4940fff87ecSAlex Elder 
495a66dd383SIlya Dryomov 	target_destroy(&req->r_t);
4963d14c5d2SYehuda Sadeh 	ceph_put_snap_context(req->r_snapc);
497d30291b9SIlya Dryomov 
4983d14c5d2SYehuda Sadeh 	if (req->r_mempool)
4993d14c5d2SYehuda Sadeh 		mempool_free(req, req->r_osdc->req_mempool);
5003f1af42aSIlya Dryomov 	else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
5015522ae0bSAlex Elder 		kmem_cache_free(ceph_osd_request_cache, req);
5023f1af42aSIlya Dryomov 	else
5033f1af42aSIlya Dryomov 		kfree(req);
5043d14c5d2SYehuda Sadeh }
5059e94af20SIlya Dryomov 
5069e94af20SIlya Dryomov void ceph_osdc_get_request(struct ceph_osd_request *req)
5079e94af20SIlya Dryomov {
5089e94af20SIlya Dryomov 	dout("%s %p (was %d)\n", __func__, req,
5092c935bc5SPeter Zijlstra 	     kref_read(&req->r_kref));
5109e94af20SIlya Dryomov 	kref_get(&req->r_kref);
5119e94af20SIlya Dryomov }
5129e94af20SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_get_request);
5139e94af20SIlya Dryomov 
5149e94af20SIlya Dryomov void ceph_osdc_put_request(struct ceph_osd_request *req)
5159e94af20SIlya Dryomov {
5163ed97d63SIlya Dryomov 	if (req) {
5179e94af20SIlya Dryomov 		dout("%s %p (was %d)\n", __func__, req,
5182c935bc5SPeter Zijlstra 		     kref_read(&req->r_kref));
5199e94af20SIlya Dryomov 		kref_put(&req->r_kref, ceph_osdc_release_request);
5209e94af20SIlya Dryomov 	}
5213ed97d63SIlya Dryomov }
5229e94af20SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_put_request);
5233d14c5d2SYehuda Sadeh 
5243540bfdbSIlya Dryomov static void request_init(struct ceph_osd_request *req)
5253540bfdbSIlya Dryomov {
5263540bfdbSIlya Dryomov 	/* req only, each op is zeroed in _osd_req_op_init() */
5273540bfdbSIlya Dryomov 	memset(req, 0, sizeof(*req));
5283540bfdbSIlya Dryomov 
5293540bfdbSIlya Dryomov 	kref_init(&req->r_kref);
5303540bfdbSIlya Dryomov 	init_completion(&req->r_completion);
5313540bfdbSIlya Dryomov 	RB_CLEAR_NODE(&req->r_node);
5324609245eSIlya Dryomov 	RB_CLEAR_NODE(&req->r_mc_node);
53394e85771SIlya Dryomov 	INIT_LIST_HEAD(&req->r_private_item);
5343540bfdbSIlya Dryomov 
5353540bfdbSIlya Dryomov 	target_init(&req->r_t);
5363540bfdbSIlya Dryomov }
5373540bfdbSIlya Dryomov 
538922dab61SIlya Dryomov /*
539922dab61SIlya Dryomov  * This is ugly, but it allows us to reuse linger registration and ping
540922dab61SIlya Dryomov  * requests, keeping the structure of the code around send_linger{_ping}()
541922dab61SIlya Dryomov  * reasonable.  Setting up a min_nr=2 mempool for each linger request
542922dab61SIlya Dryomov  * and dealing with copying ops (this blasts req only, watch op remains
543922dab61SIlya Dryomov  * intact) isn't any better.
544922dab61SIlya Dryomov  */
545922dab61SIlya Dryomov static void request_reinit(struct ceph_osd_request *req)
546922dab61SIlya Dryomov {
547922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
548922dab61SIlya Dryomov 	bool mempool = req->r_mempool;
549922dab61SIlya Dryomov 	unsigned int num_ops = req->r_num_ops;
550922dab61SIlya Dryomov 	u64 snapid = req->r_snapid;
551922dab61SIlya Dryomov 	struct ceph_snap_context *snapc = req->r_snapc;
552922dab61SIlya Dryomov 	bool linger = req->r_linger;
553922dab61SIlya Dryomov 	struct ceph_msg *request_msg = req->r_request;
554922dab61SIlya Dryomov 	struct ceph_msg *reply_msg = req->r_reply;
555922dab61SIlya Dryomov 
556922dab61SIlya Dryomov 	dout("%s req %p\n", __func__, req);
5572c935bc5SPeter Zijlstra 	WARN_ON(kref_read(&req->r_kref) != 1);
558922dab61SIlya Dryomov 	request_release_checks(req);
559922dab61SIlya Dryomov 
5602c935bc5SPeter Zijlstra 	WARN_ON(kref_read(&request_msg->kref) != 1);
5612c935bc5SPeter Zijlstra 	WARN_ON(kref_read(&reply_msg->kref) != 1);
562922dab61SIlya Dryomov 	target_destroy(&req->r_t);
563922dab61SIlya Dryomov 
564922dab61SIlya Dryomov 	request_init(req);
565922dab61SIlya Dryomov 	req->r_osdc = osdc;
566922dab61SIlya Dryomov 	req->r_mempool = mempool;
567922dab61SIlya Dryomov 	req->r_num_ops = num_ops;
568922dab61SIlya Dryomov 	req->r_snapid = snapid;
569922dab61SIlya Dryomov 	req->r_snapc = snapc;
570922dab61SIlya Dryomov 	req->r_linger = linger;
571922dab61SIlya Dryomov 	req->r_request = request_msg;
572922dab61SIlya Dryomov 	req->r_reply = reply_msg;
573922dab61SIlya Dryomov }
574922dab61SIlya Dryomov 
5753d14c5d2SYehuda Sadeh struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
5763d14c5d2SYehuda Sadeh 					       struct ceph_snap_context *snapc,
5771b83bef2SSage Weil 					       unsigned int num_ops,
5783d14c5d2SYehuda Sadeh 					       bool use_mempool,
57954a54007SAlex Elder 					       gfp_t gfp_flags)
5803d14c5d2SYehuda Sadeh {
5813d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
5823d14c5d2SYehuda Sadeh 
5833d14c5d2SYehuda Sadeh 	if (use_mempool) {
5843f1af42aSIlya Dryomov 		BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
5853d14c5d2SYehuda Sadeh 		req = mempool_alloc(osdc->req_mempool, gfp_flags);
5863f1af42aSIlya Dryomov 	} else if (num_ops <= CEPH_OSD_SLAB_OPS) {
5873f1af42aSIlya Dryomov 		req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
5883d14c5d2SYehuda Sadeh 	} else {
5893f1af42aSIlya Dryomov 		BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
590acafe7e3SKees Cook 		req = kmalloc(struct_size(req, r_ops, num_ops), gfp_flags);
5913d14c5d2SYehuda Sadeh 	}
5923f1af42aSIlya Dryomov 	if (unlikely(!req))
5933d14c5d2SYehuda Sadeh 		return NULL;
5943d14c5d2SYehuda Sadeh 
5953540bfdbSIlya Dryomov 	request_init(req);
5963d14c5d2SYehuda Sadeh 	req->r_osdc = osdc;
5973d14c5d2SYehuda Sadeh 	req->r_mempool = use_mempool;
59879528734SAlex Elder 	req->r_num_ops = num_ops;
59984127282SIlya Dryomov 	req->r_snapid = CEPH_NOSNAP;
60084127282SIlya Dryomov 	req->r_snapc = ceph_get_snap_context(snapc);
6013d14c5d2SYehuda Sadeh 
60213d1ad16SIlya Dryomov 	dout("%s req %p\n", __func__, req);
60313d1ad16SIlya Dryomov 	return req;
6043f1af42aSIlya Dryomov }
60513d1ad16SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_alloc_request);
6063f1af42aSIlya Dryomov 
6072e59ffd1SIlya Dryomov static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc)
60830c156d9SYan, Zheng {
60930c156d9SYan, Zheng 	return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
61030c156d9SYan, Zheng }
61130c156d9SYan, Zheng 
6120d9c1ab3SIlya Dryomov static int __ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp,
6130d9c1ab3SIlya Dryomov 				      int num_request_data_items,
6140d9c1ab3SIlya Dryomov 				      int num_reply_data_items)
61513d1ad16SIlya Dryomov {
61613d1ad16SIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
61713d1ad16SIlya Dryomov 	struct ceph_msg *msg;
61813d1ad16SIlya Dryomov 	int msg_size;
6193d14c5d2SYehuda Sadeh 
6200d9c1ab3SIlya Dryomov 	WARN_ON(req->r_request || req->r_reply);
621d30291b9SIlya Dryomov 	WARN_ON(ceph_oid_empty(&req->r_base_oid));
62230c156d9SYan, Zheng 	WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
623d30291b9SIlya Dryomov 
62413d1ad16SIlya Dryomov 	/* create request message */
6258cb441c0SIlya Dryomov 	msg_size = CEPH_ENCODING_START_BLK_LEN +
6268cb441c0SIlya Dryomov 			CEPH_PGID_ENCODING_LEN + 1; /* spgid */
6278cb441c0SIlya Dryomov 	msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */
6288cb441c0SIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
6298cb441c0SIlya Dryomov 			sizeof(struct ceph_osd_reqid); /* reqid */
6308cb441c0SIlya Dryomov 	msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */
6318cb441c0SIlya Dryomov 	msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */
63230c156d9SYan, Zheng 	msg_size += CEPH_ENCODING_START_BLK_LEN +
63330c156d9SYan, Zheng 			ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
63413d1ad16SIlya Dryomov 	msg_size += 4 + req->r_base_oid.name_len; /* oid */
63513d1ad16SIlya Dryomov 	msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
636ae458f5aSIlya Dryomov 	msg_size += 8; /* snapid */
637ae458f5aSIlya Dryomov 	msg_size += 8; /* snap_seq */
63813d1ad16SIlya Dryomov 	msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
6398cb441c0SIlya Dryomov 	msg_size += 4 + 8; /* retry_attempt, features */
640ae458f5aSIlya Dryomov 
64113d1ad16SIlya Dryomov 	if (req->r_mempool)
6420d9c1ab3SIlya Dryomov 		msg = ceph_msgpool_get(&osdc->msgpool_op, msg_size,
6430d9c1ab3SIlya Dryomov 				       num_request_data_items);
6443d14c5d2SYehuda Sadeh 	else
6450d9c1ab3SIlya Dryomov 		msg = ceph_msg_new2(CEPH_MSG_OSD_OP, msg_size,
6460d9c1ab3SIlya Dryomov 				    num_request_data_items, gfp, true);
64713d1ad16SIlya Dryomov 	if (!msg)
64813d1ad16SIlya Dryomov 		return -ENOMEM;
6493d14c5d2SYehuda Sadeh 
6503d14c5d2SYehuda Sadeh 	memset(msg->front.iov_base, 0, msg->front.iov_len);
6513d14c5d2SYehuda Sadeh 	req->r_request = msg;
6523d14c5d2SYehuda Sadeh 
65313d1ad16SIlya Dryomov 	/* create reply message */
65413d1ad16SIlya Dryomov 	msg_size = OSD_OPREPLY_FRONT_LEN;
655711da55dSIlya Dryomov 	msg_size += req->r_base_oid.name_len;
656711da55dSIlya Dryomov 	msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
65713d1ad16SIlya Dryomov 
65813d1ad16SIlya Dryomov 	if (req->r_mempool)
6590d9c1ab3SIlya Dryomov 		msg = ceph_msgpool_get(&osdc->msgpool_op_reply, msg_size,
6600d9c1ab3SIlya Dryomov 				       num_reply_data_items);
66113d1ad16SIlya Dryomov 	else
6620d9c1ab3SIlya Dryomov 		msg = ceph_msg_new2(CEPH_MSG_OSD_OPREPLY, msg_size,
6630d9c1ab3SIlya Dryomov 				    num_reply_data_items, gfp, true);
66413d1ad16SIlya Dryomov 	if (!msg)
66513d1ad16SIlya Dryomov 		return -ENOMEM;
66613d1ad16SIlya Dryomov 
66713d1ad16SIlya Dryomov 	req->r_reply = msg;
66813d1ad16SIlya Dryomov 
66913d1ad16SIlya Dryomov 	return 0;
67013d1ad16SIlya Dryomov }
6713d14c5d2SYehuda Sadeh 
672a8dd0a37SAlex Elder static bool osd_req_opcode_valid(u16 opcode)
673a8dd0a37SAlex Elder {
674a8dd0a37SAlex Elder 	switch (opcode) {
67570b5bfa3SIlya Dryomov #define GENERATE_CASE(op, opcode, str)	case CEPH_OSD_OP_##op: return true;
67670b5bfa3SIlya Dryomov __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
67770b5bfa3SIlya Dryomov #undef GENERATE_CASE
678a8dd0a37SAlex Elder 	default:
679a8dd0a37SAlex Elder 		return false;
680a8dd0a37SAlex Elder 	}
681a8dd0a37SAlex Elder }
682a8dd0a37SAlex Elder 
6830d9c1ab3SIlya Dryomov static void get_num_data_items(struct ceph_osd_request *req,
6840d9c1ab3SIlya Dryomov 			       int *num_request_data_items,
6850d9c1ab3SIlya Dryomov 			       int *num_reply_data_items)
6860d9c1ab3SIlya Dryomov {
6870d9c1ab3SIlya Dryomov 	struct ceph_osd_req_op *op;
6880d9c1ab3SIlya Dryomov 
6890d9c1ab3SIlya Dryomov 	*num_request_data_items = 0;
6900d9c1ab3SIlya Dryomov 	*num_reply_data_items = 0;
6910d9c1ab3SIlya Dryomov 
6920d9c1ab3SIlya Dryomov 	for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
6930d9c1ab3SIlya Dryomov 		switch (op->op) {
6940d9c1ab3SIlya Dryomov 		/* request */
6950d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_WRITE:
6960d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_WRITEFULL:
6970d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_SETXATTR:
6980d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_CMPXATTR:
6990d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY_ACK:
70023ddf9beSLuis Henriques 		case CEPH_OSD_OP_COPY_FROM:
7010d9c1ab3SIlya Dryomov 			*num_request_data_items += 1;
7020d9c1ab3SIlya Dryomov 			break;
7030d9c1ab3SIlya Dryomov 
7040d9c1ab3SIlya Dryomov 		/* reply */
7050d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_STAT:
7060d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_READ:
7070d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_LIST_WATCHERS:
7080d9c1ab3SIlya Dryomov 			*num_reply_data_items += 1;
7090d9c1ab3SIlya Dryomov 			break;
7100d9c1ab3SIlya Dryomov 
7110d9c1ab3SIlya Dryomov 		/* both */
7120d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY:
7130d9c1ab3SIlya Dryomov 			*num_request_data_items += 1;
7140d9c1ab3SIlya Dryomov 			*num_reply_data_items += 1;
7150d9c1ab3SIlya Dryomov 			break;
7160d9c1ab3SIlya Dryomov 		case CEPH_OSD_OP_CALL:
7170d9c1ab3SIlya Dryomov 			*num_request_data_items += 2;
7180d9c1ab3SIlya Dryomov 			*num_reply_data_items += 1;
7190d9c1ab3SIlya Dryomov 			break;
7200d9c1ab3SIlya Dryomov 
7210d9c1ab3SIlya Dryomov 		default:
7220d9c1ab3SIlya Dryomov 			WARN_ON(!osd_req_opcode_valid(op->op));
7230d9c1ab3SIlya Dryomov 			break;
7240d9c1ab3SIlya Dryomov 		}
7250d9c1ab3SIlya Dryomov 	}
7260d9c1ab3SIlya Dryomov }
7270d9c1ab3SIlya Dryomov 
7280d9c1ab3SIlya Dryomov /*
7290d9c1ab3SIlya Dryomov  * oid, oloc and OSD op opcode(s) must be filled in before this function
7300d9c1ab3SIlya Dryomov  * is called.
7310d9c1ab3SIlya Dryomov  */
7320d9c1ab3SIlya Dryomov int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
7330d9c1ab3SIlya Dryomov {
7340d9c1ab3SIlya Dryomov 	int num_request_data_items, num_reply_data_items;
7350d9c1ab3SIlya Dryomov 
7360d9c1ab3SIlya Dryomov 	get_num_data_items(req, &num_request_data_items, &num_reply_data_items);
7370d9c1ab3SIlya Dryomov 	return __ceph_osdc_alloc_messages(req, gfp, num_request_data_items,
7380d9c1ab3SIlya Dryomov 					  num_reply_data_items);
7390d9c1ab3SIlya Dryomov }
7400d9c1ab3SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_alloc_messages);
7410d9c1ab3SIlya Dryomov 
74233803f33SAlex Elder /*
74333803f33SAlex Elder  * This is an osd op init function for opcodes that have no data or
74433803f33SAlex Elder  * other information associated with them.  It also serves as a
74533803f33SAlex Elder  * common init routine for all the other init functions, below.
74633803f33SAlex Elder  */
747c99d2d4aSAlex Elder static struct ceph_osd_req_op *
74849719778SAlex Elder _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
749144cba14SYan, Zheng 		 u16 opcode, u32 flags)
75033803f33SAlex Elder {
751c99d2d4aSAlex Elder 	struct ceph_osd_req_op *op;
752c99d2d4aSAlex Elder 
753c99d2d4aSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
75433803f33SAlex Elder 	BUG_ON(!osd_req_opcode_valid(opcode));
75533803f33SAlex Elder 
756c99d2d4aSAlex Elder 	op = &osd_req->r_ops[which];
75733803f33SAlex Elder 	memset(op, 0, sizeof (*op));
75833803f33SAlex Elder 	op->op = opcode;
759144cba14SYan, Zheng 	op->flags = flags;
760c99d2d4aSAlex Elder 
761c99d2d4aSAlex Elder 	return op;
76233803f33SAlex Elder }
76333803f33SAlex Elder 
76449719778SAlex Elder void osd_req_op_init(struct ceph_osd_request *osd_req,
765144cba14SYan, Zheng 		     unsigned int which, u16 opcode, u32 flags)
76649719778SAlex Elder {
767144cba14SYan, Zheng 	(void)_osd_req_op_init(osd_req, which, opcode, flags);
76849719778SAlex Elder }
76949719778SAlex Elder EXPORT_SYMBOL(osd_req_op_init);
77049719778SAlex Elder 
771c99d2d4aSAlex Elder void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
772c99d2d4aSAlex Elder 				unsigned int which, u16 opcode,
77333803f33SAlex Elder 				u64 offset, u64 length,
77433803f33SAlex Elder 				u64 truncate_size, u32 truncate_seq)
77533803f33SAlex Elder {
776144cba14SYan, Zheng 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
777144cba14SYan, Zheng 						      opcode, 0);
77833803f33SAlex Elder 	size_t payload_len = 0;
77933803f33SAlex Elder 
780ad7a60deSLi Wang 	BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
781e30b7577SIlya Dryomov 	       opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
782e30b7577SIlya Dryomov 	       opcode != CEPH_OSD_OP_TRUNCATE);
78333803f33SAlex Elder 
78433803f33SAlex Elder 	op->extent.offset = offset;
78533803f33SAlex Elder 	op->extent.length = length;
78633803f33SAlex Elder 	op->extent.truncate_size = truncate_size;
78733803f33SAlex Elder 	op->extent.truncate_seq = truncate_seq;
788e30b7577SIlya Dryomov 	if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
78933803f33SAlex Elder 		payload_len += length;
79033803f33SAlex Elder 
791de2aa102SIlya Dryomov 	op->indata_len = payload_len;
79233803f33SAlex Elder }
79333803f33SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_init);
79433803f33SAlex Elder 
795c99d2d4aSAlex Elder void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
796c99d2d4aSAlex Elder 				unsigned int which, u64 length)
797e5975c7cSAlex Elder {
798c99d2d4aSAlex Elder 	struct ceph_osd_req_op *op;
799c99d2d4aSAlex Elder 	u64 previous;
800c99d2d4aSAlex Elder 
801c99d2d4aSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
802c99d2d4aSAlex Elder 	op = &osd_req->r_ops[which];
803c99d2d4aSAlex Elder 	previous = op->extent.length;
804e5975c7cSAlex Elder 
805e5975c7cSAlex Elder 	if (length == previous)
806e5975c7cSAlex Elder 		return;		/* Nothing to do */
807e5975c7cSAlex Elder 	BUG_ON(length > previous);
808e5975c7cSAlex Elder 
809e5975c7cSAlex Elder 	op->extent.length = length;
810d641df81SYan, Zheng 	if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
811de2aa102SIlya Dryomov 		op->indata_len -= previous - length;
812e5975c7cSAlex Elder }
813e5975c7cSAlex Elder EXPORT_SYMBOL(osd_req_op_extent_update);
814e5975c7cSAlex Elder 
8152c63f49aSYan, Zheng void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
8162c63f49aSYan, Zheng 				unsigned int which, u64 offset_inc)
8172c63f49aSYan, Zheng {
8182c63f49aSYan, Zheng 	struct ceph_osd_req_op *op, *prev_op;
8192c63f49aSYan, Zheng 
8202c63f49aSYan, Zheng 	BUG_ON(which + 1 >= osd_req->r_num_ops);
8212c63f49aSYan, Zheng 
8222c63f49aSYan, Zheng 	prev_op = &osd_req->r_ops[which];
8232c63f49aSYan, Zheng 	op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
8242c63f49aSYan, Zheng 	/* dup previous one */
8252c63f49aSYan, Zheng 	op->indata_len = prev_op->indata_len;
8262c63f49aSYan, Zheng 	op->outdata_len = prev_op->outdata_len;
8272c63f49aSYan, Zheng 	op->extent = prev_op->extent;
8282c63f49aSYan, Zheng 	/* adjust offset */
8292c63f49aSYan, Zheng 	op->extent.offset += offset_inc;
8302c63f49aSYan, Zheng 	op->extent.length -= offset_inc;
8312c63f49aSYan, Zheng 
8322c63f49aSYan, Zheng 	if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
8332c63f49aSYan, Zheng 		op->indata_len -= offset_inc;
8342c63f49aSYan, Zheng }
8352c63f49aSYan, Zheng EXPORT_SYMBOL(osd_req_op_extent_dup_last);
8362c63f49aSYan, Zheng 
837fe943d50SChengguang Xu int osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
83824639ce5SIlya Dryomov 			const char *class, const char *method)
83933803f33SAlex Elder {
84024639ce5SIlya Dryomov 	struct ceph_osd_req_op *op;
8415f562df5SAlex Elder 	struct ceph_pagelist *pagelist;
84233803f33SAlex Elder 	size_t payload_len = 0;
84333803f33SAlex Elder 	size_t size;
84433803f33SAlex Elder 
84524639ce5SIlya Dryomov 	op = _osd_req_op_init(osd_req, which, CEPH_OSD_OP_CALL, 0);
84633803f33SAlex Elder 
84733165d47SIlya Dryomov 	pagelist = ceph_pagelist_alloc(GFP_NOFS);
848fe943d50SChengguang Xu 	if (!pagelist)
849fe943d50SChengguang Xu 		return -ENOMEM;
850fe943d50SChengguang Xu 
85133803f33SAlex Elder 	op->cls.class_name = class;
85233803f33SAlex Elder 	size = strlen(class);
85333803f33SAlex Elder 	BUG_ON(size > (size_t) U8_MAX);
85433803f33SAlex Elder 	op->cls.class_len = size;
8555f562df5SAlex Elder 	ceph_pagelist_append(pagelist, class, size);
85633803f33SAlex Elder 	payload_len += size;
85733803f33SAlex Elder 
85833803f33SAlex Elder 	op->cls.method_name = method;
85933803f33SAlex Elder 	size = strlen(method);
86033803f33SAlex Elder 	BUG_ON(size > (size_t) U8_MAX);
86133803f33SAlex Elder 	op->cls.method_len = size;
8625f562df5SAlex Elder 	ceph_pagelist_append(pagelist, method, size);
86333803f33SAlex Elder 	payload_len += size;
86433803f33SAlex Elder 
865a4ce40a9SAlex Elder 	osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
8665f562df5SAlex Elder 
867de2aa102SIlya Dryomov 	op->indata_len = payload_len;
868fe943d50SChengguang Xu 	return 0;
86933803f33SAlex Elder }
87033803f33SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_init);
8718c042b0dSAlex Elder 
872d74b50beSYan, Zheng int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
873d74b50beSYan, Zheng 			  u16 opcode, const char *name, const void *value,
874d74b50beSYan, Zheng 			  size_t size, u8 cmp_op, u8 cmp_mode)
875d74b50beSYan, Zheng {
876144cba14SYan, Zheng 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
877144cba14SYan, Zheng 						      opcode, 0);
878d74b50beSYan, Zheng 	struct ceph_pagelist *pagelist;
879d74b50beSYan, Zheng 	size_t payload_len;
880d74b50beSYan, Zheng 
881d74b50beSYan, Zheng 	BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
882d74b50beSYan, Zheng 
88333165d47SIlya Dryomov 	pagelist = ceph_pagelist_alloc(GFP_NOFS);
884d74b50beSYan, Zheng 	if (!pagelist)
885d74b50beSYan, Zheng 		return -ENOMEM;
886d74b50beSYan, Zheng 
887d74b50beSYan, Zheng 	payload_len = strlen(name);
888d74b50beSYan, Zheng 	op->xattr.name_len = payload_len;
889d74b50beSYan, Zheng 	ceph_pagelist_append(pagelist, name, payload_len);
890d74b50beSYan, Zheng 
891d74b50beSYan, Zheng 	op->xattr.value_len = size;
892d74b50beSYan, Zheng 	ceph_pagelist_append(pagelist, value, size);
893d74b50beSYan, Zheng 	payload_len += size;
894d74b50beSYan, Zheng 
895d74b50beSYan, Zheng 	op->xattr.cmp_op = cmp_op;
896d74b50beSYan, Zheng 	op->xattr.cmp_mode = cmp_mode;
897d74b50beSYan, Zheng 
898d74b50beSYan, Zheng 	ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
899de2aa102SIlya Dryomov 	op->indata_len = payload_len;
900d74b50beSYan, Zheng 	return 0;
901d74b50beSYan, Zheng }
902d74b50beSYan, Zheng EXPORT_SYMBOL(osd_req_op_xattr_init);
903d74b50beSYan, Zheng 
904922dab61SIlya Dryomov /*
905922dab61SIlya Dryomov  * @watch_opcode: CEPH_OSD_WATCH_OP_*
906922dab61SIlya Dryomov  */
907922dab61SIlya Dryomov static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
908922dab61SIlya Dryomov 				  u64 cookie, u8 watch_opcode)
90933803f33SAlex Elder {
910922dab61SIlya Dryomov 	struct ceph_osd_req_op *op;
91133803f33SAlex Elder 
912922dab61SIlya Dryomov 	op = _osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
91333803f33SAlex Elder 	op->watch.cookie = cookie;
914922dab61SIlya Dryomov 	op->watch.op = watch_opcode;
915922dab61SIlya Dryomov 	op->watch.gen = 0;
91633803f33SAlex Elder }
91733803f33SAlex Elder 
918c647b8a8SIlya Dryomov void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
919c647b8a8SIlya Dryomov 				unsigned int which,
920c647b8a8SIlya Dryomov 				u64 expected_object_size,
921c647b8a8SIlya Dryomov 				u64 expected_write_size)
922c647b8a8SIlya Dryomov {
923c647b8a8SIlya Dryomov 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
924144cba14SYan, Zheng 						      CEPH_OSD_OP_SETALLOCHINT,
925144cba14SYan, Zheng 						      0);
926c647b8a8SIlya Dryomov 
927c647b8a8SIlya Dryomov 	op->alloc_hint.expected_object_size = expected_object_size;
928c647b8a8SIlya Dryomov 	op->alloc_hint.expected_write_size = expected_write_size;
929c647b8a8SIlya Dryomov 
930c647b8a8SIlya Dryomov 	/*
931c647b8a8SIlya Dryomov 	 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
932c647b8a8SIlya Dryomov 	 * not worth a feature bit.  Set FAILOK per-op flag to make
933c647b8a8SIlya Dryomov 	 * sure older osds don't trip over an unsupported opcode.
934c647b8a8SIlya Dryomov 	 */
935c647b8a8SIlya Dryomov 	op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
936c647b8a8SIlya Dryomov }
937c647b8a8SIlya Dryomov EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
938c647b8a8SIlya Dryomov 
93990af3602SAlex Elder static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
940ec9123c5SAlex Elder 				struct ceph_osd_data *osd_data)
941ec9123c5SAlex Elder {
942ec9123c5SAlex Elder 	u64 length = ceph_osd_data_length(osd_data);
943ec9123c5SAlex Elder 
944ec9123c5SAlex Elder 	if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
945ec9123c5SAlex Elder 		BUG_ON(length > (u64) SIZE_MAX);
946ec9123c5SAlex Elder 		if (length)
94790af3602SAlex Elder 			ceph_msg_data_add_pages(msg, osd_data->pages,
948ec9123c5SAlex Elder 					length, osd_data->alignment);
949ec9123c5SAlex Elder 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
950ec9123c5SAlex Elder 		BUG_ON(!length);
95190af3602SAlex Elder 		ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
952ec9123c5SAlex Elder #ifdef CONFIG_BLOCK
953ec9123c5SAlex Elder 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
9545359a17dSIlya Dryomov 		ceph_msg_data_add_bio(msg, &osd_data->bio_pos, length);
955ec9123c5SAlex Elder #endif
956b9e281c2SIlya Dryomov 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_BVECS) {
957b9e281c2SIlya Dryomov 		ceph_msg_data_add_bvecs(msg, &osd_data->bvec_pos);
958ec9123c5SAlex Elder 	} else {
959ec9123c5SAlex Elder 		BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
960ec9123c5SAlex Elder 	}
961ec9123c5SAlex Elder }
962ec9123c5SAlex Elder 
963bb873b53SIlya Dryomov static u32 osd_req_encode_op(struct ceph_osd_op *dst,
964bb873b53SIlya Dryomov 			     const struct ceph_osd_req_op *src)
9653d14c5d2SYehuda Sadeh {
966065a68f9SAlex Elder 	switch (src->op) {
967fbfab539SAlex Elder 	case CEPH_OSD_OP_STAT:
968fbfab539SAlex Elder 		break;
9693d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_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);
1004c647b8a8SIlya Dryomov 		break;
1005d74b50beSYan, Zheng 	case CEPH_OSD_OP_SETXATTR:
1006d74b50beSYan, Zheng 	case CEPH_OSD_OP_CMPXATTR:
1007d74b50beSYan, Zheng 		dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
1008d74b50beSYan, Zheng 		dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
1009d74b50beSYan, Zheng 		dst->xattr.cmp_op = src->xattr.cmp_op;
1010d74b50beSYan, Zheng 		dst->xattr.cmp_mode = src->xattr.cmp_mode;
1011d74b50beSYan, Zheng 		break;
1012864e9197SYan, Zheng 	case CEPH_OSD_OP_CREATE:
1013864e9197SYan, Zheng 	case CEPH_OSD_OP_DELETE:
1014864e9197SYan, Zheng 		break;
101523ddf9beSLuis Henriques 	case CEPH_OSD_OP_COPY_FROM:
101623ddf9beSLuis Henriques 		dst->copy_from.snapid = cpu_to_le64(src->copy_from.snapid);
101723ddf9beSLuis Henriques 		dst->copy_from.src_version =
101823ddf9beSLuis Henriques 			cpu_to_le64(src->copy_from.src_version);
101923ddf9beSLuis Henriques 		dst->copy_from.flags = src->copy_from.flags;
102023ddf9beSLuis Henriques 		dst->copy_from.src_fadvise_flags =
102123ddf9beSLuis Henriques 			cpu_to_le32(src->copy_from.src_fadvise_flags);
102223ddf9beSLuis Henriques 		break;
10233d14c5d2SYehuda Sadeh 	default:
10244c46459cSAlex Elder 		pr_err("unsupported osd opcode %s\n",
10258f63ca2dSAlex Elder 			ceph_osd_op_name(src->op));
10264c46459cSAlex Elder 		WARN_ON(1);
1027a8dd0a37SAlex Elder 
1028a8dd0a37SAlex Elder 		return 0;
10293d14c5d2SYehuda Sadeh 	}
10307b25bf5fSIlya Dryomov 
1031a8dd0a37SAlex Elder 	dst->op = cpu_to_le16(src->op);
10327b25bf5fSIlya Dryomov 	dst->flags = cpu_to_le32(src->flags);
1033de2aa102SIlya Dryomov 	dst->payload_len = cpu_to_le32(src->indata_len);
1034175face2SAlex Elder 
1035bb873b53SIlya Dryomov 	return src->indata_len;
10363d14c5d2SYehuda Sadeh }
10373d14c5d2SYehuda Sadeh 
10383d14c5d2SYehuda Sadeh /*
10393d14c5d2SYehuda Sadeh  * build new request AND message, calculate layout, and adjust file
10403d14c5d2SYehuda Sadeh  * extent as needed.
10413d14c5d2SYehuda Sadeh  *
10423d14c5d2SYehuda Sadeh  * if the file was recently truncated, we include information about its
10433d14c5d2SYehuda Sadeh  * old and new size so that the object can be updated appropriately.  (we
10443d14c5d2SYehuda Sadeh  * avoid synchronously deleting truncated objects because it's slow.)
10453d14c5d2SYehuda Sadeh  */
10463d14c5d2SYehuda Sadeh struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
10473d14c5d2SYehuda Sadeh 					       struct ceph_file_layout *layout,
10483d14c5d2SYehuda Sadeh 					       struct ceph_vino vino,
1049715e4cd4SYan, Zheng 					       u64 off, u64 *plen,
1050715e4cd4SYan, Zheng 					       unsigned int which, int num_ops,
10513d14c5d2SYehuda Sadeh 					       int opcode, int flags,
10523d14c5d2SYehuda Sadeh 					       struct ceph_snap_context *snapc,
10533d14c5d2SYehuda Sadeh 					       u32 truncate_seq,
10543d14c5d2SYehuda Sadeh 					       u64 truncate_size,
1055153e5167SAlex Elder 					       bool use_mempool)
10563d14c5d2SYehuda Sadeh {
10573d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
105875d1c941SAlex Elder 	u64 objnum = 0;
105975d1c941SAlex Elder 	u64 objoff = 0;
106075d1c941SAlex Elder 	u64 objlen = 0;
10616816282dSSage Weil 	int r;
10623d14c5d2SYehuda Sadeh 
1063ad7a60deSLi Wang 	BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
1064864e9197SYan, Zheng 	       opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
1065864e9197SYan, Zheng 	       opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
10663d14c5d2SYehuda Sadeh 
1067acead002SAlex Elder 	req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
1068ae7ca4a3SAlex Elder 					GFP_NOFS);
106913d1ad16SIlya Dryomov 	if (!req) {
107013d1ad16SIlya Dryomov 		r = -ENOMEM;
107113d1ad16SIlya Dryomov 		goto fail;
107213d1ad16SIlya Dryomov 	}
107379528734SAlex Elder 
10743d14c5d2SYehuda Sadeh 	/* calculate max write size */
1075a19dadfbSAlex Elder 	r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
107613d1ad16SIlya Dryomov 	if (r)
107713d1ad16SIlya Dryomov 		goto fail;
1078a19dadfbSAlex Elder 
1079864e9197SYan, Zheng 	if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
1080144cba14SYan, Zheng 		osd_req_op_init(req, which, opcode, 0);
1081864e9197SYan, Zheng 	} else {
10827627151eSYan, Zheng 		u32 object_size = layout->object_size;
1083864e9197SYan, Zheng 		u32 object_base = off - objoff;
1084ccca4e37SYan, Zheng 		if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
1085d18d1e28SAlex Elder 			if (truncate_size <= object_base) {
1086d18d1e28SAlex Elder 				truncate_size = 0;
1087d18d1e28SAlex Elder 			} else {
1088d18d1e28SAlex Elder 				truncate_size -= object_base;
1089d18d1e28SAlex Elder 				if (truncate_size > object_size)
1090d18d1e28SAlex Elder 					truncate_size = object_size;
1091d18d1e28SAlex Elder 			}
1092ccca4e37SYan, Zheng 		}
1093715e4cd4SYan, Zheng 		osd_req_op_extent_init(req, which, opcode, objoff, objlen,
1094b0270324SAlex Elder 				       truncate_size, truncate_seq);
1095864e9197SYan, Zheng 	}
1096d18d1e28SAlex Elder 
1097bb873b53SIlya Dryomov 	req->r_flags = flags;
10987627151eSYan, Zheng 	req->r_base_oloc.pool = layout->pool_id;
109930c156d9SYan, Zheng 	req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
1100d30291b9SIlya Dryomov 	ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
1101dbe0fc41SAlex Elder 
1102bb873b53SIlya Dryomov 	req->r_snapid = vino.snap;
1103bb873b53SIlya Dryomov 	if (flags & CEPH_OSD_FLAG_WRITE)
1104bb873b53SIlya Dryomov 		req->r_data_offset = off;
1105bb873b53SIlya Dryomov 
11060d9c1ab3SIlya Dryomov 	if (num_ops > 1)
11070d9c1ab3SIlya Dryomov 		/*
11080d9c1ab3SIlya Dryomov 		 * This is a special case for ceph_writepages_start(), but it
11090d9c1ab3SIlya Dryomov 		 * also covers ceph_uninline_data().  If more multi-op request
11100d9c1ab3SIlya Dryomov 		 * use cases emerge, we will need a separate helper.
11110d9c1ab3SIlya Dryomov 		 */
11120d9c1ab3SIlya Dryomov 		r = __ceph_osdc_alloc_messages(req, GFP_NOFS, num_ops, 0);
11130d9c1ab3SIlya Dryomov 	else
111413d1ad16SIlya Dryomov 		r = ceph_osdc_alloc_messages(req, GFP_NOFS);
111513d1ad16SIlya Dryomov 	if (r)
111613d1ad16SIlya Dryomov 		goto fail;
111713d1ad16SIlya Dryomov 
11183d14c5d2SYehuda Sadeh 	return req;
111913d1ad16SIlya Dryomov 
112013d1ad16SIlya Dryomov fail:
112113d1ad16SIlya Dryomov 	ceph_osdc_put_request(req);
112213d1ad16SIlya Dryomov 	return ERR_PTR(r);
11233d14c5d2SYehuda Sadeh }
11243d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_new_request);
11253d14c5d2SYehuda Sadeh 
11263d14c5d2SYehuda Sadeh /*
11273d14c5d2SYehuda Sadeh  * We keep osd requests in an rbtree, sorted by ->r_tid.
11283d14c5d2SYehuda Sadeh  */
1129fcd00b68SIlya Dryomov DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
11304609245eSIlya Dryomov DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
11313d14c5d2SYehuda Sadeh 
113266850df5SIlya Dryomov /*
113366850df5SIlya Dryomov  * Call @fn on each OSD request as long as @fn returns 0.
113466850df5SIlya Dryomov  */
113566850df5SIlya Dryomov static void for_each_request(struct ceph_osd_client *osdc,
113666850df5SIlya Dryomov 			int (*fn)(struct ceph_osd_request *req, void *arg),
113766850df5SIlya Dryomov 			void *arg)
113866850df5SIlya Dryomov {
113966850df5SIlya Dryomov 	struct rb_node *n, *p;
114066850df5SIlya Dryomov 
114166850df5SIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
114266850df5SIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
114366850df5SIlya Dryomov 
114466850df5SIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; ) {
114566850df5SIlya Dryomov 			struct ceph_osd_request *req =
114666850df5SIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
114766850df5SIlya Dryomov 
114866850df5SIlya Dryomov 			p = rb_next(p);
114966850df5SIlya Dryomov 			if (fn(req, arg))
115066850df5SIlya Dryomov 				return;
115166850df5SIlya Dryomov 		}
115266850df5SIlya Dryomov 	}
115366850df5SIlya Dryomov 
115466850df5SIlya Dryomov 	for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
115566850df5SIlya Dryomov 		struct ceph_osd_request *req =
115666850df5SIlya Dryomov 		    rb_entry(p, struct ceph_osd_request, r_node);
115766850df5SIlya Dryomov 
115866850df5SIlya Dryomov 		p = rb_next(p);
115966850df5SIlya Dryomov 		if (fn(req, arg))
116066850df5SIlya Dryomov 			return;
116166850df5SIlya Dryomov 	}
116266850df5SIlya Dryomov }
116366850df5SIlya Dryomov 
11640247a0cfSIlya Dryomov static bool osd_homeless(struct ceph_osd *osd)
11650247a0cfSIlya Dryomov {
11660247a0cfSIlya Dryomov 	return osd->o_osd == CEPH_HOMELESS_OSD;
11670247a0cfSIlya Dryomov }
11680247a0cfSIlya Dryomov 
11695aea3dcdSIlya Dryomov static bool osd_registered(struct ceph_osd *osd)
11703d14c5d2SYehuda Sadeh {
11715aea3dcdSIlya Dryomov 	verify_osdc_locked(osd->o_osdc);
11723d14c5d2SYehuda Sadeh 
11735aea3dcdSIlya Dryomov 	return !RB_EMPTY_NODE(&osd->o_node);
11743d14c5d2SYehuda Sadeh }
11753d14c5d2SYehuda Sadeh 
11763d14c5d2SYehuda Sadeh /*
11770247a0cfSIlya Dryomov  * Assumes @osd is zero-initialized.
11780247a0cfSIlya Dryomov  */
11790247a0cfSIlya Dryomov static void osd_init(struct ceph_osd *osd)
11800247a0cfSIlya Dryomov {
118102113a0fSElena Reshetova 	refcount_set(&osd->o_ref, 1);
11820247a0cfSIlya Dryomov 	RB_CLEAR_NODE(&osd->o_node);
11835aea3dcdSIlya Dryomov 	osd->o_requests = RB_ROOT;
1184922dab61SIlya Dryomov 	osd->o_linger_requests = RB_ROOT;
1185a02a946dSIlya Dryomov 	osd->o_backoff_mappings = RB_ROOT;
1186a02a946dSIlya Dryomov 	osd->o_backoffs_by_id = RB_ROOT;
11870247a0cfSIlya Dryomov 	INIT_LIST_HEAD(&osd->o_osd_lru);
11880247a0cfSIlya Dryomov 	INIT_LIST_HEAD(&osd->o_keepalive_item);
11890247a0cfSIlya Dryomov 	osd->o_incarnation = 1;
11905aea3dcdSIlya Dryomov 	mutex_init(&osd->lock);
11910247a0cfSIlya Dryomov }
11920247a0cfSIlya Dryomov 
11930247a0cfSIlya Dryomov static void osd_cleanup(struct ceph_osd *osd)
11940247a0cfSIlya Dryomov {
11950247a0cfSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
11965aea3dcdSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
1197922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
1198a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
1199a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
12000247a0cfSIlya Dryomov 	WARN_ON(!list_empty(&osd->o_osd_lru));
12010247a0cfSIlya Dryomov 	WARN_ON(!list_empty(&osd->o_keepalive_item));
12020247a0cfSIlya Dryomov 
12030247a0cfSIlya Dryomov 	if (osd->o_auth.authorizer) {
12040247a0cfSIlya Dryomov 		WARN_ON(osd_homeless(osd));
12050247a0cfSIlya Dryomov 		ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
12060247a0cfSIlya Dryomov 	}
12070247a0cfSIlya Dryomov }
12080247a0cfSIlya Dryomov 
12090247a0cfSIlya Dryomov /*
12103d14c5d2SYehuda Sadeh  * Track open sessions with osds.
12113d14c5d2SYehuda Sadeh  */
1212e10006f8SAlex Elder static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
12133d14c5d2SYehuda Sadeh {
12143d14c5d2SYehuda Sadeh 	struct ceph_osd *osd;
12153d14c5d2SYehuda Sadeh 
12160247a0cfSIlya Dryomov 	WARN_ON(onum == CEPH_HOMELESS_OSD);
12170247a0cfSIlya Dryomov 
12187a28f59bSIlya Dryomov 	osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
12190247a0cfSIlya Dryomov 	osd_init(osd);
12203d14c5d2SYehuda Sadeh 	osd->o_osdc = osdc;
1221e10006f8SAlex Elder 	osd->o_osd = onum;
12223d14c5d2SYehuda Sadeh 
1223b7a9e5ddSSage Weil 	ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
12243d14c5d2SYehuda Sadeh 
12253d14c5d2SYehuda Sadeh 	return osd;
12263d14c5d2SYehuda Sadeh }
12273d14c5d2SYehuda Sadeh 
12283d14c5d2SYehuda Sadeh static struct ceph_osd *get_osd(struct ceph_osd *osd)
12293d14c5d2SYehuda Sadeh {
123002113a0fSElena Reshetova 	if (refcount_inc_not_zero(&osd->o_ref)) {
123102113a0fSElena Reshetova 		dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
123202113a0fSElena Reshetova 		     refcount_read(&osd->o_ref));
12333d14c5d2SYehuda Sadeh 		return osd;
12343d14c5d2SYehuda Sadeh 	} else {
12353d14c5d2SYehuda Sadeh 		dout("get_osd %p FAIL\n", osd);
12363d14c5d2SYehuda Sadeh 		return NULL;
12373d14c5d2SYehuda Sadeh 	}
12383d14c5d2SYehuda Sadeh }
12393d14c5d2SYehuda Sadeh 
12403d14c5d2SYehuda Sadeh static void put_osd(struct ceph_osd *osd)
12413d14c5d2SYehuda Sadeh {
124202113a0fSElena Reshetova 	dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
124302113a0fSElena Reshetova 	     refcount_read(&osd->o_ref) - 1);
124402113a0fSElena Reshetova 	if (refcount_dec_and_test(&osd->o_ref)) {
12450247a0cfSIlya Dryomov 		osd_cleanup(osd);
12463d14c5d2SYehuda Sadeh 		kfree(osd);
12473d14c5d2SYehuda Sadeh 	}
12483d14c5d2SYehuda Sadeh }
12493d14c5d2SYehuda Sadeh 
1250fcd00b68SIlya Dryomov DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1251fcd00b68SIlya Dryomov 
12529dd2845cSIlya Dryomov static void __move_osd_to_lru(struct ceph_osd *osd)
12533d14c5d2SYehuda Sadeh {
12549dd2845cSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
12559dd2845cSIlya Dryomov 
12569dd2845cSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
12573d14c5d2SYehuda Sadeh 	BUG_ON(!list_empty(&osd->o_osd_lru));
1258bbf37ec3SIlya Dryomov 
12599dd2845cSIlya Dryomov 	spin_lock(&osdc->osd_lru_lock);
12603d14c5d2SYehuda Sadeh 	list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
12619dd2845cSIlya Dryomov 	spin_unlock(&osdc->osd_lru_lock);
12629dd2845cSIlya Dryomov 
1263a319bf56SIlya Dryomov 	osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
12643d14c5d2SYehuda Sadeh }
12653d14c5d2SYehuda Sadeh 
12669dd2845cSIlya Dryomov static void maybe_move_osd_to_lru(struct ceph_osd *osd)
1267bbf37ec3SIlya Dryomov {
12685aea3dcdSIlya Dryomov 	if (RB_EMPTY_ROOT(&osd->o_requests) &&
1269922dab61SIlya Dryomov 	    RB_EMPTY_ROOT(&osd->o_linger_requests))
12709dd2845cSIlya Dryomov 		__move_osd_to_lru(osd);
1271bbf37ec3SIlya Dryomov }
1272bbf37ec3SIlya Dryomov 
12733d14c5d2SYehuda Sadeh static void __remove_osd_from_lru(struct ceph_osd *osd)
12743d14c5d2SYehuda Sadeh {
12759dd2845cSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
12769dd2845cSIlya Dryomov 
12779dd2845cSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
12789dd2845cSIlya Dryomov 
12799dd2845cSIlya Dryomov 	spin_lock(&osdc->osd_lru_lock);
12803d14c5d2SYehuda Sadeh 	if (!list_empty(&osd->o_osd_lru))
12813d14c5d2SYehuda Sadeh 		list_del_init(&osd->o_osd_lru);
12829dd2845cSIlya Dryomov 	spin_unlock(&osdc->osd_lru_lock);
12833d14c5d2SYehuda Sadeh }
12843d14c5d2SYehuda Sadeh 
12853d14c5d2SYehuda Sadeh /*
12865aea3dcdSIlya Dryomov  * Close the connection and assign any leftover requests to the
12875aea3dcdSIlya Dryomov  * homeless session.
12885aea3dcdSIlya Dryomov  */
12895aea3dcdSIlya Dryomov static void close_osd(struct ceph_osd *osd)
12905aea3dcdSIlya Dryomov {
12915aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
12925aea3dcdSIlya Dryomov 	struct rb_node *n;
12935aea3dcdSIlya Dryomov 
12945aea3dcdSIlya Dryomov 	verify_osdc_wrlocked(osdc);
12955aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
12965aea3dcdSIlya Dryomov 
12975aea3dcdSIlya Dryomov 	ceph_con_close(&osd->o_con);
12985aea3dcdSIlya Dryomov 
12995aea3dcdSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
13005aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
13015aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
13025aea3dcdSIlya Dryomov 
13035aea3dcdSIlya Dryomov 		n = rb_next(n); /* unlink_request() */
13045aea3dcdSIlya Dryomov 
13055aea3dcdSIlya Dryomov 		dout(" reassigning req %p tid %llu\n", req, req->r_tid);
13065aea3dcdSIlya Dryomov 		unlink_request(osd, req);
13075aea3dcdSIlya Dryomov 		link_request(&osdc->homeless_osd, req);
13085aea3dcdSIlya Dryomov 	}
1309922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; ) {
1310922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
1311922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
1312922dab61SIlya Dryomov 
1313922dab61SIlya Dryomov 		n = rb_next(n); /* unlink_linger() */
1314922dab61SIlya Dryomov 
1315922dab61SIlya Dryomov 		dout(" reassigning lreq %p linger_id %llu\n", lreq,
1316922dab61SIlya Dryomov 		     lreq->linger_id);
1317922dab61SIlya Dryomov 		unlink_linger(osd, lreq);
1318922dab61SIlya Dryomov 		link_linger(&osdc->homeless_osd, lreq);
1319922dab61SIlya Dryomov 	}
1320a02a946dSIlya Dryomov 	clear_backoffs(osd);
13215aea3dcdSIlya Dryomov 
13225aea3dcdSIlya Dryomov 	__remove_osd_from_lru(osd);
13235aea3dcdSIlya Dryomov 	erase_osd(&osdc->osds, osd);
13245aea3dcdSIlya Dryomov 	put_osd(osd);
13255aea3dcdSIlya Dryomov }
13265aea3dcdSIlya Dryomov 
13275aea3dcdSIlya Dryomov /*
13283d14c5d2SYehuda Sadeh  * reset osd connect
13293d14c5d2SYehuda Sadeh  */
13305aea3dcdSIlya Dryomov static int reopen_osd(struct ceph_osd *osd)
13313d14c5d2SYehuda Sadeh {
1332c3acb181SAlex Elder 	struct ceph_entity_addr *peer_addr;
13333d14c5d2SYehuda Sadeh 
13345aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
13355aea3dcdSIlya Dryomov 
13365aea3dcdSIlya Dryomov 	if (RB_EMPTY_ROOT(&osd->o_requests) &&
1337922dab61SIlya Dryomov 	    RB_EMPTY_ROOT(&osd->o_linger_requests)) {
13385aea3dcdSIlya Dryomov 		close_osd(osd);
1339c3acb181SAlex Elder 		return -ENODEV;
1340c3acb181SAlex Elder 	}
1341c3acb181SAlex Elder 
13425aea3dcdSIlya Dryomov 	peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
1343c3acb181SAlex Elder 	if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
13443d14c5d2SYehuda Sadeh 			!ceph_con_opened(&osd->o_con)) {
13455aea3dcdSIlya Dryomov 		struct rb_node *n;
1346c3acb181SAlex Elder 
13473d14c5d2SYehuda Sadeh 		dout("osd addr hasn't changed and connection never opened, "
13480b4af2e8SIlya Dryomov 		     "letting msgr retry\n");
13493d14c5d2SYehuda Sadeh 		/* touch each r_stamp for handle_timeout()'s benfit */
13505aea3dcdSIlya Dryomov 		for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
13515aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
13525aea3dcdSIlya Dryomov 			    rb_entry(n, struct ceph_osd_request, r_node);
13533d14c5d2SYehuda Sadeh 			req->r_stamp = jiffies;
13545aea3dcdSIlya Dryomov 		}
1355c3acb181SAlex Elder 
1356c3acb181SAlex Elder 		return -EAGAIN;
13573d14c5d2SYehuda Sadeh 	}
1358c3acb181SAlex Elder 
1359c3acb181SAlex Elder 	ceph_con_close(&osd->o_con);
1360c3acb181SAlex Elder 	ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1361c3acb181SAlex Elder 	osd->o_incarnation++;
1362c3acb181SAlex Elder 
1363c3acb181SAlex Elder 	return 0;
13643d14c5d2SYehuda Sadeh }
13653d14c5d2SYehuda Sadeh 
13665aea3dcdSIlya Dryomov static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
13675aea3dcdSIlya Dryomov 					  bool wrlocked)
13683d14c5d2SYehuda Sadeh {
13695aea3dcdSIlya Dryomov 	struct ceph_osd *osd;
13705aea3dcdSIlya Dryomov 
13715aea3dcdSIlya Dryomov 	if (wrlocked)
13725aea3dcdSIlya Dryomov 		verify_osdc_wrlocked(osdc);
13735aea3dcdSIlya Dryomov 	else
13745aea3dcdSIlya Dryomov 		verify_osdc_locked(osdc);
13755aea3dcdSIlya Dryomov 
13765aea3dcdSIlya Dryomov 	if (o != CEPH_HOMELESS_OSD)
13775aea3dcdSIlya Dryomov 		osd = lookup_osd(&osdc->osds, o);
13785aea3dcdSIlya Dryomov 	else
13795aea3dcdSIlya Dryomov 		osd = &osdc->homeless_osd;
13805aea3dcdSIlya Dryomov 	if (!osd) {
13815aea3dcdSIlya Dryomov 		if (!wrlocked)
13825aea3dcdSIlya Dryomov 			return ERR_PTR(-EAGAIN);
13835aea3dcdSIlya Dryomov 
13845aea3dcdSIlya Dryomov 		osd = create_osd(osdc, o);
13855aea3dcdSIlya Dryomov 		insert_osd(&osdc->osds, osd);
13865aea3dcdSIlya Dryomov 		ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
13875aea3dcdSIlya Dryomov 			      &osdc->osdmap->osd_addr[osd->o_osd]);
13885aea3dcdSIlya Dryomov 	}
13895aea3dcdSIlya Dryomov 
13905aea3dcdSIlya Dryomov 	dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
13915aea3dcdSIlya Dryomov 	return osd;
1392a40c4f10SYehuda Sadeh }
1393a40c4f10SYehuda Sadeh 
13943d14c5d2SYehuda Sadeh /*
13955aea3dcdSIlya Dryomov  * Create request <-> OSD session relation.
13965aea3dcdSIlya Dryomov  *
13975aea3dcdSIlya Dryomov  * @req has to be assigned a tid, @osd may be homeless.
13983d14c5d2SYehuda Sadeh  */
13995aea3dcdSIlya Dryomov static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
14003d14c5d2SYehuda Sadeh {
14015aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
14025aea3dcdSIlya Dryomov 	WARN_ON(!req->r_tid || req->r_osd);
14035aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
140435f9f8a0SSage Weil 	     req, req->r_tid);
14055aea3dcdSIlya Dryomov 
14065aea3dcdSIlya Dryomov 	if (!osd_homeless(osd))
14075aea3dcdSIlya Dryomov 		__remove_osd_from_lru(osd);
14085aea3dcdSIlya Dryomov 	else
14095aea3dcdSIlya Dryomov 		atomic_inc(&osd->o_osdc->num_homeless);
14105aea3dcdSIlya Dryomov 
14115aea3dcdSIlya Dryomov 	get_osd(osd);
14125aea3dcdSIlya Dryomov 	insert_request(&osd->o_requests, req);
14135aea3dcdSIlya Dryomov 	req->r_osd = osd;
141435f9f8a0SSage Weil }
141535f9f8a0SSage Weil 
14165aea3dcdSIlya Dryomov static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
14173d14c5d2SYehuda Sadeh {
14185aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
14195aea3dcdSIlya Dryomov 	WARN_ON(req->r_osd != osd);
14205aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
14215aea3dcdSIlya Dryomov 	     req, req->r_tid);
14225aea3dcdSIlya Dryomov 
14235aea3dcdSIlya Dryomov 	req->r_osd = NULL;
14245aea3dcdSIlya Dryomov 	erase_request(&osd->o_requests, req);
14255aea3dcdSIlya Dryomov 	put_osd(osd);
14265aea3dcdSIlya Dryomov 
14275aea3dcdSIlya Dryomov 	if (!osd_homeless(osd))
14285aea3dcdSIlya Dryomov 		maybe_move_osd_to_lru(osd);
14295aea3dcdSIlya Dryomov 	else
14305aea3dcdSIlya Dryomov 		atomic_dec(&osd->o_osdc->num_homeless);
14313d14c5d2SYehuda Sadeh }
14323d14c5d2SYehuda Sadeh 
143363244fa1SIlya Dryomov static bool __pool_full(struct ceph_pg_pool_info *pi)
143463244fa1SIlya Dryomov {
143563244fa1SIlya Dryomov 	return pi->flags & CEPH_POOL_FLAG_FULL;
143663244fa1SIlya Dryomov }
143763244fa1SIlya Dryomov 
143842c1b124SIlya Dryomov static bool have_pool_full(struct ceph_osd_client *osdc)
143942c1b124SIlya Dryomov {
144042c1b124SIlya Dryomov 	struct rb_node *n;
144142c1b124SIlya Dryomov 
144242c1b124SIlya Dryomov 	for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
144342c1b124SIlya Dryomov 		struct ceph_pg_pool_info *pi =
144442c1b124SIlya Dryomov 		    rb_entry(n, struct ceph_pg_pool_info, node);
144542c1b124SIlya Dryomov 
144642c1b124SIlya Dryomov 		if (__pool_full(pi))
144742c1b124SIlya Dryomov 			return true;
144842c1b124SIlya Dryomov 	}
144942c1b124SIlya Dryomov 
145042c1b124SIlya Dryomov 	return false;
145142c1b124SIlya Dryomov }
145242c1b124SIlya Dryomov 
14535aea3dcdSIlya Dryomov static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
14545aea3dcdSIlya Dryomov {
14555aea3dcdSIlya Dryomov 	struct ceph_pg_pool_info *pi;
14565aea3dcdSIlya Dryomov 
14575aea3dcdSIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
14585aea3dcdSIlya Dryomov 	if (!pi)
14595aea3dcdSIlya Dryomov 		return false;
14605aea3dcdSIlya Dryomov 
14615aea3dcdSIlya Dryomov 	return __pool_full(pi);
14625aea3dcdSIlya Dryomov }
14635aea3dcdSIlya Dryomov 
14643d14c5d2SYehuda Sadeh /*
1465d29adb34SJosh Durgin  * Returns whether a request should be blocked from being sent
1466d29adb34SJosh Durgin  * based on the current osdmap and osd_client settings.
1467d29adb34SJosh Durgin  */
146863244fa1SIlya Dryomov static bool target_should_be_paused(struct ceph_osd_client *osdc,
146963244fa1SIlya Dryomov 				    const struct ceph_osd_request_target *t,
147063244fa1SIlya Dryomov 				    struct ceph_pg_pool_info *pi)
147163244fa1SIlya Dryomov {
1472b7ec35b3SIlya Dryomov 	bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1473b7ec35b3SIlya Dryomov 	bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1474b7ec35b3SIlya Dryomov 		       ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
147563244fa1SIlya Dryomov 		       __pool_full(pi);
147663244fa1SIlya Dryomov 
14776d637a54SIlya Dryomov 	WARN_ON(pi->id != t->target_oloc.pool);
147858eb7932SJeff Layton 	return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
147958eb7932SJeff Layton 	       ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
148058eb7932SJeff Layton 	       (osdc->osdmap->epoch < osdc->epoch_barrier);
148163244fa1SIlya Dryomov }
148263244fa1SIlya Dryomov 
148363244fa1SIlya Dryomov enum calc_target_result {
148463244fa1SIlya Dryomov 	CALC_TARGET_NO_ACTION = 0,
148563244fa1SIlya Dryomov 	CALC_TARGET_NEED_RESEND,
148663244fa1SIlya Dryomov 	CALC_TARGET_POOL_DNE,
148763244fa1SIlya Dryomov };
148863244fa1SIlya Dryomov 
148963244fa1SIlya Dryomov static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
149063244fa1SIlya Dryomov 					   struct ceph_osd_request_target *t,
14917de030d6SIlya Dryomov 					   struct ceph_connection *con,
149263244fa1SIlya Dryomov 					   bool any_change)
149363244fa1SIlya Dryomov {
149463244fa1SIlya Dryomov 	struct ceph_pg_pool_info *pi;
149563244fa1SIlya Dryomov 	struct ceph_pg pgid, last_pgid;
149663244fa1SIlya Dryomov 	struct ceph_osds up, acting;
149763244fa1SIlya Dryomov 	bool force_resend = false;
149884ed45dfSIlya Dryomov 	bool unpaused = false;
1499a5613724SIlya Dryomov 	bool legacy_change = false;
15007de030d6SIlya Dryomov 	bool split = false;
1501b7ec35b3SIlya Dryomov 	bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
1502ae78dd81SIlya Dryomov 	bool recovery_deletes = ceph_osdmap_flag(osdc,
1503ae78dd81SIlya Dryomov 						 CEPH_OSDMAP_RECOVERY_DELETES);
150463244fa1SIlya Dryomov 	enum calc_target_result ct_res;
150563244fa1SIlya Dryomov 
150604c7d789SIlya Dryomov 	t->epoch = osdc->osdmap->epoch;
150763244fa1SIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
150863244fa1SIlya Dryomov 	if (!pi) {
150963244fa1SIlya Dryomov 		t->osd = CEPH_HOMELESS_OSD;
151063244fa1SIlya Dryomov 		ct_res = CALC_TARGET_POOL_DNE;
151163244fa1SIlya Dryomov 		goto out;
151263244fa1SIlya Dryomov 	}
151363244fa1SIlya Dryomov 
151463244fa1SIlya Dryomov 	if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1515dc93e0e2SIlya Dryomov 		if (t->last_force_resend < pi->last_force_request_resend) {
1516dc93e0e2SIlya Dryomov 			t->last_force_resend = pi->last_force_request_resend;
151763244fa1SIlya Dryomov 			force_resend = true;
1518dc93e0e2SIlya Dryomov 		} else if (t->last_force_resend == 0) {
151963244fa1SIlya Dryomov 			force_resend = true;
152063244fa1SIlya Dryomov 		}
152163244fa1SIlya Dryomov 	}
152263244fa1SIlya Dryomov 
1523db098ec4SIlya Dryomov 	/* apply tiering */
1524db098ec4SIlya Dryomov 	ceph_oid_copy(&t->target_oid, &t->base_oid);
1525db098ec4SIlya Dryomov 	ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1526db098ec4SIlya Dryomov 	if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
152763244fa1SIlya Dryomov 		if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
152863244fa1SIlya Dryomov 			t->target_oloc.pool = pi->read_tier;
152963244fa1SIlya Dryomov 		if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
153063244fa1SIlya Dryomov 			t->target_oloc.pool = pi->write_tier;
15316d637a54SIlya Dryomov 
15326d637a54SIlya Dryomov 		pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
15336d637a54SIlya Dryomov 		if (!pi) {
15346d637a54SIlya Dryomov 			t->osd = CEPH_HOMELESS_OSD;
15356d637a54SIlya Dryomov 			ct_res = CALC_TARGET_POOL_DNE;
15366d637a54SIlya Dryomov 			goto out;
15376d637a54SIlya Dryomov 		}
153863244fa1SIlya Dryomov 	}
153963244fa1SIlya Dryomov 
1540a86f009fSIlya Dryomov 	__ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc, &pgid);
154163244fa1SIlya Dryomov 	last_pgid.pool = pgid.pool;
154263244fa1SIlya Dryomov 	last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
154363244fa1SIlya Dryomov 
1544df28152dSIlya Dryomov 	ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
154563244fa1SIlya Dryomov 	if (any_change &&
154663244fa1SIlya Dryomov 	    ceph_is_new_interval(&t->acting,
154763244fa1SIlya Dryomov 				 &acting,
154863244fa1SIlya Dryomov 				 &t->up,
154963244fa1SIlya Dryomov 				 &up,
155063244fa1SIlya Dryomov 				 t->size,
155163244fa1SIlya Dryomov 				 pi->size,
155263244fa1SIlya Dryomov 				 t->min_size,
155363244fa1SIlya Dryomov 				 pi->min_size,
155463244fa1SIlya Dryomov 				 t->pg_num,
155563244fa1SIlya Dryomov 				 pi->pg_num,
155663244fa1SIlya Dryomov 				 t->sort_bitwise,
155763244fa1SIlya Dryomov 				 sort_bitwise,
1558ae78dd81SIlya Dryomov 				 t->recovery_deletes,
1559ae78dd81SIlya Dryomov 				 recovery_deletes,
156063244fa1SIlya Dryomov 				 &last_pgid))
156163244fa1SIlya Dryomov 		force_resend = true;
156263244fa1SIlya Dryomov 
156363244fa1SIlya Dryomov 	if (t->paused && !target_should_be_paused(osdc, t, pi)) {
156463244fa1SIlya Dryomov 		t->paused = false;
156584ed45dfSIlya Dryomov 		unpaused = true;
156663244fa1SIlya Dryomov 	}
156784ed45dfSIlya Dryomov 	legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
156884ed45dfSIlya Dryomov 			ceph_osds_changed(&t->acting, &acting, any_change);
15697de030d6SIlya Dryomov 	if (t->pg_num)
15707de030d6SIlya Dryomov 		split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
157163244fa1SIlya Dryomov 
15727de030d6SIlya Dryomov 	if (legacy_change || force_resend || split) {
157363244fa1SIlya Dryomov 		t->pgid = pgid; /* struct */
1574df28152dSIlya Dryomov 		ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
157563244fa1SIlya Dryomov 		ceph_osds_copy(&t->acting, &acting);
157663244fa1SIlya Dryomov 		ceph_osds_copy(&t->up, &up);
157763244fa1SIlya Dryomov 		t->size = pi->size;
157863244fa1SIlya Dryomov 		t->min_size = pi->min_size;
157963244fa1SIlya Dryomov 		t->pg_num = pi->pg_num;
158063244fa1SIlya Dryomov 		t->pg_num_mask = pi->pg_num_mask;
158163244fa1SIlya Dryomov 		t->sort_bitwise = sort_bitwise;
1582ae78dd81SIlya Dryomov 		t->recovery_deletes = recovery_deletes;
158363244fa1SIlya Dryomov 
158463244fa1SIlya Dryomov 		t->osd = acting.primary;
158563244fa1SIlya Dryomov 	}
158663244fa1SIlya Dryomov 
1587a5613724SIlya Dryomov 	if (unpaused || legacy_change || force_resend || split)
158884ed45dfSIlya Dryomov 		ct_res = CALC_TARGET_NEED_RESEND;
158984ed45dfSIlya Dryomov 	else
159084ed45dfSIlya Dryomov 		ct_res = CALC_TARGET_NO_ACTION;
159184ed45dfSIlya Dryomov 
159263244fa1SIlya Dryomov out:
1593a5613724SIlya Dryomov 	dout("%s t %p -> %d%d%d%d ct_res %d osd%d\n", __func__, t, unpaused,
1594a5613724SIlya Dryomov 	     legacy_change, force_resend, split, ct_res, t->osd);
159563244fa1SIlya Dryomov 	return ct_res;
159663244fa1SIlya Dryomov }
159763244fa1SIlya Dryomov 
1598a02a946dSIlya Dryomov static struct ceph_spg_mapping *alloc_spg_mapping(void)
1599a02a946dSIlya Dryomov {
1600a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
1601a02a946dSIlya Dryomov 
1602a02a946dSIlya Dryomov 	spg = kmalloc(sizeof(*spg), GFP_NOIO);
1603a02a946dSIlya Dryomov 	if (!spg)
1604a02a946dSIlya Dryomov 		return NULL;
1605a02a946dSIlya Dryomov 
1606a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&spg->node);
1607a02a946dSIlya Dryomov 	spg->backoffs = RB_ROOT;
1608a02a946dSIlya Dryomov 	return spg;
1609a02a946dSIlya Dryomov }
1610a02a946dSIlya Dryomov 
1611a02a946dSIlya Dryomov static void free_spg_mapping(struct ceph_spg_mapping *spg)
1612a02a946dSIlya Dryomov {
1613a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&spg->node));
1614a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
1615a02a946dSIlya Dryomov 
1616a02a946dSIlya Dryomov 	kfree(spg);
1617a02a946dSIlya Dryomov }
1618a02a946dSIlya Dryomov 
1619a02a946dSIlya Dryomov /*
1620a02a946dSIlya Dryomov  * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
1621a02a946dSIlya Dryomov  * ceph_pg_mapping.  Used to track OSD backoffs -- a backoff [range] is
1622a02a946dSIlya Dryomov  * defined only within a specific spgid; it does not pass anything to
1623a02a946dSIlya Dryomov  * children on split, or to another primary.
1624a02a946dSIlya Dryomov  */
1625a02a946dSIlya Dryomov DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
1626a02a946dSIlya Dryomov 		 RB_BYPTR, const struct ceph_spg *, node)
1627a02a946dSIlya Dryomov 
1628a02a946dSIlya Dryomov static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
1629a02a946dSIlya Dryomov {
1630a02a946dSIlya Dryomov 	return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
1631a02a946dSIlya Dryomov }
1632a02a946dSIlya Dryomov 
1633a02a946dSIlya Dryomov static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
1634a02a946dSIlya Dryomov 				   void **pkey, size_t *pkey_len)
1635a02a946dSIlya Dryomov {
1636a02a946dSIlya Dryomov 	if (hoid->key_len) {
1637a02a946dSIlya Dryomov 		*pkey = hoid->key;
1638a02a946dSIlya Dryomov 		*pkey_len = hoid->key_len;
1639a02a946dSIlya Dryomov 	} else {
1640a02a946dSIlya Dryomov 		*pkey = hoid->oid;
1641a02a946dSIlya Dryomov 		*pkey_len = hoid->oid_len;
1642a02a946dSIlya Dryomov 	}
1643a02a946dSIlya Dryomov }
1644a02a946dSIlya Dryomov 
1645a02a946dSIlya Dryomov static int compare_names(const void *name1, size_t name1_len,
1646a02a946dSIlya Dryomov 			 const void *name2, size_t name2_len)
1647a02a946dSIlya Dryomov {
1648a02a946dSIlya Dryomov 	int ret;
1649a02a946dSIlya Dryomov 
1650a02a946dSIlya Dryomov 	ret = memcmp(name1, name2, min(name1_len, name2_len));
1651a02a946dSIlya Dryomov 	if (!ret) {
1652a02a946dSIlya Dryomov 		if (name1_len < name2_len)
1653a02a946dSIlya Dryomov 			ret = -1;
1654a02a946dSIlya Dryomov 		else if (name1_len > name2_len)
1655a02a946dSIlya Dryomov 			ret = 1;
1656a02a946dSIlya Dryomov 	}
1657a02a946dSIlya Dryomov 	return ret;
1658a02a946dSIlya Dryomov }
1659a02a946dSIlya Dryomov 
1660a02a946dSIlya Dryomov static int hoid_compare(const struct ceph_hobject_id *lhs,
1661a02a946dSIlya Dryomov 			const struct ceph_hobject_id *rhs)
1662a02a946dSIlya Dryomov {
1663a02a946dSIlya Dryomov 	void *effective_key1, *effective_key2;
1664a02a946dSIlya Dryomov 	size_t effective_key1_len, effective_key2_len;
1665a02a946dSIlya Dryomov 	int ret;
1666a02a946dSIlya Dryomov 
1667a02a946dSIlya Dryomov 	if (lhs->is_max < rhs->is_max)
1668a02a946dSIlya Dryomov 		return -1;
1669a02a946dSIlya Dryomov 	if (lhs->is_max > rhs->is_max)
1670a02a946dSIlya Dryomov 		return 1;
1671a02a946dSIlya Dryomov 
1672a02a946dSIlya Dryomov 	if (lhs->pool < rhs->pool)
1673a02a946dSIlya Dryomov 		return -1;
1674a02a946dSIlya Dryomov 	if (lhs->pool > rhs->pool)
1675a02a946dSIlya Dryomov 		return 1;
1676a02a946dSIlya Dryomov 
1677a02a946dSIlya Dryomov 	if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
1678a02a946dSIlya Dryomov 		return -1;
1679a02a946dSIlya Dryomov 	if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
1680a02a946dSIlya Dryomov 		return 1;
1681a02a946dSIlya Dryomov 
1682a02a946dSIlya Dryomov 	ret = compare_names(lhs->nspace, lhs->nspace_len,
1683a02a946dSIlya Dryomov 			    rhs->nspace, rhs->nspace_len);
1684a02a946dSIlya Dryomov 	if (ret)
1685a02a946dSIlya Dryomov 		return ret;
1686a02a946dSIlya Dryomov 
1687a02a946dSIlya Dryomov 	hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
1688a02a946dSIlya Dryomov 	hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
1689a02a946dSIlya Dryomov 	ret = compare_names(effective_key1, effective_key1_len,
1690a02a946dSIlya Dryomov 			    effective_key2, effective_key2_len);
1691a02a946dSIlya Dryomov 	if (ret)
1692a02a946dSIlya Dryomov 		return ret;
1693a02a946dSIlya Dryomov 
1694a02a946dSIlya Dryomov 	ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
1695a02a946dSIlya Dryomov 	if (ret)
1696a02a946dSIlya Dryomov 		return ret;
1697a02a946dSIlya Dryomov 
1698a02a946dSIlya Dryomov 	if (lhs->snapid < rhs->snapid)
1699a02a946dSIlya Dryomov 		return -1;
1700a02a946dSIlya Dryomov 	if (lhs->snapid > rhs->snapid)
1701a02a946dSIlya Dryomov 		return 1;
1702a02a946dSIlya Dryomov 
1703a02a946dSIlya Dryomov 	return 0;
1704a02a946dSIlya Dryomov }
1705a02a946dSIlya Dryomov 
1706a02a946dSIlya Dryomov /*
1707a02a946dSIlya Dryomov  * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
1708a02a946dSIlya Dryomov  * compat stuff here.
1709a02a946dSIlya Dryomov  *
1710a02a946dSIlya Dryomov  * Assumes @hoid is zero-initialized.
1711a02a946dSIlya Dryomov  */
1712a02a946dSIlya Dryomov static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
1713a02a946dSIlya Dryomov {
1714a02a946dSIlya Dryomov 	u8 struct_v;
1715a02a946dSIlya Dryomov 	u32 struct_len;
1716a02a946dSIlya Dryomov 	int ret;
1717a02a946dSIlya Dryomov 
1718a02a946dSIlya Dryomov 	ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
1719a02a946dSIlya Dryomov 				  &struct_len);
1720a02a946dSIlya Dryomov 	if (ret)
1721a02a946dSIlya Dryomov 		return ret;
1722a02a946dSIlya Dryomov 
1723a02a946dSIlya Dryomov 	if (struct_v < 4) {
1724a02a946dSIlya Dryomov 		pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
1725a02a946dSIlya Dryomov 		goto e_inval;
1726a02a946dSIlya Dryomov 	}
1727a02a946dSIlya Dryomov 
1728a02a946dSIlya Dryomov 	hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
1729a02a946dSIlya Dryomov 						GFP_NOIO);
1730a02a946dSIlya Dryomov 	if (IS_ERR(hoid->key)) {
1731a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->key);
1732a02a946dSIlya Dryomov 		hoid->key = NULL;
1733a02a946dSIlya Dryomov 		return ret;
1734a02a946dSIlya Dryomov 	}
1735a02a946dSIlya Dryomov 
1736a02a946dSIlya Dryomov 	hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
1737a02a946dSIlya Dryomov 						GFP_NOIO);
1738a02a946dSIlya Dryomov 	if (IS_ERR(hoid->oid)) {
1739a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->oid);
1740a02a946dSIlya Dryomov 		hoid->oid = NULL;
1741a02a946dSIlya Dryomov 		return ret;
1742a02a946dSIlya Dryomov 	}
1743a02a946dSIlya Dryomov 
1744a02a946dSIlya Dryomov 	ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
1745a02a946dSIlya Dryomov 	ceph_decode_32_safe(p, end, hoid->hash, e_inval);
1746a02a946dSIlya Dryomov 	ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
1747a02a946dSIlya Dryomov 
1748a02a946dSIlya Dryomov 	hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
1749a02a946dSIlya Dryomov 						   GFP_NOIO);
1750a02a946dSIlya Dryomov 	if (IS_ERR(hoid->nspace)) {
1751a02a946dSIlya Dryomov 		ret = PTR_ERR(hoid->nspace);
1752a02a946dSIlya Dryomov 		hoid->nspace = NULL;
1753a02a946dSIlya Dryomov 		return ret;
1754a02a946dSIlya Dryomov 	}
1755a02a946dSIlya Dryomov 
1756a02a946dSIlya Dryomov 	ceph_decode_64_safe(p, end, hoid->pool, e_inval);
1757a02a946dSIlya Dryomov 
1758a02a946dSIlya Dryomov 	ceph_hoid_build_hash_cache(hoid);
1759a02a946dSIlya Dryomov 	return 0;
1760a02a946dSIlya Dryomov 
1761a02a946dSIlya Dryomov e_inval:
1762a02a946dSIlya Dryomov 	return -EINVAL;
1763a02a946dSIlya Dryomov }
1764a02a946dSIlya Dryomov 
1765a02a946dSIlya Dryomov static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
1766a02a946dSIlya Dryomov {
1767a02a946dSIlya Dryomov 	return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
1768a02a946dSIlya Dryomov 	       4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
1769a02a946dSIlya Dryomov }
1770a02a946dSIlya Dryomov 
1771a02a946dSIlya Dryomov static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
1772a02a946dSIlya Dryomov {
1773a02a946dSIlya Dryomov 	ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
1774a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->key, hoid->key_len);
1775a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
1776a02a946dSIlya Dryomov 	ceph_encode_64(p, hoid->snapid);
1777a02a946dSIlya Dryomov 	ceph_encode_32(p, hoid->hash);
1778a02a946dSIlya Dryomov 	ceph_encode_8(p, hoid->is_max);
1779a02a946dSIlya Dryomov 	ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
1780a02a946dSIlya Dryomov 	ceph_encode_64(p, hoid->pool);
1781a02a946dSIlya Dryomov }
1782a02a946dSIlya Dryomov 
1783a02a946dSIlya Dryomov static void free_hoid(struct ceph_hobject_id *hoid)
1784a02a946dSIlya Dryomov {
1785a02a946dSIlya Dryomov 	if (hoid) {
1786a02a946dSIlya Dryomov 		kfree(hoid->key);
1787a02a946dSIlya Dryomov 		kfree(hoid->oid);
1788a02a946dSIlya Dryomov 		kfree(hoid->nspace);
1789a02a946dSIlya Dryomov 		kfree(hoid);
1790a02a946dSIlya Dryomov 	}
1791a02a946dSIlya Dryomov }
1792a02a946dSIlya Dryomov 
1793a02a946dSIlya Dryomov static struct ceph_osd_backoff *alloc_backoff(void)
1794a02a946dSIlya Dryomov {
1795a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
1796a02a946dSIlya Dryomov 
1797a02a946dSIlya Dryomov 	backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
1798a02a946dSIlya Dryomov 	if (!backoff)
1799a02a946dSIlya Dryomov 		return NULL;
1800a02a946dSIlya Dryomov 
1801a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&backoff->spg_node);
1802a02a946dSIlya Dryomov 	RB_CLEAR_NODE(&backoff->id_node);
1803a02a946dSIlya Dryomov 	return backoff;
1804a02a946dSIlya Dryomov }
1805a02a946dSIlya Dryomov 
1806a02a946dSIlya Dryomov static void free_backoff(struct ceph_osd_backoff *backoff)
1807a02a946dSIlya Dryomov {
1808a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
1809a02a946dSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
1810a02a946dSIlya Dryomov 
1811a02a946dSIlya Dryomov 	free_hoid(backoff->begin);
1812a02a946dSIlya Dryomov 	free_hoid(backoff->end);
1813a02a946dSIlya Dryomov 	kfree(backoff);
1814a02a946dSIlya Dryomov }
1815a02a946dSIlya Dryomov 
1816a02a946dSIlya Dryomov /*
1817a02a946dSIlya Dryomov  * Within a specific spgid, backoffs are managed by ->begin hoid.
1818a02a946dSIlya Dryomov  */
1819a02a946dSIlya Dryomov DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
1820a02a946dSIlya Dryomov 			RB_BYVAL, spg_node);
1821a02a946dSIlya Dryomov 
1822a02a946dSIlya Dryomov static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
1823a02a946dSIlya Dryomov 					    const struct ceph_hobject_id *hoid)
1824a02a946dSIlya Dryomov {
1825a02a946dSIlya Dryomov 	struct rb_node *n = root->rb_node;
1826a02a946dSIlya Dryomov 
1827a02a946dSIlya Dryomov 	while (n) {
1828a02a946dSIlya Dryomov 		struct ceph_osd_backoff *cur =
1829a02a946dSIlya Dryomov 		    rb_entry(n, struct ceph_osd_backoff, spg_node);
1830a02a946dSIlya Dryomov 		int cmp;
1831a02a946dSIlya Dryomov 
1832a02a946dSIlya Dryomov 		cmp = hoid_compare(hoid, cur->begin);
1833a02a946dSIlya Dryomov 		if (cmp < 0) {
1834a02a946dSIlya Dryomov 			n = n->rb_left;
1835a02a946dSIlya Dryomov 		} else if (cmp > 0) {
1836a02a946dSIlya Dryomov 			if (hoid_compare(hoid, cur->end) < 0)
1837a02a946dSIlya Dryomov 				return cur;
1838a02a946dSIlya Dryomov 
1839a02a946dSIlya Dryomov 			n = n->rb_right;
1840a02a946dSIlya Dryomov 		} else {
1841a02a946dSIlya Dryomov 			return cur;
1842a02a946dSIlya Dryomov 		}
1843a02a946dSIlya Dryomov 	}
1844a02a946dSIlya Dryomov 
1845a02a946dSIlya Dryomov 	return NULL;
1846a02a946dSIlya Dryomov }
1847a02a946dSIlya Dryomov 
1848a02a946dSIlya Dryomov /*
1849a02a946dSIlya Dryomov  * Each backoff has a unique id within its OSD session.
1850a02a946dSIlya Dryomov  */
1851a02a946dSIlya Dryomov DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
1852a02a946dSIlya Dryomov 
1853a02a946dSIlya Dryomov static void clear_backoffs(struct ceph_osd *osd)
1854a02a946dSIlya Dryomov {
1855a02a946dSIlya Dryomov 	while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
1856a02a946dSIlya Dryomov 		struct ceph_spg_mapping *spg =
1857a02a946dSIlya Dryomov 		    rb_entry(rb_first(&osd->o_backoff_mappings),
1858a02a946dSIlya Dryomov 			     struct ceph_spg_mapping, node);
1859a02a946dSIlya Dryomov 
1860a02a946dSIlya Dryomov 		while (!RB_EMPTY_ROOT(&spg->backoffs)) {
1861a02a946dSIlya Dryomov 			struct ceph_osd_backoff *backoff =
1862a02a946dSIlya Dryomov 			    rb_entry(rb_first(&spg->backoffs),
1863a02a946dSIlya Dryomov 				     struct ceph_osd_backoff, spg_node);
1864a02a946dSIlya Dryomov 
1865a02a946dSIlya Dryomov 			erase_backoff(&spg->backoffs, backoff);
1866a02a946dSIlya Dryomov 			erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
1867a02a946dSIlya Dryomov 			free_backoff(backoff);
1868a02a946dSIlya Dryomov 		}
1869a02a946dSIlya Dryomov 		erase_spg_mapping(&osd->o_backoff_mappings, spg);
1870a02a946dSIlya Dryomov 		free_spg_mapping(spg);
1871a02a946dSIlya Dryomov 	}
1872a02a946dSIlya Dryomov }
1873a02a946dSIlya Dryomov 
1874a02a946dSIlya Dryomov /*
1875a02a946dSIlya Dryomov  * Set up a temporary, non-owning view into @t.
1876a02a946dSIlya Dryomov  */
1877a02a946dSIlya Dryomov static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
1878a02a946dSIlya Dryomov 				  const struct ceph_osd_request_target *t)
1879a02a946dSIlya Dryomov {
1880a02a946dSIlya Dryomov 	hoid->key = NULL;
1881a02a946dSIlya Dryomov 	hoid->key_len = 0;
1882a02a946dSIlya Dryomov 	hoid->oid = t->target_oid.name;
1883a02a946dSIlya Dryomov 	hoid->oid_len = t->target_oid.name_len;
1884a02a946dSIlya Dryomov 	hoid->snapid = CEPH_NOSNAP;
1885a02a946dSIlya Dryomov 	hoid->hash = t->pgid.seed;
1886a02a946dSIlya Dryomov 	hoid->is_max = false;
1887a02a946dSIlya Dryomov 	if (t->target_oloc.pool_ns) {
1888a02a946dSIlya Dryomov 		hoid->nspace = t->target_oloc.pool_ns->str;
1889a02a946dSIlya Dryomov 		hoid->nspace_len = t->target_oloc.pool_ns->len;
1890a02a946dSIlya Dryomov 	} else {
1891a02a946dSIlya Dryomov 		hoid->nspace = NULL;
1892a02a946dSIlya Dryomov 		hoid->nspace_len = 0;
1893a02a946dSIlya Dryomov 	}
1894a02a946dSIlya Dryomov 	hoid->pool = t->target_oloc.pool;
1895a02a946dSIlya Dryomov 	ceph_hoid_build_hash_cache(hoid);
1896a02a946dSIlya Dryomov }
1897a02a946dSIlya Dryomov 
1898a02a946dSIlya Dryomov static bool should_plug_request(struct ceph_osd_request *req)
1899a02a946dSIlya Dryomov {
1900a02a946dSIlya Dryomov 	struct ceph_osd *osd = req->r_osd;
1901a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
1902a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
1903a02a946dSIlya Dryomov 	struct ceph_hobject_id hoid;
1904a02a946dSIlya Dryomov 
1905a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
1906a02a946dSIlya Dryomov 	if (!spg)
1907a02a946dSIlya Dryomov 		return false;
1908a02a946dSIlya Dryomov 
1909a02a946dSIlya Dryomov 	hoid_fill_from_target(&hoid, &req->r_t);
1910a02a946dSIlya Dryomov 	backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
1911a02a946dSIlya Dryomov 	if (!backoff)
1912a02a946dSIlya Dryomov 		return false;
1913a02a946dSIlya Dryomov 
1914a02a946dSIlya Dryomov 	dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
1915a02a946dSIlya Dryomov 	     __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
1916a02a946dSIlya Dryomov 	     backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
1917a02a946dSIlya Dryomov 	return true;
1918a02a946dSIlya Dryomov }
1919a02a946dSIlya Dryomov 
19200d9c1ab3SIlya Dryomov /*
19210d9c1ab3SIlya Dryomov  * Keep get_num_data_items() in sync with this function.
19220d9c1ab3SIlya Dryomov  */
192398c4bfe9SIlya Dryomov static void setup_request_data(struct ceph_osd_request *req)
19243d14c5d2SYehuda Sadeh {
192598c4bfe9SIlya Dryomov 	struct ceph_msg *request_msg = req->r_request;
192698c4bfe9SIlya Dryomov 	struct ceph_msg *reply_msg = req->r_reply;
192798c4bfe9SIlya Dryomov 	struct ceph_osd_req_op *op;
19283d14c5d2SYehuda Sadeh 
192998c4bfe9SIlya Dryomov 	if (req->r_request->num_data_items || req->r_reply->num_data_items)
1930bb873b53SIlya Dryomov 		return;
19313d14c5d2SYehuda Sadeh 
193298c4bfe9SIlya Dryomov 	WARN_ON(request_msg->data_length || reply_msg->data_length);
193398c4bfe9SIlya Dryomov 	for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
1934bb873b53SIlya Dryomov 		switch (op->op) {
1935bb873b53SIlya Dryomov 		/* request */
1936bb873b53SIlya Dryomov 		case CEPH_OSD_OP_WRITE:
1937bb873b53SIlya Dryomov 		case CEPH_OSD_OP_WRITEFULL:
1938bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->extent.length);
193998c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
194098c4bfe9SIlya Dryomov 					       &op->extent.osd_data);
1941bb873b53SIlya Dryomov 			break;
1942bb873b53SIlya Dryomov 		case CEPH_OSD_OP_SETXATTR:
1943bb873b53SIlya Dryomov 		case CEPH_OSD_OP_CMPXATTR:
1944bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->xattr.name_len +
1945bb873b53SIlya Dryomov 						  op->xattr.value_len);
194698c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
194798c4bfe9SIlya Dryomov 					       &op->xattr.osd_data);
1948bb873b53SIlya Dryomov 			break;
1949922dab61SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY_ACK:
195098c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
1951922dab61SIlya Dryomov 					       &op->notify_ack.request_data);
1952922dab61SIlya Dryomov 			break;
195323ddf9beSLuis Henriques 		case CEPH_OSD_OP_COPY_FROM:
195423ddf9beSLuis Henriques 			ceph_osdc_msg_data_add(request_msg,
195523ddf9beSLuis Henriques 					       &op->copy_from.osd_data);
195623ddf9beSLuis Henriques 			break;
1957bb873b53SIlya Dryomov 
1958bb873b53SIlya Dryomov 		/* reply */
1959bb873b53SIlya Dryomov 		case CEPH_OSD_OP_STAT:
196098c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
1961bb873b53SIlya Dryomov 					       &op->raw_data_in);
1962bb873b53SIlya Dryomov 			break;
1963bb873b53SIlya Dryomov 		case CEPH_OSD_OP_READ:
196498c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
1965bb873b53SIlya Dryomov 					       &op->extent.osd_data);
1966bb873b53SIlya Dryomov 			break;
1967a4ed38d7SDouglas Fuller 		case CEPH_OSD_OP_LIST_WATCHERS:
196898c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
1969a4ed38d7SDouglas Fuller 					       &op->list_watchers.response_data);
1970a4ed38d7SDouglas Fuller 			break;
1971bb873b53SIlya Dryomov 
1972bb873b53SIlya Dryomov 		/* both */
1973bb873b53SIlya Dryomov 		case CEPH_OSD_OP_CALL:
1974bb873b53SIlya Dryomov 			WARN_ON(op->indata_len != op->cls.class_len +
1975bb873b53SIlya Dryomov 						  op->cls.method_len +
1976bb873b53SIlya Dryomov 						  op->cls.indata_len);
197798c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
197898c4bfe9SIlya Dryomov 					       &op->cls.request_info);
1979bb873b53SIlya Dryomov 			/* optional, can be NONE */
198098c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
198198c4bfe9SIlya Dryomov 					       &op->cls.request_data);
1982bb873b53SIlya Dryomov 			/* optional, can be NONE */
198398c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
1984bb873b53SIlya Dryomov 					       &op->cls.response_data);
1985bb873b53SIlya Dryomov 			break;
198619079203SIlya Dryomov 		case CEPH_OSD_OP_NOTIFY:
198798c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(request_msg,
198819079203SIlya Dryomov 					       &op->notify.request_data);
198998c4bfe9SIlya Dryomov 			ceph_osdc_msg_data_add(reply_msg,
199019079203SIlya Dryomov 					       &op->notify.response_data);
199119079203SIlya Dryomov 			break;
1992bb873b53SIlya Dryomov 		}
1993bb873b53SIlya Dryomov 	}
1994bb873b53SIlya Dryomov }
1995bb873b53SIlya Dryomov 
19962e59ffd1SIlya Dryomov static void encode_pgid(void **p, const struct ceph_pg *pgid)
19972e59ffd1SIlya Dryomov {
19982e59ffd1SIlya Dryomov 	ceph_encode_8(p, 1);
19992e59ffd1SIlya Dryomov 	ceph_encode_64(p, pgid->pool);
20002e59ffd1SIlya Dryomov 	ceph_encode_32(p, pgid->seed);
20012e59ffd1SIlya Dryomov 	ceph_encode_32(p, -1); /* preferred */
20022e59ffd1SIlya Dryomov }
20032e59ffd1SIlya Dryomov 
20048cb441c0SIlya Dryomov static void encode_spgid(void **p, const struct ceph_spg *spgid)
20058cb441c0SIlya Dryomov {
20068cb441c0SIlya Dryomov 	ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
20078cb441c0SIlya Dryomov 	encode_pgid(p, &spgid->pgid);
20088cb441c0SIlya Dryomov 	ceph_encode_8(p, spgid->shard);
20098cb441c0SIlya Dryomov }
20108cb441c0SIlya Dryomov 
20112e59ffd1SIlya Dryomov static void encode_oloc(void **p, void *end,
20122e59ffd1SIlya Dryomov 			const struct ceph_object_locator *oloc)
20132e59ffd1SIlya Dryomov {
20142e59ffd1SIlya Dryomov 	ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
20152e59ffd1SIlya Dryomov 	ceph_encode_64(p, oloc->pool);
20162e59ffd1SIlya Dryomov 	ceph_encode_32(p, -1); /* preferred */
20172e59ffd1SIlya Dryomov 	ceph_encode_32(p, 0);  /* key len */
20182e59ffd1SIlya Dryomov 	if (oloc->pool_ns)
20192e59ffd1SIlya Dryomov 		ceph_encode_string(p, end, oloc->pool_ns->str,
20202e59ffd1SIlya Dryomov 				   oloc->pool_ns->len);
20212e59ffd1SIlya Dryomov 	else
20222e59ffd1SIlya Dryomov 		ceph_encode_32(p, 0);
20232e59ffd1SIlya Dryomov }
20242e59ffd1SIlya Dryomov 
20258cb441c0SIlya Dryomov static void encode_request_partial(struct ceph_osd_request *req,
20268cb441c0SIlya Dryomov 				   struct ceph_msg *msg)
2027bb873b53SIlya Dryomov {
2028bb873b53SIlya Dryomov 	void *p = msg->front.iov_base;
2029bb873b53SIlya Dryomov 	void *const end = p + msg->front_alloc_len;
2030bb873b53SIlya Dryomov 	u32 data_len = 0;
2031bb873b53SIlya Dryomov 	int i;
2032bb873b53SIlya Dryomov 
2033bb873b53SIlya Dryomov 	if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
2034bb873b53SIlya Dryomov 		/* snapshots aren't writeable */
2035bb873b53SIlya Dryomov 		WARN_ON(req->r_snapid != CEPH_NOSNAP);
2036bb873b53SIlya Dryomov 	} else {
2037bb873b53SIlya Dryomov 		WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
2038bb873b53SIlya Dryomov 			req->r_data_offset || req->r_snapc);
2039bb873b53SIlya Dryomov 	}
2040bb873b53SIlya Dryomov 
204198c4bfe9SIlya Dryomov 	setup_request_data(req);
2042bb873b53SIlya Dryomov 
20438cb441c0SIlya Dryomov 	encode_spgid(&p, &req->r_t.spgid); /* actual spg */
20448cb441c0SIlya Dryomov 	ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
2045bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
2046bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_flags);
20478cb441c0SIlya Dryomov 
20488cb441c0SIlya Dryomov 	/* reqid */
20498cb441c0SIlya Dryomov 	ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
20508cb441c0SIlya Dryomov 	memset(p, 0, sizeof(struct ceph_osd_reqid));
20518cb441c0SIlya Dryomov 	p += sizeof(struct ceph_osd_reqid);
20528cb441c0SIlya Dryomov 
20538cb441c0SIlya Dryomov 	/* trace */
20548cb441c0SIlya Dryomov 	memset(p, 0, sizeof(struct ceph_blkin_trace_info));
20558cb441c0SIlya Dryomov 	p += sizeof(struct ceph_blkin_trace_info);
20568cb441c0SIlya Dryomov 
20578cb441c0SIlya Dryomov 	ceph_encode_32(&p, 0); /* client_inc, always 0 */
2058fac02ddfSArnd Bergmann 	ceph_encode_timespec64(p, &req->r_mtime);
2059bb873b53SIlya Dryomov 	p += sizeof(struct ceph_timespec);
2060aa26d662SJeff Layton 
20612e59ffd1SIlya Dryomov 	encode_oloc(&p, end, &req->r_t.target_oloc);
20622e59ffd1SIlya Dryomov 	ceph_encode_string(&p, end, req->r_t.target_oid.name,
20632e59ffd1SIlya Dryomov 			   req->r_t.target_oid.name_len);
2064bb873b53SIlya Dryomov 
2065bb873b53SIlya Dryomov 	/* ops, can imply data */
2066bb873b53SIlya Dryomov 	ceph_encode_16(&p, req->r_num_ops);
2067bb873b53SIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
2068bb873b53SIlya Dryomov 		data_len += osd_req_encode_op(p, &req->r_ops[i]);
2069bb873b53SIlya Dryomov 		p += sizeof(struct ceph_osd_op);
2070bb873b53SIlya Dryomov 	}
2071bb873b53SIlya Dryomov 
2072bb873b53SIlya Dryomov 	ceph_encode_64(&p, req->r_snapid); /* snapid */
2073bb873b53SIlya Dryomov 	if (req->r_snapc) {
2074bb873b53SIlya Dryomov 		ceph_encode_64(&p, req->r_snapc->seq);
2075bb873b53SIlya Dryomov 		ceph_encode_32(&p, req->r_snapc->num_snaps);
2076bb873b53SIlya Dryomov 		for (i = 0; i < req->r_snapc->num_snaps; i++)
2077bb873b53SIlya Dryomov 			ceph_encode_64(&p, req->r_snapc->snaps[i]);
2078bb873b53SIlya Dryomov 	} else {
2079bb873b53SIlya Dryomov 		ceph_encode_64(&p, 0); /* snap_seq */
2080bb873b53SIlya Dryomov 		ceph_encode_32(&p, 0); /* snaps len */
2081bb873b53SIlya Dryomov 	}
2082bb873b53SIlya Dryomov 
2083bb873b53SIlya Dryomov 	ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
2084986e8989SIlya Dryomov 	BUG_ON(p > end - 8); /* space for features */
2085bb873b53SIlya Dryomov 
20868cb441c0SIlya Dryomov 	msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
20878cb441c0SIlya Dryomov 	/* front_len is finalized in encode_request_finish() */
2088986e8989SIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
2089986e8989SIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2090bb873b53SIlya Dryomov 	msg->hdr.data_len = cpu_to_le32(data_len);
2091bb873b53SIlya Dryomov 	/*
2092bb873b53SIlya Dryomov 	 * The header "data_off" is a hint to the receiver allowing it
2093bb873b53SIlya Dryomov 	 * to align received data into its buffers such that there's no
2094bb873b53SIlya Dryomov 	 * need to re-copy it before writing it to disk (direct I/O).
2095bb873b53SIlya Dryomov 	 */
2096bb873b53SIlya Dryomov 	msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
2097bb873b53SIlya Dryomov 
20988cb441c0SIlya Dryomov 	dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
20998cb441c0SIlya Dryomov 	     req->r_t.target_oid.name, req->r_t.target_oid.name_len);
21008cb441c0SIlya Dryomov }
21018cb441c0SIlya Dryomov 
21028cb441c0SIlya Dryomov static void encode_request_finish(struct ceph_msg *msg)
21038cb441c0SIlya Dryomov {
21048cb441c0SIlya Dryomov 	void *p = msg->front.iov_base;
2105986e8989SIlya Dryomov 	void *const partial_end = p + msg->front.iov_len;
21068cb441c0SIlya Dryomov 	void *const end = p + msg->front_alloc_len;
21078cb441c0SIlya Dryomov 
21088cb441c0SIlya Dryomov 	if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
21098cb441c0SIlya Dryomov 		/* luminous OSD -- encode features and be done */
2110986e8989SIlya Dryomov 		p = partial_end;
21118cb441c0SIlya Dryomov 		ceph_encode_64(&p, msg->con->peer_features);
21128cb441c0SIlya Dryomov 	} else {
21138cb441c0SIlya Dryomov 		struct {
21148cb441c0SIlya Dryomov 			char spgid[CEPH_ENCODING_START_BLK_LEN +
21158cb441c0SIlya Dryomov 				   CEPH_PGID_ENCODING_LEN + 1];
21168cb441c0SIlya Dryomov 			__le32 hash;
21178cb441c0SIlya Dryomov 			__le32 epoch;
21188cb441c0SIlya Dryomov 			__le32 flags;
21198cb441c0SIlya Dryomov 			char reqid[CEPH_ENCODING_START_BLK_LEN +
21208cb441c0SIlya Dryomov 				   sizeof(struct ceph_osd_reqid)];
21218cb441c0SIlya Dryomov 			char trace[sizeof(struct ceph_blkin_trace_info)];
21228cb441c0SIlya Dryomov 			__le32 client_inc;
21238cb441c0SIlya Dryomov 			struct ceph_timespec mtime;
21248cb441c0SIlya Dryomov 		} __packed head;
21258cb441c0SIlya Dryomov 		struct ceph_pg pgid;
21268cb441c0SIlya Dryomov 		void *oloc, *oid, *tail;
21278cb441c0SIlya Dryomov 		int oloc_len, oid_len, tail_len;
21288cb441c0SIlya Dryomov 		int len;
21298cb441c0SIlya Dryomov 
21308cb441c0SIlya Dryomov 		/*
21318cb441c0SIlya Dryomov 		 * Pre-luminous OSD -- reencode v8 into v4 using @head
21328cb441c0SIlya Dryomov 		 * as a temporary buffer.  Encode the raw PG; the rest
21338cb441c0SIlya Dryomov 		 * is just a matter of moving oloc, oid and tail blobs
21348cb441c0SIlya Dryomov 		 * around.
21358cb441c0SIlya Dryomov 		 */
21368cb441c0SIlya Dryomov 		memcpy(&head, p, sizeof(head));
21378cb441c0SIlya Dryomov 		p += sizeof(head);
21388cb441c0SIlya Dryomov 
21398cb441c0SIlya Dryomov 		oloc = p;
21408cb441c0SIlya Dryomov 		p += CEPH_ENCODING_START_BLK_LEN;
21418cb441c0SIlya Dryomov 		pgid.pool = ceph_decode_64(&p);
21428cb441c0SIlya Dryomov 		p += 4 + 4; /* preferred, key len */
21438cb441c0SIlya Dryomov 		len = ceph_decode_32(&p);
21448cb441c0SIlya Dryomov 		p += len;   /* nspace */
21458cb441c0SIlya Dryomov 		oloc_len = p - oloc;
21468cb441c0SIlya Dryomov 
21478cb441c0SIlya Dryomov 		oid = p;
21488cb441c0SIlya Dryomov 		len = ceph_decode_32(&p);
21498cb441c0SIlya Dryomov 		p += len;
21508cb441c0SIlya Dryomov 		oid_len = p - oid;
21518cb441c0SIlya Dryomov 
21528cb441c0SIlya Dryomov 		tail = p;
2153986e8989SIlya Dryomov 		tail_len = partial_end - p;
21548cb441c0SIlya Dryomov 
21558cb441c0SIlya Dryomov 		p = msg->front.iov_base;
21568cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
21578cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
21588cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
21598cb441c0SIlya Dryomov 		ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
21608cb441c0SIlya Dryomov 
21618cb441c0SIlya Dryomov 		/* reassert_version */
21628cb441c0SIlya Dryomov 		memset(p, 0, sizeof(struct ceph_eversion));
21638cb441c0SIlya Dryomov 		p += sizeof(struct ceph_eversion);
21648cb441c0SIlya Dryomov 
21658cb441c0SIlya Dryomov 		BUG_ON(p >= oloc);
21668cb441c0SIlya Dryomov 		memmove(p, oloc, oloc_len);
21678cb441c0SIlya Dryomov 		p += oloc_len;
21688cb441c0SIlya Dryomov 
21698cb441c0SIlya Dryomov 		pgid.seed = le32_to_cpu(head.hash);
21708cb441c0SIlya Dryomov 		encode_pgid(&p, &pgid); /* raw pg */
21718cb441c0SIlya Dryomov 
21728cb441c0SIlya Dryomov 		BUG_ON(p >= oid);
21738cb441c0SIlya Dryomov 		memmove(p, oid, oid_len);
21748cb441c0SIlya Dryomov 		p += oid_len;
21758cb441c0SIlya Dryomov 
21768cb441c0SIlya Dryomov 		/* tail -- ops, snapid, snapc, retry_attempt */
21778cb441c0SIlya Dryomov 		BUG_ON(p >= tail);
21788cb441c0SIlya Dryomov 		memmove(p, tail, tail_len);
21798cb441c0SIlya Dryomov 		p += tail_len;
21808cb441c0SIlya Dryomov 
21818cb441c0SIlya Dryomov 		msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
21828cb441c0SIlya Dryomov 	}
21838cb441c0SIlya Dryomov 
21848cb441c0SIlya Dryomov 	BUG_ON(p > end);
21858cb441c0SIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
21868cb441c0SIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
21878cb441c0SIlya Dryomov 
21888cb441c0SIlya Dryomov 	dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
21898cb441c0SIlya Dryomov 	     le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
21908cb441c0SIlya Dryomov 	     le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
21918cb441c0SIlya Dryomov 	     le16_to_cpu(msg->hdr.version));
2192bb873b53SIlya Dryomov }
2193bb873b53SIlya Dryomov 
2194bb873b53SIlya Dryomov /*
2195bb873b53SIlya Dryomov  * @req has to be assigned a tid and registered.
2196bb873b53SIlya Dryomov  */
2197bb873b53SIlya Dryomov static void send_request(struct ceph_osd_request *req)
2198bb873b53SIlya Dryomov {
2199bb873b53SIlya Dryomov 	struct ceph_osd *osd = req->r_osd;
2200bb873b53SIlya Dryomov 
22015aea3dcdSIlya Dryomov 	verify_osd_locked(osd);
2202bb873b53SIlya Dryomov 	WARN_ON(osd->o_osd != req->r_t.osd);
2203bb873b53SIlya Dryomov 
2204a02a946dSIlya Dryomov 	/* backoff? */
2205a02a946dSIlya Dryomov 	if (should_plug_request(req))
2206a02a946dSIlya Dryomov 		return;
2207a02a946dSIlya Dryomov 
22085aea3dcdSIlya Dryomov 	/*
22095aea3dcdSIlya Dryomov 	 * We may have a previously queued request message hanging
22105aea3dcdSIlya Dryomov 	 * around.  Cancel it to avoid corrupting the msgr.
22115aea3dcdSIlya Dryomov 	 */
22125aea3dcdSIlya Dryomov 	if (req->r_sent)
22135aea3dcdSIlya Dryomov 		ceph_msg_revoke(req->r_request);
22145aea3dcdSIlya Dryomov 
2215bb873b53SIlya Dryomov 	req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
2216bb873b53SIlya Dryomov 	if (req->r_attempts)
2217bb873b53SIlya Dryomov 		req->r_flags |= CEPH_OSD_FLAG_RETRY;
2218bb873b53SIlya Dryomov 	else
2219bb873b53SIlya Dryomov 		WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
2220bb873b53SIlya Dryomov 
22218cb441c0SIlya Dryomov 	encode_request_partial(req, req->r_request);
2222bb873b53SIlya Dryomov 
222304c7d789SIlya 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",
2224bb873b53SIlya Dryomov 	     __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
2225dc98ff72SIlya Dryomov 	     req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
222604c7d789SIlya Dryomov 	     req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
222704c7d789SIlya Dryomov 	     req->r_attempts);
2228bb873b53SIlya Dryomov 
2229bb873b53SIlya Dryomov 	req->r_t.paused = false;
22303d14c5d2SYehuda Sadeh 	req->r_stamp = jiffies;
2231bb873b53SIlya Dryomov 	req->r_attempts++;
22323d14c5d2SYehuda Sadeh 
2233bb873b53SIlya Dryomov 	req->r_sent = osd->o_incarnation;
2234bb873b53SIlya Dryomov 	req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
2235bb873b53SIlya Dryomov 	ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
22363d14c5d2SYehuda Sadeh }
22373d14c5d2SYehuda Sadeh 
223842c1b124SIlya Dryomov static void maybe_request_map(struct ceph_osd_client *osdc)
223942c1b124SIlya Dryomov {
224042c1b124SIlya Dryomov 	bool continuous = false;
224142c1b124SIlya Dryomov 
22425aea3dcdSIlya Dryomov 	verify_osdc_locked(osdc);
224342c1b124SIlya Dryomov 	WARN_ON(!osdc->osdmap->epoch);
224442c1b124SIlya Dryomov 
2245b7ec35b3SIlya Dryomov 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2246b7ec35b3SIlya Dryomov 	    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
2247b7ec35b3SIlya Dryomov 	    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
224842c1b124SIlya Dryomov 		dout("%s osdc %p continuous\n", __func__, osdc);
224942c1b124SIlya Dryomov 		continuous = true;
225042c1b124SIlya Dryomov 	} else {
225142c1b124SIlya Dryomov 		dout("%s osdc %p onetime\n", __func__, osdc);
225242c1b124SIlya Dryomov 	}
225342c1b124SIlya Dryomov 
225442c1b124SIlya Dryomov 	if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
225542c1b124SIlya Dryomov 			       osdc->osdmap->epoch + 1, continuous))
225642c1b124SIlya Dryomov 		ceph_monc_renew_subs(&osdc->client->monc);
225742c1b124SIlya Dryomov }
225842c1b124SIlya Dryomov 
2259a1f4020aSJeff Layton static void complete_request(struct ceph_osd_request *req, int err);
22604609245eSIlya Dryomov static void send_map_check(struct ceph_osd_request *req);
22614609245eSIlya Dryomov 
22625aea3dcdSIlya Dryomov static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
22630bbfdfe8SIlya Dryomov {
22645aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
22655aea3dcdSIlya Dryomov 	struct ceph_osd *osd;
22664609245eSIlya Dryomov 	enum calc_target_result ct_res;
226766850df5SIlya Dryomov 	int err = 0;
22685aea3dcdSIlya Dryomov 	bool need_send = false;
22695aea3dcdSIlya Dryomov 	bool promoted = false;
22700bbfdfe8SIlya Dryomov 
2271b18b9550SIlya Dryomov 	WARN_ON(req->r_tid);
22725aea3dcdSIlya Dryomov 	dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
22735aea3dcdSIlya Dryomov 
22745aea3dcdSIlya Dryomov again:
22757de030d6SIlya Dryomov 	ct_res = calc_target(osdc, &req->r_t, NULL, false);
22764609245eSIlya Dryomov 	if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
22774609245eSIlya Dryomov 		goto promote;
22784609245eSIlya Dryomov 
22795aea3dcdSIlya Dryomov 	osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
22805aea3dcdSIlya Dryomov 	if (IS_ERR(osd)) {
22815aea3dcdSIlya Dryomov 		WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
22825aea3dcdSIlya Dryomov 		goto promote;
22835aea3dcdSIlya Dryomov 	}
22845aea3dcdSIlya Dryomov 
228566850df5SIlya Dryomov 	if (osdc->abort_err) {
228666850df5SIlya Dryomov 		dout("req %p abort_err %d\n", req, osdc->abort_err);
228766850df5SIlya Dryomov 		err = osdc->abort_err;
228866850df5SIlya Dryomov 	} else if (osdc->osdmap->epoch < osdc->epoch_barrier) {
228958eb7932SJeff Layton 		dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
229058eb7932SJeff Layton 		     osdc->epoch_barrier);
229158eb7932SJeff Layton 		req->r_t.paused = true;
229258eb7932SJeff Layton 		maybe_request_map(osdc);
229358eb7932SJeff Layton 	} else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2294b7ec35b3SIlya Dryomov 		   ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
22955aea3dcdSIlya Dryomov 		dout("req %p pausewr\n", req);
22965aea3dcdSIlya Dryomov 		req->r_t.paused = true;
22975aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
22985aea3dcdSIlya Dryomov 	} else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
2299b7ec35b3SIlya Dryomov 		   ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
23005aea3dcdSIlya Dryomov 		dout("req %p pauserd\n", req);
23015aea3dcdSIlya Dryomov 		req->r_t.paused = true;
23025aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
23035aea3dcdSIlya Dryomov 	} else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
23045aea3dcdSIlya Dryomov 		   !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
23055aea3dcdSIlya Dryomov 				     CEPH_OSD_FLAG_FULL_FORCE)) &&
2306b7ec35b3SIlya Dryomov 		   (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
23075aea3dcdSIlya Dryomov 		    pool_full(osdc, req->r_t.base_oloc.pool))) {
23085aea3dcdSIlya Dryomov 		dout("req %p full/pool_full\n", req);
230902b2f549SDongsheng Yang 		if (ceph_test_opt(osdc->client, ABORT_ON_FULL)) {
231029e87820SIlya Dryomov 			err = -ENOSPC;
231129e87820SIlya Dryomov 		} else {
23125aea3dcdSIlya Dryomov 			pr_warn_ratelimited("FULL or reached pool quota\n");
23135aea3dcdSIlya Dryomov 			req->r_t.paused = true;
23145aea3dcdSIlya Dryomov 			maybe_request_map(osdc);
231529e87820SIlya Dryomov 		}
23165aea3dcdSIlya Dryomov 	} else if (!osd_homeless(osd)) {
23175aea3dcdSIlya Dryomov 		need_send = true;
23180bbfdfe8SIlya Dryomov 	} else {
23195aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
23200bbfdfe8SIlya Dryomov 	}
23210bbfdfe8SIlya Dryomov 
23225aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
23235aea3dcdSIlya Dryomov 	/*
23245aea3dcdSIlya Dryomov 	 * Assign the tid atomically with send_request() to protect
23255aea3dcdSIlya Dryomov 	 * multiple writes to the same object from racing with each
23265aea3dcdSIlya Dryomov 	 * other, resulting in out of order ops on the OSDs.
23275aea3dcdSIlya Dryomov 	 */
23285aea3dcdSIlya Dryomov 	req->r_tid = atomic64_inc_return(&osdc->last_tid);
23295aea3dcdSIlya Dryomov 	link_request(osd, req);
23305aea3dcdSIlya Dryomov 	if (need_send)
23315aea3dcdSIlya Dryomov 		send_request(req);
233266850df5SIlya Dryomov 	else if (err)
233366850df5SIlya Dryomov 		complete_request(req, err);
23345aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
23355aea3dcdSIlya Dryomov 
23366001567cSIlya Dryomov 	if (!err && ct_res == CALC_TARGET_POOL_DNE)
23374609245eSIlya Dryomov 		send_map_check(req);
23384609245eSIlya Dryomov 
23395aea3dcdSIlya Dryomov 	if (promoted)
23405aea3dcdSIlya Dryomov 		downgrade_write(&osdc->lock);
23415aea3dcdSIlya Dryomov 	return;
23425aea3dcdSIlya Dryomov 
23435aea3dcdSIlya Dryomov promote:
23445aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
23455aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
23465aea3dcdSIlya Dryomov 	wrlocked = true;
23475aea3dcdSIlya Dryomov 	promoted = true;
23485aea3dcdSIlya Dryomov 	goto again;
23490bbfdfe8SIlya Dryomov }
23500bbfdfe8SIlya Dryomov 
23515aea3dcdSIlya Dryomov static void account_request(struct ceph_osd_request *req)
23525aea3dcdSIlya Dryomov {
235354ea0046SIlya Dryomov 	WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
2354b18b9550SIlya Dryomov 	WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
23555aea3dcdSIlya Dryomov 
2356b18b9550SIlya Dryomov 	req->r_flags |= CEPH_OSD_FLAG_ONDISK;
23575aea3dcdSIlya Dryomov 	atomic_inc(&req->r_osdc->num_requests);
23587cc5e38fSIlya Dryomov 
23597cc5e38fSIlya Dryomov 	req->r_start_stamp = jiffies;
23605aea3dcdSIlya Dryomov }
23615aea3dcdSIlya Dryomov 
23625aea3dcdSIlya Dryomov static void submit_request(struct ceph_osd_request *req, bool wrlocked)
23635aea3dcdSIlya Dryomov {
23645aea3dcdSIlya Dryomov 	ceph_osdc_get_request(req);
23655aea3dcdSIlya Dryomov 	account_request(req);
23665aea3dcdSIlya Dryomov 	__submit_request(req, wrlocked);
23675aea3dcdSIlya Dryomov }
23685aea3dcdSIlya Dryomov 
236945ee2c1dSIlya Dryomov static void finish_request(struct ceph_osd_request *req)
23705aea3dcdSIlya Dryomov {
23715aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
23725aea3dcdSIlya Dryomov 
23734609245eSIlya Dryomov 	WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
237404c7d789SIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
237504c7d789SIlya Dryomov 
237604c7d789SIlya Dryomov 	if (req->r_osd)
237704c7d789SIlya Dryomov 		unlink_request(req->r_osd, req);
23785aea3dcdSIlya Dryomov 	atomic_dec(&osdc->num_requests);
23795aea3dcdSIlya Dryomov 
23805aea3dcdSIlya Dryomov 	/*
23815aea3dcdSIlya Dryomov 	 * If an OSD has failed or returned and a request has been sent
23825aea3dcdSIlya Dryomov 	 * twice, it's possible to get a reply and end up here while the
23835aea3dcdSIlya Dryomov 	 * request message is queued for delivery.  We will ignore the
23845aea3dcdSIlya Dryomov 	 * reply, so not a big deal, but better to try and catch it.
23855aea3dcdSIlya Dryomov 	 */
23865aea3dcdSIlya Dryomov 	ceph_msg_revoke(req->r_request);
23875aea3dcdSIlya Dryomov 	ceph_msg_revoke_incoming(req->r_reply);
23885aea3dcdSIlya Dryomov }
23895aea3dcdSIlya Dryomov 
2390fe5da05eSIlya Dryomov static void __complete_request(struct ceph_osd_request *req)
2391fe5da05eSIlya Dryomov {
2392d75f773cSSakari Ailus 	dout("%s req %p tid %llu cb %ps result %d\n", __func__, req,
2393b18b9550SIlya Dryomov 	     req->r_tid, req->r_callback, req->r_result);
239426df726bSIlya Dryomov 
239526df726bSIlya Dryomov 	if (req->r_callback)
2396fe5da05eSIlya Dryomov 		req->r_callback(req);
239726df726bSIlya Dryomov 	complete_all(&req->r_completion);
239826df726bSIlya Dryomov 	ceph_osdc_put_request(req);
2399b18b9550SIlya Dryomov }
2400fe5da05eSIlya Dryomov 
240188bc1922SIlya Dryomov static void complete_request_workfn(struct work_struct *work)
240288bc1922SIlya Dryomov {
240388bc1922SIlya Dryomov 	struct ceph_osd_request *req =
240488bc1922SIlya Dryomov 	    container_of(work, struct ceph_osd_request, r_complete_work);
240588bc1922SIlya Dryomov 
240688bc1922SIlya Dryomov 	__complete_request(req);
24070bbfdfe8SIlya Dryomov }
24080bbfdfe8SIlya Dryomov 
24094609245eSIlya Dryomov /*
2410b18b9550SIlya Dryomov  * This is open-coded in handle_reply().
24114609245eSIlya Dryomov  */
24124609245eSIlya Dryomov static void complete_request(struct ceph_osd_request *req, int err)
24134609245eSIlya Dryomov {
24144609245eSIlya Dryomov 	dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
24154609245eSIlya Dryomov 
24164609245eSIlya Dryomov 	req->r_result = err;
241745ee2c1dSIlya Dryomov 	finish_request(req);
241888bc1922SIlya Dryomov 
241988bc1922SIlya Dryomov 	INIT_WORK(&req->r_complete_work, complete_request_workfn);
242088bc1922SIlya Dryomov 	queue_work(req->r_osdc->completion_wq, &req->r_complete_work);
24214609245eSIlya Dryomov }
24224609245eSIlya Dryomov 
24234609245eSIlya Dryomov static void cancel_map_check(struct ceph_osd_request *req)
24244609245eSIlya Dryomov {
24254609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
24264609245eSIlya Dryomov 	struct ceph_osd_request *lookup_req;
24274609245eSIlya Dryomov 
24284609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
24294609245eSIlya Dryomov 
24304609245eSIlya Dryomov 	lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
24314609245eSIlya Dryomov 	if (!lookup_req)
24324609245eSIlya Dryomov 		return;
24334609245eSIlya Dryomov 
24344609245eSIlya Dryomov 	WARN_ON(lookup_req != req);
24354609245eSIlya Dryomov 	erase_request_mc(&osdc->map_checks, req);
24364609245eSIlya Dryomov 	ceph_osdc_put_request(req);
24374609245eSIlya Dryomov }
24384609245eSIlya Dryomov 
24395aea3dcdSIlya Dryomov static void cancel_request(struct ceph_osd_request *req)
24405aea3dcdSIlya Dryomov {
24415aea3dcdSIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
24425aea3dcdSIlya Dryomov 
24434609245eSIlya Dryomov 	cancel_map_check(req);
244445ee2c1dSIlya Dryomov 	finish_request(req);
2445b18b9550SIlya Dryomov 	complete_all(&req->r_completion);
2446c297eb42SIlya Dryomov 	ceph_osdc_put_request(req);
24475aea3dcdSIlya Dryomov }
24485aea3dcdSIlya Dryomov 
24497cc5e38fSIlya Dryomov static void abort_request(struct ceph_osd_request *req, int err)
24507cc5e38fSIlya Dryomov {
24517cc5e38fSIlya Dryomov 	dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
24527cc5e38fSIlya Dryomov 
24537cc5e38fSIlya Dryomov 	cancel_map_check(req);
24547cc5e38fSIlya Dryomov 	complete_request(req, err);
24557cc5e38fSIlya Dryomov }
24567cc5e38fSIlya Dryomov 
245766850df5SIlya Dryomov static int abort_fn(struct ceph_osd_request *req, void *arg)
245866850df5SIlya Dryomov {
245966850df5SIlya Dryomov 	int err = *(int *)arg;
246066850df5SIlya Dryomov 
246166850df5SIlya Dryomov 	abort_request(req, err);
246266850df5SIlya Dryomov 	return 0; /* continue iteration */
246366850df5SIlya Dryomov }
246466850df5SIlya Dryomov 
246566850df5SIlya Dryomov /*
246666850df5SIlya Dryomov  * Abort all in-flight requests with @err and arrange for all future
246766850df5SIlya Dryomov  * requests to be failed immediately.
246866850df5SIlya Dryomov  */
246966850df5SIlya Dryomov void ceph_osdc_abort_requests(struct ceph_osd_client *osdc, int err)
247066850df5SIlya Dryomov {
247166850df5SIlya Dryomov 	dout("%s osdc %p err %d\n", __func__, osdc, err);
247266850df5SIlya Dryomov 	down_write(&osdc->lock);
247366850df5SIlya Dryomov 	for_each_request(osdc, abort_fn, &err);
247466850df5SIlya Dryomov 	osdc->abort_err = err;
247566850df5SIlya Dryomov 	up_write(&osdc->lock);
247666850df5SIlya Dryomov }
247766850df5SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_abort_requests);
247866850df5SIlya Dryomov 
24792cef0ba8SYan, Zheng void ceph_osdc_clear_abort_err(struct ceph_osd_client *osdc)
24802cef0ba8SYan, Zheng {
24812cef0ba8SYan, Zheng 	down_write(&osdc->lock);
24822cef0ba8SYan, Zheng 	osdc->abort_err = 0;
24832cef0ba8SYan, Zheng 	up_write(&osdc->lock);
24842cef0ba8SYan, Zheng }
24852cef0ba8SYan, Zheng EXPORT_SYMBOL(ceph_osdc_clear_abort_err);
24862cef0ba8SYan, Zheng 
248758eb7932SJeff Layton static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
248858eb7932SJeff Layton {
248958eb7932SJeff Layton 	if (likely(eb > osdc->epoch_barrier)) {
249058eb7932SJeff Layton 		dout("updating epoch_barrier from %u to %u\n",
249158eb7932SJeff Layton 				osdc->epoch_barrier, eb);
249258eb7932SJeff Layton 		osdc->epoch_barrier = eb;
249358eb7932SJeff Layton 		/* Request map if we're not to the barrier yet */
249458eb7932SJeff Layton 		if (eb > osdc->osdmap->epoch)
249558eb7932SJeff Layton 			maybe_request_map(osdc);
249658eb7932SJeff Layton 	}
249758eb7932SJeff Layton }
249858eb7932SJeff Layton 
249958eb7932SJeff Layton void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
250058eb7932SJeff Layton {
250158eb7932SJeff Layton 	down_read(&osdc->lock);
250258eb7932SJeff Layton 	if (unlikely(eb > osdc->epoch_barrier)) {
250358eb7932SJeff Layton 		up_read(&osdc->lock);
250458eb7932SJeff Layton 		down_write(&osdc->lock);
250558eb7932SJeff Layton 		update_epoch_barrier(osdc, eb);
250658eb7932SJeff Layton 		up_write(&osdc->lock);
250758eb7932SJeff Layton 	} else {
250858eb7932SJeff Layton 		up_read(&osdc->lock);
250958eb7932SJeff Layton 	}
251058eb7932SJeff Layton }
251158eb7932SJeff Layton EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
251258eb7932SJeff Layton 
2513fc36d0a4SJeff Layton /*
25144eea0fefSIlya Dryomov  * We can end up releasing caps as a result of abort_request().
25154eea0fefSIlya Dryomov  * In that case, we probably want to ensure that the cap release message
25164eea0fefSIlya Dryomov  * has an updated epoch barrier in it, so set the epoch barrier prior to
25174eea0fefSIlya Dryomov  * aborting the first request.
25184eea0fefSIlya Dryomov  */
25194eea0fefSIlya Dryomov static int abort_on_full_fn(struct ceph_osd_request *req, void *arg)
25204eea0fefSIlya Dryomov {
25214eea0fefSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
25224eea0fefSIlya Dryomov 	bool *victims = arg;
25234eea0fefSIlya Dryomov 
2524c843d13cSIlya Dryomov 	if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
25254eea0fefSIlya Dryomov 	    (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2526690f951dSIlya Dryomov 	     pool_full(osdc, req->r_t.base_oloc.pool))) {
25274eea0fefSIlya Dryomov 		if (!*victims) {
25284eea0fefSIlya Dryomov 			update_epoch_barrier(osdc, osdc->osdmap->epoch);
25294eea0fefSIlya Dryomov 			*victims = true;
25304eea0fefSIlya Dryomov 		}
25314eea0fefSIlya Dryomov 		abort_request(req, -ENOSPC);
25324eea0fefSIlya Dryomov 	}
25334eea0fefSIlya Dryomov 
25344eea0fefSIlya Dryomov 	return 0; /* continue iteration */
25354eea0fefSIlya Dryomov }
25364eea0fefSIlya Dryomov 
25374eea0fefSIlya Dryomov /*
2538fc36d0a4SJeff Layton  * Drop all pending requests that are stalled waiting on a full condition to
253958eb7932SJeff Layton  * clear, and complete them with ENOSPC as the return code. Set the
254058eb7932SJeff Layton  * osdc->epoch_barrier to the latest map epoch that we've seen if any were
254158eb7932SJeff Layton  * cancelled.
2542fc36d0a4SJeff Layton  */
2543fc36d0a4SJeff Layton static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
2544fc36d0a4SJeff Layton {
254558eb7932SJeff Layton 	bool victims = false;
2546fc36d0a4SJeff Layton 
254702b2f549SDongsheng Yang 	if (ceph_test_opt(osdc->client, ABORT_ON_FULL) &&
2548c843d13cSIlya Dryomov 	    (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || have_pool_full(osdc)))
25494eea0fefSIlya Dryomov 		for_each_request(osdc, abort_on_full_fn, &victims);
2550fc36d0a4SJeff Layton }
2551fc36d0a4SJeff Layton 
25524609245eSIlya Dryomov static void check_pool_dne(struct ceph_osd_request *req)
25534609245eSIlya Dryomov {
25544609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
25554609245eSIlya Dryomov 	struct ceph_osdmap *map = osdc->osdmap;
25564609245eSIlya Dryomov 
25574609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
25584609245eSIlya Dryomov 	WARN_ON(!map->epoch);
25594609245eSIlya Dryomov 
25604609245eSIlya Dryomov 	if (req->r_attempts) {
25614609245eSIlya Dryomov 		/*
25624609245eSIlya Dryomov 		 * We sent a request earlier, which means that
25634609245eSIlya Dryomov 		 * previously the pool existed, and now it does not
25644609245eSIlya Dryomov 		 * (i.e., it was deleted).
25654609245eSIlya Dryomov 		 */
25664609245eSIlya Dryomov 		req->r_map_dne_bound = map->epoch;
25674609245eSIlya Dryomov 		dout("%s req %p tid %llu pool disappeared\n", __func__, req,
25684609245eSIlya Dryomov 		     req->r_tid);
25694609245eSIlya Dryomov 	} else {
25704609245eSIlya Dryomov 		dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
25714609245eSIlya Dryomov 		     req, req->r_tid, req->r_map_dne_bound, map->epoch);
25724609245eSIlya Dryomov 	}
25734609245eSIlya Dryomov 
25744609245eSIlya Dryomov 	if (req->r_map_dne_bound) {
25754609245eSIlya Dryomov 		if (map->epoch >= req->r_map_dne_bound) {
25764609245eSIlya Dryomov 			/* we had a new enough map */
25774609245eSIlya Dryomov 			pr_info_ratelimited("tid %llu pool does not exist\n",
25784609245eSIlya Dryomov 					    req->r_tid);
25794609245eSIlya Dryomov 			complete_request(req, -ENOENT);
25804609245eSIlya Dryomov 		}
25814609245eSIlya Dryomov 	} else {
25824609245eSIlya Dryomov 		send_map_check(req);
25834609245eSIlya Dryomov 	}
25844609245eSIlya Dryomov }
25854609245eSIlya Dryomov 
25864609245eSIlya Dryomov static void map_check_cb(struct ceph_mon_generic_request *greq)
25874609245eSIlya Dryomov {
25884609245eSIlya Dryomov 	struct ceph_osd_client *osdc = &greq->monc->client->osdc;
25894609245eSIlya Dryomov 	struct ceph_osd_request *req;
25904609245eSIlya Dryomov 	u64 tid = greq->private_data;
25914609245eSIlya Dryomov 
25924609245eSIlya Dryomov 	WARN_ON(greq->result || !greq->u.newest);
25934609245eSIlya Dryomov 
25944609245eSIlya Dryomov 	down_write(&osdc->lock);
25954609245eSIlya Dryomov 	req = lookup_request_mc(&osdc->map_checks, tid);
25964609245eSIlya Dryomov 	if (!req) {
25974609245eSIlya Dryomov 		dout("%s tid %llu dne\n", __func__, tid);
25984609245eSIlya Dryomov 		goto out_unlock;
25994609245eSIlya Dryomov 	}
26004609245eSIlya Dryomov 
26014609245eSIlya Dryomov 	dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
26024609245eSIlya Dryomov 	     req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
26034609245eSIlya Dryomov 	if (!req->r_map_dne_bound)
26044609245eSIlya Dryomov 		req->r_map_dne_bound = greq->u.newest;
26054609245eSIlya Dryomov 	erase_request_mc(&osdc->map_checks, req);
26064609245eSIlya Dryomov 	check_pool_dne(req);
26074609245eSIlya Dryomov 
26084609245eSIlya Dryomov 	ceph_osdc_put_request(req);
26094609245eSIlya Dryomov out_unlock:
26104609245eSIlya Dryomov 	up_write(&osdc->lock);
26114609245eSIlya Dryomov }
26124609245eSIlya Dryomov 
26134609245eSIlya Dryomov static void send_map_check(struct ceph_osd_request *req)
26144609245eSIlya Dryomov {
26154609245eSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
26164609245eSIlya Dryomov 	struct ceph_osd_request *lookup_req;
26174609245eSIlya Dryomov 	int ret;
26184609245eSIlya Dryomov 
26194609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
26204609245eSIlya Dryomov 
26214609245eSIlya Dryomov 	lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
26224609245eSIlya Dryomov 	if (lookup_req) {
26234609245eSIlya Dryomov 		WARN_ON(lookup_req != req);
26244609245eSIlya Dryomov 		return;
26254609245eSIlya Dryomov 	}
26264609245eSIlya Dryomov 
26274609245eSIlya Dryomov 	ceph_osdc_get_request(req);
26284609245eSIlya Dryomov 	insert_request_mc(&osdc->map_checks, req);
26294609245eSIlya Dryomov 	ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
26304609245eSIlya Dryomov 					  map_check_cb, req->r_tid);
26314609245eSIlya Dryomov 	WARN_ON(ret);
26324609245eSIlya Dryomov }
26334609245eSIlya Dryomov 
26340bbfdfe8SIlya Dryomov /*
2635922dab61SIlya Dryomov  * lingering requests, watch/notify v2 infrastructure
2636922dab61SIlya Dryomov  */
2637922dab61SIlya Dryomov static void linger_release(struct kref *kref)
2638922dab61SIlya Dryomov {
2639922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq =
2640922dab61SIlya Dryomov 	    container_of(kref, struct ceph_osd_linger_request, kref);
2641922dab61SIlya Dryomov 
2642922dab61SIlya Dryomov 	dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2643922dab61SIlya Dryomov 	     lreq->reg_req, lreq->ping_req);
2644922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2645922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
26464609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
2647922dab61SIlya Dryomov 	WARN_ON(!list_empty(&lreq->scan_item));
2648b07d3c4bSIlya Dryomov 	WARN_ON(!list_empty(&lreq->pending_lworks));
2649922dab61SIlya Dryomov 	WARN_ON(lreq->osd);
2650922dab61SIlya Dryomov 
2651922dab61SIlya Dryomov 	if (lreq->reg_req)
2652922dab61SIlya Dryomov 		ceph_osdc_put_request(lreq->reg_req);
2653922dab61SIlya Dryomov 	if (lreq->ping_req)
2654922dab61SIlya Dryomov 		ceph_osdc_put_request(lreq->ping_req);
2655922dab61SIlya Dryomov 	target_destroy(&lreq->t);
2656922dab61SIlya Dryomov 	kfree(lreq);
2657922dab61SIlya Dryomov }
2658922dab61SIlya Dryomov 
2659922dab61SIlya Dryomov static void linger_put(struct ceph_osd_linger_request *lreq)
2660922dab61SIlya Dryomov {
2661922dab61SIlya Dryomov 	if (lreq)
2662922dab61SIlya Dryomov 		kref_put(&lreq->kref, linger_release);
2663922dab61SIlya Dryomov }
2664922dab61SIlya Dryomov 
2665922dab61SIlya Dryomov static struct ceph_osd_linger_request *
2666922dab61SIlya Dryomov linger_get(struct ceph_osd_linger_request *lreq)
2667922dab61SIlya Dryomov {
2668922dab61SIlya Dryomov 	kref_get(&lreq->kref);
2669922dab61SIlya Dryomov 	return lreq;
2670922dab61SIlya Dryomov }
2671922dab61SIlya Dryomov 
2672922dab61SIlya Dryomov static struct ceph_osd_linger_request *
2673922dab61SIlya Dryomov linger_alloc(struct ceph_osd_client *osdc)
2674922dab61SIlya Dryomov {
2675922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
2676922dab61SIlya Dryomov 
2677922dab61SIlya Dryomov 	lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2678922dab61SIlya Dryomov 	if (!lreq)
2679922dab61SIlya Dryomov 		return NULL;
2680922dab61SIlya Dryomov 
2681922dab61SIlya Dryomov 	kref_init(&lreq->kref);
2682922dab61SIlya Dryomov 	mutex_init(&lreq->lock);
2683922dab61SIlya Dryomov 	RB_CLEAR_NODE(&lreq->node);
2684922dab61SIlya Dryomov 	RB_CLEAR_NODE(&lreq->osdc_node);
26854609245eSIlya Dryomov 	RB_CLEAR_NODE(&lreq->mc_node);
2686922dab61SIlya Dryomov 	INIT_LIST_HEAD(&lreq->scan_item);
2687b07d3c4bSIlya Dryomov 	INIT_LIST_HEAD(&lreq->pending_lworks);
2688922dab61SIlya Dryomov 	init_completion(&lreq->reg_commit_wait);
268919079203SIlya Dryomov 	init_completion(&lreq->notify_finish_wait);
2690922dab61SIlya Dryomov 
2691922dab61SIlya Dryomov 	lreq->osdc = osdc;
2692922dab61SIlya Dryomov 	target_init(&lreq->t);
2693922dab61SIlya Dryomov 
2694922dab61SIlya Dryomov 	dout("%s lreq %p\n", __func__, lreq);
2695922dab61SIlya Dryomov 	return lreq;
2696922dab61SIlya Dryomov }
2697922dab61SIlya Dryomov 
2698922dab61SIlya Dryomov DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2699922dab61SIlya Dryomov DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
27004609245eSIlya Dryomov DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
2701922dab61SIlya Dryomov 
2702922dab61SIlya Dryomov /*
2703922dab61SIlya Dryomov  * Create linger request <-> OSD session relation.
2704922dab61SIlya Dryomov  *
2705922dab61SIlya Dryomov  * @lreq has to be registered, @osd may be homeless.
2706922dab61SIlya Dryomov  */
2707922dab61SIlya Dryomov static void link_linger(struct ceph_osd *osd,
2708922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq)
2709922dab61SIlya Dryomov {
2710922dab61SIlya Dryomov 	verify_osd_locked(osd);
2711922dab61SIlya Dryomov 	WARN_ON(!lreq->linger_id || lreq->osd);
2712922dab61SIlya Dryomov 	dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2713922dab61SIlya Dryomov 	     osd->o_osd, lreq, lreq->linger_id);
2714922dab61SIlya Dryomov 
2715922dab61SIlya Dryomov 	if (!osd_homeless(osd))
2716922dab61SIlya Dryomov 		__remove_osd_from_lru(osd);
2717922dab61SIlya Dryomov 	else
2718922dab61SIlya Dryomov 		atomic_inc(&osd->o_osdc->num_homeless);
2719922dab61SIlya Dryomov 
2720922dab61SIlya Dryomov 	get_osd(osd);
2721922dab61SIlya Dryomov 	insert_linger(&osd->o_linger_requests, lreq);
2722922dab61SIlya Dryomov 	lreq->osd = osd;
2723922dab61SIlya Dryomov }
2724922dab61SIlya Dryomov 
2725922dab61SIlya Dryomov static void unlink_linger(struct ceph_osd *osd,
2726922dab61SIlya Dryomov 			  struct ceph_osd_linger_request *lreq)
2727922dab61SIlya Dryomov {
2728922dab61SIlya Dryomov 	verify_osd_locked(osd);
2729922dab61SIlya Dryomov 	WARN_ON(lreq->osd != osd);
2730922dab61SIlya Dryomov 	dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2731922dab61SIlya Dryomov 	     osd->o_osd, lreq, lreq->linger_id);
2732922dab61SIlya Dryomov 
2733922dab61SIlya Dryomov 	lreq->osd = NULL;
2734922dab61SIlya Dryomov 	erase_linger(&osd->o_linger_requests, lreq);
2735922dab61SIlya Dryomov 	put_osd(osd);
2736922dab61SIlya Dryomov 
2737922dab61SIlya Dryomov 	if (!osd_homeless(osd))
2738922dab61SIlya Dryomov 		maybe_move_osd_to_lru(osd);
2739922dab61SIlya Dryomov 	else
2740922dab61SIlya Dryomov 		atomic_dec(&osd->o_osdc->num_homeless);
2741922dab61SIlya Dryomov }
2742922dab61SIlya Dryomov 
2743922dab61SIlya Dryomov static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2744922dab61SIlya Dryomov {
2745922dab61SIlya Dryomov 	verify_osdc_locked(lreq->osdc);
2746922dab61SIlya Dryomov 
2747922dab61SIlya Dryomov 	return !RB_EMPTY_NODE(&lreq->osdc_node);
2748922dab61SIlya Dryomov }
2749922dab61SIlya Dryomov 
2750922dab61SIlya Dryomov static bool linger_registered(struct ceph_osd_linger_request *lreq)
2751922dab61SIlya Dryomov {
2752922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2753922dab61SIlya Dryomov 	bool registered;
2754922dab61SIlya Dryomov 
2755922dab61SIlya Dryomov 	down_read(&osdc->lock);
2756922dab61SIlya Dryomov 	registered = __linger_registered(lreq);
2757922dab61SIlya Dryomov 	up_read(&osdc->lock);
2758922dab61SIlya Dryomov 
2759922dab61SIlya Dryomov 	return registered;
2760922dab61SIlya Dryomov }
2761922dab61SIlya Dryomov 
2762922dab61SIlya Dryomov static void linger_register(struct ceph_osd_linger_request *lreq)
2763922dab61SIlya Dryomov {
2764922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2765922dab61SIlya Dryomov 
2766922dab61SIlya Dryomov 	verify_osdc_wrlocked(osdc);
2767922dab61SIlya Dryomov 	WARN_ON(lreq->linger_id);
2768922dab61SIlya Dryomov 
2769922dab61SIlya Dryomov 	linger_get(lreq);
2770922dab61SIlya Dryomov 	lreq->linger_id = ++osdc->last_linger_id;
2771922dab61SIlya Dryomov 	insert_linger_osdc(&osdc->linger_requests, lreq);
2772922dab61SIlya Dryomov }
2773922dab61SIlya Dryomov 
2774922dab61SIlya Dryomov static void linger_unregister(struct ceph_osd_linger_request *lreq)
2775922dab61SIlya Dryomov {
2776922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2777922dab61SIlya Dryomov 
2778922dab61SIlya Dryomov 	verify_osdc_wrlocked(osdc);
2779922dab61SIlya Dryomov 
2780922dab61SIlya Dryomov 	erase_linger_osdc(&osdc->linger_requests, lreq);
2781922dab61SIlya Dryomov 	linger_put(lreq);
2782922dab61SIlya Dryomov }
2783922dab61SIlya Dryomov 
2784922dab61SIlya Dryomov static void cancel_linger_request(struct ceph_osd_request *req)
2785922dab61SIlya Dryomov {
2786922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2787922dab61SIlya Dryomov 
2788922dab61SIlya Dryomov 	WARN_ON(!req->r_linger);
2789922dab61SIlya Dryomov 	cancel_request(req);
2790922dab61SIlya Dryomov 	linger_put(lreq);
2791922dab61SIlya Dryomov }
2792922dab61SIlya Dryomov 
2793922dab61SIlya Dryomov struct linger_work {
2794922dab61SIlya Dryomov 	struct work_struct work;
2795922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
2796b07d3c4bSIlya Dryomov 	struct list_head pending_item;
2797b07d3c4bSIlya Dryomov 	unsigned long queued_stamp;
2798922dab61SIlya Dryomov 
2799922dab61SIlya Dryomov 	union {
2800922dab61SIlya Dryomov 		struct {
2801922dab61SIlya Dryomov 			u64 notify_id;
2802922dab61SIlya Dryomov 			u64 notifier_id;
2803922dab61SIlya Dryomov 			void *payload; /* points into @msg front */
2804922dab61SIlya Dryomov 			size_t payload_len;
2805922dab61SIlya Dryomov 
2806922dab61SIlya Dryomov 			struct ceph_msg *msg; /* for ceph_msg_put() */
2807922dab61SIlya Dryomov 		} notify;
2808922dab61SIlya Dryomov 		struct {
2809922dab61SIlya Dryomov 			int err;
2810922dab61SIlya Dryomov 		} error;
2811922dab61SIlya Dryomov 	};
2812922dab61SIlya Dryomov };
2813922dab61SIlya Dryomov 
2814922dab61SIlya Dryomov static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2815922dab61SIlya Dryomov 				       work_func_t workfn)
2816922dab61SIlya Dryomov {
2817922dab61SIlya Dryomov 	struct linger_work *lwork;
2818922dab61SIlya Dryomov 
2819922dab61SIlya Dryomov 	lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2820922dab61SIlya Dryomov 	if (!lwork)
2821922dab61SIlya Dryomov 		return NULL;
2822922dab61SIlya Dryomov 
2823922dab61SIlya Dryomov 	INIT_WORK(&lwork->work, workfn);
2824b07d3c4bSIlya Dryomov 	INIT_LIST_HEAD(&lwork->pending_item);
2825922dab61SIlya Dryomov 	lwork->lreq = linger_get(lreq);
2826922dab61SIlya Dryomov 
2827922dab61SIlya Dryomov 	return lwork;
2828922dab61SIlya Dryomov }
2829922dab61SIlya Dryomov 
2830922dab61SIlya Dryomov static void lwork_free(struct linger_work *lwork)
2831922dab61SIlya Dryomov {
2832922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2833922dab61SIlya Dryomov 
2834b07d3c4bSIlya Dryomov 	mutex_lock(&lreq->lock);
2835b07d3c4bSIlya Dryomov 	list_del(&lwork->pending_item);
2836b07d3c4bSIlya Dryomov 	mutex_unlock(&lreq->lock);
2837b07d3c4bSIlya Dryomov 
2838922dab61SIlya Dryomov 	linger_put(lreq);
2839922dab61SIlya Dryomov 	kfree(lwork);
2840922dab61SIlya Dryomov }
2841922dab61SIlya Dryomov 
2842922dab61SIlya Dryomov static void lwork_queue(struct linger_work *lwork)
2843922dab61SIlya Dryomov {
2844922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2845922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
2846922dab61SIlya Dryomov 
2847922dab61SIlya Dryomov 	verify_lreq_locked(lreq);
2848b07d3c4bSIlya Dryomov 	WARN_ON(!list_empty(&lwork->pending_item));
2849b07d3c4bSIlya Dryomov 
2850b07d3c4bSIlya Dryomov 	lwork->queued_stamp = jiffies;
2851b07d3c4bSIlya Dryomov 	list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
2852922dab61SIlya Dryomov 	queue_work(osdc->notify_wq, &lwork->work);
2853922dab61SIlya Dryomov }
2854922dab61SIlya Dryomov 
2855922dab61SIlya Dryomov static void do_watch_notify(struct work_struct *w)
2856922dab61SIlya Dryomov {
2857922dab61SIlya Dryomov 	struct linger_work *lwork = container_of(w, struct linger_work, work);
2858922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2859922dab61SIlya Dryomov 
2860922dab61SIlya Dryomov 	if (!linger_registered(lreq)) {
2861922dab61SIlya Dryomov 		dout("%s lreq %p not registered\n", __func__, lreq);
2862922dab61SIlya Dryomov 		goto out;
2863922dab61SIlya Dryomov 	}
2864922dab61SIlya Dryomov 
286519079203SIlya Dryomov 	WARN_ON(!lreq->is_watch);
2866922dab61SIlya Dryomov 	dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2867922dab61SIlya Dryomov 	     __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2868922dab61SIlya Dryomov 	     lwork->notify.payload_len);
2869922dab61SIlya Dryomov 	lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2870922dab61SIlya Dryomov 		  lwork->notify.notifier_id, lwork->notify.payload,
2871922dab61SIlya Dryomov 		  lwork->notify.payload_len);
2872922dab61SIlya Dryomov 
2873922dab61SIlya Dryomov out:
2874922dab61SIlya Dryomov 	ceph_msg_put(lwork->notify.msg);
2875922dab61SIlya Dryomov 	lwork_free(lwork);
2876922dab61SIlya Dryomov }
2877922dab61SIlya Dryomov 
2878922dab61SIlya Dryomov static void do_watch_error(struct work_struct *w)
2879922dab61SIlya Dryomov {
2880922dab61SIlya Dryomov 	struct linger_work *lwork = container_of(w, struct linger_work, work);
2881922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = lwork->lreq;
2882922dab61SIlya Dryomov 
2883922dab61SIlya Dryomov 	if (!linger_registered(lreq)) {
2884922dab61SIlya Dryomov 		dout("%s lreq %p not registered\n", __func__, lreq);
2885922dab61SIlya Dryomov 		goto out;
2886922dab61SIlya Dryomov 	}
2887922dab61SIlya Dryomov 
2888922dab61SIlya Dryomov 	dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2889922dab61SIlya Dryomov 	lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2890922dab61SIlya Dryomov 
2891922dab61SIlya Dryomov out:
2892922dab61SIlya Dryomov 	lwork_free(lwork);
2893922dab61SIlya Dryomov }
2894922dab61SIlya Dryomov 
2895922dab61SIlya Dryomov static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2896922dab61SIlya Dryomov {
2897922dab61SIlya Dryomov 	struct linger_work *lwork;
2898922dab61SIlya Dryomov 
2899922dab61SIlya Dryomov 	lwork = lwork_alloc(lreq, do_watch_error);
2900922dab61SIlya Dryomov 	if (!lwork) {
2901922dab61SIlya Dryomov 		pr_err("failed to allocate error-lwork\n");
2902922dab61SIlya Dryomov 		return;
2903922dab61SIlya Dryomov 	}
2904922dab61SIlya Dryomov 
2905922dab61SIlya Dryomov 	lwork->error.err = lreq->last_error;
2906922dab61SIlya Dryomov 	lwork_queue(lwork);
2907922dab61SIlya Dryomov }
2908922dab61SIlya Dryomov 
2909922dab61SIlya Dryomov static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2910922dab61SIlya Dryomov 				       int result)
2911922dab61SIlya Dryomov {
2912922dab61SIlya Dryomov 	if (!completion_done(&lreq->reg_commit_wait)) {
2913922dab61SIlya Dryomov 		lreq->reg_commit_error = (result <= 0 ? result : 0);
2914922dab61SIlya Dryomov 		complete_all(&lreq->reg_commit_wait);
2915922dab61SIlya Dryomov 	}
2916922dab61SIlya Dryomov }
2917922dab61SIlya Dryomov 
2918922dab61SIlya Dryomov static void linger_commit_cb(struct ceph_osd_request *req)
2919922dab61SIlya Dryomov {
2920922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2921922dab61SIlya Dryomov 
2922922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
2923922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2924922dab61SIlya Dryomov 	     lreq->linger_id, req->r_result);
2925922dab61SIlya Dryomov 	linger_reg_commit_complete(lreq, req->r_result);
2926922dab61SIlya Dryomov 	lreq->committed = true;
2927922dab61SIlya Dryomov 
292819079203SIlya Dryomov 	if (!lreq->is_watch) {
292919079203SIlya Dryomov 		struct ceph_osd_data *osd_data =
293019079203SIlya Dryomov 		    osd_req_op_data(req, 0, notify, response_data);
293119079203SIlya Dryomov 		void *p = page_address(osd_data->pages[0]);
293219079203SIlya Dryomov 
293319079203SIlya Dryomov 		WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
293419079203SIlya Dryomov 			osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
293519079203SIlya Dryomov 
293619079203SIlya Dryomov 		/* make note of the notify_id */
293719079203SIlya Dryomov 		if (req->r_ops[0].outdata_len >= sizeof(u64)) {
293819079203SIlya Dryomov 			lreq->notify_id = ceph_decode_64(&p);
293919079203SIlya Dryomov 			dout("lreq %p notify_id %llu\n", lreq,
294019079203SIlya Dryomov 			     lreq->notify_id);
294119079203SIlya Dryomov 		} else {
294219079203SIlya Dryomov 			dout("lreq %p no notify_id\n", lreq);
294319079203SIlya Dryomov 		}
294419079203SIlya Dryomov 	}
294519079203SIlya Dryomov 
2946922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2947922dab61SIlya Dryomov 	linger_put(lreq);
2948922dab61SIlya Dryomov }
2949922dab61SIlya Dryomov 
2950922dab61SIlya Dryomov static int normalize_watch_error(int err)
2951922dab61SIlya Dryomov {
2952922dab61SIlya Dryomov 	/*
2953922dab61SIlya Dryomov 	 * Translate ENOENT -> ENOTCONN so that a delete->disconnection
2954922dab61SIlya Dryomov 	 * notification and a failure to reconnect because we raced with
2955922dab61SIlya Dryomov 	 * the delete appear the same to the user.
2956922dab61SIlya Dryomov 	 */
2957922dab61SIlya Dryomov 	if (err == -ENOENT)
2958922dab61SIlya Dryomov 		err = -ENOTCONN;
2959922dab61SIlya Dryomov 
2960922dab61SIlya Dryomov 	return err;
2961922dab61SIlya Dryomov }
2962922dab61SIlya Dryomov 
2963922dab61SIlya Dryomov static void linger_reconnect_cb(struct ceph_osd_request *req)
2964922dab61SIlya Dryomov {
2965922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
2966922dab61SIlya Dryomov 
2967922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
2968922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
2969922dab61SIlya Dryomov 	     lreq, lreq->linger_id, req->r_result, lreq->last_error);
2970922dab61SIlya Dryomov 	if (req->r_result < 0) {
2971922dab61SIlya Dryomov 		if (!lreq->last_error) {
2972922dab61SIlya Dryomov 			lreq->last_error = normalize_watch_error(req->r_result);
2973922dab61SIlya Dryomov 			queue_watch_error(lreq);
2974922dab61SIlya Dryomov 		}
2975922dab61SIlya Dryomov 	}
2976922dab61SIlya Dryomov 
2977922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
2978922dab61SIlya Dryomov 	linger_put(lreq);
2979922dab61SIlya Dryomov }
2980922dab61SIlya Dryomov 
2981922dab61SIlya Dryomov static void send_linger(struct ceph_osd_linger_request *lreq)
2982922dab61SIlya Dryomov {
2983922dab61SIlya Dryomov 	struct ceph_osd_request *req = lreq->reg_req;
2984922dab61SIlya Dryomov 	struct ceph_osd_req_op *op = &req->r_ops[0];
2985922dab61SIlya Dryomov 
2986922dab61SIlya Dryomov 	verify_osdc_wrlocked(req->r_osdc);
2987922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2988922dab61SIlya Dryomov 
2989922dab61SIlya Dryomov 	if (req->r_osd)
2990922dab61SIlya Dryomov 		cancel_linger_request(req);
2991922dab61SIlya Dryomov 
2992922dab61SIlya Dryomov 	request_reinit(req);
2993922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
2994922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
2995922dab61SIlya Dryomov 	req->r_flags = lreq->t.flags;
2996922dab61SIlya Dryomov 	req->r_mtime = lreq->mtime;
2997922dab61SIlya Dryomov 
2998922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
299919079203SIlya Dryomov 	if (lreq->is_watch && lreq->committed) {
3000922dab61SIlya Dryomov 		WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
3001922dab61SIlya Dryomov 			op->watch.cookie != lreq->linger_id);
3002922dab61SIlya Dryomov 		op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
3003922dab61SIlya Dryomov 		op->watch.gen = ++lreq->register_gen;
3004922dab61SIlya Dryomov 		dout("lreq %p reconnect register_gen %u\n", lreq,
3005922dab61SIlya Dryomov 		     op->watch.gen);
3006922dab61SIlya Dryomov 		req->r_callback = linger_reconnect_cb;
3007922dab61SIlya Dryomov 	} else {
300819079203SIlya Dryomov 		if (!lreq->is_watch)
300919079203SIlya Dryomov 			lreq->notify_id = 0;
301019079203SIlya Dryomov 		else
3011922dab61SIlya Dryomov 			WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
3012922dab61SIlya Dryomov 		dout("lreq %p register\n", lreq);
3013922dab61SIlya Dryomov 		req->r_callback = linger_commit_cb;
3014922dab61SIlya Dryomov 	}
3015922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
3016922dab61SIlya Dryomov 
3017922dab61SIlya Dryomov 	req->r_priv = linger_get(lreq);
3018922dab61SIlya Dryomov 	req->r_linger = true;
3019922dab61SIlya Dryomov 
3020922dab61SIlya Dryomov 	submit_request(req, true);
3021922dab61SIlya Dryomov }
3022922dab61SIlya Dryomov 
3023922dab61SIlya Dryomov static void linger_ping_cb(struct ceph_osd_request *req)
3024922dab61SIlya Dryomov {
3025922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq = req->r_priv;
3026922dab61SIlya Dryomov 
3027922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
3028922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
3029922dab61SIlya Dryomov 	     __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
3030922dab61SIlya Dryomov 	     lreq->last_error);
3031922dab61SIlya Dryomov 	if (lreq->register_gen == req->r_ops[0].watch.gen) {
3032b07d3c4bSIlya Dryomov 		if (!req->r_result) {
3033b07d3c4bSIlya Dryomov 			lreq->watch_valid_thru = lreq->ping_sent;
3034b07d3c4bSIlya Dryomov 		} else if (!lreq->last_error) {
3035922dab61SIlya Dryomov 			lreq->last_error = normalize_watch_error(req->r_result);
3036922dab61SIlya Dryomov 			queue_watch_error(lreq);
3037922dab61SIlya Dryomov 		}
3038922dab61SIlya Dryomov 	} else {
3039922dab61SIlya Dryomov 		dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
3040922dab61SIlya Dryomov 		     lreq->register_gen, req->r_ops[0].watch.gen);
3041922dab61SIlya Dryomov 	}
3042922dab61SIlya Dryomov 
3043922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
3044922dab61SIlya Dryomov 	linger_put(lreq);
3045922dab61SIlya Dryomov }
3046922dab61SIlya Dryomov 
3047922dab61SIlya Dryomov static void send_linger_ping(struct ceph_osd_linger_request *lreq)
3048922dab61SIlya Dryomov {
3049922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3050922dab61SIlya Dryomov 	struct ceph_osd_request *req = lreq->ping_req;
3051922dab61SIlya Dryomov 	struct ceph_osd_req_op *op = &req->r_ops[0];
3052922dab61SIlya Dryomov 
3053b7ec35b3SIlya Dryomov 	if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
3054922dab61SIlya Dryomov 		dout("%s PAUSERD\n", __func__);
3055922dab61SIlya Dryomov 		return;
3056922dab61SIlya Dryomov 	}
3057922dab61SIlya Dryomov 
3058922dab61SIlya Dryomov 	lreq->ping_sent = jiffies;
3059922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
3060922dab61SIlya Dryomov 	     __func__, lreq, lreq->linger_id, lreq->ping_sent,
3061922dab61SIlya Dryomov 	     lreq->register_gen);
3062922dab61SIlya Dryomov 
3063922dab61SIlya Dryomov 	if (req->r_osd)
3064922dab61SIlya Dryomov 		cancel_linger_request(req);
3065922dab61SIlya Dryomov 
3066922dab61SIlya Dryomov 	request_reinit(req);
3067922dab61SIlya Dryomov 	target_copy(&req->r_t, &lreq->t);
3068922dab61SIlya Dryomov 
3069922dab61SIlya Dryomov 	WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
3070922dab61SIlya Dryomov 		op->watch.cookie != lreq->linger_id ||
3071922dab61SIlya Dryomov 		op->watch.op != CEPH_OSD_WATCH_OP_PING);
3072922dab61SIlya Dryomov 	op->watch.gen = lreq->register_gen;
3073922dab61SIlya Dryomov 	req->r_callback = linger_ping_cb;
3074922dab61SIlya Dryomov 	req->r_priv = linger_get(lreq);
3075922dab61SIlya Dryomov 	req->r_linger = true;
3076922dab61SIlya Dryomov 
3077922dab61SIlya Dryomov 	ceph_osdc_get_request(req);
3078922dab61SIlya Dryomov 	account_request(req);
3079922dab61SIlya Dryomov 	req->r_tid = atomic64_inc_return(&osdc->last_tid);
3080922dab61SIlya Dryomov 	link_request(lreq->osd, req);
3081922dab61SIlya Dryomov 	send_request(req);
3082922dab61SIlya Dryomov }
3083922dab61SIlya Dryomov 
3084922dab61SIlya Dryomov static void linger_submit(struct ceph_osd_linger_request *lreq)
3085922dab61SIlya Dryomov {
3086922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3087922dab61SIlya Dryomov 	struct ceph_osd *osd;
3088922dab61SIlya Dryomov 
308981c65213SIlya Dryomov 	down_write(&osdc->lock);
309081c65213SIlya Dryomov 	linger_register(lreq);
309181c65213SIlya Dryomov 	if (lreq->is_watch) {
309281c65213SIlya Dryomov 		lreq->reg_req->r_ops[0].watch.cookie = lreq->linger_id;
309381c65213SIlya Dryomov 		lreq->ping_req->r_ops[0].watch.cookie = lreq->linger_id;
309481c65213SIlya Dryomov 	} else {
309581c65213SIlya Dryomov 		lreq->reg_req->r_ops[0].notify.cookie = lreq->linger_id;
309681c65213SIlya Dryomov 	}
309781c65213SIlya Dryomov 
30987de030d6SIlya Dryomov 	calc_target(osdc, &lreq->t, NULL, false);
3099922dab61SIlya Dryomov 	osd = lookup_create_osd(osdc, lreq->t.osd, true);
3100922dab61SIlya Dryomov 	link_linger(osd, lreq);
3101922dab61SIlya Dryomov 
3102922dab61SIlya Dryomov 	send_linger(lreq);
310381c65213SIlya Dryomov 	up_write(&osdc->lock);
3104922dab61SIlya Dryomov }
3105922dab61SIlya Dryomov 
31064609245eSIlya Dryomov static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
31074609245eSIlya Dryomov {
31084609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
31094609245eSIlya Dryomov 	struct ceph_osd_linger_request *lookup_lreq;
31104609245eSIlya Dryomov 
31114609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
31124609245eSIlya Dryomov 
31134609245eSIlya Dryomov 	lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
31144609245eSIlya Dryomov 				       lreq->linger_id);
31154609245eSIlya Dryomov 	if (!lookup_lreq)
31164609245eSIlya Dryomov 		return;
31174609245eSIlya Dryomov 
31184609245eSIlya Dryomov 	WARN_ON(lookup_lreq != lreq);
31194609245eSIlya Dryomov 	erase_linger_mc(&osdc->linger_map_checks, lreq);
31204609245eSIlya Dryomov 	linger_put(lreq);
31214609245eSIlya Dryomov }
31224609245eSIlya Dryomov 
3123922dab61SIlya Dryomov /*
3124922dab61SIlya Dryomov  * @lreq has to be both registered and linked.
3125922dab61SIlya Dryomov  */
3126922dab61SIlya Dryomov static void __linger_cancel(struct ceph_osd_linger_request *lreq)
3127922dab61SIlya Dryomov {
312819079203SIlya Dryomov 	if (lreq->is_watch && lreq->ping_req->r_osd)
3129922dab61SIlya Dryomov 		cancel_linger_request(lreq->ping_req);
3130922dab61SIlya Dryomov 	if (lreq->reg_req->r_osd)
3131922dab61SIlya Dryomov 		cancel_linger_request(lreq->reg_req);
31324609245eSIlya Dryomov 	cancel_linger_map_check(lreq);
3133922dab61SIlya Dryomov 	unlink_linger(lreq->osd, lreq);
3134922dab61SIlya Dryomov 	linger_unregister(lreq);
3135922dab61SIlya Dryomov }
3136922dab61SIlya Dryomov 
3137922dab61SIlya Dryomov static void linger_cancel(struct ceph_osd_linger_request *lreq)
3138922dab61SIlya Dryomov {
3139922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3140922dab61SIlya Dryomov 
3141922dab61SIlya Dryomov 	down_write(&osdc->lock);
3142922dab61SIlya Dryomov 	if (__linger_registered(lreq))
3143922dab61SIlya Dryomov 		__linger_cancel(lreq);
3144922dab61SIlya Dryomov 	up_write(&osdc->lock);
3145922dab61SIlya Dryomov }
3146922dab61SIlya Dryomov 
31474609245eSIlya Dryomov static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
31484609245eSIlya Dryomov 
31494609245eSIlya Dryomov static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
31504609245eSIlya Dryomov {
31514609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
31524609245eSIlya Dryomov 	struct ceph_osdmap *map = osdc->osdmap;
31534609245eSIlya Dryomov 
31544609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
31554609245eSIlya Dryomov 	WARN_ON(!map->epoch);
31564609245eSIlya Dryomov 
31574609245eSIlya Dryomov 	if (lreq->register_gen) {
31584609245eSIlya Dryomov 		lreq->map_dne_bound = map->epoch;
31594609245eSIlya Dryomov 		dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
31604609245eSIlya Dryomov 		     lreq, lreq->linger_id);
31614609245eSIlya Dryomov 	} else {
31624609245eSIlya Dryomov 		dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
31634609245eSIlya Dryomov 		     __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
31644609245eSIlya Dryomov 		     map->epoch);
31654609245eSIlya Dryomov 	}
31664609245eSIlya Dryomov 
31674609245eSIlya Dryomov 	if (lreq->map_dne_bound) {
31684609245eSIlya Dryomov 		if (map->epoch >= lreq->map_dne_bound) {
31694609245eSIlya Dryomov 			/* we had a new enough map */
31704609245eSIlya Dryomov 			pr_info("linger_id %llu pool does not exist\n",
31714609245eSIlya Dryomov 				lreq->linger_id);
31724609245eSIlya Dryomov 			linger_reg_commit_complete(lreq, -ENOENT);
31734609245eSIlya Dryomov 			__linger_cancel(lreq);
31744609245eSIlya Dryomov 		}
31754609245eSIlya Dryomov 	} else {
31764609245eSIlya Dryomov 		send_linger_map_check(lreq);
31774609245eSIlya Dryomov 	}
31784609245eSIlya Dryomov }
31794609245eSIlya Dryomov 
31804609245eSIlya Dryomov static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
31814609245eSIlya Dryomov {
31824609245eSIlya Dryomov 	struct ceph_osd_client *osdc = &greq->monc->client->osdc;
31834609245eSIlya Dryomov 	struct ceph_osd_linger_request *lreq;
31844609245eSIlya Dryomov 	u64 linger_id = greq->private_data;
31854609245eSIlya Dryomov 
31864609245eSIlya Dryomov 	WARN_ON(greq->result || !greq->u.newest);
31874609245eSIlya Dryomov 
31884609245eSIlya Dryomov 	down_write(&osdc->lock);
31894609245eSIlya Dryomov 	lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
31904609245eSIlya Dryomov 	if (!lreq) {
31914609245eSIlya Dryomov 		dout("%s linger_id %llu dne\n", __func__, linger_id);
31924609245eSIlya Dryomov 		goto out_unlock;
31934609245eSIlya Dryomov 	}
31944609245eSIlya Dryomov 
31954609245eSIlya Dryomov 	dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
31964609245eSIlya Dryomov 	     __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
31974609245eSIlya Dryomov 	     greq->u.newest);
31984609245eSIlya Dryomov 	if (!lreq->map_dne_bound)
31994609245eSIlya Dryomov 		lreq->map_dne_bound = greq->u.newest;
32004609245eSIlya Dryomov 	erase_linger_mc(&osdc->linger_map_checks, lreq);
32014609245eSIlya Dryomov 	check_linger_pool_dne(lreq);
32024609245eSIlya Dryomov 
32034609245eSIlya Dryomov 	linger_put(lreq);
32044609245eSIlya Dryomov out_unlock:
32054609245eSIlya Dryomov 	up_write(&osdc->lock);
32064609245eSIlya Dryomov }
32074609245eSIlya Dryomov 
32084609245eSIlya Dryomov static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
32094609245eSIlya Dryomov {
32104609245eSIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
32114609245eSIlya Dryomov 	struct ceph_osd_linger_request *lookup_lreq;
32124609245eSIlya Dryomov 	int ret;
32134609245eSIlya Dryomov 
32144609245eSIlya Dryomov 	verify_osdc_wrlocked(osdc);
32154609245eSIlya Dryomov 
32164609245eSIlya Dryomov 	lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
32174609245eSIlya Dryomov 				       lreq->linger_id);
32184609245eSIlya Dryomov 	if (lookup_lreq) {
32194609245eSIlya Dryomov 		WARN_ON(lookup_lreq != lreq);
32204609245eSIlya Dryomov 		return;
32214609245eSIlya Dryomov 	}
32224609245eSIlya Dryomov 
32234609245eSIlya Dryomov 	linger_get(lreq);
32244609245eSIlya Dryomov 	insert_linger_mc(&osdc->linger_map_checks, lreq);
32254609245eSIlya Dryomov 	ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
32264609245eSIlya Dryomov 					  linger_map_check_cb, lreq->linger_id);
32274609245eSIlya Dryomov 	WARN_ON(ret);
32284609245eSIlya Dryomov }
32294609245eSIlya Dryomov 
3230922dab61SIlya Dryomov static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
3231922dab61SIlya Dryomov {
3232922dab61SIlya Dryomov 	int ret;
3233922dab61SIlya Dryomov 
3234922dab61SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3235922dab61SIlya Dryomov 	ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
3236922dab61SIlya Dryomov 	return ret ?: lreq->reg_commit_error;
3237922dab61SIlya Dryomov }
3238922dab61SIlya Dryomov 
323919079203SIlya Dryomov static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
324019079203SIlya Dryomov {
324119079203SIlya Dryomov 	int ret;
324219079203SIlya Dryomov 
324319079203SIlya Dryomov 	dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
324419079203SIlya Dryomov 	ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
324519079203SIlya Dryomov 	return ret ?: lreq->notify_finish_error;
324619079203SIlya Dryomov }
324719079203SIlya Dryomov 
3248922dab61SIlya Dryomov /*
3249fbca9635SIlya Dryomov  * Timeout callback, called every N seconds.  When 1 or more OSD
3250fbca9635SIlya Dryomov  * requests has been active for more than N seconds, we send a keepalive
3251fbca9635SIlya Dryomov  * (tag + timestamp) to its OSD to ensure any communications channel
3252fbca9635SIlya Dryomov  * reset is detected.
32533d14c5d2SYehuda Sadeh  */
32543d14c5d2SYehuda Sadeh static void handle_timeout(struct work_struct *work)
32553d14c5d2SYehuda Sadeh {
32563d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc =
32573d14c5d2SYehuda Sadeh 		container_of(work, struct ceph_osd_client, timeout_work.work);
3258a319bf56SIlya Dryomov 	struct ceph_options *opts = osdc->client->options;
32595aea3dcdSIlya Dryomov 	unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
32607cc5e38fSIlya Dryomov 	unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
32615aea3dcdSIlya Dryomov 	LIST_HEAD(slow_osds);
32625aea3dcdSIlya Dryomov 	struct rb_node *n, *p;
32633d14c5d2SYehuda Sadeh 
32645aea3dcdSIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
32655aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
32663d14c5d2SYehuda Sadeh 
32673d14c5d2SYehuda Sadeh 	/*
32683d14c5d2SYehuda Sadeh 	 * ping osds that are a bit slow.  this ensures that if there
32693d14c5d2SYehuda Sadeh 	 * is a break in the TCP connection we will notice, and reopen
32703d14c5d2SYehuda Sadeh 	 * a connection with that osd (from the fault callback).
32713d14c5d2SYehuda Sadeh 	 */
32725aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
32735aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
32745aea3dcdSIlya Dryomov 		bool found = false;
32753d14c5d2SYehuda Sadeh 
32767cc5e38fSIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; ) {
32775aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
32785aea3dcdSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
32795aea3dcdSIlya Dryomov 
32807cc5e38fSIlya Dryomov 			p = rb_next(p); /* abort_request() */
32817cc5e38fSIlya Dryomov 
32825aea3dcdSIlya Dryomov 			if (time_before(req->r_stamp, cutoff)) {
32835aea3dcdSIlya Dryomov 				dout(" req %p tid %llu on osd%d is laggy\n",
32845aea3dcdSIlya Dryomov 				     req, req->r_tid, osd->o_osd);
32855aea3dcdSIlya Dryomov 				found = true;
32865aea3dcdSIlya Dryomov 			}
32877cc5e38fSIlya Dryomov 			if (opts->osd_request_timeout &&
32887cc5e38fSIlya Dryomov 			    time_before(req->r_start_stamp, expiry_cutoff)) {
32897cc5e38fSIlya Dryomov 				pr_err_ratelimited("tid %llu on osd%d timeout\n",
32907cc5e38fSIlya Dryomov 				       req->r_tid, osd->o_osd);
32917cc5e38fSIlya Dryomov 				abort_request(req, -ETIMEDOUT);
32927cc5e38fSIlya Dryomov 			}
32935aea3dcdSIlya Dryomov 		}
3294922dab61SIlya Dryomov 		for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
3295922dab61SIlya Dryomov 			struct ceph_osd_linger_request *lreq =
3296922dab61SIlya Dryomov 			    rb_entry(p, struct ceph_osd_linger_request, node);
3297922dab61SIlya Dryomov 
3298922dab61SIlya Dryomov 			dout(" lreq %p linger_id %llu is served by osd%d\n",
3299922dab61SIlya Dryomov 			     lreq, lreq->linger_id, osd->o_osd);
3300922dab61SIlya Dryomov 			found = true;
3301922dab61SIlya Dryomov 
3302922dab61SIlya Dryomov 			mutex_lock(&lreq->lock);
330319079203SIlya Dryomov 			if (lreq->is_watch && lreq->committed && !lreq->last_error)
3304922dab61SIlya Dryomov 				send_linger_ping(lreq);
3305922dab61SIlya Dryomov 			mutex_unlock(&lreq->lock);
3306922dab61SIlya Dryomov 		}
33075aea3dcdSIlya Dryomov 
33085aea3dcdSIlya Dryomov 		if (found)
33093d14c5d2SYehuda Sadeh 			list_move_tail(&osd->o_keepalive_item, &slow_osds);
33103d14c5d2SYehuda Sadeh 	}
33115aea3dcdSIlya Dryomov 
33127cc5e38fSIlya Dryomov 	if (opts->osd_request_timeout) {
33137cc5e38fSIlya Dryomov 		for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
33147cc5e38fSIlya Dryomov 			struct ceph_osd_request *req =
33157cc5e38fSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
33167cc5e38fSIlya Dryomov 
33177cc5e38fSIlya Dryomov 			p = rb_next(p); /* abort_request() */
33187cc5e38fSIlya Dryomov 
33197cc5e38fSIlya Dryomov 			if (time_before(req->r_start_stamp, expiry_cutoff)) {
33207cc5e38fSIlya Dryomov 				pr_err_ratelimited("tid %llu on osd%d timeout\n",
33217cc5e38fSIlya Dryomov 				       req->r_tid, osdc->homeless_osd.o_osd);
33227cc5e38fSIlya Dryomov 				abort_request(req, -ETIMEDOUT);
33237cc5e38fSIlya Dryomov 			}
33247cc5e38fSIlya Dryomov 		}
33257cc5e38fSIlya Dryomov 	}
33267cc5e38fSIlya Dryomov 
33275aea3dcdSIlya Dryomov 	if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
33285aea3dcdSIlya Dryomov 		maybe_request_map(osdc);
33295aea3dcdSIlya Dryomov 
33303d14c5d2SYehuda Sadeh 	while (!list_empty(&slow_osds)) {
33315aea3dcdSIlya Dryomov 		struct ceph_osd *osd = list_first_entry(&slow_osds,
33325aea3dcdSIlya Dryomov 							struct ceph_osd,
33333d14c5d2SYehuda Sadeh 							o_keepalive_item);
33343d14c5d2SYehuda Sadeh 		list_del_init(&osd->o_keepalive_item);
33353d14c5d2SYehuda Sadeh 		ceph_con_keepalive(&osd->o_con);
33363d14c5d2SYehuda Sadeh 	}
33373d14c5d2SYehuda Sadeh 
33385aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
3339fbca9635SIlya Dryomov 	schedule_delayed_work(&osdc->timeout_work,
3340fbca9635SIlya Dryomov 			      osdc->client->options->osd_keepalive_timeout);
33413d14c5d2SYehuda Sadeh }
33423d14c5d2SYehuda Sadeh 
33433d14c5d2SYehuda Sadeh static void handle_osds_timeout(struct work_struct *work)
33443d14c5d2SYehuda Sadeh {
33453d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc =
33463d14c5d2SYehuda Sadeh 		container_of(work, struct ceph_osd_client,
33473d14c5d2SYehuda Sadeh 			     osds_timeout_work.work);
3348a319bf56SIlya Dryomov 	unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
334942a2c09fSIlya Dryomov 	struct ceph_osd *osd, *nosd;
33503d14c5d2SYehuda Sadeh 
335142a2c09fSIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
33525aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
335342a2c09fSIlya Dryomov 	list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
335442a2c09fSIlya Dryomov 		if (time_before(jiffies, osd->lru_ttl))
335542a2c09fSIlya Dryomov 			break;
335642a2c09fSIlya Dryomov 
33575aea3dcdSIlya Dryomov 		WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
3358922dab61SIlya Dryomov 		WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
33595aea3dcdSIlya Dryomov 		close_osd(osd);
336042a2c09fSIlya Dryomov 	}
336142a2c09fSIlya Dryomov 
33625aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
33633d14c5d2SYehuda Sadeh 	schedule_delayed_work(&osdc->osds_timeout_work,
33643d14c5d2SYehuda Sadeh 			      round_jiffies_relative(delay));
33653d14c5d2SYehuda Sadeh }
33663d14c5d2SYehuda Sadeh 
3367205ee118SIlya Dryomov static int ceph_oloc_decode(void **p, void *end,
3368205ee118SIlya Dryomov 			    struct ceph_object_locator *oloc)
3369205ee118SIlya Dryomov {
3370205ee118SIlya Dryomov 	u8 struct_v, struct_cv;
3371205ee118SIlya Dryomov 	u32 len;
3372205ee118SIlya Dryomov 	void *struct_end;
3373205ee118SIlya Dryomov 	int ret = 0;
3374205ee118SIlya Dryomov 
3375205ee118SIlya Dryomov 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3376205ee118SIlya Dryomov 	struct_v = ceph_decode_8(p);
3377205ee118SIlya Dryomov 	struct_cv = ceph_decode_8(p);
3378205ee118SIlya Dryomov 	if (struct_v < 3) {
3379205ee118SIlya Dryomov 		pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
3380205ee118SIlya Dryomov 			struct_v, struct_cv);
3381205ee118SIlya Dryomov 		goto e_inval;
3382205ee118SIlya Dryomov 	}
3383205ee118SIlya Dryomov 	if (struct_cv > 6) {
3384205ee118SIlya Dryomov 		pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
3385205ee118SIlya Dryomov 			struct_v, struct_cv);
3386205ee118SIlya Dryomov 		goto e_inval;
3387205ee118SIlya Dryomov 	}
3388205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3389205ee118SIlya Dryomov 	ceph_decode_need(p, end, len, e_inval);
3390205ee118SIlya Dryomov 	struct_end = *p + len;
3391205ee118SIlya Dryomov 
3392205ee118SIlya Dryomov 	oloc->pool = ceph_decode_64(p);
3393205ee118SIlya Dryomov 	*p += 4; /* skip preferred */
3394205ee118SIlya Dryomov 
3395205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3396205ee118SIlya Dryomov 	if (len > 0) {
3397205ee118SIlya Dryomov 		pr_warn("ceph_object_locator::key is set\n");
3398205ee118SIlya Dryomov 		goto e_inval;
3399205ee118SIlya Dryomov 	}
3400205ee118SIlya Dryomov 
3401205ee118SIlya Dryomov 	if (struct_v >= 5) {
3402cd08e0a2SYan, Zheng 		bool changed = false;
3403cd08e0a2SYan, Zheng 
3404205ee118SIlya Dryomov 		len = ceph_decode_32(p);
3405205ee118SIlya Dryomov 		if (len > 0) {
340630c156d9SYan, Zheng 			ceph_decode_need(p, end, len, e_inval);
3407cd08e0a2SYan, Zheng 			if (!oloc->pool_ns ||
3408cd08e0a2SYan, Zheng 			    ceph_compare_string(oloc->pool_ns, *p, len))
3409cd08e0a2SYan, Zheng 				changed = true;
341030c156d9SYan, Zheng 			*p += len;
3411cd08e0a2SYan, Zheng 		} else {
3412cd08e0a2SYan, Zheng 			if (oloc->pool_ns)
3413cd08e0a2SYan, Zheng 				changed = true;
3414cd08e0a2SYan, Zheng 		}
3415cd08e0a2SYan, Zheng 		if (changed) {
3416cd08e0a2SYan, Zheng 			/* redirect changes namespace */
3417cd08e0a2SYan, Zheng 			pr_warn("ceph_object_locator::nspace is changed\n");
3418cd08e0a2SYan, Zheng 			goto e_inval;
3419205ee118SIlya Dryomov 		}
3420205ee118SIlya Dryomov 	}
3421205ee118SIlya Dryomov 
3422205ee118SIlya Dryomov 	if (struct_v >= 6) {
3423205ee118SIlya Dryomov 		s64 hash = ceph_decode_64(p);
3424205ee118SIlya Dryomov 		if (hash != -1) {
3425205ee118SIlya Dryomov 			pr_warn("ceph_object_locator::hash is set\n");
3426205ee118SIlya Dryomov 			goto e_inval;
3427205ee118SIlya Dryomov 		}
3428205ee118SIlya Dryomov 	}
3429205ee118SIlya Dryomov 
3430205ee118SIlya Dryomov 	/* skip the rest */
3431205ee118SIlya Dryomov 	*p = struct_end;
3432205ee118SIlya Dryomov out:
3433205ee118SIlya Dryomov 	return ret;
3434205ee118SIlya Dryomov 
3435205ee118SIlya Dryomov e_inval:
3436205ee118SIlya Dryomov 	ret = -EINVAL;
3437205ee118SIlya Dryomov 	goto out;
3438205ee118SIlya Dryomov }
3439205ee118SIlya Dryomov 
3440205ee118SIlya Dryomov static int ceph_redirect_decode(void **p, void *end,
3441205ee118SIlya Dryomov 				struct ceph_request_redirect *redir)
3442205ee118SIlya Dryomov {
3443205ee118SIlya Dryomov 	u8 struct_v, struct_cv;
3444205ee118SIlya Dryomov 	u32 len;
3445205ee118SIlya Dryomov 	void *struct_end;
3446205ee118SIlya Dryomov 	int ret;
3447205ee118SIlya Dryomov 
3448205ee118SIlya Dryomov 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3449205ee118SIlya Dryomov 	struct_v = ceph_decode_8(p);
3450205ee118SIlya Dryomov 	struct_cv = ceph_decode_8(p);
3451205ee118SIlya Dryomov 	if (struct_cv > 1) {
3452205ee118SIlya Dryomov 		pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
3453205ee118SIlya Dryomov 			struct_v, struct_cv);
3454205ee118SIlya Dryomov 		goto e_inval;
3455205ee118SIlya Dryomov 	}
3456205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3457205ee118SIlya Dryomov 	ceph_decode_need(p, end, len, e_inval);
3458205ee118SIlya Dryomov 	struct_end = *p + len;
3459205ee118SIlya Dryomov 
3460205ee118SIlya Dryomov 	ret = ceph_oloc_decode(p, end, &redir->oloc);
3461205ee118SIlya Dryomov 	if (ret)
3462205ee118SIlya Dryomov 		goto out;
3463205ee118SIlya Dryomov 
3464205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3465205ee118SIlya Dryomov 	if (len > 0) {
3466205ee118SIlya Dryomov 		pr_warn("ceph_request_redirect::object_name is set\n");
3467205ee118SIlya Dryomov 		goto e_inval;
3468205ee118SIlya Dryomov 	}
3469205ee118SIlya Dryomov 
3470205ee118SIlya Dryomov 	len = ceph_decode_32(p);
3471205ee118SIlya Dryomov 	*p += len; /* skip osd_instructions */
3472205ee118SIlya Dryomov 
3473205ee118SIlya Dryomov 	/* skip the rest */
3474205ee118SIlya Dryomov 	*p = struct_end;
3475205ee118SIlya Dryomov out:
3476205ee118SIlya Dryomov 	return ret;
3477205ee118SIlya Dryomov 
3478205ee118SIlya Dryomov e_inval:
3479205ee118SIlya Dryomov 	ret = -EINVAL;
3480205ee118SIlya Dryomov 	goto out;
3481205ee118SIlya Dryomov }
3482205ee118SIlya Dryomov 
3483fe5da05eSIlya Dryomov struct MOSDOpReply {
3484fe5da05eSIlya Dryomov 	struct ceph_pg pgid;
3485fe5da05eSIlya Dryomov 	u64 flags;
3486fe5da05eSIlya Dryomov 	int result;
3487fe5da05eSIlya Dryomov 	u32 epoch;
3488fe5da05eSIlya Dryomov 	int num_ops;
3489fe5da05eSIlya Dryomov 	u32 outdata_len[CEPH_OSD_MAX_OPS];
3490fe5da05eSIlya Dryomov 	s32 rval[CEPH_OSD_MAX_OPS];
3491fe5da05eSIlya Dryomov 	int retry_attempt;
3492fe5da05eSIlya Dryomov 	struct ceph_eversion replay_version;
3493fe5da05eSIlya Dryomov 	u64 user_version;
3494fe5da05eSIlya Dryomov 	struct ceph_request_redirect redirect;
3495fe5da05eSIlya Dryomov };
349625845472SSage Weil 
3497fe5da05eSIlya Dryomov static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
34983d14c5d2SYehuda Sadeh {
3499fe5da05eSIlya Dryomov 	void *p = msg->front.iov_base;
3500fe5da05eSIlya Dryomov 	void *const end = p + msg->front.iov_len;
3501fe5da05eSIlya Dryomov 	u16 version = le16_to_cpu(msg->hdr.version);
3502fe5da05eSIlya Dryomov 	struct ceph_eversion bad_replay_version;
3503b0b31a8fSIlya Dryomov 	u8 decode_redir;
3504fe5da05eSIlya Dryomov 	u32 len;
3505fe5da05eSIlya Dryomov 	int ret;
3506fe5da05eSIlya Dryomov 	int i;
35073d14c5d2SYehuda Sadeh 
3508fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, len, e_inval);
3509fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, len, e_inval);
3510fe5da05eSIlya Dryomov 	p += len; /* skip oid */
35111b83bef2SSage Weil 
3512fe5da05eSIlya Dryomov 	ret = ceph_decode_pgid(&p, end, &m->pgid);
3513fe5da05eSIlya Dryomov 	if (ret)
3514fe5da05eSIlya Dryomov 		return ret;
35151b83bef2SSage Weil 
3516fe5da05eSIlya Dryomov 	ceph_decode_64_safe(&p, end, m->flags, e_inval);
3517fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->result, e_inval);
3518fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
3519fe5da05eSIlya Dryomov 	memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
3520fe5da05eSIlya Dryomov 	p += sizeof(bad_replay_version);
3521fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->epoch, e_inval);
35221b83bef2SSage Weil 
3523fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
3524fe5da05eSIlya Dryomov 	if (m->num_ops > ARRAY_SIZE(m->outdata_len))
3525fe5da05eSIlya Dryomov 		goto e_inval;
35261b83bef2SSage Weil 
3527fe5da05eSIlya Dryomov 	ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
3528fe5da05eSIlya Dryomov 			 e_inval);
3529fe5da05eSIlya Dryomov 	for (i = 0; i < m->num_ops; i++) {
35301b83bef2SSage Weil 		struct ceph_osd_op *op = p;
35311b83bef2SSage Weil 
3532fe5da05eSIlya Dryomov 		m->outdata_len[i] = le32_to_cpu(op->payload_len);
35331b83bef2SSage Weil 		p += sizeof(*op);
35341b83bef2SSage Weil 	}
3535fe5da05eSIlya Dryomov 
3536fe5da05eSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
3537fe5da05eSIlya Dryomov 	for (i = 0; i < m->num_ops; i++)
3538fe5da05eSIlya Dryomov 		ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
3539fe5da05eSIlya Dryomov 
3540fe5da05eSIlya Dryomov 	if (version >= 5) {
3541fe5da05eSIlya Dryomov 		ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
3542fe5da05eSIlya Dryomov 		memcpy(&m->replay_version, p, sizeof(m->replay_version));
3543fe5da05eSIlya Dryomov 		p += sizeof(m->replay_version);
3544fe5da05eSIlya Dryomov 		ceph_decode_64_safe(&p, end, m->user_version, e_inval);
3545fe5da05eSIlya Dryomov 	} else {
3546fe5da05eSIlya Dryomov 		m->replay_version = bad_replay_version; /* struct */
3547fe5da05eSIlya Dryomov 		m->user_version = le64_to_cpu(m->replay_version.version);
35481b83bef2SSage Weil 	}
35491b83bef2SSage Weil 
3550fe5da05eSIlya Dryomov 	if (version >= 6) {
3551fe5da05eSIlya Dryomov 		if (version >= 7)
3552fe5da05eSIlya Dryomov 			ceph_decode_8_safe(&p, end, decode_redir, e_inval);
3553b0b31a8fSIlya Dryomov 		else
3554b0b31a8fSIlya Dryomov 			decode_redir = 1;
3555b0b31a8fSIlya Dryomov 	} else {
3556b0b31a8fSIlya Dryomov 		decode_redir = 0;
3557b0b31a8fSIlya Dryomov 	}
3558b0b31a8fSIlya Dryomov 
3559b0b31a8fSIlya Dryomov 	if (decode_redir) {
3560fe5da05eSIlya Dryomov 		ret = ceph_redirect_decode(&p, end, &m->redirect);
3561fe5da05eSIlya Dryomov 		if (ret)
3562fe5da05eSIlya Dryomov 			return ret;
3563205ee118SIlya Dryomov 	} else {
3564fe5da05eSIlya Dryomov 		ceph_oloc_init(&m->redirect.oloc);
3565205ee118SIlya Dryomov 	}
3566205ee118SIlya Dryomov 
3567fe5da05eSIlya Dryomov 	return 0;
3568205ee118SIlya Dryomov 
3569fe5da05eSIlya Dryomov e_inval:
3570fe5da05eSIlya Dryomov 	return -EINVAL;
3571fe5da05eSIlya Dryomov }
3572fe5da05eSIlya Dryomov 
3573fe5da05eSIlya Dryomov /*
3574b18b9550SIlya Dryomov  * Handle MOSDOpReply.  Set ->r_result and call the callback if it is
3575b18b9550SIlya Dryomov  * specified.
3576fe5da05eSIlya Dryomov  */
35775aea3dcdSIlya Dryomov static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
3578fe5da05eSIlya Dryomov {
35795aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
3580fe5da05eSIlya Dryomov 	struct ceph_osd_request *req;
3581fe5da05eSIlya Dryomov 	struct MOSDOpReply m;
3582fe5da05eSIlya Dryomov 	u64 tid = le64_to_cpu(msg->hdr.tid);
3583fe5da05eSIlya Dryomov 	u32 data_len = 0;
3584fe5da05eSIlya Dryomov 	int ret;
3585fe5da05eSIlya Dryomov 	int i;
3586fe5da05eSIlya Dryomov 
3587fe5da05eSIlya Dryomov 	dout("%s msg %p tid %llu\n", __func__, msg, tid);
3588fe5da05eSIlya Dryomov 
35895aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
35905aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
35915aea3dcdSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
35925aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
3593fe5da05eSIlya Dryomov 	}
35945aea3dcdSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
35955aea3dcdSIlya Dryomov 
35965aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
35975aea3dcdSIlya Dryomov 	req = lookup_request(&osd->o_requests, tid);
35985aea3dcdSIlya Dryomov 	if (!req) {
35995aea3dcdSIlya Dryomov 		dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
36005aea3dcdSIlya Dryomov 		goto out_unlock_session;
36015aea3dcdSIlya Dryomov 	}
3602fe5da05eSIlya Dryomov 
3603cd08e0a2SYan, Zheng 	m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
3604fe5da05eSIlya Dryomov 	ret = decode_MOSDOpReply(msg, &m);
3605cd08e0a2SYan, Zheng 	m.redirect.oloc.pool_ns = NULL;
3606fe5da05eSIlya Dryomov 	if (ret) {
3607fe5da05eSIlya Dryomov 		pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
3608fe5da05eSIlya Dryomov 		       req->r_tid, ret);
3609fe5da05eSIlya Dryomov 		ceph_msg_dump(msg);
3610fe5da05eSIlya Dryomov 		goto fail_request;
3611fe5da05eSIlya Dryomov 	}
3612fe5da05eSIlya Dryomov 	dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
3613fe5da05eSIlya Dryomov 	     __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
3614fe5da05eSIlya Dryomov 	     m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
3615fe5da05eSIlya Dryomov 	     le64_to_cpu(m.replay_version.version), m.user_version);
3616fe5da05eSIlya Dryomov 
3617fe5da05eSIlya Dryomov 	if (m.retry_attempt >= 0) {
3618fe5da05eSIlya Dryomov 		if (m.retry_attempt != req->r_attempts - 1) {
3619fe5da05eSIlya Dryomov 			dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
3620fe5da05eSIlya Dryomov 			     req, req->r_tid, m.retry_attempt,
3621fe5da05eSIlya Dryomov 			     req->r_attempts - 1);
36225aea3dcdSIlya Dryomov 			goto out_unlock_session;
3623fe5da05eSIlya Dryomov 		}
3624fe5da05eSIlya Dryomov 	} else {
3625fe5da05eSIlya Dryomov 		WARN_ON(1); /* MOSDOpReply v4 is assumed */
3626fe5da05eSIlya Dryomov 	}
3627fe5da05eSIlya Dryomov 
3628fe5da05eSIlya Dryomov 	if (!ceph_oloc_empty(&m.redirect.oloc)) {
3629fe5da05eSIlya Dryomov 		dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
3630fe5da05eSIlya Dryomov 		     m.redirect.oloc.pool);
36315aea3dcdSIlya Dryomov 		unlink_request(osd, req);
36325aea3dcdSIlya Dryomov 		mutex_unlock(&osd->lock);
3633205ee118SIlya Dryomov 
363430c156d9SYan, Zheng 		/*
363530c156d9SYan, Zheng 		 * Not ceph_oloc_copy() - changing pool_ns is not
363630c156d9SYan, Zheng 		 * supported.
363730c156d9SYan, Zheng 		 */
363830c156d9SYan, Zheng 		req->r_t.target_oloc.pool = m.redirect.oloc.pool;
36395aea3dcdSIlya Dryomov 		req->r_flags |= CEPH_OSD_FLAG_REDIRECTED;
36405aea3dcdSIlya Dryomov 		req->r_tid = 0;
36415aea3dcdSIlya Dryomov 		__submit_request(req, false);
36425aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
3643205ee118SIlya Dryomov 	}
3644205ee118SIlya Dryomov 
3645fe5da05eSIlya Dryomov 	if (m.num_ops != req->r_num_ops) {
3646fe5da05eSIlya Dryomov 		pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
3647fe5da05eSIlya Dryomov 		       req->r_num_ops, req->r_tid);
3648fe5da05eSIlya Dryomov 		goto fail_request;
3649fe5da05eSIlya Dryomov 	}
3650fe5da05eSIlya Dryomov 	for (i = 0; i < req->r_num_ops; i++) {
3651fe5da05eSIlya Dryomov 		dout(" req %p tid %llu op %d rval %d len %u\n", req,
3652fe5da05eSIlya Dryomov 		     req->r_tid, i, m.rval[i], m.outdata_len[i]);
3653fe5da05eSIlya Dryomov 		req->r_ops[i].rval = m.rval[i];
3654fe5da05eSIlya Dryomov 		req->r_ops[i].outdata_len = m.outdata_len[i];
3655fe5da05eSIlya Dryomov 		data_len += m.outdata_len[i];
3656fe5da05eSIlya Dryomov 	}
3657fe5da05eSIlya Dryomov 	if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3658fe5da05eSIlya Dryomov 		pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3659fe5da05eSIlya Dryomov 		       le32_to_cpu(msg->hdr.data_len), req->r_tid);
3660fe5da05eSIlya Dryomov 		goto fail_request;
3661fe5da05eSIlya Dryomov 	}
3662b18b9550SIlya Dryomov 	dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3663b18b9550SIlya Dryomov 	     req, req->r_tid, m.result, data_len);
36643d14c5d2SYehuda Sadeh 
3665b18b9550SIlya Dryomov 	/*
3666b18b9550SIlya Dryomov 	 * Since we only ever request ONDISK, we should only ever get
3667b18b9550SIlya Dryomov 	 * one (type of) reply back.
3668b18b9550SIlya Dryomov 	 */
3669b18b9550SIlya Dryomov 	WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3670fe5da05eSIlya Dryomov 	req->r_result = m.result ?: data_len;
367145ee2c1dSIlya Dryomov 	finish_request(req);
36725aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
36735aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
36743d14c5d2SYehuda Sadeh 
3675fe5da05eSIlya Dryomov 	__complete_request(req);
36763d14c5d2SYehuda Sadeh 	return;
3677fe5da05eSIlya Dryomov 
3678fe5da05eSIlya Dryomov fail_request:
36794609245eSIlya Dryomov 	complete_request(req, -EIO);
36805aea3dcdSIlya Dryomov out_unlock_session:
36815aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
36825aea3dcdSIlya Dryomov out_unlock_osdc:
36835aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
36843d14c5d2SYehuda Sadeh }
36853d14c5d2SYehuda Sadeh 
368642c1b124SIlya Dryomov static void set_pool_was_full(struct ceph_osd_client *osdc)
368742c1b124SIlya Dryomov {
368842c1b124SIlya Dryomov 	struct rb_node *n;
368942c1b124SIlya Dryomov 
369042c1b124SIlya Dryomov 	for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
369142c1b124SIlya Dryomov 		struct ceph_pg_pool_info *pi =
369242c1b124SIlya Dryomov 		    rb_entry(n, struct ceph_pg_pool_info, node);
369342c1b124SIlya Dryomov 
369442c1b124SIlya Dryomov 		pi->was_full = __pool_full(pi);
369542c1b124SIlya Dryomov 	}
369642c1b124SIlya Dryomov }
369742c1b124SIlya Dryomov 
36985aea3dcdSIlya Dryomov static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
36993d14c5d2SYehuda Sadeh {
37005aea3dcdSIlya Dryomov 	struct ceph_pg_pool_info *pi;
37013d14c5d2SYehuda Sadeh 
37025aea3dcdSIlya Dryomov 	pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
37035aea3dcdSIlya Dryomov 	if (!pi)
37045aea3dcdSIlya Dryomov 		return false;
37053d14c5d2SYehuda Sadeh 
37065aea3dcdSIlya Dryomov 	return pi->was_full && !__pool_full(pi);
37073d14c5d2SYehuda Sadeh }
37083d14c5d2SYehuda Sadeh 
3709922dab61SIlya Dryomov static enum calc_target_result
3710922dab61SIlya Dryomov recalc_linger_target(struct ceph_osd_linger_request *lreq)
3711922dab61SIlya Dryomov {
3712922dab61SIlya Dryomov 	struct ceph_osd_client *osdc = lreq->osdc;
3713922dab61SIlya Dryomov 	enum calc_target_result ct_res;
3714922dab61SIlya Dryomov 
37157de030d6SIlya Dryomov 	ct_res = calc_target(osdc, &lreq->t, NULL, true);
3716922dab61SIlya Dryomov 	if (ct_res == CALC_TARGET_NEED_RESEND) {
3717922dab61SIlya Dryomov 		struct ceph_osd *osd;
3718922dab61SIlya Dryomov 
3719922dab61SIlya Dryomov 		osd = lookup_create_osd(osdc, lreq->t.osd, true);
3720922dab61SIlya Dryomov 		if (osd != lreq->osd) {
3721922dab61SIlya Dryomov 			unlink_linger(lreq->osd, lreq);
3722922dab61SIlya Dryomov 			link_linger(osd, lreq);
3723922dab61SIlya Dryomov 		}
3724922dab61SIlya Dryomov 	}
3725922dab61SIlya Dryomov 
3726922dab61SIlya Dryomov 	return ct_res;
3727922dab61SIlya Dryomov }
3728922dab61SIlya Dryomov 
37293d14c5d2SYehuda Sadeh /*
37305aea3dcdSIlya Dryomov  * Requeue requests whose mapping to an OSD has changed.
37313d14c5d2SYehuda Sadeh  */
37325aea3dcdSIlya Dryomov static void scan_requests(struct ceph_osd *osd,
37335aea3dcdSIlya Dryomov 			  bool force_resend,
37345aea3dcdSIlya Dryomov 			  bool cleared_full,
37355aea3dcdSIlya Dryomov 			  bool check_pool_cleared_full,
37365aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
37375aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
37383d14c5d2SYehuda Sadeh {
37395aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
37405aea3dcdSIlya Dryomov 	struct rb_node *n;
37415aea3dcdSIlya Dryomov 	bool force_resend_writes;
37423d14c5d2SYehuda Sadeh 
3743922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; ) {
3744922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
3745922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
3746922dab61SIlya Dryomov 		enum calc_target_result ct_res;
3747922dab61SIlya Dryomov 
3748922dab61SIlya Dryomov 		n = rb_next(n); /* recalc_linger_target() */
3749922dab61SIlya Dryomov 
3750922dab61SIlya Dryomov 		dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3751922dab61SIlya Dryomov 		     lreq->linger_id);
3752922dab61SIlya Dryomov 		ct_res = recalc_linger_target(lreq);
3753922dab61SIlya Dryomov 		switch (ct_res) {
3754922dab61SIlya Dryomov 		case CALC_TARGET_NO_ACTION:
3755922dab61SIlya Dryomov 			force_resend_writes = cleared_full ||
3756922dab61SIlya Dryomov 			    (check_pool_cleared_full &&
3757922dab61SIlya Dryomov 			     pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3758922dab61SIlya Dryomov 			if (!force_resend && !force_resend_writes)
3759922dab61SIlya Dryomov 				break;
3760922dab61SIlya Dryomov 
3761922dab61SIlya Dryomov 			/* fall through */
3762922dab61SIlya Dryomov 		case CALC_TARGET_NEED_RESEND:
37634609245eSIlya Dryomov 			cancel_linger_map_check(lreq);
3764922dab61SIlya Dryomov 			/*
3765922dab61SIlya Dryomov 			 * scan_requests() for the previous epoch(s)
3766922dab61SIlya Dryomov 			 * may have already added it to the list, since
3767922dab61SIlya Dryomov 			 * it's not unlinked here.
3768922dab61SIlya Dryomov 			 */
3769922dab61SIlya Dryomov 			if (list_empty(&lreq->scan_item))
3770922dab61SIlya Dryomov 				list_add_tail(&lreq->scan_item, need_resend_linger);
3771922dab61SIlya Dryomov 			break;
3772922dab61SIlya Dryomov 		case CALC_TARGET_POOL_DNE:
3773a10bcb19SIlya Dryomov 			list_del_init(&lreq->scan_item);
37744609245eSIlya Dryomov 			check_linger_pool_dne(lreq);
3775922dab61SIlya Dryomov 			break;
3776922dab61SIlya Dryomov 		}
3777922dab61SIlya Dryomov 	}
3778922dab61SIlya Dryomov 
37795aea3dcdSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
37805aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
37815aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
37825aea3dcdSIlya Dryomov 		enum calc_target_result ct_res;
3783ab60b16dSAlex Elder 
37844609245eSIlya Dryomov 		n = rb_next(n); /* unlink_request(), check_pool_dne() */
3785ab60b16dSAlex Elder 
37865aea3dcdSIlya Dryomov 		dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
37877de030d6SIlya Dryomov 		ct_res = calc_target(osdc, &req->r_t, &req->r_osd->o_con,
37887de030d6SIlya Dryomov 				     false);
37895aea3dcdSIlya Dryomov 		switch (ct_res) {
37905aea3dcdSIlya Dryomov 		case CALC_TARGET_NO_ACTION:
37915aea3dcdSIlya Dryomov 			force_resend_writes = cleared_full ||
37925aea3dcdSIlya Dryomov 			    (check_pool_cleared_full &&
37935aea3dcdSIlya Dryomov 			     pool_cleared_full(osdc, req->r_t.base_oloc.pool));
37945aea3dcdSIlya Dryomov 			if (!force_resend &&
37955aea3dcdSIlya Dryomov 			    (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
37965aea3dcdSIlya Dryomov 			     !force_resend_writes))
37975aea3dcdSIlya Dryomov 				break;
3798a40c4f10SYehuda Sadeh 
37995aea3dcdSIlya Dryomov 			/* fall through */
38005aea3dcdSIlya Dryomov 		case CALC_TARGET_NEED_RESEND:
38014609245eSIlya Dryomov 			cancel_map_check(req);
38025aea3dcdSIlya Dryomov 			unlink_request(osd, req);
38035aea3dcdSIlya Dryomov 			insert_request(need_resend, req);
38045aea3dcdSIlya Dryomov 			break;
38055aea3dcdSIlya Dryomov 		case CALC_TARGET_POOL_DNE:
38064609245eSIlya Dryomov 			check_pool_dne(req);
38075aea3dcdSIlya Dryomov 			break;
3808a40c4f10SYehuda Sadeh 		}
38093d14c5d2SYehuda Sadeh 	}
38103d14c5d2SYehuda Sadeh }
38116f6c7006SSage Weil 
381242c1b124SIlya Dryomov static int handle_one_map(struct ceph_osd_client *osdc,
38135aea3dcdSIlya Dryomov 			  void *p, void *end, bool incremental,
38145aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
38155aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
381642c1b124SIlya Dryomov {
381742c1b124SIlya Dryomov 	struct ceph_osdmap *newmap;
381842c1b124SIlya Dryomov 	struct rb_node *n;
381942c1b124SIlya Dryomov 	bool skipped_map = false;
382042c1b124SIlya Dryomov 	bool was_full;
382142c1b124SIlya Dryomov 
3822b7ec35b3SIlya Dryomov 	was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
382342c1b124SIlya Dryomov 	set_pool_was_full(osdc);
382442c1b124SIlya Dryomov 
382542c1b124SIlya Dryomov 	if (incremental)
382642c1b124SIlya Dryomov 		newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
382742c1b124SIlya Dryomov 	else
382842c1b124SIlya Dryomov 		newmap = ceph_osdmap_decode(&p, end);
382942c1b124SIlya Dryomov 	if (IS_ERR(newmap))
383042c1b124SIlya Dryomov 		return PTR_ERR(newmap);
383142c1b124SIlya Dryomov 
383242c1b124SIlya Dryomov 	if (newmap != osdc->osdmap) {
383342c1b124SIlya Dryomov 		/*
383442c1b124SIlya Dryomov 		 * Preserve ->was_full before destroying the old map.
383542c1b124SIlya Dryomov 		 * For pools that weren't in the old map, ->was_full
383642c1b124SIlya Dryomov 		 * should be false.
383742c1b124SIlya Dryomov 		 */
383842c1b124SIlya Dryomov 		for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
383942c1b124SIlya Dryomov 			struct ceph_pg_pool_info *pi =
384042c1b124SIlya Dryomov 			    rb_entry(n, struct ceph_pg_pool_info, node);
384142c1b124SIlya Dryomov 			struct ceph_pg_pool_info *old_pi;
384242c1b124SIlya Dryomov 
384342c1b124SIlya Dryomov 			old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
384442c1b124SIlya Dryomov 			if (old_pi)
384542c1b124SIlya Dryomov 				pi->was_full = old_pi->was_full;
384642c1b124SIlya Dryomov 			else
384742c1b124SIlya Dryomov 				WARN_ON(pi->was_full);
384842c1b124SIlya Dryomov 		}
384942c1b124SIlya Dryomov 
385042c1b124SIlya Dryomov 		if (osdc->osdmap->epoch &&
385142c1b124SIlya Dryomov 		    osdc->osdmap->epoch + 1 < newmap->epoch) {
385242c1b124SIlya Dryomov 			WARN_ON(incremental);
385342c1b124SIlya Dryomov 			skipped_map = true;
385442c1b124SIlya Dryomov 		}
385542c1b124SIlya Dryomov 
385642c1b124SIlya Dryomov 		ceph_osdmap_destroy(osdc->osdmap);
385742c1b124SIlya Dryomov 		osdc->osdmap = newmap;
385842c1b124SIlya Dryomov 	}
385942c1b124SIlya Dryomov 
3860b7ec35b3SIlya Dryomov 	was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
38615aea3dcdSIlya Dryomov 	scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
38625aea3dcdSIlya Dryomov 		      need_resend, need_resend_linger);
38635aea3dcdSIlya Dryomov 
38645aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; ) {
38655aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
38665aea3dcdSIlya Dryomov 
38675aea3dcdSIlya Dryomov 		n = rb_next(n); /* close_osd() */
38685aea3dcdSIlya Dryomov 
38695aea3dcdSIlya Dryomov 		scan_requests(osd, skipped_map, was_full, true, need_resend,
38705aea3dcdSIlya Dryomov 			      need_resend_linger);
38715aea3dcdSIlya Dryomov 		if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
38725aea3dcdSIlya Dryomov 		    memcmp(&osd->o_con.peer_addr,
38735aea3dcdSIlya Dryomov 			   ceph_osd_addr(osdc->osdmap, osd->o_osd),
38745aea3dcdSIlya Dryomov 			   sizeof(struct ceph_entity_addr)))
38755aea3dcdSIlya Dryomov 			close_osd(osd);
38765aea3dcdSIlya Dryomov 	}
387742c1b124SIlya Dryomov 
387842c1b124SIlya Dryomov 	return 0;
387942c1b124SIlya Dryomov }
38806f6c7006SSage Weil 
38815aea3dcdSIlya Dryomov static void kick_requests(struct ceph_osd_client *osdc,
38825aea3dcdSIlya Dryomov 			  struct rb_root *need_resend,
38835aea3dcdSIlya Dryomov 			  struct list_head *need_resend_linger)
38845aea3dcdSIlya Dryomov {
3885922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq, *nlreq;
388604c7d789SIlya Dryomov 	enum calc_target_result ct_res;
38875aea3dcdSIlya Dryomov 	struct rb_node *n;
38885aea3dcdSIlya Dryomov 
388904c7d789SIlya Dryomov 	/* make sure need_resend targets reflect latest map */
389004c7d789SIlya Dryomov 	for (n = rb_first(need_resend); n; ) {
389104c7d789SIlya Dryomov 		struct ceph_osd_request *req =
389204c7d789SIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
389304c7d789SIlya Dryomov 
389404c7d789SIlya Dryomov 		n = rb_next(n);
389504c7d789SIlya Dryomov 
389604c7d789SIlya Dryomov 		if (req->r_t.epoch < osdc->osdmap->epoch) {
389704c7d789SIlya Dryomov 			ct_res = calc_target(osdc, &req->r_t, NULL, false);
389804c7d789SIlya Dryomov 			if (ct_res == CALC_TARGET_POOL_DNE) {
389904c7d789SIlya Dryomov 				erase_request(need_resend, req);
390004c7d789SIlya Dryomov 				check_pool_dne(req);
390104c7d789SIlya Dryomov 			}
390204c7d789SIlya Dryomov 		}
390304c7d789SIlya Dryomov 	}
390404c7d789SIlya Dryomov 
39055aea3dcdSIlya Dryomov 	for (n = rb_first(need_resend); n; ) {
39065aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
39075aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
39085aea3dcdSIlya Dryomov 		struct ceph_osd *osd;
39095aea3dcdSIlya Dryomov 
39105aea3dcdSIlya Dryomov 		n = rb_next(n);
39115aea3dcdSIlya Dryomov 		erase_request(need_resend, req); /* before link_request() */
39125aea3dcdSIlya Dryomov 
39135aea3dcdSIlya Dryomov 		osd = lookup_create_osd(osdc, req->r_t.osd, true);
39145aea3dcdSIlya Dryomov 		link_request(osd, req);
39155aea3dcdSIlya Dryomov 		if (!req->r_linger) {
39165aea3dcdSIlya Dryomov 			if (!osd_homeless(osd) && !req->r_t.paused)
39175aea3dcdSIlya Dryomov 				send_request(req);
3918922dab61SIlya Dryomov 		} else {
3919922dab61SIlya Dryomov 			cancel_linger_request(req);
39205aea3dcdSIlya Dryomov 		}
39215aea3dcdSIlya Dryomov 	}
3922922dab61SIlya Dryomov 
3923922dab61SIlya Dryomov 	list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
3924922dab61SIlya Dryomov 		if (!osd_homeless(lreq->osd))
3925922dab61SIlya Dryomov 			send_linger(lreq);
3926922dab61SIlya Dryomov 
3927922dab61SIlya Dryomov 		list_del_init(&lreq->scan_item);
3928922dab61SIlya Dryomov 	}
39295aea3dcdSIlya Dryomov }
39305aea3dcdSIlya Dryomov 
39313d14c5d2SYehuda Sadeh /*
39323d14c5d2SYehuda Sadeh  * Process updated osd map.
39333d14c5d2SYehuda Sadeh  *
39343d14c5d2SYehuda Sadeh  * The message contains any number of incremental and full maps, normally
39353d14c5d2SYehuda Sadeh  * indicating some sort of topology change in the cluster.  Kick requests
39363d14c5d2SYehuda Sadeh  * off to different OSDs as needed.
39373d14c5d2SYehuda Sadeh  */
39383d14c5d2SYehuda Sadeh void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
39393d14c5d2SYehuda Sadeh {
394042c1b124SIlya Dryomov 	void *p = msg->front.iov_base;
394142c1b124SIlya Dryomov 	void *const end = p + msg->front.iov_len;
39423d14c5d2SYehuda Sadeh 	u32 nr_maps, maplen;
39433d14c5d2SYehuda Sadeh 	u32 epoch;
39443d14c5d2SYehuda Sadeh 	struct ceph_fsid fsid;
39455aea3dcdSIlya Dryomov 	struct rb_root need_resend = RB_ROOT;
39465aea3dcdSIlya Dryomov 	LIST_HEAD(need_resend_linger);
394742c1b124SIlya Dryomov 	bool handled_incremental = false;
394842c1b124SIlya Dryomov 	bool was_pauserd, was_pausewr;
394942c1b124SIlya Dryomov 	bool pauserd, pausewr;
395042c1b124SIlya Dryomov 	int err;
39513d14c5d2SYehuda Sadeh 
395242c1b124SIlya Dryomov 	dout("%s have %u\n", __func__, osdc->osdmap->epoch);
39535aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
39543d14c5d2SYehuda Sadeh 
39553d14c5d2SYehuda Sadeh 	/* verify fsid */
39563d14c5d2SYehuda Sadeh 	ceph_decode_need(&p, end, sizeof(fsid), bad);
39573d14c5d2SYehuda Sadeh 	ceph_decode_copy(&p, &fsid, sizeof(fsid));
39583d14c5d2SYehuda Sadeh 	if (ceph_check_fsid(osdc->client, &fsid) < 0)
395942c1b124SIlya Dryomov 		goto bad;
39603d14c5d2SYehuda Sadeh 
3961b7ec35b3SIlya Dryomov 	was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3962b7ec35b3SIlya Dryomov 	was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3963b7ec35b3SIlya Dryomov 		      ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
396442c1b124SIlya Dryomov 		      have_pool_full(osdc);
39659a1ea2dbSJosh Durgin 
39663d14c5d2SYehuda Sadeh 	/* incremental maps */
39673d14c5d2SYehuda Sadeh 	ceph_decode_32_safe(&p, end, nr_maps, bad);
39683d14c5d2SYehuda Sadeh 	dout(" %d inc maps\n", nr_maps);
39693d14c5d2SYehuda Sadeh 	while (nr_maps > 0) {
39703d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
39713d14c5d2SYehuda Sadeh 		epoch = ceph_decode_32(&p);
39723d14c5d2SYehuda Sadeh 		maplen = ceph_decode_32(&p);
39733d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, maplen, bad);
397442c1b124SIlya Dryomov 		if (osdc->osdmap->epoch &&
397542c1b124SIlya Dryomov 		    osdc->osdmap->epoch + 1 == epoch) {
39763d14c5d2SYehuda Sadeh 			dout("applying incremental map %u len %d\n",
39773d14c5d2SYehuda Sadeh 			     epoch, maplen);
39785aea3dcdSIlya Dryomov 			err = handle_one_map(osdc, p, p + maplen, true,
39795aea3dcdSIlya Dryomov 					     &need_resend, &need_resend_linger);
398042c1b124SIlya Dryomov 			if (err)
39813d14c5d2SYehuda Sadeh 				goto bad;
398242c1b124SIlya Dryomov 			handled_incremental = true;
39833d14c5d2SYehuda Sadeh 		} else {
39843d14c5d2SYehuda Sadeh 			dout("ignoring incremental map %u len %d\n",
39853d14c5d2SYehuda Sadeh 			     epoch, maplen);
39863d14c5d2SYehuda Sadeh 		}
398742c1b124SIlya Dryomov 		p += maplen;
39883d14c5d2SYehuda Sadeh 		nr_maps--;
39893d14c5d2SYehuda Sadeh 	}
399042c1b124SIlya Dryomov 	if (handled_incremental)
39913d14c5d2SYehuda Sadeh 		goto done;
39923d14c5d2SYehuda Sadeh 
39933d14c5d2SYehuda Sadeh 	/* full maps */
39943d14c5d2SYehuda Sadeh 	ceph_decode_32_safe(&p, end, nr_maps, bad);
39953d14c5d2SYehuda Sadeh 	dout(" %d full maps\n", nr_maps);
39963d14c5d2SYehuda Sadeh 	while (nr_maps) {
39973d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
39983d14c5d2SYehuda Sadeh 		epoch = ceph_decode_32(&p);
39993d14c5d2SYehuda Sadeh 		maplen = ceph_decode_32(&p);
40003d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, maplen, bad);
40013d14c5d2SYehuda Sadeh 		if (nr_maps > 1) {
40023d14c5d2SYehuda Sadeh 			dout("skipping non-latest full map %u len %d\n",
40033d14c5d2SYehuda Sadeh 			     epoch, maplen);
4004e5253a7bSIlya Dryomov 		} else if (osdc->osdmap->epoch >= epoch) {
40053d14c5d2SYehuda Sadeh 			dout("skipping full map %u len %d, "
40063d14c5d2SYehuda Sadeh 			     "older than our %u\n", epoch, maplen,
40073d14c5d2SYehuda Sadeh 			     osdc->osdmap->epoch);
40083d14c5d2SYehuda Sadeh 		} else {
40093d14c5d2SYehuda Sadeh 			dout("taking full map %u len %d\n", epoch, maplen);
40105aea3dcdSIlya Dryomov 			err = handle_one_map(osdc, p, p + maplen, false,
40115aea3dcdSIlya Dryomov 					     &need_resend, &need_resend_linger);
401242c1b124SIlya Dryomov 			if (err)
40133d14c5d2SYehuda Sadeh 				goto bad;
40143d14c5d2SYehuda Sadeh 		}
40153d14c5d2SYehuda Sadeh 		p += maplen;
40163d14c5d2SYehuda Sadeh 		nr_maps--;
40173d14c5d2SYehuda Sadeh 	}
40183d14c5d2SYehuda Sadeh 
40193d14c5d2SYehuda Sadeh done:
4020cd634fb6SSage Weil 	/*
4021cd634fb6SSage Weil 	 * subscribe to subsequent osdmap updates if full to ensure
4022cd634fb6SSage Weil 	 * we find out when we are no longer full and stop returning
4023cd634fb6SSage Weil 	 * ENOSPC.
4024cd634fb6SSage Weil 	 */
4025b7ec35b3SIlya Dryomov 	pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
4026b7ec35b3SIlya Dryomov 	pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
4027b7ec35b3SIlya Dryomov 		  ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
402842c1b124SIlya Dryomov 		  have_pool_full(osdc);
402958eb7932SJeff Layton 	if (was_pauserd || was_pausewr || pauserd || pausewr ||
403058eb7932SJeff Layton 	    osdc->osdmap->epoch < osdc->epoch_barrier)
403142c1b124SIlya Dryomov 		maybe_request_map(osdc);
4032cd634fb6SSage Weil 
40335aea3dcdSIlya Dryomov 	kick_requests(osdc, &need_resend, &need_resend_linger);
403442c1b124SIlya Dryomov 
4035fc36d0a4SJeff Layton 	ceph_osdc_abort_on_full(osdc);
403642c1b124SIlya Dryomov 	ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
403742c1b124SIlya Dryomov 			  osdc->osdmap->epoch);
40385aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
40393d14c5d2SYehuda Sadeh 	wake_up_all(&osdc->client->auth_wq);
40403d14c5d2SYehuda Sadeh 	return;
40413d14c5d2SYehuda Sadeh 
40423d14c5d2SYehuda Sadeh bad:
40433d14c5d2SYehuda Sadeh 	pr_err("osdc handle_map corrupt msg\n");
40443d14c5d2SYehuda Sadeh 	ceph_msg_dump(msg);
40455aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
40465aea3dcdSIlya Dryomov }
40475aea3dcdSIlya Dryomov 
40485aea3dcdSIlya Dryomov /*
40495aea3dcdSIlya Dryomov  * Resubmit requests pending on the given osd.
40505aea3dcdSIlya Dryomov  */
40515aea3dcdSIlya Dryomov static void kick_osd_requests(struct ceph_osd *osd)
40525aea3dcdSIlya Dryomov {
40535aea3dcdSIlya Dryomov 	struct rb_node *n;
40545aea3dcdSIlya Dryomov 
4055a02a946dSIlya Dryomov 	clear_backoffs(osd);
4056a02a946dSIlya Dryomov 
4057922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; ) {
40585aea3dcdSIlya Dryomov 		struct ceph_osd_request *req =
40595aea3dcdSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
40605aea3dcdSIlya Dryomov 
4061922dab61SIlya Dryomov 		n = rb_next(n); /* cancel_linger_request() */
4062922dab61SIlya Dryomov 
40635aea3dcdSIlya Dryomov 		if (!req->r_linger) {
40645aea3dcdSIlya Dryomov 			if (!req->r_t.paused)
40655aea3dcdSIlya Dryomov 				send_request(req);
4066922dab61SIlya Dryomov 		} else {
4067922dab61SIlya Dryomov 			cancel_linger_request(req);
40685aea3dcdSIlya Dryomov 		}
40695aea3dcdSIlya Dryomov 	}
4070922dab61SIlya Dryomov 	for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
4071922dab61SIlya Dryomov 		struct ceph_osd_linger_request *lreq =
4072922dab61SIlya Dryomov 		    rb_entry(n, struct ceph_osd_linger_request, node);
4073922dab61SIlya Dryomov 
4074922dab61SIlya Dryomov 		send_linger(lreq);
4075922dab61SIlya Dryomov 	}
40765aea3dcdSIlya Dryomov }
40775aea3dcdSIlya Dryomov 
40785aea3dcdSIlya Dryomov /*
40795aea3dcdSIlya Dryomov  * If the osd connection drops, we need to resubmit all requests.
40805aea3dcdSIlya Dryomov  */
40815aea3dcdSIlya Dryomov static void osd_fault(struct ceph_connection *con)
40825aea3dcdSIlya Dryomov {
40835aea3dcdSIlya Dryomov 	struct ceph_osd *osd = con->private;
40845aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
40855aea3dcdSIlya Dryomov 
40865aea3dcdSIlya Dryomov 	dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
40875aea3dcdSIlya Dryomov 
40885aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
40895aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
40905aea3dcdSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
40915aea3dcdSIlya Dryomov 		goto out_unlock;
40925aea3dcdSIlya Dryomov 	}
40935aea3dcdSIlya Dryomov 
40945aea3dcdSIlya Dryomov 	if (!reopen_osd(osd))
40955aea3dcdSIlya Dryomov 		kick_osd_requests(osd);
40965aea3dcdSIlya Dryomov 	maybe_request_map(osdc);
40975aea3dcdSIlya Dryomov 
40985aea3dcdSIlya Dryomov out_unlock:
40995aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
41003d14c5d2SYehuda Sadeh }
41013d14c5d2SYehuda Sadeh 
4102a02a946dSIlya Dryomov struct MOSDBackoff {
4103a02a946dSIlya Dryomov 	struct ceph_spg spgid;
4104a02a946dSIlya Dryomov 	u32 map_epoch;
4105a02a946dSIlya Dryomov 	u8 op;
4106a02a946dSIlya Dryomov 	u64 id;
4107a02a946dSIlya Dryomov 	struct ceph_hobject_id *begin;
4108a02a946dSIlya Dryomov 	struct ceph_hobject_id *end;
4109a02a946dSIlya Dryomov };
4110a02a946dSIlya Dryomov 
4111a02a946dSIlya Dryomov static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m)
4112a02a946dSIlya Dryomov {
4113a02a946dSIlya Dryomov 	void *p = msg->front.iov_base;
4114a02a946dSIlya Dryomov 	void *const end = p + msg->front.iov_len;
4115a02a946dSIlya Dryomov 	u8 struct_v;
4116a02a946dSIlya Dryomov 	u32 struct_len;
4117a02a946dSIlya Dryomov 	int ret;
4118a02a946dSIlya Dryomov 
4119a02a946dSIlya Dryomov 	ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len);
4120a02a946dSIlya Dryomov 	if (ret)
4121a02a946dSIlya Dryomov 		return ret;
4122a02a946dSIlya Dryomov 
4123a02a946dSIlya Dryomov 	ret = ceph_decode_pgid(&p, end, &m->spgid.pgid);
4124a02a946dSIlya Dryomov 	if (ret)
4125a02a946dSIlya Dryomov 		return ret;
4126a02a946dSIlya Dryomov 
4127a02a946dSIlya Dryomov 	ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval);
4128a02a946dSIlya Dryomov 	ceph_decode_32_safe(&p, end, m->map_epoch, e_inval);
4129a02a946dSIlya Dryomov 	ceph_decode_8_safe(&p, end, m->op, e_inval);
4130a02a946dSIlya Dryomov 	ceph_decode_64_safe(&p, end, m->id, e_inval);
4131a02a946dSIlya Dryomov 
4132a02a946dSIlya Dryomov 	m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO);
4133a02a946dSIlya Dryomov 	if (!m->begin)
4134a02a946dSIlya Dryomov 		return -ENOMEM;
4135a02a946dSIlya Dryomov 
4136a02a946dSIlya Dryomov 	ret = decode_hoid(&p, end, m->begin);
4137a02a946dSIlya Dryomov 	if (ret) {
4138a02a946dSIlya Dryomov 		free_hoid(m->begin);
4139a02a946dSIlya Dryomov 		return ret;
4140a02a946dSIlya Dryomov 	}
4141a02a946dSIlya Dryomov 
4142a02a946dSIlya Dryomov 	m->end = kzalloc(sizeof(*m->end), GFP_NOIO);
4143a02a946dSIlya Dryomov 	if (!m->end) {
4144a02a946dSIlya Dryomov 		free_hoid(m->begin);
4145a02a946dSIlya Dryomov 		return -ENOMEM;
4146a02a946dSIlya Dryomov 	}
4147a02a946dSIlya Dryomov 
4148a02a946dSIlya Dryomov 	ret = decode_hoid(&p, end, m->end);
4149a02a946dSIlya Dryomov 	if (ret) {
4150a02a946dSIlya Dryomov 		free_hoid(m->begin);
4151a02a946dSIlya Dryomov 		free_hoid(m->end);
4152a02a946dSIlya Dryomov 		return ret;
4153a02a946dSIlya Dryomov 	}
4154a02a946dSIlya Dryomov 
4155a02a946dSIlya Dryomov 	return 0;
4156a02a946dSIlya Dryomov 
4157a02a946dSIlya Dryomov e_inval:
4158a02a946dSIlya Dryomov 	return -EINVAL;
4159a02a946dSIlya Dryomov }
4160a02a946dSIlya Dryomov 
4161a02a946dSIlya Dryomov static struct ceph_msg *create_backoff_message(
4162a02a946dSIlya Dryomov 				const struct ceph_osd_backoff *backoff,
4163a02a946dSIlya Dryomov 				u32 map_epoch)
4164a02a946dSIlya Dryomov {
4165a02a946dSIlya Dryomov 	struct ceph_msg *msg;
4166a02a946dSIlya Dryomov 	void *p, *end;
4167a02a946dSIlya Dryomov 	int msg_size;
4168a02a946dSIlya Dryomov 
4169a02a946dSIlya Dryomov 	msg_size = CEPH_ENCODING_START_BLK_LEN +
4170a02a946dSIlya Dryomov 			CEPH_PGID_ENCODING_LEN + 1; /* spgid */
4171a02a946dSIlya Dryomov 	msg_size += 4 + 1 + 8; /* map_epoch, op, id */
4172a02a946dSIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
4173a02a946dSIlya Dryomov 			hoid_encoding_size(backoff->begin);
4174a02a946dSIlya Dryomov 	msg_size += CEPH_ENCODING_START_BLK_LEN +
4175a02a946dSIlya Dryomov 			hoid_encoding_size(backoff->end);
4176a02a946dSIlya Dryomov 
4177a02a946dSIlya Dryomov 	msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true);
4178a02a946dSIlya Dryomov 	if (!msg)
4179a02a946dSIlya Dryomov 		return NULL;
4180a02a946dSIlya Dryomov 
4181a02a946dSIlya Dryomov 	p = msg->front.iov_base;
4182a02a946dSIlya Dryomov 	end = p + msg->front_alloc_len;
4183a02a946dSIlya Dryomov 
4184a02a946dSIlya Dryomov 	encode_spgid(&p, &backoff->spgid);
4185a02a946dSIlya Dryomov 	ceph_encode_32(&p, map_epoch);
4186a02a946dSIlya Dryomov 	ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK);
4187a02a946dSIlya Dryomov 	ceph_encode_64(&p, backoff->id);
4188a02a946dSIlya Dryomov 	encode_hoid(&p, end, backoff->begin);
4189a02a946dSIlya Dryomov 	encode_hoid(&p, end, backoff->end);
4190a02a946dSIlya Dryomov 	BUG_ON(p != end);
4191a02a946dSIlya Dryomov 
4192a02a946dSIlya Dryomov 	msg->front.iov_len = p - msg->front.iov_base;
4193a02a946dSIlya Dryomov 	msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */
4194a02a946dSIlya Dryomov 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
4195a02a946dSIlya Dryomov 
4196a02a946dSIlya Dryomov 	return msg;
4197a02a946dSIlya Dryomov }
4198a02a946dSIlya Dryomov 
4199a02a946dSIlya Dryomov static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m)
4200a02a946dSIlya Dryomov {
4201a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
4202a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
4203a02a946dSIlya Dryomov 	struct ceph_msg *msg;
4204a02a946dSIlya Dryomov 
4205a02a946dSIlya Dryomov 	dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4206a02a946dSIlya Dryomov 	     m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4207a02a946dSIlya Dryomov 
4208a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid);
4209a02a946dSIlya Dryomov 	if (!spg) {
4210a02a946dSIlya Dryomov 		spg = alloc_spg_mapping();
4211a02a946dSIlya Dryomov 		if (!spg) {
4212a02a946dSIlya Dryomov 			pr_err("%s failed to allocate spg\n", __func__);
4213a02a946dSIlya Dryomov 			return;
4214a02a946dSIlya Dryomov 		}
4215a02a946dSIlya Dryomov 		spg->spgid = m->spgid; /* struct */
4216a02a946dSIlya Dryomov 		insert_spg_mapping(&osd->o_backoff_mappings, spg);
4217a02a946dSIlya Dryomov 	}
4218a02a946dSIlya Dryomov 
4219a02a946dSIlya Dryomov 	backoff = alloc_backoff();
4220a02a946dSIlya Dryomov 	if (!backoff) {
4221a02a946dSIlya Dryomov 		pr_err("%s failed to allocate backoff\n", __func__);
4222a02a946dSIlya Dryomov 		return;
4223a02a946dSIlya Dryomov 	}
4224a02a946dSIlya Dryomov 	backoff->spgid = m->spgid; /* struct */
4225a02a946dSIlya Dryomov 	backoff->id = m->id;
4226a02a946dSIlya Dryomov 	backoff->begin = m->begin;
4227a02a946dSIlya Dryomov 	m->begin = NULL; /* backoff now owns this */
4228a02a946dSIlya Dryomov 	backoff->end = m->end;
4229a02a946dSIlya Dryomov 	m->end = NULL;   /* ditto */
4230a02a946dSIlya Dryomov 
4231a02a946dSIlya Dryomov 	insert_backoff(&spg->backoffs, backoff);
4232a02a946dSIlya Dryomov 	insert_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4233a02a946dSIlya Dryomov 
4234a02a946dSIlya Dryomov 	/*
4235a02a946dSIlya Dryomov 	 * Ack with original backoff's epoch so that the OSD can
4236a02a946dSIlya Dryomov 	 * discard this if there was a PG split.
4237a02a946dSIlya Dryomov 	 */
4238a02a946dSIlya Dryomov 	msg = create_backoff_message(backoff, m->map_epoch);
4239a02a946dSIlya Dryomov 	if (!msg) {
4240a02a946dSIlya Dryomov 		pr_err("%s failed to allocate msg\n", __func__);
4241a02a946dSIlya Dryomov 		return;
4242a02a946dSIlya Dryomov 	}
4243a02a946dSIlya Dryomov 	ceph_con_send(&osd->o_con, msg);
4244a02a946dSIlya Dryomov }
4245a02a946dSIlya Dryomov 
4246a02a946dSIlya Dryomov static bool target_contained_by(const struct ceph_osd_request_target *t,
4247a02a946dSIlya Dryomov 				const struct ceph_hobject_id *begin,
4248a02a946dSIlya Dryomov 				const struct ceph_hobject_id *end)
4249a02a946dSIlya Dryomov {
4250a02a946dSIlya Dryomov 	struct ceph_hobject_id hoid;
4251a02a946dSIlya Dryomov 	int cmp;
4252a02a946dSIlya Dryomov 
4253a02a946dSIlya Dryomov 	hoid_fill_from_target(&hoid, t);
4254a02a946dSIlya Dryomov 	cmp = hoid_compare(&hoid, begin);
4255a02a946dSIlya Dryomov 	return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0);
4256a02a946dSIlya Dryomov }
4257a02a946dSIlya Dryomov 
4258a02a946dSIlya Dryomov static void handle_backoff_unblock(struct ceph_osd *osd,
4259a02a946dSIlya Dryomov 				   const struct MOSDBackoff *m)
4260a02a946dSIlya Dryomov {
4261a02a946dSIlya Dryomov 	struct ceph_spg_mapping *spg;
4262a02a946dSIlya Dryomov 	struct ceph_osd_backoff *backoff;
4263a02a946dSIlya Dryomov 	struct rb_node *n;
4264a02a946dSIlya Dryomov 
4265a02a946dSIlya Dryomov 	dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4266a02a946dSIlya Dryomov 	     m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4267a02a946dSIlya Dryomov 
4268a02a946dSIlya Dryomov 	backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id);
4269a02a946dSIlya Dryomov 	if (!backoff) {
4270a02a946dSIlya Dryomov 		pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n",
4271a02a946dSIlya Dryomov 		       __func__, osd->o_osd, m->spgid.pgid.pool,
4272a02a946dSIlya Dryomov 		       m->spgid.pgid.seed, m->spgid.shard, m->id);
4273a02a946dSIlya Dryomov 		return;
4274a02a946dSIlya Dryomov 	}
4275a02a946dSIlya Dryomov 
4276a02a946dSIlya Dryomov 	if (hoid_compare(backoff->begin, m->begin) &&
4277a02a946dSIlya Dryomov 	    hoid_compare(backoff->end, m->end)) {
4278a02a946dSIlya Dryomov 		pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n",
4279a02a946dSIlya Dryomov 		       __func__, osd->o_osd, m->spgid.pgid.pool,
4280a02a946dSIlya Dryomov 		       m->spgid.pgid.seed, m->spgid.shard, m->id);
4281a02a946dSIlya Dryomov 		/* unblock it anyway... */
4282a02a946dSIlya Dryomov 	}
4283a02a946dSIlya Dryomov 
4284a02a946dSIlya Dryomov 	spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid);
4285a02a946dSIlya Dryomov 	BUG_ON(!spg);
4286a02a946dSIlya Dryomov 
4287a02a946dSIlya Dryomov 	erase_backoff(&spg->backoffs, backoff);
4288a02a946dSIlya Dryomov 	erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4289a02a946dSIlya Dryomov 	free_backoff(backoff);
4290a02a946dSIlya Dryomov 
4291a02a946dSIlya Dryomov 	if (RB_EMPTY_ROOT(&spg->backoffs)) {
4292a02a946dSIlya Dryomov 		erase_spg_mapping(&osd->o_backoff_mappings, spg);
4293a02a946dSIlya Dryomov 		free_spg_mapping(spg);
4294a02a946dSIlya Dryomov 	}
4295a02a946dSIlya Dryomov 
4296a02a946dSIlya Dryomov 	for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
4297a02a946dSIlya Dryomov 		struct ceph_osd_request *req =
4298a02a946dSIlya Dryomov 		    rb_entry(n, struct ceph_osd_request, r_node);
4299a02a946dSIlya Dryomov 
4300a02a946dSIlya Dryomov 		if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) {
4301a02a946dSIlya Dryomov 			/*
4302a02a946dSIlya Dryomov 			 * Match against @m, not @backoff -- the PG may
4303a02a946dSIlya Dryomov 			 * have split on the OSD.
4304a02a946dSIlya Dryomov 			 */
4305a02a946dSIlya Dryomov 			if (target_contained_by(&req->r_t, m->begin, m->end)) {
4306a02a946dSIlya Dryomov 				/*
4307a02a946dSIlya Dryomov 				 * If no other installed backoff applies,
4308a02a946dSIlya Dryomov 				 * resend.
4309a02a946dSIlya Dryomov 				 */
4310a02a946dSIlya Dryomov 				send_request(req);
4311a02a946dSIlya Dryomov 			}
4312a02a946dSIlya Dryomov 		}
4313a02a946dSIlya Dryomov 	}
4314a02a946dSIlya Dryomov }
4315a02a946dSIlya Dryomov 
4316a02a946dSIlya Dryomov static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg)
4317a02a946dSIlya Dryomov {
4318a02a946dSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
4319a02a946dSIlya Dryomov 	struct MOSDBackoff m;
4320a02a946dSIlya Dryomov 	int ret;
4321a02a946dSIlya Dryomov 
4322a02a946dSIlya Dryomov 	down_read(&osdc->lock);
4323a02a946dSIlya Dryomov 	if (!osd_registered(osd)) {
4324a02a946dSIlya Dryomov 		dout("%s osd%d unknown\n", __func__, osd->o_osd);
4325a02a946dSIlya Dryomov 		up_read(&osdc->lock);
4326a02a946dSIlya Dryomov 		return;
4327a02a946dSIlya Dryomov 	}
4328a02a946dSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
4329a02a946dSIlya Dryomov 
4330a02a946dSIlya Dryomov 	mutex_lock(&osd->lock);
4331a02a946dSIlya Dryomov 	ret = decode_MOSDBackoff(msg, &m);
4332a02a946dSIlya Dryomov 	if (ret) {
4333a02a946dSIlya Dryomov 		pr_err("failed to decode MOSDBackoff: %d\n", ret);
4334a02a946dSIlya Dryomov 		ceph_msg_dump(msg);
4335a02a946dSIlya Dryomov 		goto out_unlock;
4336a02a946dSIlya Dryomov 	}
4337a02a946dSIlya Dryomov 
4338a02a946dSIlya Dryomov 	switch (m.op) {
4339a02a946dSIlya Dryomov 	case CEPH_OSD_BACKOFF_OP_BLOCK:
4340a02a946dSIlya Dryomov 		handle_backoff_block(osd, &m);
4341a02a946dSIlya Dryomov 		break;
4342a02a946dSIlya Dryomov 	case CEPH_OSD_BACKOFF_OP_UNBLOCK:
4343a02a946dSIlya Dryomov 		handle_backoff_unblock(osd, &m);
4344a02a946dSIlya Dryomov 		break;
4345a02a946dSIlya Dryomov 	default:
4346a02a946dSIlya Dryomov 		pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op);
4347a02a946dSIlya Dryomov 	}
4348a02a946dSIlya Dryomov 
4349a02a946dSIlya Dryomov 	free_hoid(m.begin);
4350a02a946dSIlya Dryomov 	free_hoid(m.end);
4351a02a946dSIlya Dryomov 
4352a02a946dSIlya Dryomov out_unlock:
4353a02a946dSIlya Dryomov 	mutex_unlock(&osd->lock);
4354a02a946dSIlya Dryomov 	up_read(&osdc->lock);
4355a02a946dSIlya Dryomov }
4356a02a946dSIlya Dryomov 
43573d14c5d2SYehuda Sadeh /*
4358a40c4f10SYehuda Sadeh  * Process osd watch notifications
4359a40c4f10SYehuda Sadeh  */
43603c663bbdSAlex Elder static void handle_watch_notify(struct ceph_osd_client *osdc,
43613c663bbdSAlex Elder 				struct ceph_msg *msg)
4362a40c4f10SYehuda Sadeh {
4363922dab61SIlya Dryomov 	void *p = msg->front.iov_base;
4364922dab61SIlya Dryomov 	void *const end = p + msg->front.iov_len;
4365922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
4366922dab61SIlya Dryomov 	struct linger_work *lwork;
4367922dab61SIlya Dryomov 	u8 proto_ver, opcode;
4368922dab61SIlya Dryomov 	u64 cookie, notify_id;
4369922dab61SIlya Dryomov 	u64 notifier_id = 0;
437019079203SIlya Dryomov 	s32 return_code = 0;
4371922dab61SIlya Dryomov 	void *payload = NULL;
4372922dab61SIlya Dryomov 	u32 payload_len = 0;
4373a40c4f10SYehuda Sadeh 
4374a40c4f10SYehuda Sadeh 	ceph_decode_8_safe(&p, end, proto_ver, bad);
4375a40c4f10SYehuda Sadeh 	ceph_decode_8_safe(&p, end, opcode, bad);
4376a40c4f10SYehuda Sadeh 	ceph_decode_64_safe(&p, end, cookie, bad);
4377922dab61SIlya Dryomov 	p += 8; /* skip ver */
4378a40c4f10SYehuda Sadeh 	ceph_decode_64_safe(&p, end, notify_id, bad);
4379a40c4f10SYehuda Sadeh 
4380922dab61SIlya Dryomov 	if (proto_ver >= 1) {
4381922dab61SIlya Dryomov 		ceph_decode_32_safe(&p, end, payload_len, bad);
4382922dab61SIlya Dryomov 		ceph_decode_need(&p, end, payload_len, bad);
4383922dab61SIlya Dryomov 		payload = p;
4384922dab61SIlya Dryomov 		p += payload_len;
4385a40c4f10SYehuda Sadeh 	}
4386a40c4f10SYehuda Sadeh 
4387922dab61SIlya Dryomov 	if (le16_to_cpu(msg->hdr.version) >= 2)
438819079203SIlya Dryomov 		ceph_decode_32_safe(&p, end, return_code, bad);
4389922dab61SIlya Dryomov 
4390922dab61SIlya Dryomov 	if (le16_to_cpu(msg->hdr.version) >= 3)
4391922dab61SIlya Dryomov 		ceph_decode_64_safe(&p, end, notifier_id, bad);
4392922dab61SIlya Dryomov 
4393922dab61SIlya Dryomov 	down_read(&osdc->lock);
4394922dab61SIlya Dryomov 	lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
4395922dab61SIlya Dryomov 	if (!lreq) {
4396922dab61SIlya Dryomov 		dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
4397922dab61SIlya Dryomov 		     cookie);
4398922dab61SIlya Dryomov 		goto out_unlock_osdc;
4399922dab61SIlya Dryomov 	}
4400922dab61SIlya Dryomov 
4401922dab61SIlya Dryomov 	mutex_lock(&lreq->lock);
440219079203SIlya Dryomov 	dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
440319079203SIlya Dryomov 	     opcode, cookie, lreq, lreq->is_watch);
4404922dab61SIlya Dryomov 	if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
4405922dab61SIlya Dryomov 		if (!lreq->last_error) {
4406922dab61SIlya Dryomov 			lreq->last_error = -ENOTCONN;
4407922dab61SIlya Dryomov 			queue_watch_error(lreq);
4408922dab61SIlya Dryomov 		}
440919079203SIlya Dryomov 	} else if (!lreq->is_watch) {
441019079203SIlya Dryomov 		/* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
441119079203SIlya Dryomov 		if (lreq->notify_id && lreq->notify_id != notify_id) {
441219079203SIlya Dryomov 			dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
441319079203SIlya Dryomov 			     lreq->notify_id, notify_id);
441419079203SIlya Dryomov 		} else if (!completion_done(&lreq->notify_finish_wait)) {
441519079203SIlya Dryomov 			struct ceph_msg_data *data =
44160d9c1ab3SIlya Dryomov 			    msg->num_data_items ? &msg->data[0] : NULL;
441719079203SIlya Dryomov 
441819079203SIlya Dryomov 			if (data) {
441919079203SIlya Dryomov 				if (lreq->preply_pages) {
442019079203SIlya Dryomov 					WARN_ON(data->type !=
442119079203SIlya Dryomov 							CEPH_MSG_DATA_PAGES);
442219079203SIlya Dryomov 					*lreq->preply_pages = data->pages;
442319079203SIlya Dryomov 					*lreq->preply_len = data->length;
442419079203SIlya Dryomov 				} else {
442519079203SIlya Dryomov 					ceph_release_page_vector(data->pages,
442619079203SIlya Dryomov 					       calc_pages_for(0, data->length));
442719079203SIlya Dryomov 				}
442819079203SIlya Dryomov 			}
442919079203SIlya Dryomov 			lreq->notify_finish_error = return_code;
443019079203SIlya Dryomov 			complete_all(&lreq->notify_finish_wait);
443119079203SIlya Dryomov 		}
4432922dab61SIlya Dryomov 	} else {
4433922dab61SIlya Dryomov 		/* CEPH_WATCH_EVENT_NOTIFY */
4434922dab61SIlya Dryomov 		lwork = lwork_alloc(lreq, do_watch_notify);
4435922dab61SIlya Dryomov 		if (!lwork) {
4436922dab61SIlya Dryomov 			pr_err("failed to allocate notify-lwork\n");
4437922dab61SIlya Dryomov 			goto out_unlock_lreq;
4438922dab61SIlya Dryomov 		}
4439922dab61SIlya Dryomov 
4440922dab61SIlya Dryomov 		lwork->notify.notify_id = notify_id;
4441922dab61SIlya Dryomov 		lwork->notify.notifier_id = notifier_id;
4442922dab61SIlya Dryomov 		lwork->notify.payload = payload;
4443922dab61SIlya Dryomov 		lwork->notify.payload_len = payload_len;
4444922dab61SIlya Dryomov 		lwork->notify.msg = ceph_msg_get(msg);
4445922dab61SIlya Dryomov 		lwork_queue(lwork);
4446922dab61SIlya Dryomov 	}
4447922dab61SIlya Dryomov 
4448922dab61SIlya Dryomov out_unlock_lreq:
4449922dab61SIlya Dryomov 	mutex_unlock(&lreq->lock);
4450922dab61SIlya Dryomov out_unlock_osdc:
4451922dab61SIlya Dryomov 	up_read(&osdc->lock);
4452a40c4f10SYehuda Sadeh 	return;
4453a40c4f10SYehuda Sadeh 
4454a40c4f10SYehuda Sadeh bad:
4455a40c4f10SYehuda Sadeh 	pr_err("osdc handle_watch_notify corrupt msg\n");
4456a40c4f10SYehuda Sadeh }
4457a40c4f10SYehuda Sadeh 
4458a40c4f10SYehuda Sadeh /*
44593d14c5d2SYehuda Sadeh  * Register request, send initial attempt.
44603d14c5d2SYehuda Sadeh  */
44613d14c5d2SYehuda Sadeh int ceph_osdc_start_request(struct ceph_osd_client *osdc,
44623d14c5d2SYehuda Sadeh 			    struct ceph_osd_request *req,
44633d14c5d2SYehuda Sadeh 			    bool nofail)
44643d14c5d2SYehuda Sadeh {
44655aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
44665aea3dcdSIlya Dryomov 	submit_request(req, false);
44675aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
44683d14c5d2SYehuda Sadeh 
44695aea3dcdSIlya Dryomov 	return 0;
44703d14c5d2SYehuda Sadeh }
44713d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_start_request);
44723d14c5d2SYehuda Sadeh 
44733d14c5d2SYehuda Sadeh /*
4474c297eb42SIlya Dryomov  * Unregister a registered request.  The request is not completed:
4475c297eb42SIlya Dryomov  * ->r_result isn't set and __complete_request() isn't called.
4476c9f9b93dSIlya Dryomov  */
4477c9f9b93dSIlya Dryomov void ceph_osdc_cancel_request(struct ceph_osd_request *req)
4478c9f9b93dSIlya Dryomov {
4479c9f9b93dSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
4480c9f9b93dSIlya Dryomov 
44815aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
44825aea3dcdSIlya Dryomov 	if (req->r_osd)
44835aea3dcdSIlya Dryomov 		cancel_request(req);
44845aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
4485c9f9b93dSIlya Dryomov }
4486c9f9b93dSIlya Dryomov EXPORT_SYMBOL(ceph_osdc_cancel_request);
4487c9f9b93dSIlya Dryomov 
4488c9f9b93dSIlya Dryomov /*
448942b06965SIlya Dryomov  * @timeout: in jiffies, 0 means "wait forever"
449042b06965SIlya Dryomov  */
449142b06965SIlya Dryomov static int wait_request_timeout(struct ceph_osd_request *req,
449242b06965SIlya Dryomov 				unsigned long timeout)
449342b06965SIlya Dryomov {
449442b06965SIlya Dryomov 	long left;
449542b06965SIlya Dryomov 
449642b06965SIlya Dryomov 	dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
44970e76abf2SYan, Zheng 	left = wait_for_completion_killable_timeout(&req->r_completion,
449842b06965SIlya Dryomov 						ceph_timeout_jiffies(timeout));
449942b06965SIlya Dryomov 	if (left <= 0) {
450042b06965SIlya Dryomov 		left = left ?: -ETIMEDOUT;
450142b06965SIlya Dryomov 		ceph_osdc_cancel_request(req);
450242b06965SIlya Dryomov 	} else {
450342b06965SIlya Dryomov 		left = req->r_result; /* completed */
450442b06965SIlya Dryomov 	}
450542b06965SIlya Dryomov 
450642b06965SIlya Dryomov 	return left;
450742b06965SIlya Dryomov }
450842b06965SIlya Dryomov 
450942b06965SIlya Dryomov /*
45103d14c5d2SYehuda Sadeh  * wait for a request to complete
45113d14c5d2SYehuda Sadeh  */
45123d14c5d2SYehuda Sadeh int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
45133d14c5d2SYehuda Sadeh 			   struct ceph_osd_request *req)
45143d14c5d2SYehuda Sadeh {
451542b06965SIlya Dryomov 	return wait_request_timeout(req, 0);
45163d14c5d2SYehuda Sadeh }
45173d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_wait_request);
45183d14c5d2SYehuda Sadeh 
45193d14c5d2SYehuda Sadeh /*
45203d14c5d2SYehuda Sadeh  * sync - wait for all in-flight requests to flush.  avoid starvation.
45213d14c5d2SYehuda Sadeh  */
45223d14c5d2SYehuda Sadeh void ceph_osdc_sync(struct ceph_osd_client *osdc)
45233d14c5d2SYehuda Sadeh {
45245aea3dcdSIlya Dryomov 	struct rb_node *n, *p;
45255aea3dcdSIlya Dryomov 	u64 last_tid = atomic64_read(&osdc->last_tid);
45263d14c5d2SYehuda Sadeh 
45275aea3dcdSIlya Dryomov again:
45285aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
45295aea3dcdSIlya Dryomov 	for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
45305aea3dcdSIlya Dryomov 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
45315aea3dcdSIlya Dryomov 
45325aea3dcdSIlya Dryomov 		mutex_lock(&osd->lock);
45335aea3dcdSIlya Dryomov 		for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
45345aea3dcdSIlya Dryomov 			struct ceph_osd_request *req =
45355aea3dcdSIlya Dryomov 			    rb_entry(p, struct ceph_osd_request, r_node);
45365aea3dcdSIlya Dryomov 
45373d14c5d2SYehuda Sadeh 			if (req->r_tid > last_tid)
45383d14c5d2SYehuda Sadeh 				break;
45393d14c5d2SYehuda Sadeh 
45405aea3dcdSIlya Dryomov 			if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
45413d14c5d2SYehuda Sadeh 				continue;
45423d14c5d2SYehuda Sadeh 
45433d14c5d2SYehuda Sadeh 			ceph_osdc_get_request(req);
45445aea3dcdSIlya Dryomov 			mutex_unlock(&osd->lock);
45455aea3dcdSIlya Dryomov 			up_read(&osdc->lock);
45465aea3dcdSIlya Dryomov 			dout("%s waiting on req %p tid %llu last_tid %llu\n",
45475aea3dcdSIlya Dryomov 			     __func__, req, req->r_tid, last_tid);
4548b18b9550SIlya Dryomov 			wait_for_completion(&req->r_completion);
45493d14c5d2SYehuda Sadeh 			ceph_osdc_put_request(req);
45505aea3dcdSIlya Dryomov 			goto again;
45513d14c5d2SYehuda Sadeh 		}
45525aea3dcdSIlya Dryomov 
45535aea3dcdSIlya Dryomov 		mutex_unlock(&osd->lock);
45545aea3dcdSIlya Dryomov 	}
45555aea3dcdSIlya Dryomov 
45565aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
45575aea3dcdSIlya Dryomov 	dout("%s done last_tid %llu\n", __func__, last_tid);
45583d14c5d2SYehuda Sadeh }
45593d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_sync);
45603d14c5d2SYehuda Sadeh 
4561922dab61SIlya Dryomov static struct ceph_osd_request *
4562922dab61SIlya Dryomov alloc_linger_request(struct ceph_osd_linger_request *lreq)
4563922dab61SIlya Dryomov {
4564922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4565922dab61SIlya Dryomov 
4566922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO);
4567922dab61SIlya Dryomov 	if (!req)
4568922dab61SIlya Dryomov 		return NULL;
4569922dab61SIlya Dryomov 
4570922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4571922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
4572922dab61SIlya Dryomov 	return req;
4573922dab61SIlya Dryomov }
4574922dab61SIlya Dryomov 
457539e58c34SIlya Dryomov static struct ceph_osd_request *
457639e58c34SIlya Dryomov alloc_watch_request(struct ceph_osd_linger_request *lreq, u8 watch_opcode)
457739e58c34SIlya Dryomov {
457839e58c34SIlya Dryomov 	struct ceph_osd_request *req;
457939e58c34SIlya Dryomov 
458039e58c34SIlya Dryomov 	req = alloc_linger_request(lreq);
458139e58c34SIlya Dryomov 	if (!req)
458239e58c34SIlya Dryomov 		return NULL;
458339e58c34SIlya Dryomov 
458439e58c34SIlya Dryomov 	/*
458539e58c34SIlya Dryomov 	 * Pass 0 for cookie because we don't know it yet, it will be
458639e58c34SIlya Dryomov 	 * filled in by linger_submit().
458739e58c34SIlya Dryomov 	 */
458839e58c34SIlya Dryomov 	osd_req_op_watch_init(req, 0, 0, watch_opcode);
458926f887e0SIlya Dryomov 
459026f887e0SIlya Dryomov 	if (ceph_osdc_alloc_messages(req, GFP_NOIO)) {
459126f887e0SIlya Dryomov 		ceph_osdc_put_request(req);
459226f887e0SIlya Dryomov 		return NULL;
459326f887e0SIlya Dryomov 	}
459426f887e0SIlya Dryomov 
459539e58c34SIlya Dryomov 	return req;
459639e58c34SIlya Dryomov }
459739e58c34SIlya Dryomov 
4598922dab61SIlya Dryomov /*
4599922dab61SIlya Dryomov  * Returns a handle, caller owns a ref.
4600922dab61SIlya Dryomov  */
4601922dab61SIlya Dryomov struct ceph_osd_linger_request *
4602922dab61SIlya Dryomov ceph_osdc_watch(struct ceph_osd_client *osdc,
4603922dab61SIlya Dryomov 		struct ceph_object_id *oid,
4604922dab61SIlya Dryomov 		struct ceph_object_locator *oloc,
4605922dab61SIlya Dryomov 		rados_watchcb2_t wcb,
4606922dab61SIlya Dryomov 		rados_watcherrcb_t errcb,
4607922dab61SIlya Dryomov 		void *data)
4608922dab61SIlya Dryomov {
4609922dab61SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
4610922dab61SIlya Dryomov 	int ret;
4611922dab61SIlya Dryomov 
4612922dab61SIlya Dryomov 	lreq = linger_alloc(osdc);
4613922dab61SIlya Dryomov 	if (!lreq)
4614922dab61SIlya Dryomov 		return ERR_PTR(-ENOMEM);
4615922dab61SIlya Dryomov 
461619079203SIlya Dryomov 	lreq->is_watch = true;
4617922dab61SIlya Dryomov 	lreq->wcb = wcb;
4618922dab61SIlya Dryomov 	lreq->errcb = errcb;
4619922dab61SIlya Dryomov 	lreq->data = data;
4620b07d3c4bSIlya Dryomov 	lreq->watch_valid_thru = jiffies;
4621922dab61SIlya Dryomov 
4622922dab61SIlya Dryomov 	ceph_oid_copy(&lreq->t.base_oid, oid);
4623922dab61SIlya Dryomov 	ceph_oloc_copy(&lreq->t.base_oloc, oloc);
462454ea0046SIlya Dryomov 	lreq->t.flags = CEPH_OSD_FLAG_WRITE;
4625fac02ddfSArnd Bergmann 	ktime_get_real_ts64(&lreq->mtime);
4626922dab61SIlya Dryomov 
462739e58c34SIlya Dryomov 	lreq->reg_req = alloc_watch_request(lreq, CEPH_OSD_WATCH_OP_WATCH);
4628922dab61SIlya Dryomov 	if (!lreq->reg_req) {
4629922dab61SIlya Dryomov 		ret = -ENOMEM;
4630922dab61SIlya Dryomov 		goto err_put_lreq;
4631922dab61SIlya Dryomov 	}
4632922dab61SIlya Dryomov 
463339e58c34SIlya Dryomov 	lreq->ping_req = alloc_watch_request(lreq, CEPH_OSD_WATCH_OP_PING);
4634922dab61SIlya Dryomov 	if (!lreq->ping_req) {
4635922dab61SIlya Dryomov 		ret = -ENOMEM;
4636922dab61SIlya Dryomov 		goto err_put_lreq;
4637922dab61SIlya Dryomov 	}
4638922dab61SIlya Dryomov 
463981c65213SIlya Dryomov 	linger_submit(lreq);
4640922dab61SIlya Dryomov 	ret = linger_reg_commit_wait(lreq);
4641922dab61SIlya Dryomov 	if (ret) {
4642922dab61SIlya Dryomov 		linger_cancel(lreq);
4643922dab61SIlya Dryomov 		goto err_put_lreq;
4644922dab61SIlya Dryomov 	}
4645922dab61SIlya Dryomov 
4646922dab61SIlya Dryomov 	return lreq;
4647922dab61SIlya Dryomov 
4648922dab61SIlya Dryomov err_put_lreq:
4649922dab61SIlya Dryomov 	linger_put(lreq);
4650922dab61SIlya Dryomov 	return ERR_PTR(ret);
4651922dab61SIlya Dryomov }
4652922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_watch);
4653922dab61SIlya Dryomov 
4654922dab61SIlya Dryomov /*
4655922dab61SIlya Dryomov  * Releases a ref.
4656922dab61SIlya Dryomov  *
4657922dab61SIlya Dryomov  * Times out after mount_timeout to preserve rbd unmap behaviour
4658922dab61SIlya Dryomov  * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
4659922dab61SIlya Dryomov  * with mount_timeout").
4660922dab61SIlya Dryomov  */
4661922dab61SIlya Dryomov int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
4662922dab61SIlya Dryomov 		      struct ceph_osd_linger_request *lreq)
4663922dab61SIlya Dryomov {
4664922dab61SIlya Dryomov 	struct ceph_options *opts = osdc->client->options;
4665922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4666922dab61SIlya Dryomov 	int ret;
4667922dab61SIlya Dryomov 
4668922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4669922dab61SIlya Dryomov 	if (!req)
4670922dab61SIlya Dryomov 		return -ENOMEM;
4671922dab61SIlya Dryomov 
4672922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4673922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
467454ea0046SIlya Dryomov 	req->r_flags = CEPH_OSD_FLAG_WRITE;
4675fac02ddfSArnd Bergmann 	ktime_get_real_ts64(&req->r_mtime);
4676922dab61SIlya Dryomov 	osd_req_op_watch_init(req, 0, lreq->linger_id,
4677922dab61SIlya Dryomov 			      CEPH_OSD_WATCH_OP_UNWATCH);
4678922dab61SIlya Dryomov 
4679922dab61SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4680922dab61SIlya Dryomov 	if (ret)
4681922dab61SIlya Dryomov 		goto out_put_req;
4682922dab61SIlya Dryomov 
4683922dab61SIlya Dryomov 	ceph_osdc_start_request(osdc, req, false);
4684922dab61SIlya Dryomov 	linger_cancel(lreq);
4685922dab61SIlya Dryomov 	linger_put(lreq);
4686922dab61SIlya Dryomov 	ret = wait_request_timeout(req, opts->mount_timeout);
4687922dab61SIlya Dryomov 
4688922dab61SIlya Dryomov out_put_req:
4689922dab61SIlya Dryomov 	ceph_osdc_put_request(req);
4690922dab61SIlya Dryomov 	return ret;
4691922dab61SIlya Dryomov }
4692922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_unwatch);
4693922dab61SIlya Dryomov 
4694922dab61SIlya Dryomov static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
4695922dab61SIlya Dryomov 				      u64 notify_id, u64 cookie, void *payload,
46966d54228fSIlya Dryomov 				      u32 payload_len)
4697922dab61SIlya Dryomov {
4698922dab61SIlya Dryomov 	struct ceph_osd_req_op *op;
4699922dab61SIlya Dryomov 	struct ceph_pagelist *pl;
4700922dab61SIlya Dryomov 	int ret;
4701922dab61SIlya Dryomov 
4702922dab61SIlya Dryomov 	op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
4703922dab61SIlya Dryomov 
470433165d47SIlya Dryomov 	pl = ceph_pagelist_alloc(GFP_NOIO);
4705922dab61SIlya Dryomov 	if (!pl)
4706922dab61SIlya Dryomov 		return -ENOMEM;
4707922dab61SIlya Dryomov 
4708922dab61SIlya Dryomov 	ret = ceph_pagelist_encode_64(pl, notify_id);
4709922dab61SIlya Dryomov 	ret |= ceph_pagelist_encode_64(pl, cookie);
4710922dab61SIlya Dryomov 	if (payload) {
4711922dab61SIlya Dryomov 		ret |= ceph_pagelist_encode_32(pl, payload_len);
4712922dab61SIlya Dryomov 		ret |= ceph_pagelist_append(pl, payload, payload_len);
4713922dab61SIlya Dryomov 	} else {
4714922dab61SIlya Dryomov 		ret |= ceph_pagelist_encode_32(pl, 0);
4715922dab61SIlya Dryomov 	}
4716922dab61SIlya Dryomov 	if (ret) {
4717922dab61SIlya Dryomov 		ceph_pagelist_release(pl);
4718922dab61SIlya Dryomov 		return -ENOMEM;
4719922dab61SIlya Dryomov 	}
4720922dab61SIlya Dryomov 
4721922dab61SIlya Dryomov 	ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
4722922dab61SIlya Dryomov 	op->indata_len = pl->length;
4723922dab61SIlya Dryomov 	return 0;
4724922dab61SIlya Dryomov }
4725922dab61SIlya Dryomov 
4726922dab61SIlya Dryomov int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
4727922dab61SIlya Dryomov 			 struct ceph_object_id *oid,
4728922dab61SIlya Dryomov 			 struct ceph_object_locator *oloc,
4729922dab61SIlya Dryomov 			 u64 notify_id,
4730922dab61SIlya Dryomov 			 u64 cookie,
4731922dab61SIlya Dryomov 			 void *payload,
47326d54228fSIlya Dryomov 			 u32 payload_len)
4733922dab61SIlya Dryomov {
4734922dab61SIlya Dryomov 	struct ceph_osd_request *req;
4735922dab61SIlya Dryomov 	int ret;
4736922dab61SIlya Dryomov 
4737922dab61SIlya Dryomov 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4738922dab61SIlya Dryomov 	if (!req)
4739922dab61SIlya Dryomov 		return -ENOMEM;
4740922dab61SIlya Dryomov 
4741922dab61SIlya Dryomov 	ceph_oid_copy(&req->r_base_oid, oid);
4742922dab61SIlya Dryomov 	ceph_oloc_copy(&req->r_base_oloc, oloc);
4743922dab61SIlya Dryomov 	req->r_flags = CEPH_OSD_FLAG_READ;
4744922dab61SIlya Dryomov 
474526f887e0SIlya Dryomov 	ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
474626f887e0SIlya Dryomov 					 payload_len);
4747922dab61SIlya Dryomov 	if (ret)
4748922dab61SIlya Dryomov 		goto out_put_req;
4749922dab61SIlya Dryomov 
475026f887e0SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4751922dab61SIlya Dryomov 	if (ret)
4752922dab61SIlya Dryomov 		goto out_put_req;
4753922dab61SIlya Dryomov 
4754922dab61SIlya Dryomov 	ceph_osdc_start_request(osdc, req, false);
4755922dab61SIlya Dryomov 	ret = ceph_osdc_wait_request(osdc, req);
4756922dab61SIlya Dryomov 
4757922dab61SIlya Dryomov out_put_req:
4758922dab61SIlya Dryomov 	ceph_osdc_put_request(req);
4759922dab61SIlya Dryomov 	return ret;
4760922dab61SIlya Dryomov }
4761922dab61SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_notify_ack);
4762922dab61SIlya Dryomov 
476319079203SIlya Dryomov static int osd_req_op_notify_init(struct ceph_osd_request *req, int which,
476419079203SIlya Dryomov 				  u64 cookie, u32 prot_ver, u32 timeout,
47656d54228fSIlya Dryomov 				  void *payload, u32 payload_len)
476619079203SIlya Dryomov {
476719079203SIlya Dryomov 	struct ceph_osd_req_op *op;
476819079203SIlya Dryomov 	struct ceph_pagelist *pl;
476919079203SIlya Dryomov 	int ret;
477019079203SIlya Dryomov 
477119079203SIlya Dryomov 	op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
477219079203SIlya Dryomov 	op->notify.cookie = cookie;
477319079203SIlya Dryomov 
477433165d47SIlya Dryomov 	pl = ceph_pagelist_alloc(GFP_NOIO);
477519079203SIlya Dryomov 	if (!pl)
477619079203SIlya Dryomov 		return -ENOMEM;
477719079203SIlya Dryomov 
477819079203SIlya Dryomov 	ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */
477919079203SIlya Dryomov 	ret |= ceph_pagelist_encode_32(pl, timeout);
478019079203SIlya Dryomov 	ret |= ceph_pagelist_encode_32(pl, payload_len);
478119079203SIlya Dryomov 	ret |= ceph_pagelist_append(pl, payload, payload_len);
478219079203SIlya Dryomov 	if (ret) {
478319079203SIlya Dryomov 		ceph_pagelist_release(pl);
478419079203SIlya Dryomov 		return -ENOMEM;
478519079203SIlya Dryomov 	}
478619079203SIlya Dryomov 
478719079203SIlya Dryomov 	ceph_osd_data_pagelist_init(&op->notify.request_data, pl);
478819079203SIlya Dryomov 	op->indata_len = pl->length;
478919079203SIlya Dryomov 	return 0;
479019079203SIlya Dryomov }
479119079203SIlya Dryomov 
479219079203SIlya Dryomov /*
479319079203SIlya Dryomov  * @timeout: in seconds
479419079203SIlya Dryomov  *
479519079203SIlya Dryomov  * @preply_{pages,len} are initialized both on success and error.
479619079203SIlya Dryomov  * The caller is responsible for:
479719079203SIlya Dryomov  *
479819079203SIlya Dryomov  *     ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
479919079203SIlya Dryomov  */
480019079203SIlya Dryomov int ceph_osdc_notify(struct ceph_osd_client *osdc,
480119079203SIlya Dryomov 		     struct ceph_object_id *oid,
480219079203SIlya Dryomov 		     struct ceph_object_locator *oloc,
480319079203SIlya Dryomov 		     void *payload,
48046d54228fSIlya Dryomov 		     u32 payload_len,
480519079203SIlya Dryomov 		     u32 timeout,
480619079203SIlya Dryomov 		     struct page ***preply_pages,
480719079203SIlya Dryomov 		     size_t *preply_len)
480819079203SIlya Dryomov {
480919079203SIlya Dryomov 	struct ceph_osd_linger_request *lreq;
481019079203SIlya Dryomov 	struct page **pages;
481119079203SIlya Dryomov 	int ret;
481219079203SIlya Dryomov 
481319079203SIlya Dryomov 	WARN_ON(!timeout);
481419079203SIlya Dryomov 	if (preply_pages) {
481519079203SIlya Dryomov 		*preply_pages = NULL;
481619079203SIlya Dryomov 		*preply_len = 0;
481719079203SIlya Dryomov 	}
481819079203SIlya Dryomov 
481919079203SIlya Dryomov 	lreq = linger_alloc(osdc);
482019079203SIlya Dryomov 	if (!lreq)
482119079203SIlya Dryomov 		return -ENOMEM;
482219079203SIlya Dryomov 
482319079203SIlya Dryomov 	lreq->preply_pages = preply_pages;
482419079203SIlya Dryomov 	lreq->preply_len = preply_len;
482519079203SIlya Dryomov 
482619079203SIlya Dryomov 	ceph_oid_copy(&lreq->t.base_oid, oid);
482719079203SIlya Dryomov 	ceph_oloc_copy(&lreq->t.base_oloc, oloc);
482819079203SIlya Dryomov 	lreq->t.flags = CEPH_OSD_FLAG_READ;
482919079203SIlya Dryomov 
483019079203SIlya Dryomov 	lreq->reg_req = alloc_linger_request(lreq);
483119079203SIlya Dryomov 	if (!lreq->reg_req) {
483219079203SIlya Dryomov 		ret = -ENOMEM;
483319079203SIlya Dryomov 		goto out_put_lreq;
483419079203SIlya Dryomov 	}
483519079203SIlya Dryomov 
483681c65213SIlya Dryomov 	/*
483781c65213SIlya Dryomov 	 * Pass 0 for cookie because we don't know it yet, it will be
483881c65213SIlya Dryomov 	 * filled in by linger_submit().
483981c65213SIlya Dryomov 	 */
484081c65213SIlya Dryomov 	ret = osd_req_op_notify_init(lreq->reg_req, 0, 0, 1, timeout,
484181c65213SIlya Dryomov 				     payload, payload_len);
484281c65213SIlya Dryomov 	if (ret)
484381c65213SIlya Dryomov 		goto out_put_lreq;
484481c65213SIlya Dryomov 
484519079203SIlya Dryomov 	/* for notify_id */
484619079203SIlya Dryomov 	pages = ceph_alloc_page_vector(1, GFP_NOIO);
484719079203SIlya Dryomov 	if (IS_ERR(pages)) {
484819079203SIlya Dryomov 		ret = PTR_ERR(pages);
484919079203SIlya Dryomov 		goto out_put_lreq;
485019079203SIlya Dryomov 	}
485119079203SIlya Dryomov 	ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify,
485219079203SIlya Dryomov 						 response_data),
485319079203SIlya Dryomov 				 pages, PAGE_SIZE, 0, false, true);
485419079203SIlya Dryomov 
485526f887e0SIlya Dryomov 	ret = ceph_osdc_alloc_messages(lreq->reg_req, GFP_NOIO);
485626f887e0SIlya Dryomov 	if (ret)
485726f887e0SIlya Dryomov 		goto out_put_lreq;
485826f887e0SIlya Dryomov 
485981c65213SIlya Dryomov 	linger_submit(lreq);
486019079203SIlya Dryomov 	ret = linger_reg_commit_wait(lreq);
486119079203SIlya Dryomov 	if (!ret)
486219079203SIlya Dryomov 		ret = linger_notify_finish_wait(lreq);
486319079203SIlya Dryomov 	else
486419079203SIlya Dryomov 		dout("lreq %p failed to initiate notify %d\n", lreq, ret);
486519079203SIlya Dryomov 
486619079203SIlya Dryomov 	linger_cancel(lreq);
486719079203SIlya Dryomov out_put_lreq:
486819079203SIlya Dryomov 	linger_put(lreq);
486919079203SIlya Dryomov 	return ret;
487019079203SIlya Dryomov }
487119079203SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_notify);
487219079203SIlya Dryomov 
48733d14c5d2SYehuda Sadeh /*
4874b07d3c4bSIlya Dryomov  * Return the number of milliseconds since the watch was last
4875b07d3c4bSIlya Dryomov  * confirmed, or an error.  If there is an error, the watch is no
4876b07d3c4bSIlya Dryomov  * longer valid, and should be destroyed with ceph_osdc_unwatch().
4877b07d3c4bSIlya Dryomov  */
4878b07d3c4bSIlya Dryomov int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
4879b07d3c4bSIlya Dryomov 			  struct ceph_osd_linger_request *lreq)
4880b07d3c4bSIlya Dryomov {
4881b07d3c4bSIlya Dryomov 	unsigned long stamp, age;
4882b07d3c4bSIlya Dryomov 	int ret;
4883b07d3c4bSIlya Dryomov 
4884b07d3c4bSIlya Dryomov 	down_read(&osdc->lock);
4885b07d3c4bSIlya Dryomov 	mutex_lock(&lreq->lock);
4886b07d3c4bSIlya Dryomov 	stamp = lreq->watch_valid_thru;
4887b07d3c4bSIlya Dryomov 	if (!list_empty(&lreq->pending_lworks)) {
4888b07d3c4bSIlya Dryomov 		struct linger_work *lwork =
4889b07d3c4bSIlya Dryomov 		    list_first_entry(&lreq->pending_lworks,
4890b07d3c4bSIlya Dryomov 				     struct linger_work,
4891b07d3c4bSIlya Dryomov 				     pending_item);
4892b07d3c4bSIlya Dryomov 
4893b07d3c4bSIlya Dryomov 		if (time_before(lwork->queued_stamp, stamp))
4894b07d3c4bSIlya Dryomov 			stamp = lwork->queued_stamp;
4895b07d3c4bSIlya Dryomov 	}
4896b07d3c4bSIlya Dryomov 	age = jiffies - stamp;
4897b07d3c4bSIlya Dryomov 	dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
4898b07d3c4bSIlya Dryomov 	     lreq, lreq->linger_id, age, lreq->last_error);
4899b07d3c4bSIlya Dryomov 	/* we are truncating to msecs, so return a safe upper bound */
4900b07d3c4bSIlya Dryomov 	ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
4901b07d3c4bSIlya Dryomov 
4902b07d3c4bSIlya Dryomov 	mutex_unlock(&lreq->lock);
4903b07d3c4bSIlya Dryomov 	up_read(&osdc->lock);
4904b07d3c4bSIlya Dryomov 	return ret;
4905b07d3c4bSIlya Dryomov }
4906b07d3c4bSIlya Dryomov 
4907a4ed38d7SDouglas Fuller static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
4908a4ed38d7SDouglas Fuller {
4909a4ed38d7SDouglas Fuller 	u8 struct_v;
4910a4ed38d7SDouglas Fuller 	u32 struct_len;
4911a4ed38d7SDouglas Fuller 	int ret;
4912a4ed38d7SDouglas Fuller 
4913a4ed38d7SDouglas Fuller 	ret = ceph_start_decoding(p, end, 2, "watch_item_t",
4914a4ed38d7SDouglas Fuller 				  &struct_v, &struct_len);
4915a4ed38d7SDouglas Fuller 	if (ret)
491651fc7ab4SJeff Layton 		goto bad;
4917a4ed38d7SDouglas Fuller 
491851fc7ab4SJeff Layton 	ret = -EINVAL;
491951fc7ab4SJeff Layton 	ceph_decode_copy_safe(p, end, &item->name, sizeof(item->name), bad);
492051fc7ab4SJeff Layton 	ceph_decode_64_safe(p, end, item->cookie, bad);
492151fc7ab4SJeff Layton 	ceph_decode_skip_32(p, end, bad); /* skip timeout seconds */
492251fc7ab4SJeff Layton 
4923a4ed38d7SDouglas Fuller 	if (struct_v >= 2) {
492451fc7ab4SJeff Layton 		ret = ceph_decode_entity_addr(p, end, &item->addr);
492551fc7ab4SJeff Layton 		if (ret)
492651fc7ab4SJeff Layton 			goto bad;
492751fc7ab4SJeff Layton 	} else {
492851fc7ab4SJeff Layton 		ret = 0;
4929a4ed38d7SDouglas Fuller 	}
4930a4ed38d7SDouglas Fuller 
4931a4ed38d7SDouglas Fuller 	dout("%s %s%llu cookie %llu addr %s\n", __func__,
4932a4ed38d7SDouglas Fuller 	     ENTITY_NAME(item->name), item->cookie,
4933b726ec97SJeff Layton 	     ceph_pr_addr(&item->addr));
493451fc7ab4SJeff Layton bad:
493551fc7ab4SJeff Layton 	return ret;
4936a4ed38d7SDouglas Fuller }
4937a4ed38d7SDouglas Fuller 
4938a4ed38d7SDouglas Fuller static int decode_watchers(void **p, void *end,
4939a4ed38d7SDouglas Fuller 			   struct ceph_watch_item **watchers,
4940a4ed38d7SDouglas Fuller 			   u32 *num_watchers)
4941a4ed38d7SDouglas Fuller {
4942a4ed38d7SDouglas Fuller 	u8 struct_v;
4943a4ed38d7SDouglas Fuller 	u32 struct_len;
4944a4ed38d7SDouglas Fuller 	int i;
4945a4ed38d7SDouglas Fuller 	int ret;
4946a4ed38d7SDouglas Fuller 
4947a4ed38d7SDouglas Fuller 	ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
4948a4ed38d7SDouglas Fuller 				  &struct_v, &struct_len);
4949a4ed38d7SDouglas Fuller 	if (ret)
4950a4ed38d7SDouglas Fuller 		return ret;
4951a4ed38d7SDouglas Fuller 
4952a4ed38d7SDouglas Fuller 	*num_watchers = ceph_decode_32(p);
4953a4ed38d7SDouglas Fuller 	*watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
4954a4ed38d7SDouglas Fuller 	if (!*watchers)
4955a4ed38d7SDouglas Fuller 		return -ENOMEM;
4956a4ed38d7SDouglas Fuller 
4957a4ed38d7SDouglas Fuller 	for (i = 0; i < *num_watchers; i++) {
4958a4ed38d7SDouglas Fuller 		ret = decode_watcher(p, end, *watchers + i);
4959a4ed38d7SDouglas Fuller 		if (ret) {
4960a4ed38d7SDouglas Fuller 			kfree(*watchers);
4961a4ed38d7SDouglas Fuller 			return ret;
4962a4ed38d7SDouglas Fuller 		}
4963a4ed38d7SDouglas Fuller 	}
4964a4ed38d7SDouglas Fuller 
4965a4ed38d7SDouglas Fuller 	return 0;
4966a4ed38d7SDouglas Fuller }
4967a4ed38d7SDouglas Fuller 
4968a4ed38d7SDouglas Fuller /*
4969a4ed38d7SDouglas Fuller  * On success, the caller is responsible for:
4970a4ed38d7SDouglas Fuller  *
4971a4ed38d7SDouglas Fuller  *     kfree(watchers);
4972a4ed38d7SDouglas Fuller  */
4973a4ed38d7SDouglas Fuller int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
4974a4ed38d7SDouglas Fuller 			    struct ceph_object_id *oid,
4975a4ed38d7SDouglas Fuller 			    struct ceph_object_locator *oloc,
4976a4ed38d7SDouglas Fuller 			    struct ceph_watch_item **watchers,
4977a4ed38d7SDouglas Fuller 			    u32 *num_watchers)
4978a4ed38d7SDouglas Fuller {
4979a4ed38d7SDouglas Fuller 	struct ceph_osd_request *req;
4980a4ed38d7SDouglas Fuller 	struct page **pages;
4981a4ed38d7SDouglas Fuller 	int ret;
4982a4ed38d7SDouglas Fuller 
4983a4ed38d7SDouglas Fuller 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4984a4ed38d7SDouglas Fuller 	if (!req)
4985a4ed38d7SDouglas Fuller 		return -ENOMEM;
4986a4ed38d7SDouglas Fuller 
4987a4ed38d7SDouglas Fuller 	ceph_oid_copy(&req->r_base_oid, oid);
4988a4ed38d7SDouglas Fuller 	ceph_oloc_copy(&req->r_base_oloc, oloc);
4989a4ed38d7SDouglas Fuller 	req->r_flags = CEPH_OSD_FLAG_READ;
4990a4ed38d7SDouglas Fuller 
4991a4ed38d7SDouglas Fuller 	pages = ceph_alloc_page_vector(1, GFP_NOIO);
4992a4ed38d7SDouglas Fuller 	if (IS_ERR(pages)) {
4993a4ed38d7SDouglas Fuller 		ret = PTR_ERR(pages);
4994a4ed38d7SDouglas Fuller 		goto out_put_req;
4995a4ed38d7SDouglas Fuller 	}
4996a4ed38d7SDouglas Fuller 
4997a4ed38d7SDouglas Fuller 	osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
4998a4ed38d7SDouglas Fuller 	ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
4999a4ed38d7SDouglas Fuller 						 response_data),
5000a4ed38d7SDouglas Fuller 				 pages, PAGE_SIZE, 0, false, true);
5001a4ed38d7SDouglas Fuller 
500226f887e0SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
500326f887e0SIlya Dryomov 	if (ret)
500426f887e0SIlya Dryomov 		goto out_put_req;
500526f887e0SIlya Dryomov 
5006a4ed38d7SDouglas Fuller 	ceph_osdc_start_request(osdc, req, false);
5007a4ed38d7SDouglas Fuller 	ret = ceph_osdc_wait_request(osdc, req);
5008a4ed38d7SDouglas Fuller 	if (ret >= 0) {
5009a4ed38d7SDouglas Fuller 		void *p = page_address(pages[0]);
5010a4ed38d7SDouglas Fuller 		void *const end = p + req->r_ops[0].outdata_len;
5011a4ed38d7SDouglas Fuller 
5012a4ed38d7SDouglas Fuller 		ret = decode_watchers(&p, end, watchers, num_watchers);
5013a4ed38d7SDouglas Fuller 	}
5014a4ed38d7SDouglas Fuller 
5015a4ed38d7SDouglas Fuller out_put_req:
5016a4ed38d7SDouglas Fuller 	ceph_osdc_put_request(req);
5017a4ed38d7SDouglas Fuller 	return ret;
5018a4ed38d7SDouglas Fuller }
5019a4ed38d7SDouglas Fuller EXPORT_SYMBOL(ceph_osdc_list_watchers);
5020a4ed38d7SDouglas Fuller 
5021b07d3c4bSIlya Dryomov /*
5022dd935f44SJosh Durgin  * Call all pending notify callbacks - for use after a watch is
5023dd935f44SJosh Durgin  * unregistered, to make sure no more callbacks for it will be invoked
5024dd935f44SJosh Durgin  */
5025f6479449Sstephen hemminger void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
5026dd935f44SJosh Durgin {
502799d16943SIlya Dryomov 	dout("%s osdc %p\n", __func__, osdc);
5028dd935f44SJosh Durgin 	flush_workqueue(osdc->notify_wq);
5029dd935f44SJosh Durgin }
5030dd935f44SJosh Durgin EXPORT_SYMBOL(ceph_osdc_flush_notifies);
5031dd935f44SJosh Durgin 
50327cca78c9SIlya Dryomov void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
50337cca78c9SIlya Dryomov {
50347cca78c9SIlya Dryomov 	down_read(&osdc->lock);
50357cca78c9SIlya Dryomov 	maybe_request_map(osdc);
50367cca78c9SIlya Dryomov 	up_read(&osdc->lock);
50377cca78c9SIlya Dryomov }
50387cca78c9SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
5039dd935f44SJosh Durgin 
5040dd935f44SJosh Durgin /*
5041428a7158SDouglas Fuller  * Execute an OSD class method on an object.
5042428a7158SDouglas Fuller  *
5043428a7158SDouglas Fuller  * @flags: CEPH_OSD_FLAG_*
50442544a020SIlya Dryomov  * @resp_len: in/out param for reply length
5045428a7158SDouglas Fuller  */
5046428a7158SDouglas Fuller int ceph_osdc_call(struct ceph_osd_client *osdc,
5047428a7158SDouglas Fuller 		   struct ceph_object_id *oid,
5048428a7158SDouglas Fuller 		   struct ceph_object_locator *oloc,
5049428a7158SDouglas Fuller 		   const char *class, const char *method,
5050428a7158SDouglas Fuller 		   unsigned int flags,
5051428a7158SDouglas Fuller 		   struct page *req_page, size_t req_len,
505268ada915SIlya Dryomov 		   struct page **resp_pages, size_t *resp_len)
5053428a7158SDouglas Fuller {
5054428a7158SDouglas Fuller 	struct ceph_osd_request *req;
5055428a7158SDouglas Fuller 	int ret;
5056428a7158SDouglas Fuller 
505768ada915SIlya Dryomov 	if (req_len > PAGE_SIZE)
50582544a020SIlya Dryomov 		return -E2BIG;
50592544a020SIlya Dryomov 
5060428a7158SDouglas Fuller 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
5061428a7158SDouglas Fuller 	if (!req)
5062428a7158SDouglas Fuller 		return -ENOMEM;
5063428a7158SDouglas Fuller 
5064428a7158SDouglas Fuller 	ceph_oid_copy(&req->r_base_oid, oid);
5065428a7158SDouglas Fuller 	ceph_oloc_copy(&req->r_base_oloc, oloc);
5066428a7158SDouglas Fuller 	req->r_flags = flags;
5067428a7158SDouglas Fuller 
506824639ce5SIlya Dryomov 	ret = osd_req_op_cls_init(req, 0, class, method);
5069fe943d50SChengguang Xu 	if (ret)
5070fe943d50SChengguang Xu 		goto out_put_req;
5071fe943d50SChengguang Xu 
5072428a7158SDouglas Fuller 	if (req_page)
5073428a7158SDouglas Fuller 		osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
5074428a7158SDouglas Fuller 						  0, false, false);
507568ada915SIlya Dryomov 	if (resp_pages)
507668ada915SIlya Dryomov 		osd_req_op_cls_response_data_pages(req, 0, resp_pages,
50772544a020SIlya Dryomov 						   *resp_len, 0, false, false);
5078428a7158SDouglas Fuller 
507926f887e0SIlya Dryomov 	ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
508026f887e0SIlya Dryomov 	if (ret)
508126f887e0SIlya Dryomov 		goto out_put_req;
508226f887e0SIlya Dryomov 
5083428a7158SDouglas Fuller 	ceph_osdc_start_request(osdc, req, false);
5084428a7158SDouglas Fuller 	ret = ceph_osdc_wait_request(osdc, req);
5085428a7158SDouglas Fuller 	if (ret >= 0) {
5086428a7158SDouglas Fuller 		ret = req->r_ops[0].rval;
508768ada915SIlya Dryomov 		if (resp_pages)
5088428a7158SDouglas Fuller 			*resp_len = req->r_ops[0].outdata_len;
5089428a7158SDouglas Fuller 	}
5090428a7158SDouglas Fuller 
5091428a7158SDouglas Fuller out_put_req:
5092428a7158SDouglas Fuller 	ceph_osdc_put_request(req);
5093428a7158SDouglas Fuller 	return ret;
5094428a7158SDouglas Fuller }
5095428a7158SDouglas Fuller EXPORT_SYMBOL(ceph_osdc_call);
5096428a7158SDouglas Fuller 
5097428a7158SDouglas Fuller /*
5098120a75eaSYan, Zheng  * reset all osd connections
5099120a75eaSYan, Zheng  */
5100120a75eaSYan, Zheng void ceph_osdc_reopen_osds(struct ceph_osd_client *osdc)
5101120a75eaSYan, Zheng {
5102120a75eaSYan, Zheng 	struct rb_node *n;
5103120a75eaSYan, Zheng 
5104120a75eaSYan, Zheng 	down_write(&osdc->lock);
5105120a75eaSYan, Zheng 	for (n = rb_first(&osdc->osds); n; ) {
5106120a75eaSYan, Zheng 		struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
5107120a75eaSYan, Zheng 
5108120a75eaSYan, Zheng 		n = rb_next(n);
5109120a75eaSYan, Zheng 		if (!reopen_osd(osd))
5110120a75eaSYan, Zheng 			kick_osd_requests(osd);
5111120a75eaSYan, Zheng 	}
5112120a75eaSYan, Zheng 	up_write(&osdc->lock);
5113120a75eaSYan, Zheng }
5114120a75eaSYan, Zheng 
5115120a75eaSYan, Zheng /*
51163d14c5d2SYehuda Sadeh  * init, shutdown
51173d14c5d2SYehuda Sadeh  */
51183d14c5d2SYehuda Sadeh int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
51193d14c5d2SYehuda Sadeh {
51203d14c5d2SYehuda Sadeh 	int err;
51213d14c5d2SYehuda Sadeh 
51223d14c5d2SYehuda Sadeh 	dout("init\n");
51233d14c5d2SYehuda Sadeh 	osdc->client = client;
51245aea3dcdSIlya Dryomov 	init_rwsem(&osdc->lock);
51253d14c5d2SYehuda Sadeh 	osdc->osds = RB_ROOT;
51263d14c5d2SYehuda Sadeh 	INIT_LIST_HEAD(&osdc->osd_lru);
51279dd2845cSIlya Dryomov 	spin_lock_init(&osdc->osd_lru_lock);
51285aea3dcdSIlya Dryomov 	osd_init(&osdc->homeless_osd);
51295aea3dcdSIlya Dryomov 	osdc->homeless_osd.o_osdc = osdc;
51305aea3dcdSIlya Dryomov 	osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
5131264048afSIlya Dryomov 	osdc->last_linger_id = CEPH_LINGER_ID_START;
5132922dab61SIlya Dryomov 	osdc->linger_requests = RB_ROOT;
51334609245eSIlya Dryomov 	osdc->map_checks = RB_ROOT;
51344609245eSIlya Dryomov 	osdc->linger_map_checks = RB_ROOT;
51353d14c5d2SYehuda Sadeh 	INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
51363d14c5d2SYehuda Sadeh 	INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
51373d14c5d2SYehuda Sadeh 
51383d14c5d2SYehuda Sadeh 	err = -ENOMEM;
5139e5253a7bSIlya Dryomov 	osdc->osdmap = ceph_osdmap_alloc();
5140e5253a7bSIlya Dryomov 	if (!osdc->osdmap)
5141e5253a7bSIlya Dryomov 		goto out;
5142e5253a7bSIlya Dryomov 
51439e767adbSIlya Dryomov 	osdc->req_mempool = mempool_create_slab_pool(10,
51449e767adbSIlya Dryomov 						     ceph_osd_request_cache);
51453d14c5d2SYehuda Sadeh 	if (!osdc->req_mempool)
5146e5253a7bSIlya Dryomov 		goto out_map;
51473d14c5d2SYehuda Sadeh 
5148d50b409fSSage Weil 	err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
51490d9c1ab3SIlya Dryomov 				PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10, "osd_op");
51503d14c5d2SYehuda Sadeh 	if (err < 0)
51513d14c5d2SYehuda Sadeh 		goto out_mempool;
5152d50b409fSSage Weil 	err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
51530d9c1ab3SIlya Dryomov 				PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10,
51540d9c1ab3SIlya Dryomov 				"osd_op_reply");
51553d14c5d2SYehuda Sadeh 	if (err < 0)
51563d14c5d2SYehuda Sadeh 		goto out_msgpool;
5157a40c4f10SYehuda Sadeh 
5158dbcae088SDan Carpenter 	err = -ENOMEM;
5159a40c4f10SYehuda Sadeh 	osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
5160dbcae088SDan Carpenter 	if (!osdc->notify_wq)
5161c172ec5cSIlya Dryomov 		goto out_msgpool_reply;
5162c172ec5cSIlya Dryomov 
516388bc1922SIlya Dryomov 	osdc->completion_wq = create_singlethread_workqueue("ceph-completion");
516488bc1922SIlya Dryomov 	if (!osdc->completion_wq)
516588bc1922SIlya Dryomov 		goto out_notify_wq;
516688bc1922SIlya Dryomov 
5167fbca9635SIlya Dryomov 	schedule_delayed_work(&osdc->timeout_work,
5168fbca9635SIlya Dryomov 			      osdc->client->options->osd_keepalive_timeout);
5169b37ee1b9SIlya Dryomov 	schedule_delayed_work(&osdc->osds_timeout_work,
5170b37ee1b9SIlya Dryomov 	    round_jiffies_relative(osdc->client->options->osd_idle_ttl));
5171b37ee1b9SIlya Dryomov 
51723d14c5d2SYehuda Sadeh 	return 0;
51733d14c5d2SYehuda Sadeh 
517488bc1922SIlya Dryomov out_notify_wq:
517588bc1922SIlya Dryomov 	destroy_workqueue(osdc->notify_wq);
5176c172ec5cSIlya Dryomov out_msgpool_reply:
5177c172ec5cSIlya Dryomov 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
51783d14c5d2SYehuda Sadeh out_msgpool:
51793d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op);
51803d14c5d2SYehuda Sadeh out_mempool:
51813d14c5d2SYehuda Sadeh 	mempool_destroy(osdc->req_mempool);
5182e5253a7bSIlya Dryomov out_map:
5183e5253a7bSIlya Dryomov 	ceph_osdmap_destroy(osdc->osdmap);
51843d14c5d2SYehuda Sadeh out:
51853d14c5d2SYehuda Sadeh 	return err;
51863d14c5d2SYehuda Sadeh }
51873d14c5d2SYehuda Sadeh 
51883d14c5d2SYehuda Sadeh void ceph_osdc_stop(struct ceph_osd_client *osdc)
51893d14c5d2SYehuda Sadeh {
519088bc1922SIlya Dryomov 	destroy_workqueue(osdc->completion_wq);
5191a40c4f10SYehuda Sadeh 	destroy_workqueue(osdc->notify_wq);
51923d14c5d2SYehuda Sadeh 	cancel_delayed_work_sync(&osdc->timeout_work);
51933d14c5d2SYehuda Sadeh 	cancel_delayed_work_sync(&osdc->osds_timeout_work);
519442a2c09fSIlya Dryomov 
51955aea3dcdSIlya Dryomov 	down_write(&osdc->lock);
519642a2c09fSIlya Dryomov 	while (!RB_EMPTY_ROOT(&osdc->osds)) {
519742a2c09fSIlya Dryomov 		struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
519842a2c09fSIlya Dryomov 						struct ceph_osd, o_node);
51995aea3dcdSIlya Dryomov 		close_osd(osd);
520042a2c09fSIlya Dryomov 	}
52015aea3dcdSIlya Dryomov 	up_write(&osdc->lock);
520202113a0fSElena Reshetova 	WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
52035aea3dcdSIlya Dryomov 	osd_cleanup(&osdc->homeless_osd);
52045aea3dcdSIlya Dryomov 
52055aea3dcdSIlya Dryomov 	WARN_ON(!list_empty(&osdc->osd_lru));
5206922dab61SIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
52074609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
52084609245eSIlya Dryomov 	WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
52095aea3dcdSIlya Dryomov 	WARN_ON(atomic_read(&osdc->num_requests));
52105aea3dcdSIlya Dryomov 	WARN_ON(atomic_read(&osdc->num_homeless));
521142a2c09fSIlya Dryomov 
52123d14c5d2SYehuda Sadeh 	ceph_osdmap_destroy(osdc->osdmap);
52133d14c5d2SYehuda Sadeh 	mempool_destroy(osdc->req_mempool);
52143d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op);
52153d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
52163d14c5d2SYehuda Sadeh }
52173d14c5d2SYehuda Sadeh 
52183d14c5d2SYehuda Sadeh /*
52193d14c5d2SYehuda Sadeh  * Read some contiguous pages.  If we cross a stripe boundary, shorten
52203d14c5d2SYehuda Sadeh  * *plen.  Return number of bytes read, or error.
52213d14c5d2SYehuda Sadeh  */
52223d14c5d2SYehuda Sadeh int ceph_osdc_readpages(struct ceph_osd_client *osdc,
52233d14c5d2SYehuda Sadeh 			struct ceph_vino vino, struct ceph_file_layout *layout,
52243d14c5d2SYehuda Sadeh 			u64 off, u64 *plen,
52253d14c5d2SYehuda Sadeh 			u32 truncate_seq, u64 truncate_size,
5226b7495fc2SSage Weil 			struct page **pages, int num_pages, int page_align)
52273d14c5d2SYehuda Sadeh {
52283d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
52293d14c5d2SYehuda Sadeh 	int rc = 0;
52303d14c5d2SYehuda Sadeh 
52313d14c5d2SYehuda Sadeh 	dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
52323d14c5d2SYehuda Sadeh 	     vino.snap, off, *plen);
5233715e4cd4SYan, Zheng 	req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
52343d14c5d2SYehuda Sadeh 				    CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
5235acead002SAlex Elder 				    NULL, truncate_seq, truncate_size,
5236153e5167SAlex Elder 				    false);
52376816282dSSage Weil 	if (IS_ERR(req))
52386816282dSSage Weil 		return PTR_ERR(req);
52393d14c5d2SYehuda Sadeh 
52403d14c5d2SYehuda Sadeh 	/* it may be a short read due to an object boundary */
5241406e2c9fSAlex Elder 	osd_req_op_extent_osd_data_pages(req, 0,
5242a4ce40a9SAlex Elder 				pages, *plen, page_align, false, false);
52433d14c5d2SYehuda Sadeh 
5244e0c59487SAlex Elder 	dout("readpages  final extent is %llu~%llu (%llu bytes align %d)\n",
524543bfe5deSAlex Elder 	     off, *plen, *plen, page_align);
52463d14c5d2SYehuda Sadeh 
52473d14c5d2SYehuda Sadeh 	rc = ceph_osdc_start_request(osdc, req, false);
52483d14c5d2SYehuda Sadeh 	if (!rc)
52493d14c5d2SYehuda Sadeh 		rc = ceph_osdc_wait_request(osdc, req);
52503d14c5d2SYehuda Sadeh 
52513d14c5d2SYehuda Sadeh 	ceph_osdc_put_request(req);
52523d14c5d2SYehuda Sadeh 	dout("readpages result %d\n", rc);
52533d14c5d2SYehuda Sadeh 	return rc;
52543d14c5d2SYehuda Sadeh }
52553d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_readpages);
52563d14c5d2SYehuda Sadeh 
52573d14c5d2SYehuda Sadeh /*
52583d14c5d2SYehuda Sadeh  * do a synchronous write on N pages
52593d14c5d2SYehuda Sadeh  */
52603d14c5d2SYehuda Sadeh int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
52613d14c5d2SYehuda Sadeh 			 struct ceph_file_layout *layout,
52623d14c5d2SYehuda Sadeh 			 struct ceph_snap_context *snapc,
52633d14c5d2SYehuda Sadeh 			 u64 off, u64 len,
52643d14c5d2SYehuda Sadeh 			 u32 truncate_seq, u64 truncate_size,
5265fac02ddfSArnd Bergmann 			 struct timespec64 *mtime,
526624808826SAlex Elder 			 struct page **pages, int num_pages)
52673d14c5d2SYehuda Sadeh {
52683d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
52693d14c5d2SYehuda Sadeh 	int rc = 0;
5270b7495fc2SSage Weil 	int page_align = off & ~PAGE_MASK;
52713d14c5d2SYehuda Sadeh 
5272715e4cd4SYan, Zheng 	req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
527354ea0046SIlya Dryomov 				    CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
5274acead002SAlex Elder 				    snapc, truncate_seq, truncate_size,
5275153e5167SAlex Elder 				    true);
52766816282dSSage Weil 	if (IS_ERR(req))
52776816282dSSage Weil 		return PTR_ERR(req);
52783d14c5d2SYehuda Sadeh 
52793d14c5d2SYehuda Sadeh 	/* it may be a short write due to an object boundary */
5280406e2c9fSAlex Elder 	osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
528143bfe5deSAlex Elder 				false, false);
528243bfe5deSAlex Elder 	dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
52833d14c5d2SYehuda Sadeh 
5284bb873b53SIlya Dryomov 	req->r_mtime = *mtime;
528587f979d3SAlex Elder 	rc = ceph_osdc_start_request(osdc, req, true);
52863d14c5d2SYehuda Sadeh 	if (!rc)
52873d14c5d2SYehuda Sadeh 		rc = ceph_osdc_wait_request(osdc, req);
52883d14c5d2SYehuda Sadeh 
52893d14c5d2SYehuda Sadeh 	ceph_osdc_put_request(req);
52903d14c5d2SYehuda Sadeh 	if (rc == 0)
52913d14c5d2SYehuda Sadeh 		rc = len;
52923d14c5d2SYehuda Sadeh 	dout("writepages result %d\n", rc);
52933d14c5d2SYehuda Sadeh 	return rc;
52943d14c5d2SYehuda Sadeh }
52953d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_writepages);
52963d14c5d2SYehuda Sadeh 
529723ddf9beSLuis Henriques static int osd_req_op_copy_from_init(struct ceph_osd_request *req,
529823ddf9beSLuis Henriques 				     u64 src_snapid, u64 src_version,
529923ddf9beSLuis Henriques 				     struct ceph_object_id *src_oid,
530023ddf9beSLuis Henriques 				     struct ceph_object_locator *src_oloc,
530123ddf9beSLuis Henriques 				     u32 src_fadvise_flags,
530223ddf9beSLuis Henriques 				     u32 dst_fadvise_flags,
530323ddf9beSLuis Henriques 				     u8 copy_from_flags)
530423ddf9beSLuis Henriques {
530523ddf9beSLuis Henriques 	struct ceph_osd_req_op *op;
530623ddf9beSLuis Henriques 	struct page **pages;
530723ddf9beSLuis Henriques 	void *p, *end;
530823ddf9beSLuis Henriques 
530923ddf9beSLuis Henriques 	pages = ceph_alloc_page_vector(1, GFP_KERNEL);
531023ddf9beSLuis Henriques 	if (IS_ERR(pages))
531123ddf9beSLuis Henriques 		return PTR_ERR(pages);
531223ddf9beSLuis Henriques 
531323ddf9beSLuis Henriques 	op = _osd_req_op_init(req, 0, CEPH_OSD_OP_COPY_FROM, dst_fadvise_flags);
531423ddf9beSLuis Henriques 	op->copy_from.snapid = src_snapid;
531523ddf9beSLuis Henriques 	op->copy_from.src_version = src_version;
531623ddf9beSLuis Henriques 	op->copy_from.flags = copy_from_flags;
531723ddf9beSLuis Henriques 	op->copy_from.src_fadvise_flags = src_fadvise_flags;
531823ddf9beSLuis Henriques 
531923ddf9beSLuis Henriques 	p = page_address(pages[0]);
532023ddf9beSLuis Henriques 	end = p + PAGE_SIZE;
532123ddf9beSLuis Henriques 	ceph_encode_string(&p, end, src_oid->name, src_oid->name_len);
532223ddf9beSLuis Henriques 	encode_oloc(&p, end, src_oloc);
532323ddf9beSLuis Henriques 	op->indata_len = PAGE_SIZE - (end - p);
532423ddf9beSLuis Henriques 
532523ddf9beSLuis Henriques 	ceph_osd_data_pages_init(&op->copy_from.osd_data, pages,
532623ddf9beSLuis Henriques 				 op->indata_len, 0, false, true);
532723ddf9beSLuis Henriques 	return 0;
532823ddf9beSLuis Henriques }
532923ddf9beSLuis Henriques 
533023ddf9beSLuis Henriques int ceph_osdc_copy_from(struct ceph_osd_client *osdc,
533123ddf9beSLuis Henriques 			u64 src_snapid, u64 src_version,
533223ddf9beSLuis Henriques 			struct ceph_object_id *src_oid,
533323ddf9beSLuis Henriques 			struct ceph_object_locator *src_oloc,
533423ddf9beSLuis Henriques 			u32 src_fadvise_flags,
533523ddf9beSLuis Henriques 			struct ceph_object_id *dst_oid,
533623ddf9beSLuis Henriques 			struct ceph_object_locator *dst_oloc,
533723ddf9beSLuis Henriques 			u32 dst_fadvise_flags,
533823ddf9beSLuis Henriques 			u8 copy_from_flags)
533923ddf9beSLuis Henriques {
534023ddf9beSLuis Henriques 	struct ceph_osd_request *req;
534123ddf9beSLuis Henriques 	int ret;
534223ddf9beSLuis Henriques 
534323ddf9beSLuis Henriques 	req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL);
534423ddf9beSLuis Henriques 	if (!req)
534523ddf9beSLuis Henriques 		return -ENOMEM;
534623ddf9beSLuis Henriques 
534723ddf9beSLuis Henriques 	req->r_flags = CEPH_OSD_FLAG_WRITE;
534823ddf9beSLuis Henriques 
534923ddf9beSLuis Henriques 	ceph_oloc_copy(&req->r_t.base_oloc, dst_oloc);
535023ddf9beSLuis Henriques 	ceph_oid_copy(&req->r_t.base_oid, dst_oid);
535123ddf9beSLuis Henriques 
535223ddf9beSLuis Henriques 	ret = osd_req_op_copy_from_init(req, src_snapid, src_version, src_oid,
535323ddf9beSLuis Henriques 					src_oloc, src_fadvise_flags,
535423ddf9beSLuis Henriques 					dst_fadvise_flags, copy_from_flags);
535523ddf9beSLuis Henriques 	if (ret)
535623ddf9beSLuis Henriques 		goto out;
535723ddf9beSLuis Henriques 
535823ddf9beSLuis Henriques 	ret = ceph_osdc_alloc_messages(req, GFP_KERNEL);
535923ddf9beSLuis Henriques 	if (ret)
536023ddf9beSLuis Henriques 		goto out;
536123ddf9beSLuis Henriques 
536223ddf9beSLuis Henriques 	ceph_osdc_start_request(osdc, req, false);
536323ddf9beSLuis Henriques 	ret = ceph_osdc_wait_request(osdc, req);
536423ddf9beSLuis Henriques 
536523ddf9beSLuis Henriques out:
536623ddf9beSLuis Henriques 	ceph_osdc_put_request(req);
536723ddf9beSLuis Henriques 	return ret;
536823ddf9beSLuis Henriques }
536923ddf9beSLuis Henriques EXPORT_SYMBOL(ceph_osdc_copy_from);
537023ddf9beSLuis Henriques 
537157a35dfbSChengguang Xu int __init ceph_osdc_setup(void)
53725522ae0bSAlex Elder {
53733f1af42aSIlya Dryomov 	size_t size = sizeof(struct ceph_osd_request) +
53743f1af42aSIlya Dryomov 	    CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
53753f1af42aSIlya Dryomov 
53765522ae0bSAlex Elder 	BUG_ON(ceph_osd_request_cache);
53773f1af42aSIlya Dryomov 	ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
53783f1af42aSIlya Dryomov 						   0, 0, NULL);
53795522ae0bSAlex Elder 
53805522ae0bSAlex Elder 	return ceph_osd_request_cache ? 0 : -ENOMEM;
53815522ae0bSAlex Elder }
53825522ae0bSAlex Elder 
53835522ae0bSAlex Elder void ceph_osdc_cleanup(void)
53845522ae0bSAlex Elder {
53855522ae0bSAlex Elder 	BUG_ON(!ceph_osd_request_cache);
53865522ae0bSAlex Elder 	kmem_cache_destroy(ceph_osd_request_cache);
53875522ae0bSAlex Elder 	ceph_osd_request_cache = NULL;
53885522ae0bSAlex Elder }
53895522ae0bSAlex Elder 
53903d14c5d2SYehuda Sadeh /*
53913d14c5d2SYehuda Sadeh  * handle incoming message
53923d14c5d2SYehuda Sadeh  */
53933d14c5d2SYehuda Sadeh static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
53943d14c5d2SYehuda Sadeh {
53953d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
53965aea3dcdSIlya Dryomov 	struct ceph_osd_client *osdc = osd->o_osdc;
53973d14c5d2SYehuda Sadeh 	int type = le16_to_cpu(msg->hdr.type);
53983d14c5d2SYehuda Sadeh 
53993d14c5d2SYehuda Sadeh 	switch (type) {
54003d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP:
54013d14c5d2SYehuda Sadeh 		ceph_osdc_handle_map(osdc, msg);
54023d14c5d2SYehuda Sadeh 		break;
54033d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY:
54045aea3dcdSIlya Dryomov 		handle_reply(osd, msg);
54053d14c5d2SYehuda Sadeh 		break;
5406a02a946dSIlya Dryomov 	case CEPH_MSG_OSD_BACKOFF:
5407a02a946dSIlya Dryomov 		handle_backoff(osd, msg);
5408a02a946dSIlya Dryomov 		break;
5409a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY:
5410a40c4f10SYehuda Sadeh 		handle_watch_notify(osdc, msg);
5411a40c4f10SYehuda Sadeh 		break;
54123d14c5d2SYehuda Sadeh 
54133d14c5d2SYehuda Sadeh 	default:
54143d14c5d2SYehuda Sadeh 		pr_err("received unknown message type %d %s\n", type,
54153d14c5d2SYehuda Sadeh 		       ceph_msg_type_name(type));
54163d14c5d2SYehuda Sadeh 	}
54175aea3dcdSIlya Dryomov 
54183d14c5d2SYehuda Sadeh 	ceph_msg_put(msg);
54193d14c5d2SYehuda Sadeh }
54203d14c5d2SYehuda Sadeh 
54213d14c5d2SYehuda Sadeh /*
5422d15f9d69SIlya Dryomov  * Lookup and return message for incoming reply.  Don't try to do
5423d15f9d69SIlya Dryomov  * anything about a larger than preallocated data portion of the
5424d15f9d69SIlya Dryomov  * message at the moment - for now, just skip the message.
54253d14c5d2SYehuda Sadeh  */
54263d14c5d2SYehuda Sadeh static struct ceph_msg *get_reply(struct ceph_connection *con,
54273d14c5d2SYehuda Sadeh 				  struct ceph_msg_header *hdr,
54283d14c5d2SYehuda Sadeh 				  int *skip)
54293d14c5d2SYehuda Sadeh {
54303d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
54313d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = osd->o_osdc;
54325aea3dcdSIlya Dryomov 	struct ceph_msg *m = NULL;
54333d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
54343f0a4ac5SIlya Dryomov 	int front_len = le32_to_cpu(hdr->front_len);
54353d14c5d2SYehuda Sadeh 	int data_len = le32_to_cpu(hdr->data_len);
54365aea3dcdSIlya Dryomov 	u64 tid = le64_to_cpu(hdr->tid);
54373d14c5d2SYehuda Sadeh 
54385aea3dcdSIlya Dryomov 	down_read(&osdc->lock);
54395aea3dcdSIlya Dryomov 	if (!osd_registered(osd)) {
54405aea3dcdSIlya Dryomov 		dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
54415aea3dcdSIlya Dryomov 		*skip = 1;
54425aea3dcdSIlya Dryomov 		goto out_unlock_osdc;
54435aea3dcdSIlya Dryomov 	}
54445aea3dcdSIlya Dryomov 	WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
54455aea3dcdSIlya Dryomov 
54465aea3dcdSIlya Dryomov 	mutex_lock(&osd->lock);
54475aea3dcdSIlya Dryomov 	req = lookup_request(&osd->o_requests, tid);
54483d14c5d2SYehuda Sadeh 	if (!req) {
5449cd8140c6SIlya Dryomov 		dout("%s osd%d tid %llu unknown, skipping\n", __func__,
5450cd8140c6SIlya Dryomov 		     osd->o_osd, tid);
5451d15f9d69SIlya Dryomov 		*skip = 1;
54525aea3dcdSIlya Dryomov 		goto out_unlock_session;
54533d14c5d2SYehuda Sadeh 	}
54543d14c5d2SYehuda Sadeh 
54558921d114SAlex Elder 	ceph_msg_revoke_incoming(req->r_reply);
54563d14c5d2SYehuda Sadeh 
5457f2be82b0SIlya Dryomov 	if (front_len > req->r_reply->front_alloc_len) {
5458d15f9d69SIlya Dryomov 		pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
5459d15f9d69SIlya Dryomov 			__func__, osd->o_osd, req->r_tid, front_len,
5460d15f9d69SIlya Dryomov 			req->r_reply->front_alloc_len);
54613f0a4ac5SIlya Dryomov 		m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
54623f0a4ac5SIlya Dryomov 				 false);
54633d14c5d2SYehuda Sadeh 		if (!m)
54645aea3dcdSIlya Dryomov 			goto out_unlock_session;
54653d14c5d2SYehuda Sadeh 		ceph_msg_put(req->r_reply);
54663d14c5d2SYehuda Sadeh 		req->r_reply = m;
54673d14c5d2SYehuda Sadeh 	}
54683d14c5d2SYehuda Sadeh 
5469d15f9d69SIlya Dryomov 	if (data_len > req->r_reply->data_length) {
5470d15f9d69SIlya Dryomov 		pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
5471d15f9d69SIlya Dryomov 			__func__, osd->o_osd, req->r_tid, data_len,
5472d15f9d69SIlya Dryomov 			req->r_reply->data_length);
54733d14c5d2SYehuda Sadeh 		m = NULL;
5474d15f9d69SIlya Dryomov 		*skip = 1;
54755aea3dcdSIlya Dryomov 		goto out_unlock_session;
54763d14c5d2SYehuda Sadeh 	}
5477d15f9d69SIlya Dryomov 
5478d15f9d69SIlya Dryomov 	m = ceph_msg_get(req->r_reply);
54793d14c5d2SYehuda Sadeh 	dout("get_reply tid %lld %p\n", tid, m);
54803d14c5d2SYehuda Sadeh 
54815aea3dcdSIlya Dryomov out_unlock_session:
54825aea3dcdSIlya Dryomov 	mutex_unlock(&osd->lock);
54835aea3dcdSIlya Dryomov out_unlock_osdc:
54845aea3dcdSIlya Dryomov 	up_read(&osdc->lock);
54853d14c5d2SYehuda Sadeh 	return m;
54863d14c5d2SYehuda Sadeh }
54873d14c5d2SYehuda Sadeh 
548819079203SIlya Dryomov /*
548919079203SIlya Dryomov  * TODO: switch to a msg-owned pagelist
549019079203SIlya Dryomov  */
549119079203SIlya Dryomov static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
549219079203SIlya Dryomov {
549319079203SIlya Dryomov 	struct ceph_msg *m;
549419079203SIlya Dryomov 	int type = le16_to_cpu(hdr->type);
549519079203SIlya Dryomov 	u32 front_len = le32_to_cpu(hdr->front_len);
549619079203SIlya Dryomov 	u32 data_len = le32_to_cpu(hdr->data_len);
549719079203SIlya Dryomov 
54980d9c1ab3SIlya Dryomov 	m = ceph_msg_new2(type, front_len, 1, GFP_NOIO, false);
549919079203SIlya Dryomov 	if (!m)
550019079203SIlya Dryomov 		return NULL;
550119079203SIlya Dryomov 
550219079203SIlya Dryomov 	if (data_len) {
550319079203SIlya Dryomov 		struct page **pages;
550419079203SIlya Dryomov 		struct ceph_osd_data osd_data;
550519079203SIlya Dryomov 
550619079203SIlya Dryomov 		pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
550719079203SIlya Dryomov 					       GFP_NOIO);
5508c22e853aSWei Yongjun 		if (IS_ERR(pages)) {
550919079203SIlya Dryomov 			ceph_msg_put(m);
551019079203SIlya Dryomov 			return NULL;
551119079203SIlya Dryomov 		}
551219079203SIlya Dryomov 
551319079203SIlya Dryomov 		ceph_osd_data_pages_init(&osd_data, pages, data_len, 0, false,
551419079203SIlya Dryomov 					 false);
551519079203SIlya Dryomov 		ceph_osdc_msg_data_add(m, &osd_data);
551619079203SIlya Dryomov 	}
551719079203SIlya Dryomov 
551819079203SIlya Dryomov 	return m;
551919079203SIlya Dryomov }
552019079203SIlya Dryomov 
55213d14c5d2SYehuda Sadeh static struct ceph_msg *alloc_msg(struct ceph_connection *con,
55223d14c5d2SYehuda Sadeh 				  struct ceph_msg_header *hdr,
55233d14c5d2SYehuda Sadeh 				  int *skip)
55243d14c5d2SYehuda Sadeh {
55253d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
55263d14c5d2SYehuda Sadeh 	int type = le16_to_cpu(hdr->type);
55273d14c5d2SYehuda Sadeh 
55281c20f2d2SAlex Elder 	*skip = 0;
55293d14c5d2SYehuda Sadeh 	switch (type) {
55303d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP:
5531a02a946dSIlya Dryomov 	case CEPH_MSG_OSD_BACKOFF:
5532a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY:
553319079203SIlya Dryomov 		return alloc_msg_with_page_vector(hdr);
55343d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY:
55353d14c5d2SYehuda Sadeh 		return get_reply(con, hdr, skip);
55363d14c5d2SYehuda Sadeh 	default:
55375aea3dcdSIlya Dryomov 		pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
55385aea3dcdSIlya Dryomov 			osd->o_osd, type);
55393d14c5d2SYehuda Sadeh 		*skip = 1;
55403d14c5d2SYehuda Sadeh 		return NULL;
55413d14c5d2SYehuda Sadeh 	}
55423d14c5d2SYehuda Sadeh }
55433d14c5d2SYehuda Sadeh 
55443d14c5d2SYehuda Sadeh /*
55453d14c5d2SYehuda Sadeh  * Wrappers to refcount containing ceph_osd struct
55463d14c5d2SYehuda Sadeh  */
55473d14c5d2SYehuda Sadeh static struct ceph_connection *get_osd_con(struct ceph_connection *con)
55483d14c5d2SYehuda Sadeh {
55493d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
55503d14c5d2SYehuda Sadeh 	if (get_osd(osd))
55513d14c5d2SYehuda Sadeh 		return con;
55523d14c5d2SYehuda Sadeh 	return NULL;
55533d14c5d2SYehuda Sadeh }
55543d14c5d2SYehuda Sadeh 
55553d14c5d2SYehuda Sadeh static void put_osd_con(struct ceph_connection *con)
55563d14c5d2SYehuda Sadeh {
55573d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
55583d14c5d2SYehuda Sadeh 	put_osd(osd);
55593d14c5d2SYehuda Sadeh }
55603d14c5d2SYehuda Sadeh 
55613d14c5d2SYehuda Sadeh /*
55623d14c5d2SYehuda Sadeh  * authentication
55633d14c5d2SYehuda Sadeh  */
5564a3530df3SAlex Elder /*
5565a3530df3SAlex Elder  * Note: returned pointer is the address of a structure that's
5566a3530df3SAlex Elder  * managed separately.  Caller must *not* attempt to free it.
5567a3530df3SAlex Elder  */
5568a3530df3SAlex Elder static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
55698f43fb53SAlex Elder 					int *proto, int force_new)
55703d14c5d2SYehuda Sadeh {
55713d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
55723d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
55733d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
557474f1869fSAlex Elder 	struct ceph_auth_handshake *auth = &o->o_auth;
55753d14c5d2SYehuda Sadeh 
557674f1869fSAlex Elder 	if (force_new && auth->authorizer) {
55776c1ea260SIlya Dryomov 		ceph_auth_destroy_authorizer(auth->authorizer);
557874f1869fSAlex Elder 		auth->authorizer = NULL;
55793d14c5d2SYehuda Sadeh 	}
558027859f97SSage Weil 	if (!auth->authorizer) {
558127859f97SSage Weil 		int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
5582a3530df3SAlex Elder 						      auth);
55833d14c5d2SYehuda Sadeh 		if (ret)
5584a3530df3SAlex Elder 			return ERR_PTR(ret);
558527859f97SSage Weil 	} else {
558627859f97SSage Weil 		int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
55870bed9b5cSSage Weil 						     auth);
55880bed9b5cSSage Weil 		if (ret)
55890bed9b5cSSage Weil 			return ERR_PTR(ret);
55903d14c5d2SYehuda Sadeh 	}
55913d14c5d2SYehuda Sadeh 	*proto = ac->protocol;
559274f1869fSAlex Elder 
5593a3530df3SAlex Elder 	return auth;
55943d14c5d2SYehuda Sadeh }
55953d14c5d2SYehuda Sadeh 
55966daca13dSIlya Dryomov static int add_authorizer_challenge(struct ceph_connection *con,
55976daca13dSIlya Dryomov 				    void *challenge_buf, int challenge_buf_len)
55986daca13dSIlya Dryomov {
55996daca13dSIlya Dryomov 	struct ceph_osd *o = con->private;
56006daca13dSIlya Dryomov 	struct ceph_osd_client *osdc = o->o_osdc;
56016daca13dSIlya Dryomov 	struct ceph_auth_client *ac = osdc->client->monc.auth;
56026daca13dSIlya Dryomov 
56036daca13dSIlya Dryomov 	return ceph_auth_add_authorizer_challenge(ac, o->o_auth.authorizer,
56046daca13dSIlya Dryomov 					    challenge_buf, challenge_buf_len);
56056daca13dSIlya Dryomov }
56063d14c5d2SYehuda Sadeh 
56070dde5848SIlya Dryomov static int verify_authorizer_reply(struct ceph_connection *con)
56083d14c5d2SYehuda Sadeh {
56093d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
56103d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
56113d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
56123d14c5d2SYehuda Sadeh 
56130dde5848SIlya Dryomov 	return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer);
56143d14c5d2SYehuda Sadeh }
56153d14c5d2SYehuda Sadeh 
56163d14c5d2SYehuda Sadeh static int invalidate_authorizer(struct ceph_connection *con)
56173d14c5d2SYehuda Sadeh {
56183d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
56193d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
56203d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
56213d14c5d2SYehuda Sadeh 
562227859f97SSage Weil 	ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
56233d14c5d2SYehuda Sadeh 	return ceph_monc_validate_auth(&osdc->client->monc);
56243d14c5d2SYehuda Sadeh }
56253d14c5d2SYehuda Sadeh 
56268cb441c0SIlya Dryomov static void osd_reencode_message(struct ceph_msg *msg)
56278cb441c0SIlya Dryomov {
5628914902afSIlya Dryomov 	int type = le16_to_cpu(msg->hdr.type);
5629914902afSIlya Dryomov 
5630914902afSIlya Dryomov 	if (type == CEPH_MSG_OSD_OP)
56318cb441c0SIlya Dryomov 		encode_request_finish(msg);
56328cb441c0SIlya Dryomov }
56338cb441c0SIlya Dryomov 
563479dbd1baSIlya Dryomov static int osd_sign_message(struct ceph_msg *msg)
563533d07337SYan, Zheng {
563679dbd1baSIlya Dryomov 	struct ceph_osd *o = msg->con->private;
563733d07337SYan, Zheng 	struct ceph_auth_handshake *auth = &o->o_auth;
563879dbd1baSIlya Dryomov 
563933d07337SYan, Zheng 	return ceph_auth_sign_message(auth, msg);
564033d07337SYan, Zheng }
564133d07337SYan, Zheng 
564279dbd1baSIlya Dryomov static int osd_check_message_signature(struct ceph_msg *msg)
564333d07337SYan, Zheng {
564479dbd1baSIlya Dryomov 	struct ceph_osd *o = msg->con->private;
564533d07337SYan, Zheng 	struct ceph_auth_handshake *auth = &o->o_auth;
564679dbd1baSIlya Dryomov 
564733d07337SYan, Zheng 	return ceph_auth_check_message_signature(auth, msg);
564833d07337SYan, Zheng }
564933d07337SYan, Zheng 
56503d14c5d2SYehuda Sadeh static const struct ceph_connection_operations osd_con_ops = {
56513d14c5d2SYehuda Sadeh 	.get = get_osd_con,
56523d14c5d2SYehuda Sadeh 	.put = put_osd_con,
56533d14c5d2SYehuda Sadeh 	.dispatch = dispatch,
56543d14c5d2SYehuda Sadeh 	.get_authorizer = get_authorizer,
56556daca13dSIlya Dryomov 	.add_authorizer_challenge = add_authorizer_challenge,
56563d14c5d2SYehuda Sadeh 	.verify_authorizer_reply = verify_authorizer_reply,
56573d14c5d2SYehuda Sadeh 	.invalidate_authorizer = invalidate_authorizer,
56583d14c5d2SYehuda Sadeh 	.alloc_msg = alloc_msg,
56598cb441c0SIlya Dryomov 	.reencode_message = osd_reencode_message,
566079dbd1baSIlya Dryomov 	.sign_message = osd_sign_message,
566179dbd1baSIlya Dryomov 	.check_message_signature = osd_check_message_signature,
56625aea3dcdSIlya Dryomov 	.fault = osd_fault,
56633d14c5d2SYehuda Sadeh };
5664