xref: /openbmc/linux/net/ceph/osd_client.c (revision 864e9197)
1a4ce40a9SAlex Elder 
23d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h>
33d14c5d2SYehuda Sadeh 
43d14c5d2SYehuda Sadeh #include <linux/module.h>
53d14c5d2SYehuda Sadeh #include <linux/err.h>
63d14c5d2SYehuda Sadeh #include <linux/highmem.h>
73d14c5d2SYehuda Sadeh #include <linux/mm.h>
83d14c5d2SYehuda Sadeh #include <linux/pagemap.h>
93d14c5d2SYehuda Sadeh #include <linux/slab.h>
103d14c5d2SYehuda Sadeh #include <linux/uaccess.h>
113d14c5d2SYehuda Sadeh #ifdef CONFIG_BLOCK
123d14c5d2SYehuda Sadeh #include <linux/bio.h>
133d14c5d2SYehuda Sadeh #endif
143d14c5d2SYehuda Sadeh 
153d14c5d2SYehuda Sadeh #include <linux/ceph/libceph.h>
163d14c5d2SYehuda Sadeh #include <linux/ceph/osd_client.h>
173d14c5d2SYehuda Sadeh #include <linux/ceph/messenger.h>
183d14c5d2SYehuda Sadeh #include <linux/ceph/decode.h>
193d14c5d2SYehuda Sadeh #include <linux/ceph/auth.h>
203d14c5d2SYehuda Sadeh #include <linux/ceph/pagelist.h>
213d14c5d2SYehuda Sadeh 
223d14c5d2SYehuda Sadeh #define OSD_OP_FRONT_LEN	4096
233d14c5d2SYehuda Sadeh #define OSD_OPREPLY_FRONT_LEN	512
243d14c5d2SYehuda Sadeh 
255522ae0bSAlex Elder static struct kmem_cache	*ceph_osd_request_cache;
265522ae0bSAlex Elder 
273d14c5d2SYehuda Sadeh static const struct ceph_connection_operations osd_con_ops;
283d14c5d2SYehuda Sadeh 
29f9d25199SAlex Elder static void __send_queued(struct ceph_osd_client *osdc);
306f6c7006SSage Weil static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
31a40c4f10SYehuda Sadeh static void __register_request(struct ceph_osd_client *osdc,
32a40c4f10SYehuda Sadeh 			       struct ceph_osd_request *req);
332cc6128aSIlya Dryomov static void __unregister_request(struct ceph_osd_client *osdc,
342cc6128aSIlya Dryomov 				 struct ceph_osd_request *req);
35a40c4f10SYehuda Sadeh static void __unregister_linger_request(struct ceph_osd_client *osdc,
36a40c4f10SYehuda Sadeh 					struct ceph_osd_request *req);
372cc6128aSIlya Dryomov static void __enqueue_request(struct ceph_osd_request *req);
3856e925b6SSage Weil static void __send_request(struct ceph_osd_client *osdc,
39a40c4f10SYehuda Sadeh 			   struct ceph_osd_request *req);
403d14c5d2SYehuda Sadeh 
413d14c5d2SYehuda Sadeh /*
423d14c5d2SYehuda Sadeh  * Implement client access to distributed object storage cluster.
433d14c5d2SYehuda Sadeh  *
443d14c5d2SYehuda Sadeh  * All data objects are stored within a cluster/cloud of OSDs, or
453d14c5d2SYehuda Sadeh  * "object storage devices."  (Note that Ceph OSDs have _nothing_ to
463d14c5d2SYehuda Sadeh  * do with the T10 OSD extensions to SCSI.)  Ceph OSDs are simply
473d14c5d2SYehuda Sadeh  * remote daemons serving up and coordinating consistent and safe
483d14c5d2SYehuda Sadeh  * access to storage.
493d14c5d2SYehuda Sadeh  *
503d14c5d2SYehuda Sadeh  * Cluster membership and the mapping of data objects onto storage devices
513d14c5d2SYehuda Sadeh  * are described by the osd map.
523d14c5d2SYehuda Sadeh  *
533d14c5d2SYehuda Sadeh  * We keep track of pending OSD requests (read, write), resubmit
543d14c5d2SYehuda Sadeh  * requests to different OSDs when the cluster topology/data layout
553d14c5d2SYehuda Sadeh  * change, or retry the affected requests when the communications
563d14c5d2SYehuda Sadeh  * channel with an OSD is reset.
573d14c5d2SYehuda Sadeh  */
583d14c5d2SYehuda Sadeh 
593d14c5d2SYehuda Sadeh /*
603d14c5d2SYehuda Sadeh  * calculate the mapping of a file extent onto an object, and fill out the
613d14c5d2SYehuda Sadeh  * request accordingly.  shorten extent as necessary if it crosses an
623d14c5d2SYehuda Sadeh  * object boundary.
633d14c5d2SYehuda Sadeh  *
643d14c5d2SYehuda Sadeh  * fill osd op in request message.
653d14c5d2SYehuda Sadeh  */
66dbe0fc41SAlex Elder static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
67a19dadfbSAlex Elder 			u64 *objnum, u64 *objoff, u64 *objlen)
683d14c5d2SYehuda Sadeh {
6960e56f13SAlex Elder 	u64 orig_len = *plen;
70d63b77f4SSage Weil 	int r;
713d14c5d2SYehuda Sadeh 
7260e56f13SAlex Elder 	/* object extent? */
7375d1c941SAlex Elder 	r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
7475d1c941SAlex Elder 					  objoff, objlen);
75d63b77f4SSage Weil 	if (r < 0)
76d63b77f4SSage Weil 		return r;
7775d1c941SAlex Elder 	if (*objlen < orig_len) {
7875d1c941SAlex Elder 		*plen = *objlen;
7960e56f13SAlex Elder 		dout(" skipping last %llu, final file extent %llu~%llu\n",
8060e56f13SAlex Elder 		     orig_len - *plen, off, *plen);
8160e56f13SAlex Elder 	}
8260e56f13SAlex Elder 
8375d1c941SAlex Elder 	dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
843d14c5d2SYehuda Sadeh 
853ff5f385SAlex Elder 	return 0;
863d14c5d2SYehuda Sadeh }
873d14c5d2SYehuda Sadeh 
88c54d47bfSAlex Elder static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
89c54d47bfSAlex Elder {
90c54d47bfSAlex Elder 	memset(osd_data, 0, sizeof (*osd_data));
91c54d47bfSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
92c54d47bfSAlex Elder }
93c54d47bfSAlex Elder 
94a4ce40a9SAlex Elder static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
9543bfe5deSAlex Elder 			struct page **pages, u64 length, u32 alignment,
9643bfe5deSAlex Elder 			bool pages_from_pool, bool own_pages)
9743bfe5deSAlex Elder {
9843bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
9943bfe5deSAlex Elder 	osd_data->pages = pages;
10043bfe5deSAlex Elder 	osd_data->length = length;
10143bfe5deSAlex Elder 	osd_data->alignment = alignment;
10243bfe5deSAlex Elder 	osd_data->pages_from_pool = pages_from_pool;
10343bfe5deSAlex Elder 	osd_data->own_pages = own_pages;
10443bfe5deSAlex Elder }
10543bfe5deSAlex Elder 
106a4ce40a9SAlex Elder static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
10743bfe5deSAlex Elder 			struct ceph_pagelist *pagelist)
10843bfe5deSAlex Elder {
10943bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
11043bfe5deSAlex Elder 	osd_data->pagelist = pagelist;
11143bfe5deSAlex Elder }
11243bfe5deSAlex Elder 
11343bfe5deSAlex Elder #ifdef CONFIG_BLOCK
114a4ce40a9SAlex Elder static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
11543bfe5deSAlex Elder 			struct bio *bio, size_t bio_length)
11643bfe5deSAlex Elder {
11743bfe5deSAlex Elder 	osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
11843bfe5deSAlex Elder 	osd_data->bio = bio;
11943bfe5deSAlex Elder 	osd_data->bio_length = bio_length;
12043bfe5deSAlex Elder }
12143bfe5deSAlex Elder #endif /* CONFIG_BLOCK */
12243bfe5deSAlex Elder 
123863c7eb5SAlex Elder #define osd_req_op_data(oreq, whch, typ, fld)	\
124863c7eb5SAlex Elder 	({						\
125863c7eb5SAlex Elder 		BUG_ON(whch >= (oreq)->r_num_ops);	\
126863c7eb5SAlex Elder 		&(oreq)->r_ops[whch].typ.fld;		\
127863c7eb5SAlex Elder 	})
128863c7eb5SAlex Elder 
12949719778SAlex Elder static struct ceph_osd_data *
13049719778SAlex Elder osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
13149719778SAlex Elder {
13249719778SAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
13349719778SAlex Elder 
13449719778SAlex Elder 	return &osd_req->r_ops[which].raw_data_in;
13549719778SAlex Elder }
13649719778SAlex Elder 
137a4ce40a9SAlex Elder struct ceph_osd_data *
138a4ce40a9SAlex Elder osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
139406e2c9fSAlex Elder 			unsigned int which)
140a4ce40a9SAlex Elder {
141863c7eb5SAlex Elder 	return osd_req_op_data(osd_req, which, extent, osd_data);
142a4ce40a9SAlex Elder }
143a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data);
144a4ce40a9SAlex Elder 
145a4ce40a9SAlex Elder struct ceph_osd_data *
146a4ce40a9SAlex Elder osd_req_op_cls_response_data(struct ceph_osd_request *osd_req,
147a4ce40a9SAlex Elder 			unsigned int which)
148a4ce40a9SAlex Elder {
149863c7eb5SAlex Elder 	return osd_req_op_data(osd_req, which, cls, response_data);
150a4ce40a9SAlex Elder }
151a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_response_data);	/* ??? */
152a4ce40a9SAlex Elder 
15349719778SAlex Elder void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
15449719778SAlex Elder 			unsigned int which, struct page **pages,
15549719778SAlex Elder 			u64 length, u32 alignment,
15649719778SAlex Elder 			bool pages_from_pool, bool own_pages)
15749719778SAlex Elder {
15849719778SAlex Elder 	struct ceph_osd_data *osd_data;
15949719778SAlex Elder 
16049719778SAlex Elder 	osd_data = osd_req_op_raw_data_in(osd_req, which);
16149719778SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
16249719778SAlex Elder 				pages_from_pool, own_pages);
16349719778SAlex Elder }
16449719778SAlex Elder EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
16549719778SAlex Elder 
166a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
167406e2c9fSAlex Elder 			unsigned int which, struct page **pages,
168406e2c9fSAlex Elder 			u64 length, u32 alignment,
169a4ce40a9SAlex Elder 			bool pages_from_pool, bool own_pages)
170a4ce40a9SAlex Elder {
171a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
172a4ce40a9SAlex Elder 
173863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
174a4ce40a9SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
175a4ce40a9SAlex Elder 				pages_from_pool, own_pages);
176a4ce40a9SAlex Elder }
177a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
178a4ce40a9SAlex Elder 
179a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
180406e2c9fSAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
181a4ce40a9SAlex Elder {
182a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
183a4ce40a9SAlex Elder 
184863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
185a4ce40a9SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
186a4ce40a9SAlex Elder }
187a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
188a4ce40a9SAlex Elder 
189a4ce40a9SAlex Elder #ifdef CONFIG_BLOCK
190a4ce40a9SAlex Elder void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
191406e2c9fSAlex Elder 			unsigned int which, struct bio *bio, size_t bio_length)
192a4ce40a9SAlex Elder {
193a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
194863c7eb5SAlex Elder 
195863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
196a4ce40a9SAlex Elder 	ceph_osd_data_bio_init(osd_data, bio, bio_length);
197a4ce40a9SAlex Elder }
198a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
199a4ce40a9SAlex Elder #endif /* CONFIG_BLOCK */
200a4ce40a9SAlex Elder 
201a4ce40a9SAlex Elder static void osd_req_op_cls_request_info_pagelist(
202a4ce40a9SAlex Elder 			struct ceph_osd_request *osd_req,
203a4ce40a9SAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
204a4ce40a9SAlex Elder {
205a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
206a4ce40a9SAlex Elder 
207863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_info);
208a4ce40a9SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
209a4ce40a9SAlex Elder }
210a4ce40a9SAlex Elder 
21104017e29SAlex Elder void osd_req_op_cls_request_data_pagelist(
21204017e29SAlex Elder 			struct ceph_osd_request *osd_req,
21304017e29SAlex Elder 			unsigned int which, struct ceph_pagelist *pagelist)
21404017e29SAlex Elder {
21504017e29SAlex Elder 	struct ceph_osd_data *osd_data;
21604017e29SAlex Elder 
217863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
21804017e29SAlex Elder 	ceph_osd_data_pagelist_init(osd_data, pagelist);
21904017e29SAlex Elder }
22004017e29SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
22104017e29SAlex Elder 
2226c57b554SAlex Elder void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
2236c57b554SAlex Elder 			unsigned int which, struct page **pages, u64 length,
2246c57b554SAlex Elder 			u32 alignment, bool pages_from_pool, bool own_pages)
2256c57b554SAlex Elder {
2266c57b554SAlex Elder 	struct ceph_osd_data *osd_data;
2276c57b554SAlex Elder 
2286c57b554SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, request_data);
2296c57b554SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
2306c57b554SAlex Elder 				pages_from_pool, own_pages);
2316c57b554SAlex Elder }
2326c57b554SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
2336c57b554SAlex Elder 
234a4ce40a9SAlex Elder void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
235a4ce40a9SAlex Elder 			unsigned int which, struct page **pages, u64 length,
236a4ce40a9SAlex Elder 			u32 alignment, bool pages_from_pool, bool own_pages)
237a4ce40a9SAlex Elder {
238a4ce40a9SAlex Elder 	struct ceph_osd_data *osd_data;
239a4ce40a9SAlex Elder 
240863c7eb5SAlex Elder 	osd_data = osd_req_op_data(osd_req, which, cls, response_data);
241a4ce40a9SAlex Elder 	ceph_osd_data_pages_init(osd_data, pages, length, alignment,
242a4ce40a9SAlex Elder 				pages_from_pool, own_pages);
243a4ce40a9SAlex Elder }
244a4ce40a9SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
245a4ce40a9SAlex Elder 
24623c08a9cSAlex Elder static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
24723c08a9cSAlex Elder {
24823c08a9cSAlex Elder 	switch (osd_data->type) {
24923c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_NONE:
25023c08a9cSAlex Elder 		return 0;
25123c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_PAGES:
25223c08a9cSAlex Elder 		return osd_data->length;
25323c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_PAGELIST:
25423c08a9cSAlex Elder 		return (u64)osd_data->pagelist->length;
25523c08a9cSAlex Elder #ifdef CONFIG_BLOCK
25623c08a9cSAlex Elder 	case CEPH_OSD_DATA_TYPE_BIO:
25723c08a9cSAlex Elder 		return (u64)osd_data->bio_length;
25823c08a9cSAlex Elder #endif /* CONFIG_BLOCK */
25923c08a9cSAlex Elder 	default:
26023c08a9cSAlex Elder 		WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
26123c08a9cSAlex Elder 		return 0;
26223c08a9cSAlex Elder 	}
26323c08a9cSAlex Elder }
26423c08a9cSAlex Elder 
265c54d47bfSAlex Elder static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
266c54d47bfSAlex Elder {
2675476492fSAlex Elder 	if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
268c54d47bfSAlex Elder 		int num_pages;
269c54d47bfSAlex Elder 
270c54d47bfSAlex Elder 		num_pages = calc_pages_for((u64)osd_data->alignment,
271c54d47bfSAlex Elder 						(u64)osd_data->length);
272c54d47bfSAlex Elder 		ceph_release_page_vector(osd_data->pages, num_pages);
273c54d47bfSAlex Elder 	}
2745476492fSAlex Elder 	ceph_osd_data_init(osd_data);
2755476492fSAlex Elder }
2765476492fSAlex Elder 
2775476492fSAlex Elder static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
2785476492fSAlex Elder 			unsigned int which)
2795476492fSAlex Elder {
2805476492fSAlex Elder 	struct ceph_osd_req_op *op;
2815476492fSAlex Elder 
2825476492fSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
2835476492fSAlex Elder 	op = &osd_req->r_ops[which];
2845476492fSAlex Elder 
2855476492fSAlex Elder 	switch (op->op) {
2865476492fSAlex Elder 	case CEPH_OSD_OP_READ:
2875476492fSAlex Elder 	case CEPH_OSD_OP_WRITE:
2885476492fSAlex Elder 		ceph_osd_data_release(&op->extent.osd_data);
2895476492fSAlex Elder 		break;
2905476492fSAlex Elder 	case CEPH_OSD_OP_CALL:
2915476492fSAlex Elder 		ceph_osd_data_release(&op->cls.request_info);
29204017e29SAlex Elder 		ceph_osd_data_release(&op->cls.request_data);
2935476492fSAlex Elder 		ceph_osd_data_release(&op->cls.response_data);
2945476492fSAlex Elder 		break;
295d74b50beSYan, Zheng 	case CEPH_OSD_OP_SETXATTR:
296d74b50beSYan, Zheng 	case CEPH_OSD_OP_CMPXATTR:
297d74b50beSYan, Zheng 		ceph_osd_data_release(&op->xattr.osd_data);
298d74b50beSYan, Zheng 		break;
2995476492fSAlex Elder 	default:
3005476492fSAlex Elder 		break;
3015476492fSAlex Elder 	}
302c54d47bfSAlex Elder }
303c54d47bfSAlex Elder 
3043d14c5d2SYehuda Sadeh /*
3053d14c5d2SYehuda Sadeh  * requests
3063d14c5d2SYehuda Sadeh  */
3079e94af20SIlya Dryomov static void ceph_osdc_release_request(struct kref *kref)
3083d14c5d2SYehuda Sadeh {
3099e94af20SIlya Dryomov 	struct ceph_osd_request *req = container_of(kref,
3109e94af20SIlya Dryomov 					    struct ceph_osd_request, r_kref);
3115476492fSAlex Elder 	unsigned int which;
3123d14c5d2SYehuda Sadeh 
3139e94af20SIlya Dryomov 	dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
3149e94af20SIlya Dryomov 	     req->r_request, req->r_reply);
3156562d661SIlya Dryomov 	WARN_ON(!RB_EMPTY_NODE(&req->r_node));
3166562d661SIlya Dryomov 	WARN_ON(!list_empty(&req->r_req_lru_item));
3176562d661SIlya Dryomov 	WARN_ON(!list_empty(&req->r_osd_item));
3186562d661SIlya Dryomov 	WARN_ON(!list_empty(&req->r_linger_item));
3196562d661SIlya Dryomov 	WARN_ON(!list_empty(&req->r_linger_osd_item));
3206562d661SIlya Dryomov 	WARN_ON(req->r_osd);
3219e94af20SIlya Dryomov 
3223d14c5d2SYehuda Sadeh 	if (req->r_request)
3233d14c5d2SYehuda Sadeh 		ceph_msg_put(req->r_request);
324ace6d3a9SAlex Elder 	if (req->r_reply) {
3258921d114SAlex Elder 		ceph_msg_revoke_incoming(req->r_reply);
326ab8cb34aSAlex Elder 		ceph_msg_put(req->r_reply);
327ace6d3a9SAlex Elder 	}
3280fff87ecSAlex Elder 
3295476492fSAlex Elder 	for (which = 0; which < req->r_num_ops; which++)
3305476492fSAlex Elder 		osd_req_op_data_release(req, which);
3310fff87ecSAlex Elder 
3323d14c5d2SYehuda Sadeh 	ceph_put_snap_context(req->r_snapc);
3333d14c5d2SYehuda Sadeh 	if (req->r_mempool)
3343d14c5d2SYehuda Sadeh 		mempool_free(req, req->r_osdc->req_mempool);
3353d14c5d2SYehuda Sadeh 	else
3365522ae0bSAlex Elder 		kmem_cache_free(ceph_osd_request_cache, req);
3375522ae0bSAlex Elder 
3383d14c5d2SYehuda Sadeh }
3399e94af20SIlya Dryomov 
3409e94af20SIlya Dryomov void ceph_osdc_get_request(struct ceph_osd_request *req)
3419e94af20SIlya Dryomov {
3429e94af20SIlya Dryomov 	dout("%s %p (was %d)\n", __func__, req,
3439e94af20SIlya Dryomov 	     atomic_read(&req->r_kref.refcount));
3449e94af20SIlya Dryomov 	kref_get(&req->r_kref);
3459e94af20SIlya Dryomov }
3469e94af20SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_get_request);
3479e94af20SIlya Dryomov 
3489e94af20SIlya Dryomov void ceph_osdc_put_request(struct ceph_osd_request *req)
3499e94af20SIlya Dryomov {
3509e94af20SIlya Dryomov 	dout("%s %p (was %d)\n", __func__, req,
3519e94af20SIlya Dryomov 	     atomic_read(&req->r_kref.refcount));
3529e94af20SIlya Dryomov 	kref_put(&req->r_kref, ceph_osdc_release_request);
3539e94af20SIlya Dryomov }
3549e94af20SIlya Dryomov EXPORT_SYMBOL(ceph_osdc_put_request);
3553d14c5d2SYehuda Sadeh 
3563d14c5d2SYehuda Sadeh struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
3573d14c5d2SYehuda Sadeh 					       struct ceph_snap_context *snapc,
3581b83bef2SSage Weil 					       unsigned int num_ops,
3593d14c5d2SYehuda Sadeh 					       bool use_mempool,
36054a54007SAlex Elder 					       gfp_t gfp_flags)
3613d14c5d2SYehuda Sadeh {
3623d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
3633d14c5d2SYehuda Sadeh 	struct ceph_msg *msg;
3641b83bef2SSage Weil 	size_t msg_size;
3653d14c5d2SYehuda Sadeh 
36679528734SAlex Elder 	BUILD_BUG_ON(CEPH_OSD_MAX_OP > U16_MAX);
36779528734SAlex Elder 	BUG_ON(num_ops > CEPH_OSD_MAX_OP);
36879528734SAlex Elder 
3691b83bef2SSage Weil 	msg_size = 4 + 4 + 8 + 8 + 4+8;
3701b83bef2SSage Weil 	msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
3711b83bef2SSage Weil 	msg_size += 1 + 8 + 4 + 4;     /* pg_t */
3722d0ebc5dSIlya Dryomov 	msg_size += 4 + CEPH_MAX_OID_NAME_LEN; /* oid */
3731b83bef2SSage Weil 	msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
3741b83bef2SSage Weil 	msg_size += 8;  /* snapid */
3751b83bef2SSage Weil 	msg_size += 8;  /* snap_seq */
3761b83bef2SSage Weil 	msg_size += 8 * (snapc ? snapc->num_snaps : 0);  /* snaps */
3771b83bef2SSage Weil 	msg_size += 4;
3783d14c5d2SYehuda Sadeh 
3793d14c5d2SYehuda Sadeh 	if (use_mempool) {
3803d14c5d2SYehuda Sadeh 		req = mempool_alloc(osdc->req_mempool, gfp_flags);
3813d14c5d2SYehuda Sadeh 		memset(req, 0, sizeof(*req));
3823d14c5d2SYehuda Sadeh 	} else {
3835522ae0bSAlex Elder 		req = kmem_cache_zalloc(ceph_osd_request_cache, gfp_flags);
3843d14c5d2SYehuda Sadeh 	}
3853d14c5d2SYehuda Sadeh 	if (req == NULL)
3863d14c5d2SYehuda Sadeh 		return NULL;
3873d14c5d2SYehuda Sadeh 
3883d14c5d2SYehuda Sadeh 	req->r_osdc = osdc;
3893d14c5d2SYehuda Sadeh 	req->r_mempool = use_mempool;
39079528734SAlex Elder 	req->r_num_ops = num_ops;
3913d14c5d2SYehuda Sadeh 
3923d14c5d2SYehuda Sadeh 	kref_init(&req->r_kref);
3933d14c5d2SYehuda Sadeh 	init_completion(&req->r_completion);
3943d14c5d2SYehuda Sadeh 	init_completion(&req->r_safe_completion);
395a978fa20SAlex Elder 	RB_CLEAR_NODE(&req->r_node);
3963d14c5d2SYehuda Sadeh 	INIT_LIST_HEAD(&req->r_unsafe_item);
397a40c4f10SYehuda Sadeh 	INIT_LIST_HEAD(&req->r_linger_item);
3981d0326b1SIlya Dryomov 	INIT_LIST_HEAD(&req->r_linger_osd_item);
399935b639aSSage Weil 	INIT_LIST_HEAD(&req->r_req_lru_item);
400cd43045cSSage Weil 	INIT_LIST_HEAD(&req->r_osd_item);
401cd43045cSSage Weil 
4023c972c95SIlya Dryomov 	req->r_base_oloc.pool = -1;
403205ee118SIlya Dryomov 	req->r_target_oloc.pool = -1;
40422116525SIlya Dryomov 
4053d14c5d2SYehuda Sadeh 	/* create reply message */
4063d14c5d2SYehuda Sadeh 	if (use_mempool)
4073d14c5d2SYehuda Sadeh 		msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
4083d14c5d2SYehuda Sadeh 	else
4093d14c5d2SYehuda Sadeh 		msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
410b61c2763SSage Weil 				   OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
4113d14c5d2SYehuda Sadeh 	if (!msg) {
4123d14c5d2SYehuda Sadeh 		ceph_osdc_put_request(req);
4133d14c5d2SYehuda Sadeh 		return NULL;
4143d14c5d2SYehuda Sadeh 	}
4153d14c5d2SYehuda Sadeh 	req->r_reply = msg;
4163d14c5d2SYehuda Sadeh 
4173d14c5d2SYehuda Sadeh 	/* create request message; allow space for oid */
4183d14c5d2SYehuda Sadeh 	if (use_mempool)
4193d14c5d2SYehuda Sadeh 		msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
4203d14c5d2SYehuda Sadeh 	else
421b61c2763SSage Weil 		msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
4223d14c5d2SYehuda Sadeh 	if (!msg) {
4233d14c5d2SYehuda Sadeh 		ceph_osdc_put_request(req);
4243d14c5d2SYehuda Sadeh 		return NULL;
4253d14c5d2SYehuda Sadeh 	}
4263d14c5d2SYehuda Sadeh 
4273d14c5d2SYehuda Sadeh 	memset(msg->front.iov_base, 0, msg->front.iov_len);
4283d14c5d2SYehuda Sadeh 
4293d14c5d2SYehuda Sadeh 	req->r_request = msg;
4303d14c5d2SYehuda Sadeh 
4313d14c5d2SYehuda Sadeh 	return req;
4323d14c5d2SYehuda Sadeh }
4333d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_alloc_request);
4343d14c5d2SYehuda Sadeh 
435a8dd0a37SAlex Elder static bool osd_req_opcode_valid(u16 opcode)
436a8dd0a37SAlex Elder {
437a8dd0a37SAlex Elder 	switch (opcode) {
43870b5bfa3SIlya Dryomov #define GENERATE_CASE(op, opcode, str)	case CEPH_OSD_OP_##op: return true;
43970b5bfa3SIlya Dryomov __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
44070b5bfa3SIlya Dryomov #undef GENERATE_CASE
441a8dd0a37SAlex Elder 	default:
442a8dd0a37SAlex Elder 		return false;
443a8dd0a37SAlex Elder 	}
444a8dd0a37SAlex Elder }
445a8dd0a37SAlex Elder 
44633803f33SAlex Elder /*
44733803f33SAlex Elder  * This is an osd op init function for opcodes that have no data or
44833803f33SAlex Elder  * other information associated with them.  It also serves as a
44933803f33SAlex Elder  * common init routine for all the other init functions, below.
45033803f33SAlex Elder  */
451c99d2d4aSAlex Elder static struct ceph_osd_req_op *
45249719778SAlex Elder _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
453c99d2d4aSAlex Elder 				u16 opcode)
45433803f33SAlex Elder {
455c99d2d4aSAlex Elder 	struct ceph_osd_req_op *op;
456c99d2d4aSAlex Elder 
457c99d2d4aSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
45833803f33SAlex Elder 	BUG_ON(!osd_req_opcode_valid(opcode));
45933803f33SAlex Elder 
460c99d2d4aSAlex Elder 	op = &osd_req->r_ops[which];
46133803f33SAlex Elder 	memset(op, 0, sizeof (*op));
46233803f33SAlex Elder 	op->op = opcode;
463c99d2d4aSAlex Elder 
464c99d2d4aSAlex Elder 	return op;
46533803f33SAlex Elder }
46633803f33SAlex Elder 
46749719778SAlex Elder void osd_req_op_init(struct ceph_osd_request *osd_req,
46849719778SAlex Elder 				unsigned int which, u16 opcode)
46949719778SAlex Elder {
47049719778SAlex Elder 	(void)_osd_req_op_init(osd_req, which, opcode);
47149719778SAlex Elder }
47249719778SAlex Elder EXPORT_SYMBOL(osd_req_op_init);
47349719778SAlex Elder 
474c99d2d4aSAlex Elder void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
475c99d2d4aSAlex Elder 				unsigned int which, u16 opcode,
47633803f33SAlex Elder 				u64 offset, u64 length,
47733803f33SAlex Elder 				u64 truncate_size, u32 truncate_seq)
47833803f33SAlex Elder {
47949719778SAlex Elder 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
48033803f33SAlex Elder 	size_t payload_len = 0;
48133803f33SAlex Elder 
482ad7a60deSLi Wang 	BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
483864e9197SYan, Zheng 	       opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE);
48433803f33SAlex Elder 
48533803f33SAlex Elder 	op->extent.offset = offset;
48633803f33SAlex Elder 	op->extent.length = length;
48733803f33SAlex Elder 	op->extent.truncate_size = truncate_size;
48833803f33SAlex Elder 	op->extent.truncate_seq = truncate_seq;
48933803f33SAlex Elder 	if (opcode == CEPH_OSD_OP_WRITE)
49033803f33SAlex Elder 		payload_len += length;
49133803f33SAlex Elder 
49233803f33SAlex Elder 	op->payload_len = payload_len;
49333803f33SAlex Elder }
49433803f33SAlex Elder EXPORT_SYMBOL(osd_req_op_extent_init);
49533803f33SAlex Elder 
496c99d2d4aSAlex Elder void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
497c99d2d4aSAlex Elder 				unsigned int which, u64 length)
498e5975c7cSAlex Elder {
499c99d2d4aSAlex Elder 	struct ceph_osd_req_op *op;
500c99d2d4aSAlex Elder 	u64 previous;
501c99d2d4aSAlex Elder 
502c99d2d4aSAlex Elder 	BUG_ON(which >= osd_req->r_num_ops);
503c99d2d4aSAlex Elder 	op = &osd_req->r_ops[which];
504c99d2d4aSAlex Elder 	previous = op->extent.length;
505e5975c7cSAlex Elder 
506e5975c7cSAlex Elder 	if (length == previous)
507e5975c7cSAlex Elder 		return;		/* Nothing to do */
508e5975c7cSAlex Elder 	BUG_ON(length > previous);
509e5975c7cSAlex Elder 
510e5975c7cSAlex Elder 	op->extent.length = length;
511e5975c7cSAlex Elder 	op->payload_len -= previous - length;
512e5975c7cSAlex Elder }
513e5975c7cSAlex Elder EXPORT_SYMBOL(osd_req_op_extent_update);
514e5975c7cSAlex Elder 
515c99d2d4aSAlex Elder void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
51604017e29SAlex Elder 			u16 opcode, const char *class, const char *method)
51733803f33SAlex Elder {
51849719778SAlex Elder 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
5195f562df5SAlex Elder 	struct ceph_pagelist *pagelist;
52033803f33SAlex Elder 	size_t payload_len = 0;
52133803f33SAlex Elder 	size_t size;
52233803f33SAlex Elder 
52333803f33SAlex Elder 	BUG_ON(opcode != CEPH_OSD_OP_CALL);
52433803f33SAlex Elder 
5255f562df5SAlex Elder 	pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
5265f562df5SAlex Elder 	BUG_ON(!pagelist);
5275f562df5SAlex Elder 	ceph_pagelist_init(pagelist);
5285f562df5SAlex Elder 
52933803f33SAlex Elder 	op->cls.class_name = class;
53033803f33SAlex Elder 	size = strlen(class);
53133803f33SAlex Elder 	BUG_ON(size > (size_t) U8_MAX);
53233803f33SAlex Elder 	op->cls.class_len = size;
5335f562df5SAlex Elder 	ceph_pagelist_append(pagelist, class, size);
53433803f33SAlex Elder 	payload_len += size;
53533803f33SAlex Elder 
53633803f33SAlex Elder 	op->cls.method_name = method;
53733803f33SAlex Elder 	size = strlen(method);
53833803f33SAlex Elder 	BUG_ON(size > (size_t) U8_MAX);
53933803f33SAlex Elder 	op->cls.method_len = size;
5405f562df5SAlex Elder 	ceph_pagelist_append(pagelist, method, size);
54133803f33SAlex Elder 	payload_len += size;
54233803f33SAlex Elder 
543a4ce40a9SAlex Elder 	osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
5445f562df5SAlex Elder 
54533803f33SAlex Elder 	op->cls.argc = 0;	/* currently unused */
54633803f33SAlex Elder 
54733803f33SAlex Elder 	op->payload_len = payload_len;
54833803f33SAlex Elder }
54933803f33SAlex Elder EXPORT_SYMBOL(osd_req_op_cls_init);
5508c042b0dSAlex Elder 
551d74b50beSYan, Zheng int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
552d74b50beSYan, Zheng 			  u16 opcode, const char *name, const void *value,
553d74b50beSYan, Zheng 			  size_t size, u8 cmp_op, u8 cmp_mode)
554d74b50beSYan, Zheng {
555d74b50beSYan, Zheng 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
556d74b50beSYan, Zheng 	struct ceph_pagelist *pagelist;
557d74b50beSYan, Zheng 	size_t payload_len;
558d74b50beSYan, Zheng 
559d74b50beSYan, Zheng 	BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
560d74b50beSYan, Zheng 
561d74b50beSYan, Zheng 	pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
562d74b50beSYan, Zheng 	if (!pagelist)
563d74b50beSYan, Zheng 		return -ENOMEM;
564d74b50beSYan, Zheng 
565d74b50beSYan, Zheng 	ceph_pagelist_init(pagelist);
566d74b50beSYan, Zheng 
567d74b50beSYan, Zheng 	payload_len = strlen(name);
568d74b50beSYan, Zheng 	op->xattr.name_len = payload_len;
569d74b50beSYan, Zheng 	ceph_pagelist_append(pagelist, name, payload_len);
570d74b50beSYan, Zheng 
571d74b50beSYan, Zheng 	op->xattr.value_len = size;
572d74b50beSYan, Zheng 	ceph_pagelist_append(pagelist, value, size);
573d74b50beSYan, Zheng 	payload_len += size;
574d74b50beSYan, Zheng 
575d74b50beSYan, Zheng 	op->xattr.cmp_op = cmp_op;
576d74b50beSYan, Zheng 	op->xattr.cmp_mode = cmp_mode;
577d74b50beSYan, Zheng 
578d74b50beSYan, Zheng 	ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
579d74b50beSYan, Zheng 	op->payload_len = payload_len;
580d74b50beSYan, Zheng 	return 0;
581d74b50beSYan, Zheng }
582d74b50beSYan, Zheng EXPORT_SYMBOL(osd_req_op_xattr_init);
583d74b50beSYan, Zheng 
584c99d2d4aSAlex Elder void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
585c99d2d4aSAlex Elder 				unsigned int which, u16 opcode,
58633803f33SAlex Elder 				u64 cookie, u64 version, int flag)
58733803f33SAlex Elder {
58849719778SAlex Elder 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
58933803f33SAlex Elder 
590c99d2d4aSAlex Elder 	BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
59133803f33SAlex Elder 
59233803f33SAlex Elder 	op->watch.cookie = cookie;
5939ef1ee5aSAlex Elder 	op->watch.ver = version;
59433803f33SAlex Elder 	if (opcode == CEPH_OSD_OP_WATCH && flag)
59533803f33SAlex Elder 		op->watch.flag = (u8)1;
59633803f33SAlex Elder }
59733803f33SAlex Elder EXPORT_SYMBOL(osd_req_op_watch_init);
59833803f33SAlex Elder 
599c647b8a8SIlya Dryomov void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
600c647b8a8SIlya Dryomov 				unsigned int which,
601c647b8a8SIlya Dryomov 				u64 expected_object_size,
602c647b8a8SIlya Dryomov 				u64 expected_write_size)
603c647b8a8SIlya Dryomov {
604c647b8a8SIlya Dryomov 	struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
605c647b8a8SIlya Dryomov 						      CEPH_OSD_OP_SETALLOCHINT);
606c647b8a8SIlya Dryomov 
607c647b8a8SIlya Dryomov 	op->alloc_hint.expected_object_size = expected_object_size;
608c647b8a8SIlya Dryomov 	op->alloc_hint.expected_write_size = expected_write_size;
609c647b8a8SIlya Dryomov 
610c647b8a8SIlya Dryomov 	/*
611c647b8a8SIlya Dryomov 	 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
612c647b8a8SIlya Dryomov 	 * not worth a feature bit.  Set FAILOK per-op flag to make
613c647b8a8SIlya Dryomov 	 * sure older osds don't trip over an unsupported opcode.
614c647b8a8SIlya Dryomov 	 */
615c647b8a8SIlya Dryomov 	op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
616c647b8a8SIlya Dryomov }
617c647b8a8SIlya Dryomov EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
618c647b8a8SIlya Dryomov 
61990af3602SAlex Elder static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
620ec9123c5SAlex Elder 				struct ceph_osd_data *osd_data)
621ec9123c5SAlex Elder {
622ec9123c5SAlex Elder 	u64 length = ceph_osd_data_length(osd_data);
623ec9123c5SAlex Elder 
624ec9123c5SAlex Elder 	if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
625ec9123c5SAlex Elder 		BUG_ON(length > (u64) SIZE_MAX);
626ec9123c5SAlex Elder 		if (length)
62790af3602SAlex Elder 			ceph_msg_data_add_pages(msg, osd_data->pages,
628ec9123c5SAlex Elder 					length, osd_data->alignment);
629ec9123c5SAlex Elder 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
630ec9123c5SAlex Elder 		BUG_ON(!length);
63190af3602SAlex Elder 		ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
632ec9123c5SAlex Elder #ifdef CONFIG_BLOCK
633ec9123c5SAlex Elder 	} else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
63490af3602SAlex Elder 		ceph_msg_data_add_bio(msg, osd_data->bio, length);
635ec9123c5SAlex Elder #endif
636ec9123c5SAlex Elder 	} else {
637ec9123c5SAlex Elder 		BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
638ec9123c5SAlex Elder 	}
639ec9123c5SAlex Elder }
640ec9123c5SAlex Elder 
641175face2SAlex Elder static u64 osd_req_encode_op(struct ceph_osd_request *req,
64279528734SAlex Elder 			      struct ceph_osd_op *dst, unsigned int which)
6433d14c5d2SYehuda Sadeh {
64479528734SAlex Elder 	struct ceph_osd_req_op *src;
64504017e29SAlex Elder 	struct ceph_osd_data *osd_data;
64654d50649SAlex Elder 	u64 request_data_len = 0;
64704017e29SAlex Elder 	u64 data_length;
648175face2SAlex Elder 
64979528734SAlex Elder 	BUG_ON(which >= req->r_num_ops);
65079528734SAlex Elder 	src = &req->r_ops[which];
651a8dd0a37SAlex Elder 	if (WARN_ON(!osd_req_opcode_valid(src->op))) {
652a8dd0a37SAlex Elder 		pr_err("unrecognized osd opcode %d\n", src->op);
653a8dd0a37SAlex Elder 
654a8dd0a37SAlex Elder 		return 0;
655a8dd0a37SAlex Elder 	}
6563d14c5d2SYehuda Sadeh 
657065a68f9SAlex Elder 	switch (src->op) {
658fbfab539SAlex Elder 	case CEPH_OSD_OP_STAT:
65949719778SAlex Elder 		osd_data = &src->raw_data_in;
66049719778SAlex Elder 		ceph_osdc_msg_data_add(req->r_reply, osd_data);
661fbfab539SAlex Elder 		break;
6623d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_READ:
6633d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_WRITE:
664ad7a60deSLi Wang 	case CEPH_OSD_OP_ZERO:
665ad7a60deSLi Wang 	case CEPH_OSD_OP_TRUNCATE:
666175face2SAlex Elder 		if (src->op == CEPH_OSD_OP_WRITE)
66754d50649SAlex Elder 			request_data_len = src->extent.length;
668175face2SAlex Elder 		dst->extent.offset = cpu_to_le64(src->extent.offset);
669175face2SAlex Elder 		dst->extent.length = cpu_to_le64(src->extent.length);
6703d14c5d2SYehuda Sadeh 		dst->extent.truncate_size =
6713d14c5d2SYehuda Sadeh 			cpu_to_le64(src->extent.truncate_size);
6723d14c5d2SYehuda Sadeh 		dst->extent.truncate_seq =
6733d14c5d2SYehuda Sadeh 			cpu_to_le32(src->extent.truncate_seq);
67404017e29SAlex Elder 		osd_data = &src->extent.osd_data;
6755476492fSAlex Elder 		if (src->op == CEPH_OSD_OP_WRITE)
67604017e29SAlex Elder 			ceph_osdc_msg_data_add(req->r_request, osd_data);
6775476492fSAlex Elder 		else
67804017e29SAlex Elder 			ceph_osdc_msg_data_add(req->r_reply, osd_data);
6793d14c5d2SYehuda Sadeh 		break;
6803d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_CALL:
6813d14c5d2SYehuda Sadeh 		dst->cls.class_len = src->cls.class_len;
6823d14c5d2SYehuda Sadeh 		dst->cls.method_len = src->cls.method_len;
68304017e29SAlex Elder 		osd_data = &src->cls.request_info;
68404017e29SAlex Elder 		ceph_osdc_msg_data_add(req->r_request, osd_data);
68504017e29SAlex Elder 		BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
68604017e29SAlex Elder 		request_data_len = osd_data->pagelist->length;
68704017e29SAlex Elder 
68804017e29SAlex Elder 		osd_data = &src->cls.request_data;
68904017e29SAlex Elder 		data_length = ceph_osd_data_length(osd_data);
69004017e29SAlex Elder 		if (data_length) {
69104017e29SAlex Elder 			BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
69204017e29SAlex Elder 			dst->cls.indata_len = cpu_to_le32(data_length);
69304017e29SAlex Elder 			ceph_osdc_msg_data_add(req->r_request, osd_data);
69404017e29SAlex Elder 			src->payload_len += data_length;
69504017e29SAlex Elder 			request_data_len += data_length;
69604017e29SAlex Elder 		}
69704017e29SAlex Elder 		osd_data = &src->cls.response_data;
69804017e29SAlex Elder 		ceph_osdc_msg_data_add(req->r_reply, osd_data);
6993d14c5d2SYehuda Sadeh 		break;
7003d14c5d2SYehuda Sadeh 	case CEPH_OSD_OP_STARTSYNC:
7013d14c5d2SYehuda Sadeh 		break;
702a40c4f10SYehuda Sadeh 	case CEPH_OSD_OP_NOTIFY_ACK:
703a40c4f10SYehuda Sadeh 	case CEPH_OSD_OP_WATCH:
704a40c4f10SYehuda Sadeh 		dst->watch.cookie = cpu_to_le64(src->watch.cookie);
705a40c4f10SYehuda Sadeh 		dst->watch.ver = cpu_to_le64(src->watch.ver);
706a40c4f10SYehuda Sadeh 		dst->watch.flag = src->watch.flag;
707a40c4f10SYehuda Sadeh 		break;
708c647b8a8SIlya Dryomov 	case CEPH_OSD_OP_SETALLOCHINT:
709c647b8a8SIlya Dryomov 		dst->alloc_hint.expected_object_size =
710c647b8a8SIlya Dryomov 		    cpu_to_le64(src->alloc_hint.expected_object_size);
711c647b8a8SIlya Dryomov 		dst->alloc_hint.expected_write_size =
712c647b8a8SIlya Dryomov 		    cpu_to_le64(src->alloc_hint.expected_write_size);
713c647b8a8SIlya Dryomov 		break;
714d74b50beSYan, Zheng 	case CEPH_OSD_OP_SETXATTR:
715d74b50beSYan, Zheng 	case CEPH_OSD_OP_CMPXATTR:
716d74b50beSYan, Zheng 		dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
717d74b50beSYan, Zheng 		dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
718d74b50beSYan, Zheng 		dst->xattr.cmp_op = src->xattr.cmp_op;
719d74b50beSYan, Zheng 		dst->xattr.cmp_mode = src->xattr.cmp_mode;
720d74b50beSYan, Zheng 		osd_data = &src->xattr.osd_data;
721d74b50beSYan, Zheng 		ceph_osdc_msg_data_add(req->r_request, osd_data);
722d74b50beSYan, Zheng 		request_data_len = osd_data->pagelist->length;
723d74b50beSYan, Zheng 		break;
724864e9197SYan, Zheng 	case CEPH_OSD_OP_CREATE:
725864e9197SYan, Zheng 	case CEPH_OSD_OP_DELETE:
726864e9197SYan, Zheng 		break;
7273d14c5d2SYehuda Sadeh 	default:
7284c46459cSAlex Elder 		pr_err("unsupported osd opcode %s\n",
7298f63ca2dSAlex Elder 			ceph_osd_op_name(src->op));
7304c46459cSAlex Elder 		WARN_ON(1);
731a8dd0a37SAlex Elder 
732a8dd0a37SAlex Elder 		return 0;
7333d14c5d2SYehuda Sadeh 	}
7347b25bf5fSIlya Dryomov 
735a8dd0a37SAlex Elder 	dst->op = cpu_to_le16(src->op);
7367b25bf5fSIlya Dryomov 	dst->flags = cpu_to_le32(src->flags);
7373d14c5d2SYehuda Sadeh 	dst->payload_len = cpu_to_le32(src->payload_len);
738175face2SAlex Elder 
73954d50649SAlex Elder 	return request_data_len;
7403d14c5d2SYehuda Sadeh }
7413d14c5d2SYehuda Sadeh 
7423d14c5d2SYehuda Sadeh /*
7433d14c5d2SYehuda Sadeh  * build new request AND message, calculate layout, and adjust file
7443d14c5d2SYehuda Sadeh  * extent as needed.
7453d14c5d2SYehuda Sadeh  *
7463d14c5d2SYehuda Sadeh  * if the file was recently truncated, we include information about its
7473d14c5d2SYehuda Sadeh  * old and new size so that the object can be updated appropriately.  (we
7483d14c5d2SYehuda Sadeh  * avoid synchronously deleting truncated objects because it's slow.)
7493d14c5d2SYehuda Sadeh  *
7503d14c5d2SYehuda Sadeh  * if @do_sync, include a 'startsync' command so that the osd will flush
7513d14c5d2SYehuda Sadeh  * data quickly.
7523d14c5d2SYehuda Sadeh  */
7533d14c5d2SYehuda Sadeh struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
7543d14c5d2SYehuda Sadeh 					       struct ceph_file_layout *layout,
7553d14c5d2SYehuda Sadeh 					       struct ceph_vino vino,
756acead002SAlex Elder 					       u64 off, u64 *plen, int num_ops,
7573d14c5d2SYehuda Sadeh 					       int opcode, int flags,
7583d14c5d2SYehuda Sadeh 					       struct ceph_snap_context *snapc,
7593d14c5d2SYehuda Sadeh 					       u32 truncate_seq,
7603d14c5d2SYehuda Sadeh 					       u64 truncate_size,
761153e5167SAlex Elder 					       bool use_mempool)
7623d14c5d2SYehuda Sadeh {
7633d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
76475d1c941SAlex Elder 	u64 objnum = 0;
76575d1c941SAlex Elder 	u64 objoff = 0;
76675d1c941SAlex Elder 	u64 objlen = 0;
7676816282dSSage Weil 	int r;
7683d14c5d2SYehuda Sadeh 
769ad7a60deSLi Wang 	BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
770864e9197SYan, Zheng 	       opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
771864e9197SYan, Zheng 	       opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
7723d14c5d2SYehuda Sadeh 
773acead002SAlex Elder 	req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
774ae7ca4a3SAlex Elder 					GFP_NOFS);
7754ad12621SSage Weil 	if (!req)
7766816282dSSage Weil 		return ERR_PTR(-ENOMEM);
77779528734SAlex Elder 
778d178a9e7SAlex Elder 	req->r_flags = flags;
7793d14c5d2SYehuda Sadeh 
7803d14c5d2SYehuda Sadeh 	/* calculate max write size */
781a19dadfbSAlex Elder 	r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
7823ff5f385SAlex Elder 	if (r < 0) {
7833ff5f385SAlex Elder 		ceph_osdc_put_request(req);
7846816282dSSage Weil 		return ERR_PTR(r);
7853ff5f385SAlex Elder 	}
786a19dadfbSAlex Elder 
787864e9197SYan, Zheng 	if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
788864e9197SYan, Zheng 		osd_req_op_init(req, 0, opcode);
789864e9197SYan, Zheng 	} else {
790864e9197SYan, Zheng 		u32 object_size = le32_to_cpu(layout->fl_object_size);
791864e9197SYan, Zheng 		u32 object_base = off - objoff;
792ccca4e37SYan, Zheng 		if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
793d18d1e28SAlex Elder 			if (truncate_size <= object_base) {
794d18d1e28SAlex Elder 				truncate_size = 0;
795d18d1e28SAlex Elder 			} else {
796d18d1e28SAlex Elder 				truncate_size -= object_base;
797d18d1e28SAlex Elder 				if (truncate_size > object_size)
798d18d1e28SAlex Elder 					truncate_size = object_size;
799d18d1e28SAlex Elder 			}
800ccca4e37SYan, Zheng 		}
801d18d1e28SAlex Elder 
802c99d2d4aSAlex Elder 		osd_req_op_extent_init(req, 0, opcode, objoff, objlen,
803b0270324SAlex Elder 				       truncate_size, truncate_seq);
804864e9197SYan, Zheng 	}
805acead002SAlex Elder 	/*
806acead002SAlex Elder 	 * A second op in the ops array means the caller wants to
807acead002SAlex Elder 	 * also issue a include a 'startsync' command so that the
808acead002SAlex Elder 	 * osd will flush data quickly.
809acead002SAlex Elder 	 */
810acead002SAlex Elder 	if (num_ops > 1)
811c99d2d4aSAlex Elder 		osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC);
812d18d1e28SAlex Elder 
8133c972c95SIlya Dryomov 	req->r_base_oloc.pool = ceph_file_layout_pg_pool(*layout);
8143d14c5d2SYehuda Sadeh 
8153c972c95SIlya Dryomov 	snprintf(req->r_base_oid.name, sizeof(req->r_base_oid.name),
8164295f221SIlya Dryomov 		 "%llx.%08llx", vino.ino, objnum);
8173c972c95SIlya Dryomov 	req->r_base_oid.name_len = strlen(req->r_base_oid.name);
818dbe0fc41SAlex Elder 
8193d14c5d2SYehuda Sadeh 	return req;
8203d14c5d2SYehuda Sadeh }
8213d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_new_request);
8223d14c5d2SYehuda Sadeh 
8233d14c5d2SYehuda Sadeh /*
8243d14c5d2SYehuda Sadeh  * We keep osd requests in an rbtree, sorted by ->r_tid.
8253d14c5d2SYehuda Sadeh  */
8263d14c5d2SYehuda Sadeh static void __insert_request(struct ceph_osd_client *osdc,
8273d14c5d2SYehuda Sadeh 			     struct ceph_osd_request *new)
8283d14c5d2SYehuda Sadeh {
8293d14c5d2SYehuda Sadeh 	struct rb_node **p = &osdc->requests.rb_node;
8303d14c5d2SYehuda Sadeh 	struct rb_node *parent = NULL;
8313d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req = NULL;
8323d14c5d2SYehuda Sadeh 
8333d14c5d2SYehuda Sadeh 	while (*p) {
8343d14c5d2SYehuda Sadeh 		parent = *p;
8353d14c5d2SYehuda Sadeh 		req = rb_entry(parent, struct ceph_osd_request, r_node);
8363d14c5d2SYehuda Sadeh 		if (new->r_tid < req->r_tid)
8373d14c5d2SYehuda Sadeh 			p = &(*p)->rb_left;
8383d14c5d2SYehuda Sadeh 		else if (new->r_tid > req->r_tid)
8393d14c5d2SYehuda Sadeh 			p = &(*p)->rb_right;
8403d14c5d2SYehuda Sadeh 		else
8413d14c5d2SYehuda Sadeh 			BUG();
8423d14c5d2SYehuda Sadeh 	}
8433d14c5d2SYehuda Sadeh 
8443d14c5d2SYehuda Sadeh 	rb_link_node(&new->r_node, parent, p);
8453d14c5d2SYehuda Sadeh 	rb_insert_color(&new->r_node, &osdc->requests);
8463d14c5d2SYehuda Sadeh }
8473d14c5d2SYehuda Sadeh 
8483d14c5d2SYehuda Sadeh static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
8493d14c5d2SYehuda Sadeh 						 u64 tid)
8503d14c5d2SYehuda Sadeh {
8513d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
8523d14c5d2SYehuda Sadeh 	struct rb_node *n = osdc->requests.rb_node;
8533d14c5d2SYehuda Sadeh 
8543d14c5d2SYehuda Sadeh 	while (n) {
8553d14c5d2SYehuda Sadeh 		req = rb_entry(n, struct ceph_osd_request, r_node);
8563d14c5d2SYehuda Sadeh 		if (tid < req->r_tid)
8573d14c5d2SYehuda Sadeh 			n = n->rb_left;
8583d14c5d2SYehuda Sadeh 		else if (tid > req->r_tid)
8593d14c5d2SYehuda Sadeh 			n = n->rb_right;
8603d14c5d2SYehuda Sadeh 		else
8613d14c5d2SYehuda Sadeh 			return req;
8623d14c5d2SYehuda Sadeh 	}
8633d14c5d2SYehuda Sadeh 	return NULL;
8643d14c5d2SYehuda Sadeh }
8653d14c5d2SYehuda Sadeh 
8663d14c5d2SYehuda Sadeh static struct ceph_osd_request *
8673d14c5d2SYehuda Sadeh __lookup_request_ge(struct ceph_osd_client *osdc,
8683d14c5d2SYehuda Sadeh 		    u64 tid)
8693d14c5d2SYehuda Sadeh {
8703d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
8713d14c5d2SYehuda Sadeh 	struct rb_node *n = osdc->requests.rb_node;
8723d14c5d2SYehuda Sadeh 
8733d14c5d2SYehuda Sadeh 	while (n) {
8743d14c5d2SYehuda Sadeh 		req = rb_entry(n, struct ceph_osd_request, r_node);
8753d14c5d2SYehuda Sadeh 		if (tid < req->r_tid) {
8763d14c5d2SYehuda Sadeh 			if (!n->rb_left)
8773d14c5d2SYehuda Sadeh 				return req;
8783d14c5d2SYehuda Sadeh 			n = n->rb_left;
8793d14c5d2SYehuda Sadeh 		} else if (tid > req->r_tid) {
8803d14c5d2SYehuda Sadeh 			n = n->rb_right;
8813d14c5d2SYehuda Sadeh 		} else {
8823d14c5d2SYehuda Sadeh 			return req;
8833d14c5d2SYehuda Sadeh 		}
8843d14c5d2SYehuda Sadeh 	}
8853d14c5d2SYehuda Sadeh 	return NULL;
8863d14c5d2SYehuda Sadeh }
8873d14c5d2SYehuda Sadeh 
8882cc6128aSIlya Dryomov static void __kick_linger_request(struct ceph_osd_request *req)
8892cc6128aSIlya Dryomov {
8902cc6128aSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
8912cc6128aSIlya Dryomov 	struct ceph_osd *osd = req->r_osd;
8922cc6128aSIlya Dryomov 
8932cc6128aSIlya Dryomov 	/*
8942cc6128aSIlya Dryomov 	 * Linger requests need to be resent with a new tid to avoid
8952cc6128aSIlya Dryomov 	 * the dup op detection logic on the OSDs.  Achieve this with
8962cc6128aSIlya Dryomov 	 * a re-register dance instead of open-coding.
8972cc6128aSIlya Dryomov 	 */
8982cc6128aSIlya Dryomov 	ceph_osdc_get_request(req);
8992cc6128aSIlya Dryomov 	if (!list_empty(&req->r_linger_item))
9002cc6128aSIlya Dryomov 		__unregister_linger_request(osdc, req);
9012cc6128aSIlya Dryomov 	else
9022cc6128aSIlya Dryomov 		__unregister_request(osdc, req);
9032cc6128aSIlya Dryomov 	__register_request(osdc, req);
9042cc6128aSIlya Dryomov 	ceph_osdc_put_request(req);
9052cc6128aSIlya Dryomov 
9062cc6128aSIlya Dryomov 	/*
9072cc6128aSIlya Dryomov 	 * Unless request has been registered as both normal and
9082cc6128aSIlya Dryomov 	 * lingering, __unregister{,_linger}_request clears r_osd.
9092cc6128aSIlya Dryomov 	 * However, here we need to preserve r_osd to make sure we
9102cc6128aSIlya Dryomov 	 * requeue on the same OSD.
9112cc6128aSIlya Dryomov 	 */
9122cc6128aSIlya Dryomov 	WARN_ON(req->r_osd || !osd);
9132cc6128aSIlya Dryomov 	req->r_osd = osd;
9142cc6128aSIlya Dryomov 
9152cc6128aSIlya Dryomov 	dout("%s requeueing %p tid %llu\n", __func__, req, req->r_tid);
9162cc6128aSIlya Dryomov 	__enqueue_request(req);
9172cc6128aSIlya Dryomov }
9182cc6128aSIlya Dryomov 
9196f6c7006SSage Weil /*
9206f6c7006SSage Weil  * Resubmit requests pending on the given osd.
9216f6c7006SSage Weil  */
9226f6c7006SSage Weil static void __kick_osd_requests(struct ceph_osd_client *osdc,
9236f6c7006SSage Weil 				struct ceph_osd *osd)
9246f6c7006SSage Weil {
925a40c4f10SYehuda Sadeh 	struct ceph_osd_request *req, *nreq;
926e02493c0SAlex Elder 	LIST_HEAD(resend);
9272cc6128aSIlya Dryomov 	LIST_HEAD(resend_linger);
9286f6c7006SSage Weil 	int err;
9296f6c7006SSage Weil 
9302cc6128aSIlya Dryomov 	dout("%s osd%d\n", __func__, osd->o_osd);
9316f6c7006SSage Weil 	err = __reset_osd(osdc, osd);
932685a7555SAlex Elder 	if (err)
9336f6c7006SSage Weil 		return;
9342cc6128aSIlya Dryomov 
935e02493c0SAlex Elder 	/*
936e02493c0SAlex Elder 	 * Build up a list of requests to resend by traversing the
937e02493c0SAlex Elder 	 * osd's list of requests.  Requests for a given object are
938e02493c0SAlex Elder 	 * sent in tid order, and that is also the order they're
939e02493c0SAlex Elder 	 * kept on this list.  Therefore all requests that are in
940e02493c0SAlex Elder 	 * flight will be found first, followed by all requests that
941e02493c0SAlex Elder 	 * have not yet been sent.  And to resend requests while
942e02493c0SAlex Elder 	 * preserving this order we will want to put any sent
943e02493c0SAlex Elder 	 * requests back on the front of the osd client's unsent
944e02493c0SAlex Elder 	 * list.
945e02493c0SAlex Elder 	 *
946e02493c0SAlex Elder 	 * So we build a separate ordered list of already-sent
947e02493c0SAlex Elder 	 * requests for the affected osd and splice it onto the
948e02493c0SAlex Elder 	 * front of the osd client's unsent list.  Once we've seen a
949e02493c0SAlex Elder 	 * request that has not yet been sent we're done.  Those
950e02493c0SAlex Elder 	 * requests are already sitting right where they belong.
951e02493c0SAlex Elder 	 */
9526f6c7006SSage Weil 	list_for_each_entry(req, &osd->o_requests, r_osd_item) {
953e02493c0SAlex Elder 		if (!req->r_sent)
954e02493c0SAlex Elder 			break;
9552cc6128aSIlya Dryomov 
9562cc6128aSIlya Dryomov 		if (!req->r_linger) {
9572cc6128aSIlya Dryomov 			dout("%s requeueing %p tid %llu\n", __func__, req,
9582cc6128aSIlya Dryomov 			     req->r_tid);
959e02493c0SAlex Elder 			list_move_tail(&req->r_req_lru_item, &resend);
9606f6c7006SSage Weil 			req->r_flags |= CEPH_OSD_FLAG_RETRY;
9612cc6128aSIlya Dryomov 		} else {
9622cc6128aSIlya Dryomov 			list_move_tail(&req->r_req_lru_item, &resend_linger);
9632cc6128aSIlya Dryomov 		}
9646f6c7006SSage Weil 	}
965e02493c0SAlex Elder 	list_splice(&resend, &osdc->req_unsent);
966a40c4f10SYehuda Sadeh 
967e02493c0SAlex Elder 	/*
9682cc6128aSIlya Dryomov 	 * Both registered and not yet registered linger requests are
9692cc6128aSIlya Dryomov 	 * enqueued with a new tid on the same OSD.  We add/move them
9702cc6128aSIlya Dryomov 	 * to req_unsent/o_requests at the end to keep things in tid
9712cc6128aSIlya Dryomov 	 * order.
972e02493c0SAlex Elder 	 */
973a40c4f10SYehuda Sadeh 	list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
9741d0326b1SIlya Dryomov 				 r_linger_osd_item) {
9752cc6128aSIlya Dryomov 		WARN_ON(!list_empty(&req->r_req_lru_item));
9762cc6128aSIlya Dryomov 		__kick_linger_request(req);
977a40c4f10SYehuda Sadeh 	}
9782cc6128aSIlya Dryomov 
9792cc6128aSIlya Dryomov 	list_for_each_entry_safe(req, nreq, &resend_linger, r_req_lru_item)
9802cc6128aSIlya Dryomov 		__kick_linger_request(req);
9816f6c7006SSage Weil }
9826f6c7006SSage Weil 
9833d14c5d2SYehuda Sadeh /*
9843d14c5d2SYehuda Sadeh  * If the osd connection drops, we need to resubmit all requests.
9853d14c5d2SYehuda Sadeh  */
9863d14c5d2SYehuda Sadeh static void osd_reset(struct ceph_connection *con)
9873d14c5d2SYehuda Sadeh {
9883d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
9893d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc;
9903d14c5d2SYehuda Sadeh 
9913d14c5d2SYehuda Sadeh 	if (!osd)
9923d14c5d2SYehuda Sadeh 		return;
9933d14c5d2SYehuda Sadeh 	dout("osd_reset osd%d\n", osd->o_osd);
9943d14c5d2SYehuda Sadeh 	osdc = osd->o_osdc;
9953d14c5d2SYehuda Sadeh 	down_read(&osdc->map_sem);
99683aff95eSSage Weil 	mutex_lock(&osdc->request_mutex);
99783aff95eSSage Weil 	__kick_osd_requests(osdc, osd);
998f9d25199SAlex Elder 	__send_queued(osdc);
99983aff95eSSage Weil 	mutex_unlock(&osdc->request_mutex);
10003d14c5d2SYehuda Sadeh 	up_read(&osdc->map_sem);
10013d14c5d2SYehuda Sadeh }
10023d14c5d2SYehuda Sadeh 
10033d14c5d2SYehuda Sadeh /*
10043d14c5d2SYehuda Sadeh  * Track open sessions with osds.
10053d14c5d2SYehuda Sadeh  */
1006e10006f8SAlex Elder static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
10073d14c5d2SYehuda Sadeh {
10083d14c5d2SYehuda Sadeh 	struct ceph_osd *osd;
10093d14c5d2SYehuda Sadeh 
10103d14c5d2SYehuda Sadeh 	osd = kzalloc(sizeof(*osd), GFP_NOFS);
10113d14c5d2SYehuda Sadeh 	if (!osd)
10123d14c5d2SYehuda Sadeh 		return NULL;
10133d14c5d2SYehuda Sadeh 
10143d14c5d2SYehuda Sadeh 	atomic_set(&osd->o_ref, 1);
10153d14c5d2SYehuda Sadeh 	osd->o_osdc = osdc;
1016e10006f8SAlex Elder 	osd->o_osd = onum;
1017f407731dSAlex Elder 	RB_CLEAR_NODE(&osd->o_node);
10183d14c5d2SYehuda Sadeh 	INIT_LIST_HEAD(&osd->o_requests);
1019a40c4f10SYehuda Sadeh 	INIT_LIST_HEAD(&osd->o_linger_requests);
10203d14c5d2SYehuda Sadeh 	INIT_LIST_HEAD(&osd->o_osd_lru);
10213d14c5d2SYehuda Sadeh 	osd->o_incarnation = 1;
10223d14c5d2SYehuda Sadeh 
1023b7a9e5ddSSage Weil 	ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
10243d14c5d2SYehuda Sadeh 
10253d14c5d2SYehuda Sadeh 	INIT_LIST_HEAD(&osd->o_keepalive_item);
10263d14c5d2SYehuda Sadeh 	return osd;
10273d14c5d2SYehuda Sadeh }
10283d14c5d2SYehuda Sadeh 
10293d14c5d2SYehuda Sadeh static struct ceph_osd *get_osd(struct ceph_osd *osd)
10303d14c5d2SYehuda Sadeh {
10313d14c5d2SYehuda Sadeh 	if (atomic_inc_not_zero(&osd->o_ref)) {
10323d14c5d2SYehuda Sadeh 		dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
10333d14c5d2SYehuda Sadeh 		     atomic_read(&osd->o_ref));
10343d14c5d2SYehuda Sadeh 		return osd;
10353d14c5d2SYehuda Sadeh 	} else {
10363d14c5d2SYehuda Sadeh 		dout("get_osd %p FAIL\n", osd);
10373d14c5d2SYehuda Sadeh 		return NULL;
10383d14c5d2SYehuda Sadeh 	}
10393d14c5d2SYehuda Sadeh }
10403d14c5d2SYehuda Sadeh 
10413d14c5d2SYehuda Sadeh static void put_osd(struct ceph_osd *osd)
10423d14c5d2SYehuda Sadeh {
10433d14c5d2SYehuda Sadeh 	dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
10443d14c5d2SYehuda Sadeh 	     atomic_read(&osd->o_ref) - 1);
1045a255651dSAlex Elder 	if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
10463d14c5d2SYehuda Sadeh 		struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
10473d14c5d2SYehuda Sadeh 
104827859f97SSage Weil 		ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
10493d14c5d2SYehuda Sadeh 		kfree(osd);
10503d14c5d2SYehuda Sadeh 	}
10513d14c5d2SYehuda Sadeh }
10523d14c5d2SYehuda Sadeh 
10533d14c5d2SYehuda Sadeh /*
10543d14c5d2SYehuda Sadeh  * remove an osd from our map
10553d14c5d2SYehuda Sadeh  */
10563d14c5d2SYehuda Sadeh static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
10573d14c5d2SYehuda Sadeh {
10583d14c5d2SYehuda Sadeh 	dout("__remove_osd %p\n", osd);
1059cc9f1f51SIlya Dryomov 	WARN_ON(!list_empty(&osd->o_requests));
1060cc9f1f51SIlya Dryomov 	WARN_ON(!list_empty(&osd->o_linger_requests));
10617c6e6fc5SIlya Dryomov 
10623d14c5d2SYehuda Sadeh 	rb_erase(&osd->o_node, &osdc->osds);
10633d14c5d2SYehuda Sadeh 	list_del_init(&osd->o_osd_lru);
10643d14c5d2SYehuda Sadeh 	ceph_con_close(&osd->o_con);
10653d14c5d2SYehuda Sadeh 	put_osd(osd);
10663d14c5d2SYehuda Sadeh }
10673d14c5d2SYehuda Sadeh 
1068aca420bcSSage Weil static void remove_all_osds(struct ceph_osd_client *osdc)
1069aca420bcSSage Weil {
1070048a9d2dSJiaju Zhang 	dout("%s %p\n", __func__, osdc);
1071aca420bcSSage Weil 	mutex_lock(&osdc->request_mutex);
1072aca420bcSSage Weil 	while (!RB_EMPTY_ROOT(&osdc->osds)) {
1073aca420bcSSage Weil 		struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
1074aca420bcSSage Weil 						struct ceph_osd, o_node);
1075aca420bcSSage Weil 		__remove_osd(osdc, osd);
1076aca420bcSSage Weil 	}
1077aca420bcSSage Weil 	mutex_unlock(&osdc->request_mutex);
1078aca420bcSSage Weil }
1079aca420bcSSage Weil 
10803d14c5d2SYehuda Sadeh static void __move_osd_to_lru(struct ceph_osd_client *osdc,
10813d14c5d2SYehuda Sadeh 			      struct ceph_osd *osd)
10823d14c5d2SYehuda Sadeh {
1083bbf37ec3SIlya Dryomov 	dout("%s %p\n", __func__, osd);
10843d14c5d2SYehuda Sadeh 	BUG_ON(!list_empty(&osd->o_osd_lru));
1085bbf37ec3SIlya Dryomov 
10863d14c5d2SYehuda Sadeh 	list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
10873d14c5d2SYehuda Sadeh 	osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
10883d14c5d2SYehuda Sadeh }
10893d14c5d2SYehuda Sadeh 
1090bbf37ec3SIlya Dryomov static void maybe_move_osd_to_lru(struct ceph_osd_client *osdc,
1091bbf37ec3SIlya Dryomov 				  struct ceph_osd *osd)
1092bbf37ec3SIlya Dryomov {
1093bbf37ec3SIlya Dryomov 	dout("%s %p\n", __func__, osd);
1094bbf37ec3SIlya Dryomov 
1095bbf37ec3SIlya Dryomov 	if (list_empty(&osd->o_requests) &&
1096bbf37ec3SIlya Dryomov 	    list_empty(&osd->o_linger_requests))
1097bbf37ec3SIlya Dryomov 		__move_osd_to_lru(osdc, osd);
1098bbf37ec3SIlya Dryomov }
1099bbf37ec3SIlya Dryomov 
11003d14c5d2SYehuda Sadeh static void __remove_osd_from_lru(struct ceph_osd *osd)
11013d14c5d2SYehuda Sadeh {
11023d14c5d2SYehuda Sadeh 	dout("__remove_osd_from_lru %p\n", osd);
11033d14c5d2SYehuda Sadeh 	if (!list_empty(&osd->o_osd_lru))
11043d14c5d2SYehuda Sadeh 		list_del_init(&osd->o_osd_lru);
11053d14c5d2SYehuda Sadeh }
11063d14c5d2SYehuda Sadeh 
1107aca420bcSSage Weil static void remove_old_osds(struct ceph_osd_client *osdc)
11083d14c5d2SYehuda Sadeh {
11093d14c5d2SYehuda Sadeh 	struct ceph_osd *osd, *nosd;
11103d14c5d2SYehuda Sadeh 
11113d14c5d2SYehuda Sadeh 	dout("__remove_old_osds %p\n", osdc);
11123d14c5d2SYehuda Sadeh 	mutex_lock(&osdc->request_mutex);
11133d14c5d2SYehuda Sadeh 	list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
1114aca420bcSSage Weil 		if (time_before(jiffies, osd->lru_ttl))
11153d14c5d2SYehuda Sadeh 			break;
11163d14c5d2SYehuda Sadeh 		__remove_osd(osdc, osd);
11173d14c5d2SYehuda Sadeh 	}
11183d14c5d2SYehuda Sadeh 	mutex_unlock(&osdc->request_mutex);
11193d14c5d2SYehuda Sadeh }
11203d14c5d2SYehuda Sadeh 
11213d14c5d2SYehuda Sadeh /*
11223d14c5d2SYehuda Sadeh  * reset osd connect
11233d14c5d2SYehuda Sadeh  */
11243d14c5d2SYehuda Sadeh static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
11253d14c5d2SYehuda Sadeh {
1126c3acb181SAlex Elder 	struct ceph_entity_addr *peer_addr;
11273d14c5d2SYehuda Sadeh 
11283d14c5d2SYehuda Sadeh 	dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
1129a40c4f10SYehuda Sadeh 	if (list_empty(&osd->o_requests) &&
1130a40c4f10SYehuda Sadeh 	    list_empty(&osd->o_linger_requests)) {
11313d14c5d2SYehuda Sadeh 		__remove_osd(osdc, osd);
1132c3acb181SAlex Elder 
1133c3acb181SAlex Elder 		return -ENODEV;
1134c3acb181SAlex Elder 	}
1135c3acb181SAlex Elder 
1136c3acb181SAlex Elder 	peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1137c3acb181SAlex Elder 	if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
11383d14c5d2SYehuda Sadeh 			!ceph_con_opened(&osd->o_con)) {
1139c3acb181SAlex Elder 		struct ceph_osd_request *req;
1140c3acb181SAlex Elder 
11413d14c5d2SYehuda Sadeh 		dout("osd addr hasn't changed and connection never opened, "
11420b4af2e8SIlya Dryomov 		     "letting msgr retry\n");
11433d14c5d2SYehuda Sadeh 		/* touch each r_stamp for handle_timeout()'s benfit */
11443d14c5d2SYehuda Sadeh 		list_for_each_entry(req, &osd->o_requests, r_osd_item)
11453d14c5d2SYehuda Sadeh 			req->r_stamp = jiffies;
1146c3acb181SAlex Elder 
1147c3acb181SAlex Elder 		return -EAGAIN;
11483d14c5d2SYehuda Sadeh 	}
1149c3acb181SAlex Elder 
1150c3acb181SAlex Elder 	ceph_con_close(&osd->o_con);
1151c3acb181SAlex Elder 	ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1152c3acb181SAlex Elder 	osd->o_incarnation++;
1153c3acb181SAlex Elder 
1154c3acb181SAlex Elder 	return 0;
11553d14c5d2SYehuda Sadeh }
11563d14c5d2SYehuda Sadeh 
11573d14c5d2SYehuda Sadeh static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
11583d14c5d2SYehuda Sadeh {
11593d14c5d2SYehuda Sadeh 	struct rb_node **p = &osdc->osds.rb_node;
11603d14c5d2SYehuda Sadeh 	struct rb_node *parent = NULL;
11613d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = NULL;
11623d14c5d2SYehuda Sadeh 
1163aca420bcSSage Weil 	dout("__insert_osd %p osd%d\n", new, new->o_osd);
11643d14c5d2SYehuda Sadeh 	while (*p) {
11653d14c5d2SYehuda Sadeh 		parent = *p;
11663d14c5d2SYehuda Sadeh 		osd = rb_entry(parent, struct ceph_osd, o_node);
11673d14c5d2SYehuda Sadeh 		if (new->o_osd < osd->o_osd)
11683d14c5d2SYehuda Sadeh 			p = &(*p)->rb_left;
11693d14c5d2SYehuda Sadeh 		else if (new->o_osd > osd->o_osd)
11703d14c5d2SYehuda Sadeh 			p = &(*p)->rb_right;
11713d14c5d2SYehuda Sadeh 		else
11723d14c5d2SYehuda Sadeh 			BUG();
11733d14c5d2SYehuda Sadeh 	}
11743d14c5d2SYehuda Sadeh 
11753d14c5d2SYehuda Sadeh 	rb_link_node(&new->o_node, parent, p);
11763d14c5d2SYehuda Sadeh 	rb_insert_color(&new->o_node, &osdc->osds);
11773d14c5d2SYehuda Sadeh }
11783d14c5d2SYehuda Sadeh 
11793d14c5d2SYehuda Sadeh static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
11803d14c5d2SYehuda Sadeh {
11813d14c5d2SYehuda Sadeh 	struct ceph_osd *osd;
11823d14c5d2SYehuda Sadeh 	struct rb_node *n = osdc->osds.rb_node;
11833d14c5d2SYehuda Sadeh 
11843d14c5d2SYehuda Sadeh 	while (n) {
11853d14c5d2SYehuda Sadeh 		osd = rb_entry(n, struct ceph_osd, o_node);
11863d14c5d2SYehuda Sadeh 		if (o < osd->o_osd)
11873d14c5d2SYehuda Sadeh 			n = n->rb_left;
11883d14c5d2SYehuda Sadeh 		else if (o > osd->o_osd)
11893d14c5d2SYehuda Sadeh 			n = n->rb_right;
11903d14c5d2SYehuda Sadeh 		else
11913d14c5d2SYehuda Sadeh 			return osd;
11923d14c5d2SYehuda Sadeh 	}
11933d14c5d2SYehuda Sadeh 	return NULL;
11943d14c5d2SYehuda Sadeh }
11953d14c5d2SYehuda Sadeh 
11963d14c5d2SYehuda Sadeh static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
11973d14c5d2SYehuda Sadeh {
11983d14c5d2SYehuda Sadeh 	schedule_delayed_work(&osdc->timeout_work,
11993d14c5d2SYehuda Sadeh 			osdc->client->options->osd_keepalive_timeout * HZ);
12003d14c5d2SYehuda Sadeh }
12013d14c5d2SYehuda Sadeh 
12023d14c5d2SYehuda Sadeh static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
12033d14c5d2SYehuda Sadeh {
12043d14c5d2SYehuda Sadeh 	cancel_delayed_work(&osdc->timeout_work);
12053d14c5d2SYehuda Sadeh }
12063d14c5d2SYehuda Sadeh 
12073d14c5d2SYehuda Sadeh /*
12083d14c5d2SYehuda Sadeh  * Register request, assign tid.  If this is the first request, set up
12093d14c5d2SYehuda Sadeh  * the timeout event.
12103d14c5d2SYehuda Sadeh  */
1211a40c4f10SYehuda Sadeh static void __register_request(struct ceph_osd_client *osdc,
12123d14c5d2SYehuda Sadeh 			       struct ceph_osd_request *req)
12133d14c5d2SYehuda Sadeh {
12143d14c5d2SYehuda Sadeh 	req->r_tid = ++osdc->last_tid;
12153d14c5d2SYehuda Sadeh 	req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
121677f38e0eSSage Weil 	dout("__register_request %p tid %lld\n", req, req->r_tid);
12173d14c5d2SYehuda Sadeh 	__insert_request(osdc, req);
12183d14c5d2SYehuda Sadeh 	ceph_osdc_get_request(req);
12193d14c5d2SYehuda Sadeh 	osdc->num_requests++;
12203d14c5d2SYehuda Sadeh 	if (osdc->num_requests == 1) {
12213d14c5d2SYehuda Sadeh 		dout(" first request, scheduling timeout\n");
12223d14c5d2SYehuda Sadeh 		__schedule_osd_timeout(osdc);
12233d14c5d2SYehuda Sadeh 	}
1224a40c4f10SYehuda Sadeh }
1225a40c4f10SYehuda Sadeh 
12263d14c5d2SYehuda Sadeh /*
12273d14c5d2SYehuda Sadeh  * called under osdc->request_mutex
12283d14c5d2SYehuda Sadeh  */
12293d14c5d2SYehuda Sadeh static void __unregister_request(struct ceph_osd_client *osdc,
12303d14c5d2SYehuda Sadeh 				 struct ceph_osd_request *req)
12313d14c5d2SYehuda Sadeh {
123235f9f8a0SSage Weil 	if (RB_EMPTY_NODE(&req->r_node)) {
123335f9f8a0SSage Weil 		dout("__unregister_request %p tid %lld not registered\n",
123435f9f8a0SSage Weil 			req, req->r_tid);
123535f9f8a0SSage Weil 		return;
123635f9f8a0SSage Weil 	}
123735f9f8a0SSage Weil 
12383d14c5d2SYehuda Sadeh 	dout("__unregister_request %p tid %lld\n", req, req->r_tid);
12393d14c5d2SYehuda Sadeh 	rb_erase(&req->r_node, &osdc->requests);
12406562d661SIlya Dryomov 	RB_CLEAR_NODE(&req->r_node);
12413d14c5d2SYehuda Sadeh 	osdc->num_requests--;
12423d14c5d2SYehuda Sadeh 
12433d14c5d2SYehuda Sadeh 	if (req->r_osd) {
12443d14c5d2SYehuda Sadeh 		/* make sure the original request isn't in flight. */
12456740a845SAlex Elder 		ceph_msg_revoke(req->r_request);
12463d14c5d2SYehuda Sadeh 
12473d14c5d2SYehuda Sadeh 		list_del_init(&req->r_osd_item);
1248bbf37ec3SIlya Dryomov 		maybe_move_osd_to_lru(osdc, req->r_osd);
12494f23409eSIlya Dryomov 		if (list_empty(&req->r_linger_osd_item))
12503d14c5d2SYehuda Sadeh 			req->r_osd = NULL;
12513d14c5d2SYehuda Sadeh 	}
12523d14c5d2SYehuda Sadeh 
12537d5f2481SAlex Elder 	list_del_init(&req->r_req_lru_item);
12543d14c5d2SYehuda Sadeh 	ceph_osdc_put_request(req);
12553d14c5d2SYehuda Sadeh 
12563d14c5d2SYehuda Sadeh 	if (osdc->num_requests == 0) {
12573d14c5d2SYehuda Sadeh 		dout(" no requests, canceling timeout\n");
12583d14c5d2SYehuda Sadeh 		__cancel_osd_timeout(osdc);
12593d14c5d2SYehuda Sadeh 	}
12603d14c5d2SYehuda Sadeh }
12613d14c5d2SYehuda Sadeh 
12623d14c5d2SYehuda Sadeh /*
12633d14c5d2SYehuda Sadeh  * Cancel a previously queued request message
12643d14c5d2SYehuda Sadeh  */
12653d14c5d2SYehuda Sadeh static void __cancel_request(struct ceph_osd_request *req)
12663d14c5d2SYehuda Sadeh {
12673d14c5d2SYehuda Sadeh 	if (req->r_sent && req->r_osd) {
12686740a845SAlex Elder 		ceph_msg_revoke(req->r_request);
12693d14c5d2SYehuda Sadeh 		req->r_sent = 0;
12703d14c5d2SYehuda Sadeh 	}
12713d14c5d2SYehuda Sadeh }
12723d14c5d2SYehuda Sadeh 
1273a40c4f10SYehuda Sadeh static void __register_linger_request(struct ceph_osd_client *osdc,
1274a40c4f10SYehuda Sadeh 				    struct ceph_osd_request *req)
1275a40c4f10SYehuda Sadeh {
1276af593064SIlya Dryomov 	dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1277af593064SIlya Dryomov 	WARN_ON(!req->r_linger);
1278af593064SIlya Dryomov 
127996e4dac6SAlex Elder 	ceph_osdc_get_request(req);
1280a40c4f10SYehuda Sadeh 	list_add_tail(&req->r_linger_item, &osdc->req_linger);
12816194ea89SSage Weil 	if (req->r_osd)
12821d0326b1SIlya Dryomov 		list_add_tail(&req->r_linger_osd_item,
12836194ea89SSage Weil 			      &req->r_osd->o_linger_requests);
1284a40c4f10SYehuda Sadeh }
1285a40c4f10SYehuda Sadeh 
1286a40c4f10SYehuda Sadeh static void __unregister_linger_request(struct ceph_osd_client *osdc,
1287a40c4f10SYehuda Sadeh 					struct ceph_osd_request *req)
1288a40c4f10SYehuda Sadeh {
1289af593064SIlya Dryomov 	WARN_ON(!req->r_linger);
1290af593064SIlya Dryomov 
1291af593064SIlya Dryomov 	if (list_empty(&req->r_linger_item)) {
1292af593064SIlya Dryomov 		dout("%s %p tid %llu not registered\n", __func__, req,
1293af593064SIlya Dryomov 		     req->r_tid);
1294af593064SIlya Dryomov 		return;
1295af593064SIlya Dryomov 	}
1296af593064SIlya Dryomov 
1297af593064SIlya Dryomov 	dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1298a40c4f10SYehuda Sadeh 	list_del_init(&req->r_linger_item);
1299af593064SIlya Dryomov 
130061c74035SAlex Elder 	if (req->r_osd) {
13011d0326b1SIlya Dryomov 		list_del_init(&req->r_linger_osd_item);
1302bbf37ec3SIlya Dryomov 		maybe_move_osd_to_lru(osdc, req->r_osd);
1303fbdb9190SSage Weil 		if (list_empty(&req->r_osd_item))
1304a40c4f10SYehuda Sadeh 			req->r_osd = NULL;
1305a40c4f10SYehuda Sadeh 	}
1306ba9d114eSIlya Dryomov 
1307ba9d114eSIlya Dryomov 	list_del_init(&req->r_req_lru_item); /* can be on notarget */
130896e4dac6SAlex Elder 	ceph_osdc_put_request(req);
1309a40c4f10SYehuda Sadeh }
1310a40c4f10SYehuda Sadeh 
1311a40c4f10SYehuda Sadeh void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1312a40c4f10SYehuda Sadeh 				  struct ceph_osd_request *req)
1313a40c4f10SYehuda Sadeh {
1314a40c4f10SYehuda Sadeh 	if (!req->r_linger) {
1315a40c4f10SYehuda Sadeh 		dout("set_request_linger %p\n", req);
1316a40c4f10SYehuda Sadeh 		req->r_linger = 1;
1317a40c4f10SYehuda Sadeh 	}
1318a40c4f10SYehuda Sadeh }
1319a40c4f10SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1320a40c4f10SYehuda Sadeh 
13213d14c5d2SYehuda Sadeh /*
1322d29adb34SJosh Durgin  * Returns whether a request should be blocked from being sent
1323d29adb34SJosh Durgin  * based on the current osdmap and osd_client settings.
1324d29adb34SJosh Durgin  *
1325d29adb34SJosh Durgin  * Caller should hold map_sem for read.
1326d29adb34SJosh Durgin  */
1327d29adb34SJosh Durgin static bool __req_should_be_paused(struct ceph_osd_client *osdc,
1328d29adb34SJosh Durgin 				   struct ceph_osd_request *req)
1329d29adb34SJosh Durgin {
1330d29adb34SJosh Durgin 	bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
1331d29adb34SJosh Durgin 	bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
1332d29adb34SJosh Durgin 		ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
1333d29adb34SJosh Durgin 	return (req->r_flags & CEPH_OSD_FLAG_READ && pauserd) ||
1334d29adb34SJosh Durgin 		(req->r_flags & CEPH_OSD_FLAG_WRITE && pausewr);
1335d29adb34SJosh Durgin }
1336d29adb34SJosh Durgin 
1337d29adb34SJosh Durgin /*
133817a13e40SIlya Dryomov  * Calculate mapping of a request to a PG.  Takes tiering into account.
133917a13e40SIlya Dryomov  */
134017a13e40SIlya Dryomov static int __calc_request_pg(struct ceph_osdmap *osdmap,
134117a13e40SIlya Dryomov 			     struct ceph_osd_request *req,
134217a13e40SIlya Dryomov 			     struct ceph_pg *pg_out)
134317a13e40SIlya Dryomov {
1344205ee118SIlya Dryomov 	bool need_check_tiering;
1345205ee118SIlya Dryomov 
1346205ee118SIlya Dryomov 	need_check_tiering = false;
1347205ee118SIlya Dryomov 	if (req->r_target_oloc.pool == -1) {
1348205ee118SIlya Dryomov 		req->r_target_oloc = req->r_base_oloc; /* struct */
1349205ee118SIlya Dryomov 		need_check_tiering = true;
1350205ee118SIlya Dryomov 	}
1351205ee118SIlya Dryomov 	if (req->r_target_oid.name_len == 0) {
1352205ee118SIlya Dryomov 		ceph_oid_copy(&req->r_target_oid, &req->r_base_oid);
1353205ee118SIlya Dryomov 		need_check_tiering = true;
1354205ee118SIlya Dryomov 	}
1355205ee118SIlya Dryomov 
1356205ee118SIlya Dryomov 	if (need_check_tiering &&
1357205ee118SIlya Dryomov 	    (req->r_flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
135817a13e40SIlya Dryomov 		struct ceph_pg_pool_info *pi;
135917a13e40SIlya Dryomov 
1360205ee118SIlya Dryomov 		pi = ceph_pg_pool_by_id(osdmap, req->r_target_oloc.pool);
136117a13e40SIlya Dryomov 		if (pi) {
136217a13e40SIlya Dryomov 			if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
136317a13e40SIlya Dryomov 			    pi->read_tier >= 0)
1364205ee118SIlya Dryomov 				req->r_target_oloc.pool = pi->read_tier;
136517a13e40SIlya Dryomov 			if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
136617a13e40SIlya Dryomov 			    pi->write_tier >= 0)
1367205ee118SIlya Dryomov 				req->r_target_oloc.pool = pi->write_tier;
136817a13e40SIlya Dryomov 		}
136917a13e40SIlya Dryomov 		/* !pi is caught in ceph_oloc_oid_to_pg() */
137017a13e40SIlya Dryomov 	}
137117a13e40SIlya Dryomov 
1372205ee118SIlya Dryomov 	return ceph_oloc_oid_to_pg(osdmap, &req->r_target_oloc,
1373205ee118SIlya Dryomov 				   &req->r_target_oid, pg_out);
137417a13e40SIlya Dryomov }
137517a13e40SIlya Dryomov 
1376f671b581SIlya Dryomov static void __enqueue_request(struct ceph_osd_request *req)
1377f671b581SIlya Dryomov {
1378f671b581SIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
1379f671b581SIlya Dryomov 
1380f671b581SIlya Dryomov 	dout("%s %p tid %llu to osd%d\n", __func__, req, req->r_tid,
1381f671b581SIlya Dryomov 	     req->r_osd ? req->r_osd->o_osd : -1);
1382f671b581SIlya Dryomov 
1383f671b581SIlya Dryomov 	if (req->r_osd) {
1384f671b581SIlya Dryomov 		__remove_osd_from_lru(req->r_osd);
1385f671b581SIlya Dryomov 		list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1386f671b581SIlya Dryomov 		list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1387f671b581SIlya Dryomov 	} else {
1388f671b581SIlya Dryomov 		list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1389f671b581SIlya Dryomov 	}
1390f671b581SIlya Dryomov }
1391f671b581SIlya Dryomov 
139217a13e40SIlya Dryomov /*
13933d14c5d2SYehuda Sadeh  * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
13943d14c5d2SYehuda Sadeh  * (as needed), and set the request r_osd appropriately.  If there is
139525985edcSLucas De Marchi  * no up osd, set r_osd to NULL.  Move the request to the appropriate list
13966f6c7006SSage Weil  * (unsent, homeless) or leave on in-flight lru.
13973d14c5d2SYehuda Sadeh  *
13983d14c5d2SYehuda Sadeh  * Return 0 if unchanged, 1 if changed, or negative on error.
13993d14c5d2SYehuda Sadeh  *
14003d14c5d2SYehuda Sadeh  * Caller should hold map_sem for read and request_mutex.
14013d14c5d2SYehuda Sadeh  */
14026f6c7006SSage Weil static int __map_request(struct ceph_osd_client *osdc,
140338d6453cSSage Weil 			 struct ceph_osd_request *req, int force_resend)
14043d14c5d2SYehuda Sadeh {
14055b191d99SSage Weil 	struct ceph_pg pgid;
14063d14c5d2SYehuda Sadeh 	int acting[CEPH_PG_MAX_SIZE];
14078008ab10SIlya Dryomov 	int num, o;
14083d14c5d2SYehuda Sadeh 	int err;
1409d29adb34SJosh Durgin 	bool was_paused;
14103d14c5d2SYehuda Sadeh 
14116f6c7006SSage Weil 	dout("map_request %p tid %lld\n", req, req->r_tid);
141217a13e40SIlya Dryomov 
141317a13e40SIlya Dryomov 	err = __calc_request_pg(osdc->osdmap, req, &pgid);
14146f6c7006SSage Weil 	if (err) {
14156f6c7006SSage Weil 		list_move(&req->r_req_lru_item, &osdc->req_notarget);
14163d14c5d2SYehuda Sadeh 		return err;
14176f6c7006SSage Weil 	}
14183d14c5d2SYehuda Sadeh 	req->r_pgid = pgid;
14193d14c5d2SYehuda Sadeh 
14208008ab10SIlya Dryomov 	num = ceph_calc_pg_acting(osdc->osdmap, pgid, acting, &o);
14218008ab10SIlya Dryomov 	if (num < 0)
14228008ab10SIlya Dryomov 		num = 0;
14233d14c5d2SYehuda Sadeh 
1424d29adb34SJosh Durgin 	was_paused = req->r_paused;
1425d29adb34SJosh Durgin 	req->r_paused = __req_should_be_paused(osdc, req);
1426d29adb34SJosh Durgin 	if (was_paused && !req->r_paused)
1427d29adb34SJosh Durgin 		force_resend = 1;
1428d29adb34SJosh Durgin 
142938d6453cSSage Weil 	if ((!force_resend &&
143038d6453cSSage Weil 	     req->r_osd && req->r_osd->o_osd == o &&
14313d14c5d2SYehuda Sadeh 	     req->r_sent >= req->r_osd->o_incarnation &&
14323d14c5d2SYehuda Sadeh 	     req->r_num_pg_osds == num &&
14333d14c5d2SYehuda Sadeh 	     memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
1434d29adb34SJosh Durgin 	    (req->r_osd == NULL && o == -1) ||
1435d29adb34SJosh Durgin 	    req->r_paused)
14363d14c5d2SYehuda Sadeh 		return 0;  /* no change */
14373d14c5d2SYehuda Sadeh 
14385b191d99SSage Weil 	dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
14395b191d99SSage Weil 	     req->r_tid, pgid.pool, pgid.seed, o,
14403d14c5d2SYehuda Sadeh 	     req->r_osd ? req->r_osd->o_osd : -1);
14413d14c5d2SYehuda Sadeh 
14423d14c5d2SYehuda Sadeh 	/* record full pg acting set */
14433d14c5d2SYehuda Sadeh 	memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
14443d14c5d2SYehuda Sadeh 	req->r_num_pg_osds = num;
14453d14c5d2SYehuda Sadeh 
14463d14c5d2SYehuda Sadeh 	if (req->r_osd) {
14473d14c5d2SYehuda Sadeh 		__cancel_request(req);
14483d14c5d2SYehuda Sadeh 		list_del_init(&req->r_osd_item);
1449a390de02SIlya Dryomov 		list_del_init(&req->r_linger_osd_item);
14503d14c5d2SYehuda Sadeh 		req->r_osd = NULL;
14513d14c5d2SYehuda Sadeh 	}
14523d14c5d2SYehuda Sadeh 
14533d14c5d2SYehuda Sadeh 	req->r_osd = __lookup_osd(osdc, o);
14543d14c5d2SYehuda Sadeh 	if (!req->r_osd && o >= 0) {
14553d14c5d2SYehuda Sadeh 		err = -ENOMEM;
1456e10006f8SAlex Elder 		req->r_osd = create_osd(osdc, o);
14576f6c7006SSage Weil 		if (!req->r_osd) {
14586f6c7006SSage Weil 			list_move(&req->r_req_lru_item, &osdc->req_notarget);
14593d14c5d2SYehuda Sadeh 			goto out;
14606f6c7006SSage Weil 		}
14613d14c5d2SYehuda Sadeh 
14626f6c7006SSage Weil 		dout("map_request osd %p is osd%d\n", req->r_osd, o);
14633d14c5d2SYehuda Sadeh 		__insert_osd(osdc, req->r_osd);
14643d14c5d2SYehuda Sadeh 
1465b7a9e5ddSSage Weil 		ceph_con_open(&req->r_osd->o_con,
1466b7a9e5ddSSage Weil 			      CEPH_ENTITY_TYPE_OSD, o,
1467b7a9e5ddSSage Weil 			      &osdc->osdmap->osd_addr[o]);
14683d14c5d2SYehuda Sadeh 	}
14693d14c5d2SYehuda Sadeh 
1470f671b581SIlya Dryomov 	__enqueue_request(req);
14713d14c5d2SYehuda Sadeh 	err = 1;   /* osd or pg changed */
14723d14c5d2SYehuda Sadeh 
14733d14c5d2SYehuda Sadeh out:
14743d14c5d2SYehuda Sadeh 	return err;
14753d14c5d2SYehuda Sadeh }
14763d14c5d2SYehuda Sadeh 
14773d14c5d2SYehuda Sadeh /*
14783d14c5d2SYehuda Sadeh  * caller should hold map_sem (for read) and request_mutex
14793d14c5d2SYehuda Sadeh  */
148056e925b6SSage Weil static void __send_request(struct ceph_osd_client *osdc,
14813d14c5d2SYehuda Sadeh 			   struct ceph_osd_request *req)
14823d14c5d2SYehuda Sadeh {
14831b83bef2SSage Weil 	void *p;
14843d14c5d2SYehuda Sadeh 
14851b83bef2SSage Weil 	dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
14861b83bef2SSage Weil 	     req, req->r_tid, req->r_osd->o_osd, req->r_flags,
14871b83bef2SSage Weil 	     (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
14883d14c5d2SYehuda Sadeh 
14891b83bef2SSage Weil 	/* fill in message content that changes each time we send it */
14901b83bef2SSage Weil 	put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
14911b83bef2SSage Weil 	put_unaligned_le32(req->r_flags, req->r_request_flags);
1492205ee118SIlya Dryomov 	put_unaligned_le64(req->r_target_oloc.pool, req->r_request_pool);
14931b83bef2SSage Weil 	p = req->r_request_pgid;
14941b83bef2SSage Weil 	ceph_encode_64(&p, req->r_pgid.pool);
14951b83bef2SSage Weil 	ceph_encode_32(&p, req->r_pgid.seed);
14961b83bef2SSage Weil 	put_unaligned_le64(1, req->r_request_attempts);  /* FIXME */
14971b83bef2SSage Weil 	memcpy(req->r_request_reassert_version, &req->r_reassert_version,
14981b83bef2SSage Weil 	       sizeof(req->r_reassert_version));
14992169aea6SSage Weil 
15003d14c5d2SYehuda Sadeh 	req->r_stamp = jiffies;
15013d14c5d2SYehuda Sadeh 	list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
15023d14c5d2SYehuda Sadeh 
15033d14c5d2SYehuda Sadeh 	ceph_msg_get(req->r_request); /* send consumes a ref */
150426be8808SAlex Elder 
15053d14c5d2SYehuda Sadeh 	req->r_sent = req->r_osd->o_incarnation;
150626be8808SAlex Elder 
150726be8808SAlex Elder 	ceph_con_send(&req->r_osd->o_con, req->r_request);
15083d14c5d2SYehuda Sadeh }
15093d14c5d2SYehuda Sadeh 
15103d14c5d2SYehuda Sadeh /*
15116f6c7006SSage Weil  * Send any requests in the queue (req_unsent).
15126f6c7006SSage Weil  */
1513f9d25199SAlex Elder static void __send_queued(struct ceph_osd_client *osdc)
15146f6c7006SSage Weil {
15156f6c7006SSage Weil 	struct ceph_osd_request *req, *tmp;
15166f6c7006SSage Weil 
1517f9d25199SAlex Elder 	dout("__send_queued\n");
1518f9d25199SAlex Elder 	list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
15196f6c7006SSage Weil 		__send_request(osdc, req);
15206f6c7006SSage Weil }
15216f6c7006SSage Weil 
15226f6c7006SSage Weil /*
15230bbfdfe8SIlya Dryomov  * Caller should hold map_sem for read and request_mutex.
15240bbfdfe8SIlya Dryomov  */
15250bbfdfe8SIlya Dryomov static int __ceph_osdc_start_request(struct ceph_osd_client *osdc,
15260bbfdfe8SIlya Dryomov 				     struct ceph_osd_request *req,
15270bbfdfe8SIlya Dryomov 				     bool nofail)
15280bbfdfe8SIlya Dryomov {
15290bbfdfe8SIlya Dryomov 	int rc;
15300bbfdfe8SIlya Dryomov 
15310bbfdfe8SIlya Dryomov 	__register_request(osdc, req);
15320bbfdfe8SIlya Dryomov 	req->r_sent = 0;
15330bbfdfe8SIlya Dryomov 	req->r_got_reply = 0;
15340bbfdfe8SIlya Dryomov 	rc = __map_request(osdc, req, 0);
15350bbfdfe8SIlya Dryomov 	if (rc < 0) {
15360bbfdfe8SIlya Dryomov 		if (nofail) {
15370bbfdfe8SIlya Dryomov 			dout("osdc_start_request failed map, "
15380bbfdfe8SIlya Dryomov 				" will retry %lld\n", req->r_tid);
15390bbfdfe8SIlya Dryomov 			rc = 0;
15400bbfdfe8SIlya Dryomov 		} else {
15410bbfdfe8SIlya Dryomov 			__unregister_request(osdc, req);
15420bbfdfe8SIlya Dryomov 		}
15430bbfdfe8SIlya Dryomov 		return rc;
15440bbfdfe8SIlya Dryomov 	}
15450bbfdfe8SIlya Dryomov 
15460bbfdfe8SIlya Dryomov 	if (req->r_osd == NULL) {
15470bbfdfe8SIlya Dryomov 		dout("send_request %p no up osds in pg\n", req);
15480bbfdfe8SIlya Dryomov 		ceph_monc_request_next_osdmap(&osdc->client->monc);
15490bbfdfe8SIlya Dryomov 	} else {
15500bbfdfe8SIlya Dryomov 		__send_queued(osdc);
15510bbfdfe8SIlya Dryomov 	}
15520bbfdfe8SIlya Dryomov 
15530bbfdfe8SIlya Dryomov 	return 0;
15540bbfdfe8SIlya Dryomov }
15550bbfdfe8SIlya Dryomov 
15560bbfdfe8SIlya Dryomov /*
15573d14c5d2SYehuda Sadeh  * Timeout callback, called every N seconds when 1 or more osd
15583d14c5d2SYehuda Sadeh  * requests has been active for more than N seconds.  When this
15593d14c5d2SYehuda Sadeh  * happens, we ping all OSDs with requests who have timed out to
15603d14c5d2SYehuda Sadeh  * ensure any communications channel reset is detected.  Reset the
15613d14c5d2SYehuda Sadeh  * request timeouts another N seconds in the future as we go.
15623d14c5d2SYehuda Sadeh  * Reschedule the timeout event another N seconds in future (unless
15633d14c5d2SYehuda Sadeh  * there are no open requests).
15643d14c5d2SYehuda Sadeh  */
15653d14c5d2SYehuda Sadeh static void handle_timeout(struct work_struct *work)
15663d14c5d2SYehuda Sadeh {
15673d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc =
15683d14c5d2SYehuda Sadeh 		container_of(work, struct ceph_osd_client, timeout_work.work);
156983aff95eSSage Weil 	struct ceph_osd_request *req;
15703d14c5d2SYehuda Sadeh 	struct ceph_osd *osd;
15713d14c5d2SYehuda Sadeh 	unsigned long keepalive =
15723d14c5d2SYehuda Sadeh 		osdc->client->options->osd_keepalive_timeout * HZ;
15733d14c5d2SYehuda Sadeh 	struct list_head slow_osds;
15743d14c5d2SYehuda Sadeh 	dout("timeout\n");
15753d14c5d2SYehuda Sadeh 	down_read(&osdc->map_sem);
15763d14c5d2SYehuda Sadeh 
15773d14c5d2SYehuda Sadeh 	ceph_monc_request_next_osdmap(&osdc->client->monc);
15783d14c5d2SYehuda Sadeh 
15793d14c5d2SYehuda Sadeh 	mutex_lock(&osdc->request_mutex);
15803d14c5d2SYehuda Sadeh 
15813d14c5d2SYehuda Sadeh 	/*
15823d14c5d2SYehuda Sadeh 	 * ping osds that are a bit slow.  this ensures that if there
15833d14c5d2SYehuda Sadeh 	 * is a break in the TCP connection we will notice, and reopen
15843d14c5d2SYehuda Sadeh 	 * a connection with that osd (from the fault callback).
15853d14c5d2SYehuda Sadeh 	 */
15863d14c5d2SYehuda Sadeh 	INIT_LIST_HEAD(&slow_osds);
15873d14c5d2SYehuda Sadeh 	list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
15883d14c5d2SYehuda Sadeh 		if (time_before(jiffies, req->r_stamp + keepalive))
15893d14c5d2SYehuda Sadeh 			break;
15903d14c5d2SYehuda Sadeh 
15913d14c5d2SYehuda Sadeh 		osd = req->r_osd;
15923d14c5d2SYehuda Sadeh 		BUG_ON(!osd);
15933d14c5d2SYehuda Sadeh 		dout(" tid %llu is slow, will send keepalive on osd%d\n",
15943d14c5d2SYehuda Sadeh 		     req->r_tid, osd->o_osd);
15953d14c5d2SYehuda Sadeh 		list_move_tail(&osd->o_keepalive_item, &slow_osds);
15963d14c5d2SYehuda Sadeh 	}
15973d14c5d2SYehuda Sadeh 	while (!list_empty(&slow_osds)) {
15983d14c5d2SYehuda Sadeh 		osd = list_entry(slow_osds.next, struct ceph_osd,
15993d14c5d2SYehuda Sadeh 				 o_keepalive_item);
16003d14c5d2SYehuda Sadeh 		list_del_init(&osd->o_keepalive_item);
16013d14c5d2SYehuda Sadeh 		ceph_con_keepalive(&osd->o_con);
16023d14c5d2SYehuda Sadeh 	}
16033d14c5d2SYehuda Sadeh 
16043d14c5d2SYehuda Sadeh 	__schedule_osd_timeout(osdc);
1605f9d25199SAlex Elder 	__send_queued(osdc);
16063d14c5d2SYehuda Sadeh 	mutex_unlock(&osdc->request_mutex);
16073d14c5d2SYehuda Sadeh 	up_read(&osdc->map_sem);
16083d14c5d2SYehuda Sadeh }
16093d14c5d2SYehuda Sadeh 
16103d14c5d2SYehuda Sadeh static void handle_osds_timeout(struct work_struct *work)
16113d14c5d2SYehuda Sadeh {
16123d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc =
16133d14c5d2SYehuda Sadeh 		container_of(work, struct ceph_osd_client,
16143d14c5d2SYehuda Sadeh 			     osds_timeout_work.work);
16153d14c5d2SYehuda Sadeh 	unsigned long delay =
16163d14c5d2SYehuda Sadeh 		osdc->client->options->osd_idle_ttl * HZ >> 2;
16173d14c5d2SYehuda Sadeh 
16183d14c5d2SYehuda Sadeh 	dout("osds timeout\n");
16193d14c5d2SYehuda Sadeh 	down_read(&osdc->map_sem);
1620aca420bcSSage Weil 	remove_old_osds(osdc);
16213d14c5d2SYehuda Sadeh 	up_read(&osdc->map_sem);
16223d14c5d2SYehuda Sadeh 
16233d14c5d2SYehuda Sadeh 	schedule_delayed_work(&osdc->osds_timeout_work,
16243d14c5d2SYehuda Sadeh 			      round_jiffies_relative(delay));
16253d14c5d2SYehuda Sadeh }
16263d14c5d2SYehuda Sadeh 
1627205ee118SIlya Dryomov static int ceph_oloc_decode(void **p, void *end,
1628205ee118SIlya Dryomov 			    struct ceph_object_locator *oloc)
1629205ee118SIlya Dryomov {
1630205ee118SIlya Dryomov 	u8 struct_v, struct_cv;
1631205ee118SIlya Dryomov 	u32 len;
1632205ee118SIlya Dryomov 	void *struct_end;
1633205ee118SIlya Dryomov 	int ret = 0;
1634205ee118SIlya Dryomov 
1635205ee118SIlya Dryomov 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1636205ee118SIlya Dryomov 	struct_v = ceph_decode_8(p);
1637205ee118SIlya Dryomov 	struct_cv = ceph_decode_8(p);
1638205ee118SIlya Dryomov 	if (struct_v < 3) {
1639205ee118SIlya Dryomov 		pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1640205ee118SIlya Dryomov 			struct_v, struct_cv);
1641205ee118SIlya Dryomov 		goto e_inval;
1642205ee118SIlya Dryomov 	}
1643205ee118SIlya Dryomov 	if (struct_cv > 6) {
1644205ee118SIlya Dryomov 		pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1645205ee118SIlya Dryomov 			struct_v, struct_cv);
1646205ee118SIlya Dryomov 		goto e_inval;
1647205ee118SIlya Dryomov 	}
1648205ee118SIlya Dryomov 	len = ceph_decode_32(p);
1649205ee118SIlya Dryomov 	ceph_decode_need(p, end, len, e_inval);
1650205ee118SIlya Dryomov 	struct_end = *p + len;
1651205ee118SIlya Dryomov 
1652205ee118SIlya Dryomov 	oloc->pool = ceph_decode_64(p);
1653205ee118SIlya Dryomov 	*p += 4; /* skip preferred */
1654205ee118SIlya Dryomov 
1655205ee118SIlya Dryomov 	len = ceph_decode_32(p);
1656205ee118SIlya Dryomov 	if (len > 0) {
1657205ee118SIlya Dryomov 		pr_warn("ceph_object_locator::key is set\n");
1658205ee118SIlya Dryomov 		goto e_inval;
1659205ee118SIlya Dryomov 	}
1660205ee118SIlya Dryomov 
1661205ee118SIlya Dryomov 	if (struct_v >= 5) {
1662205ee118SIlya Dryomov 		len = ceph_decode_32(p);
1663205ee118SIlya Dryomov 		if (len > 0) {
1664205ee118SIlya Dryomov 			pr_warn("ceph_object_locator::nspace is set\n");
1665205ee118SIlya Dryomov 			goto e_inval;
1666205ee118SIlya Dryomov 		}
1667205ee118SIlya Dryomov 	}
1668205ee118SIlya Dryomov 
1669205ee118SIlya Dryomov 	if (struct_v >= 6) {
1670205ee118SIlya Dryomov 		s64 hash = ceph_decode_64(p);
1671205ee118SIlya Dryomov 		if (hash != -1) {
1672205ee118SIlya Dryomov 			pr_warn("ceph_object_locator::hash is set\n");
1673205ee118SIlya Dryomov 			goto e_inval;
1674205ee118SIlya Dryomov 		}
1675205ee118SIlya Dryomov 	}
1676205ee118SIlya Dryomov 
1677205ee118SIlya Dryomov 	/* skip the rest */
1678205ee118SIlya Dryomov 	*p = struct_end;
1679205ee118SIlya Dryomov out:
1680205ee118SIlya Dryomov 	return ret;
1681205ee118SIlya Dryomov 
1682205ee118SIlya Dryomov e_inval:
1683205ee118SIlya Dryomov 	ret = -EINVAL;
1684205ee118SIlya Dryomov 	goto out;
1685205ee118SIlya Dryomov }
1686205ee118SIlya Dryomov 
1687205ee118SIlya Dryomov static int ceph_redirect_decode(void **p, void *end,
1688205ee118SIlya Dryomov 				struct ceph_request_redirect *redir)
1689205ee118SIlya Dryomov {
1690205ee118SIlya Dryomov 	u8 struct_v, struct_cv;
1691205ee118SIlya Dryomov 	u32 len;
1692205ee118SIlya Dryomov 	void *struct_end;
1693205ee118SIlya Dryomov 	int ret;
1694205ee118SIlya Dryomov 
1695205ee118SIlya Dryomov 	ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1696205ee118SIlya Dryomov 	struct_v = ceph_decode_8(p);
1697205ee118SIlya Dryomov 	struct_cv = ceph_decode_8(p);
1698205ee118SIlya Dryomov 	if (struct_cv > 1) {
1699205ee118SIlya Dryomov 		pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1700205ee118SIlya Dryomov 			struct_v, struct_cv);
1701205ee118SIlya Dryomov 		goto e_inval;
1702205ee118SIlya Dryomov 	}
1703205ee118SIlya Dryomov 	len = ceph_decode_32(p);
1704205ee118SIlya Dryomov 	ceph_decode_need(p, end, len, e_inval);
1705205ee118SIlya Dryomov 	struct_end = *p + len;
1706205ee118SIlya Dryomov 
1707205ee118SIlya Dryomov 	ret = ceph_oloc_decode(p, end, &redir->oloc);
1708205ee118SIlya Dryomov 	if (ret)
1709205ee118SIlya Dryomov 		goto out;
1710205ee118SIlya Dryomov 
1711205ee118SIlya Dryomov 	len = ceph_decode_32(p);
1712205ee118SIlya Dryomov 	if (len > 0) {
1713205ee118SIlya Dryomov 		pr_warn("ceph_request_redirect::object_name is set\n");
1714205ee118SIlya Dryomov 		goto e_inval;
1715205ee118SIlya Dryomov 	}
1716205ee118SIlya Dryomov 
1717205ee118SIlya Dryomov 	len = ceph_decode_32(p);
1718205ee118SIlya Dryomov 	*p += len; /* skip osd_instructions */
1719205ee118SIlya Dryomov 
1720205ee118SIlya Dryomov 	/* skip the rest */
1721205ee118SIlya Dryomov 	*p = struct_end;
1722205ee118SIlya Dryomov out:
1723205ee118SIlya Dryomov 	return ret;
1724205ee118SIlya Dryomov 
1725205ee118SIlya Dryomov e_inval:
1726205ee118SIlya Dryomov 	ret = -EINVAL;
1727205ee118SIlya Dryomov 	goto out;
1728205ee118SIlya Dryomov }
1729205ee118SIlya Dryomov 
173025845472SSage Weil static void complete_request(struct ceph_osd_request *req)
173125845472SSage Weil {
173225845472SSage Weil 	complete_all(&req->r_safe_completion);  /* fsync waiter */
173325845472SSage Weil }
173425845472SSage Weil 
17353d14c5d2SYehuda Sadeh /*
17363d14c5d2SYehuda Sadeh  * handle osd op reply.  either call the callback if it is specified,
17373d14c5d2SYehuda Sadeh  * or do the completion to wake up the waiting thread.
17383d14c5d2SYehuda Sadeh  */
17393d14c5d2SYehuda Sadeh static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
17403d14c5d2SYehuda Sadeh 			 struct ceph_connection *con)
17413d14c5d2SYehuda Sadeh {
17421b83bef2SSage Weil 	void *p, *end;
17433d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
1744205ee118SIlya Dryomov 	struct ceph_request_redirect redir;
17453d14c5d2SYehuda Sadeh 	u64 tid;
17461b83bef2SSage Weil 	int object_len;
174779528734SAlex Elder 	unsigned int numops;
174879528734SAlex Elder 	int payload_len, flags;
17493d14c5d2SYehuda Sadeh 	s32 result;
17501b83bef2SSage Weil 	s32 retry_attempt;
17511b83bef2SSage Weil 	struct ceph_pg pg;
17521b83bef2SSage Weil 	int err;
17531b83bef2SSage Weil 	u32 reassert_epoch;
17541b83bef2SSage Weil 	u64 reassert_version;
17551b83bef2SSage Weil 	u32 osdmap_epoch;
17560d5af164SAlex Elder 	int already_completed;
17579fc6e064SAlex Elder 	u32 bytes;
175879528734SAlex Elder 	unsigned int i;
17593d14c5d2SYehuda Sadeh 
17603d14c5d2SYehuda Sadeh 	tid = le64_to_cpu(msg->hdr.tid);
17611b83bef2SSage Weil 	dout("handle_reply %p tid %llu\n", msg, tid);
17621b83bef2SSage Weil 
17631b83bef2SSage Weil 	p = msg->front.iov_base;
17641b83bef2SSage Weil 	end = p + msg->front.iov_len;
17651b83bef2SSage Weil 
17661b83bef2SSage Weil 	ceph_decode_need(&p, end, 4, bad);
17671b83bef2SSage Weil 	object_len = ceph_decode_32(&p);
17681b83bef2SSage Weil 	ceph_decode_need(&p, end, object_len, bad);
17691b83bef2SSage Weil 	p += object_len;
17701b83bef2SSage Weil 
1771ef4859d6SAlex Elder 	err = ceph_decode_pgid(&p, end, &pg);
17721b83bef2SSage Weil 	if (err)
17733d14c5d2SYehuda Sadeh 		goto bad;
17741b83bef2SSage Weil 
17751b83bef2SSage Weil 	ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
17761b83bef2SSage Weil 	flags = ceph_decode_64(&p);
17771b83bef2SSage Weil 	result = ceph_decode_32(&p);
17781b83bef2SSage Weil 	reassert_epoch = ceph_decode_32(&p);
17791b83bef2SSage Weil 	reassert_version = ceph_decode_64(&p);
17801b83bef2SSage Weil 	osdmap_epoch = ceph_decode_32(&p);
17811b83bef2SSage Weil 
17823d14c5d2SYehuda Sadeh 	/* lookup */
1783ff513aceSIlya Dryomov 	down_read(&osdc->map_sem);
17843d14c5d2SYehuda Sadeh 	mutex_lock(&osdc->request_mutex);
17853d14c5d2SYehuda Sadeh 	req = __lookup_request(osdc, tid);
17863d14c5d2SYehuda Sadeh 	if (req == NULL) {
17873d14c5d2SYehuda Sadeh 		dout("handle_reply tid %llu dne\n", tid);
17888058fd45SAlex Elder 		goto bad_mutex;
17893d14c5d2SYehuda Sadeh 	}
17903d14c5d2SYehuda Sadeh 	ceph_osdc_get_request(req);
17911b83bef2SSage Weil 
17921b83bef2SSage Weil 	dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
17931b83bef2SSage Weil 	     req, result);
17941b83bef2SSage Weil 
179518741196SDan Carpenter 	ceph_decode_need(&p, end, 4, bad_put);
17961b83bef2SSage Weil 	numops = ceph_decode_32(&p);
17971b83bef2SSage Weil 	if (numops > CEPH_OSD_MAX_OP)
17981b83bef2SSage Weil 		goto bad_put;
17991b83bef2SSage Weil 	if (numops != req->r_num_ops)
18001b83bef2SSage Weil 		goto bad_put;
18011b83bef2SSage Weil 	payload_len = 0;
180218741196SDan Carpenter 	ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad_put);
18031b83bef2SSage Weil 	for (i = 0; i < numops; i++) {
18041b83bef2SSage Weil 		struct ceph_osd_op *op = p;
18051b83bef2SSage Weil 		int len;
18061b83bef2SSage Weil 
18071b83bef2SSage Weil 		len = le32_to_cpu(op->payload_len);
18081b83bef2SSage Weil 		req->r_reply_op_len[i] = len;
18091b83bef2SSage Weil 		dout(" op %d has %d bytes\n", i, len);
18101b83bef2SSage Weil 		payload_len += len;
18111b83bef2SSage Weil 		p += sizeof(*op);
18121b83bef2SSage Weil 	}
18139fc6e064SAlex Elder 	bytes = le32_to_cpu(msg->hdr.data_len);
18149fc6e064SAlex Elder 	if (payload_len != bytes) {
1815b9a67899SJoe Perches 		pr_warn("sum of op payload lens %d != data_len %d\n",
18169fc6e064SAlex Elder 			payload_len, bytes);
18171b83bef2SSage Weil 		goto bad_put;
18181b83bef2SSage Weil 	}
18191b83bef2SSage Weil 
182018741196SDan Carpenter 	ceph_decode_need(&p, end, 4 + numops * 4, bad_put);
18211b83bef2SSage Weil 	retry_attempt = ceph_decode_32(&p);
18221b83bef2SSage Weil 	for (i = 0; i < numops; i++)
18231b83bef2SSage Weil 		req->r_reply_op_result[i] = ceph_decode_32(&p);
18243d14c5d2SYehuda Sadeh 
1825205ee118SIlya Dryomov 	if (le16_to_cpu(msg->hdr.version) >= 6) {
1826205ee118SIlya Dryomov 		p += 8 + 4; /* skip replay_version */
1827205ee118SIlya Dryomov 		p += 8; /* skip user_version */
1828205ee118SIlya Dryomov 
1829205ee118SIlya Dryomov 		err = ceph_redirect_decode(&p, end, &redir);
1830205ee118SIlya Dryomov 		if (err)
1831205ee118SIlya Dryomov 			goto bad_put;
1832205ee118SIlya Dryomov 	} else {
1833205ee118SIlya Dryomov 		redir.oloc.pool = -1;
1834205ee118SIlya Dryomov 	}
1835205ee118SIlya Dryomov 
1836205ee118SIlya Dryomov 	if (redir.oloc.pool != -1) {
1837205ee118SIlya Dryomov 		dout("redirect pool %lld\n", redir.oloc.pool);
1838205ee118SIlya Dryomov 
1839205ee118SIlya Dryomov 		__unregister_request(osdc, req);
1840205ee118SIlya Dryomov 
1841205ee118SIlya Dryomov 		req->r_target_oloc = redir.oloc; /* struct */
1842205ee118SIlya Dryomov 
1843205ee118SIlya Dryomov 		/*
1844205ee118SIlya Dryomov 		 * Start redirect requests with nofail=true.  If
1845205ee118SIlya Dryomov 		 * mapping fails, request will end up on the notarget
1846205ee118SIlya Dryomov 		 * list, waiting for the new osdmap (which can take
1847205ee118SIlya Dryomov 		 * a while), even though the original request mapped
1848205ee118SIlya Dryomov 		 * successfully.  In the future we might want to follow
1849205ee118SIlya Dryomov 		 * original request's nofail setting here.
1850205ee118SIlya Dryomov 		 */
1851ff513aceSIlya Dryomov 		err = __ceph_osdc_start_request(osdc, req, true);
1852205ee118SIlya Dryomov 		BUG_ON(err);
1853205ee118SIlya Dryomov 
1854ff513aceSIlya Dryomov 		goto out_unlock;
1855205ee118SIlya Dryomov 	}
1856205ee118SIlya Dryomov 
1857eb845ff1SYan, Zheng 	already_completed = req->r_got_reply;
18583d14c5d2SYehuda Sadeh 	if (!req->r_got_reply) {
18591b83bef2SSage Weil 		req->r_result = result;
18603d14c5d2SYehuda Sadeh 		dout("handle_reply result %d bytes %d\n", req->r_result,
18613d14c5d2SYehuda Sadeh 		     bytes);
18623d14c5d2SYehuda Sadeh 		if (req->r_result == 0)
18633d14c5d2SYehuda Sadeh 			req->r_result = bytes;
18643d14c5d2SYehuda Sadeh 
18653d14c5d2SYehuda Sadeh 		/* in case this is a write and we need to replay, */
18661b83bef2SSage Weil 		req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
18671b83bef2SSage Weil 		req->r_reassert_version.version = cpu_to_le64(reassert_version);
18683d14c5d2SYehuda Sadeh 
18693d14c5d2SYehuda Sadeh 		req->r_got_reply = 1;
18703d14c5d2SYehuda Sadeh 	} else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
18713d14c5d2SYehuda Sadeh 		dout("handle_reply tid %llu dup ack\n", tid);
1872ff513aceSIlya Dryomov 		goto out_unlock;
18733d14c5d2SYehuda Sadeh 	}
18743d14c5d2SYehuda Sadeh 
18753d14c5d2SYehuda Sadeh 	dout("handle_reply tid %llu flags %d\n", tid, flags);
18763d14c5d2SYehuda Sadeh 
1877a40c4f10SYehuda Sadeh 	if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1878a40c4f10SYehuda Sadeh 		__register_linger_request(osdc, req);
1879a40c4f10SYehuda Sadeh 
18803d14c5d2SYehuda Sadeh 	/* either this is a read, or we got the safe response */
18813d14c5d2SYehuda Sadeh 	if (result < 0 ||
18823d14c5d2SYehuda Sadeh 	    (flags & CEPH_OSD_FLAG_ONDISK) ||
18833d14c5d2SYehuda Sadeh 	    ((flags & CEPH_OSD_FLAG_WRITE) == 0))
18843d14c5d2SYehuda Sadeh 		__unregister_request(osdc, req);
18853d14c5d2SYehuda Sadeh 
18863d14c5d2SYehuda Sadeh 	mutex_unlock(&osdc->request_mutex);
1887ff513aceSIlya Dryomov 	up_read(&osdc->map_sem);
18883d14c5d2SYehuda Sadeh 
1889eb845ff1SYan, Zheng 	if (!already_completed) {
189061c5d6bfSYan, Zheng 		if (req->r_unsafe_callback &&
189161c5d6bfSYan, Zheng 		    result >= 0 && !(flags & CEPH_OSD_FLAG_ONDISK))
189261c5d6bfSYan, Zheng 			req->r_unsafe_callback(req, true);
18933d14c5d2SYehuda Sadeh 		if (req->r_callback)
18943d14c5d2SYehuda Sadeh 			req->r_callback(req, msg);
18953d14c5d2SYehuda Sadeh 		else
18963d14c5d2SYehuda Sadeh 			complete_all(&req->r_completion);
1897eb845ff1SYan, Zheng 	}
18983d14c5d2SYehuda Sadeh 
189961c5d6bfSYan, Zheng 	if (flags & CEPH_OSD_FLAG_ONDISK) {
190061c5d6bfSYan, Zheng 		if (req->r_unsafe_callback && already_completed)
190161c5d6bfSYan, Zheng 			req->r_unsafe_callback(req, false);
190225845472SSage Weil 		complete_request(req);
190361c5d6bfSYan, Zheng 	}
19043d14c5d2SYehuda Sadeh 
1905ff513aceSIlya Dryomov out:
1906a40c4f10SYehuda Sadeh 	dout("req=%p req->r_linger=%d\n", req, req->r_linger);
19073d14c5d2SYehuda Sadeh 	ceph_osdc_put_request(req);
19083d14c5d2SYehuda Sadeh 	return;
1909ff513aceSIlya Dryomov out_unlock:
1910ff513aceSIlya Dryomov 	mutex_unlock(&osdc->request_mutex);
1911ff513aceSIlya Dryomov 	up_read(&osdc->map_sem);
1912ff513aceSIlya Dryomov 	goto out;
19133d14c5d2SYehuda Sadeh 
19141b83bef2SSage Weil bad_put:
191537c89bdeSLi Wang 	req->r_result = -EIO;
191637c89bdeSLi Wang 	__unregister_request(osdc, req);
191737c89bdeSLi Wang 	if (req->r_callback)
191837c89bdeSLi Wang 		req->r_callback(req, msg);
191937c89bdeSLi Wang 	else
192037c89bdeSLi Wang 		complete_all(&req->r_completion);
192137c89bdeSLi Wang 	complete_request(req);
19221b83bef2SSage Weil 	ceph_osdc_put_request(req);
19238058fd45SAlex Elder bad_mutex:
19248058fd45SAlex Elder 	mutex_unlock(&osdc->request_mutex);
1925ff513aceSIlya Dryomov 	up_read(&osdc->map_sem);
19263d14c5d2SYehuda Sadeh bad:
19271b83bef2SSage Weil 	pr_err("corrupt osd_op_reply got %d %d\n",
19281b83bef2SSage Weil 	       (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
19293d14c5d2SYehuda Sadeh 	ceph_msg_dump(msg);
19303d14c5d2SYehuda Sadeh }
19313d14c5d2SYehuda Sadeh 
19326f6c7006SSage Weil static void reset_changed_osds(struct ceph_osd_client *osdc)
19333d14c5d2SYehuda Sadeh {
19343d14c5d2SYehuda Sadeh 	struct rb_node *p, *n;
19353d14c5d2SYehuda Sadeh 
19363d14c5d2SYehuda Sadeh 	for (p = rb_first(&osdc->osds); p; p = n) {
19376f6c7006SSage Weil 		struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
19383d14c5d2SYehuda Sadeh 
19393d14c5d2SYehuda Sadeh 		n = rb_next(p);
19403d14c5d2SYehuda Sadeh 		if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
19413d14c5d2SYehuda Sadeh 		    memcmp(&osd->o_con.peer_addr,
19423d14c5d2SYehuda Sadeh 			   ceph_osd_addr(osdc->osdmap,
19433d14c5d2SYehuda Sadeh 					 osd->o_osd),
19443d14c5d2SYehuda Sadeh 			   sizeof(struct ceph_entity_addr)) != 0)
19453d14c5d2SYehuda Sadeh 			__reset_osd(osdc, osd);
19463d14c5d2SYehuda Sadeh 	}
19473d14c5d2SYehuda Sadeh }
19483d14c5d2SYehuda Sadeh 
19493d14c5d2SYehuda Sadeh /*
19506f6c7006SSage Weil  * Requeue requests whose mapping to an OSD has changed.  If requests map to
19516f6c7006SSage Weil  * no osd, request a new map.
19523d14c5d2SYehuda Sadeh  *
1953e6d50f67SAlex Elder  * Caller should hold map_sem for read.
19543d14c5d2SYehuda Sadeh  */
19559a1ea2dbSJosh Durgin static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
19569a1ea2dbSJosh Durgin 			  bool force_resend_writes)
19573d14c5d2SYehuda Sadeh {
1958a40c4f10SYehuda Sadeh 	struct ceph_osd_request *req, *nreq;
19596f6c7006SSage Weil 	struct rb_node *p;
19606f6c7006SSage Weil 	int needmap = 0;
19616f6c7006SSage Weil 	int err;
19629a1ea2dbSJosh Durgin 	bool force_resend_req;
19633d14c5d2SYehuda Sadeh 
19649a1ea2dbSJosh Durgin 	dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
19659a1ea2dbSJosh Durgin 		force_resend_writes ? " (force resend writes)" : "");
19663d14c5d2SYehuda Sadeh 	mutex_lock(&osdc->request_mutex);
19676194ea89SSage Weil 	for (p = rb_first(&osdc->requests); p; ) {
19686f6c7006SSage Weil 		req = rb_entry(p, struct ceph_osd_request, r_node);
19696194ea89SSage Weil 		p = rb_next(p);
1970ab60b16dSAlex Elder 
1971ab60b16dSAlex Elder 		/*
1972ab60b16dSAlex Elder 		 * For linger requests that have not yet been
1973ab60b16dSAlex Elder 		 * registered, move them to the linger list; they'll
1974ab60b16dSAlex Elder 		 * be sent to the osd in the loop below.  Unregister
1975ab60b16dSAlex Elder 		 * the request before re-registering it as a linger
1976ab60b16dSAlex Elder 		 * request to ensure the __map_request() below
1977ab60b16dSAlex Elder 		 * will decide it needs to be sent.
1978ab60b16dSAlex Elder 		 */
1979ab60b16dSAlex Elder 		if (req->r_linger && list_empty(&req->r_linger_item)) {
1980ab60b16dSAlex Elder 			dout("%p tid %llu restart on osd%d\n",
1981ab60b16dSAlex Elder 			     req, req->r_tid,
1982ab60b16dSAlex Elder 			     req->r_osd ? req->r_osd->o_osd : -1);
198396e4dac6SAlex Elder 			ceph_osdc_get_request(req);
1984ab60b16dSAlex Elder 			__unregister_request(osdc, req);
1985ab60b16dSAlex Elder 			__register_linger_request(osdc, req);
198696e4dac6SAlex Elder 			ceph_osdc_put_request(req);
1987ab60b16dSAlex Elder 			continue;
1988ab60b16dSAlex Elder 		}
1989ab60b16dSAlex Elder 
19909a1ea2dbSJosh Durgin 		force_resend_req = force_resend ||
19919a1ea2dbSJosh Durgin 			(force_resend_writes &&
19929a1ea2dbSJosh Durgin 				req->r_flags & CEPH_OSD_FLAG_WRITE);
19939a1ea2dbSJosh Durgin 		err = __map_request(osdc, req, force_resend_req);
19946f6c7006SSage Weil 		if (err < 0)
19956f6c7006SSage Weil 			continue;  /* error */
19966f6c7006SSage Weil 		if (req->r_osd == NULL) {
19976f6c7006SSage Weil 			dout("%p tid %llu maps to no osd\n", req, req->r_tid);
19986f6c7006SSage Weil 			needmap++;  /* request a newer map */
19996f6c7006SSage Weil 		} else if (err > 0) {
20006194ea89SSage Weil 			if (!req->r_linger) {
20016194ea89SSage Weil 				dout("%p tid %llu requeued on osd%d\n", req,
20026194ea89SSage Weil 				     req->r_tid,
20036f6c7006SSage Weil 				     req->r_osd ? req->r_osd->o_osd : -1);
20046f6c7006SSage Weil 				req->r_flags |= CEPH_OSD_FLAG_RETRY;
20056f6c7006SSage Weil 			}
20066f6c7006SSage Weil 		}
20076194ea89SSage Weil 	}
2008a40c4f10SYehuda Sadeh 
2009a40c4f10SYehuda Sadeh 	list_for_each_entry_safe(req, nreq, &osdc->req_linger,
2010a40c4f10SYehuda Sadeh 				 r_linger_item) {
2011a40c4f10SYehuda Sadeh 		dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
2012a40c4f10SYehuda Sadeh 
20139a1ea2dbSJosh Durgin 		err = __map_request(osdc, req,
20149a1ea2dbSJosh Durgin 				    force_resend || force_resend_writes);
2015ab60b16dSAlex Elder 		dout("__map_request returned %d\n", err);
2016a40c4f10SYehuda Sadeh 		if (err == 0)
2017a40c4f10SYehuda Sadeh 			continue;  /* no change and no osd was specified */
2018a40c4f10SYehuda Sadeh 		if (err < 0)
2019a40c4f10SYehuda Sadeh 			continue;  /* hrm! */
2020a40c4f10SYehuda Sadeh 		if (req->r_osd == NULL) {
2021a40c4f10SYehuda Sadeh 			dout("tid %llu maps to no valid osd\n", req->r_tid);
2022a40c4f10SYehuda Sadeh 			needmap++;  /* request a newer map */
2023a40c4f10SYehuda Sadeh 			continue;
2024a40c4f10SYehuda Sadeh 		}
2025a40c4f10SYehuda Sadeh 
2026a40c4f10SYehuda Sadeh 		dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
2027a40c4f10SYehuda Sadeh 		     req->r_osd ? req->r_osd->o_osd : -1);
2028a40c4f10SYehuda Sadeh 		__register_request(osdc, req);
2029c89ce05eSAlex Elder 		__unregister_linger_request(osdc, req);
2030a40c4f10SYehuda Sadeh 	}
203114d2f38dSAlex Elder 	reset_changed_osds(osdc);
20323d14c5d2SYehuda Sadeh 	mutex_unlock(&osdc->request_mutex);
20333d14c5d2SYehuda Sadeh 
20343d14c5d2SYehuda Sadeh 	if (needmap) {
20353d14c5d2SYehuda Sadeh 		dout("%d requests for down osds, need new map\n", needmap);
20363d14c5d2SYehuda Sadeh 		ceph_monc_request_next_osdmap(&osdc->client->monc);
20373d14c5d2SYehuda Sadeh 	}
20383d14c5d2SYehuda Sadeh }
20396f6c7006SSage Weil 
20406f6c7006SSage Weil 
20413d14c5d2SYehuda Sadeh /*
20423d14c5d2SYehuda Sadeh  * Process updated osd map.
20433d14c5d2SYehuda Sadeh  *
20443d14c5d2SYehuda Sadeh  * The message contains any number of incremental and full maps, normally
20453d14c5d2SYehuda Sadeh  * indicating some sort of topology change in the cluster.  Kick requests
20463d14c5d2SYehuda Sadeh  * off to different OSDs as needed.
20473d14c5d2SYehuda Sadeh  */
20483d14c5d2SYehuda Sadeh void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
20493d14c5d2SYehuda Sadeh {
20503d14c5d2SYehuda Sadeh 	void *p, *end, *next;
20513d14c5d2SYehuda Sadeh 	u32 nr_maps, maplen;
20523d14c5d2SYehuda Sadeh 	u32 epoch;
20533d14c5d2SYehuda Sadeh 	struct ceph_osdmap *newmap = NULL, *oldmap;
20543d14c5d2SYehuda Sadeh 	int err;
20553d14c5d2SYehuda Sadeh 	struct ceph_fsid fsid;
20569a1ea2dbSJosh Durgin 	bool was_full;
20573d14c5d2SYehuda Sadeh 
20583d14c5d2SYehuda Sadeh 	dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
20593d14c5d2SYehuda Sadeh 	p = msg->front.iov_base;
20603d14c5d2SYehuda Sadeh 	end = p + msg->front.iov_len;
20613d14c5d2SYehuda Sadeh 
20623d14c5d2SYehuda Sadeh 	/* verify fsid */
20633d14c5d2SYehuda Sadeh 	ceph_decode_need(&p, end, sizeof(fsid), bad);
20643d14c5d2SYehuda Sadeh 	ceph_decode_copy(&p, &fsid, sizeof(fsid));
20653d14c5d2SYehuda Sadeh 	if (ceph_check_fsid(osdc->client, &fsid) < 0)
20663d14c5d2SYehuda Sadeh 		return;
20673d14c5d2SYehuda Sadeh 
20683d14c5d2SYehuda Sadeh 	down_write(&osdc->map_sem);
20693d14c5d2SYehuda Sadeh 
20709a1ea2dbSJosh Durgin 	was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
20719a1ea2dbSJosh Durgin 
20723d14c5d2SYehuda Sadeh 	/* incremental maps */
20733d14c5d2SYehuda Sadeh 	ceph_decode_32_safe(&p, end, nr_maps, bad);
20743d14c5d2SYehuda Sadeh 	dout(" %d inc maps\n", nr_maps);
20753d14c5d2SYehuda Sadeh 	while (nr_maps > 0) {
20763d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
20773d14c5d2SYehuda Sadeh 		epoch = ceph_decode_32(&p);
20783d14c5d2SYehuda Sadeh 		maplen = ceph_decode_32(&p);
20793d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, maplen, bad);
20803d14c5d2SYehuda Sadeh 		next = p + maplen;
20813d14c5d2SYehuda Sadeh 		if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
20823d14c5d2SYehuda Sadeh 			dout("applying incremental map %u len %d\n",
20833d14c5d2SYehuda Sadeh 			     epoch, maplen);
20843d14c5d2SYehuda Sadeh 			newmap = osdmap_apply_incremental(&p, next,
20853d14c5d2SYehuda Sadeh 							  osdc->osdmap,
208615d9882cSAlex Elder 							  &osdc->client->msgr);
20873d14c5d2SYehuda Sadeh 			if (IS_ERR(newmap)) {
20883d14c5d2SYehuda Sadeh 				err = PTR_ERR(newmap);
20893d14c5d2SYehuda Sadeh 				goto bad;
20903d14c5d2SYehuda Sadeh 			}
20913d14c5d2SYehuda Sadeh 			BUG_ON(!newmap);
20923d14c5d2SYehuda Sadeh 			if (newmap != osdc->osdmap) {
20933d14c5d2SYehuda Sadeh 				ceph_osdmap_destroy(osdc->osdmap);
20943d14c5d2SYehuda Sadeh 				osdc->osdmap = newmap;
20953d14c5d2SYehuda Sadeh 			}
20969a1ea2dbSJosh Durgin 			was_full = was_full ||
20979a1ea2dbSJosh Durgin 				ceph_osdmap_flag(osdc->osdmap,
20989a1ea2dbSJosh Durgin 						 CEPH_OSDMAP_FULL);
20999a1ea2dbSJosh Durgin 			kick_requests(osdc, 0, was_full);
21003d14c5d2SYehuda Sadeh 		} else {
21013d14c5d2SYehuda Sadeh 			dout("ignoring incremental map %u len %d\n",
21023d14c5d2SYehuda Sadeh 			     epoch, maplen);
21033d14c5d2SYehuda Sadeh 		}
21043d14c5d2SYehuda Sadeh 		p = next;
21053d14c5d2SYehuda Sadeh 		nr_maps--;
21063d14c5d2SYehuda Sadeh 	}
21073d14c5d2SYehuda Sadeh 	if (newmap)
21083d14c5d2SYehuda Sadeh 		goto done;
21093d14c5d2SYehuda Sadeh 
21103d14c5d2SYehuda Sadeh 	/* full maps */
21113d14c5d2SYehuda Sadeh 	ceph_decode_32_safe(&p, end, nr_maps, bad);
21123d14c5d2SYehuda Sadeh 	dout(" %d full maps\n", nr_maps);
21133d14c5d2SYehuda Sadeh 	while (nr_maps) {
21143d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, 2*sizeof(u32), bad);
21153d14c5d2SYehuda Sadeh 		epoch = ceph_decode_32(&p);
21163d14c5d2SYehuda Sadeh 		maplen = ceph_decode_32(&p);
21173d14c5d2SYehuda Sadeh 		ceph_decode_need(&p, end, maplen, bad);
21183d14c5d2SYehuda Sadeh 		if (nr_maps > 1) {
21193d14c5d2SYehuda Sadeh 			dout("skipping non-latest full map %u len %d\n",
21203d14c5d2SYehuda Sadeh 			     epoch, maplen);
21213d14c5d2SYehuda Sadeh 		} else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
21223d14c5d2SYehuda Sadeh 			dout("skipping full map %u len %d, "
21233d14c5d2SYehuda Sadeh 			     "older than our %u\n", epoch, maplen,
21243d14c5d2SYehuda Sadeh 			     osdc->osdmap->epoch);
21253d14c5d2SYehuda Sadeh 		} else {
212638d6453cSSage Weil 			int skipped_map = 0;
212738d6453cSSage Weil 
21283d14c5d2SYehuda Sadeh 			dout("taking full map %u len %d\n", epoch, maplen);
2129a2505d63SIlya Dryomov 			newmap = ceph_osdmap_decode(&p, p+maplen);
21303d14c5d2SYehuda Sadeh 			if (IS_ERR(newmap)) {
21313d14c5d2SYehuda Sadeh 				err = PTR_ERR(newmap);
21323d14c5d2SYehuda Sadeh 				goto bad;
21333d14c5d2SYehuda Sadeh 			}
21343d14c5d2SYehuda Sadeh 			BUG_ON(!newmap);
21353d14c5d2SYehuda Sadeh 			oldmap = osdc->osdmap;
21363d14c5d2SYehuda Sadeh 			osdc->osdmap = newmap;
213738d6453cSSage Weil 			if (oldmap) {
213838d6453cSSage Weil 				if (oldmap->epoch + 1 < newmap->epoch)
213938d6453cSSage Weil 					skipped_map = 1;
21403d14c5d2SYehuda Sadeh 				ceph_osdmap_destroy(oldmap);
214138d6453cSSage Weil 			}
21429a1ea2dbSJosh Durgin 			was_full = was_full ||
21439a1ea2dbSJosh Durgin 				ceph_osdmap_flag(osdc->osdmap,
21449a1ea2dbSJosh Durgin 						 CEPH_OSDMAP_FULL);
21459a1ea2dbSJosh Durgin 			kick_requests(osdc, skipped_map, was_full);
21463d14c5d2SYehuda Sadeh 		}
21473d14c5d2SYehuda Sadeh 		p += maplen;
21483d14c5d2SYehuda Sadeh 		nr_maps--;
21493d14c5d2SYehuda Sadeh 	}
21503d14c5d2SYehuda Sadeh 
2151b72e19b9SDan Carpenter 	if (!osdc->osdmap)
2152b72e19b9SDan Carpenter 		goto bad;
21533d14c5d2SYehuda Sadeh done:
21543d14c5d2SYehuda Sadeh 	downgrade_write(&osdc->map_sem);
21553d14c5d2SYehuda Sadeh 	ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
2156cd634fb6SSage Weil 
2157cd634fb6SSage Weil 	/*
2158cd634fb6SSage Weil 	 * subscribe to subsequent osdmap updates if full to ensure
2159cd634fb6SSage Weil 	 * we find out when we are no longer full and stop returning
2160cd634fb6SSage Weil 	 * ENOSPC.
2161cd634fb6SSage Weil 	 */
2162d29adb34SJosh Durgin 	if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
2163d29adb34SJosh Durgin 		ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
2164d29adb34SJosh Durgin 		ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR))
2165cd634fb6SSage Weil 		ceph_monc_request_next_osdmap(&osdc->client->monc);
2166cd634fb6SSage Weil 
2167f9d25199SAlex Elder 	mutex_lock(&osdc->request_mutex);
2168f9d25199SAlex Elder 	__send_queued(osdc);
2169f9d25199SAlex Elder 	mutex_unlock(&osdc->request_mutex);
21703d14c5d2SYehuda Sadeh 	up_read(&osdc->map_sem);
21713d14c5d2SYehuda Sadeh 	wake_up_all(&osdc->client->auth_wq);
21723d14c5d2SYehuda Sadeh 	return;
21733d14c5d2SYehuda Sadeh 
21743d14c5d2SYehuda Sadeh bad:
21753d14c5d2SYehuda Sadeh 	pr_err("osdc handle_map corrupt msg\n");
21763d14c5d2SYehuda Sadeh 	ceph_msg_dump(msg);
21773d14c5d2SYehuda Sadeh 	up_write(&osdc->map_sem);
21783d14c5d2SYehuda Sadeh }
21793d14c5d2SYehuda Sadeh 
21803d14c5d2SYehuda Sadeh /*
2181a40c4f10SYehuda Sadeh  * watch/notify callback event infrastructure
2182a40c4f10SYehuda Sadeh  *
2183a40c4f10SYehuda Sadeh  * These callbacks are used both for watch and notify operations.
2184a40c4f10SYehuda Sadeh  */
2185a40c4f10SYehuda Sadeh static void __release_event(struct kref *kref)
2186a40c4f10SYehuda Sadeh {
2187a40c4f10SYehuda Sadeh 	struct ceph_osd_event *event =
2188a40c4f10SYehuda Sadeh 		container_of(kref, struct ceph_osd_event, kref);
2189a40c4f10SYehuda Sadeh 
2190a40c4f10SYehuda Sadeh 	dout("__release_event %p\n", event);
2191a40c4f10SYehuda Sadeh 	kfree(event);
2192a40c4f10SYehuda Sadeh }
2193a40c4f10SYehuda Sadeh 
2194a40c4f10SYehuda Sadeh static void get_event(struct ceph_osd_event *event)
2195a40c4f10SYehuda Sadeh {
2196a40c4f10SYehuda Sadeh 	kref_get(&event->kref);
2197a40c4f10SYehuda Sadeh }
2198a40c4f10SYehuda Sadeh 
2199a40c4f10SYehuda Sadeh void ceph_osdc_put_event(struct ceph_osd_event *event)
2200a40c4f10SYehuda Sadeh {
2201a40c4f10SYehuda Sadeh 	kref_put(&event->kref, __release_event);
2202a40c4f10SYehuda Sadeh }
2203a40c4f10SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_put_event);
2204a40c4f10SYehuda Sadeh 
2205a40c4f10SYehuda Sadeh static void __insert_event(struct ceph_osd_client *osdc,
2206a40c4f10SYehuda Sadeh 			     struct ceph_osd_event *new)
2207a40c4f10SYehuda Sadeh {
2208a40c4f10SYehuda Sadeh 	struct rb_node **p = &osdc->event_tree.rb_node;
2209a40c4f10SYehuda Sadeh 	struct rb_node *parent = NULL;
2210a40c4f10SYehuda Sadeh 	struct ceph_osd_event *event = NULL;
2211a40c4f10SYehuda Sadeh 
2212a40c4f10SYehuda Sadeh 	while (*p) {
2213a40c4f10SYehuda Sadeh 		parent = *p;
2214a40c4f10SYehuda Sadeh 		event = rb_entry(parent, struct ceph_osd_event, node);
2215a40c4f10SYehuda Sadeh 		if (new->cookie < event->cookie)
2216a40c4f10SYehuda Sadeh 			p = &(*p)->rb_left;
2217a40c4f10SYehuda Sadeh 		else if (new->cookie > event->cookie)
2218a40c4f10SYehuda Sadeh 			p = &(*p)->rb_right;
2219a40c4f10SYehuda Sadeh 		else
2220a40c4f10SYehuda Sadeh 			BUG();
2221a40c4f10SYehuda Sadeh 	}
2222a40c4f10SYehuda Sadeh 
2223a40c4f10SYehuda Sadeh 	rb_link_node(&new->node, parent, p);
2224a40c4f10SYehuda Sadeh 	rb_insert_color(&new->node, &osdc->event_tree);
2225a40c4f10SYehuda Sadeh }
2226a40c4f10SYehuda Sadeh 
2227a40c4f10SYehuda Sadeh static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
2228a40c4f10SYehuda Sadeh 					        u64 cookie)
2229a40c4f10SYehuda Sadeh {
2230a40c4f10SYehuda Sadeh 	struct rb_node **p = &osdc->event_tree.rb_node;
2231a40c4f10SYehuda Sadeh 	struct rb_node *parent = NULL;
2232a40c4f10SYehuda Sadeh 	struct ceph_osd_event *event = NULL;
2233a40c4f10SYehuda Sadeh 
2234a40c4f10SYehuda Sadeh 	while (*p) {
2235a40c4f10SYehuda Sadeh 		parent = *p;
2236a40c4f10SYehuda Sadeh 		event = rb_entry(parent, struct ceph_osd_event, node);
2237a40c4f10SYehuda Sadeh 		if (cookie < event->cookie)
2238a40c4f10SYehuda Sadeh 			p = &(*p)->rb_left;
2239a40c4f10SYehuda Sadeh 		else if (cookie > event->cookie)
2240a40c4f10SYehuda Sadeh 			p = &(*p)->rb_right;
2241a40c4f10SYehuda Sadeh 		else
2242a40c4f10SYehuda Sadeh 			return event;
2243a40c4f10SYehuda Sadeh 	}
2244a40c4f10SYehuda Sadeh 	return NULL;
2245a40c4f10SYehuda Sadeh }
2246a40c4f10SYehuda Sadeh 
2247a40c4f10SYehuda Sadeh static void __remove_event(struct ceph_osd_event *event)
2248a40c4f10SYehuda Sadeh {
2249a40c4f10SYehuda Sadeh 	struct ceph_osd_client *osdc = event->osdc;
2250a40c4f10SYehuda Sadeh 
2251a40c4f10SYehuda Sadeh 	if (!RB_EMPTY_NODE(&event->node)) {
2252a40c4f10SYehuda Sadeh 		dout("__remove_event removed %p\n", event);
2253a40c4f10SYehuda Sadeh 		rb_erase(&event->node, &osdc->event_tree);
2254a40c4f10SYehuda Sadeh 		ceph_osdc_put_event(event);
2255a40c4f10SYehuda Sadeh 	} else {
2256a40c4f10SYehuda Sadeh 		dout("__remove_event didn't remove %p\n", event);
2257a40c4f10SYehuda Sadeh 	}
2258a40c4f10SYehuda Sadeh }
2259a40c4f10SYehuda Sadeh 
2260a40c4f10SYehuda Sadeh int ceph_osdc_create_event(struct ceph_osd_client *osdc,
2261a40c4f10SYehuda Sadeh 			   void (*event_cb)(u64, u64, u8, void *),
22623c663bbdSAlex Elder 			   void *data, struct ceph_osd_event **pevent)
2263a40c4f10SYehuda Sadeh {
2264a40c4f10SYehuda Sadeh 	struct ceph_osd_event *event;
2265a40c4f10SYehuda Sadeh 
2266a40c4f10SYehuda Sadeh 	event = kmalloc(sizeof(*event), GFP_NOIO);
2267a40c4f10SYehuda Sadeh 	if (!event)
2268a40c4f10SYehuda Sadeh 		return -ENOMEM;
2269a40c4f10SYehuda Sadeh 
2270a40c4f10SYehuda Sadeh 	dout("create_event %p\n", event);
2271a40c4f10SYehuda Sadeh 	event->cb = event_cb;
22723c663bbdSAlex Elder 	event->one_shot = 0;
2273a40c4f10SYehuda Sadeh 	event->data = data;
2274a40c4f10SYehuda Sadeh 	event->osdc = osdc;
2275a40c4f10SYehuda Sadeh 	INIT_LIST_HEAD(&event->osd_node);
22763ee5234dSAlex Elder 	RB_CLEAR_NODE(&event->node);
2277a40c4f10SYehuda Sadeh 	kref_init(&event->kref);   /* one ref for us */
2278a40c4f10SYehuda Sadeh 	kref_get(&event->kref);    /* one ref for the caller */
2279a40c4f10SYehuda Sadeh 
2280a40c4f10SYehuda Sadeh 	spin_lock(&osdc->event_lock);
2281a40c4f10SYehuda Sadeh 	event->cookie = ++osdc->event_count;
2282a40c4f10SYehuda Sadeh 	__insert_event(osdc, event);
2283a40c4f10SYehuda Sadeh 	spin_unlock(&osdc->event_lock);
2284a40c4f10SYehuda Sadeh 
2285a40c4f10SYehuda Sadeh 	*pevent = event;
2286a40c4f10SYehuda Sadeh 	return 0;
2287a40c4f10SYehuda Sadeh }
2288a40c4f10SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_create_event);
2289a40c4f10SYehuda Sadeh 
2290a40c4f10SYehuda Sadeh void ceph_osdc_cancel_event(struct ceph_osd_event *event)
2291a40c4f10SYehuda Sadeh {
2292a40c4f10SYehuda Sadeh 	struct ceph_osd_client *osdc = event->osdc;
2293a40c4f10SYehuda Sadeh 
2294a40c4f10SYehuda Sadeh 	dout("cancel_event %p\n", event);
2295a40c4f10SYehuda Sadeh 	spin_lock(&osdc->event_lock);
2296a40c4f10SYehuda Sadeh 	__remove_event(event);
2297a40c4f10SYehuda Sadeh 	spin_unlock(&osdc->event_lock);
2298a40c4f10SYehuda Sadeh 	ceph_osdc_put_event(event); /* caller's */
2299a40c4f10SYehuda Sadeh }
2300a40c4f10SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_cancel_event);
2301a40c4f10SYehuda Sadeh 
2302a40c4f10SYehuda Sadeh 
2303a40c4f10SYehuda Sadeh static void do_event_work(struct work_struct *work)
2304a40c4f10SYehuda Sadeh {
2305a40c4f10SYehuda Sadeh 	struct ceph_osd_event_work *event_work =
2306a40c4f10SYehuda Sadeh 		container_of(work, struct ceph_osd_event_work, work);
2307a40c4f10SYehuda Sadeh 	struct ceph_osd_event *event = event_work->event;
2308a40c4f10SYehuda Sadeh 	u64 ver = event_work->ver;
2309a40c4f10SYehuda Sadeh 	u64 notify_id = event_work->notify_id;
2310a40c4f10SYehuda Sadeh 	u8 opcode = event_work->opcode;
2311a40c4f10SYehuda Sadeh 
2312a40c4f10SYehuda Sadeh 	dout("do_event_work completing %p\n", event);
2313a40c4f10SYehuda Sadeh 	event->cb(ver, notify_id, opcode, event->data);
2314a40c4f10SYehuda Sadeh 	dout("do_event_work completed %p\n", event);
2315a40c4f10SYehuda Sadeh 	ceph_osdc_put_event(event);
2316a40c4f10SYehuda Sadeh 	kfree(event_work);
2317a40c4f10SYehuda Sadeh }
2318a40c4f10SYehuda Sadeh 
2319a40c4f10SYehuda Sadeh 
2320a40c4f10SYehuda Sadeh /*
2321a40c4f10SYehuda Sadeh  * Process osd watch notifications
2322a40c4f10SYehuda Sadeh  */
23233c663bbdSAlex Elder static void handle_watch_notify(struct ceph_osd_client *osdc,
23243c663bbdSAlex Elder 				struct ceph_msg *msg)
2325a40c4f10SYehuda Sadeh {
2326a40c4f10SYehuda Sadeh 	void *p, *end;
2327a40c4f10SYehuda Sadeh 	u8 proto_ver;
2328a40c4f10SYehuda Sadeh 	u64 cookie, ver, notify_id;
2329a40c4f10SYehuda Sadeh 	u8 opcode;
2330a40c4f10SYehuda Sadeh 	struct ceph_osd_event *event;
2331a40c4f10SYehuda Sadeh 	struct ceph_osd_event_work *event_work;
2332a40c4f10SYehuda Sadeh 
2333a40c4f10SYehuda Sadeh 	p = msg->front.iov_base;
2334a40c4f10SYehuda Sadeh 	end = p + msg->front.iov_len;
2335a40c4f10SYehuda Sadeh 
2336a40c4f10SYehuda Sadeh 	ceph_decode_8_safe(&p, end, proto_ver, bad);
2337a40c4f10SYehuda Sadeh 	ceph_decode_8_safe(&p, end, opcode, bad);
2338a40c4f10SYehuda Sadeh 	ceph_decode_64_safe(&p, end, cookie, bad);
2339a40c4f10SYehuda Sadeh 	ceph_decode_64_safe(&p, end, ver, bad);
2340a40c4f10SYehuda Sadeh 	ceph_decode_64_safe(&p, end, notify_id, bad);
2341a40c4f10SYehuda Sadeh 
2342a40c4f10SYehuda Sadeh 	spin_lock(&osdc->event_lock);
2343a40c4f10SYehuda Sadeh 	event = __find_event(osdc, cookie);
2344a40c4f10SYehuda Sadeh 	if (event) {
23453c663bbdSAlex Elder 		BUG_ON(event->one_shot);
2346a40c4f10SYehuda Sadeh 		get_event(event);
2347a40c4f10SYehuda Sadeh 	}
2348a40c4f10SYehuda Sadeh 	spin_unlock(&osdc->event_lock);
2349a40c4f10SYehuda Sadeh 	dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2350a40c4f10SYehuda Sadeh 	     cookie, ver, event);
2351a40c4f10SYehuda Sadeh 	if (event) {
2352a40c4f10SYehuda Sadeh 		event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
2353a40c4f10SYehuda Sadeh 		if (!event_work) {
235491883cd2SIlya Dryomov 			pr_err("couldn't allocate event_work\n");
235591883cd2SIlya Dryomov 			ceph_osdc_put_event(event);
235691883cd2SIlya Dryomov 			return;
2357a40c4f10SYehuda Sadeh 		}
23586b0ae409SMariusz Kozlowski 		INIT_WORK(&event_work->work, do_event_work);
2359a40c4f10SYehuda Sadeh 		event_work->event = event;
2360a40c4f10SYehuda Sadeh 		event_work->ver = ver;
2361a40c4f10SYehuda Sadeh 		event_work->notify_id = notify_id;
2362a40c4f10SYehuda Sadeh 		event_work->opcode = opcode;
236391883cd2SIlya Dryomov 
236491883cd2SIlya Dryomov 		queue_work(osdc->notify_wq, &event_work->work);
2365a40c4f10SYehuda Sadeh 	}
2366a40c4f10SYehuda Sadeh 
2367a40c4f10SYehuda Sadeh 	return;
2368a40c4f10SYehuda Sadeh 
2369a40c4f10SYehuda Sadeh bad:
2370a40c4f10SYehuda Sadeh 	pr_err("osdc handle_watch_notify corrupt msg\n");
2371a40c4f10SYehuda Sadeh }
2372a40c4f10SYehuda Sadeh 
2373a40c4f10SYehuda Sadeh /*
2374e65550fdSAlex Elder  * build new request AND message
2375e65550fdSAlex Elder  *
2376e65550fdSAlex Elder  */
2377e65550fdSAlex Elder void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
2378e65550fdSAlex Elder 				struct ceph_snap_context *snapc, u64 snap_id,
2379e65550fdSAlex Elder 				struct timespec *mtime)
2380e65550fdSAlex Elder {
2381e65550fdSAlex Elder 	struct ceph_msg *msg = req->r_request;
2382e65550fdSAlex Elder 	void *p;
2383e65550fdSAlex Elder 	size_t msg_size;
2384e65550fdSAlex Elder 	int flags = req->r_flags;
2385e65550fdSAlex Elder 	u64 data_len;
2386e65550fdSAlex Elder 	unsigned int i;
2387e65550fdSAlex Elder 
2388e65550fdSAlex Elder 	req->r_snapid = snap_id;
2389e65550fdSAlex Elder 	req->r_snapc = ceph_get_snap_context(snapc);
2390e65550fdSAlex Elder 
2391e65550fdSAlex Elder 	/* encode request */
2392e65550fdSAlex Elder 	msg->hdr.version = cpu_to_le16(4);
2393e65550fdSAlex Elder 
2394e65550fdSAlex Elder 	p = msg->front.iov_base;
2395e65550fdSAlex Elder 	ceph_encode_32(&p, 1);   /* client_inc  is always 1 */
2396e65550fdSAlex Elder 	req->r_request_osdmap_epoch = p;
2397e65550fdSAlex Elder 	p += 4;
2398e65550fdSAlex Elder 	req->r_request_flags = p;
2399e65550fdSAlex Elder 	p += 4;
2400e65550fdSAlex Elder 	if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2401e65550fdSAlex Elder 		ceph_encode_timespec(p, mtime);
2402e65550fdSAlex Elder 	p += sizeof(struct ceph_timespec);
2403e65550fdSAlex Elder 	req->r_request_reassert_version = p;
2404e65550fdSAlex Elder 	p += sizeof(struct ceph_eversion); /* will get filled in */
2405e65550fdSAlex Elder 
2406e65550fdSAlex Elder 	/* oloc */
2407e65550fdSAlex Elder 	ceph_encode_8(&p, 4);
2408e65550fdSAlex Elder 	ceph_encode_8(&p, 4);
2409e65550fdSAlex Elder 	ceph_encode_32(&p, 8 + 4 + 4);
2410e65550fdSAlex Elder 	req->r_request_pool = p;
2411e65550fdSAlex Elder 	p += 8;
2412e65550fdSAlex Elder 	ceph_encode_32(&p, -1);  /* preferred */
2413e65550fdSAlex Elder 	ceph_encode_32(&p, 0);   /* key len */
2414e65550fdSAlex Elder 
2415e65550fdSAlex Elder 	ceph_encode_8(&p, 1);
2416e65550fdSAlex Elder 	req->r_request_pgid = p;
2417e65550fdSAlex Elder 	p += 8 + 4;
2418e65550fdSAlex Elder 	ceph_encode_32(&p, -1);  /* preferred */
2419e65550fdSAlex Elder 
2420e65550fdSAlex Elder 	/* oid */
24213c972c95SIlya Dryomov 	ceph_encode_32(&p, req->r_base_oid.name_len);
24223c972c95SIlya Dryomov 	memcpy(p, req->r_base_oid.name, req->r_base_oid.name_len);
24233c972c95SIlya Dryomov 	dout("oid '%.*s' len %d\n", req->r_base_oid.name_len,
24243c972c95SIlya Dryomov 	     req->r_base_oid.name, req->r_base_oid.name_len);
24253c972c95SIlya Dryomov 	p += req->r_base_oid.name_len;
2426e65550fdSAlex Elder 
2427e65550fdSAlex Elder 	/* ops--can imply data */
2428e65550fdSAlex Elder 	ceph_encode_16(&p, (u16)req->r_num_ops);
2429e65550fdSAlex Elder 	data_len = 0;
2430e65550fdSAlex Elder 	for (i = 0; i < req->r_num_ops; i++) {
2431e65550fdSAlex Elder 		data_len += osd_req_encode_op(req, p, i);
2432e65550fdSAlex Elder 		p += sizeof(struct ceph_osd_op);
2433e65550fdSAlex Elder 	}
2434e65550fdSAlex Elder 
2435e65550fdSAlex Elder 	/* snaps */
2436e65550fdSAlex Elder 	ceph_encode_64(&p, req->r_snapid);
2437e65550fdSAlex Elder 	ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2438e65550fdSAlex Elder 	ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2439e65550fdSAlex Elder 	if (req->r_snapc) {
2440e65550fdSAlex Elder 		for (i = 0; i < snapc->num_snaps; i++) {
2441e65550fdSAlex Elder 			ceph_encode_64(&p, req->r_snapc->snaps[i]);
2442e65550fdSAlex Elder 		}
2443e65550fdSAlex Elder 	}
2444e65550fdSAlex Elder 
2445e65550fdSAlex Elder 	req->r_request_attempts = p;
2446e65550fdSAlex Elder 	p += 4;
2447e65550fdSAlex Elder 
2448e65550fdSAlex Elder 	/* data */
2449e65550fdSAlex Elder 	if (flags & CEPH_OSD_FLAG_WRITE) {
2450e65550fdSAlex Elder 		u16 data_off;
2451e65550fdSAlex Elder 
2452e65550fdSAlex Elder 		/*
2453e65550fdSAlex Elder 		 * The header "data_off" is a hint to the receiver
2454e65550fdSAlex Elder 		 * allowing it to align received data into its
2455e65550fdSAlex Elder 		 * buffers such that there's no need to re-copy
2456e65550fdSAlex Elder 		 * it before writing it to disk (direct I/O).
2457e65550fdSAlex Elder 		 */
2458e65550fdSAlex Elder 		data_off = (u16) (off & 0xffff);
2459e65550fdSAlex Elder 		req->r_request->hdr.data_off = cpu_to_le16(data_off);
2460e65550fdSAlex Elder 	}
2461e65550fdSAlex Elder 	req->r_request->hdr.data_len = cpu_to_le32(data_len);
2462e65550fdSAlex Elder 
2463e65550fdSAlex Elder 	BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2464e65550fdSAlex Elder 	msg_size = p - msg->front.iov_base;
2465e65550fdSAlex Elder 	msg->front.iov_len = msg_size;
2466e65550fdSAlex Elder 	msg->hdr.front_len = cpu_to_le32(msg_size);
2467e65550fdSAlex Elder 
2468e65550fdSAlex Elder 	dout("build_request msg_size was %d\n", (int)msg_size);
2469e65550fdSAlex Elder }
2470e65550fdSAlex Elder EXPORT_SYMBOL(ceph_osdc_build_request);
2471e65550fdSAlex Elder 
2472e65550fdSAlex Elder /*
24733d14c5d2SYehuda Sadeh  * Register request, send initial attempt.
24743d14c5d2SYehuda Sadeh  */
24753d14c5d2SYehuda Sadeh int ceph_osdc_start_request(struct ceph_osd_client *osdc,
24763d14c5d2SYehuda Sadeh 			    struct ceph_osd_request *req,
24773d14c5d2SYehuda Sadeh 			    bool nofail)
24783d14c5d2SYehuda Sadeh {
24790bbfdfe8SIlya Dryomov 	int rc;
24803d14c5d2SYehuda Sadeh 
24813d14c5d2SYehuda Sadeh 	down_read(&osdc->map_sem);
24823d14c5d2SYehuda Sadeh 	mutex_lock(&osdc->request_mutex);
24830bbfdfe8SIlya Dryomov 
24840bbfdfe8SIlya Dryomov 	rc = __ceph_osdc_start_request(osdc, req, nofail);
24850bbfdfe8SIlya Dryomov 
24863d14c5d2SYehuda Sadeh 	mutex_unlock(&osdc->request_mutex);
24873d14c5d2SYehuda Sadeh 	up_read(&osdc->map_sem);
24880bbfdfe8SIlya Dryomov 
24893d14c5d2SYehuda Sadeh 	return rc;
24903d14c5d2SYehuda Sadeh }
24913d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_start_request);
24923d14c5d2SYehuda Sadeh 
24933d14c5d2SYehuda Sadeh /*
2494c9f9b93dSIlya Dryomov  * Unregister a registered request.  The request is not completed (i.e.
2495c9f9b93dSIlya Dryomov  * no callbacks or wakeups) - higher layers are supposed to know what
2496c9f9b93dSIlya Dryomov  * they are canceling.
2497c9f9b93dSIlya Dryomov  */
2498c9f9b93dSIlya Dryomov void ceph_osdc_cancel_request(struct ceph_osd_request *req)
2499c9f9b93dSIlya Dryomov {
2500c9f9b93dSIlya Dryomov 	struct ceph_osd_client *osdc = req->r_osdc;
2501c9f9b93dSIlya Dryomov 
2502c9f9b93dSIlya Dryomov 	mutex_lock(&osdc->request_mutex);
2503c9f9b93dSIlya Dryomov 	if (req->r_linger)
2504c9f9b93dSIlya Dryomov 		__unregister_linger_request(osdc, req);
2505c9f9b93dSIlya Dryomov 	__unregister_request(osdc, req);
2506c9f9b93dSIlya Dryomov 	mutex_unlock(&osdc->request_mutex);
2507c9f9b93dSIlya Dryomov 
2508c9f9b93dSIlya Dryomov 	dout("%s %p tid %llu canceled\n", __func__, req, req->r_tid);
2509c9f9b93dSIlya Dryomov }
2510c9f9b93dSIlya Dryomov EXPORT_SYMBOL(ceph_osdc_cancel_request);
2511c9f9b93dSIlya Dryomov 
2512c9f9b93dSIlya Dryomov /*
25133d14c5d2SYehuda Sadeh  * wait for a request to complete
25143d14c5d2SYehuda Sadeh  */
25153d14c5d2SYehuda Sadeh int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
25163d14c5d2SYehuda Sadeh 			   struct ceph_osd_request *req)
25173d14c5d2SYehuda Sadeh {
25183d14c5d2SYehuda Sadeh 	int rc;
25193d14c5d2SYehuda Sadeh 
2520c9f9b93dSIlya Dryomov 	dout("%s %p tid %llu\n", __func__, req, req->r_tid);
2521c9f9b93dSIlya Dryomov 
25223d14c5d2SYehuda Sadeh 	rc = wait_for_completion_interruptible(&req->r_completion);
25233d14c5d2SYehuda Sadeh 	if (rc < 0) {
2524c9f9b93dSIlya Dryomov 		dout("%s %p tid %llu interrupted\n", __func__, req, req->r_tid);
2525c9f9b93dSIlya Dryomov 		ceph_osdc_cancel_request(req);
252625845472SSage Weil 		complete_request(req);
25273d14c5d2SYehuda Sadeh 		return rc;
25283d14c5d2SYehuda Sadeh 	}
25293d14c5d2SYehuda Sadeh 
2530c9f9b93dSIlya Dryomov 	dout("%s %p tid %llu result %d\n", __func__, req, req->r_tid,
2531c9f9b93dSIlya Dryomov 	     req->r_result);
25323d14c5d2SYehuda Sadeh 	return req->r_result;
25333d14c5d2SYehuda Sadeh }
25343d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_wait_request);
25353d14c5d2SYehuda Sadeh 
25363d14c5d2SYehuda Sadeh /*
25373d14c5d2SYehuda Sadeh  * sync - wait for all in-flight requests to flush.  avoid starvation.
25383d14c5d2SYehuda Sadeh  */
25393d14c5d2SYehuda Sadeh void ceph_osdc_sync(struct ceph_osd_client *osdc)
25403d14c5d2SYehuda Sadeh {
25413d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
25423d14c5d2SYehuda Sadeh 	u64 last_tid, next_tid = 0;
25433d14c5d2SYehuda Sadeh 
25443d14c5d2SYehuda Sadeh 	mutex_lock(&osdc->request_mutex);
25453d14c5d2SYehuda Sadeh 	last_tid = osdc->last_tid;
25463d14c5d2SYehuda Sadeh 	while (1) {
25473d14c5d2SYehuda Sadeh 		req = __lookup_request_ge(osdc, next_tid);
25483d14c5d2SYehuda Sadeh 		if (!req)
25493d14c5d2SYehuda Sadeh 			break;
25503d14c5d2SYehuda Sadeh 		if (req->r_tid > last_tid)
25513d14c5d2SYehuda Sadeh 			break;
25523d14c5d2SYehuda Sadeh 
25533d14c5d2SYehuda Sadeh 		next_tid = req->r_tid + 1;
25543d14c5d2SYehuda Sadeh 		if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
25553d14c5d2SYehuda Sadeh 			continue;
25563d14c5d2SYehuda Sadeh 
25573d14c5d2SYehuda Sadeh 		ceph_osdc_get_request(req);
25583d14c5d2SYehuda Sadeh 		mutex_unlock(&osdc->request_mutex);
25593d14c5d2SYehuda Sadeh 		dout("sync waiting on tid %llu (last is %llu)\n",
25603d14c5d2SYehuda Sadeh 		     req->r_tid, last_tid);
25613d14c5d2SYehuda Sadeh 		wait_for_completion(&req->r_safe_completion);
25623d14c5d2SYehuda Sadeh 		mutex_lock(&osdc->request_mutex);
25633d14c5d2SYehuda Sadeh 		ceph_osdc_put_request(req);
25643d14c5d2SYehuda Sadeh 	}
25653d14c5d2SYehuda Sadeh 	mutex_unlock(&osdc->request_mutex);
25663d14c5d2SYehuda Sadeh 	dout("sync done (thru tid %llu)\n", last_tid);
25673d14c5d2SYehuda Sadeh }
25683d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_sync);
25693d14c5d2SYehuda Sadeh 
25703d14c5d2SYehuda Sadeh /*
2571dd935f44SJosh Durgin  * Call all pending notify callbacks - for use after a watch is
2572dd935f44SJosh Durgin  * unregistered, to make sure no more callbacks for it will be invoked
2573dd935f44SJosh Durgin  */
2574f6479449Sstephen hemminger void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
2575dd935f44SJosh Durgin {
2576dd935f44SJosh Durgin 	flush_workqueue(osdc->notify_wq);
2577dd935f44SJosh Durgin }
2578dd935f44SJosh Durgin EXPORT_SYMBOL(ceph_osdc_flush_notifies);
2579dd935f44SJosh Durgin 
2580dd935f44SJosh Durgin 
2581dd935f44SJosh Durgin /*
25823d14c5d2SYehuda Sadeh  * init, shutdown
25833d14c5d2SYehuda Sadeh  */
25843d14c5d2SYehuda Sadeh int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
25853d14c5d2SYehuda Sadeh {
25863d14c5d2SYehuda Sadeh 	int err;
25873d14c5d2SYehuda Sadeh 
25883d14c5d2SYehuda Sadeh 	dout("init\n");
25893d14c5d2SYehuda Sadeh 	osdc->client = client;
25903d14c5d2SYehuda Sadeh 	osdc->osdmap = NULL;
25913d14c5d2SYehuda Sadeh 	init_rwsem(&osdc->map_sem);
25923d14c5d2SYehuda Sadeh 	init_completion(&osdc->map_waiters);
25933d14c5d2SYehuda Sadeh 	osdc->last_requested_map = 0;
25943d14c5d2SYehuda Sadeh 	mutex_init(&osdc->request_mutex);
25953d14c5d2SYehuda Sadeh 	osdc->last_tid = 0;
25963d14c5d2SYehuda Sadeh 	osdc->osds = RB_ROOT;
25973d14c5d2SYehuda Sadeh 	INIT_LIST_HEAD(&osdc->osd_lru);
25983d14c5d2SYehuda Sadeh 	osdc->requests = RB_ROOT;
25993d14c5d2SYehuda Sadeh 	INIT_LIST_HEAD(&osdc->req_lru);
26006f6c7006SSage Weil 	INIT_LIST_HEAD(&osdc->req_unsent);
26016f6c7006SSage Weil 	INIT_LIST_HEAD(&osdc->req_notarget);
2602a40c4f10SYehuda Sadeh 	INIT_LIST_HEAD(&osdc->req_linger);
26033d14c5d2SYehuda Sadeh 	osdc->num_requests = 0;
26043d14c5d2SYehuda Sadeh 	INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
26053d14c5d2SYehuda Sadeh 	INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
2606a40c4f10SYehuda Sadeh 	spin_lock_init(&osdc->event_lock);
2607a40c4f10SYehuda Sadeh 	osdc->event_tree = RB_ROOT;
2608a40c4f10SYehuda Sadeh 	osdc->event_count = 0;
26093d14c5d2SYehuda Sadeh 
26103d14c5d2SYehuda Sadeh 	schedule_delayed_work(&osdc->osds_timeout_work,
26113d14c5d2SYehuda Sadeh 	   round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
26123d14c5d2SYehuda Sadeh 
26133d14c5d2SYehuda Sadeh 	err = -ENOMEM;
26143d14c5d2SYehuda Sadeh 	osdc->req_mempool = mempool_create_kmalloc_pool(10,
26153d14c5d2SYehuda Sadeh 					sizeof(struct ceph_osd_request));
26163d14c5d2SYehuda Sadeh 	if (!osdc->req_mempool)
26173d14c5d2SYehuda Sadeh 		goto out;
26183d14c5d2SYehuda Sadeh 
2619d50b409fSSage Weil 	err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2620d50b409fSSage Weil 				OSD_OP_FRONT_LEN, 10, true,
26213d14c5d2SYehuda Sadeh 				"osd_op");
26223d14c5d2SYehuda Sadeh 	if (err < 0)
26233d14c5d2SYehuda Sadeh 		goto out_mempool;
2624d50b409fSSage Weil 	err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
26253d14c5d2SYehuda Sadeh 				OSD_OPREPLY_FRONT_LEN, 10, true,
26263d14c5d2SYehuda Sadeh 				"osd_op_reply");
26273d14c5d2SYehuda Sadeh 	if (err < 0)
26283d14c5d2SYehuda Sadeh 		goto out_msgpool;
2629a40c4f10SYehuda Sadeh 
2630dbcae088SDan Carpenter 	err = -ENOMEM;
2631a40c4f10SYehuda Sadeh 	osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
2632dbcae088SDan Carpenter 	if (!osdc->notify_wq)
2633c172ec5cSIlya Dryomov 		goto out_msgpool_reply;
2634c172ec5cSIlya Dryomov 
26353d14c5d2SYehuda Sadeh 	return 0;
26363d14c5d2SYehuda Sadeh 
2637c172ec5cSIlya Dryomov out_msgpool_reply:
2638c172ec5cSIlya Dryomov 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
26393d14c5d2SYehuda Sadeh out_msgpool:
26403d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op);
26413d14c5d2SYehuda Sadeh out_mempool:
26423d14c5d2SYehuda Sadeh 	mempool_destroy(osdc->req_mempool);
26433d14c5d2SYehuda Sadeh out:
26443d14c5d2SYehuda Sadeh 	return err;
26453d14c5d2SYehuda Sadeh }
26463d14c5d2SYehuda Sadeh 
26473d14c5d2SYehuda Sadeh void ceph_osdc_stop(struct ceph_osd_client *osdc)
26483d14c5d2SYehuda Sadeh {
2649a40c4f10SYehuda Sadeh 	flush_workqueue(osdc->notify_wq);
2650a40c4f10SYehuda Sadeh 	destroy_workqueue(osdc->notify_wq);
26513d14c5d2SYehuda Sadeh 	cancel_delayed_work_sync(&osdc->timeout_work);
26523d14c5d2SYehuda Sadeh 	cancel_delayed_work_sync(&osdc->osds_timeout_work);
26533d14c5d2SYehuda Sadeh 	if (osdc->osdmap) {
26543d14c5d2SYehuda Sadeh 		ceph_osdmap_destroy(osdc->osdmap);
26553d14c5d2SYehuda Sadeh 		osdc->osdmap = NULL;
26563d14c5d2SYehuda Sadeh 	}
2657aca420bcSSage Weil 	remove_all_osds(osdc);
26583d14c5d2SYehuda Sadeh 	mempool_destroy(osdc->req_mempool);
26593d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op);
26603d14c5d2SYehuda Sadeh 	ceph_msgpool_destroy(&osdc->msgpool_op_reply);
26613d14c5d2SYehuda Sadeh }
26623d14c5d2SYehuda Sadeh 
26633d14c5d2SYehuda Sadeh /*
26643d14c5d2SYehuda Sadeh  * Read some contiguous pages.  If we cross a stripe boundary, shorten
26653d14c5d2SYehuda Sadeh  * *plen.  Return number of bytes read, or error.
26663d14c5d2SYehuda Sadeh  */
26673d14c5d2SYehuda Sadeh int ceph_osdc_readpages(struct ceph_osd_client *osdc,
26683d14c5d2SYehuda Sadeh 			struct ceph_vino vino, struct ceph_file_layout *layout,
26693d14c5d2SYehuda Sadeh 			u64 off, u64 *plen,
26703d14c5d2SYehuda Sadeh 			u32 truncate_seq, u64 truncate_size,
2671b7495fc2SSage Weil 			struct page **pages, int num_pages, int page_align)
26723d14c5d2SYehuda Sadeh {
26733d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
26743d14c5d2SYehuda Sadeh 	int rc = 0;
26753d14c5d2SYehuda Sadeh 
26763d14c5d2SYehuda Sadeh 	dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
26773d14c5d2SYehuda Sadeh 	     vino.snap, off, *plen);
267879528734SAlex Elder 	req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 1,
26793d14c5d2SYehuda Sadeh 				    CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
2680acead002SAlex Elder 				    NULL, truncate_seq, truncate_size,
2681153e5167SAlex Elder 				    false);
26826816282dSSage Weil 	if (IS_ERR(req))
26836816282dSSage Weil 		return PTR_ERR(req);
26843d14c5d2SYehuda Sadeh 
26853d14c5d2SYehuda Sadeh 	/* it may be a short read due to an object boundary */
26860fff87ecSAlex Elder 
2687406e2c9fSAlex Elder 	osd_req_op_extent_osd_data_pages(req, 0,
2688a4ce40a9SAlex Elder 				pages, *plen, page_align, false, false);
26893d14c5d2SYehuda Sadeh 
2690e0c59487SAlex Elder 	dout("readpages  final extent is %llu~%llu (%llu bytes align %d)\n",
269143bfe5deSAlex Elder 	     off, *plen, *plen, page_align);
26923d14c5d2SYehuda Sadeh 
269379528734SAlex Elder 	ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
269402ee07d3SAlex Elder 
26953d14c5d2SYehuda Sadeh 	rc = ceph_osdc_start_request(osdc, req, false);
26963d14c5d2SYehuda Sadeh 	if (!rc)
26973d14c5d2SYehuda Sadeh 		rc = ceph_osdc_wait_request(osdc, req);
26983d14c5d2SYehuda Sadeh 
26993d14c5d2SYehuda Sadeh 	ceph_osdc_put_request(req);
27003d14c5d2SYehuda Sadeh 	dout("readpages result %d\n", rc);
27013d14c5d2SYehuda Sadeh 	return rc;
27023d14c5d2SYehuda Sadeh }
27033d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_readpages);
27043d14c5d2SYehuda Sadeh 
27053d14c5d2SYehuda Sadeh /*
27063d14c5d2SYehuda Sadeh  * do a synchronous write on N pages
27073d14c5d2SYehuda Sadeh  */
27083d14c5d2SYehuda Sadeh int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
27093d14c5d2SYehuda Sadeh 			 struct ceph_file_layout *layout,
27103d14c5d2SYehuda Sadeh 			 struct ceph_snap_context *snapc,
27113d14c5d2SYehuda Sadeh 			 u64 off, u64 len,
27123d14c5d2SYehuda Sadeh 			 u32 truncate_seq, u64 truncate_size,
27133d14c5d2SYehuda Sadeh 			 struct timespec *mtime,
271424808826SAlex Elder 			 struct page **pages, int num_pages)
27153d14c5d2SYehuda Sadeh {
27163d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
27173d14c5d2SYehuda Sadeh 	int rc = 0;
2718b7495fc2SSage Weil 	int page_align = off & ~PAGE_MASK;
27193d14c5d2SYehuda Sadeh 
2720acead002SAlex Elder 	BUG_ON(vino.snap != CEPH_NOSNAP);	/* snapshots aren't writeable */
272179528734SAlex Elder 	req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 1,
27223d14c5d2SYehuda Sadeh 				    CEPH_OSD_OP_WRITE,
272324808826SAlex Elder 				    CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
2724acead002SAlex Elder 				    snapc, truncate_seq, truncate_size,
2725153e5167SAlex Elder 				    true);
27266816282dSSage Weil 	if (IS_ERR(req))
27276816282dSSage Weil 		return PTR_ERR(req);
27283d14c5d2SYehuda Sadeh 
27293d14c5d2SYehuda Sadeh 	/* it may be a short write due to an object boundary */
2730406e2c9fSAlex Elder 	osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
273143bfe5deSAlex Elder 				false, false);
273243bfe5deSAlex Elder 	dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
27333d14c5d2SYehuda Sadeh 
273479528734SAlex Elder 	ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
273502ee07d3SAlex Elder 
273687f979d3SAlex Elder 	rc = ceph_osdc_start_request(osdc, req, true);
27373d14c5d2SYehuda Sadeh 	if (!rc)
27383d14c5d2SYehuda Sadeh 		rc = ceph_osdc_wait_request(osdc, req);
27393d14c5d2SYehuda Sadeh 
27403d14c5d2SYehuda Sadeh 	ceph_osdc_put_request(req);
27413d14c5d2SYehuda Sadeh 	if (rc == 0)
27423d14c5d2SYehuda Sadeh 		rc = len;
27433d14c5d2SYehuda Sadeh 	dout("writepages result %d\n", rc);
27443d14c5d2SYehuda Sadeh 	return rc;
27453d14c5d2SYehuda Sadeh }
27463d14c5d2SYehuda Sadeh EXPORT_SYMBOL(ceph_osdc_writepages);
27473d14c5d2SYehuda Sadeh 
27485522ae0bSAlex Elder int ceph_osdc_setup(void)
27495522ae0bSAlex Elder {
27505522ae0bSAlex Elder 	BUG_ON(ceph_osd_request_cache);
27515522ae0bSAlex Elder 	ceph_osd_request_cache = kmem_cache_create("ceph_osd_request",
27525522ae0bSAlex Elder 					sizeof (struct ceph_osd_request),
27535522ae0bSAlex Elder 					__alignof__(struct ceph_osd_request),
27545522ae0bSAlex Elder 					0, NULL);
27555522ae0bSAlex Elder 
27565522ae0bSAlex Elder 	return ceph_osd_request_cache ? 0 : -ENOMEM;
27575522ae0bSAlex Elder }
27585522ae0bSAlex Elder EXPORT_SYMBOL(ceph_osdc_setup);
27595522ae0bSAlex Elder 
27605522ae0bSAlex Elder void ceph_osdc_cleanup(void)
27615522ae0bSAlex Elder {
27625522ae0bSAlex Elder 	BUG_ON(!ceph_osd_request_cache);
27635522ae0bSAlex Elder 	kmem_cache_destroy(ceph_osd_request_cache);
27645522ae0bSAlex Elder 	ceph_osd_request_cache = NULL;
27655522ae0bSAlex Elder }
27665522ae0bSAlex Elder EXPORT_SYMBOL(ceph_osdc_cleanup);
27675522ae0bSAlex Elder 
27683d14c5d2SYehuda Sadeh /*
27693d14c5d2SYehuda Sadeh  * handle incoming message
27703d14c5d2SYehuda Sadeh  */
27713d14c5d2SYehuda Sadeh static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
27723d14c5d2SYehuda Sadeh {
27733d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
27743d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc;
27753d14c5d2SYehuda Sadeh 	int type = le16_to_cpu(msg->hdr.type);
27763d14c5d2SYehuda Sadeh 
27773d14c5d2SYehuda Sadeh 	if (!osd)
27783d14c5d2SYehuda Sadeh 		goto out;
27793d14c5d2SYehuda Sadeh 	osdc = osd->o_osdc;
27803d14c5d2SYehuda Sadeh 
27813d14c5d2SYehuda Sadeh 	switch (type) {
27823d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP:
27833d14c5d2SYehuda Sadeh 		ceph_osdc_handle_map(osdc, msg);
27843d14c5d2SYehuda Sadeh 		break;
27853d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY:
27863d14c5d2SYehuda Sadeh 		handle_reply(osdc, msg, con);
27873d14c5d2SYehuda Sadeh 		break;
2788a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY:
2789a40c4f10SYehuda Sadeh 		handle_watch_notify(osdc, msg);
2790a40c4f10SYehuda Sadeh 		break;
27913d14c5d2SYehuda Sadeh 
27923d14c5d2SYehuda Sadeh 	default:
27933d14c5d2SYehuda Sadeh 		pr_err("received unknown message type %d %s\n", type,
27943d14c5d2SYehuda Sadeh 		       ceph_msg_type_name(type));
27953d14c5d2SYehuda Sadeh 	}
27963d14c5d2SYehuda Sadeh out:
27973d14c5d2SYehuda Sadeh 	ceph_msg_put(msg);
27983d14c5d2SYehuda Sadeh }
27993d14c5d2SYehuda Sadeh 
28003d14c5d2SYehuda Sadeh /*
28013d14c5d2SYehuda Sadeh  * lookup and return message for incoming reply.  set up reply message
28023d14c5d2SYehuda Sadeh  * pages.
28033d14c5d2SYehuda Sadeh  */
28043d14c5d2SYehuda Sadeh static struct ceph_msg *get_reply(struct ceph_connection *con,
28053d14c5d2SYehuda Sadeh 				  struct ceph_msg_header *hdr,
28063d14c5d2SYehuda Sadeh 				  int *skip)
28073d14c5d2SYehuda Sadeh {
28083d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
28093d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = osd->o_osdc;
28103d14c5d2SYehuda Sadeh 	struct ceph_msg *m;
28113d14c5d2SYehuda Sadeh 	struct ceph_osd_request *req;
28123f0a4ac5SIlya Dryomov 	int front_len = le32_to_cpu(hdr->front_len);
28133d14c5d2SYehuda Sadeh 	int data_len = le32_to_cpu(hdr->data_len);
28143d14c5d2SYehuda Sadeh 	u64 tid;
28153d14c5d2SYehuda Sadeh 
28163d14c5d2SYehuda Sadeh 	tid = le64_to_cpu(hdr->tid);
28173d14c5d2SYehuda Sadeh 	mutex_lock(&osdc->request_mutex);
28183d14c5d2SYehuda Sadeh 	req = __lookup_request(osdc, tid);
28193d14c5d2SYehuda Sadeh 	if (!req) {
28203d14c5d2SYehuda Sadeh 		*skip = 1;
28213d14c5d2SYehuda Sadeh 		m = NULL;
2822756a16a5SSage Weil 		dout("get_reply unknown tid %llu from osd%d\n", tid,
28233d14c5d2SYehuda Sadeh 		     osd->o_osd);
28243d14c5d2SYehuda Sadeh 		goto out;
28253d14c5d2SYehuda Sadeh 	}
28263d14c5d2SYehuda Sadeh 
2827ace6d3a9SAlex Elder 	if (req->r_reply->con)
28288921d114SAlex Elder 		dout("%s revoking msg %p from old con %p\n", __func__,
2829ace6d3a9SAlex Elder 		     req->r_reply, req->r_reply->con);
28308921d114SAlex Elder 	ceph_msg_revoke_incoming(req->r_reply);
28313d14c5d2SYehuda Sadeh 
2832f2be82b0SIlya Dryomov 	if (front_len > req->r_reply->front_alloc_len) {
2833b9a67899SJoe Perches 		pr_warn("get_reply front %d > preallocated %d (%u#%llu)\n",
2834f2be82b0SIlya Dryomov 			front_len, req->r_reply->front_alloc_len,
28354974341eSAlex Elder 			(unsigned int)con->peer_name.type,
28364974341eSAlex Elder 			le64_to_cpu(con->peer_name.num));
28373f0a4ac5SIlya Dryomov 		m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
28383f0a4ac5SIlya Dryomov 				 false);
28393d14c5d2SYehuda Sadeh 		if (!m)
28403d14c5d2SYehuda Sadeh 			goto out;
28413d14c5d2SYehuda Sadeh 		ceph_msg_put(req->r_reply);
28423d14c5d2SYehuda Sadeh 		req->r_reply = m;
28433d14c5d2SYehuda Sadeh 	}
28443d14c5d2SYehuda Sadeh 	m = ceph_msg_get(req->r_reply);
28453d14c5d2SYehuda Sadeh 
28463d14c5d2SYehuda Sadeh 	if (data_len > 0) {
2847a4ce40a9SAlex Elder 		struct ceph_osd_data *osd_data;
28480fff87ecSAlex Elder 
2849a4ce40a9SAlex Elder 		/*
2850a4ce40a9SAlex Elder 		 * XXX This is assuming there is only one op containing
2851a4ce40a9SAlex Elder 		 * XXX page data.  Probably OK for reads, but this
2852a4ce40a9SAlex Elder 		 * XXX ought to be done more generally.
2853a4ce40a9SAlex Elder 		 */
2854406e2c9fSAlex Elder 		osd_data = osd_req_op_extent_osd_data(req, 0);
28550fff87ecSAlex Elder 		if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
28560fff87ecSAlex Elder 			if (osd_data->pages &&
2857e0c59487SAlex Elder 				unlikely(osd_data->length < data_len)) {
28582ac2b7a6SAlex Elder 
2859b9a67899SJoe Perches 				pr_warn("tid %lld reply has %d bytes we had only %llu bytes ready\n",
2860e0c59487SAlex Elder 					tid, data_len, osd_data->length);
28613d14c5d2SYehuda Sadeh 				*skip = 1;
28623d14c5d2SYehuda Sadeh 				ceph_msg_put(m);
28633d14c5d2SYehuda Sadeh 				m = NULL;
28643d14c5d2SYehuda Sadeh 				goto out;
28653d14c5d2SYehuda Sadeh 			}
28663d14c5d2SYehuda Sadeh 		}
28672ac2b7a6SAlex Elder 	}
28683d14c5d2SYehuda Sadeh 	*skip = 0;
28693d14c5d2SYehuda Sadeh 	dout("get_reply tid %lld %p\n", tid, m);
28703d14c5d2SYehuda Sadeh 
28713d14c5d2SYehuda Sadeh out:
28723d14c5d2SYehuda Sadeh 	mutex_unlock(&osdc->request_mutex);
28733d14c5d2SYehuda Sadeh 	return m;
28743d14c5d2SYehuda Sadeh 
28753d14c5d2SYehuda Sadeh }
28763d14c5d2SYehuda Sadeh 
28773d14c5d2SYehuda Sadeh static struct ceph_msg *alloc_msg(struct ceph_connection *con,
28783d14c5d2SYehuda Sadeh 				  struct ceph_msg_header *hdr,
28793d14c5d2SYehuda Sadeh 				  int *skip)
28803d14c5d2SYehuda Sadeh {
28813d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
28823d14c5d2SYehuda Sadeh 	int type = le16_to_cpu(hdr->type);
28833d14c5d2SYehuda Sadeh 	int front = le32_to_cpu(hdr->front_len);
28843d14c5d2SYehuda Sadeh 
28851c20f2d2SAlex Elder 	*skip = 0;
28863d14c5d2SYehuda Sadeh 	switch (type) {
28873d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_MAP:
2888a40c4f10SYehuda Sadeh 	case CEPH_MSG_WATCH_NOTIFY:
2889b61c2763SSage Weil 		return ceph_msg_new(type, front, GFP_NOFS, false);
28903d14c5d2SYehuda Sadeh 	case CEPH_MSG_OSD_OPREPLY:
28913d14c5d2SYehuda Sadeh 		return get_reply(con, hdr, skip);
28923d14c5d2SYehuda Sadeh 	default:
28933d14c5d2SYehuda Sadeh 		pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
28943d14c5d2SYehuda Sadeh 			osd->o_osd);
28953d14c5d2SYehuda Sadeh 		*skip = 1;
28963d14c5d2SYehuda Sadeh 		return NULL;
28973d14c5d2SYehuda Sadeh 	}
28983d14c5d2SYehuda Sadeh }
28993d14c5d2SYehuda Sadeh 
29003d14c5d2SYehuda Sadeh /*
29013d14c5d2SYehuda Sadeh  * Wrappers to refcount containing ceph_osd struct
29023d14c5d2SYehuda Sadeh  */
29033d14c5d2SYehuda Sadeh static struct ceph_connection *get_osd_con(struct ceph_connection *con)
29043d14c5d2SYehuda Sadeh {
29053d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
29063d14c5d2SYehuda Sadeh 	if (get_osd(osd))
29073d14c5d2SYehuda Sadeh 		return con;
29083d14c5d2SYehuda Sadeh 	return NULL;
29093d14c5d2SYehuda Sadeh }
29103d14c5d2SYehuda Sadeh 
29113d14c5d2SYehuda Sadeh static void put_osd_con(struct ceph_connection *con)
29123d14c5d2SYehuda Sadeh {
29133d14c5d2SYehuda Sadeh 	struct ceph_osd *osd = con->private;
29143d14c5d2SYehuda Sadeh 	put_osd(osd);
29153d14c5d2SYehuda Sadeh }
29163d14c5d2SYehuda Sadeh 
29173d14c5d2SYehuda Sadeh /*
29183d14c5d2SYehuda Sadeh  * authentication
29193d14c5d2SYehuda Sadeh  */
2920a3530df3SAlex Elder /*
2921a3530df3SAlex Elder  * Note: returned pointer is the address of a structure that's
2922a3530df3SAlex Elder  * managed separately.  Caller must *not* attempt to free it.
2923a3530df3SAlex Elder  */
2924a3530df3SAlex Elder static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
29258f43fb53SAlex Elder 					int *proto, int force_new)
29263d14c5d2SYehuda Sadeh {
29273d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
29283d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
29293d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
293074f1869fSAlex Elder 	struct ceph_auth_handshake *auth = &o->o_auth;
29313d14c5d2SYehuda Sadeh 
293274f1869fSAlex Elder 	if (force_new && auth->authorizer) {
293327859f97SSage Weil 		ceph_auth_destroy_authorizer(ac, auth->authorizer);
293474f1869fSAlex Elder 		auth->authorizer = NULL;
29353d14c5d2SYehuda Sadeh 	}
293627859f97SSage Weil 	if (!auth->authorizer) {
293727859f97SSage Weil 		int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2938a3530df3SAlex Elder 						      auth);
29393d14c5d2SYehuda Sadeh 		if (ret)
2940a3530df3SAlex Elder 			return ERR_PTR(ret);
294127859f97SSage Weil 	} else {
294227859f97SSage Weil 		int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
29430bed9b5cSSage Weil 						     auth);
29440bed9b5cSSage Weil 		if (ret)
29450bed9b5cSSage Weil 			return ERR_PTR(ret);
29463d14c5d2SYehuda Sadeh 	}
29473d14c5d2SYehuda Sadeh 	*proto = ac->protocol;
294874f1869fSAlex Elder 
2949a3530df3SAlex Elder 	return auth;
29503d14c5d2SYehuda Sadeh }
29513d14c5d2SYehuda Sadeh 
29523d14c5d2SYehuda Sadeh 
29533d14c5d2SYehuda Sadeh static int verify_authorizer_reply(struct ceph_connection *con, int len)
29543d14c5d2SYehuda Sadeh {
29553d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
29563d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
29573d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
29583d14c5d2SYehuda Sadeh 
295927859f97SSage Weil 	return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
29603d14c5d2SYehuda Sadeh }
29613d14c5d2SYehuda Sadeh 
29623d14c5d2SYehuda Sadeh static int invalidate_authorizer(struct ceph_connection *con)
29633d14c5d2SYehuda Sadeh {
29643d14c5d2SYehuda Sadeh 	struct ceph_osd *o = con->private;
29653d14c5d2SYehuda Sadeh 	struct ceph_osd_client *osdc = o->o_osdc;
29663d14c5d2SYehuda Sadeh 	struct ceph_auth_client *ac = osdc->client->monc.auth;
29673d14c5d2SYehuda Sadeh 
296827859f97SSage Weil 	ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
29693d14c5d2SYehuda Sadeh 	return ceph_monc_validate_auth(&osdc->client->monc);
29703d14c5d2SYehuda Sadeh }
29713d14c5d2SYehuda Sadeh 
297233d07337SYan, Zheng static int sign_message(struct ceph_connection *con, struct ceph_msg *msg)
297333d07337SYan, Zheng {
297433d07337SYan, Zheng 	struct ceph_osd *o = con->private;
297533d07337SYan, Zheng 	struct ceph_auth_handshake *auth = &o->o_auth;
297633d07337SYan, Zheng 	return ceph_auth_sign_message(auth, msg);
297733d07337SYan, Zheng }
297833d07337SYan, Zheng 
297933d07337SYan, Zheng static int check_message_signature(struct ceph_connection *con, struct ceph_msg *msg)
298033d07337SYan, Zheng {
298133d07337SYan, Zheng 	struct ceph_osd *o = con->private;
298233d07337SYan, Zheng 	struct ceph_auth_handshake *auth = &o->o_auth;
298333d07337SYan, Zheng 	return ceph_auth_check_message_signature(auth, msg);
298433d07337SYan, Zheng }
298533d07337SYan, Zheng 
29863d14c5d2SYehuda Sadeh static const struct ceph_connection_operations osd_con_ops = {
29873d14c5d2SYehuda Sadeh 	.get = get_osd_con,
29883d14c5d2SYehuda Sadeh 	.put = put_osd_con,
29893d14c5d2SYehuda Sadeh 	.dispatch = dispatch,
29903d14c5d2SYehuda Sadeh 	.get_authorizer = get_authorizer,
29913d14c5d2SYehuda Sadeh 	.verify_authorizer_reply = verify_authorizer_reply,
29923d14c5d2SYehuda Sadeh 	.invalidate_authorizer = invalidate_authorizer,
29933d14c5d2SYehuda Sadeh 	.alloc_msg = alloc_msg,
299433d07337SYan, Zheng 	.sign_message = sign_message,
299533d07337SYan, Zheng 	.check_message_signature = check_message_signature,
29963d14c5d2SYehuda Sadeh 	.fault = osd_reset,
29973d14c5d2SYehuda Sadeh };
2998