blob: a528ea34253a404a32e2056b12ecbe798edcf6c1 [file] [log] [blame]
Alex Eldera4ce40a2013-04-05 01:27:12 -05001
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Sage Weilf24e9982009-10-06 11:31:10 -07003
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004#include <linux/module.h>
Sage Weilf24e9982009-10-06 11:31:10 -07005#include <linux/err.h>
6#include <linux/highmem.h>
7#include <linux/mm.h>
8#include <linux/pagemap.h>
9#include <linux/slab.h>
10#include <linux/uaccess.h>
Yehuda Sadeh68b44762010-04-06 15:01:27 -070011#ifdef CONFIG_BLOCK
12#include <linux/bio.h>
13#endif
Sage Weilf24e9982009-10-06 11:31:10 -070014
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070015#include <linux/ceph/libceph.h>
16#include <linux/ceph/osd_client.h>
17#include <linux/ceph/messenger.h>
18#include <linux/ceph/decode.h>
19#include <linux/ceph/auth.h>
20#include <linux/ceph/pagelist.h>
Sage Weilf24e9982009-10-06 11:31:10 -070021
Sage Weilc16e7862010-03-01 13:02:00 -080022#define OSD_OP_FRONT_LEN 4096
23#define OSD_OPREPLY_FRONT_LEN 512
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -080024
Alex Elder5522ae02013-05-01 12:43:04 -050025static struct kmem_cache *ceph_osd_request_cache;
26
Tobias Klauser9e327892010-05-20 10:40:19 +020027static const struct ceph_connection_operations osd_con_ops;
Sage Weilf24e9982009-10-06 11:31:10 -070028
Alex Elderf9d25192013-02-15 11:42:29 -060029static void __send_queued(struct ceph_osd_client *osdc);
Sage Weil6f6c7002011-01-17 20:34:08 -080030static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -070031static void __register_request(struct ceph_osd_client *osdc,
32 struct ceph_osd_request *req);
Ilya Dryomov2cc61282014-09-03 14:41:45 +040033static void __unregister_request(struct ceph_osd_client *osdc,
34 struct ceph_osd_request *req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -070035static void __unregister_linger_request(struct ceph_osd_client *osdc,
36 struct ceph_osd_request *req);
Ilya Dryomov2cc61282014-09-03 14:41:45 +040037static void __enqueue_request(struct ceph_osd_request *req);
Sage Weil56e925b2012-01-03 12:34:34 -080038static void __send_request(struct ceph_osd_client *osdc,
39 struct ceph_osd_request *req);
Sage Weilf24e9982009-10-06 11:31:10 -070040
41/*
42 * Implement client access to distributed object storage cluster.
43 *
44 * All data objects are stored within a cluster/cloud of OSDs, or
45 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
46 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
47 * remote daemons serving up and coordinating consistent and safe
48 * access to storage.
49 *
50 * Cluster membership and the mapping of data objects onto storage devices
51 * are described by the osd map.
52 *
53 * We keep track of pending OSD requests (read, write), resubmit
54 * requests to different OSDs when the cluster topology/data layout
55 * change, or retry the affected requests when the communications
56 * channel with an OSD is reset.
57 */
58
59/*
60 * calculate the mapping of a file extent onto an object, and fill out the
61 * request accordingly. shorten extent as necessary if it crosses an
62 * object boundary.
63 *
64 * fill osd op in request message.
65 */
Alex Elderdbe0fc42013-02-15 22:10:17 -060066static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
Alex Eldera19dadf2013-03-13 20:50:01 -050067 u64 *objnum, u64 *objoff, u64 *objlen)
Sage Weilf24e9982009-10-06 11:31:10 -070068{
Alex Elder60e56f12013-02-15 11:42:29 -060069 u64 orig_len = *plen;
Sage Weild63b77f2012-09-24 20:59:48 -070070 int r;
Sage Weilf24e9982009-10-06 11:31:10 -070071
Alex Elder60e56f12013-02-15 11:42:29 -060072 /* object extent? */
Alex Elder75d1c942013-03-13 20:50:00 -050073 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
74 objoff, objlen);
Sage Weild63b77f2012-09-24 20:59:48 -070075 if (r < 0)
76 return r;
Alex Elder75d1c942013-03-13 20:50:00 -050077 if (*objlen < orig_len) {
78 *plen = *objlen;
Alex Elder60e56f12013-02-15 11:42:29 -060079 dout(" skipping last %llu, final file extent %llu~%llu\n",
80 orig_len - *plen, off, *plen);
81 }
82
Alex Elder75d1c942013-03-13 20:50:00 -050083 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
Sage Weilf24e9982009-10-06 11:31:10 -070084
Alex Elder3ff5f382013-02-15 22:10:17 -060085 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -070086}
87
Alex Elderc54d47b2013-04-03 01:28:57 -050088static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
89{
90 memset(osd_data, 0, sizeof (*osd_data));
91 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
92}
93
Alex Eldera4ce40a2013-04-05 01:27:12 -050094static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -050095 struct page **pages, u64 length, u32 alignment,
96 bool pages_from_pool, bool own_pages)
97{
98 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
99 osd_data->pages = pages;
100 osd_data->length = length;
101 osd_data->alignment = alignment;
102 osd_data->pages_from_pool = pages_from_pool;
103 osd_data->own_pages = own_pages;
104}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500105
Alex Eldera4ce40a2013-04-05 01:27:12 -0500106static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500107 struct ceph_pagelist *pagelist)
108{
109 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
110 osd_data->pagelist = pagelist;
111}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500112
113#ifdef CONFIG_BLOCK
Alex Eldera4ce40a2013-04-05 01:27:12 -0500114static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500115 struct bio *bio, size_t bio_length)
116{
117 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
118 osd_data->bio = bio;
119 osd_data->bio_length = bio_length;
120}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500121#endif /* CONFIG_BLOCK */
122
Alex Elder863c7eb2013-04-15 14:50:36 -0500123#define osd_req_op_data(oreq, whch, typ, fld) \
124 ({ \
125 BUG_ON(whch >= (oreq)->r_num_ops); \
126 &(oreq)->r_ops[whch].typ.fld; \
127 })
128
Alex Elder49719772013-02-11 12:33:24 -0600129static struct ceph_osd_data *
130osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
131{
132 BUG_ON(which >= osd_req->r_num_ops);
133
134 return &osd_req->r_ops[which].raw_data_in;
135}
136
Alex Eldera4ce40a2013-04-05 01:27:12 -0500137struct ceph_osd_data *
138osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500139 unsigned int which)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500140{
Alex Elder863c7eb2013-04-15 14:50:36 -0500141 return osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500142}
143EXPORT_SYMBOL(osd_req_op_extent_osd_data);
144
145struct ceph_osd_data *
Alex Eldera4ce40a2013-04-05 01:27:12 -0500146osd_req_op_cls_response_data(struct ceph_osd_request *osd_req,
147 unsigned int which)
148{
Alex Elder863c7eb2013-04-15 14:50:36 -0500149 return osd_req_op_data(osd_req, which, cls, response_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500150}
151EXPORT_SYMBOL(osd_req_op_cls_response_data); /* ??? */
152
Alex Elder49719772013-02-11 12:33:24 -0600153void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
154 unsigned int which, struct page **pages,
155 u64 length, u32 alignment,
156 bool pages_from_pool, bool own_pages)
157{
158 struct ceph_osd_data *osd_data;
159
160 osd_data = osd_req_op_raw_data_in(osd_req, which);
161 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
162 pages_from_pool, own_pages);
163}
164EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
165
Alex Eldera4ce40a2013-04-05 01:27:12 -0500166void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500167 unsigned int which, struct page **pages,
168 u64 length, u32 alignment,
Alex Eldera4ce40a2013-04-05 01:27:12 -0500169 bool pages_from_pool, bool own_pages)
170{
171 struct ceph_osd_data *osd_data;
172
Alex Elder863c7eb2013-04-15 14:50:36 -0500173 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500174 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
175 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500176}
177EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
178
179void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500180 unsigned int which, struct ceph_pagelist *pagelist)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500181{
182 struct ceph_osd_data *osd_data;
183
Alex Elder863c7eb2013-04-15 14:50:36 -0500184 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500185 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500186}
187EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
188
189#ifdef CONFIG_BLOCK
190void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500191 unsigned int which, struct bio *bio, size_t bio_length)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500192{
193 struct ceph_osd_data *osd_data;
Alex Elder863c7eb2013-04-15 14:50:36 -0500194
195 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500196 ceph_osd_data_bio_init(osd_data, bio, bio_length);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500197}
198EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
199#endif /* CONFIG_BLOCK */
200
201static void osd_req_op_cls_request_info_pagelist(
202 struct ceph_osd_request *osd_req,
203 unsigned int which, struct ceph_pagelist *pagelist)
204{
205 struct ceph_osd_data *osd_data;
206
Alex Elder863c7eb2013-04-15 14:50:36 -0500207 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500208 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500209}
210
Alex Elder04017e22013-04-05 14:46:02 -0500211void osd_req_op_cls_request_data_pagelist(
212 struct ceph_osd_request *osd_req,
213 unsigned int which, struct ceph_pagelist *pagelist)
214{
215 struct ceph_osd_data *osd_data;
216
Alex Elder863c7eb2013-04-15 14:50:36 -0500217 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
Alex Elder04017e22013-04-05 14:46:02 -0500218 ceph_osd_data_pagelist_init(osd_data, pagelist);
219}
220EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
221
Alex Elder6c57b552013-04-19 15:34:49 -0500222void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
223 unsigned int which, struct page **pages, u64 length,
224 u32 alignment, bool pages_from_pool, bool own_pages)
225{
226 struct ceph_osd_data *osd_data;
227
228 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
229 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
230 pages_from_pool, own_pages);
231}
232EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
233
Alex Eldera4ce40a2013-04-05 01:27:12 -0500234void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
235 unsigned int which, struct page **pages, u64 length,
236 u32 alignment, bool pages_from_pool, bool own_pages)
237{
238 struct ceph_osd_data *osd_data;
239
Alex Elder863c7eb2013-04-15 14:50:36 -0500240 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500241 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
242 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500243}
244EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
245
Alex Elder23c08a9cb2013-04-03 01:28:58 -0500246static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
247{
248 switch (osd_data->type) {
249 case CEPH_OSD_DATA_TYPE_NONE:
250 return 0;
251 case CEPH_OSD_DATA_TYPE_PAGES:
252 return osd_data->length;
253 case CEPH_OSD_DATA_TYPE_PAGELIST:
254 return (u64)osd_data->pagelist->length;
255#ifdef CONFIG_BLOCK
256 case CEPH_OSD_DATA_TYPE_BIO:
257 return (u64)osd_data->bio_length;
258#endif /* CONFIG_BLOCK */
259 default:
260 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
261 return 0;
262 }
263}
264
Alex Elderc54d47b2013-04-03 01:28:57 -0500265static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
266{
Alex Elder54764922013-04-05 01:27:12 -0500267 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
Alex Elderc54d47b2013-04-03 01:28:57 -0500268 int num_pages;
269
270 num_pages = calc_pages_for((u64)osd_data->alignment,
271 (u64)osd_data->length);
272 ceph_release_page_vector(osd_data->pages, num_pages);
273 }
Alex Elder54764922013-04-05 01:27:12 -0500274 ceph_osd_data_init(osd_data);
275}
276
277static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
278 unsigned int which)
279{
280 struct ceph_osd_req_op *op;
281
282 BUG_ON(which >= osd_req->r_num_ops);
283 op = &osd_req->r_ops[which];
284
285 switch (op->op) {
286 case CEPH_OSD_OP_READ:
287 case CEPH_OSD_OP_WRITE:
288 ceph_osd_data_release(&op->extent.osd_data);
289 break;
290 case CEPH_OSD_OP_CALL:
291 ceph_osd_data_release(&op->cls.request_info);
Alex Elder04017e22013-04-05 14:46:02 -0500292 ceph_osd_data_release(&op->cls.request_data);
Alex Elder54764922013-04-05 01:27:12 -0500293 ceph_osd_data_release(&op->cls.response_data);
294 break;
295 default:
296 break;
297 }
Alex Elderc54d47b2013-04-03 01:28:57 -0500298}
299
Sage Weilf24e9982009-10-06 11:31:10 -0700300/*
301 * requests
302 */
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400303static void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700304{
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400305 struct ceph_osd_request *req = container_of(kref,
306 struct ceph_osd_request, r_kref);
Alex Elder54764922013-04-05 01:27:12 -0500307 unsigned int which;
Sage Weil415e49a2009-12-07 13:37:03 -0800308
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400309 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
310 req->r_request, req->r_reply);
Ilya Dryomov6562d662014-06-20 14:14:42 +0400311 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
312 WARN_ON(!list_empty(&req->r_req_lru_item));
313 WARN_ON(!list_empty(&req->r_osd_item));
314 WARN_ON(!list_empty(&req->r_linger_item));
315 WARN_ON(!list_empty(&req->r_linger_osd_item));
316 WARN_ON(req->r_osd);
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400317
Sage Weil415e49a2009-12-07 13:37:03 -0800318 if (req->r_request)
319 ceph_msg_put(req->r_request);
Alex Elderace6d3a2013-04-01 16:12:14 -0500320 if (req->r_reply) {
Alex Elder8921d112012-06-01 14:56:43 -0500321 ceph_msg_revoke_incoming(req->r_reply);
Alex Elderab8cb342012-06-04 14:43:32 -0500322 ceph_msg_put(req->r_reply);
Alex Elderace6d3a2013-04-01 16:12:14 -0500323 }
Alex Elder0fff87e2013-02-14 12:16:43 -0600324
Alex Elder54764922013-04-05 01:27:12 -0500325 for (which = 0; which < req->r_num_ops; which++)
326 osd_req_op_data_release(req, which);
Alex Elder0fff87e2013-02-14 12:16:43 -0600327
Sage Weil415e49a2009-12-07 13:37:03 -0800328 ceph_put_snap_context(req->r_snapc);
329 if (req->r_mempool)
330 mempool_free(req, req->r_osdc->req_mempool);
331 else
Alex Elder5522ae02013-05-01 12:43:04 -0500332 kmem_cache_free(ceph_osd_request_cache, req);
333
Sage Weilf24e9982009-10-06 11:31:10 -0700334}
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400335
336void ceph_osdc_get_request(struct ceph_osd_request *req)
337{
338 dout("%s %p (was %d)\n", __func__, req,
339 atomic_read(&req->r_kref.refcount));
340 kref_get(&req->r_kref);
341}
342EXPORT_SYMBOL(ceph_osdc_get_request);
343
344void ceph_osdc_put_request(struct ceph_osd_request *req)
345{
346 dout("%s %p (was %d)\n", __func__, req,
347 atomic_read(&req->r_kref.refcount));
348 kref_put(&req->r_kref, ceph_osdc_release_request);
349}
350EXPORT_SYMBOL(ceph_osdc_put_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700351
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700352struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700353 struct ceph_snap_context *snapc,
Sage Weil1b83bef2013-02-25 16:11:12 -0800354 unsigned int num_ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700355 bool use_mempool,
Alex Elder54a54002012-11-13 21:11:15 -0600356 gfp_t gfp_flags)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700357{
358 struct ceph_osd_request *req;
359 struct ceph_msg *msg;
Sage Weil1b83bef2013-02-25 16:11:12 -0800360 size_t msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700361
Alex Elder79528732013-04-03 21:32:51 -0500362 BUILD_BUG_ON(CEPH_OSD_MAX_OP > U16_MAX);
363 BUG_ON(num_ops > CEPH_OSD_MAX_OP);
364
Sage Weil1b83bef2013-02-25 16:11:12 -0800365 msg_size = 4 + 4 + 8 + 8 + 4+8;
366 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
367 msg_size += 1 + 8 + 4 + 4; /* pg_t */
Ilya Dryomov2d0ebc52014-01-27 17:40:18 +0200368 msg_size += 4 + CEPH_MAX_OID_NAME_LEN; /* oid */
Sage Weil1b83bef2013-02-25 16:11:12 -0800369 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
370 msg_size += 8; /* snapid */
371 msg_size += 8; /* snap_seq */
372 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
373 msg_size += 4;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700374
375 if (use_mempool) {
376 req = mempool_alloc(osdc->req_mempool, gfp_flags);
377 memset(req, 0, sizeof(*req));
378 } else {
Alex Elder5522ae02013-05-01 12:43:04 -0500379 req = kmem_cache_zalloc(ceph_osd_request_cache, gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700380 }
381 if (req == NULL)
382 return NULL;
383
384 req->r_osdc = osdc;
385 req->r_mempool = use_mempool;
Alex Elder79528732013-04-03 21:32:51 -0500386 req->r_num_ops = num_ops;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700387
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700388 kref_init(&req->r_kref);
389 init_completion(&req->r_completion);
390 init_completion(&req->r_safe_completion);
Alex Eldera978fa22012-12-17 12:23:48 -0600391 RB_CLEAR_NODE(&req->r_node);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700392 INIT_LIST_HEAD(&req->r_unsafe_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700393 INIT_LIST_HEAD(&req->r_linger_item);
Ilya Dryomov1d0326b2014-06-20 14:14:41 +0400394 INIT_LIST_HEAD(&req->r_linger_osd_item);
Sage Weil935b6392011-09-16 11:13:17 -0700395 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilcd430452012-07-09 14:31:41 -0700396 INIT_LIST_HEAD(&req->r_osd_item);
397
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200398 req->r_base_oloc.pool = -1;
Ilya Dryomov205ee1182014-01-27 17:40:20 +0200399 req->r_target_oloc.pool = -1;
Ilya Dryomov221165252014-01-27 17:40:18 +0200400
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700401 /* create reply message */
402 if (use_mempool)
403 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
404 else
405 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
Sage Weilb61c2762011-08-09 15:03:46 -0700406 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700407 if (!msg) {
408 ceph_osdc_put_request(req);
409 return NULL;
410 }
411 req->r_reply = msg;
412
413 /* create request message; allow space for oid */
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700414 if (use_mempool)
415 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
416 else
Sage Weilb61c2762011-08-09 15:03:46 -0700417 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700418 if (!msg) {
419 ceph_osdc_put_request(req);
420 return NULL;
421 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700422
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700423 memset(msg->front.iov_base, 0, msg->front.iov_len);
424
425 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700426
427 return req;
428}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700429EXPORT_SYMBOL(ceph_osdc_alloc_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700430
Alex Eldera8dd0a32013-03-13 20:50:00 -0500431static bool osd_req_opcode_valid(u16 opcode)
432{
433 switch (opcode) {
434 case CEPH_OSD_OP_READ:
435 case CEPH_OSD_OP_STAT:
436 case CEPH_OSD_OP_MAPEXT:
437 case CEPH_OSD_OP_MASKTRUNC:
438 case CEPH_OSD_OP_SPARSE_READ:
439 case CEPH_OSD_OP_NOTIFY:
440 case CEPH_OSD_OP_NOTIFY_ACK:
441 case CEPH_OSD_OP_ASSERT_VER:
442 case CEPH_OSD_OP_WRITE:
443 case CEPH_OSD_OP_WRITEFULL:
444 case CEPH_OSD_OP_TRUNCATE:
445 case CEPH_OSD_OP_ZERO:
446 case CEPH_OSD_OP_DELETE:
447 case CEPH_OSD_OP_APPEND:
448 case CEPH_OSD_OP_STARTSYNC:
449 case CEPH_OSD_OP_SETTRUNC:
450 case CEPH_OSD_OP_TRIMTRUNC:
451 case CEPH_OSD_OP_TMAPUP:
452 case CEPH_OSD_OP_TMAPPUT:
453 case CEPH_OSD_OP_TMAPGET:
454 case CEPH_OSD_OP_CREATE:
455 case CEPH_OSD_OP_ROLLBACK:
456 case CEPH_OSD_OP_WATCH:
457 case CEPH_OSD_OP_OMAPGETKEYS:
458 case CEPH_OSD_OP_OMAPGETVALS:
459 case CEPH_OSD_OP_OMAPGETHEADER:
460 case CEPH_OSD_OP_OMAPGETVALSBYKEYS:
461 case CEPH_OSD_OP_OMAPSETVALS:
462 case CEPH_OSD_OP_OMAPSETHEADER:
463 case CEPH_OSD_OP_OMAPCLEAR:
464 case CEPH_OSD_OP_OMAPRMKEYS:
465 case CEPH_OSD_OP_OMAP_CMP:
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200466 case CEPH_OSD_OP_SETALLOCHINT:
Alex Eldera8dd0a32013-03-13 20:50:00 -0500467 case CEPH_OSD_OP_CLONERANGE:
468 case CEPH_OSD_OP_ASSERT_SRC_VERSION:
469 case CEPH_OSD_OP_SRC_CMPXATTR:
470 case CEPH_OSD_OP_GETXATTR:
471 case CEPH_OSD_OP_GETXATTRS:
472 case CEPH_OSD_OP_CMPXATTR:
473 case CEPH_OSD_OP_SETXATTR:
474 case CEPH_OSD_OP_SETXATTRS:
475 case CEPH_OSD_OP_RESETXATTRS:
476 case CEPH_OSD_OP_RMXATTR:
477 case CEPH_OSD_OP_PULL:
478 case CEPH_OSD_OP_PUSH:
479 case CEPH_OSD_OP_BALANCEREADS:
480 case CEPH_OSD_OP_UNBALANCEREADS:
481 case CEPH_OSD_OP_SCRUB:
482 case CEPH_OSD_OP_SCRUB_RESERVE:
483 case CEPH_OSD_OP_SCRUB_UNRESERVE:
484 case CEPH_OSD_OP_SCRUB_STOP:
485 case CEPH_OSD_OP_SCRUB_MAP:
486 case CEPH_OSD_OP_WRLOCK:
487 case CEPH_OSD_OP_WRUNLOCK:
488 case CEPH_OSD_OP_RDLOCK:
489 case CEPH_OSD_OP_RDUNLOCK:
490 case CEPH_OSD_OP_UPLOCK:
491 case CEPH_OSD_OP_DNLOCK:
492 case CEPH_OSD_OP_CALL:
493 case CEPH_OSD_OP_PGLS:
494 case CEPH_OSD_OP_PGLS_FILTER:
495 return true;
496 default:
497 return false;
498 }
499}
500
Alex Elder33803f32013-03-13 20:50:00 -0500501/*
502 * This is an osd op init function for opcodes that have no data or
503 * other information associated with them. It also serves as a
504 * common init routine for all the other init functions, below.
505 */
Alex Elderc99d2d42013-04-05 01:27:11 -0500506static struct ceph_osd_req_op *
Alex Elder49719772013-02-11 12:33:24 -0600507_osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elderc99d2d42013-04-05 01:27:11 -0500508 u16 opcode)
Alex Elder33803f32013-03-13 20:50:00 -0500509{
Alex Elderc99d2d42013-04-05 01:27:11 -0500510 struct ceph_osd_req_op *op;
511
512 BUG_ON(which >= osd_req->r_num_ops);
Alex Elder33803f32013-03-13 20:50:00 -0500513 BUG_ON(!osd_req_opcode_valid(opcode));
514
Alex Elderc99d2d42013-04-05 01:27:11 -0500515 op = &osd_req->r_ops[which];
Alex Elder33803f32013-03-13 20:50:00 -0500516 memset(op, 0, sizeof (*op));
Alex Elder33803f32013-03-13 20:50:00 -0500517 op->op = opcode;
Alex Elderc99d2d42013-04-05 01:27:11 -0500518
519 return op;
Alex Elder33803f32013-03-13 20:50:00 -0500520}
521
Alex Elder49719772013-02-11 12:33:24 -0600522void osd_req_op_init(struct ceph_osd_request *osd_req,
523 unsigned int which, u16 opcode)
524{
525 (void)_osd_req_op_init(osd_req, which, opcode);
526}
527EXPORT_SYMBOL(osd_req_op_init);
528
Alex Elderc99d2d42013-04-05 01:27:11 -0500529void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
530 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500531 u64 offset, u64 length,
532 u64 truncate_size, u32 truncate_seq)
533{
Alex Elder49719772013-02-11 12:33:24 -0600534 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
Alex Elder33803f32013-03-13 20:50:00 -0500535 size_t payload_len = 0;
536
Li Wangad7a60d2013-08-15 11:51:44 +0800537 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
538 opcode != CEPH_OSD_OP_DELETE && opcode != CEPH_OSD_OP_ZERO &&
539 opcode != CEPH_OSD_OP_TRUNCATE);
Alex Elder33803f32013-03-13 20:50:00 -0500540
Alex Elder33803f32013-03-13 20:50:00 -0500541 op->extent.offset = offset;
542 op->extent.length = length;
543 op->extent.truncate_size = truncate_size;
544 op->extent.truncate_seq = truncate_seq;
545 if (opcode == CEPH_OSD_OP_WRITE)
546 payload_len += length;
547
548 op->payload_len = payload_len;
549}
550EXPORT_SYMBOL(osd_req_op_extent_init);
551
Alex Elderc99d2d42013-04-05 01:27:11 -0500552void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
553 unsigned int which, u64 length)
Alex Eldere5975c72013-03-14 14:09:05 -0500554{
Alex Elderc99d2d42013-04-05 01:27:11 -0500555 struct ceph_osd_req_op *op;
556 u64 previous;
557
558 BUG_ON(which >= osd_req->r_num_ops);
559 op = &osd_req->r_ops[which];
560 previous = op->extent.length;
Alex Eldere5975c72013-03-14 14:09:05 -0500561
562 if (length == previous)
563 return; /* Nothing to do */
564 BUG_ON(length > previous);
565
566 op->extent.length = length;
567 op->payload_len -= previous - length;
568}
569EXPORT_SYMBOL(osd_req_op_extent_update);
570
Alex Elderc99d2d42013-04-05 01:27:11 -0500571void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elder04017e22013-04-05 14:46:02 -0500572 u16 opcode, const char *class, const char *method)
Alex Elder33803f32013-03-13 20:50:00 -0500573{
Alex Elder49719772013-02-11 12:33:24 -0600574 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
Alex Elder5f562df2013-04-05 01:27:12 -0500575 struct ceph_pagelist *pagelist;
Alex Elder33803f32013-03-13 20:50:00 -0500576 size_t payload_len = 0;
577 size_t size;
578
579 BUG_ON(opcode != CEPH_OSD_OP_CALL);
580
Alex Elder5f562df2013-04-05 01:27:12 -0500581 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
582 BUG_ON(!pagelist);
583 ceph_pagelist_init(pagelist);
584
Alex Elder33803f32013-03-13 20:50:00 -0500585 op->cls.class_name = class;
586 size = strlen(class);
587 BUG_ON(size > (size_t) U8_MAX);
588 op->cls.class_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500589 ceph_pagelist_append(pagelist, class, size);
Alex Elder33803f32013-03-13 20:50:00 -0500590 payload_len += size;
591
592 op->cls.method_name = method;
593 size = strlen(method);
594 BUG_ON(size > (size_t) U8_MAX);
595 op->cls.method_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500596 ceph_pagelist_append(pagelist, method, size);
Alex Elder33803f32013-03-13 20:50:00 -0500597 payload_len += size;
598
Alex Eldera4ce40a2013-04-05 01:27:12 -0500599 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
Alex Elder5f562df2013-04-05 01:27:12 -0500600
Alex Elder33803f32013-03-13 20:50:00 -0500601 op->cls.argc = 0; /* currently unused */
602
603 op->payload_len = payload_len;
604}
605EXPORT_SYMBOL(osd_req_op_cls_init);
Alex Elder8c042b02013-04-03 01:28:58 -0500606
Alex Elderc99d2d42013-04-05 01:27:11 -0500607void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
608 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500609 u64 cookie, u64 version, int flag)
610{
Alex Elder49719772013-02-11 12:33:24 -0600611 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
Alex Elder33803f32013-03-13 20:50:00 -0500612
Alex Elderc99d2d42013-04-05 01:27:11 -0500613 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
Alex Elder33803f32013-03-13 20:50:00 -0500614
615 op->watch.cookie = cookie;
Alex Elder9ef1ee52013-04-21 16:51:50 -0500616 op->watch.ver = version;
Alex Elder33803f32013-03-13 20:50:00 -0500617 if (opcode == CEPH_OSD_OP_WATCH && flag)
Alex Elderc99d2d42013-04-05 01:27:11 -0500618 op->watch.flag = (u8)1;
Alex Elder33803f32013-03-13 20:50:00 -0500619}
620EXPORT_SYMBOL(osd_req_op_watch_init);
621
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200622void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
623 unsigned int which,
624 u64 expected_object_size,
625 u64 expected_write_size)
626{
627 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
628 CEPH_OSD_OP_SETALLOCHINT);
629
630 op->alloc_hint.expected_object_size = expected_object_size;
631 op->alloc_hint.expected_write_size = expected_write_size;
632
633 /*
634 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
635 * not worth a feature bit. Set FAILOK per-op flag to make
636 * sure older osds don't trip over an unsupported opcode.
637 */
638 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
639}
640EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
641
Alex Elder90af3602013-04-05 14:46:01 -0500642static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
Alex Elderec9123c2013-04-05 01:27:12 -0500643 struct ceph_osd_data *osd_data)
644{
645 u64 length = ceph_osd_data_length(osd_data);
646
647 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
648 BUG_ON(length > (u64) SIZE_MAX);
649 if (length)
Alex Elder90af3602013-04-05 14:46:01 -0500650 ceph_msg_data_add_pages(msg, osd_data->pages,
Alex Elderec9123c2013-04-05 01:27:12 -0500651 length, osd_data->alignment);
652 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
653 BUG_ON(!length);
Alex Elder90af3602013-04-05 14:46:01 -0500654 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
Alex Elderec9123c2013-04-05 01:27:12 -0500655#ifdef CONFIG_BLOCK
656 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
Alex Elder90af3602013-04-05 14:46:01 -0500657 ceph_msg_data_add_bio(msg, osd_data->bio, length);
Alex Elderec9123c2013-04-05 01:27:12 -0500658#endif
659 } else {
660 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
661 }
662}
663
Alex Elder175face2013-03-08 13:35:36 -0600664static u64 osd_req_encode_op(struct ceph_osd_request *req,
Alex Elder79528732013-04-03 21:32:51 -0500665 struct ceph_osd_op *dst, unsigned int which)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700666{
Alex Elder79528732013-04-03 21:32:51 -0500667 struct ceph_osd_req_op *src;
Alex Elder04017e22013-04-05 14:46:02 -0500668 struct ceph_osd_data *osd_data;
Alex Elder54d50642013-04-03 01:28:58 -0500669 u64 request_data_len = 0;
Alex Elder04017e22013-04-05 14:46:02 -0500670 u64 data_length;
Alex Elder175face2013-03-08 13:35:36 -0600671
Alex Elder79528732013-04-03 21:32:51 -0500672 BUG_ON(which >= req->r_num_ops);
673 src = &req->r_ops[which];
Alex Eldera8dd0a32013-03-13 20:50:00 -0500674 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
675 pr_err("unrecognized osd opcode %d\n", src->op);
676
677 return 0;
678 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700679
Alex Elder065a68f2012-04-20 15:49:43 -0500680 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600681 case CEPH_OSD_OP_STAT:
Alex Elder49719772013-02-11 12:33:24 -0600682 osd_data = &src->raw_data_in;
683 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Alex Elderfbfab532013-02-08 09:55:48 -0600684 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700685 case CEPH_OSD_OP_READ:
686 case CEPH_OSD_OP_WRITE:
Li Wangad7a60d2013-08-15 11:51:44 +0800687 case CEPH_OSD_OP_ZERO:
688 case CEPH_OSD_OP_DELETE:
689 case CEPH_OSD_OP_TRUNCATE:
Alex Elder175face2013-03-08 13:35:36 -0600690 if (src->op == CEPH_OSD_OP_WRITE)
Alex Elder54d50642013-04-03 01:28:58 -0500691 request_data_len = src->extent.length;
Alex Elder175face2013-03-08 13:35:36 -0600692 dst->extent.offset = cpu_to_le64(src->extent.offset);
693 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700694 dst->extent.truncate_size =
695 cpu_to_le64(src->extent.truncate_size);
696 dst->extent.truncate_seq =
697 cpu_to_le32(src->extent.truncate_seq);
Alex Elder04017e22013-04-05 14:46:02 -0500698 osd_data = &src->extent.osd_data;
Alex Elder54764922013-04-05 01:27:12 -0500699 if (src->op == CEPH_OSD_OP_WRITE)
Alex Elder04017e22013-04-05 14:46:02 -0500700 ceph_osdc_msg_data_add(req->r_request, osd_data);
Alex Elder54764922013-04-05 01:27:12 -0500701 else
Alex Elder04017e22013-04-05 14:46:02 -0500702 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700703 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700704 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700705 dst->cls.class_len = src->cls.class_len;
706 dst->cls.method_len = src->cls.method_len;
Alex Elder04017e22013-04-05 14:46:02 -0500707 osd_data = &src->cls.request_info;
708 ceph_osdc_msg_data_add(req->r_request, osd_data);
709 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
710 request_data_len = osd_data->pagelist->length;
711
712 osd_data = &src->cls.request_data;
713 data_length = ceph_osd_data_length(osd_data);
714 if (data_length) {
715 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
716 dst->cls.indata_len = cpu_to_le32(data_length);
717 ceph_osdc_msg_data_add(req->r_request, osd_data);
718 src->payload_len += data_length;
719 request_data_len += data_length;
720 }
721 osd_data = &src->cls.response_data;
722 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700723 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700724 case CEPH_OSD_OP_STARTSYNC:
725 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700726 case CEPH_OSD_OP_NOTIFY_ACK:
727 case CEPH_OSD_OP_WATCH:
728 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
729 dst->watch.ver = cpu_to_le64(src->watch.ver);
730 dst->watch.flag = src->watch.flag;
731 break;
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200732 case CEPH_OSD_OP_SETALLOCHINT:
733 dst->alloc_hint.expected_object_size =
734 cpu_to_le64(src->alloc_hint.expected_object_size);
735 dst->alloc_hint.expected_write_size =
736 cpu_to_le64(src->alloc_hint.expected_write_size);
737 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700738 default:
Alex Elder4c464592013-02-15 11:42:30 -0600739 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600740 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600741 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -0500742
743 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700744 }
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200745
Alex Eldera8dd0a32013-03-13 20:50:00 -0500746 dst->op = cpu_to_le16(src->op);
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200747 dst->flags = cpu_to_le32(src->flags);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700748 dst->payload_len = cpu_to_le32(src->payload_len);
Alex Elder175face2013-03-08 13:35:36 -0600749
Alex Elder54d50642013-04-03 01:28:58 -0500750 return request_data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700751}
752
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700753/*
Sage Weilf24e9982009-10-06 11:31:10 -0700754 * build new request AND message, calculate layout, and adjust file
755 * extent as needed.
756 *
757 * if the file was recently truncated, we include information about its
758 * old and new size so that the object can be updated appropriately. (we
759 * avoid synchronously deleting truncated objects because it's slow.)
760 *
761 * if @do_sync, include a 'startsync' command so that the osd will flush
762 * data quickly.
763 */
764struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
765 struct ceph_file_layout *layout,
766 struct ceph_vino vino,
Alex Elderacead002013-03-14 14:09:05 -0500767 u64 off, u64 *plen, int num_ops,
Sage Weilf24e9982009-10-06 11:31:10 -0700768 int opcode, int flags,
769 struct ceph_snap_context *snapc,
Sage Weilf24e9982009-10-06 11:31:10 -0700770 u32 truncate_seq,
771 u64 truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -0600772 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700773{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700774 struct ceph_osd_request *req;
Alex Elder75d1c942013-03-13 20:50:00 -0500775 u64 objnum = 0;
776 u64 objoff = 0;
777 u64 objlen = 0;
Alex Elderd18d1e22013-03-13 20:50:01 -0500778 u32 object_size;
779 u64 object_base;
Sage Weil68162822012-09-24 21:01:02 -0700780 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700781
Li Wangad7a60d2013-08-15 11:51:44 +0800782 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
783 opcode != CEPH_OSD_OP_DELETE && opcode != CEPH_OSD_OP_ZERO &&
784 opcode != CEPH_OSD_OP_TRUNCATE);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700785
Alex Elderacead002013-03-14 14:09:05 -0500786 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600787 GFP_NOFS);
Sage Weil4ad12622011-05-03 09:23:36 -0700788 if (!req)
Sage Weil68162822012-09-24 21:01:02 -0700789 return ERR_PTR(-ENOMEM);
Alex Elder79528732013-04-03 21:32:51 -0500790
Alex Elderd178a9e2012-11-13 21:11:15 -0600791 req->r_flags = flags;
Sage Weilf24e9982009-10-06 11:31:10 -0700792
793 /* calculate max write size */
Alex Eldera19dadf2013-03-13 20:50:01 -0500794 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
Alex Elder3ff5f382013-02-15 22:10:17 -0600795 if (r < 0) {
796 ceph_osdc_put_request(req);
Sage Weil68162822012-09-24 21:01:02 -0700797 return ERR_PTR(r);
Alex Elder3ff5f382013-02-15 22:10:17 -0600798 }
Alex Eldera19dadf2013-03-13 20:50:01 -0500799
Alex Elderd18d1e22013-03-13 20:50:01 -0500800 object_size = le32_to_cpu(layout->fl_object_size);
801 object_base = off - objoff;
Yan, Zhengccca4e32013-06-02 18:40:23 +0800802 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
803 if (truncate_size <= object_base) {
804 truncate_size = 0;
805 } else {
806 truncate_size -= object_base;
807 if (truncate_size > object_size)
808 truncate_size = object_size;
809 }
Alex Eldera19dadf2013-03-13 20:50:01 -0500810 }
Alex Elderd18d1e22013-03-13 20:50:01 -0500811
Alex Elderc99d2d42013-04-05 01:27:11 -0500812 osd_req_op_extent_init(req, 0, opcode, objoff, objlen,
Alex Elderb0270322013-03-13 20:50:01 -0500813 truncate_size, truncate_seq);
Alex Elder8c042b02013-04-03 01:28:58 -0500814
Alex Elderacead002013-03-14 14:09:05 -0500815 /*
816 * A second op in the ops array means the caller wants to
817 * also issue a include a 'startsync' command so that the
818 * osd will flush data quickly.
819 */
820 if (num_ops > 1)
Alex Elderc99d2d42013-04-05 01:27:11 -0500821 osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC);
Alex Elderd18d1e22013-03-13 20:50:01 -0500822
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200823 req->r_base_oloc.pool = ceph_file_layout_pg_pool(*layout);
Sage Weilf24e9982009-10-06 11:31:10 -0700824
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200825 snprintf(req->r_base_oid.name, sizeof(req->r_base_oid.name),
Ilya Dryomov4295f222014-01-27 17:40:18 +0200826 "%llx.%08llx", vino.ino, objnum);
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200827 req->r_base_oid.name_len = strlen(req->r_base_oid.name);
Alex Elderdbe0fc42013-02-15 22:10:17 -0600828
Sage Weilf24e9982009-10-06 11:31:10 -0700829 return req;
830}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700831EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700832
833/*
834 * We keep osd requests in an rbtree, sorted by ->r_tid.
835 */
836static void __insert_request(struct ceph_osd_client *osdc,
837 struct ceph_osd_request *new)
838{
839 struct rb_node **p = &osdc->requests.rb_node;
840 struct rb_node *parent = NULL;
841 struct ceph_osd_request *req = NULL;
842
843 while (*p) {
844 parent = *p;
845 req = rb_entry(parent, struct ceph_osd_request, r_node);
846 if (new->r_tid < req->r_tid)
847 p = &(*p)->rb_left;
848 else if (new->r_tid > req->r_tid)
849 p = &(*p)->rb_right;
850 else
851 BUG();
852 }
853
854 rb_link_node(&new->r_node, parent, p);
855 rb_insert_color(&new->r_node, &osdc->requests);
856}
857
858static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
859 u64 tid)
860{
861 struct ceph_osd_request *req;
862 struct rb_node *n = osdc->requests.rb_node;
863
864 while (n) {
865 req = rb_entry(n, struct ceph_osd_request, r_node);
866 if (tid < req->r_tid)
867 n = n->rb_left;
868 else if (tid > req->r_tid)
869 n = n->rb_right;
870 else
871 return req;
872 }
873 return NULL;
874}
875
876static struct ceph_osd_request *
877__lookup_request_ge(struct ceph_osd_client *osdc,
878 u64 tid)
879{
880 struct ceph_osd_request *req;
881 struct rb_node *n = osdc->requests.rb_node;
882
883 while (n) {
884 req = rb_entry(n, struct ceph_osd_request, r_node);
885 if (tid < req->r_tid) {
886 if (!n->rb_left)
887 return req;
888 n = n->rb_left;
889 } else if (tid > req->r_tid) {
890 n = n->rb_right;
891 } else {
892 return req;
893 }
894 }
895 return NULL;
896}
897
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400898static void __kick_linger_request(struct ceph_osd_request *req)
899{
900 struct ceph_osd_client *osdc = req->r_osdc;
901 struct ceph_osd *osd = req->r_osd;
902
903 /*
904 * Linger requests need to be resent with a new tid to avoid
905 * the dup op detection logic on the OSDs. Achieve this with
906 * a re-register dance instead of open-coding.
907 */
908 ceph_osdc_get_request(req);
909 if (!list_empty(&req->r_linger_item))
910 __unregister_linger_request(osdc, req);
911 else
912 __unregister_request(osdc, req);
913 __register_request(osdc, req);
914 ceph_osdc_put_request(req);
915
916 /*
917 * Unless request has been registered as both normal and
918 * lingering, __unregister{,_linger}_request clears r_osd.
919 * However, here we need to preserve r_osd to make sure we
920 * requeue on the same OSD.
921 */
922 WARN_ON(req->r_osd || !osd);
923 req->r_osd = osd;
924
925 dout("%s requeueing %p tid %llu\n", __func__, req, req->r_tid);
926 __enqueue_request(req);
927}
928
Sage Weil6f6c7002011-01-17 20:34:08 -0800929/*
930 * Resubmit requests pending on the given osd.
931 */
932static void __kick_osd_requests(struct ceph_osd_client *osdc,
933 struct ceph_osd *osd)
934{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700935 struct ceph_osd_request *req, *nreq;
Alex Eldere02493c2013-03-25 18:16:11 -0500936 LIST_HEAD(resend);
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400937 LIST_HEAD(resend_linger);
Sage Weil6f6c7002011-01-17 20:34:08 -0800938 int err;
939
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400940 dout("%s osd%d\n", __func__, osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800941 err = __reset_osd(osdc, osd);
Alex Elder685a7552012-12-07 09:57:58 -0600942 if (err)
Sage Weil6f6c7002011-01-17 20:34:08 -0800943 return;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400944
Alex Eldere02493c2013-03-25 18:16:11 -0500945 /*
946 * Build up a list of requests to resend by traversing the
947 * osd's list of requests. Requests for a given object are
948 * sent in tid order, and that is also the order they're
949 * kept on this list. Therefore all requests that are in
950 * flight will be found first, followed by all requests that
951 * have not yet been sent. And to resend requests while
952 * preserving this order we will want to put any sent
953 * requests back on the front of the osd client's unsent
954 * list.
955 *
956 * So we build a separate ordered list of already-sent
957 * requests for the affected osd and splice it onto the
958 * front of the osd client's unsent list. Once we've seen a
959 * request that has not yet been sent we're done. Those
960 * requests are already sitting right where they belong.
961 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800962 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
Alex Eldere02493c2013-03-25 18:16:11 -0500963 if (!req->r_sent)
964 break;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400965
966 if (!req->r_linger) {
967 dout("%s requeueing %p tid %llu\n", __func__, req,
968 req->r_tid);
969 list_move_tail(&req->r_req_lru_item, &resend);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700970 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400971 } else {
972 list_move_tail(&req->r_req_lru_item, &resend_linger);
973 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700974 }
Alex Eldere02493c2013-03-25 18:16:11 -0500975 list_splice(&resend, &osdc->req_unsent);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700976
Alex Eldere02493c2013-03-25 18:16:11 -0500977 /*
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400978 * Both registered and not yet registered linger requests are
979 * enqueued with a new tid on the same OSD. We add/move them
980 * to req_unsent/o_requests at the end to keep things in tid
981 * order.
Alex Eldere02493c2013-03-25 18:16:11 -0500982 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700983 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
Ilya Dryomov1d0326b2014-06-20 14:14:41 +0400984 r_linger_osd_item) {
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400985 WARN_ON(!list_empty(&req->r_req_lru_item));
986 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -0800987 }
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400988
989 list_for_each_entry_safe(req, nreq, &resend_linger, r_req_lru_item)
990 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -0800991}
992
Sage Weilf24e9982009-10-06 11:31:10 -0700993/*
Sage Weil81b024e2009-10-09 10:29:18 -0700994 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700995 */
996static void osd_reset(struct ceph_connection *con)
997{
998 struct ceph_osd *osd = con->private;
999 struct ceph_osd_client *osdc;
1000
1001 if (!osd)
1002 return;
1003 dout("osd_reset osd%d\n", osd->o_osd);
1004 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001005 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -08001006 mutex_lock(&osdc->request_mutex);
1007 __kick_osd_requests(osdc, osd);
Alex Elderf9d25192013-02-15 11:42:29 -06001008 __send_queued(osdc);
Sage Weil83aff952012-11-28 12:28:24 -08001009 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001010 up_read(&osdc->map_sem);
1011}
1012
1013/*
1014 * Track open sessions with osds.
1015 */
Alex Eldere10006f2012-05-26 23:26:43 -05001016static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -07001017{
1018 struct ceph_osd *osd;
1019
1020 osd = kzalloc(sizeof(*osd), GFP_NOFS);
1021 if (!osd)
1022 return NULL;
1023
1024 atomic_set(&osd->o_ref, 1);
1025 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -05001026 osd->o_osd = onum;
Alex Elderf4077312012-12-06 07:22:04 -06001027 RB_CLEAR_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001028 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001029 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001030 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001031 osd->o_incarnation = 1;
1032
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001033 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001034
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001035 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001036 return osd;
1037}
1038
1039static struct ceph_osd *get_osd(struct ceph_osd *osd)
1040{
1041 if (atomic_inc_not_zero(&osd->o_ref)) {
1042 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
1043 atomic_read(&osd->o_ref));
1044 return osd;
1045 } else {
1046 dout("get_osd %p FAIL\n", osd);
1047 return NULL;
1048 }
1049}
1050
1051static void put_osd(struct ceph_osd *osd)
1052{
1053 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
1054 atomic_read(&osd->o_ref) - 1);
Alex Eldera2556512012-05-16 15:16:39 -05001055 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
Sage Weil79494d12010-05-27 14:15:49 -07001056 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
1057
Sage Weil27859f92013-03-25 10:26:14 -07001058 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -07001059 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -07001060 }
Sage Weilf24e9982009-10-06 11:31:10 -07001061}
1062
1063/*
1064 * remove an osd from our map
1065 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001066static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001067{
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001068 dout("__remove_osd %p\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001069 BUG_ON(!list_empty(&osd->o_requests));
Ilya Dryomov7c6e6fc2014-06-18 13:02:12 +04001070 BUG_ON(!list_empty(&osd->o_linger_requests));
1071
Sage Weilf24e9982009-10-06 11:31:10 -07001072 rb_erase(&osd->o_node, &osdc->osds);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001073 list_del_init(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001074 ceph_con_close(&osd->o_con);
1075 put_osd(osd);
1076}
1077
Sage Weilaca420b2011-08-31 14:45:53 -07001078static void remove_all_osds(struct ceph_osd_client *osdc)
1079{
Jiaju Zhang048a9d22012-07-20 08:18:36 -05001080 dout("%s %p\n", __func__, osdc);
Sage Weilaca420b2011-08-31 14:45:53 -07001081 mutex_lock(&osdc->request_mutex);
1082 while (!RB_EMPTY_ROOT(&osdc->osds)) {
1083 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
1084 struct ceph_osd, o_node);
1085 __remove_osd(osdc, osd);
1086 }
1087 mutex_unlock(&osdc->request_mutex);
1088}
1089
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001090static void __move_osd_to_lru(struct ceph_osd_client *osdc,
1091 struct ceph_osd *osd)
1092{
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001093 dout("%s %p\n", __func__, osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001094 BUG_ON(!list_empty(&osd->o_osd_lru));
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001095
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001096 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001097 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001098}
1099
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001100static void maybe_move_osd_to_lru(struct ceph_osd_client *osdc,
1101 struct ceph_osd *osd)
1102{
1103 dout("%s %p\n", __func__, osd);
1104
1105 if (list_empty(&osd->o_requests) &&
1106 list_empty(&osd->o_linger_requests))
1107 __move_osd_to_lru(osdc, osd);
1108}
1109
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001110static void __remove_osd_from_lru(struct ceph_osd *osd)
1111{
1112 dout("__remove_osd_from_lru %p\n", osd);
1113 if (!list_empty(&osd->o_osd_lru))
1114 list_del_init(&osd->o_osd_lru);
1115}
1116
Sage Weilaca420b2011-08-31 14:45:53 -07001117static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001118{
1119 struct ceph_osd *osd, *nosd;
1120
1121 dout("__remove_old_osds %p\n", osdc);
1122 mutex_lock(&osdc->request_mutex);
1123 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -07001124 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001125 break;
1126 __remove_osd(osdc, osd);
1127 }
1128 mutex_unlock(&osdc->request_mutex);
1129}
1130
Sage Weilf24e9982009-10-06 11:31:10 -07001131/*
1132 * reset osd connect
1133 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001134static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001135{
Alex Elderc3acb182012-12-07 09:57:58 -06001136 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -07001137
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001138 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001139 if (list_empty(&osd->o_requests) &&
1140 list_empty(&osd->o_linger_requests)) {
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001141 __remove_osd(osdc, osd);
Alex Elderc3acb182012-12-07 09:57:58 -06001142
1143 return -ENODEV;
1144 }
1145
1146 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1147 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1148 !ceph_con_opened(&osd->o_con)) {
1149 struct ceph_osd_request *req;
1150
Ilya Dryomov0b4af2e2014-01-16 19:18:27 +02001151 dout("osd addr hasn't changed and connection never opened, "
1152 "letting msgr retry\n");
Sage Weil87b315a2010-03-22 14:51:18 -07001153 /* touch each r_stamp for handle_timeout()'s benfit */
1154 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1155 req->r_stamp = jiffies;
Alex Elderc3acb182012-12-07 09:57:58 -06001156
1157 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -07001158 }
Alex Elderc3acb182012-12-07 09:57:58 -06001159
1160 ceph_con_close(&osd->o_con);
1161 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1162 osd->o_incarnation++;
1163
1164 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001165}
1166
1167static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1168{
1169 struct rb_node **p = &osdc->osds.rb_node;
1170 struct rb_node *parent = NULL;
1171 struct ceph_osd *osd = NULL;
1172
Sage Weilaca420b2011-08-31 14:45:53 -07001173 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001174 while (*p) {
1175 parent = *p;
1176 osd = rb_entry(parent, struct ceph_osd, o_node);
1177 if (new->o_osd < osd->o_osd)
1178 p = &(*p)->rb_left;
1179 else if (new->o_osd > osd->o_osd)
1180 p = &(*p)->rb_right;
1181 else
1182 BUG();
1183 }
1184
1185 rb_link_node(&new->o_node, parent, p);
1186 rb_insert_color(&new->o_node, &osdc->osds);
1187}
1188
1189static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1190{
1191 struct ceph_osd *osd;
1192 struct rb_node *n = osdc->osds.rb_node;
1193
1194 while (n) {
1195 osd = rb_entry(n, struct ceph_osd, o_node);
1196 if (o < osd->o_osd)
1197 n = n->rb_left;
1198 else if (o > osd->o_osd)
1199 n = n->rb_right;
1200 else
1201 return osd;
1202 }
1203 return NULL;
1204}
1205
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001206static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1207{
1208 schedule_delayed_work(&osdc->timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001209 osdc->client->options->osd_keepalive_timeout * HZ);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001210}
1211
1212static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1213{
1214 cancel_delayed_work(&osdc->timeout_work);
1215}
Sage Weilf24e9982009-10-06 11:31:10 -07001216
1217/*
1218 * Register request, assign tid. If this is the first request, set up
1219 * the timeout event.
1220 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001221static void __register_request(struct ceph_osd_client *osdc,
1222 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001223{
Sage Weilf24e9982009-10-06 11:31:10 -07001224 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -08001225 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -07001226 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001227 __insert_request(osdc, req);
1228 ceph_osdc_get_request(req);
1229 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -07001230 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001231 dout(" first request, scheduling timeout\n");
1232 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001233 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001234}
1235
Sage Weilf24e9982009-10-06 11:31:10 -07001236/*
1237 * called under osdc->request_mutex
1238 */
1239static void __unregister_request(struct ceph_osd_client *osdc,
1240 struct ceph_osd_request *req)
1241{
Sage Weil35f9f8a2012-05-16 15:16:38 -05001242 if (RB_EMPTY_NODE(&req->r_node)) {
1243 dout("__unregister_request %p tid %lld not registered\n",
1244 req, req->r_tid);
1245 return;
1246 }
1247
Sage Weilf24e9982009-10-06 11:31:10 -07001248 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1249 rb_erase(&req->r_node, &osdc->requests);
Ilya Dryomov6562d662014-06-20 14:14:42 +04001250 RB_CLEAR_NODE(&req->r_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001251 osdc->num_requests--;
1252
Sage Weil0ba64782009-10-08 16:57:16 -07001253 if (req->r_osd) {
1254 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -05001255 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -07001256
1257 list_del_init(&req->r_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001258 maybe_move_osd_to_lru(osdc, req->r_osd);
Ilya Dryomov4f234092014-06-20 18:29:20 +04001259 if (list_empty(&req->r_linger_osd_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001260 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -07001261 }
Sage Weilf24e9982009-10-06 11:31:10 -07001262
Alex Elder7d5f2482012-11-29 08:37:03 -06001263 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001264 ceph_osdc_put_request(req);
1265
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001266 if (osdc->num_requests == 0) {
1267 dout(" no requests, canceling timeout\n");
1268 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001269 }
1270}
1271
1272/*
1273 * Cancel a previously queued request message
1274 */
1275static void __cancel_request(struct ceph_osd_request *req)
1276{
Sage Weil6bc18872010-09-27 10:18:52 -07001277 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -05001278 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001279 req->r_sent = 0;
1280 }
1281}
1282
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001283static void __register_linger_request(struct ceph_osd_client *osdc,
1284 struct ceph_osd_request *req)
1285{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001286 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1287 WARN_ON(!req->r_linger);
1288
Alex Elder96e4dac2013-05-22 20:54:25 -05001289 ceph_osdc_get_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001290 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -07001291 if (req->r_osd)
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001292 list_add_tail(&req->r_linger_osd_item,
Sage Weil6194ea82012-07-30 16:19:28 -07001293 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001294}
1295
1296static void __unregister_linger_request(struct ceph_osd_client *osdc,
1297 struct ceph_osd_request *req)
1298{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001299 WARN_ON(!req->r_linger);
1300
1301 if (list_empty(&req->r_linger_item)) {
1302 dout("%s %p tid %llu not registered\n", __func__, req,
1303 req->r_tid);
1304 return;
1305 }
1306
1307 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
Alex Elder61c74032012-12-06 09:37:23 -06001308 list_del_init(&req->r_linger_item);
Ilya Dryomovaf593062014-06-20 18:29:20 +04001309
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001310 if (req->r_osd) {
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001311 list_del_init(&req->r_linger_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001312 maybe_move_osd_to_lru(osdc, req->r_osd);
Sage Weilfbdb9192011-03-29 12:11:06 -07001313 if (list_empty(&req->r_osd_item))
1314 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001315 }
Alex Elder96e4dac2013-05-22 20:54:25 -05001316 ceph_osdc_put_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001317}
1318
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001319void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1320 struct ceph_osd_request *req)
1321{
1322 if (!req->r_linger) {
1323 dout("set_request_linger %p\n", req);
1324 req->r_linger = 1;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001325 }
1326}
1327EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1328
Sage Weilf24e9982009-10-06 11:31:10 -07001329/*
Josh Durgind29adb32013-12-02 19:11:48 -08001330 * Returns whether a request should be blocked from being sent
1331 * based on the current osdmap and osd_client settings.
1332 *
1333 * Caller should hold map_sem for read.
1334 */
1335static bool __req_should_be_paused(struct ceph_osd_client *osdc,
1336 struct ceph_osd_request *req)
1337{
1338 bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
1339 bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
1340 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
1341 return (req->r_flags & CEPH_OSD_FLAG_READ && pauserd) ||
1342 (req->r_flags & CEPH_OSD_FLAG_WRITE && pausewr);
1343}
1344
1345/*
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001346 * Calculate mapping of a request to a PG. Takes tiering into account.
1347 */
1348static int __calc_request_pg(struct ceph_osdmap *osdmap,
1349 struct ceph_osd_request *req,
1350 struct ceph_pg *pg_out)
1351{
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001352 bool need_check_tiering;
1353
1354 need_check_tiering = false;
1355 if (req->r_target_oloc.pool == -1) {
1356 req->r_target_oloc = req->r_base_oloc; /* struct */
1357 need_check_tiering = true;
1358 }
1359 if (req->r_target_oid.name_len == 0) {
1360 ceph_oid_copy(&req->r_target_oid, &req->r_base_oid);
1361 need_check_tiering = true;
1362 }
1363
1364 if (need_check_tiering &&
1365 (req->r_flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001366 struct ceph_pg_pool_info *pi;
1367
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001368 pi = ceph_pg_pool_by_id(osdmap, req->r_target_oloc.pool);
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001369 if (pi) {
1370 if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
1371 pi->read_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001372 req->r_target_oloc.pool = pi->read_tier;
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001373 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1374 pi->write_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001375 req->r_target_oloc.pool = pi->write_tier;
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001376 }
1377 /* !pi is caught in ceph_oloc_oid_to_pg() */
1378 }
1379
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001380 return ceph_oloc_oid_to_pg(osdmap, &req->r_target_oloc,
1381 &req->r_target_oid, pg_out);
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001382}
1383
Ilya Dryomovf671b582014-09-02 13:40:33 +04001384static void __enqueue_request(struct ceph_osd_request *req)
1385{
1386 struct ceph_osd_client *osdc = req->r_osdc;
1387
1388 dout("%s %p tid %llu to osd%d\n", __func__, req, req->r_tid,
1389 req->r_osd ? req->r_osd->o_osd : -1);
1390
1391 if (req->r_osd) {
1392 __remove_osd_from_lru(req->r_osd);
1393 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1394 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1395 } else {
1396 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1397 }
1398}
1399
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001400/*
Sage Weilf24e9982009-10-06 11:31:10 -07001401 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1402 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001403 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -08001404 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -07001405 *
1406 * Return 0 if unchanged, 1 if changed, or negative on error.
1407 *
1408 * Caller should hold map_sem for read and request_mutex.
1409 */
Sage Weil6f6c7002011-01-17 20:34:08 -08001410static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -07001411 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -07001412{
Sage Weil5b191d92013-02-23 10:38:16 -08001413 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -07001414 int acting[CEPH_PG_MAX_SIZE];
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001415 int num, o;
Sage Weilf24e9982009-10-06 11:31:10 -07001416 int err;
Josh Durgind29adb32013-12-02 19:11:48 -08001417 bool was_paused;
Sage Weilf24e9982009-10-06 11:31:10 -07001418
Sage Weil6f6c7002011-01-17 20:34:08 -08001419 dout("map_request %p tid %lld\n", req, req->r_tid);
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001420
1421 err = __calc_request_pg(osdc->osdmap, req, &pgid);
Sage Weil6f6c7002011-01-17 20:34:08 -08001422 if (err) {
1423 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -07001424 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -08001425 }
Sage Weil7740a422010-01-08 15:58:25 -08001426 req->r_pgid = pgid;
1427
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001428 num = ceph_calc_pg_acting(osdc->osdmap, pgid, acting, &o);
1429 if (num < 0)
1430 num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001431
Josh Durgind29adb32013-12-02 19:11:48 -08001432 was_paused = req->r_paused;
1433 req->r_paused = __req_should_be_paused(osdc, req);
1434 if (was_paused && !req->r_paused)
1435 force_resend = 1;
1436
Sage Weil38d64532011-10-14 13:33:55 -07001437 if ((!force_resend &&
1438 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -07001439 req->r_sent >= req->r_osd->o_incarnation &&
1440 req->r_num_pg_osds == num &&
1441 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Josh Durgind29adb32013-12-02 19:11:48 -08001442 (req->r_osd == NULL && o == -1) ||
1443 req->r_paused)
Sage Weilf24e9982009-10-06 11:31:10 -07001444 return 0; /* no change */
1445
Sage Weil5b191d92013-02-23 10:38:16 -08001446 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1447 req->r_tid, pgid.pool, pgid.seed, o,
Sage Weilf24e9982009-10-06 11:31:10 -07001448 req->r_osd ? req->r_osd->o_osd : -1);
1449
Sage Weild85b7052010-05-10 10:24:48 -07001450 /* record full pg acting set */
1451 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1452 req->r_num_pg_osds = num;
1453
Sage Weilf24e9982009-10-06 11:31:10 -07001454 if (req->r_osd) {
1455 __cancel_request(req);
1456 list_del_init(&req->r_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001457 req->r_osd = NULL;
1458 }
1459
1460 req->r_osd = __lookup_osd(osdc, o);
1461 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -08001462 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -05001463 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -08001464 if (!req->r_osd) {
1465 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001466 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001467 }
Sage Weilf24e9982009-10-06 11:31:10 -07001468
Sage Weil6f6c7002011-01-17 20:34:08 -08001469 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001470 __insert_osd(osdc, req->r_osd);
1471
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001472 ceph_con_open(&req->r_osd->o_con,
1473 CEPH_ENTITY_TYPE_OSD, o,
1474 &osdc->osdmap->osd_addr[o]);
Sage Weilf24e9982009-10-06 11:31:10 -07001475 }
1476
Ilya Dryomovf671b582014-09-02 13:40:33 +04001477 __enqueue_request(req);
Sage Weild85b7052010-05-10 10:24:48 -07001478 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001479
1480out:
Sage Weilf24e9982009-10-06 11:31:10 -07001481 return err;
1482}
1483
1484/*
1485 * caller should hold map_sem (for read) and request_mutex
1486 */
Sage Weil56e925b2012-01-03 12:34:34 -08001487static void __send_request(struct ceph_osd_client *osdc,
1488 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001489{
Sage Weil1b83bef2013-02-25 16:11:12 -08001490 void *p;
Sage Weilf24e9982009-10-06 11:31:10 -07001491
Sage Weil1b83bef2013-02-25 16:11:12 -08001492 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1493 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1494 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001495
Sage Weil1b83bef2013-02-25 16:11:12 -08001496 /* fill in message content that changes each time we send it */
1497 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1498 put_unaligned_le32(req->r_flags, req->r_request_flags);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001499 put_unaligned_le64(req->r_target_oloc.pool, req->r_request_pool);
Sage Weil1b83bef2013-02-25 16:11:12 -08001500 p = req->r_request_pgid;
1501 ceph_encode_64(&p, req->r_pgid.pool);
1502 ceph_encode_32(&p, req->r_pgid.seed);
1503 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1504 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1505 sizeof(req->r_reassert_version));
Sage Weil2169aea2013-02-25 16:13:08 -08001506
Sage Weil3dd72fc2010-03-22 14:42:30 -07001507 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001508 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001509
1510 ceph_msg_get(req->r_request); /* send consumes a ref */
Alex Elder26be8802013-04-15 11:20:42 -05001511
Sage Weilf24e9982009-10-06 11:31:10 -07001512 req->r_sent = req->r_osd->o_incarnation;
Alex Elder26be8802013-04-15 11:20:42 -05001513
1514 ceph_con_send(&req->r_osd->o_con, req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001515}
1516
1517/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001518 * Send any requests in the queue (req_unsent).
1519 */
Alex Elderf9d25192013-02-15 11:42:29 -06001520static void __send_queued(struct ceph_osd_client *osdc)
Sage Weil6f6c7002011-01-17 20:34:08 -08001521{
1522 struct ceph_osd_request *req, *tmp;
1523
Alex Elderf9d25192013-02-15 11:42:29 -06001524 dout("__send_queued\n");
1525 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
Sage Weil6f6c7002011-01-17 20:34:08 -08001526 __send_request(osdc, req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001527}
1528
1529/*
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001530 * Caller should hold map_sem for read and request_mutex.
1531 */
1532static int __ceph_osdc_start_request(struct ceph_osd_client *osdc,
1533 struct ceph_osd_request *req,
1534 bool nofail)
1535{
1536 int rc;
1537
1538 __register_request(osdc, req);
1539 req->r_sent = 0;
1540 req->r_got_reply = 0;
1541 rc = __map_request(osdc, req, 0);
1542 if (rc < 0) {
1543 if (nofail) {
1544 dout("osdc_start_request failed map, "
1545 " will retry %lld\n", req->r_tid);
1546 rc = 0;
1547 } else {
1548 __unregister_request(osdc, req);
1549 }
1550 return rc;
1551 }
1552
1553 if (req->r_osd == NULL) {
1554 dout("send_request %p no up osds in pg\n", req);
1555 ceph_monc_request_next_osdmap(&osdc->client->monc);
1556 } else {
1557 __send_queued(osdc);
1558 }
1559
1560 return 0;
1561}
1562
1563/*
Sage Weilf24e9982009-10-06 11:31:10 -07001564 * Timeout callback, called every N seconds when 1 or more osd
1565 * requests has been active for more than N seconds. When this
1566 * happens, we ping all OSDs with requests who have timed out to
1567 * ensure any communications channel reset is detected. Reset the
1568 * request timeouts another N seconds in the future as we go.
1569 * Reschedule the timeout event another N seconds in future (unless
1570 * there are no open requests).
1571 */
1572static void handle_timeout(struct work_struct *work)
1573{
1574 struct ceph_osd_client *osdc =
1575 container_of(work, struct ceph_osd_client, timeout_work.work);
Sage Weil83aff952012-11-28 12:28:24 -08001576 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001577 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001578 unsigned long keepalive =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001579 osdc->client->options->osd_keepalive_timeout * HZ;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001580 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001581 dout("timeout\n");
1582 down_read(&osdc->map_sem);
1583
1584 ceph_monc_request_next_osdmap(&osdc->client->monc);
1585
1586 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001587
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001588 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001589 * ping osds that are a bit slow. this ensures that if there
1590 * is a break in the TCP connection we will notice, and reopen
1591 * a connection with that osd (from the fault callback).
1592 */
1593 INIT_LIST_HEAD(&slow_osds);
1594 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Sage Weil3dd72fc2010-03-22 14:42:30 -07001595 if (time_before(jiffies, req->r_stamp + keepalive))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001596 break;
1597
1598 osd = req->r_osd;
1599 BUG_ON(!osd);
1600 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001601 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001602 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1603 }
1604 while (!list_empty(&slow_osds)) {
1605 osd = list_entry(slow_osds.next, struct ceph_osd,
1606 o_keepalive_item);
1607 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001608 ceph_con_keepalive(&osd->o_con);
1609 }
1610
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001611 __schedule_osd_timeout(osdc);
Alex Elderf9d25192013-02-15 11:42:29 -06001612 __send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001613 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001614 up_read(&osdc->map_sem);
1615}
1616
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001617static void handle_osds_timeout(struct work_struct *work)
1618{
1619 struct ceph_osd_client *osdc =
1620 container_of(work, struct ceph_osd_client,
1621 osds_timeout_work.work);
1622 unsigned long delay =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001623 osdc->client->options->osd_idle_ttl * HZ >> 2;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001624
1625 dout("osds timeout\n");
1626 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001627 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001628 up_read(&osdc->map_sem);
1629
1630 schedule_delayed_work(&osdc->osds_timeout_work,
1631 round_jiffies_relative(delay));
1632}
1633
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001634static int ceph_oloc_decode(void **p, void *end,
1635 struct ceph_object_locator *oloc)
1636{
1637 u8 struct_v, struct_cv;
1638 u32 len;
1639 void *struct_end;
1640 int ret = 0;
1641
1642 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1643 struct_v = ceph_decode_8(p);
1644 struct_cv = ceph_decode_8(p);
1645 if (struct_v < 3) {
1646 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1647 struct_v, struct_cv);
1648 goto e_inval;
1649 }
1650 if (struct_cv > 6) {
1651 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1652 struct_v, struct_cv);
1653 goto e_inval;
1654 }
1655 len = ceph_decode_32(p);
1656 ceph_decode_need(p, end, len, e_inval);
1657 struct_end = *p + len;
1658
1659 oloc->pool = ceph_decode_64(p);
1660 *p += 4; /* skip preferred */
1661
1662 len = ceph_decode_32(p);
1663 if (len > 0) {
1664 pr_warn("ceph_object_locator::key is set\n");
1665 goto e_inval;
1666 }
1667
1668 if (struct_v >= 5) {
1669 len = ceph_decode_32(p);
1670 if (len > 0) {
1671 pr_warn("ceph_object_locator::nspace is set\n");
1672 goto e_inval;
1673 }
1674 }
1675
1676 if (struct_v >= 6) {
1677 s64 hash = ceph_decode_64(p);
1678 if (hash != -1) {
1679 pr_warn("ceph_object_locator::hash is set\n");
1680 goto e_inval;
1681 }
1682 }
1683
1684 /* skip the rest */
1685 *p = struct_end;
1686out:
1687 return ret;
1688
1689e_inval:
1690 ret = -EINVAL;
1691 goto out;
1692}
1693
1694static int ceph_redirect_decode(void **p, void *end,
1695 struct ceph_request_redirect *redir)
1696{
1697 u8 struct_v, struct_cv;
1698 u32 len;
1699 void *struct_end;
1700 int ret;
1701
1702 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1703 struct_v = ceph_decode_8(p);
1704 struct_cv = ceph_decode_8(p);
1705 if (struct_cv > 1) {
1706 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1707 struct_v, struct_cv);
1708 goto e_inval;
1709 }
1710 len = ceph_decode_32(p);
1711 ceph_decode_need(p, end, len, e_inval);
1712 struct_end = *p + len;
1713
1714 ret = ceph_oloc_decode(p, end, &redir->oloc);
1715 if (ret)
1716 goto out;
1717
1718 len = ceph_decode_32(p);
1719 if (len > 0) {
1720 pr_warn("ceph_request_redirect::object_name is set\n");
1721 goto e_inval;
1722 }
1723
1724 len = ceph_decode_32(p);
1725 *p += len; /* skip osd_instructions */
1726
1727 /* skip the rest */
1728 *p = struct_end;
1729out:
1730 return ret;
1731
1732e_inval:
1733 ret = -EINVAL;
1734 goto out;
1735}
1736
Sage Weil25845472011-06-03 09:37:09 -07001737static void complete_request(struct ceph_osd_request *req)
1738{
Sage Weil25845472011-06-03 09:37:09 -07001739 complete_all(&req->r_safe_completion); /* fsync waiter */
1740}
1741
Sage Weilf24e9982009-10-06 11:31:10 -07001742/*
1743 * handle osd op reply. either call the callback if it is specified,
1744 * or do the completion to wake up the waiting thread.
1745 */
Sage Weil350b1c32009-12-22 10:45:45 -08001746static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1747 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -07001748{
Sage Weil1b83bef2013-02-25 16:11:12 -08001749 void *p, *end;
Sage Weilf24e9982009-10-06 11:31:10 -07001750 struct ceph_osd_request *req;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001751 struct ceph_request_redirect redir;
Sage Weilf24e9982009-10-06 11:31:10 -07001752 u64 tid;
Sage Weil1b83bef2013-02-25 16:11:12 -08001753 int object_len;
Alex Elder79528732013-04-03 21:32:51 -05001754 unsigned int numops;
1755 int payload_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001756 s32 result;
Sage Weil1b83bef2013-02-25 16:11:12 -08001757 s32 retry_attempt;
1758 struct ceph_pg pg;
1759 int err;
1760 u32 reassert_epoch;
1761 u64 reassert_version;
1762 u32 osdmap_epoch;
Alex Elder0d5af162013-02-27 10:26:25 -06001763 int already_completed;
Alex Elder9fc6e062013-04-03 01:28:57 -05001764 u32 bytes;
Alex Elder79528732013-04-03 21:32:51 -05001765 unsigned int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001766
Sage Weil6df058c2009-12-22 11:24:33 -08001767 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil1b83bef2013-02-25 16:11:12 -08001768 dout("handle_reply %p tid %llu\n", msg, tid);
1769
1770 p = msg->front.iov_base;
1771 end = p + msg->front.iov_len;
1772
1773 ceph_decode_need(&p, end, 4, bad);
1774 object_len = ceph_decode_32(&p);
1775 ceph_decode_need(&p, end, object_len, bad);
1776 p += object_len;
1777
Alex Elderef4859d2013-04-01 18:58:26 -05001778 err = ceph_decode_pgid(&p, end, &pg);
Sage Weil1b83bef2013-02-25 16:11:12 -08001779 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001780 goto bad;
Sage Weil1b83bef2013-02-25 16:11:12 -08001781
1782 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1783 flags = ceph_decode_64(&p);
1784 result = ceph_decode_32(&p);
1785 reassert_epoch = ceph_decode_32(&p);
1786 reassert_version = ceph_decode_64(&p);
1787 osdmap_epoch = ceph_decode_32(&p);
1788
Sage Weilf24e9982009-10-06 11:31:10 -07001789 /* lookup */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001790 down_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001791 mutex_lock(&osdc->request_mutex);
1792 req = __lookup_request(osdc, tid);
1793 if (req == NULL) {
1794 dout("handle_reply tid %llu dne\n", tid);
Alex Elder8058fd42013-04-01 18:58:26 -05001795 goto bad_mutex;
Sage Weilf24e9982009-10-06 11:31:10 -07001796 }
1797 ceph_osdc_get_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001798
1799 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1800 req, result);
1801
Dan Carpenter18741192013-08-15 08:51:58 +03001802 ceph_decode_need(&p, end, 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001803 numops = ceph_decode_32(&p);
1804 if (numops > CEPH_OSD_MAX_OP)
1805 goto bad_put;
1806 if (numops != req->r_num_ops)
1807 goto bad_put;
1808 payload_len = 0;
Dan Carpenter18741192013-08-15 08:51:58 +03001809 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001810 for (i = 0; i < numops; i++) {
1811 struct ceph_osd_op *op = p;
1812 int len;
1813
1814 len = le32_to_cpu(op->payload_len);
1815 req->r_reply_op_len[i] = len;
1816 dout(" op %d has %d bytes\n", i, len);
1817 payload_len += len;
1818 p += sizeof(*op);
1819 }
Alex Elder9fc6e062013-04-03 01:28:57 -05001820 bytes = le32_to_cpu(msg->hdr.data_len);
1821 if (payload_len != bytes) {
Joe Perchesb9a67892014-09-09 21:17:29 -07001822 pr_warn("sum of op payload lens %d != data_len %d\n",
1823 payload_len, bytes);
Sage Weil1b83bef2013-02-25 16:11:12 -08001824 goto bad_put;
1825 }
1826
Dan Carpenter18741192013-08-15 08:51:58 +03001827 ceph_decode_need(&p, end, 4 + numops * 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001828 retry_attempt = ceph_decode_32(&p);
1829 for (i = 0; i < numops; i++)
1830 req->r_reply_op_result[i] = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001831
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001832 if (le16_to_cpu(msg->hdr.version) >= 6) {
1833 p += 8 + 4; /* skip replay_version */
1834 p += 8; /* skip user_version */
1835
1836 err = ceph_redirect_decode(&p, end, &redir);
1837 if (err)
1838 goto bad_put;
1839 } else {
1840 redir.oloc.pool = -1;
1841 }
1842
1843 if (redir.oloc.pool != -1) {
1844 dout("redirect pool %lld\n", redir.oloc.pool);
1845
1846 __unregister_request(osdc, req);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001847
1848 req->r_target_oloc = redir.oloc; /* struct */
1849
1850 /*
1851 * Start redirect requests with nofail=true. If
1852 * mapping fails, request will end up on the notarget
1853 * list, waiting for the new osdmap (which can take
1854 * a while), even though the original request mapped
1855 * successfully. In the future we might want to follow
1856 * original request's nofail setting here.
1857 */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001858 err = __ceph_osdc_start_request(osdc, req, true);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001859 BUG_ON(err);
1860
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001861 goto out_unlock;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001862 }
1863
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001864 already_completed = req->r_got_reply;
Sage Weilf24e9982009-10-06 11:31:10 -07001865 if (!req->r_got_reply) {
Sage Weil1b83bef2013-02-25 16:11:12 -08001866 req->r_result = result;
Sage Weilf24e9982009-10-06 11:31:10 -07001867 dout("handle_reply result %d bytes %d\n", req->r_result,
1868 bytes);
1869 if (req->r_result == 0)
1870 req->r_result = bytes;
1871
1872 /* in case this is a write and we need to replay, */
Sage Weil1b83bef2013-02-25 16:11:12 -08001873 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1874 req->r_reassert_version.version = cpu_to_le64(reassert_version);
Sage Weilf24e9982009-10-06 11:31:10 -07001875
1876 req->r_got_reply = 1;
1877 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1878 dout("handle_reply tid %llu dup ack\n", tid);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001879 goto out_unlock;
Sage Weilf24e9982009-10-06 11:31:10 -07001880 }
1881
1882 dout("handle_reply tid %llu flags %d\n", tid, flags);
1883
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001884 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1885 __register_linger_request(osdc, req);
1886
Sage Weilf24e9982009-10-06 11:31:10 -07001887 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001888 if (result < 0 ||
1889 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001890 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1891 __unregister_request(osdc, req);
1892
1893 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001894 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001895
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001896 if (!already_completed) {
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001897 if (req->r_unsafe_callback &&
1898 result >= 0 && !(flags & CEPH_OSD_FLAG_ONDISK))
1899 req->r_unsafe_callback(req, true);
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001900 if (req->r_callback)
1901 req->r_callback(req, msg);
1902 else
1903 complete_all(&req->r_completion);
1904 }
Sage Weilf24e9982009-10-06 11:31:10 -07001905
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001906 if (flags & CEPH_OSD_FLAG_ONDISK) {
1907 if (req->r_unsafe_callback && already_completed)
1908 req->r_unsafe_callback(req, false);
Sage Weil25845472011-06-03 09:37:09 -07001909 complete_request(req);
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001910 }
Sage Weilf24e9982009-10-06 11:31:10 -07001911
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001912out:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001913 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001914 ceph_osdc_put_request(req);
1915 return;
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001916out_unlock:
1917 mutex_unlock(&osdc->request_mutex);
1918 up_read(&osdc->map_sem);
1919 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001920
Sage Weil1b83bef2013-02-25 16:11:12 -08001921bad_put:
Li Wang37c89bd2013-11-27 22:28:14 +08001922 req->r_result = -EIO;
1923 __unregister_request(osdc, req);
1924 if (req->r_callback)
1925 req->r_callback(req, msg);
1926 else
1927 complete_all(&req->r_completion);
1928 complete_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001929 ceph_osdc_put_request(req);
Alex Elder8058fd42013-04-01 18:58:26 -05001930bad_mutex:
1931 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001932 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001933bad:
Sage Weil1b83bef2013-02-25 16:11:12 -08001934 pr_err("corrupt osd_op_reply got %d %d\n",
1935 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001936 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001937}
1938
Sage Weil6f6c7002011-01-17 20:34:08 -08001939static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001940{
Sage Weilf24e9982009-10-06 11:31:10 -07001941 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001942
Sage Weil6f6c7002011-01-17 20:34:08 -08001943 for (p = rb_first(&osdc->osds); p; p = n) {
1944 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001945
Sage Weil6f6c7002011-01-17 20:34:08 -08001946 n = rb_next(p);
1947 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1948 memcmp(&osd->o_con.peer_addr,
1949 ceph_osd_addr(osdc->osdmap,
1950 osd->o_osd),
1951 sizeof(struct ceph_entity_addr)) != 0)
1952 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001953 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001954}
1955
1956/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001957 * Requeue requests whose mapping to an OSD has changed. If requests map to
1958 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001959 *
Alex Eldere6d50f62012-12-26 14:31:40 -06001960 * Caller should hold map_sem for read.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001961 */
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001962static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
1963 bool force_resend_writes)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001964{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001965 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001966 struct rb_node *p;
1967 int needmap = 0;
1968 int err;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001969 bool force_resend_req;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001970
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001971 dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
1972 force_resend_writes ? " (force resend writes)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001973 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07001974 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08001975 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07001976 p = rb_next(p);
Alex Elderab60b162012-12-19 15:52:36 -06001977
1978 /*
1979 * For linger requests that have not yet been
1980 * registered, move them to the linger list; they'll
1981 * be sent to the osd in the loop below. Unregister
1982 * the request before re-registering it as a linger
1983 * request to ensure the __map_request() below
1984 * will decide it needs to be sent.
1985 */
1986 if (req->r_linger && list_empty(&req->r_linger_item)) {
1987 dout("%p tid %llu restart on osd%d\n",
1988 req, req->r_tid,
1989 req->r_osd ? req->r_osd->o_osd : -1);
Alex Elder96e4dac2013-05-22 20:54:25 -05001990 ceph_osdc_get_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06001991 __unregister_request(osdc, req);
1992 __register_linger_request(osdc, req);
Alex Elder96e4dac2013-05-22 20:54:25 -05001993 ceph_osdc_put_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06001994 continue;
1995 }
1996
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001997 force_resend_req = force_resend ||
1998 (force_resend_writes &&
1999 req->r_flags & CEPH_OSD_FLAG_WRITE);
2000 err = __map_request(osdc, req, force_resend_req);
Sage Weil6f6c7002011-01-17 20:34:08 -08002001 if (err < 0)
2002 continue; /* error */
2003 if (req->r_osd == NULL) {
2004 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
2005 needmap++; /* request a newer map */
2006 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07002007 if (!req->r_linger) {
2008 dout("%p tid %llu requeued on osd%d\n", req,
2009 req->r_tid,
2010 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002011 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07002012 }
2013 }
Sage Weil6f6c7002011-01-17 20:34:08 -08002014 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002015
2016 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
2017 r_linger_item) {
2018 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
2019
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002020 err = __map_request(osdc, req,
2021 force_resend || force_resend_writes);
Alex Elderab60b162012-12-19 15:52:36 -06002022 dout("__map_request returned %d\n", err);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002023 if (err == 0)
2024 continue; /* no change and no osd was specified */
2025 if (err < 0)
2026 continue; /* hrm! */
2027 if (req->r_osd == NULL) {
2028 dout("tid %llu maps to no valid osd\n", req->r_tid);
2029 needmap++; /* request a newer map */
2030 continue;
2031 }
2032
2033 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
2034 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002035 __register_request(osdc, req);
Alex Elderc89ce052012-12-06 07:22:04 -06002036 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002037 }
Alex Elder14d2f382013-05-15 16:28:33 -05002038 reset_changed_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002039 mutex_unlock(&osdc->request_mutex);
2040
2041 if (needmap) {
2042 dout("%d requests for down osds, need new map\n", needmap);
2043 ceph_monc_request_next_osdmap(&osdc->client->monc);
2044 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002045}
Sage Weil6f6c7002011-01-17 20:34:08 -08002046
2047
Sage Weilf24e9982009-10-06 11:31:10 -07002048/*
2049 * Process updated osd map.
2050 *
2051 * The message contains any number of incremental and full maps, normally
2052 * indicating some sort of topology change in the cluster. Kick requests
2053 * off to different OSDs as needed.
2054 */
2055void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
2056{
2057 void *p, *end, *next;
2058 u32 nr_maps, maplen;
2059 u32 epoch;
2060 struct ceph_osdmap *newmap = NULL, *oldmap;
2061 int err;
2062 struct ceph_fsid fsid;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002063 bool was_full;
Sage Weilf24e9982009-10-06 11:31:10 -07002064
2065 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
2066 p = msg->front.iov_base;
2067 end = p + msg->front.iov_len;
2068
2069 /* verify fsid */
2070 ceph_decode_need(&p, end, sizeof(fsid), bad);
2071 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08002072 if (ceph_check_fsid(osdc->client, &fsid) < 0)
2073 return;
Sage Weilf24e9982009-10-06 11:31:10 -07002074
2075 down_write(&osdc->map_sem);
2076
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002077 was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
2078
Sage Weilf24e9982009-10-06 11:31:10 -07002079 /* incremental maps */
2080 ceph_decode_32_safe(&p, end, nr_maps, bad);
2081 dout(" %d inc maps\n", nr_maps);
2082 while (nr_maps > 0) {
2083 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002084 epoch = ceph_decode_32(&p);
2085 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002086 ceph_decode_need(&p, end, maplen, bad);
2087 next = p + maplen;
2088 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
2089 dout("applying incremental map %u len %d\n",
2090 epoch, maplen);
2091 newmap = osdmap_apply_incremental(&p, next,
2092 osdc->osdmap,
Alex Elder15d98822012-05-26 23:26:43 -05002093 &osdc->client->msgr);
Sage Weilf24e9982009-10-06 11:31:10 -07002094 if (IS_ERR(newmap)) {
2095 err = PTR_ERR(newmap);
2096 goto bad;
2097 }
Sage Weil30dc6382009-12-21 14:49:37 -08002098 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002099 if (newmap != osdc->osdmap) {
2100 ceph_osdmap_destroy(osdc->osdmap);
2101 osdc->osdmap = newmap;
2102 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002103 was_full = was_full ||
2104 ceph_osdmap_flag(osdc->osdmap,
2105 CEPH_OSDMAP_FULL);
2106 kick_requests(osdc, 0, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002107 } else {
2108 dout("ignoring incremental map %u len %d\n",
2109 epoch, maplen);
2110 }
2111 p = next;
2112 nr_maps--;
2113 }
2114 if (newmap)
2115 goto done;
2116
2117 /* full maps */
2118 ceph_decode_32_safe(&p, end, nr_maps, bad);
2119 dout(" %d full maps\n", nr_maps);
2120 while (nr_maps) {
2121 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002122 epoch = ceph_decode_32(&p);
2123 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002124 ceph_decode_need(&p, end, maplen, bad);
2125 if (nr_maps > 1) {
2126 dout("skipping non-latest full map %u len %d\n",
2127 epoch, maplen);
2128 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
2129 dout("skipping full map %u len %d, "
2130 "older than our %u\n", epoch, maplen,
2131 osdc->osdmap->epoch);
2132 } else {
Sage Weil38d64532011-10-14 13:33:55 -07002133 int skipped_map = 0;
2134
Sage Weilf24e9982009-10-06 11:31:10 -07002135 dout("taking full map %u len %d\n", epoch, maplen);
Ilya Dryomova2505d62014-03-13 16:36:13 +02002136 newmap = ceph_osdmap_decode(&p, p+maplen);
Sage Weilf24e9982009-10-06 11:31:10 -07002137 if (IS_ERR(newmap)) {
2138 err = PTR_ERR(newmap);
2139 goto bad;
2140 }
Sage Weil30dc6382009-12-21 14:49:37 -08002141 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002142 oldmap = osdc->osdmap;
2143 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07002144 if (oldmap) {
2145 if (oldmap->epoch + 1 < newmap->epoch)
2146 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07002147 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07002148 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002149 was_full = was_full ||
2150 ceph_osdmap_flag(osdc->osdmap,
2151 CEPH_OSDMAP_FULL);
2152 kick_requests(osdc, skipped_map, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002153 }
2154 p += maplen;
2155 nr_maps--;
2156 }
2157
Dan Carpenterb72e19b2013-08-15 08:52:48 +03002158 if (!osdc->osdmap)
2159 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07002160done:
2161 downgrade_write(&osdc->map_sem);
2162 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07002163
2164 /*
2165 * subscribe to subsequent osdmap updates if full to ensure
2166 * we find out when we are no longer full and stop returning
2167 * ENOSPC.
2168 */
Josh Durgind29adb32013-12-02 19:11:48 -08002169 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
2170 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
2171 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR))
Sage Weilcd634fb2011-05-12 09:29:18 -07002172 ceph_monc_request_next_osdmap(&osdc->client->monc);
2173
Alex Elderf9d25192013-02-15 11:42:29 -06002174 mutex_lock(&osdc->request_mutex);
2175 __send_queued(osdc);
2176 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002177 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002178 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002179 return;
2180
2181bad:
2182 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002183 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07002184 up_write(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07002185}
2186
Sage Weilf24e9982009-10-06 11:31:10 -07002187/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002188 * watch/notify callback event infrastructure
2189 *
2190 * These callbacks are used both for watch and notify operations.
2191 */
2192static void __release_event(struct kref *kref)
2193{
2194 struct ceph_osd_event *event =
2195 container_of(kref, struct ceph_osd_event, kref);
2196
2197 dout("__release_event %p\n", event);
2198 kfree(event);
2199}
2200
2201static void get_event(struct ceph_osd_event *event)
2202{
2203 kref_get(&event->kref);
2204}
2205
2206void ceph_osdc_put_event(struct ceph_osd_event *event)
2207{
2208 kref_put(&event->kref, __release_event);
2209}
2210EXPORT_SYMBOL(ceph_osdc_put_event);
2211
2212static void __insert_event(struct ceph_osd_client *osdc,
2213 struct ceph_osd_event *new)
2214{
2215 struct rb_node **p = &osdc->event_tree.rb_node;
2216 struct rb_node *parent = NULL;
2217 struct ceph_osd_event *event = NULL;
2218
2219 while (*p) {
2220 parent = *p;
2221 event = rb_entry(parent, struct ceph_osd_event, node);
2222 if (new->cookie < event->cookie)
2223 p = &(*p)->rb_left;
2224 else if (new->cookie > event->cookie)
2225 p = &(*p)->rb_right;
2226 else
2227 BUG();
2228 }
2229
2230 rb_link_node(&new->node, parent, p);
2231 rb_insert_color(&new->node, &osdc->event_tree);
2232}
2233
2234static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
2235 u64 cookie)
2236{
2237 struct rb_node **p = &osdc->event_tree.rb_node;
2238 struct rb_node *parent = NULL;
2239 struct ceph_osd_event *event = NULL;
2240
2241 while (*p) {
2242 parent = *p;
2243 event = rb_entry(parent, struct ceph_osd_event, node);
2244 if (cookie < event->cookie)
2245 p = &(*p)->rb_left;
2246 else if (cookie > event->cookie)
2247 p = &(*p)->rb_right;
2248 else
2249 return event;
2250 }
2251 return NULL;
2252}
2253
2254static void __remove_event(struct ceph_osd_event *event)
2255{
2256 struct ceph_osd_client *osdc = event->osdc;
2257
2258 if (!RB_EMPTY_NODE(&event->node)) {
2259 dout("__remove_event removed %p\n", event);
2260 rb_erase(&event->node, &osdc->event_tree);
2261 ceph_osdc_put_event(event);
2262 } else {
2263 dout("__remove_event didn't remove %p\n", event);
2264 }
2265}
2266
2267int ceph_osdc_create_event(struct ceph_osd_client *osdc,
2268 void (*event_cb)(u64, u64, u8, void *),
Alex Elder3c663bb2013-02-15 11:42:30 -06002269 void *data, struct ceph_osd_event **pevent)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002270{
2271 struct ceph_osd_event *event;
2272
2273 event = kmalloc(sizeof(*event), GFP_NOIO);
2274 if (!event)
2275 return -ENOMEM;
2276
2277 dout("create_event %p\n", event);
2278 event->cb = event_cb;
Alex Elder3c663bb2013-02-15 11:42:30 -06002279 event->one_shot = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002280 event->data = data;
2281 event->osdc = osdc;
2282 INIT_LIST_HEAD(&event->osd_node);
Alex Elder3ee52342012-12-17 12:23:48 -06002283 RB_CLEAR_NODE(&event->node);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002284 kref_init(&event->kref); /* one ref for us */
2285 kref_get(&event->kref); /* one ref for the caller */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002286
2287 spin_lock(&osdc->event_lock);
2288 event->cookie = ++osdc->event_count;
2289 __insert_event(osdc, event);
2290 spin_unlock(&osdc->event_lock);
2291
2292 *pevent = event;
2293 return 0;
2294}
2295EXPORT_SYMBOL(ceph_osdc_create_event);
2296
2297void ceph_osdc_cancel_event(struct ceph_osd_event *event)
2298{
2299 struct ceph_osd_client *osdc = event->osdc;
2300
2301 dout("cancel_event %p\n", event);
2302 spin_lock(&osdc->event_lock);
2303 __remove_event(event);
2304 spin_unlock(&osdc->event_lock);
2305 ceph_osdc_put_event(event); /* caller's */
2306}
2307EXPORT_SYMBOL(ceph_osdc_cancel_event);
2308
2309
2310static void do_event_work(struct work_struct *work)
2311{
2312 struct ceph_osd_event_work *event_work =
2313 container_of(work, struct ceph_osd_event_work, work);
2314 struct ceph_osd_event *event = event_work->event;
2315 u64 ver = event_work->ver;
2316 u64 notify_id = event_work->notify_id;
2317 u8 opcode = event_work->opcode;
2318
2319 dout("do_event_work completing %p\n", event);
2320 event->cb(ver, notify_id, opcode, event->data);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002321 dout("do_event_work completed %p\n", event);
2322 ceph_osdc_put_event(event);
2323 kfree(event_work);
2324}
2325
2326
2327/*
2328 * Process osd watch notifications
2329 */
Alex Elder3c663bb2013-02-15 11:42:30 -06002330static void handle_watch_notify(struct ceph_osd_client *osdc,
2331 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002332{
2333 void *p, *end;
2334 u8 proto_ver;
2335 u64 cookie, ver, notify_id;
2336 u8 opcode;
2337 struct ceph_osd_event *event;
2338 struct ceph_osd_event_work *event_work;
2339
2340 p = msg->front.iov_base;
2341 end = p + msg->front.iov_len;
2342
2343 ceph_decode_8_safe(&p, end, proto_ver, bad);
2344 ceph_decode_8_safe(&p, end, opcode, bad);
2345 ceph_decode_64_safe(&p, end, cookie, bad);
2346 ceph_decode_64_safe(&p, end, ver, bad);
2347 ceph_decode_64_safe(&p, end, notify_id, bad);
2348
2349 spin_lock(&osdc->event_lock);
2350 event = __find_event(osdc, cookie);
2351 if (event) {
Alex Elder3c663bb2013-02-15 11:42:30 -06002352 BUG_ON(event->one_shot);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002353 get_event(event);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002354 }
2355 spin_unlock(&osdc->event_lock);
2356 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2357 cookie, ver, event);
2358 if (event) {
2359 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002360 if (!event_work) {
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002361 pr_err("couldn't allocate event_work\n");
2362 ceph_osdc_put_event(event);
2363 return;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002364 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01002365 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002366 event_work->event = event;
2367 event_work->ver = ver;
2368 event_work->notify_id = notify_id;
2369 event_work->opcode = opcode;
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002370
2371 queue_work(osdc->notify_wq, &event_work->work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002372 }
2373
2374 return;
2375
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002376bad:
2377 pr_err("osdc handle_watch_notify corrupt msg\n");
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002378}
2379
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002380/*
Alex Eldere65550f2013-04-05 01:27:12 -05002381 * build new request AND message
2382 *
2383 */
2384void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
2385 struct ceph_snap_context *snapc, u64 snap_id,
2386 struct timespec *mtime)
2387{
2388 struct ceph_msg *msg = req->r_request;
2389 void *p;
2390 size_t msg_size;
2391 int flags = req->r_flags;
2392 u64 data_len;
2393 unsigned int i;
2394
2395 req->r_snapid = snap_id;
2396 req->r_snapc = ceph_get_snap_context(snapc);
2397
2398 /* encode request */
2399 msg->hdr.version = cpu_to_le16(4);
2400
2401 p = msg->front.iov_base;
2402 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2403 req->r_request_osdmap_epoch = p;
2404 p += 4;
2405 req->r_request_flags = p;
2406 p += 4;
2407 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2408 ceph_encode_timespec(p, mtime);
2409 p += sizeof(struct ceph_timespec);
2410 req->r_request_reassert_version = p;
2411 p += sizeof(struct ceph_eversion); /* will get filled in */
2412
2413 /* oloc */
2414 ceph_encode_8(&p, 4);
2415 ceph_encode_8(&p, 4);
2416 ceph_encode_32(&p, 8 + 4 + 4);
2417 req->r_request_pool = p;
2418 p += 8;
2419 ceph_encode_32(&p, -1); /* preferred */
2420 ceph_encode_32(&p, 0); /* key len */
2421
2422 ceph_encode_8(&p, 1);
2423 req->r_request_pgid = p;
2424 p += 8 + 4;
2425 ceph_encode_32(&p, -1); /* preferred */
2426
2427 /* oid */
Ilya Dryomov3c972c92014-01-27 17:40:20 +02002428 ceph_encode_32(&p, req->r_base_oid.name_len);
2429 memcpy(p, req->r_base_oid.name, req->r_base_oid.name_len);
2430 dout("oid '%.*s' len %d\n", req->r_base_oid.name_len,
2431 req->r_base_oid.name, req->r_base_oid.name_len);
2432 p += req->r_base_oid.name_len;
Alex Eldere65550f2013-04-05 01:27:12 -05002433
2434 /* ops--can imply data */
2435 ceph_encode_16(&p, (u16)req->r_num_ops);
2436 data_len = 0;
2437 for (i = 0; i < req->r_num_ops; i++) {
2438 data_len += osd_req_encode_op(req, p, i);
2439 p += sizeof(struct ceph_osd_op);
2440 }
2441
2442 /* snaps */
2443 ceph_encode_64(&p, req->r_snapid);
2444 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2445 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2446 if (req->r_snapc) {
2447 for (i = 0; i < snapc->num_snaps; i++) {
2448 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2449 }
2450 }
2451
2452 req->r_request_attempts = p;
2453 p += 4;
2454
2455 /* data */
2456 if (flags & CEPH_OSD_FLAG_WRITE) {
2457 u16 data_off;
2458
2459 /*
2460 * The header "data_off" is a hint to the receiver
2461 * allowing it to align received data into its
2462 * buffers such that there's no need to re-copy
2463 * it before writing it to disk (direct I/O).
2464 */
2465 data_off = (u16) (off & 0xffff);
2466 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2467 }
2468 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2469
2470 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2471 msg_size = p - msg->front.iov_base;
2472 msg->front.iov_len = msg_size;
2473 msg->hdr.front_len = cpu_to_le32(msg_size);
2474
2475 dout("build_request msg_size was %d\n", (int)msg_size);
2476}
2477EXPORT_SYMBOL(ceph_osdc_build_request);
2478
2479/*
Sage Weilf24e9982009-10-06 11:31:10 -07002480 * Register request, send initial attempt.
2481 */
2482int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2483 struct ceph_osd_request *req,
2484 bool nofail)
2485{
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002486 int rc;
Sage Weilf24e9982009-10-06 11:31:10 -07002487
Sage Weilf24e9982009-10-06 11:31:10 -07002488 down_read(&osdc->map_sem);
2489 mutex_lock(&osdc->request_mutex);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002490
2491 rc = __ceph_osdc_start_request(osdc, req, nofail);
2492
Sage Weilf24e9982009-10-06 11:31:10 -07002493 mutex_unlock(&osdc->request_mutex);
2494 up_read(&osdc->map_sem);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002495
Sage Weilf24e9982009-10-06 11:31:10 -07002496 return rc;
2497}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002498EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002499
2500/*
Ilya Dryomovc9f9b932014-06-19 11:38:13 +04002501 * Unregister a registered request. The request is not completed (i.e.
2502 * no callbacks or wakeups) - higher layers are supposed to know what
2503 * they are canceling.
2504 */
2505void ceph_osdc_cancel_request(struct ceph_osd_request *req)
2506{
2507 struct ceph_osd_client *osdc = req->r_osdc;
2508
2509 mutex_lock(&osdc->request_mutex);
2510 if (req->r_linger)
2511 __unregister_linger_request(osdc, req);
2512 __unregister_request(osdc, req);
2513 mutex_unlock(&osdc->request_mutex);
2514
2515 dout("%s %p tid %llu canceled\n", __func__, req, req->r_tid);
2516}
2517EXPORT_SYMBOL(ceph_osdc_cancel_request);
2518
2519/*
Sage Weilf24e9982009-10-06 11:31:10 -07002520 * wait for a request to complete
2521 */
2522int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2523 struct ceph_osd_request *req)
2524{
2525 int rc;
2526
Ilya Dryomovc9f9b932014-06-19 11:38:13 +04002527 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
2528
Sage Weilf24e9982009-10-06 11:31:10 -07002529 rc = wait_for_completion_interruptible(&req->r_completion);
2530 if (rc < 0) {
Ilya Dryomovc9f9b932014-06-19 11:38:13 +04002531 dout("%s %p tid %llu interrupted\n", __func__, req, req->r_tid);
2532 ceph_osdc_cancel_request(req);
Sage Weil25845472011-06-03 09:37:09 -07002533 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002534 return rc;
2535 }
2536
Ilya Dryomovc9f9b932014-06-19 11:38:13 +04002537 dout("%s %p tid %llu result %d\n", __func__, req, req->r_tid,
2538 req->r_result);
Sage Weilf24e9982009-10-06 11:31:10 -07002539 return req->r_result;
2540}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002541EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002542
2543/*
2544 * sync - wait for all in-flight requests to flush. avoid starvation.
2545 */
2546void ceph_osdc_sync(struct ceph_osd_client *osdc)
2547{
2548 struct ceph_osd_request *req;
2549 u64 last_tid, next_tid = 0;
2550
2551 mutex_lock(&osdc->request_mutex);
2552 last_tid = osdc->last_tid;
2553 while (1) {
2554 req = __lookup_request_ge(osdc, next_tid);
2555 if (!req)
2556 break;
2557 if (req->r_tid > last_tid)
2558 break;
2559
2560 next_tid = req->r_tid + 1;
2561 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2562 continue;
2563
2564 ceph_osdc_get_request(req);
2565 mutex_unlock(&osdc->request_mutex);
2566 dout("sync waiting on tid %llu (last is %llu)\n",
2567 req->r_tid, last_tid);
2568 wait_for_completion(&req->r_safe_completion);
2569 mutex_lock(&osdc->request_mutex);
2570 ceph_osdc_put_request(req);
2571 }
2572 mutex_unlock(&osdc->request_mutex);
2573 dout("sync done (thru tid %llu)\n", last_tid);
2574}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002575EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07002576
2577/*
Josh Durgindd935f42013-08-28 21:43:09 -07002578 * Call all pending notify callbacks - for use after a watch is
2579 * unregistered, to make sure no more callbacks for it will be invoked
2580 */
stephen hemmingerf64794492014-06-10 20:30:13 -07002581void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
Josh Durgindd935f42013-08-28 21:43:09 -07002582{
2583 flush_workqueue(osdc->notify_wq);
2584}
2585EXPORT_SYMBOL(ceph_osdc_flush_notifies);
2586
2587
2588/*
Sage Weilf24e9982009-10-06 11:31:10 -07002589 * init, shutdown
2590 */
2591int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2592{
2593 int err;
2594
2595 dout("init\n");
2596 osdc->client = client;
2597 osdc->osdmap = NULL;
2598 init_rwsem(&osdc->map_sem);
2599 init_completion(&osdc->map_waiters);
2600 osdc->last_requested_map = 0;
2601 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002602 osdc->last_tid = 0;
2603 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002604 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07002605 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002606 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08002607 INIT_LIST_HEAD(&osdc->req_unsent);
2608 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002609 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07002610 osdc->num_requests = 0;
2611 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002612 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002613 spin_lock_init(&osdc->event_lock);
2614 osdc->event_tree = RB_ROOT;
2615 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002616
2617 schedule_delayed_work(&osdc->osds_timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002618 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07002619
Sage Weil5f44f142009-11-18 14:52:18 -08002620 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07002621 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2622 sizeof(struct ceph_osd_request));
2623 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08002624 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07002625
Sage Weild50b4092012-07-09 14:22:34 -07002626 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2627 OSD_OP_FRONT_LEN, 10, true,
Sage Weil4f482802010-04-24 09:56:35 -07002628 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07002629 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08002630 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07002631 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Sage Weil4f482802010-04-24 09:56:35 -07002632 OSD_OPREPLY_FRONT_LEN, 10, true,
2633 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08002634 if (err < 0)
2635 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002636
Dan Carpenterdbcae082013-08-15 08:58:59 +03002637 err = -ENOMEM;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002638 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
Dan Carpenterdbcae082013-08-15 08:58:59 +03002639 if (!osdc->notify_wq)
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002640 goto out_msgpool_reply;
2641
Sage Weilf24e9982009-10-06 11:31:10 -07002642 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08002643
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002644out_msgpool_reply:
2645 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08002646out_msgpool:
2647 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08002648out_mempool:
2649 mempool_destroy(osdc->req_mempool);
2650out:
2651 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07002652}
2653
2654void ceph_osdc_stop(struct ceph_osd_client *osdc)
2655{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002656 flush_workqueue(osdc->notify_wq);
2657 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002658 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002659 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07002660 if (osdc->osdmap) {
2661 ceph_osdmap_destroy(osdc->osdmap);
2662 osdc->osdmap = NULL;
2663 }
Sage Weilaca420b2011-08-31 14:45:53 -07002664 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002665 mempool_destroy(osdc->req_mempool);
2666 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08002667 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07002668}
2669
2670/*
2671 * Read some contiguous pages. If we cross a stripe boundary, shorten
2672 * *plen. Return number of bytes read, or error.
2673 */
2674int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2675 struct ceph_vino vino, struct ceph_file_layout *layout,
2676 u64 off, u64 *plen,
2677 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08002678 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07002679{
2680 struct ceph_osd_request *req;
2681 int rc = 0;
2682
2683 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2684 vino.snap, off, *plen);
Alex Elder79528732013-04-03 21:32:51 -05002685 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002686 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
Alex Elderacead002013-03-14 14:09:05 -05002687 NULL, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002688 false);
Sage Weil68162822012-09-24 21:01:02 -07002689 if (IS_ERR(req))
2690 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002691
2692 /* it may be a short read due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06002693
Alex Elder406e2c92013-04-15 14:50:36 -05002694 osd_req_op_extent_osd_data_pages(req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05002695 pages, *plen, page_align, false, false);
Sage Weilf24e9982009-10-06 11:31:10 -07002696
Alex Eldere0c59482013-03-07 15:38:25 -06002697 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
Alex Elder43bfe5d2013-04-03 01:28:57 -05002698 off, *plen, *plen, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07002699
Alex Elder79528732013-04-03 21:32:51 -05002700 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
Alex Elder02ee07d2013-03-14 14:09:06 -05002701
Sage Weilf24e9982009-10-06 11:31:10 -07002702 rc = ceph_osdc_start_request(osdc, req, false);
2703 if (!rc)
2704 rc = ceph_osdc_wait_request(osdc, req);
2705
2706 ceph_osdc_put_request(req);
2707 dout("readpages result %d\n", rc);
2708 return rc;
2709}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002710EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07002711
2712/*
2713 * do a synchronous write on N pages
2714 */
2715int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2716 struct ceph_file_layout *layout,
2717 struct ceph_snap_context *snapc,
2718 u64 off, u64 len,
2719 u32 truncate_seq, u64 truncate_size,
2720 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06002721 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07002722{
2723 struct ceph_osd_request *req;
2724 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08002725 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07002726
Alex Elderacead002013-03-14 14:09:05 -05002727 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
Alex Elder79528732013-04-03 21:32:51 -05002728 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002729 CEPH_OSD_OP_WRITE,
Alex Elder24808822013-02-15 11:42:29 -06002730 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Alex Elderacead002013-03-14 14:09:05 -05002731 snapc, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002732 true);
Sage Weil68162822012-09-24 21:01:02 -07002733 if (IS_ERR(req))
2734 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002735
2736 /* it may be a short write due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05002737 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
Alex Elder43bfe5d2013-04-03 01:28:57 -05002738 false, false);
2739 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
Sage Weilf24e9982009-10-06 11:31:10 -07002740
Alex Elder79528732013-04-03 21:32:51 -05002741 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
Alex Elder02ee07d2013-03-14 14:09:06 -05002742
Alex Elder87f979d2013-02-15 11:42:29 -06002743 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07002744 if (!rc)
2745 rc = ceph_osdc_wait_request(osdc, req);
2746
2747 ceph_osdc_put_request(req);
2748 if (rc == 0)
2749 rc = len;
2750 dout("writepages result %d\n", rc);
2751 return rc;
2752}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002753EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07002754
Alex Elder5522ae02013-05-01 12:43:04 -05002755int ceph_osdc_setup(void)
2756{
2757 BUG_ON(ceph_osd_request_cache);
2758 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request",
2759 sizeof (struct ceph_osd_request),
2760 __alignof__(struct ceph_osd_request),
2761 0, NULL);
2762
2763 return ceph_osd_request_cache ? 0 : -ENOMEM;
2764}
2765EXPORT_SYMBOL(ceph_osdc_setup);
2766
2767void ceph_osdc_cleanup(void)
2768{
2769 BUG_ON(!ceph_osd_request_cache);
2770 kmem_cache_destroy(ceph_osd_request_cache);
2771 ceph_osd_request_cache = NULL;
2772}
2773EXPORT_SYMBOL(ceph_osdc_cleanup);
2774
Sage Weilf24e9982009-10-06 11:31:10 -07002775/*
2776 * handle incoming message
2777 */
2778static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2779{
2780 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01002781 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002782 int type = le16_to_cpu(msg->hdr.type);
2783
2784 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07002785 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01002786 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002787
2788 switch (type) {
2789 case CEPH_MSG_OSD_MAP:
2790 ceph_osdc_handle_map(osdc, msg);
2791 break;
2792 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08002793 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07002794 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002795 case CEPH_MSG_WATCH_NOTIFY:
2796 handle_watch_notify(osdc, msg);
2797 break;
Sage Weilf24e9982009-10-06 11:31:10 -07002798
2799 default:
2800 pr_err("received unknown message type %d %s\n", type,
2801 ceph_msg_type_name(type));
2802 }
Sage Weil4a32f932010-06-13 10:27:53 -07002803out:
Sage Weilf24e9982009-10-06 11:31:10 -07002804 ceph_msg_put(msg);
2805}
2806
Sage Weil5b3a4db2010-02-19 21:43:23 -08002807/*
Sage Weil21b667f2010-03-04 10:22:59 -08002808 * lookup and return message for incoming reply. set up reply message
2809 * pages.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002810 */
2811static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002812 struct ceph_msg_header *hdr,
2813 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002814{
2815 struct ceph_osd *osd = con->private;
2816 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002817 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002818 struct ceph_osd_request *req;
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002819 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002820 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002821 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002822
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002823 tid = le64_to_cpu(hdr->tid);
2824 mutex_lock(&osdc->request_mutex);
2825 req = __lookup_request(osdc, tid);
2826 if (!req) {
2827 *skip = 1;
2828 m = NULL;
Sage Weil756a16a2012-07-30 16:26:13 -07002829 dout("get_reply unknown tid %llu from osd%d\n", tid,
2830 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002831 goto out;
2832 }
Sage Weilc16e7862010-03-01 13:02:00 -08002833
Alex Elderace6d3a2013-04-01 16:12:14 -05002834 if (req->r_reply->con)
Alex Elder8921d112012-06-01 14:56:43 -05002835 dout("%s revoking msg %p from old con %p\n", __func__,
Alex Elderace6d3a2013-04-01 16:12:14 -05002836 req->r_reply, req->r_reply->con);
2837 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002838
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02002839 if (front_len > req->r_reply->front_alloc_len) {
Joe Perchesb9a67892014-09-09 21:17:29 -07002840 pr_warn("get_reply front %d > preallocated %d (%u#%llu)\n",
2841 front_len, req->r_reply->front_alloc_len,
2842 (unsigned int)con->peer_name.type,
2843 le64_to_cpu(con->peer_name.num));
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002844 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
2845 false);
Sage Weila79832f2010-04-01 16:06:19 -07002846 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002847 goto out;
2848 ceph_msg_put(req->r_reply);
2849 req->r_reply = m;
2850 }
2851 m = ceph_msg_get(req->r_reply);
2852
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002853 if (data_len > 0) {
Alex Eldera4ce40a2013-04-05 01:27:12 -05002854 struct ceph_osd_data *osd_data;
Alex Elder0fff87e2013-02-14 12:16:43 -06002855
Alex Eldera4ce40a2013-04-05 01:27:12 -05002856 /*
2857 * XXX This is assuming there is only one op containing
2858 * XXX page data. Probably OK for reads, but this
2859 * XXX ought to be done more generally.
2860 */
Alex Elder406e2c92013-04-15 14:50:36 -05002861 osd_data = osd_req_op_extent_osd_data(req, 0);
Alex Elder0fff87e2013-02-14 12:16:43 -06002862 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
Alex Elder0fff87e2013-02-14 12:16:43 -06002863 if (osd_data->pages &&
Alex Eldere0c59482013-03-07 15:38:25 -06002864 unlikely(osd_data->length < data_len)) {
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002865
Joe Perchesb9a67892014-09-09 21:17:29 -07002866 pr_warn("tid %lld reply has %d bytes we had only %llu bytes ready\n",
Alex Eldere0c59482013-03-07 15:38:25 -06002867 tid, data_len, osd_data->length);
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002868 *skip = 1;
2869 ceph_msg_put(m);
2870 m = NULL;
2871 goto out;
2872 }
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002873 }
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002874 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08002875 *skip = 0;
Sage Weilc16e7862010-03-01 13:02:00 -08002876 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002877
2878out:
2879 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002880 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002881
2882}
2883
2884static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2885 struct ceph_msg_header *hdr,
2886 int *skip)
2887{
2888 struct ceph_osd *osd = con->private;
2889 int type = le16_to_cpu(hdr->type);
2890 int front = le32_to_cpu(hdr->front_len);
2891
Alex Elder1c20f2d2012-06-04 14:43:32 -05002892 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002893 switch (type) {
2894 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002895 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002896 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002897 case CEPH_MSG_OSD_OPREPLY:
2898 return get_reply(con, hdr, skip);
2899 default:
2900 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2901 osd->o_osd);
2902 *skip = 1;
2903 return NULL;
2904 }
Sage Weilf24e9982009-10-06 11:31:10 -07002905}
2906
2907/*
2908 * Wrappers to refcount containing ceph_osd struct
2909 */
2910static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2911{
2912 struct ceph_osd *osd = con->private;
2913 if (get_osd(osd))
2914 return con;
2915 return NULL;
2916}
2917
2918static void put_osd_con(struct ceph_connection *con)
2919{
2920 struct ceph_osd *osd = con->private;
2921 put_osd(osd);
2922}
2923
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002924/*
2925 * authentication
2926 */
Alex Eldera3530df2012-05-16 15:16:39 -05002927/*
2928 * Note: returned pointer is the address of a structure that's
2929 * managed separately. Caller must *not* attempt to free it.
2930 */
2931static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002932 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002933{
2934 struct ceph_osd *o = con->private;
2935 struct ceph_osd_client *osdc = o->o_osdc;
2936 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002937 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002938
Alex Elder74f18692012-05-16 15:16:39 -05002939 if (force_new && auth->authorizer) {
Sage Weil27859f92013-03-25 10:26:14 -07002940 ceph_auth_destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002941 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002942 }
Sage Weil27859f92013-03-25 10:26:14 -07002943 if (!auth->authorizer) {
2944 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2945 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002946 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002947 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07002948 } else {
2949 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07002950 auth);
2951 if (ret)
2952 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002953 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002954 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05002955
Alex Eldera3530df2012-05-16 15:16:39 -05002956 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002957}
2958
2959
2960static int verify_authorizer_reply(struct ceph_connection *con, int len)
2961{
2962 struct ceph_osd *o = con->private;
2963 struct ceph_osd_client *osdc = o->o_osdc;
2964 struct ceph_auth_client *ac = osdc->client->monc.auth;
2965
Sage Weil27859f92013-03-25 10:26:14 -07002966 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002967}
2968
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002969static int invalidate_authorizer(struct ceph_connection *con)
2970{
2971 struct ceph_osd *o = con->private;
2972 struct ceph_osd_client *osdc = o->o_osdc;
2973 struct ceph_auth_client *ac = osdc->client->monc.auth;
2974
Sage Weil27859f92013-03-25 10:26:14 -07002975 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002976 return ceph_monc_validate_auth(&osdc->client->monc);
2977}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002978
Tobias Klauser9e327892010-05-20 10:40:19 +02002979static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07002980 .get = get_osd_con,
2981 .put = put_osd_con,
2982 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002983 .get_authorizer = get_authorizer,
2984 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002985 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07002986 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07002987 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07002988};