blob: 57d8db5b055a9fe3663ac374c474ff0a3f875444 [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
Tobias Klauser9e327892010-05-20 10:40:19 +020025static const struct ceph_connection_operations osd_con_ops;
Sage Weilf24e9982009-10-06 11:31:10 -070026
Alex Elderf9d25192013-02-15 11:42:29 -060027static void __send_queued(struct ceph_osd_client *osdc);
Sage Weil6f6c7002011-01-17 20:34:08 -080028static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -070029static void __register_request(struct ceph_osd_client *osdc,
30 struct ceph_osd_request *req);
31static void __unregister_linger_request(struct ceph_osd_client *osdc,
32 struct ceph_osd_request *req);
Sage Weil56e925b2012-01-03 12:34:34 -080033static void __send_request(struct ceph_osd_client *osdc,
34 struct ceph_osd_request *req);
Sage Weilf24e9982009-10-06 11:31:10 -070035
36/*
37 * Implement client access to distributed object storage cluster.
38 *
39 * All data objects are stored within a cluster/cloud of OSDs, or
40 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
41 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
42 * remote daemons serving up and coordinating consistent and safe
43 * access to storage.
44 *
45 * Cluster membership and the mapping of data objects onto storage devices
46 * are described by the osd map.
47 *
48 * We keep track of pending OSD requests (read, write), resubmit
49 * requests to different OSDs when the cluster topology/data layout
50 * change, or retry the affected requests when the communications
51 * channel with an OSD is reset.
52 */
53
54/*
55 * calculate the mapping of a file extent onto an object, and fill out the
56 * request accordingly. shorten extent as necessary if it crosses an
57 * object boundary.
58 *
59 * fill osd op in request message.
60 */
Alex Elderdbe0fc42013-02-15 22:10:17 -060061static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
Alex Eldera19dadf2013-03-13 20:50:01 -050062 u64 *objnum, u64 *objoff, u64 *objlen)
Sage Weilf24e9982009-10-06 11:31:10 -070063{
Alex Elder60e56f12013-02-15 11:42:29 -060064 u64 orig_len = *plen;
Sage Weild63b77f2012-09-24 20:59:48 -070065 int r;
Sage Weilf24e9982009-10-06 11:31:10 -070066
Alex Elder60e56f12013-02-15 11:42:29 -060067 /* object extent? */
Alex Elder75d1c942013-03-13 20:50:00 -050068 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
69 objoff, objlen);
Sage Weild63b77f2012-09-24 20:59:48 -070070 if (r < 0)
71 return r;
Alex Elder75d1c942013-03-13 20:50:00 -050072 if (*objlen < orig_len) {
73 *plen = *objlen;
Alex Elder60e56f12013-02-15 11:42:29 -060074 dout(" skipping last %llu, final file extent %llu~%llu\n",
75 orig_len - *plen, off, *plen);
76 }
77
Alex Elder75d1c942013-03-13 20:50:00 -050078 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
Sage Weilf24e9982009-10-06 11:31:10 -070079
Alex Elder3ff5f382013-02-15 22:10:17 -060080 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -070081}
82
Alex Elderc54d47b2013-04-03 01:28:57 -050083static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
84{
85 memset(osd_data, 0, sizeof (*osd_data));
86 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
87}
88
Alex Eldera4ce40a2013-04-05 01:27:12 -050089static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -050090 struct page **pages, u64 length, u32 alignment,
91 bool pages_from_pool, bool own_pages)
92{
93 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
94 osd_data->pages = pages;
95 osd_data->length = length;
96 osd_data->alignment = alignment;
97 osd_data->pages_from_pool = pages_from_pool;
98 osd_data->own_pages = own_pages;
99}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500100
Alex Eldera4ce40a2013-04-05 01:27:12 -0500101static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500102 struct ceph_pagelist *pagelist)
103{
104 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
105 osd_data->pagelist = pagelist;
106}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500107
108#ifdef CONFIG_BLOCK
Alex Eldera4ce40a2013-04-05 01:27:12 -0500109static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500110 struct bio *bio, size_t bio_length)
111{
112 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
113 osd_data->bio = bio;
114 osd_data->bio_length = bio_length;
115}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500116#endif /* CONFIG_BLOCK */
117
Alex Elder863c7eb2013-04-15 14:50:36 -0500118#define osd_req_op_data(oreq, whch, typ, fld) \
119 ({ \
120 BUG_ON(whch >= (oreq)->r_num_ops); \
121 &(oreq)->r_ops[whch].typ.fld; \
122 })
123
Alex Elder49719772013-02-11 12:33:24 -0600124static struct ceph_osd_data *
125osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
126{
127 BUG_ON(which >= osd_req->r_num_ops);
128
129 return &osd_req->r_ops[which].raw_data_in;
130}
131
Alex Eldera4ce40a2013-04-05 01:27:12 -0500132struct ceph_osd_data *
133osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500134 unsigned int which)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500135{
Alex Elder863c7eb2013-04-15 14:50:36 -0500136 return osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500137}
138EXPORT_SYMBOL(osd_req_op_extent_osd_data);
139
140struct ceph_osd_data *
Alex Eldera4ce40a2013-04-05 01:27:12 -0500141osd_req_op_cls_response_data(struct ceph_osd_request *osd_req,
142 unsigned int which)
143{
Alex Elder863c7eb2013-04-15 14:50:36 -0500144 return osd_req_op_data(osd_req, which, cls, response_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500145}
146EXPORT_SYMBOL(osd_req_op_cls_response_data); /* ??? */
147
Alex Elder49719772013-02-11 12:33:24 -0600148void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
149 unsigned int which, struct page **pages,
150 u64 length, u32 alignment,
151 bool pages_from_pool, bool own_pages)
152{
153 struct ceph_osd_data *osd_data;
154
155 osd_data = osd_req_op_raw_data_in(osd_req, which);
156 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
157 pages_from_pool, own_pages);
158}
159EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
160
Alex Eldera4ce40a2013-04-05 01:27:12 -0500161void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500162 unsigned int which, struct page **pages,
163 u64 length, u32 alignment,
Alex Eldera4ce40a2013-04-05 01:27:12 -0500164 bool pages_from_pool, bool own_pages)
165{
166 struct ceph_osd_data *osd_data;
167
Alex Elder863c7eb2013-04-15 14:50:36 -0500168 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500169 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
170 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500171}
172EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
173
174void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500175 unsigned int which, struct ceph_pagelist *pagelist)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500176{
177 struct ceph_osd_data *osd_data;
178
Alex Elder863c7eb2013-04-15 14:50:36 -0500179 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500180 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500181}
182EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
183
184#ifdef CONFIG_BLOCK
185void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500186 unsigned int which, struct bio *bio, size_t bio_length)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500187{
188 struct ceph_osd_data *osd_data;
Alex Elder863c7eb2013-04-15 14:50:36 -0500189
190 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500191 ceph_osd_data_bio_init(osd_data, bio, bio_length);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500192}
193EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
194#endif /* CONFIG_BLOCK */
195
196static void osd_req_op_cls_request_info_pagelist(
197 struct ceph_osd_request *osd_req,
198 unsigned int which, struct ceph_pagelist *pagelist)
199{
200 struct ceph_osd_data *osd_data;
201
Alex Elder863c7eb2013-04-15 14:50:36 -0500202 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500203 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500204}
205
Alex Elder04017e22013-04-05 14:46:02 -0500206void osd_req_op_cls_request_data_pagelist(
207 struct ceph_osd_request *osd_req,
208 unsigned int which, struct ceph_pagelist *pagelist)
209{
210 struct ceph_osd_data *osd_data;
211
Alex Elder863c7eb2013-04-15 14:50:36 -0500212 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
Alex Elder04017e22013-04-05 14:46:02 -0500213 ceph_osd_data_pagelist_init(osd_data, pagelist);
214}
215EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
216
Alex Elder6c57b552013-04-19 15:34:49 -0500217void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
218 unsigned int which, struct page **pages, u64 length,
219 u32 alignment, bool pages_from_pool, bool own_pages)
220{
221 struct ceph_osd_data *osd_data;
222
223 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
224 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
225 pages_from_pool, own_pages);
226}
227EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
228
Alex Eldera4ce40a2013-04-05 01:27:12 -0500229void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
230 unsigned int which, struct page **pages, u64 length,
231 u32 alignment, bool pages_from_pool, bool own_pages)
232{
233 struct ceph_osd_data *osd_data;
234
Alex Elder863c7eb2013-04-15 14:50:36 -0500235 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500236 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
237 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500238}
239EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
240
Alex Elder23c08a9cb2013-04-03 01:28:58 -0500241static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
242{
243 switch (osd_data->type) {
244 case CEPH_OSD_DATA_TYPE_NONE:
245 return 0;
246 case CEPH_OSD_DATA_TYPE_PAGES:
247 return osd_data->length;
248 case CEPH_OSD_DATA_TYPE_PAGELIST:
249 return (u64)osd_data->pagelist->length;
250#ifdef CONFIG_BLOCK
251 case CEPH_OSD_DATA_TYPE_BIO:
252 return (u64)osd_data->bio_length;
253#endif /* CONFIG_BLOCK */
254 default:
255 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
256 return 0;
257 }
258}
259
Alex Elderc54d47b2013-04-03 01:28:57 -0500260static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
261{
Alex Elder54764922013-04-05 01:27:12 -0500262 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
Alex Elderc54d47b2013-04-03 01:28:57 -0500263 int num_pages;
264
265 num_pages = calc_pages_for((u64)osd_data->alignment,
266 (u64)osd_data->length);
267 ceph_release_page_vector(osd_data->pages, num_pages);
268 }
Alex Elder54764922013-04-05 01:27:12 -0500269 ceph_osd_data_init(osd_data);
270}
271
272static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
273 unsigned int which)
274{
275 struct ceph_osd_req_op *op;
276
277 BUG_ON(which >= osd_req->r_num_ops);
278 op = &osd_req->r_ops[which];
279
280 switch (op->op) {
281 case CEPH_OSD_OP_READ:
282 case CEPH_OSD_OP_WRITE:
283 ceph_osd_data_release(&op->extent.osd_data);
284 break;
285 case CEPH_OSD_OP_CALL:
286 ceph_osd_data_release(&op->cls.request_info);
Alex Elder04017e22013-04-05 14:46:02 -0500287 ceph_osd_data_release(&op->cls.request_data);
Alex Elder54764922013-04-05 01:27:12 -0500288 ceph_osd_data_release(&op->cls.response_data);
289 break;
290 default:
291 break;
292 }
Alex Elderc54d47b2013-04-03 01:28:57 -0500293}
294
Sage Weilf24e9982009-10-06 11:31:10 -0700295/*
296 * requests
297 */
Sage Weil415e49a2009-12-07 13:37:03 -0800298void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700299{
Alex Elderc54d47b2013-04-03 01:28:57 -0500300 struct ceph_osd_request *req;
Alex Elder54764922013-04-05 01:27:12 -0500301 unsigned int which;
Sage Weil415e49a2009-12-07 13:37:03 -0800302
Alex Elderc54d47b2013-04-03 01:28:57 -0500303 req = container_of(kref, struct ceph_osd_request, r_kref);
Sage Weil415e49a2009-12-07 13:37:03 -0800304 if (req->r_request)
305 ceph_msg_put(req->r_request);
Alex Elderace6d3a2013-04-01 16:12:14 -0500306 if (req->r_reply) {
Alex Elder8921d112012-06-01 14:56:43 -0500307 ceph_msg_revoke_incoming(req->r_reply);
Alex Elderab8cb342012-06-04 14:43:32 -0500308 ceph_msg_put(req->r_reply);
Alex Elderace6d3a2013-04-01 16:12:14 -0500309 }
Alex Elder0fff87e2013-02-14 12:16:43 -0600310
Alex Elder54764922013-04-05 01:27:12 -0500311 for (which = 0; which < req->r_num_ops; which++)
312 osd_req_op_data_release(req, which);
Alex Elder0fff87e2013-02-14 12:16:43 -0600313
Sage Weil415e49a2009-12-07 13:37:03 -0800314 ceph_put_snap_context(req->r_snapc);
315 if (req->r_mempool)
316 mempool_free(req, req->r_osdc->req_mempool);
317 else
318 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700319}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700320EXPORT_SYMBOL(ceph_osdc_release_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700321
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700322struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700323 struct ceph_snap_context *snapc,
Sage Weil1b83bef2013-02-25 16:11:12 -0800324 unsigned int num_ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700325 bool use_mempool,
Alex Elder54a54002012-11-13 21:11:15 -0600326 gfp_t gfp_flags)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700327{
328 struct ceph_osd_request *req;
329 struct ceph_msg *msg;
Sage Weil1b83bef2013-02-25 16:11:12 -0800330 size_t msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700331
Alex Elder79528732013-04-03 21:32:51 -0500332 BUILD_BUG_ON(CEPH_OSD_MAX_OP > U16_MAX);
333 BUG_ON(num_ops > CEPH_OSD_MAX_OP);
334
Sage Weil1b83bef2013-02-25 16:11:12 -0800335 msg_size = 4 + 4 + 8 + 8 + 4+8;
336 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
337 msg_size += 1 + 8 + 4 + 4; /* pg_t */
338 msg_size += 4 + MAX_OBJ_NAME_SIZE;
339 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
340 msg_size += 8; /* snapid */
341 msg_size += 8; /* snap_seq */
342 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
343 msg_size += 4;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700344
345 if (use_mempool) {
346 req = mempool_alloc(osdc->req_mempool, gfp_flags);
347 memset(req, 0, sizeof(*req));
348 } else {
349 req = kzalloc(sizeof(*req), gfp_flags);
350 }
351 if (req == NULL)
352 return NULL;
353
354 req->r_osdc = osdc;
355 req->r_mempool = use_mempool;
Alex Elder79528732013-04-03 21:32:51 -0500356 req->r_num_ops = num_ops;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700357
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700358 kref_init(&req->r_kref);
359 init_completion(&req->r_completion);
360 init_completion(&req->r_safe_completion);
Alex Eldera978fa22012-12-17 12:23:48 -0600361 RB_CLEAR_NODE(&req->r_node);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700362 INIT_LIST_HEAD(&req->r_unsafe_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700363 INIT_LIST_HEAD(&req->r_linger_item);
364 INIT_LIST_HEAD(&req->r_linger_osd);
Sage Weil935b6392011-09-16 11:13:17 -0700365 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilcd430452012-07-09 14:31:41 -0700366 INIT_LIST_HEAD(&req->r_osd_item);
367
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700368 /* create reply message */
369 if (use_mempool)
370 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
371 else
372 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
Sage Weilb61c2762011-08-09 15:03:46 -0700373 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700374 if (!msg) {
375 ceph_osdc_put_request(req);
376 return NULL;
377 }
378 req->r_reply = msg;
379
380 /* create request message; allow space for oid */
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700381 if (use_mempool)
382 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
383 else
Sage Weilb61c2762011-08-09 15:03:46 -0700384 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700385 if (!msg) {
386 ceph_osdc_put_request(req);
387 return NULL;
388 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700389
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700390 memset(msg->front.iov_base, 0, msg->front.iov_len);
391
392 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700393
394 return req;
395}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700396EXPORT_SYMBOL(ceph_osdc_alloc_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700397
Alex Eldera8dd0a32013-03-13 20:50:00 -0500398static bool osd_req_opcode_valid(u16 opcode)
399{
400 switch (opcode) {
401 case CEPH_OSD_OP_READ:
402 case CEPH_OSD_OP_STAT:
403 case CEPH_OSD_OP_MAPEXT:
404 case CEPH_OSD_OP_MASKTRUNC:
405 case CEPH_OSD_OP_SPARSE_READ:
406 case CEPH_OSD_OP_NOTIFY:
407 case CEPH_OSD_OP_NOTIFY_ACK:
408 case CEPH_OSD_OP_ASSERT_VER:
409 case CEPH_OSD_OP_WRITE:
410 case CEPH_OSD_OP_WRITEFULL:
411 case CEPH_OSD_OP_TRUNCATE:
412 case CEPH_OSD_OP_ZERO:
413 case CEPH_OSD_OP_DELETE:
414 case CEPH_OSD_OP_APPEND:
415 case CEPH_OSD_OP_STARTSYNC:
416 case CEPH_OSD_OP_SETTRUNC:
417 case CEPH_OSD_OP_TRIMTRUNC:
418 case CEPH_OSD_OP_TMAPUP:
419 case CEPH_OSD_OP_TMAPPUT:
420 case CEPH_OSD_OP_TMAPGET:
421 case CEPH_OSD_OP_CREATE:
422 case CEPH_OSD_OP_ROLLBACK:
423 case CEPH_OSD_OP_WATCH:
424 case CEPH_OSD_OP_OMAPGETKEYS:
425 case CEPH_OSD_OP_OMAPGETVALS:
426 case CEPH_OSD_OP_OMAPGETHEADER:
427 case CEPH_OSD_OP_OMAPGETVALSBYKEYS:
428 case CEPH_OSD_OP_OMAPSETVALS:
429 case CEPH_OSD_OP_OMAPSETHEADER:
430 case CEPH_OSD_OP_OMAPCLEAR:
431 case CEPH_OSD_OP_OMAPRMKEYS:
432 case CEPH_OSD_OP_OMAP_CMP:
433 case CEPH_OSD_OP_CLONERANGE:
434 case CEPH_OSD_OP_ASSERT_SRC_VERSION:
435 case CEPH_OSD_OP_SRC_CMPXATTR:
436 case CEPH_OSD_OP_GETXATTR:
437 case CEPH_OSD_OP_GETXATTRS:
438 case CEPH_OSD_OP_CMPXATTR:
439 case CEPH_OSD_OP_SETXATTR:
440 case CEPH_OSD_OP_SETXATTRS:
441 case CEPH_OSD_OP_RESETXATTRS:
442 case CEPH_OSD_OP_RMXATTR:
443 case CEPH_OSD_OP_PULL:
444 case CEPH_OSD_OP_PUSH:
445 case CEPH_OSD_OP_BALANCEREADS:
446 case CEPH_OSD_OP_UNBALANCEREADS:
447 case CEPH_OSD_OP_SCRUB:
448 case CEPH_OSD_OP_SCRUB_RESERVE:
449 case CEPH_OSD_OP_SCRUB_UNRESERVE:
450 case CEPH_OSD_OP_SCRUB_STOP:
451 case CEPH_OSD_OP_SCRUB_MAP:
452 case CEPH_OSD_OP_WRLOCK:
453 case CEPH_OSD_OP_WRUNLOCK:
454 case CEPH_OSD_OP_RDLOCK:
455 case CEPH_OSD_OP_RDUNLOCK:
456 case CEPH_OSD_OP_UPLOCK:
457 case CEPH_OSD_OP_DNLOCK:
458 case CEPH_OSD_OP_CALL:
459 case CEPH_OSD_OP_PGLS:
460 case CEPH_OSD_OP_PGLS_FILTER:
461 return true;
462 default:
463 return false;
464 }
465}
466
Alex Elder33803f32013-03-13 20:50:00 -0500467/*
468 * This is an osd op init function for opcodes that have no data or
469 * other information associated with them. It also serves as a
470 * common init routine for all the other init functions, below.
471 */
Alex Elderc99d2d42013-04-05 01:27:11 -0500472static struct ceph_osd_req_op *
Alex Elder49719772013-02-11 12:33:24 -0600473_osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elderc99d2d42013-04-05 01:27:11 -0500474 u16 opcode)
Alex Elder33803f32013-03-13 20:50:00 -0500475{
Alex Elderc99d2d42013-04-05 01:27:11 -0500476 struct ceph_osd_req_op *op;
477
478 BUG_ON(which >= osd_req->r_num_ops);
Alex Elder33803f32013-03-13 20:50:00 -0500479 BUG_ON(!osd_req_opcode_valid(opcode));
480
Alex Elderc99d2d42013-04-05 01:27:11 -0500481 op = &osd_req->r_ops[which];
Alex Elder33803f32013-03-13 20:50:00 -0500482 memset(op, 0, sizeof (*op));
Alex Elder33803f32013-03-13 20:50:00 -0500483 op->op = opcode;
Alex Elderc99d2d42013-04-05 01:27:11 -0500484
485 return op;
Alex Elder33803f32013-03-13 20:50:00 -0500486}
487
Alex Elder49719772013-02-11 12:33:24 -0600488void osd_req_op_init(struct ceph_osd_request *osd_req,
489 unsigned int which, u16 opcode)
490{
491 (void)_osd_req_op_init(osd_req, which, opcode);
492}
493EXPORT_SYMBOL(osd_req_op_init);
494
Alex Elderc99d2d42013-04-05 01:27:11 -0500495void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
496 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500497 u64 offset, u64 length,
498 u64 truncate_size, u32 truncate_seq)
499{
Alex Elder49719772013-02-11 12:33:24 -0600500 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
Alex Elder33803f32013-03-13 20:50:00 -0500501 size_t payload_len = 0;
502
503 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
504
Alex Elder33803f32013-03-13 20:50:00 -0500505 op->extent.offset = offset;
506 op->extent.length = length;
507 op->extent.truncate_size = truncate_size;
508 op->extent.truncate_seq = truncate_seq;
509 if (opcode == CEPH_OSD_OP_WRITE)
510 payload_len += length;
511
512 op->payload_len = payload_len;
513}
514EXPORT_SYMBOL(osd_req_op_extent_init);
515
Alex Elderc99d2d42013-04-05 01:27:11 -0500516void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
517 unsigned int which, u64 length)
Alex Eldere5975c72013-03-14 14:09:05 -0500518{
Alex Elderc99d2d42013-04-05 01:27:11 -0500519 struct ceph_osd_req_op *op;
520 u64 previous;
521
522 BUG_ON(which >= osd_req->r_num_ops);
523 op = &osd_req->r_ops[which];
524 previous = op->extent.length;
Alex Eldere5975c72013-03-14 14:09:05 -0500525
526 if (length == previous)
527 return; /* Nothing to do */
528 BUG_ON(length > previous);
529
530 op->extent.length = length;
531 op->payload_len -= previous - length;
532}
533EXPORT_SYMBOL(osd_req_op_extent_update);
534
Alex Elderc99d2d42013-04-05 01:27:11 -0500535void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elder04017e22013-04-05 14:46:02 -0500536 u16 opcode, const char *class, const char *method)
Alex Elder33803f32013-03-13 20:50:00 -0500537{
Alex Elder49719772013-02-11 12:33:24 -0600538 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
Alex Elder5f562df2013-04-05 01:27:12 -0500539 struct ceph_pagelist *pagelist;
Alex Elder33803f32013-03-13 20:50:00 -0500540 size_t payload_len = 0;
541 size_t size;
542
543 BUG_ON(opcode != CEPH_OSD_OP_CALL);
544
Alex Elder5f562df2013-04-05 01:27:12 -0500545 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
546 BUG_ON(!pagelist);
547 ceph_pagelist_init(pagelist);
548
Alex Elder33803f32013-03-13 20:50:00 -0500549 op->cls.class_name = class;
550 size = strlen(class);
551 BUG_ON(size > (size_t) U8_MAX);
552 op->cls.class_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500553 ceph_pagelist_append(pagelist, class, size);
Alex Elder33803f32013-03-13 20:50:00 -0500554 payload_len += size;
555
556 op->cls.method_name = method;
557 size = strlen(method);
558 BUG_ON(size > (size_t) U8_MAX);
559 op->cls.method_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500560 ceph_pagelist_append(pagelist, method, size);
Alex Elder33803f32013-03-13 20:50:00 -0500561 payload_len += size;
562
Alex Eldera4ce40a2013-04-05 01:27:12 -0500563 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
Alex Elder5f562df2013-04-05 01:27:12 -0500564
Alex Elder33803f32013-03-13 20:50:00 -0500565 op->cls.argc = 0; /* currently unused */
566
567 op->payload_len = payload_len;
568}
569EXPORT_SYMBOL(osd_req_op_cls_init);
Alex Elder8c042b02013-04-03 01:28:58 -0500570
Alex Elderc99d2d42013-04-05 01:27:11 -0500571void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
572 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500573 u64 cookie, u64 version, int flag)
574{
Alex Elder49719772013-02-11 12:33:24 -0600575 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
Alex Elder33803f32013-03-13 20:50:00 -0500576
Alex Elderc99d2d42013-04-05 01:27:11 -0500577 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
Alex Elder33803f32013-03-13 20:50:00 -0500578
579 op->watch.cookie = cookie;
Alex Elder9ef1ee52013-04-21 16:51:50 -0500580 op->watch.ver = version;
Alex Elder33803f32013-03-13 20:50:00 -0500581 if (opcode == CEPH_OSD_OP_WATCH && flag)
Alex Elderc99d2d42013-04-05 01:27:11 -0500582 op->watch.flag = (u8)1;
Alex Elder33803f32013-03-13 20:50:00 -0500583}
584EXPORT_SYMBOL(osd_req_op_watch_init);
585
Alex Elder90af3602013-04-05 14:46:01 -0500586static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
Alex Elderec9123c2013-04-05 01:27:12 -0500587 struct ceph_osd_data *osd_data)
588{
589 u64 length = ceph_osd_data_length(osd_data);
590
591 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
592 BUG_ON(length > (u64) SIZE_MAX);
593 if (length)
Alex Elder90af3602013-04-05 14:46:01 -0500594 ceph_msg_data_add_pages(msg, osd_data->pages,
Alex Elderec9123c2013-04-05 01:27:12 -0500595 length, osd_data->alignment);
596 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
597 BUG_ON(!length);
Alex Elder90af3602013-04-05 14:46:01 -0500598 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
Alex Elderec9123c2013-04-05 01:27:12 -0500599#ifdef CONFIG_BLOCK
600 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
Alex Elder90af3602013-04-05 14:46:01 -0500601 ceph_msg_data_add_bio(msg, osd_data->bio, length);
Alex Elderec9123c2013-04-05 01:27:12 -0500602#endif
603 } else {
604 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
605 }
606}
607
Alex Elder175face2013-03-08 13:35:36 -0600608static u64 osd_req_encode_op(struct ceph_osd_request *req,
Alex Elder79528732013-04-03 21:32:51 -0500609 struct ceph_osd_op *dst, unsigned int which)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700610{
Alex Elder79528732013-04-03 21:32:51 -0500611 struct ceph_osd_req_op *src;
Alex Elder04017e22013-04-05 14:46:02 -0500612 struct ceph_osd_data *osd_data;
Alex Elder54d50642013-04-03 01:28:58 -0500613 u64 request_data_len = 0;
Alex Elder04017e22013-04-05 14:46:02 -0500614 u64 data_length;
Alex Elder175face2013-03-08 13:35:36 -0600615
Alex Elder79528732013-04-03 21:32:51 -0500616 BUG_ON(which >= req->r_num_ops);
617 src = &req->r_ops[which];
Alex Eldera8dd0a32013-03-13 20:50:00 -0500618 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
619 pr_err("unrecognized osd opcode %d\n", src->op);
620
621 return 0;
622 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700623
Alex Elder065a68f2012-04-20 15:49:43 -0500624 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600625 case CEPH_OSD_OP_STAT:
Alex Elder49719772013-02-11 12:33:24 -0600626 osd_data = &src->raw_data_in;
627 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Alex Elderfbfab532013-02-08 09:55:48 -0600628 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700629 case CEPH_OSD_OP_READ:
630 case CEPH_OSD_OP_WRITE:
Alex Elder175face2013-03-08 13:35:36 -0600631 if (src->op == CEPH_OSD_OP_WRITE)
Alex Elder54d50642013-04-03 01:28:58 -0500632 request_data_len = src->extent.length;
Alex Elder175face2013-03-08 13:35:36 -0600633 dst->extent.offset = cpu_to_le64(src->extent.offset);
634 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700635 dst->extent.truncate_size =
636 cpu_to_le64(src->extent.truncate_size);
637 dst->extent.truncate_seq =
638 cpu_to_le32(src->extent.truncate_seq);
Alex Elder04017e22013-04-05 14:46:02 -0500639 osd_data = &src->extent.osd_data;
Alex Elder54764922013-04-05 01:27:12 -0500640 if (src->op == CEPH_OSD_OP_WRITE)
Alex Elder04017e22013-04-05 14:46:02 -0500641 ceph_osdc_msg_data_add(req->r_request, osd_data);
Alex Elder54764922013-04-05 01:27:12 -0500642 else
Alex Elder04017e22013-04-05 14:46:02 -0500643 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700644 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700645 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700646 dst->cls.class_len = src->cls.class_len;
647 dst->cls.method_len = src->cls.method_len;
Alex Elder04017e22013-04-05 14:46:02 -0500648 osd_data = &src->cls.request_info;
649 ceph_osdc_msg_data_add(req->r_request, osd_data);
650 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
651 request_data_len = osd_data->pagelist->length;
652
653 osd_data = &src->cls.request_data;
654 data_length = ceph_osd_data_length(osd_data);
655 if (data_length) {
656 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
657 dst->cls.indata_len = cpu_to_le32(data_length);
658 ceph_osdc_msg_data_add(req->r_request, osd_data);
659 src->payload_len += data_length;
660 request_data_len += data_length;
661 }
662 osd_data = &src->cls.response_data;
663 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700664 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700665 case CEPH_OSD_OP_STARTSYNC:
666 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700667 case CEPH_OSD_OP_NOTIFY_ACK:
668 case CEPH_OSD_OP_WATCH:
669 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
670 dst->watch.ver = cpu_to_le64(src->watch.ver);
671 dst->watch.flag = src->watch.flag;
672 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700673 default:
Alex Elder4c464592013-02-15 11:42:30 -0600674 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600675 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600676 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -0500677
678 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700679 }
Alex Eldera8dd0a32013-03-13 20:50:00 -0500680 dst->op = cpu_to_le16(src->op);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700681 dst->payload_len = cpu_to_le32(src->payload_len);
Alex Elder175face2013-03-08 13:35:36 -0600682
Alex Elder54d50642013-04-03 01:28:58 -0500683 return request_data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700684}
685
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700686/*
Sage Weilf24e9982009-10-06 11:31:10 -0700687 * build new request AND message, calculate layout, and adjust file
688 * extent as needed.
689 *
690 * if the file was recently truncated, we include information about its
691 * old and new size so that the object can be updated appropriately. (we
692 * avoid synchronously deleting truncated objects because it's slow.)
693 *
694 * if @do_sync, include a 'startsync' command so that the osd will flush
695 * data quickly.
696 */
697struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
698 struct ceph_file_layout *layout,
699 struct ceph_vino vino,
Alex Elderacead002013-03-14 14:09:05 -0500700 u64 off, u64 *plen, int num_ops,
Sage Weilf24e9982009-10-06 11:31:10 -0700701 int opcode, int flags,
702 struct ceph_snap_context *snapc,
Sage Weilf24e9982009-10-06 11:31:10 -0700703 u32 truncate_seq,
704 u64 truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -0600705 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700706{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700707 struct ceph_osd_request *req;
Alex Elder75d1c942013-03-13 20:50:00 -0500708 u64 objnum = 0;
709 u64 objoff = 0;
710 u64 objlen = 0;
Alex Elderd18d1e22013-03-13 20:50:01 -0500711 u32 object_size;
712 u64 object_base;
Sage Weil68162822012-09-24 21:01:02 -0700713 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700714
Alex Elderd18d1e22013-03-13 20:50:01 -0500715 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700716
Alex Elderacead002013-03-14 14:09:05 -0500717 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600718 GFP_NOFS);
Sage Weil4ad12622011-05-03 09:23:36 -0700719 if (!req)
Sage Weil68162822012-09-24 21:01:02 -0700720 return ERR_PTR(-ENOMEM);
Alex Elder79528732013-04-03 21:32:51 -0500721
Alex Elderd178a9e2012-11-13 21:11:15 -0600722 req->r_flags = flags;
Sage Weilf24e9982009-10-06 11:31:10 -0700723
724 /* calculate max write size */
Alex Eldera19dadf2013-03-13 20:50:01 -0500725 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
Alex Elder3ff5f382013-02-15 22:10:17 -0600726 if (r < 0) {
727 ceph_osdc_put_request(req);
Sage Weil68162822012-09-24 21:01:02 -0700728 return ERR_PTR(r);
Alex Elder3ff5f382013-02-15 22:10:17 -0600729 }
Alex Eldera19dadf2013-03-13 20:50:01 -0500730
Alex Elderd18d1e22013-03-13 20:50:01 -0500731 object_size = le32_to_cpu(layout->fl_object_size);
732 object_base = off - objoff;
733 if (truncate_size <= object_base) {
734 truncate_size = 0;
735 } else {
736 truncate_size -= object_base;
737 if (truncate_size > object_size)
738 truncate_size = object_size;
Alex Eldera19dadf2013-03-13 20:50:01 -0500739 }
Alex Elderd18d1e22013-03-13 20:50:01 -0500740
Alex Elderc99d2d42013-04-05 01:27:11 -0500741 osd_req_op_extent_init(req, 0, opcode, objoff, objlen,
Alex Elderb0270322013-03-13 20:50:01 -0500742 truncate_size, truncate_seq);
Alex Elder8c042b02013-04-03 01:28:58 -0500743
Alex Elderacead002013-03-14 14:09:05 -0500744 /*
745 * A second op in the ops array means the caller wants to
746 * also issue a include a 'startsync' command so that the
747 * osd will flush data quickly.
748 */
749 if (num_ops > 1)
Alex Elderc99d2d42013-04-05 01:27:11 -0500750 osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC);
Alex Elderd18d1e22013-03-13 20:50:01 -0500751
Sage Weilf24e9982009-10-06 11:31:10 -0700752 req->r_file_layout = *layout; /* keep a copy */
753
Alex Elder75d1c942013-03-13 20:50:00 -0500754 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx",
755 vino.ino, objnum);
Alex Elderdbe0fc42013-02-15 22:10:17 -0600756 req->r_oid_len = strlen(req->r_oid);
757
Sage Weilf24e9982009-10-06 11:31:10 -0700758 return req;
759}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700760EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700761
762/*
763 * We keep osd requests in an rbtree, sorted by ->r_tid.
764 */
765static void __insert_request(struct ceph_osd_client *osdc,
766 struct ceph_osd_request *new)
767{
768 struct rb_node **p = &osdc->requests.rb_node;
769 struct rb_node *parent = NULL;
770 struct ceph_osd_request *req = NULL;
771
772 while (*p) {
773 parent = *p;
774 req = rb_entry(parent, struct ceph_osd_request, r_node);
775 if (new->r_tid < req->r_tid)
776 p = &(*p)->rb_left;
777 else if (new->r_tid > req->r_tid)
778 p = &(*p)->rb_right;
779 else
780 BUG();
781 }
782
783 rb_link_node(&new->r_node, parent, p);
784 rb_insert_color(&new->r_node, &osdc->requests);
785}
786
787static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
788 u64 tid)
789{
790 struct ceph_osd_request *req;
791 struct rb_node *n = osdc->requests.rb_node;
792
793 while (n) {
794 req = rb_entry(n, struct ceph_osd_request, r_node);
795 if (tid < req->r_tid)
796 n = n->rb_left;
797 else if (tid > req->r_tid)
798 n = n->rb_right;
799 else
800 return req;
801 }
802 return NULL;
803}
804
805static struct ceph_osd_request *
806__lookup_request_ge(struct ceph_osd_client *osdc,
807 u64 tid)
808{
809 struct ceph_osd_request *req;
810 struct rb_node *n = osdc->requests.rb_node;
811
812 while (n) {
813 req = rb_entry(n, struct ceph_osd_request, r_node);
814 if (tid < req->r_tid) {
815 if (!n->rb_left)
816 return req;
817 n = n->rb_left;
818 } else if (tid > req->r_tid) {
819 n = n->rb_right;
820 } else {
821 return req;
822 }
823 }
824 return NULL;
825}
826
Sage Weil6f6c7002011-01-17 20:34:08 -0800827/*
828 * Resubmit requests pending on the given osd.
829 */
830static void __kick_osd_requests(struct ceph_osd_client *osdc,
831 struct ceph_osd *osd)
832{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700833 struct ceph_osd_request *req, *nreq;
Alex Eldere02493c2013-03-25 18:16:11 -0500834 LIST_HEAD(resend);
Sage Weil6f6c7002011-01-17 20:34:08 -0800835 int err;
836
837 dout("__kick_osd_requests osd%d\n", osd->o_osd);
838 err = __reset_osd(osdc, osd);
Alex Elder685a7552012-12-07 09:57:58 -0600839 if (err)
Sage Weil6f6c7002011-01-17 20:34:08 -0800840 return;
Alex Eldere02493c2013-03-25 18:16:11 -0500841 /*
842 * Build up a list of requests to resend by traversing the
843 * osd's list of requests. Requests for a given object are
844 * sent in tid order, and that is also the order they're
845 * kept on this list. Therefore all requests that are in
846 * flight will be found first, followed by all requests that
847 * have not yet been sent. And to resend requests while
848 * preserving this order we will want to put any sent
849 * requests back on the front of the osd client's unsent
850 * list.
851 *
852 * So we build a separate ordered list of already-sent
853 * requests for the affected osd and splice it onto the
854 * front of the osd client's unsent list. Once we've seen a
855 * request that has not yet been sent we're done. Those
856 * requests are already sitting right where they belong.
857 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800858 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
Alex Eldere02493c2013-03-25 18:16:11 -0500859 if (!req->r_sent)
860 break;
861 list_move_tail(&req->r_req_lru_item, &resend);
862 dout("requeueing %p tid %llu osd%d\n", req, req->r_tid,
Sage Weil6f6c7002011-01-17 20:34:08 -0800863 osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700864 if (!req->r_linger)
865 req->r_flags |= CEPH_OSD_FLAG_RETRY;
866 }
Alex Eldere02493c2013-03-25 18:16:11 -0500867 list_splice(&resend, &osdc->req_unsent);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700868
Alex Eldere02493c2013-03-25 18:16:11 -0500869 /*
870 * Linger requests are re-registered before sending, which
871 * sets up a new tid for each. We add them to the unsent
872 * list at the end to keep things in tid order.
873 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700874 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
875 r_linger_osd) {
Sage Weil77f38e02011-04-06 09:09:16 -0700876 /*
877 * reregister request prior to unregistering linger so
878 * that r_osd is preserved.
879 */
880 BUG_ON(!list_empty(&req->r_req_lru_item));
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700881 __register_request(osdc, req);
Alex Eldere02493c2013-03-25 18:16:11 -0500882 list_add_tail(&req->r_req_lru_item, &osdc->req_unsent);
Alex Elderad885922013-03-25 18:16:11 -0500883 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
Sage Weil77f38e02011-04-06 09:09:16 -0700884 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700885 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
886 osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800887 }
888}
889
Sage Weilf24e9982009-10-06 11:31:10 -0700890/*
Sage Weil81b024e2009-10-09 10:29:18 -0700891 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700892 */
893static void osd_reset(struct ceph_connection *con)
894{
895 struct ceph_osd *osd = con->private;
896 struct ceph_osd_client *osdc;
897
898 if (!osd)
899 return;
900 dout("osd_reset osd%d\n", osd->o_osd);
901 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -0700902 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -0800903 mutex_lock(&osdc->request_mutex);
904 __kick_osd_requests(osdc, osd);
Alex Elderf9d25192013-02-15 11:42:29 -0600905 __send_queued(osdc);
Sage Weil83aff952012-11-28 12:28:24 -0800906 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -0700907 up_read(&osdc->map_sem);
908}
909
910/*
911 * Track open sessions with osds.
912 */
Alex Eldere10006f2012-05-26 23:26:43 -0500913static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -0700914{
915 struct ceph_osd *osd;
916
917 osd = kzalloc(sizeof(*osd), GFP_NOFS);
918 if (!osd)
919 return NULL;
920
921 atomic_set(&osd->o_ref, 1);
922 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -0500923 osd->o_osd = onum;
Alex Elderf4077312012-12-06 07:22:04 -0600924 RB_CLEAR_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -0700925 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700926 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800927 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700928 osd->o_incarnation = 1;
929
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700930 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800931
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800932 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700933 return osd;
934}
935
936static struct ceph_osd *get_osd(struct ceph_osd *osd)
937{
938 if (atomic_inc_not_zero(&osd->o_ref)) {
939 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
940 atomic_read(&osd->o_ref));
941 return osd;
942 } else {
943 dout("get_osd %p FAIL\n", osd);
944 return NULL;
945 }
946}
947
948static void put_osd(struct ceph_osd *osd)
949{
950 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
951 atomic_read(&osd->o_ref) - 1);
Alex Eldera2556512012-05-16 15:16:39 -0500952 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
Sage Weil79494d12010-05-27 14:15:49 -0700953 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
954
Sage Weil27859f92013-03-25 10:26:14 -0700955 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -0700956 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -0700957 }
Sage Weilf24e9982009-10-06 11:31:10 -0700958}
959
960/*
961 * remove an osd from our map
962 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800963static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700964{
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800965 dout("__remove_osd %p\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700966 BUG_ON(!list_empty(&osd->o_requests));
967 rb_erase(&osd->o_node, &osdc->osds);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800968 list_del_init(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700969 ceph_con_close(&osd->o_con);
970 put_osd(osd);
971}
972
Sage Weilaca420b2011-08-31 14:45:53 -0700973static void remove_all_osds(struct ceph_osd_client *osdc)
974{
Jiaju Zhang048a9d22012-07-20 08:18:36 -0500975 dout("%s %p\n", __func__, osdc);
Sage Weilaca420b2011-08-31 14:45:53 -0700976 mutex_lock(&osdc->request_mutex);
977 while (!RB_EMPTY_ROOT(&osdc->osds)) {
978 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
979 struct ceph_osd, o_node);
980 __remove_osd(osdc, osd);
981 }
982 mutex_unlock(&osdc->request_mutex);
983}
984
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800985static void __move_osd_to_lru(struct ceph_osd_client *osdc,
986 struct ceph_osd *osd)
987{
988 dout("__move_osd_to_lru %p\n", osd);
989 BUG_ON(!list_empty(&osd->o_osd_lru));
990 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700991 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800992}
993
994static void __remove_osd_from_lru(struct ceph_osd *osd)
995{
996 dout("__remove_osd_from_lru %p\n", osd);
997 if (!list_empty(&osd->o_osd_lru))
998 list_del_init(&osd->o_osd_lru);
999}
1000
Sage Weilaca420b2011-08-31 14:45:53 -07001001static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001002{
1003 struct ceph_osd *osd, *nosd;
1004
1005 dout("__remove_old_osds %p\n", osdc);
1006 mutex_lock(&osdc->request_mutex);
1007 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -07001008 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001009 break;
1010 __remove_osd(osdc, osd);
1011 }
1012 mutex_unlock(&osdc->request_mutex);
1013}
1014
Sage Weilf24e9982009-10-06 11:31:10 -07001015/*
1016 * reset osd connect
1017 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001018static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001019{
Alex Elderc3acb182012-12-07 09:57:58 -06001020 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -07001021
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001022 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001023 if (list_empty(&osd->o_requests) &&
1024 list_empty(&osd->o_linger_requests)) {
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001025 __remove_osd(osdc, osd);
Alex Elderc3acb182012-12-07 09:57:58 -06001026
1027 return -ENODEV;
1028 }
1029
1030 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1031 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1032 !ceph_con_opened(&osd->o_con)) {
1033 struct ceph_osd_request *req;
1034
Sage Weil87b315a2010-03-22 14:51:18 -07001035 dout(" osd addr hasn't changed and connection never opened,"
1036 " letting msgr retry");
1037 /* touch each r_stamp for handle_timeout()'s benfit */
1038 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1039 req->r_stamp = jiffies;
Alex Elderc3acb182012-12-07 09:57:58 -06001040
1041 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -07001042 }
Alex Elderc3acb182012-12-07 09:57:58 -06001043
1044 ceph_con_close(&osd->o_con);
1045 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1046 osd->o_incarnation++;
1047
1048 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001049}
1050
1051static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1052{
1053 struct rb_node **p = &osdc->osds.rb_node;
1054 struct rb_node *parent = NULL;
1055 struct ceph_osd *osd = NULL;
1056
Sage Weilaca420b2011-08-31 14:45:53 -07001057 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001058 while (*p) {
1059 parent = *p;
1060 osd = rb_entry(parent, struct ceph_osd, o_node);
1061 if (new->o_osd < osd->o_osd)
1062 p = &(*p)->rb_left;
1063 else if (new->o_osd > osd->o_osd)
1064 p = &(*p)->rb_right;
1065 else
1066 BUG();
1067 }
1068
1069 rb_link_node(&new->o_node, parent, p);
1070 rb_insert_color(&new->o_node, &osdc->osds);
1071}
1072
1073static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1074{
1075 struct ceph_osd *osd;
1076 struct rb_node *n = osdc->osds.rb_node;
1077
1078 while (n) {
1079 osd = rb_entry(n, struct ceph_osd, o_node);
1080 if (o < osd->o_osd)
1081 n = n->rb_left;
1082 else if (o > osd->o_osd)
1083 n = n->rb_right;
1084 else
1085 return osd;
1086 }
1087 return NULL;
1088}
1089
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001090static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1091{
1092 schedule_delayed_work(&osdc->timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001093 osdc->client->options->osd_keepalive_timeout * HZ);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001094}
1095
1096static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1097{
1098 cancel_delayed_work(&osdc->timeout_work);
1099}
Sage Weilf24e9982009-10-06 11:31:10 -07001100
1101/*
1102 * Register request, assign tid. If this is the first request, set up
1103 * the timeout event.
1104 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001105static void __register_request(struct ceph_osd_client *osdc,
1106 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001107{
Sage Weilf24e9982009-10-06 11:31:10 -07001108 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -08001109 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -07001110 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001111 __insert_request(osdc, req);
1112 ceph_osdc_get_request(req);
1113 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -07001114 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001115 dout(" first request, scheduling timeout\n");
1116 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001117 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001118}
1119
Sage Weilf24e9982009-10-06 11:31:10 -07001120/*
1121 * called under osdc->request_mutex
1122 */
1123static void __unregister_request(struct ceph_osd_client *osdc,
1124 struct ceph_osd_request *req)
1125{
Sage Weil35f9f8a2012-05-16 15:16:38 -05001126 if (RB_EMPTY_NODE(&req->r_node)) {
1127 dout("__unregister_request %p tid %lld not registered\n",
1128 req, req->r_tid);
1129 return;
1130 }
1131
Sage Weilf24e9982009-10-06 11:31:10 -07001132 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1133 rb_erase(&req->r_node, &osdc->requests);
1134 osdc->num_requests--;
1135
Sage Weil0ba64782009-10-08 16:57:16 -07001136 if (req->r_osd) {
1137 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -05001138 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -07001139
1140 list_del_init(&req->r_osd_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001141 if (list_empty(&req->r_osd->o_requests) &&
1142 list_empty(&req->r_osd->o_linger_requests)) {
1143 dout("moving osd to %p lru\n", req->r_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001144 __move_osd_to_lru(osdc, req->r_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001145 }
Sage Weilfbdb9192011-03-29 12:11:06 -07001146 if (list_empty(&req->r_linger_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001147 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -07001148 }
Sage Weilf24e9982009-10-06 11:31:10 -07001149
Alex Elder7d5f2482012-11-29 08:37:03 -06001150 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001151 ceph_osdc_put_request(req);
1152
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001153 if (osdc->num_requests == 0) {
1154 dout(" no requests, canceling timeout\n");
1155 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001156 }
1157}
1158
1159/*
1160 * Cancel a previously queued request message
1161 */
1162static void __cancel_request(struct ceph_osd_request *req)
1163{
Sage Weil6bc18872010-09-27 10:18:52 -07001164 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -05001165 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001166 req->r_sent = 0;
1167 }
1168}
1169
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001170static void __register_linger_request(struct ceph_osd_client *osdc,
1171 struct ceph_osd_request *req)
1172{
1173 dout("__register_linger_request %p\n", req);
1174 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -07001175 if (req->r_osd)
1176 list_add_tail(&req->r_linger_osd,
1177 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001178}
1179
1180static void __unregister_linger_request(struct ceph_osd_client *osdc,
1181 struct ceph_osd_request *req)
1182{
1183 dout("__unregister_linger_request %p\n", req);
Alex Elder61c74032012-12-06 09:37:23 -06001184 list_del_init(&req->r_linger_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001185 if (req->r_osd) {
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001186 list_del_init(&req->r_linger_osd);
1187
1188 if (list_empty(&req->r_osd->o_requests) &&
1189 list_empty(&req->r_osd->o_linger_requests)) {
1190 dout("moving osd to %p lru\n", req->r_osd);
1191 __move_osd_to_lru(osdc, req->r_osd);
1192 }
Sage Weilfbdb9192011-03-29 12:11:06 -07001193 if (list_empty(&req->r_osd_item))
1194 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001195 }
1196}
1197
1198void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
1199 struct ceph_osd_request *req)
1200{
1201 mutex_lock(&osdc->request_mutex);
1202 if (req->r_linger) {
1203 __unregister_linger_request(osdc, req);
1204 ceph_osdc_put_request(req);
1205 }
1206 mutex_unlock(&osdc->request_mutex);
1207}
1208EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
1209
1210void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1211 struct ceph_osd_request *req)
1212{
1213 if (!req->r_linger) {
1214 dout("set_request_linger %p\n", req);
1215 req->r_linger = 1;
1216 /*
1217 * caller is now responsible for calling
1218 * unregister_linger_request
1219 */
1220 ceph_osdc_get_request(req);
1221 }
1222}
1223EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1224
Sage Weilf24e9982009-10-06 11:31:10 -07001225/*
1226 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1227 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001228 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -08001229 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -07001230 *
1231 * Return 0 if unchanged, 1 if changed, or negative on error.
1232 *
1233 * Caller should hold map_sem for read and request_mutex.
1234 */
Sage Weil6f6c7002011-01-17 20:34:08 -08001235static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -07001236 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -07001237{
Sage Weil5b191d92013-02-23 10:38:16 -08001238 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -07001239 int acting[CEPH_PG_MAX_SIZE];
1240 int o = -1, num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001241 int err;
Sage Weilf24e9982009-10-06 11:31:10 -07001242
Sage Weil6f6c7002011-01-17 20:34:08 -08001243 dout("map_request %p tid %lld\n", req, req->r_tid);
Alex Elder41766f82013-03-01 18:00:15 -06001244 err = ceph_calc_ceph_pg(&pgid, req->r_oid, osdc->osdmap,
1245 ceph_file_layout_pg_pool(req->r_file_layout));
Sage Weil6f6c7002011-01-17 20:34:08 -08001246 if (err) {
1247 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -07001248 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -08001249 }
Sage Weil7740a422010-01-08 15:58:25 -08001250 req->r_pgid = pgid;
1251
Sage Weild85b7052010-05-10 10:24:48 -07001252 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
1253 if (err > 0) {
1254 o = acting[0];
1255 num = err;
1256 }
Sage Weilf24e9982009-10-06 11:31:10 -07001257
Sage Weil38d64532011-10-14 13:33:55 -07001258 if ((!force_resend &&
1259 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -07001260 req->r_sent >= req->r_osd->o_incarnation &&
1261 req->r_num_pg_osds == num &&
1262 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001263 (req->r_osd == NULL && o == -1))
1264 return 0; /* no change */
1265
Sage Weil5b191d92013-02-23 10:38:16 -08001266 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1267 req->r_tid, pgid.pool, pgid.seed, o,
Sage Weilf24e9982009-10-06 11:31:10 -07001268 req->r_osd ? req->r_osd->o_osd : -1);
1269
Sage Weild85b7052010-05-10 10:24:48 -07001270 /* record full pg acting set */
1271 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1272 req->r_num_pg_osds = num;
1273
Sage Weilf24e9982009-10-06 11:31:10 -07001274 if (req->r_osd) {
1275 __cancel_request(req);
1276 list_del_init(&req->r_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001277 req->r_osd = NULL;
1278 }
1279
1280 req->r_osd = __lookup_osd(osdc, o);
1281 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -08001282 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -05001283 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -08001284 if (!req->r_osd) {
1285 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001286 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001287 }
Sage Weilf24e9982009-10-06 11:31:10 -07001288
Sage Weil6f6c7002011-01-17 20:34:08 -08001289 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001290 __insert_osd(osdc, req->r_osd);
1291
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001292 ceph_con_open(&req->r_osd->o_con,
1293 CEPH_ENTITY_TYPE_OSD, o,
1294 &osdc->osdmap->osd_addr[o]);
Sage Weilf24e9982009-10-06 11:31:10 -07001295 }
1296
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001297 if (req->r_osd) {
1298 __remove_osd_from_lru(req->r_osd);
Alex Elderad885922013-03-25 18:16:11 -05001299 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1300 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
Sage Weil6f6c7002011-01-17 20:34:08 -08001301 } else {
Alex Elderad885922013-03-25 18:16:11 -05001302 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001303 }
Sage Weild85b7052010-05-10 10:24:48 -07001304 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001305
1306out:
Sage Weilf24e9982009-10-06 11:31:10 -07001307 return err;
1308}
1309
1310/*
1311 * caller should hold map_sem (for read) and request_mutex
1312 */
Sage Weil56e925b2012-01-03 12:34:34 -08001313static void __send_request(struct ceph_osd_client *osdc,
1314 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001315{
Sage Weil1b83bef2013-02-25 16:11:12 -08001316 void *p;
Sage Weilf24e9982009-10-06 11:31:10 -07001317
Sage Weil1b83bef2013-02-25 16:11:12 -08001318 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1319 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1320 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001321
Sage Weil1b83bef2013-02-25 16:11:12 -08001322 /* fill in message content that changes each time we send it */
1323 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1324 put_unaligned_le32(req->r_flags, req->r_request_flags);
1325 put_unaligned_le64(req->r_pgid.pool, req->r_request_pool);
1326 p = req->r_request_pgid;
1327 ceph_encode_64(&p, req->r_pgid.pool);
1328 ceph_encode_32(&p, req->r_pgid.seed);
1329 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1330 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1331 sizeof(req->r_reassert_version));
Sage Weil2169aea2013-02-25 16:13:08 -08001332
Sage Weil3dd72fc2010-03-22 14:42:30 -07001333 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001334 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001335
1336 ceph_msg_get(req->r_request); /* send consumes a ref */
Alex Elder26be8802013-04-15 11:20:42 -05001337
1338 /* Mark the request unsafe if this is the first timet's being sent. */
1339
1340 if (!req->r_sent && req->r_unsafe_callback)
1341 req->r_unsafe_callback(req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07001342 req->r_sent = req->r_osd->o_incarnation;
Alex Elder26be8802013-04-15 11:20:42 -05001343
1344 ceph_con_send(&req->r_osd->o_con, req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001345}
1346
1347/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001348 * Send any requests in the queue (req_unsent).
1349 */
Alex Elderf9d25192013-02-15 11:42:29 -06001350static void __send_queued(struct ceph_osd_client *osdc)
Sage Weil6f6c7002011-01-17 20:34:08 -08001351{
1352 struct ceph_osd_request *req, *tmp;
1353
Alex Elderf9d25192013-02-15 11:42:29 -06001354 dout("__send_queued\n");
1355 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
Sage Weil6f6c7002011-01-17 20:34:08 -08001356 __send_request(osdc, req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001357}
1358
1359/*
Sage Weilf24e9982009-10-06 11:31:10 -07001360 * Timeout callback, called every N seconds when 1 or more osd
1361 * requests has been active for more than N seconds. When this
1362 * happens, we ping all OSDs with requests who have timed out to
1363 * ensure any communications channel reset is detected. Reset the
1364 * request timeouts another N seconds in the future as we go.
1365 * Reschedule the timeout event another N seconds in future (unless
1366 * there are no open requests).
1367 */
1368static void handle_timeout(struct work_struct *work)
1369{
1370 struct ceph_osd_client *osdc =
1371 container_of(work, struct ceph_osd_client, timeout_work.work);
Sage Weil83aff952012-11-28 12:28:24 -08001372 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001373 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001374 unsigned long keepalive =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001375 osdc->client->options->osd_keepalive_timeout * HZ;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001376 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001377 dout("timeout\n");
1378 down_read(&osdc->map_sem);
1379
1380 ceph_monc_request_next_osdmap(&osdc->client->monc);
1381
1382 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001383
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001384 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001385 * ping osds that are a bit slow. this ensures that if there
1386 * is a break in the TCP connection we will notice, and reopen
1387 * a connection with that osd (from the fault callback).
1388 */
1389 INIT_LIST_HEAD(&slow_osds);
1390 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Sage Weil3dd72fc2010-03-22 14:42:30 -07001391 if (time_before(jiffies, req->r_stamp + keepalive))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001392 break;
1393
1394 osd = req->r_osd;
1395 BUG_ON(!osd);
1396 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001397 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001398 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1399 }
1400 while (!list_empty(&slow_osds)) {
1401 osd = list_entry(slow_osds.next, struct ceph_osd,
1402 o_keepalive_item);
1403 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001404 ceph_con_keepalive(&osd->o_con);
1405 }
1406
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001407 __schedule_osd_timeout(osdc);
Alex Elderf9d25192013-02-15 11:42:29 -06001408 __send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001409 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001410 up_read(&osdc->map_sem);
1411}
1412
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001413static void handle_osds_timeout(struct work_struct *work)
1414{
1415 struct ceph_osd_client *osdc =
1416 container_of(work, struct ceph_osd_client,
1417 osds_timeout_work.work);
1418 unsigned long delay =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001419 osdc->client->options->osd_idle_ttl * HZ >> 2;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001420
1421 dout("osds timeout\n");
1422 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001423 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001424 up_read(&osdc->map_sem);
1425
1426 schedule_delayed_work(&osdc->osds_timeout_work,
1427 round_jiffies_relative(delay));
1428}
1429
Sage Weil25845472011-06-03 09:37:09 -07001430static void complete_request(struct ceph_osd_request *req)
1431{
Alex Elder26be8802013-04-15 11:20:42 -05001432 if (req->r_unsafe_callback)
1433 req->r_unsafe_callback(req, false);
Sage Weil25845472011-06-03 09:37:09 -07001434 complete_all(&req->r_safe_completion); /* fsync waiter */
1435}
1436
Sage Weilf24e9982009-10-06 11:31:10 -07001437/*
1438 * handle osd op reply. either call the callback if it is specified,
1439 * or do the completion to wake up the waiting thread.
1440 */
Sage Weil350b1c32009-12-22 10:45:45 -08001441static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1442 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -07001443{
Sage Weil1b83bef2013-02-25 16:11:12 -08001444 void *p, *end;
Sage Weilf24e9982009-10-06 11:31:10 -07001445 struct ceph_osd_request *req;
1446 u64 tid;
Sage Weil1b83bef2013-02-25 16:11:12 -08001447 int object_len;
Alex Elder79528732013-04-03 21:32:51 -05001448 unsigned int numops;
1449 int payload_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001450 s32 result;
Sage Weil1b83bef2013-02-25 16:11:12 -08001451 s32 retry_attempt;
1452 struct ceph_pg pg;
1453 int err;
1454 u32 reassert_epoch;
1455 u64 reassert_version;
1456 u32 osdmap_epoch;
Alex Elder0d5af162013-02-27 10:26:25 -06001457 int already_completed;
Alex Elder9fc6e062013-04-03 01:28:57 -05001458 u32 bytes;
Alex Elder79528732013-04-03 21:32:51 -05001459 unsigned int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001460
Sage Weil6df058c2009-12-22 11:24:33 -08001461 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil1b83bef2013-02-25 16:11:12 -08001462 dout("handle_reply %p tid %llu\n", msg, tid);
1463
1464 p = msg->front.iov_base;
1465 end = p + msg->front.iov_len;
1466
1467 ceph_decode_need(&p, end, 4, bad);
1468 object_len = ceph_decode_32(&p);
1469 ceph_decode_need(&p, end, object_len, bad);
1470 p += object_len;
1471
Alex Elderef4859d2013-04-01 18:58:26 -05001472 err = ceph_decode_pgid(&p, end, &pg);
Sage Weil1b83bef2013-02-25 16:11:12 -08001473 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001474 goto bad;
Sage Weil1b83bef2013-02-25 16:11:12 -08001475
1476 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1477 flags = ceph_decode_64(&p);
1478 result = ceph_decode_32(&p);
1479 reassert_epoch = ceph_decode_32(&p);
1480 reassert_version = ceph_decode_64(&p);
1481 osdmap_epoch = ceph_decode_32(&p);
1482
Sage Weilf24e9982009-10-06 11:31:10 -07001483 /* lookup */
1484 mutex_lock(&osdc->request_mutex);
1485 req = __lookup_request(osdc, tid);
1486 if (req == NULL) {
1487 dout("handle_reply tid %llu dne\n", tid);
Alex Elder8058fd42013-04-01 18:58:26 -05001488 goto bad_mutex;
Sage Weilf24e9982009-10-06 11:31:10 -07001489 }
1490 ceph_osdc_get_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001491
1492 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1493 req, result);
1494
1495 ceph_decode_need(&p, end, 4, bad);
1496 numops = ceph_decode_32(&p);
1497 if (numops > CEPH_OSD_MAX_OP)
1498 goto bad_put;
1499 if (numops != req->r_num_ops)
1500 goto bad_put;
1501 payload_len = 0;
1502 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad);
1503 for (i = 0; i < numops; i++) {
1504 struct ceph_osd_op *op = p;
1505 int len;
1506
1507 len = le32_to_cpu(op->payload_len);
1508 req->r_reply_op_len[i] = len;
1509 dout(" op %d has %d bytes\n", i, len);
1510 payload_len += len;
1511 p += sizeof(*op);
1512 }
Alex Elder9fc6e062013-04-03 01:28:57 -05001513 bytes = le32_to_cpu(msg->hdr.data_len);
1514 if (payload_len != bytes) {
Sage Weil1b83bef2013-02-25 16:11:12 -08001515 pr_warning("sum of op payload lens %d != data_len %d",
Alex Elder9fc6e062013-04-03 01:28:57 -05001516 payload_len, bytes);
Sage Weil1b83bef2013-02-25 16:11:12 -08001517 goto bad_put;
1518 }
1519
1520 ceph_decode_need(&p, end, 4 + numops * 4, bad);
1521 retry_attempt = ceph_decode_32(&p);
1522 for (i = 0; i < numops; i++)
1523 req->r_reply_op_result[i] = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001524
Sage Weilf24e9982009-10-06 11:31:10 -07001525 if (!req->r_got_reply) {
Sage Weilf24e9982009-10-06 11:31:10 -07001526
Sage Weil1b83bef2013-02-25 16:11:12 -08001527 req->r_result = result;
Sage Weilf24e9982009-10-06 11:31:10 -07001528 dout("handle_reply result %d bytes %d\n", req->r_result,
1529 bytes);
1530 if (req->r_result == 0)
1531 req->r_result = bytes;
1532
1533 /* in case this is a write and we need to replay, */
Sage Weil1b83bef2013-02-25 16:11:12 -08001534 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1535 req->r_reassert_version.version = cpu_to_le64(reassert_version);
Sage Weilf24e9982009-10-06 11:31:10 -07001536
1537 req->r_got_reply = 1;
1538 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1539 dout("handle_reply tid %llu dup ack\n", tid);
Sage Weil34b43a52009-12-01 12:23:54 -08001540 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001541 goto done;
1542 }
1543
1544 dout("handle_reply tid %llu flags %d\n", tid, flags);
1545
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001546 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1547 __register_linger_request(osdc, req);
1548
Sage Weilf24e9982009-10-06 11:31:10 -07001549 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001550 if (result < 0 ||
1551 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001552 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1553 __unregister_request(osdc, req);
1554
Alex Elder0d5af162013-02-27 10:26:25 -06001555 already_completed = req->r_completed;
1556 req->r_completed = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07001557 mutex_unlock(&osdc->request_mutex);
Alex Elder0d5af162013-02-27 10:26:25 -06001558 if (already_completed)
1559 goto done;
Sage Weilf24e9982009-10-06 11:31:10 -07001560
1561 if (req->r_callback)
1562 req->r_callback(req, msg);
1563 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001564 complete_all(&req->r_completion);
Sage Weilf24e9982009-10-06 11:31:10 -07001565
Sage Weil25845472011-06-03 09:37:09 -07001566 if (flags & CEPH_OSD_FLAG_ONDISK)
1567 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001568
1569done:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001570 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001571 ceph_osdc_put_request(req);
1572 return;
1573
Sage Weil1b83bef2013-02-25 16:11:12 -08001574bad_put:
1575 ceph_osdc_put_request(req);
Alex Elder8058fd42013-04-01 18:58:26 -05001576bad_mutex:
1577 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001578bad:
Sage Weil1b83bef2013-02-25 16:11:12 -08001579 pr_err("corrupt osd_op_reply got %d %d\n",
1580 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001581 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001582}
1583
Sage Weil6f6c7002011-01-17 20:34:08 -08001584static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001585{
Sage Weilf24e9982009-10-06 11:31:10 -07001586 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001587
Sage Weil6f6c7002011-01-17 20:34:08 -08001588 for (p = rb_first(&osdc->osds); p; p = n) {
1589 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001590
Sage Weil6f6c7002011-01-17 20:34:08 -08001591 n = rb_next(p);
1592 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1593 memcmp(&osd->o_con.peer_addr,
1594 ceph_osd_addr(osdc->osdmap,
1595 osd->o_osd),
1596 sizeof(struct ceph_entity_addr)) != 0)
1597 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001598 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001599}
1600
1601/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001602 * Requeue requests whose mapping to an OSD has changed. If requests map to
1603 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001604 *
Alex Eldere6d50f62012-12-26 14:31:40 -06001605 * Caller should hold map_sem for read.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001606 */
Sage Weil38d64532011-10-14 13:33:55 -07001607static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001608{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001609 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001610 struct rb_node *p;
1611 int needmap = 0;
1612 int err;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001613
Sage Weil38d64532011-10-14 13:33:55 -07001614 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001615 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07001616 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08001617 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07001618 p = rb_next(p);
Alex Elderab60b162012-12-19 15:52:36 -06001619
1620 /*
1621 * For linger requests that have not yet been
1622 * registered, move them to the linger list; they'll
1623 * be sent to the osd in the loop below. Unregister
1624 * the request before re-registering it as a linger
1625 * request to ensure the __map_request() below
1626 * will decide it needs to be sent.
1627 */
1628 if (req->r_linger && list_empty(&req->r_linger_item)) {
1629 dout("%p tid %llu restart on osd%d\n",
1630 req, req->r_tid,
1631 req->r_osd ? req->r_osd->o_osd : -1);
1632 __unregister_request(osdc, req);
1633 __register_linger_request(osdc, req);
1634 continue;
1635 }
1636
Sage Weil38d64532011-10-14 13:33:55 -07001637 err = __map_request(osdc, req, force_resend);
Sage Weil6f6c7002011-01-17 20:34:08 -08001638 if (err < 0)
1639 continue; /* error */
1640 if (req->r_osd == NULL) {
1641 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1642 needmap++; /* request a newer map */
1643 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07001644 if (!req->r_linger) {
1645 dout("%p tid %llu requeued on osd%d\n", req,
1646 req->r_tid,
1647 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001648 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07001649 }
1650 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001651 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001652
1653 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1654 r_linger_item) {
1655 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1656
Sage Weil38d64532011-10-14 13:33:55 -07001657 err = __map_request(osdc, req, force_resend);
Alex Elderab60b162012-12-19 15:52:36 -06001658 dout("__map_request returned %d\n", err);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001659 if (err == 0)
1660 continue; /* no change and no osd was specified */
1661 if (err < 0)
1662 continue; /* hrm! */
1663 if (req->r_osd == NULL) {
1664 dout("tid %llu maps to no valid osd\n", req->r_tid);
1665 needmap++; /* request a newer map */
1666 continue;
1667 }
1668
1669 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1670 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001671 __register_request(osdc, req);
Alex Elderc89ce052012-12-06 07:22:04 -06001672 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001673 }
Sage Weilf24e9982009-10-06 11:31:10 -07001674 mutex_unlock(&osdc->request_mutex);
1675
1676 if (needmap) {
1677 dout("%d requests for down osds, need new map\n", needmap);
1678 ceph_monc_request_next_osdmap(&osdc->client->monc);
1679 }
Alex Eldere6d50f62012-12-26 14:31:40 -06001680 reset_changed_osds(osdc);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001681}
Sage Weil6f6c7002011-01-17 20:34:08 -08001682
1683
Sage Weilf24e9982009-10-06 11:31:10 -07001684/*
1685 * Process updated osd map.
1686 *
1687 * The message contains any number of incremental and full maps, normally
1688 * indicating some sort of topology change in the cluster. Kick requests
1689 * off to different OSDs as needed.
1690 */
1691void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1692{
1693 void *p, *end, *next;
1694 u32 nr_maps, maplen;
1695 u32 epoch;
1696 struct ceph_osdmap *newmap = NULL, *oldmap;
1697 int err;
1698 struct ceph_fsid fsid;
1699
1700 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1701 p = msg->front.iov_base;
1702 end = p + msg->front.iov_len;
1703
1704 /* verify fsid */
1705 ceph_decode_need(&p, end, sizeof(fsid), bad);
1706 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08001707 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1708 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001709
1710 down_write(&osdc->map_sem);
1711
1712 /* incremental maps */
1713 ceph_decode_32_safe(&p, end, nr_maps, bad);
1714 dout(" %d inc maps\n", nr_maps);
1715 while (nr_maps > 0) {
1716 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001717 epoch = ceph_decode_32(&p);
1718 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001719 ceph_decode_need(&p, end, maplen, bad);
1720 next = p + maplen;
1721 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1722 dout("applying incremental map %u len %d\n",
1723 epoch, maplen);
1724 newmap = osdmap_apply_incremental(&p, next,
1725 osdc->osdmap,
Alex Elder15d98822012-05-26 23:26:43 -05001726 &osdc->client->msgr);
Sage Weilf24e9982009-10-06 11:31:10 -07001727 if (IS_ERR(newmap)) {
1728 err = PTR_ERR(newmap);
1729 goto bad;
1730 }
Sage Weil30dc6382009-12-21 14:49:37 -08001731 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001732 if (newmap != osdc->osdmap) {
1733 ceph_osdmap_destroy(osdc->osdmap);
1734 osdc->osdmap = newmap;
1735 }
Sage Weil38d64532011-10-14 13:33:55 -07001736 kick_requests(osdc, 0);
Sage Weilf24e9982009-10-06 11:31:10 -07001737 } else {
1738 dout("ignoring incremental map %u len %d\n",
1739 epoch, maplen);
1740 }
1741 p = next;
1742 nr_maps--;
1743 }
1744 if (newmap)
1745 goto done;
1746
1747 /* full maps */
1748 ceph_decode_32_safe(&p, end, nr_maps, bad);
1749 dout(" %d full maps\n", nr_maps);
1750 while (nr_maps) {
1751 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001752 epoch = ceph_decode_32(&p);
1753 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001754 ceph_decode_need(&p, end, maplen, bad);
1755 if (nr_maps > 1) {
1756 dout("skipping non-latest full map %u len %d\n",
1757 epoch, maplen);
1758 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1759 dout("skipping full map %u len %d, "
1760 "older than our %u\n", epoch, maplen,
1761 osdc->osdmap->epoch);
1762 } else {
Sage Weil38d64532011-10-14 13:33:55 -07001763 int skipped_map = 0;
1764
Sage Weilf24e9982009-10-06 11:31:10 -07001765 dout("taking full map %u len %d\n", epoch, maplen);
1766 newmap = osdmap_decode(&p, p+maplen);
1767 if (IS_ERR(newmap)) {
1768 err = PTR_ERR(newmap);
1769 goto bad;
1770 }
Sage Weil30dc6382009-12-21 14:49:37 -08001771 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001772 oldmap = osdc->osdmap;
1773 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07001774 if (oldmap) {
1775 if (oldmap->epoch + 1 < newmap->epoch)
1776 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07001777 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07001778 }
1779 kick_requests(osdc, skipped_map);
Sage Weilf24e9982009-10-06 11:31:10 -07001780 }
1781 p += maplen;
1782 nr_maps--;
1783 }
1784
1785done:
1786 downgrade_write(&osdc->map_sem);
1787 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07001788
1789 /*
1790 * subscribe to subsequent osdmap updates if full to ensure
1791 * we find out when we are no longer full and stop returning
1792 * ENOSPC.
1793 */
1794 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1795 ceph_monc_request_next_osdmap(&osdc->client->monc);
1796
Alex Elderf9d25192013-02-15 11:42:29 -06001797 mutex_lock(&osdc->request_mutex);
1798 __send_queued(osdc);
1799 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001800 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001801 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001802 return;
1803
1804bad:
1805 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001806 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001807 up_write(&osdc->map_sem);
1808 return;
1809}
1810
Sage Weilf24e9982009-10-06 11:31:10 -07001811/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001812 * watch/notify callback event infrastructure
1813 *
1814 * These callbacks are used both for watch and notify operations.
1815 */
1816static void __release_event(struct kref *kref)
1817{
1818 struct ceph_osd_event *event =
1819 container_of(kref, struct ceph_osd_event, kref);
1820
1821 dout("__release_event %p\n", event);
1822 kfree(event);
1823}
1824
1825static void get_event(struct ceph_osd_event *event)
1826{
1827 kref_get(&event->kref);
1828}
1829
1830void ceph_osdc_put_event(struct ceph_osd_event *event)
1831{
1832 kref_put(&event->kref, __release_event);
1833}
1834EXPORT_SYMBOL(ceph_osdc_put_event);
1835
1836static void __insert_event(struct ceph_osd_client *osdc,
1837 struct ceph_osd_event *new)
1838{
1839 struct rb_node **p = &osdc->event_tree.rb_node;
1840 struct rb_node *parent = NULL;
1841 struct ceph_osd_event *event = NULL;
1842
1843 while (*p) {
1844 parent = *p;
1845 event = rb_entry(parent, struct ceph_osd_event, node);
1846 if (new->cookie < event->cookie)
1847 p = &(*p)->rb_left;
1848 else if (new->cookie > event->cookie)
1849 p = &(*p)->rb_right;
1850 else
1851 BUG();
1852 }
1853
1854 rb_link_node(&new->node, parent, p);
1855 rb_insert_color(&new->node, &osdc->event_tree);
1856}
1857
1858static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1859 u64 cookie)
1860{
1861 struct rb_node **p = &osdc->event_tree.rb_node;
1862 struct rb_node *parent = NULL;
1863 struct ceph_osd_event *event = NULL;
1864
1865 while (*p) {
1866 parent = *p;
1867 event = rb_entry(parent, struct ceph_osd_event, node);
1868 if (cookie < event->cookie)
1869 p = &(*p)->rb_left;
1870 else if (cookie > event->cookie)
1871 p = &(*p)->rb_right;
1872 else
1873 return event;
1874 }
1875 return NULL;
1876}
1877
1878static void __remove_event(struct ceph_osd_event *event)
1879{
1880 struct ceph_osd_client *osdc = event->osdc;
1881
1882 if (!RB_EMPTY_NODE(&event->node)) {
1883 dout("__remove_event removed %p\n", event);
1884 rb_erase(&event->node, &osdc->event_tree);
1885 ceph_osdc_put_event(event);
1886 } else {
1887 dout("__remove_event didn't remove %p\n", event);
1888 }
1889}
1890
1891int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1892 void (*event_cb)(u64, u64, u8, void *),
Alex Elder3c663bb2013-02-15 11:42:30 -06001893 void *data, struct ceph_osd_event **pevent)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001894{
1895 struct ceph_osd_event *event;
1896
1897 event = kmalloc(sizeof(*event), GFP_NOIO);
1898 if (!event)
1899 return -ENOMEM;
1900
1901 dout("create_event %p\n", event);
1902 event->cb = event_cb;
Alex Elder3c663bb2013-02-15 11:42:30 -06001903 event->one_shot = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001904 event->data = data;
1905 event->osdc = osdc;
1906 INIT_LIST_HEAD(&event->osd_node);
Alex Elder3ee52342012-12-17 12:23:48 -06001907 RB_CLEAR_NODE(&event->node);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001908 kref_init(&event->kref); /* one ref for us */
1909 kref_get(&event->kref); /* one ref for the caller */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001910
1911 spin_lock(&osdc->event_lock);
1912 event->cookie = ++osdc->event_count;
1913 __insert_event(osdc, event);
1914 spin_unlock(&osdc->event_lock);
1915
1916 *pevent = event;
1917 return 0;
1918}
1919EXPORT_SYMBOL(ceph_osdc_create_event);
1920
1921void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1922{
1923 struct ceph_osd_client *osdc = event->osdc;
1924
1925 dout("cancel_event %p\n", event);
1926 spin_lock(&osdc->event_lock);
1927 __remove_event(event);
1928 spin_unlock(&osdc->event_lock);
1929 ceph_osdc_put_event(event); /* caller's */
1930}
1931EXPORT_SYMBOL(ceph_osdc_cancel_event);
1932
1933
1934static void do_event_work(struct work_struct *work)
1935{
1936 struct ceph_osd_event_work *event_work =
1937 container_of(work, struct ceph_osd_event_work, work);
1938 struct ceph_osd_event *event = event_work->event;
1939 u64 ver = event_work->ver;
1940 u64 notify_id = event_work->notify_id;
1941 u8 opcode = event_work->opcode;
1942
1943 dout("do_event_work completing %p\n", event);
1944 event->cb(ver, notify_id, opcode, event->data);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001945 dout("do_event_work completed %p\n", event);
1946 ceph_osdc_put_event(event);
1947 kfree(event_work);
1948}
1949
1950
1951/*
1952 * Process osd watch notifications
1953 */
Alex Elder3c663bb2013-02-15 11:42:30 -06001954static void handle_watch_notify(struct ceph_osd_client *osdc,
1955 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001956{
1957 void *p, *end;
1958 u8 proto_ver;
1959 u64 cookie, ver, notify_id;
1960 u8 opcode;
1961 struct ceph_osd_event *event;
1962 struct ceph_osd_event_work *event_work;
1963
1964 p = msg->front.iov_base;
1965 end = p + msg->front.iov_len;
1966
1967 ceph_decode_8_safe(&p, end, proto_ver, bad);
1968 ceph_decode_8_safe(&p, end, opcode, bad);
1969 ceph_decode_64_safe(&p, end, cookie, bad);
1970 ceph_decode_64_safe(&p, end, ver, bad);
1971 ceph_decode_64_safe(&p, end, notify_id, bad);
1972
1973 spin_lock(&osdc->event_lock);
1974 event = __find_event(osdc, cookie);
1975 if (event) {
Alex Elder3c663bb2013-02-15 11:42:30 -06001976 BUG_ON(event->one_shot);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001977 get_event(event);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001978 }
1979 spin_unlock(&osdc->event_lock);
1980 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1981 cookie, ver, event);
1982 if (event) {
1983 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001984 if (!event_work) {
1985 dout("ERROR: could not allocate event_work\n");
1986 goto done_err;
1987 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01001988 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001989 event_work->event = event;
1990 event_work->ver = ver;
1991 event_work->notify_id = notify_id;
1992 event_work->opcode = opcode;
1993 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1994 dout("WARNING: failed to queue notify event work\n");
1995 goto done_err;
1996 }
1997 }
1998
1999 return;
2000
2001done_err:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002002 ceph_osdc_put_event(event);
2003 return;
2004
2005bad:
2006 pr_err("osdc handle_watch_notify corrupt msg\n");
2007 return;
2008}
2009
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002010/*
Alex Eldere65550f2013-04-05 01:27:12 -05002011 * build new request AND message
2012 *
2013 */
2014void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
2015 struct ceph_snap_context *snapc, u64 snap_id,
2016 struct timespec *mtime)
2017{
2018 struct ceph_msg *msg = req->r_request;
2019 void *p;
2020 size_t msg_size;
2021 int flags = req->r_flags;
2022 u64 data_len;
2023 unsigned int i;
2024
2025 req->r_snapid = snap_id;
2026 req->r_snapc = ceph_get_snap_context(snapc);
2027
2028 /* encode request */
2029 msg->hdr.version = cpu_to_le16(4);
2030
2031 p = msg->front.iov_base;
2032 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2033 req->r_request_osdmap_epoch = p;
2034 p += 4;
2035 req->r_request_flags = p;
2036 p += 4;
2037 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2038 ceph_encode_timespec(p, mtime);
2039 p += sizeof(struct ceph_timespec);
2040 req->r_request_reassert_version = p;
2041 p += sizeof(struct ceph_eversion); /* will get filled in */
2042
2043 /* oloc */
2044 ceph_encode_8(&p, 4);
2045 ceph_encode_8(&p, 4);
2046 ceph_encode_32(&p, 8 + 4 + 4);
2047 req->r_request_pool = p;
2048 p += 8;
2049 ceph_encode_32(&p, -1); /* preferred */
2050 ceph_encode_32(&p, 0); /* key len */
2051
2052 ceph_encode_8(&p, 1);
2053 req->r_request_pgid = p;
2054 p += 8 + 4;
2055 ceph_encode_32(&p, -1); /* preferred */
2056
2057 /* oid */
2058 ceph_encode_32(&p, req->r_oid_len);
2059 memcpy(p, req->r_oid, req->r_oid_len);
2060 dout("oid '%.*s' len %d\n", req->r_oid_len, req->r_oid, req->r_oid_len);
2061 p += req->r_oid_len;
2062
2063 /* ops--can imply data */
2064 ceph_encode_16(&p, (u16)req->r_num_ops);
2065 data_len = 0;
2066 for (i = 0; i < req->r_num_ops; i++) {
2067 data_len += osd_req_encode_op(req, p, i);
2068 p += sizeof(struct ceph_osd_op);
2069 }
2070
2071 /* snaps */
2072 ceph_encode_64(&p, req->r_snapid);
2073 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2074 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2075 if (req->r_snapc) {
2076 for (i = 0; i < snapc->num_snaps; i++) {
2077 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2078 }
2079 }
2080
2081 req->r_request_attempts = p;
2082 p += 4;
2083
2084 /* data */
2085 if (flags & CEPH_OSD_FLAG_WRITE) {
2086 u16 data_off;
2087
2088 /*
2089 * The header "data_off" is a hint to the receiver
2090 * allowing it to align received data into its
2091 * buffers such that there's no need to re-copy
2092 * it before writing it to disk (direct I/O).
2093 */
2094 data_off = (u16) (off & 0xffff);
2095 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2096 }
2097 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2098
2099 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2100 msg_size = p - msg->front.iov_base;
2101 msg->front.iov_len = msg_size;
2102 msg->hdr.front_len = cpu_to_le32(msg_size);
2103
2104 dout("build_request msg_size was %d\n", (int)msg_size);
2105}
2106EXPORT_SYMBOL(ceph_osdc_build_request);
2107
2108/*
Sage Weilf24e9982009-10-06 11:31:10 -07002109 * Register request, send initial attempt.
2110 */
2111int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2112 struct ceph_osd_request *req,
2113 bool nofail)
2114{
Sage Weilc1ea8822009-10-08 16:55:47 -07002115 int rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07002116
Sage Weilf24e9982009-10-06 11:31:10 -07002117 down_read(&osdc->map_sem);
2118 mutex_lock(&osdc->request_mutex);
Alex Elderdc4b8702013-03-25 18:16:11 -05002119 __register_request(osdc, req);
Alex Elder92451b42013-03-25 18:16:11 -05002120 WARN_ON(req->r_sent);
2121 rc = __map_request(osdc, req, 0);
2122 if (rc < 0) {
2123 if (nofail) {
2124 dout("osdc_start_request failed map, "
2125 " will retry %lld\n", req->r_tid);
2126 rc = 0;
Sage Weil9d6fcb02011-05-12 15:48:16 -07002127 }
Alex Elder92451b42013-03-25 18:16:11 -05002128 goto out_unlock;
Sage Weilf24e9982009-10-06 11:31:10 -07002129 }
Alex Elder92451b42013-03-25 18:16:11 -05002130 if (req->r_osd == NULL) {
2131 dout("send_request %p no up osds in pg\n", req);
2132 ceph_monc_request_next_osdmap(&osdc->client->monc);
2133 } else {
Alex Elder7e2766a2013-03-25 18:16:11 -05002134 __send_queued(osdc);
Alex Elder92451b42013-03-25 18:16:11 -05002135 }
2136 rc = 0;
Dan Carpenter234af26f2011-03-29 06:25:59 +03002137out_unlock:
Sage Weilf24e9982009-10-06 11:31:10 -07002138 mutex_unlock(&osdc->request_mutex);
2139 up_read(&osdc->map_sem);
2140 return rc;
2141}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002142EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002143
2144/*
2145 * wait for a request to complete
2146 */
2147int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2148 struct ceph_osd_request *req)
2149{
2150 int rc;
2151
2152 rc = wait_for_completion_interruptible(&req->r_completion);
2153 if (rc < 0) {
2154 mutex_lock(&osdc->request_mutex);
2155 __cancel_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08002156 __unregister_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07002157 mutex_unlock(&osdc->request_mutex);
Sage Weil25845472011-06-03 09:37:09 -07002158 complete_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08002159 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07002160 return rc;
2161 }
2162
2163 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
2164 return req->r_result;
2165}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002166EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002167
2168/*
2169 * sync - wait for all in-flight requests to flush. avoid starvation.
2170 */
2171void ceph_osdc_sync(struct ceph_osd_client *osdc)
2172{
2173 struct ceph_osd_request *req;
2174 u64 last_tid, next_tid = 0;
2175
2176 mutex_lock(&osdc->request_mutex);
2177 last_tid = osdc->last_tid;
2178 while (1) {
2179 req = __lookup_request_ge(osdc, next_tid);
2180 if (!req)
2181 break;
2182 if (req->r_tid > last_tid)
2183 break;
2184
2185 next_tid = req->r_tid + 1;
2186 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2187 continue;
2188
2189 ceph_osdc_get_request(req);
2190 mutex_unlock(&osdc->request_mutex);
2191 dout("sync waiting on tid %llu (last is %llu)\n",
2192 req->r_tid, last_tid);
2193 wait_for_completion(&req->r_safe_completion);
2194 mutex_lock(&osdc->request_mutex);
2195 ceph_osdc_put_request(req);
2196 }
2197 mutex_unlock(&osdc->request_mutex);
2198 dout("sync done (thru tid %llu)\n", last_tid);
2199}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002200EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07002201
2202/*
2203 * init, shutdown
2204 */
2205int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2206{
2207 int err;
2208
2209 dout("init\n");
2210 osdc->client = client;
2211 osdc->osdmap = NULL;
2212 init_rwsem(&osdc->map_sem);
2213 init_completion(&osdc->map_waiters);
2214 osdc->last_requested_map = 0;
2215 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002216 osdc->last_tid = 0;
2217 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002218 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07002219 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002220 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08002221 INIT_LIST_HEAD(&osdc->req_unsent);
2222 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002223 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07002224 osdc->num_requests = 0;
2225 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002226 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002227 spin_lock_init(&osdc->event_lock);
2228 osdc->event_tree = RB_ROOT;
2229 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002230
2231 schedule_delayed_work(&osdc->osds_timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002232 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07002233
Sage Weil5f44f142009-11-18 14:52:18 -08002234 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07002235 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2236 sizeof(struct ceph_osd_request));
2237 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08002238 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07002239
Sage Weild50b4092012-07-09 14:22:34 -07002240 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2241 OSD_OP_FRONT_LEN, 10, true,
Sage Weil4f482802010-04-24 09:56:35 -07002242 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07002243 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08002244 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07002245 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Sage Weil4f482802010-04-24 09:56:35 -07002246 OSD_OPREPLY_FRONT_LEN, 10, true,
2247 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08002248 if (err < 0)
2249 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002250
2251 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
2252 if (IS_ERR(osdc->notify_wq)) {
2253 err = PTR_ERR(osdc->notify_wq);
2254 osdc->notify_wq = NULL;
2255 goto out_msgpool;
2256 }
Sage Weilf24e9982009-10-06 11:31:10 -07002257 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08002258
Sage Weilc16e7862010-03-01 13:02:00 -08002259out_msgpool:
2260 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08002261out_mempool:
2262 mempool_destroy(osdc->req_mempool);
2263out:
2264 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07002265}
2266
2267void ceph_osdc_stop(struct ceph_osd_client *osdc)
2268{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002269 flush_workqueue(osdc->notify_wq);
2270 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002271 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002272 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07002273 if (osdc->osdmap) {
2274 ceph_osdmap_destroy(osdc->osdmap);
2275 osdc->osdmap = NULL;
2276 }
Sage Weilaca420b2011-08-31 14:45:53 -07002277 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002278 mempool_destroy(osdc->req_mempool);
2279 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08002280 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07002281}
2282
2283/*
2284 * Read some contiguous pages. If we cross a stripe boundary, shorten
2285 * *plen. Return number of bytes read, or error.
2286 */
2287int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2288 struct ceph_vino vino, struct ceph_file_layout *layout,
2289 u64 off, u64 *plen,
2290 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08002291 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07002292{
2293 struct ceph_osd_request *req;
2294 int rc = 0;
2295
2296 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2297 vino.snap, off, *plen);
Alex Elder79528732013-04-03 21:32:51 -05002298 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002299 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
Alex Elderacead002013-03-14 14:09:05 -05002300 NULL, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002301 false);
Sage Weil68162822012-09-24 21:01:02 -07002302 if (IS_ERR(req))
2303 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002304
2305 /* it may be a short read due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06002306
Alex Elder406e2c92013-04-15 14:50:36 -05002307 osd_req_op_extent_osd_data_pages(req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05002308 pages, *plen, page_align, false, false);
Sage Weilf24e9982009-10-06 11:31:10 -07002309
Alex Eldere0c59482013-03-07 15:38:25 -06002310 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
Alex Elder43bfe5d2013-04-03 01:28:57 -05002311 off, *plen, *plen, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07002312
Alex Elder79528732013-04-03 21:32:51 -05002313 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
Alex Elder02ee07d2013-03-14 14:09:06 -05002314
Sage Weilf24e9982009-10-06 11:31:10 -07002315 rc = ceph_osdc_start_request(osdc, req, false);
2316 if (!rc)
2317 rc = ceph_osdc_wait_request(osdc, req);
2318
2319 ceph_osdc_put_request(req);
2320 dout("readpages result %d\n", rc);
2321 return rc;
2322}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002323EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07002324
2325/*
2326 * do a synchronous write on N pages
2327 */
2328int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2329 struct ceph_file_layout *layout,
2330 struct ceph_snap_context *snapc,
2331 u64 off, u64 len,
2332 u32 truncate_seq, u64 truncate_size,
2333 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06002334 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07002335{
2336 struct ceph_osd_request *req;
2337 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08002338 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07002339
Alex Elderacead002013-03-14 14:09:05 -05002340 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
Alex Elder79528732013-04-03 21:32:51 -05002341 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002342 CEPH_OSD_OP_WRITE,
Alex Elder24808822013-02-15 11:42:29 -06002343 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Alex Elderacead002013-03-14 14:09:05 -05002344 snapc, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002345 true);
Sage Weil68162822012-09-24 21:01:02 -07002346 if (IS_ERR(req))
2347 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002348
2349 /* it may be a short write due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05002350 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
Alex Elder43bfe5d2013-04-03 01:28:57 -05002351 false, false);
2352 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
Sage Weilf24e9982009-10-06 11:31:10 -07002353
Alex Elder79528732013-04-03 21:32:51 -05002354 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
Alex Elder02ee07d2013-03-14 14:09:06 -05002355
Alex Elder87f979d2013-02-15 11:42:29 -06002356 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07002357 if (!rc)
2358 rc = ceph_osdc_wait_request(osdc, req);
2359
2360 ceph_osdc_put_request(req);
2361 if (rc == 0)
2362 rc = len;
2363 dout("writepages result %d\n", rc);
2364 return rc;
2365}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002366EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07002367
2368/*
2369 * handle incoming message
2370 */
2371static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2372{
2373 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01002374 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002375 int type = le16_to_cpu(msg->hdr.type);
2376
2377 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07002378 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01002379 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002380
2381 switch (type) {
2382 case CEPH_MSG_OSD_MAP:
2383 ceph_osdc_handle_map(osdc, msg);
2384 break;
2385 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08002386 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07002387 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002388 case CEPH_MSG_WATCH_NOTIFY:
2389 handle_watch_notify(osdc, msg);
2390 break;
Sage Weilf24e9982009-10-06 11:31:10 -07002391
2392 default:
2393 pr_err("received unknown message type %d %s\n", type,
2394 ceph_msg_type_name(type));
2395 }
Sage Weil4a32f932010-06-13 10:27:53 -07002396out:
Sage Weilf24e9982009-10-06 11:31:10 -07002397 ceph_msg_put(msg);
2398}
2399
Sage Weil5b3a4db2010-02-19 21:43:23 -08002400/*
Sage Weil21b667f2010-03-04 10:22:59 -08002401 * lookup and return message for incoming reply. set up reply message
2402 * pages.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002403 */
2404static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002405 struct ceph_msg_header *hdr,
2406 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002407{
2408 struct ceph_osd *osd = con->private;
2409 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002410 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002411 struct ceph_osd_request *req;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002412 int front = le32_to_cpu(hdr->front_len);
2413 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002414 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002415
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002416 tid = le64_to_cpu(hdr->tid);
2417 mutex_lock(&osdc->request_mutex);
2418 req = __lookup_request(osdc, tid);
2419 if (!req) {
2420 *skip = 1;
2421 m = NULL;
Sage Weil756a16a2012-07-30 16:26:13 -07002422 dout("get_reply unknown tid %llu from osd%d\n", tid,
2423 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002424 goto out;
2425 }
Sage Weilc16e7862010-03-01 13:02:00 -08002426
Alex Elderace6d3a2013-04-01 16:12:14 -05002427 if (req->r_reply->con)
Alex Elder8921d112012-06-01 14:56:43 -05002428 dout("%s revoking msg %p from old con %p\n", __func__,
Alex Elderace6d3a2013-04-01 16:12:14 -05002429 req->r_reply, req->r_reply->con);
2430 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002431
Sage Weilc16e7862010-03-01 13:02:00 -08002432 if (front > req->r_reply->front.iov_len) {
2433 pr_warning("get_reply front %d > preallocated %d\n",
2434 front, (int)req->r_reply->front.iov_len);
Sage Weilb61c2762011-08-09 15:03:46 -07002435 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002436 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002437 goto out;
2438 ceph_msg_put(req->r_reply);
2439 req->r_reply = m;
2440 }
2441 m = ceph_msg_get(req->r_reply);
2442
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002443 if (data_len > 0) {
Alex Eldera4ce40a2013-04-05 01:27:12 -05002444 struct ceph_osd_data *osd_data;
Alex Elder0fff87e2013-02-14 12:16:43 -06002445
Alex Eldera4ce40a2013-04-05 01:27:12 -05002446 /*
2447 * XXX This is assuming there is only one op containing
2448 * XXX page data. Probably OK for reads, but this
2449 * XXX ought to be done more generally.
2450 */
Alex Elder406e2c92013-04-15 14:50:36 -05002451 osd_data = osd_req_op_extent_osd_data(req, 0);
Alex Elder0fff87e2013-02-14 12:16:43 -06002452 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
Alex Elder0fff87e2013-02-14 12:16:43 -06002453 if (osd_data->pages &&
Alex Eldere0c59482013-03-07 15:38:25 -06002454 unlikely(osd_data->length < data_len)) {
Alex Elder2ac2b7a62013-02-14 12:16:43 -06002455
Alex Eldere0c59482013-03-07 15:38:25 -06002456 pr_warning("tid %lld reply has %d bytes "
2457 "we had only %llu bytes ready\n",
2458 tid, data_len, osd_data->length);
Alex Elder2ac2b7a62013-02-14 12:16:43 -06002459 *skip = 1;
2460 ceph_msg_put(m);
2461 m = NULL;
2462 goto out;
2463 }
Alex Elder2ac2b7a62013-02-14 12:16:43 -06002464 }
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002465 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08002466 *skip = 0;
Sage Weilc16e7862010-03-01 13:02:00 -08002467 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002468
2469out:
2470 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002471 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002472
2473}
2474
2475static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2476 struct ceph_msg_header *hdr,
2477 int *skip)
2478{
2479 struct ceph_osd *osd = con->private;
2480 int type = le16_to_cpu(hdr->type);
2481 int front = le32_to_cpu(hdr->front_len);
2482
Alex Elder1c20f2d2012-06-04 14:43:32 -05002483 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002484 switch (type) {
2485 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002486 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002487 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002488 case CEPH_MSG_OSD_OPREPLY:
2489 return get_reply(con, hdr, skip);
2490 default:
2491 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2492 osd->o_osd);
2493 *skip = 1;
2494 return NULL;
2495 }
Sage Weilf24e9982009-10-06 11:31:10 -07002496}
2497
2498/*
2499 * Wrappers to refcount containing ceph_osd struct
2500 */
2501static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2502{
2503 struct ceph_osd *osd = con->private;
2504 if (get_osd(osd))
2505 return con;
2506 return NULL;
2507}
2508
2509static void put_osd_con(struct ceph_connection *con)
2510{
2511 struct ceph_osd *osd = con->private;
2512 put_osd(osd);
2513}
2514
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002515/*
2516 * authentication
2517 */
Alex Eldera3530df2012-05-16 15:16:39 -05002518/*
2519 * Note: returned pointer is the address of a structure that's
2520 * managed separately. Caller must *not* attempt to free it.
2521 */
2522static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002523 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002524{
2525 struct ceph_osd *o = con->private;
2526 struct ceph_osd_client *osdc = o->o_osdc;
2527 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002528 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002529
Alex Elder74f18692012-05-16 15:16:39 -05002530 if (force_new && auth->authorizer) {
Sage Weil27859f92013-03-25 10:26:14 -07002531 ceph_auth_destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002532 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002533 }
Sage Weil27859f92013-03-25 10:26:14 -07002534 if (!auth->authorizer) {
2535 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2536 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002537 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002538 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07002539 } else {
2540 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07002541 auth);
2542 if (ret)
2543 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002544 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002545 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05002546
Alex Eldera3530df2012-05-16 15:16:39 -05002547 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002548}
2549
2550
2551static int verify_authorizer_reply(struct ceph_connection *con, int len)
2552{
2553 struct ceph_osd *o = con->private;
2554 struct ceph_osd_client *osdc = o->o_osdc;
2555 struct ceph_auth_client *ac = osdc->client->monc.auth;
2556
Sage Weil27859f92013-03-25 10:26:14 -07002557 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002558}
2559
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002560static int invalidate_authorizer(struct ceph_connection *con)
2561{
2562 struct ceph_osd *o = con->private;
2563 struct ceph_osd_client *osdc = o->o_osdc;
2564 struct ceph_auth_client *ac = osdc->client->monc.auth;
2565
Sage Weil27859f92013-03-25 10:26:14 -07002566 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002567 return ceph_monc_validate_auth(&osdc->client->monc);
2568}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002569
Tobias Klauser9e327892010-05-20 10:40:19 +02002570static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07002571 .get = get_osd_con,
2572 .put = put_osd_con,
2573 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002574 .get_authorizer = get_authorizer,
2575 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002576 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07002577 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07002578 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07002579};