blob: 2562e4e52245771e0f455ea72abb0d3a74b470dc [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 Eldera4ce40a2013-04-05 01:27:12 -0500118struct ceph_osd_data *
119osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
120 unsigned int which, bool write_request)
121{
122 BUG_ON(which >= osd_req->r_num_ops);
123
Alex Elder54764922013-04-05 01:27:12 -0500124 return &osd_req->r_ops[which].extent.osd_data;
Alex Eldera4ce40a2013-04-05 01:27:12 -0500125}
126EXPORT_SYMBOL(osd_req_op_extent_osd_data);
127
128struct ceph_osd_data *
129osd_req_op_cls_request_info(struct ceph_osd_request *osd_req,
130 unsigned int which)
131{
132 BUG_ON(which >= osd_req->r_num_ops);
133
Alex Elder54764922013-04-05 01:27:12 -0500134 return &osd_req->r_ops[which].cls.request_info;
Alex Eldera4ce40a2013-04-05 01:27:12 -0500135}
136EXPORT_SYMBOL(osd_req_op_cls_request_info); /* ??? */
137
138struct ceph_osd_data *
139osd_req_op_cls_response_data(struct ceph_osd_request *osd_req,
140 unsigned int which)
141{
142 BUG_ON(which >= osd_req->r_num_ops);
143
Alex Elder54764922013-04-05 01:27:12 -0500144 return &osd_req->r_ops[which].cls.response_data;
Alex Eldera4ce40a2013-04-05 01:27:12 -0500145}
146EXPORT_SYMBOL(osd_req_op_cls_response_data); /* ??? */
147
148void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
149 unsigned int which, bool write_request,
150 struct page **pages, 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_extent_osd_data(osd_req, which, write_request);
156 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
157 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500158}
159EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
160
161void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
162 unsigned int which, bool write_request,
163 struct ceph_pagelist *pagelist)
164{
165 struct ceph_osd_data *osd_data;
166
167 osd_data = osd_req_op_extent_osd_data(osd_req, which, write_request);
168 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500169}
170EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
171
172#ifdef CONFIG_BLOCK
173void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
174 unsigned int which, bool write_request,
175 struct bio *bio, size_t bio_length)
176{
177 struct ceph_osd_data *osd_data;
178
179 osd_data = osd_req_op_extent_osd_data(osd_req, which, write_request);
180 ceph_osd_data_bio_init(osd_data, bio, bio_length);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500181}
182EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
183#endif /* CONFIG_BLOCK */
184
185static void osd_req_op_cls_request_info_pagelist(
186 struct ceph_osd_request *osd_req,
187 unsigned int which, struct ceph_pagelist *pagelist)
188{
189 struct ceph_osd_data *osd_data;
190
191 osd_data = osd_req_op_cls_request_info(osd_req, which);
192 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500193}
194
195void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
196 unsigned int which, struct page **pages, u64 length,
197 u32 alignment, bool pages_from_pool, bool own_pages)
198{
199 struct ceph_osd_data *osd_data;
200
201 osd_data = osd_req_op_cls_response_data(osd_req, which);
202 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
203 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500204}
205EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
206
Alex Elder23c08a9cb2013-04-03 01:28:58 -0500207static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
208{
209 switch (osd_data->type) {
210 case CEPH_OSD_DATA_TYPE_NONE:
211 return 0;
212 case CEPH_OSD_DATA_TYPE_PAGES:
213 return osd_data->length;
214 case CEPH_OSD_DATA_TYPE_PAGELIST:
215 return (u64)osd_data->pagelist->length;
216#ifdef CONFIG_BLOCK
217 case CEPH_OSD_DATA_TYPE_BIO:
218 return (u64)osd_data->bio_length;
219#endif /* CONFIG_BLOCK */
220 default:
221 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
222 return 0;
223 }
224}
225
Alex Elder54764922013-04-05 01:27:12 -0500226
Alex Elderc54d47b2013-04-03 01:28:57 -0500227static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
228{
Alex Elder54764922013-04-05 01:27:12 -0500229 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
Alex Elderc54d47b2013-04-03 01:28:57 -0500230 int num_pages;
231
232 num_pages = calc_pages_for((u64)osd_data->alignment,
233 (u64)osd_data->length);
234 ceph_release_page_vector(osd_data->pages, num_pages);
235 }
Alex Elder54764922013-04-05 01:27:12 -0500236 ceph_osd_data_init(osd_data);
237}
238
239static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
240 unsigned int which)
241{
242 struct ceph_osd_req_op *op;
243
244 BUG_ON(which >= osd_req->r_num_ops);
245 op = &osd_req->r_ops[which];
246
247 switch (op->op) {
248 case CEPH_OSD_OP_READ:
249 case CEPH_OSD_OP_WRITE:
250 ceph_osd_data_release(&op->extent.osd_data);
251 break;
252 case CEPH_OSD_OP_CALL:
253 ceph_osd_data_release(&op->cls.request_info);
254 ceph_osd_data_release(&op->cls.response_data);
255 break;
256 default:
257 break;
258 }
Alex Elderc54d47b2013-04-03 01:28:57 -0500259}
260
Sage Weilf24e9982009-10-06 11:31:10 -0700261/*
262 * requests
263 */
Sage Weil415e49a2009-12-07 13:37:03 -0800264void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700265{
Alex Elderc54d47b2013-04-03 01:28:57 -0500266 struct ceph_osd_request *req;
Alex Elder54764922013-04-05 01:27:12 -0500267 unsigned int which;
Sage Weil415e49a2009-12-07 13:37:03 -0800268
Alex Elderc54d47b2013-04-03 01:28:57 -0500269 req = container_of(kref, struct ceph_osd_request, r_kref);
Sage Weil415e49a2009-12-07 13:37:03 -0800270 if (req->r_request)
271 ceph_msg_put(req->r_request);
Alex Elderace6d3a2013-04-01 16:12:14 -0500272 if (req->r_reply) {
Alex Elder8921d112012-06-01 14:56:43 -0500273 ceph_msg_revoke_incoming(req->r_reply);
Alex Elderab8cb342012-06-04 14:43:32 -0500274 ceph_msg_put(req->r_reply);
Alex Elderace6d3a2013-04-01 16:12:14 -0500275 }
Alex Elder0fff87e2013-02-14 12:16:43 -0600276
Alex Elder54764922013-04-05 01:27:12 -0500277 for (which = 0; which < req->r_num_ops; which++)
278 osd_req_op_data_release(req, which);
Alex Elder0fff87e2013-02-14 12:16:43 -0600279
Sage Weil415e49a2009-12-07 13:37:03 -0800280 ceph_put_snap_context(req->r_snapc);
281 if (req->r_mempool)
282 mempool_free(req, req->r_osdc->req_mempool);
283 else
284 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700285}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700286EXPORT_SYMBOL(ceph_osdc_release_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700287
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700288struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700289 struct ceph_snap_context *snapc,
Sage Weil1b83bef2013-02-25 16:11:12 -0800290 unsigned int num_ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700291 bool use_mempool,
Alex Elder54a54002012-11-13 21:11:15 -0600292 gfp_t gfp_flags)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700293{
294 struct ceph_osd_request *req;
295 struct ceph_msg *msg;
Sage Weil1b83bef2013-02-25 16:11:12 -0800296 size_t msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700297
Alex Elder79528732013-04-03 21:32:51 -0500298 BUILD_BUG_ON(CEPH_OSD_MAX_OP > U16_MAX);
299 BUG_ON(num_ops > CEPH_OSD_MAX_OP);
300
Sage Weil1b83bef2013-02-25 16:11:12 -0800301 msg_size = 4 + 4 + 8 + 8 + 4+8;
302 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
303 msg_size += 1 + 8 + 4 + 4; /* pg_t */
304 msg_size += 4 + MAX_OBJ_NAME_SIZE;
305 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
306 msg_size += 8; /* snapid */
307 msg_size += 8; /* snap_seq */
308 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
309 msg_size += 4;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700310
311 if (use_mempool) {
312 req = mempool_alloc(osdc->req_mempool, gfp_flags);
313 memset(req, 0, sizeof(*req));
314 } else {
315 req = kzalloc(sizeof(*req), gfp_flags);
316 }
317 if (req == NULL)
318 return NULL;
319
320 req->r_osdc = osdc;
321 req->r_mempool = use_mempool;
Alex Elder79528732013-04-03 21:32:51 -0500322 req->r_num_ops = num_ops;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700323
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700324 kref_init(&req->r_kref);
325 init_completion(&req->r_completion);
326 init_completion(&req->r_safe_completion);
Alex Eldera978fa22012-12-17 12:23:48 -0600327 RB_CLEAR_NODE(&req->r_node);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700328 INIT_LIST_HEAD(&req->r_unsafe_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700329 INIT_LIST_HEAD(&req->r_linger_item);
330 INIT_LIST_HEAD(&req->r_linger_osd);
Sage Weil935b6392011-09-16 11:13:17 -0700331 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilcd430452012-07-09 14:31:41 -0700332 INIT_LIST_HEAD(&req->r_osd_item);
333
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700334 /* create reply message */
335 if (use_mempool)
336 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
337 else
338 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
Sage Weilb61c2762011-08-09 15:03:46 -0700339 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700340 if (!msg) {
341 ceph_osdc_put_request(req);
342 return NULL;
343 }
344 req->r_reply = msg;
345
346 /* create request message; allow space for oid */
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700347 if (use_mempool)
348 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
349 else
Sage Weilb61c2762011-08-09 15:03:46 -0700350 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700351 if (!msg) {
352 ceph_osdc_put_request(req);
353 return NULL;
354 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700355
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700356 memset(msg->front.iov_base, 0, msg->front.iov_len);
357
358 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700359
360 return req;
361}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700362EXPORT_SYMBOL(ceph_osdc_alloc_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700363
Alex Eldera8dd0a32013-03-13 20:50:00 -0500364static bool osd_req_opcode_valid(u16 opcode)
365{
366 switch (opcode) {
367 case CEPH_OSD_OP_READ:
368 case CEPH_OSD_OP_STAT:
369 case CEPH_OSD_OP_MAPEXT:
370 case CEPH_OSD_OP_MASKTRUNC:
371 case CEPH_OSD_OP_SPARSE_READ:
372 case CEPH_OSD_OP_NOTIFY:
373 case CEPH_OSD_OP_NOTIFY_ACK:
374 case CEPH_OSD_OP_ASSERT_VER:
375 case CEPH_OSD_OP_WRITE:
376 case CEPH_OSD_OP_WRITEFULL:
377 case CEPH_OSD_OP_TRUNCATE:
378 case CEPH_OSD_OP_ZERO:
379 case CEPH_OSD_OP_DELETE:
380 case CEPH_OSD_OP_APPEND:
381 case CEPH_OSD_OP_STARTSYNC:
382 case CEPH_OSD_OP_SETTRUNC:
383 case CEPH_OSD_OP_TRIMTRUNC:
384 case CEPH_OSD_OP_TMAPUP:
385 case CEPH_OSD_OP_TMAPPUT:
386 case CEPH_OSD_OP_TMAPGET:
387 case CEPH_OSD_OP_CREATE:
388 case CEPH_OSD_OP_ROLLBACK:
389 case CEPH_OSD_OP_WATCH:
390 case CEPH_OSD_OP_OMAPGETKEYS:
391 case CEPH_OSD_OP_OMAPGETVALS:
392 case CEPH_OSD_OP_OMAPGETHEADER:
393 case CEPH_OSD_OP_OMAPGETVALSBYKEYS:
394 case CEPH_OSD_OP_OMAPSETVALS:
395 case CEPH_OSD_OP_OMAPSETHEADER:
396 case CEPH_OSD_OP_OMAPCLEAR:
397 case CEPH_OSD_OP_OMAPRMKEYS:
398 case CEPH_OSD_OP_OMAP_CMP:
399 case CEPH_OSD_OP_CLONERANGE:
400 case CEPH_OSD_OP_ASSERT_SRC_VERSION:
401 case CEPH_OSD_OP_SRC_CMPXATTR:
402 case CEPH_OSD_OP_GETXATTR:
403 case CEPH_OSD_OP_GETXATTRS:
404 case CEPH_OSD_OP_CMPXATTR:
405 case CEPH_OSD_OP_SETXATTR:
406 case CEPH_OSD_OP_SETXATTRS:
407 case CEPH_OSD_OP_RESETXATTRS:
408 case CEPH_OSD_OP_RMXATTR:
409 case CEPH_OSD_OP_PULL:
410 case CEPH_OSD_OP_PUSH:
411 case CEPH_OSD_OP_BALANCEREADS:
412 case CEPH_OSD_OP_UNBALANCEREADS:
413 case CEPH_OSD_OP_SCRUB:
414 case CEPH_OSD_OP_SCRUB_RESERVE:
415 case CEPH_OSD_OP_SCRUB_UNRESERVE:
416 case CEPH_OSD_OP_SCRUB_STOP:
417 case CEPH_OSD_OP_SCRUB_MAP:
418 case CEPH_OSD_OP_WRLOCK:
419 case CEPH_OSD_OP_WRUNLOCK:
420 case CEPH_OSD_OP_RDLOCK:
421 case CEPH_OSD_OP_RDUNLOCK:
422 case CEPH_OSD_OP_UPLOCK:
423 case CEPH_OSD_OP_DNLOCK:
424 case CEPH_OSD_OP_CALL:
425 case CEPH_OSD_OP_PGLS:
426 case CEPH_OSD_OP_PGLS_FILTER:
427 return true;
428 default:
429 return false;
430 }
431}
432
Alex Elder33803f32013-03-13 20:50:00 -0500433/*
434 * This is an osd op init function for opcodes that have no data or
435 * other information associated with them. It also serves as a
436 * common init routine for all the other init functions, below.
437 */
Alex Elderc99d2d42013-04-05 01:27:11 -0500438static struct ceph_osd_req_op *
439osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
440 u16 opcode)
Alex Elder33803f32013-03-13 20:50:00 -0500441{
Alex Elderc99d2d42013-04-05 01:27:11 -0500442 struct ceph_osd_req_op *op;
443
444 BUG_ON(which >= osd_req->r_num_ops);
Alex Elder33803f32013-03-13 20:50:00 -0500445 BUG_ON(!osd_req_opcode_valid(opcode));
446
Alex Elderc99d2d42013-04-05 01:27:11 -0500447 op = &osd_req->r_ops[which];
Alex Elder33803f32013-03-13 20:50:00 -0500448 memset(op, 0, sizeof (*op));
Alex Elder33803f32013-03-13 20:50:00 -0500449 op->op = opcode;
Alex Elderc99d2d42013-04-05 01:27:11 -0500450
451 return op;
Alex Elder33803f32013-03-13 20:50:00 -0500452}
453
Alex Elderc99d2d42013-04-05 01:27:11 -0500454void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
455 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500456 u64 offset, u64 length,
457 u64 truncate_size, u32 truncate_seq)
458{
Alex Elderc99d2d42013-04-05 01:27:11 -0500459 struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which, opcode);
Alex Elder33803f32013-03-13 20:50:00 -0500460 size_t payload_len = 0;
461
462 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
463
Alex Elder33803f32013-03-13 20:50:00 -0500464 op->extent.offset = offset;
465 op->extent.length = length;
466 op->extent.truncate_size = truncate_size;
467 op->extent.truncate_seq = truncate_seq;
468 if (opcode == CEPH_OSD_OP_WRITE)
469 payload_len += length;
470
471 op->payload_len = payload_len;
472}
473EXPORT_SYMBOL(osd_req_op_extent_init);
474
Alex Elderc99d2d42013-04-05 01:27:11 -0500475void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
476 unsigned int which, u64 length)
Alex Eldere5975c72013-03-14 14:09:05 -0500477{
Alex Elderc99d2d42013-04-05 01:27:11 -0500478 struct ceph_osd_req_op *op;
479 u64 previous;
480
481 BUG_ON(which >= osd_req->r_num_ops);
482 op = &osd_req->r_ops[which];
483 previous = op->extent.length;
Alex Eldere5975c72013-03-14 14:09:05 -0500484
485 if (length == previous)
486 return; /* Nothing to do */
487 BUG_ON(length > previous);
488
489 op->extent.length = length;
490 op->payload_len -= previous - length;
491}
492EXPORT_SYMBOL(osd_req_op_extent_update);
493
Alex Elderc99d2d42013-04-05 01:27:11 -0500494void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
495 u16 opcode, const char *class, const char *method,
Alex Elder33803f32013-03-13 20:50:00 -0500496 const void *request_data, size_t request_data_size)
497{
Alex Elderc99d2d42013-04-05 01:27:11 -0500498 struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which, opcode);
Alex Elder5f562df2013-04-05 01:27:12 -0500499 struct ceph_pagelist *pagelist;
Alex Elder33803f32013-03-13 20:50:00 -0500500 size_t payload_len = 0;
501 size_t size;
502
503 BUG_ON(opcode != CEPH_OSD_OP_CALL);
504
Alex Elder5f562df2013-04-05 01:27:12 -0500505 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
506 BUG_ON(!pagelist);
507 ceph_pagelist_init(pagelist);
508
Alex Elder33803f32013-03-13 20:50:00 -0500509 op->cls.class_name = class;
510 size = strlen(class);
511 BUG_ON(size > (size_t) U8_MAX);
512 op->cls.class_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500513 ceph_pagelist_append(pagelist, class, size);
Alex Elder33803f32013-03-13 20:50:00 -0500514 payload_len += size;
515
516 op->cls.method_name = method;
517 size = strlen(method);
518 BUG_ON(size > (size_t) U8_MAX);
519 op->cls.method_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500520 ceph_pagelist_append(pagelist, method, size);
Alex Elder33803f32013-03-13 20:50:00 -0500521 payload_len += size;
522
Alex Elder54d50642013-04-03 01:28:58 -0500523 op->cls.request_data = request_data;
Alex Elder33803f32013-03-13 20:50:00 -0500524 BUG_ON(request_data_size > (size_t) U32_MAX);
Alex Elder54d50642013-04-03 01:28:58 -0500525 op->cls.request_data_len = (u32) request_data_size;
Alex Elder5f562df2013-04-05 01:27:12 -0500526 ceph_pagelist_append(pagelist, request_data, request_data_size);
Alex Elder33803f32013-03-13 20:50:00 -0500527 payload_len += request_data_size;
528
Alex Eldera4ce40a2013-04-05 01:27:12 -0500529 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
Alex Elder5f562df2013-04-05 01:27:12 -0500530
Alex Elder33803f32013-03-13 20:50:00 -0500531 op->cls.argc = 0; /* currently unused */
532
533 op->payload_len = payload_len;
534}
535EXPORT_SYMBOL(osd_req_op_cls_init);
Alex Elder8c042b02013-04-03 01:28:58 -0500536
Alex Elderc99d2d42013-04-05 01:27:11 -0500537void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
538 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500539 u64 cookie, u64 version, int flag)
540{
Alex Elderc99d2d42013-04-05 01:27:11 -0500541 struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which, opcode);
Alex Elder33803f32013-03-13 20:50:00 -0500542
Alex Elderc99d2d42013-04-05 01:27:11 -0500543 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
Alex Elder33803f32013-03-13 20:50:00 -0500544
545 op->watch.cookie = cookie;
546 /* op->watch.ver = version; */ /* XXX 3847 */
547 op->watch.ver = cpu_to_le64(version);
548 if (opcode == CEPH_OSD_OP_WATCH && flag)
Alex Elderc99d2d42013-04-05 01:27:11 -0500549 op->watch.flag = (u8)1;
Alex Elder33803f32013-03-13 20:50:00 -0500550}
551EXPORT_SYMBOL(osd_req_op_watch_init);
552
Alex Elderec9123c2013-04-05 01:27:12 -0500553static void ceph_osdc_msg_data_set(struct ceph_msg *msg,
554 struct ceph_osd_data *osd_data)
555{
556 u64 length = ceph_osd_data_length(osd_data);
557
558 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
559 BUG_ON(length > (u64) SIZE_MAX);
560 if (length)
561 ceph_msg_data_set_pages(msg, osd_data->pages,
562 length, osd_data->alignment);
563 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
564 BUG_ON(!length);
565 ceph_msg_data_set_pagelist(msg, osd_data->pagelist);
566#ifdef CONFIG_BLOCK
567 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
568 ceph_msg_data_set_bio(msg, osd_data->bio, length);
569#endif
570 } else {
571 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
572 }
573}
574
Alex Elder175face2013-03-08 13:35:36 -0600575static u64 osd_req_encode_op(struct ceph_osd_request *req,
Alex Elder79528732013-04-03 21:32:51 -0500576 struct ceph_osd_op *dst, unsigned int which)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700577{
Alex Elder79528732013-04-03 21:32:51 -0500578 struct ceph_osd_req_op *src;
Alex Elder54d50642013-04-03 01:28:58 -0500579 u64 request_data_len = 0;
Alex Elder175face2013-03-08 13:35:36 -0600580
Alex Elder79528732013-04-03 21:32:51 -0500581 BUG_ON(which >= req->r_num_ops);
582 src = &req->r_ops[which];
Alex Eldera8dd0a32013-03-13 20:50:00 -0500583 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
584 pr_err("unrecognized osd opcode %d\n", src->op);
585
586 return 0;
587 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700588
Alex Elder065a68f2012-04-20 15:49:43 -0500589 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600590 case CEPH_OSD_OP_STAT:
591 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700592 case CEPH_OSD_OP_READ:
593 case CEPH_OSD_OP_WRITE:
Alex Elder175face2013-03-08 13:35:36 -0600594 if (src->op == CEPH_OSD_OP_WRITE)
Alex Elder54d50642013-04-03 01:28:58 -0500595 request_data_len = src->extent.length;
Alex Elder175face2013-03-08 13:35:36 -0600596 dst->extent.offset = cpu_to_le64(src->extent.offset);
597 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700598 dst->extent.truncate_size =
599 cpu_to_le64(src->extent.truncate_size);
600 dst->extent.truncate_seq =
601 cpu_to_le32(src->extent.truncate_seq);
Alex Elder54764922013-04-05 01:27:12 -0500602 if (src->op == CEPH_OSD_OP_WRITE)
Alex Elderec9123c2013-04-05 01:27:12 -0500603 ceph_osdc_msg_data_set(req->r_request,
Alex Elder54764922013-04-05 01:27:12 -0500604 &src->extent.osd_data);
605 else
Alex Elderec9123c2013-04-05 01:27:12 -0500606 ceph_osdc_msg_data_set(req->r_reply,
Alex Elder54764922013-04-05 01:27:12 -0500607 &src->extent.osd_data);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700608 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700609 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700610 dst->cls.class_len = src->cls.class_len;
611 dst->cls.method_len = src->cls.method_len;
Alex Elder54d50642013-04-03 01:28:58 -0500612 dst->cls.indata_len = cpu_to_le32(src->cls.request_data_len);
Alex Elder54764922013-04-05 01:27:12 -0500613 ceph_osdc_msg_data_set(req->r_reply, &src->cls.response_data);
614 ceph_osdc_msg_data_set(req->r_request, &src->cls.request_info);
615 BUG_ON(src->cls.request_info.type !=
Alex Elder5f562df2013-04-05 01:27:12 -0500616 CEPH_OSD_DATA_TYPE_PAGELIST);
Alex Elder54764922013-04-05 01:27:12 -0500617 request_data_len = src->cls.request_info.pagelist->length;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700618 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700619 case CEPH_OSD_OP_STARTSYNC:
620 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700621 case CEPH_OSD_OP_NOTIFY_ACK:
622 case CEPH_OSD_OP_WATCH:
623 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
624 dst->watch.ver = cpu_to_le64(src->watch.ver);
625 dst->watch.flag = src->watch.flag;
626 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700627 default:
Alex Elder4c464592013-02-15 11:42:30 -0600628 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600629 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600630 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -0500631
632 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700633 }
Alex Eldera8dd0a32013-03-13 20:50:00 -0500634 dst->op = cpu_to_le16(src->op);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700635 dst->payload_len = cpu_to_le32(src->payload_len);
Alex Elder175face2013-03-08 13:35:36 -0600636
Alex Elder54d50642013-04-03 01:28:58 -0500637 return request_data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700638}
639
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700640/*
Sage Weilf24e9982009-10-06 11:31:10 -0700641 * build new request AND message, calculate layout, and adjust file
642 * extent as needed.
643 *
644 * if the file was recently truncated, we include information about its
645 * old and new size so that the object can be updated appropriately. (we
646 * avoid synchronously deleting truncated objects because it's slow.)
647 *
648 * if @do_sync, include a 'startsync' command so that the osd will flush
649 * data quickly.
650 */
651struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
652 struct ceph_file_layout *layout,
653 struct ceph_vino vino,
Alex Elderacead002013-03-14 14:09:05 -0500654 u64 off, u64 *plen, int num_ops,
Sage Weilf24e9982009-10-06 11:31:10 -0700655 int opcode, int flags,
656 struct ceph_snap_context *snapc,
Sage Weilf24e9982009-10-06 11:31:10 -0700657 u32 truncate_seq,
658 u64 truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -0600659 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700660{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700661 struct ceph_osd_request *req;
Alex Elder75d1c942013-03-13 20:50:00 -0500662 u64 objnum = 0;
663 u64 objoff = 0;
664 u64 objlen = 0;
Alex Elderd18d1e22013-03-13 20:50:01 -0500665 u32 object_size;
666 u64 object_base;
Sage Weil68162822012-09-24 21:01:02 -0700667 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700668
Alex Elderd18d1e22013-03-13 20:50:01 -0500669 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700670
Alex Elderacead002013-03-14 14:09:05 -0500671 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600672 GFP_NOFS);
Sage Weil4ad12622011-05-03 09:23:36 -0700673 if (!req)
Sage Weil68162822012-09-24 21:01:02 -0700674 return ERR_PTR(-ENOMEM);
Alex Elder79528732013-04-03 21:32:51 -0500675
Alex Elderd178a9e2012-11-13 21:11:15 -0600676 req->r_flags = flags;
Sage Weilf24e9982009-10-06 11:31:10 -0700677
678 /* calculate max write size */
Alex Eldera19dadf2013-03-13 20:50:01 -0500679 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
Alex Elder3ff5f382013-02-15 22:10:17 -0600680 if (r < 0) {
681 ceph_osdc_put_request(req);
Sage Weil68162822012-09-24 21:01:02 -0700682 return ERR_PTR(r);
Alex Elder3ff5f382013-02-15 22:10:17 -0600683 }
Alex Eldera19dadf2013-03-13 20:50:01 -0500684
Alex Elderd18d1e22013-03-13 20:50:01 -0500685 object_size = le32_to_cpu(layout->fl_object_size);
686 object_base = off - objoff;
687 if (truncate_size <= object_base) {
688 truncate_size = 0;
689 } else {
690 truncate_size -= object_base;
691 if (truncate_size > object_size)
692 truncate_size = object_size;
Alex Eldera19dadf2013-03-13 20:50:01 -0500693 }
Alex Elderd18d1e22013-03-13 20:50:01 -0500694
Alex Elderc99d2d42013-04-05 01:27:11 -0500695 osd_req_op_extent_init(req, 0, opcode, objoff, objlen,
Alex Elderb0270322013-03-13 20:50:01 -0500696 truncate_size, truncate_seq);
Alex Elder8c042b02013-04-03 01:28:58 -0500697
Alex Elderacead002013-03-14 14:09:05 -0500698 /*
699 * A second op in the ops array means the caller wants to
700 * also issue a include a 'startsync' command so that the
701 * osd will flush data quickly.
702 */
703 if (num_ops > 1)
Alex Elderc99d2d42013-04-05 01:27:11 -0500704 osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC);
Alex Elderd18d1e22013-03-13 20:50:01 -0500705
Sage Weilf24e9982009-10-06 11:31:10 -0700706 req->r_file_layout = *layout; /* keep a copy */
707
Alex Elder75d1c942013-03-13 20:50:00 -0500708 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx",
709 vino.ino, objnum);
Alex Elderdbe0fc42013-02-15 22:10:17 -0600710 req->r_oid_len = strlen(req->r_oid);
711
Sage Weilf24e9982009-10-06 11:31:10 -0700712 return req;
713}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700714EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700715
716/*
717 * We keep osd requests in an rbtree, sorted by ->r_tid.
718 */
719static void __insert_request(struct ceph_osd_client *osdc,
720 struct ceph_osd_request *new)
721{
722 struct rb_node **p = &osdc->requests.rb_node;
723 struct rb_node *parent = NULL;
724 struct ceph_osd_request *req = NULL;
725
726 while (*p) {
727 parent = *p;
728 req = rb_entry(parent, struct ceph_osd_request, r_node);
729 if (new->r_tid < req->r_tid)
730 p = &(*p)->rb_left;
731 else if (new->r_tid > req->r_tid)
732 p = &(*p)->rb_right;
733 else
734 BUG();
735 }
736
737 rb_link_node(&new->r_node, parent, p);
738 rb_insert_color(&new->r_node, &osdc->requests);
739}
740
741static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
742 u64 tid)
743{
744 struct ceph_osd_request *req;
745 struct rb_node *n = osdc->requests.rb_node;
746
747 while (n) {
748 req = rb_entry(n, struct ceph_osd_request, r_node);
749 if (tid < req->r_tid)
750 n = n->rb_left;
751 else if (tid > req->r_tid)
752 n = n->rb_right;
753 else
754 return req;
755 }
756 return NULL;
757}
758
759static struct ceph_osd_request *
760__lookup_request_ge(struct ceph_osd_client *osdc,
761 u64 tid)
762{
763 struct ceph_osd_request *req;
764 struct rb_node *n = osdc->requests.rb_node;
765
766 while (n) {
767 req = rb_entry(n, struct ceph_osd_request, r_node);
768 if (tid < req->r_tid) {
769 if (!n->rb_left)
770 return req;
771 n = n->rb_left;
772 } else if (tid > req->r_tid) {
773 n = n->rb_right;
774 } else {
775 return req;
776 }
777 }
778 return NULL;
779}
780
Sage Weil6f6c7002011-01-17 20:34:08 -0800781/*
782 * Resubmit requests pending on the given osd.
783 */
784static void __kick_osd_requests(struct ceph_osd_client *osdc,
785 struct ceph_osd *osd)
786{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700787 struct ceph_osd_request *req, *nreq;
Alex Eldere02493c2013-03-25 18:16:11 -0500788 LIST_HEAD(resend);
Sage Weil6f6c7002011-01-17 20:34:08 -0800789 int err;
790
791 dout("__kick_osd_requests osd%d\n", osd->o_osd);
792 err = __reset_osd(osdc, osd);
Alex Elder685a7552012-12-07 09:57:58 -0600793 if (err)
Sage Weil6f6c7002011-01-17 20:34:08 -0800794 return;
Alex Eldere02493c2013-03-25 18:16:11 -0500795 /*
796 * Build up a list of requests to resend by traversing the
797 * osd's list of requests. Requests for a given object are
798 * sent in tid order, and that is also the order they're
799 * kept on this list. Therefore all requests that are in
800 * flight will be found first, followed by all requests that
801 * have not yet been sent. And to resend requests while
802 * preserving this order we will want to put any sent
803 * requests back on the front of the osd client's unsent
804 * list.
805 *
806 * So we build a separate ordered list of already-sent
807 * requests for the affected osd and splice it onto the
808 * front of the osd client's unsent list. Once we've seen a
809 * request that has not yet been sent we're done. Those
810 * requests are already sitting right where they belong.
811 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800812 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
Alex Eldere02493c2013-03-25 18:16:11 -0500813 if (!req->r_sent)
814 break;
815 list_move_tail(&req->r_req_lru_item, &resend);
816 dout("requeueing %p tid %llu osd%d\n", req, req->r_tid,
Sage Weil6f6c7002011-01-17 20:34:08 -0800817 osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700818 if (!req->r_linger)
819 req->r_flags |= CEPH_OSD_FLAG_RETRY;
820 }
Alex Eldere02493c2013-03-25 18:16:11 -0500821 list_splice(&resend, &osdc->req_unsent);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700822
Alex Eldere02493c2013-03-25 18:16:11 -0500823 /*
824 * Linger requests are re-registered before sending, which
825 * sets up a new tid for each. We add them to the unsent
826 * list at the end to keep things in tid order.
827 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700828 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
829 r_linger_osd) {
Sage Weil77f38e02011-04-06 09:09:16 -0700830 /*
831 * reregister request prior to unregistering linger so
832 * that r_osd is preserved.
833 */
834 BUG_ON(!list_empty(&req->r_req_lru_item));
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700835 __register_request(osdc, req);
Alex Eldere02493c2013-03-25 18:16:11 -0500836 list_add_tail(&req->r_req_lru_item, &osdc->req_unsent);
Alex Elderad885922013-03-25 18:16:11 -0500837 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
Sage Weil77f38e02011-04-06 09:09:16 -0700838 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700839 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
840 osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800841 }
842}
843
Sage Weilf24e9982009-10-06 11:31:10 -0700844/*
Sage Weil81b024e2009-10-09 10:29:18 -0700845 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700846 */
847static void osd_reset(struct ceph_connection *con)
848{
849 struct ceph_osd *osd = con->private;
850 struct ceph_osd_client *osdc;
851
852 if (!osd)
853 return;
854 dout("osd_reset osd%d\n", osd->o_osd);
855 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -0700856 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -0800857 mutex_lock(&osdc->request_mutex);
858 __kick_osd_requests(osdc, osd);
Alex Elderf9d25192013-02-15 11:42:29 -0600859 __send_queued(osdc);
Sage Weil83aff952012-11-28 12:28:24 -0800860 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -0700861 up_read(&osdc->map_sem);
862}
863
864/*
865 * Track open sessions with osds.
866 */
Alex Eldere10006f2012-05-26 23:26:43 -0500867static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -0700868{
869 struct ceph_osd *osd;
870
871 osd = kzalloc(sizeof(*osd), GFP_NOFS);
872 if (!osd)
873 return NULL;
874
875 atomic_set(&osd->o_ref, 1);
876 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -0500877 osd->o_osd = onum;
Alex Elderf4077312012-12-06 07:22:04 -0600878 RB_CLEAR_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -0700879 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700880 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800881 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700882 osd->o_incarnation = 1;
883
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700884 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800885
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800886 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700887 return osd;
888}
889
890static struct ceph_osd *get_osd(struct ceph_osd *osd)
891{
892 if (atomic_inc_not_zero(&osd->o_ref)) {
893 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
894 atomic_read(&osd->o_ref));
895 return osd;
896 } else {
897 dout("get_osd %p FAIL\n", osd);
898 return NULL;
899 }
900}
901
902static void put_osd(struct ceph_osd *osd)
903{
904 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
905 atomic_read(&osd->o_ref) - 1);
Alex Eldera2556512012-05-16 15:16:39 -0500906 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
Sage Weil79494d12010-05-27 14:15:49 -0700907 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
908
Sage Weil27859f92013-03-25 10:26:14 -0700909 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -0700910 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -0700911 }
Sage Weilf24e9982009-10-06 11:31:10 -0700912}
913
914/*
915 * remove an osd from our map
916 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800917static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700918{
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800919 dout("__remove_osd %p\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700920 BUG_ON(!list_empty(&osd->o_requests));
921 rb_erase(&osd->o_node, &osdc->osds);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800922 list_del_init(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700923 ceph_con_close(&osd->o_con);
924 put_osd(osd);
925}
926
Sage Weilaca420b2011-08-31 14:45:53 -0700927static void remove_all_osds(struct ceph_osd_client *osdc)
928{
Jiaju Zhang048a9d22012-07-20 08:18:36 -0500929 dout("%s %p\n", __func__, osdc);
Sage Weilaca420b2011-08-31 14:45:53 -0700930 mutex_lock(&osdc->request_mutex);
931 while (!RB_EMPTY_ROOT(&osdc->osds)) {
932 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
933 struct ceph_osd, o_node);
934 __remove_osd(osdc, osd);
935 }
936 mutex_unlock(&osdc->request_mutex);
937}
938
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800939static void __move_osd_to_lru(struct ceph_osd_client *osdc,
940 struct ceph_osd *osd)
941{
942 dout("__move_osd_to_lru %p\n", osd);
943 BUG_ON(!list_empty(&osd->o_osd_lru));
944 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700945 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800946}
947
948static void __remove_osd_from_lru(struct ceph_osd *osd)
949{
950 dout("__remove_osd_from_lru %p\n", osd);
951 if (!list_empty(&osd->o_osd_lru))
952 list_del_init(&osd->o_osd_lru);
953}
954
Sage Weilaca420b2011-08-31 14:45:53 -0700955static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800956{
957 struct ceph_osd *osd, *nosd;
958
959 dout("__remove_old_osds %p\n", osdc);
960 mutex_lock(&osdc->request_mutex);
961 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -0700962 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800963 break;
964 __remove_osd(osdc, osd);
965 }
966 mutex_unlock(&osdc->request_mutex);
967}
968
Sage Weilf24e9982009-10-06 11:31:10 -0700969/*
970 * reset osd connect
971 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800972static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700973{
Alex Elderc3acb182012-12-07 09:57:58 -0600974 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -0700975
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800976 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700977 if (list_empty(&osd->o_requests) &&
978 list_empty(&osd->o_linger_requests)) {
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800979 __remove_osd(osdc, osd);
Alex Elderc3acb182012-12-07 09:57:58 -0600980
981 return -ENODEV;
982 }
983
984 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
985 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
986 !ceph_con_opened(&osd->o_con)) {
987 struct ceph_osd_request *req;
988
Sage Weil87b315a2010-03-22 14:51:18 -0700989 dout(" osd addr hasn't changed and connection never opened,"
990 " letting msgr retry");
991 /* touch each r_stamp for handle_timeout()'s benfit */
992 list_for_each_entry(req, &osd->o_requests, r_osd_item)
993 req->r_stamp = jiffies;
Alex Elderc3acb182012-12-07 09:57:58 -0600994
995 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -0700996 }
Alex Elderc3acb182012-12-07 09:57:58 -0600997
998 ceph_con_close(&osd->o_con);
999 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1000 osd->o_incarnation++;
1001
1002 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001003}
1004
1005static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1006{
1007 struct rb_node **p = &osdc->osds.rb_node;
1008 struct rb_node *parent = NULL;
1009 struct ceph_osd *osd = NULL;
1010
Sage Weilaca420b2011-08-31 14:45:53 -07001011 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001012 while (*p) {
1013 parent = *p;
1014 osd = rb_entry(parent, struct ceph_osd, o_node);
1015 if (new->o_osd < osd->o_osd)
1016 p = &(*p)->rb_left;
1017 else if (new->o_osd > osd->o_osd)
1018 p = &(*p)->rb_right;
1019 else
1020 BUG();
1021 }
1022
1023 rb_link_node(&new->o_node, parent, p);
1024 rb_insert_color(&new->o_node, &osdc->osds);
1025}
1026
1027static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1028{
1029 struct ceph_osd *osd;
1030 struct rb_node *n = osdc->osds.rb_node;
1031
1032 while (n) {
1033 osd = rb_entry(n, struct ceph_osd, o_node);
1034 if (o < osd->o_osd)
1035 n = n->rb_left;
1036 else if (o > osd->o_osd)
1037 n = n->rb_right;
1038 else
1039 return osd;
1040 }
1041 return NULL;
1042}
1043
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001044static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1045{
1046 schedule_delayed_work(&osdc->timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001047 osdc->client->options->osd_keepalive_timeout * HZ);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001048}
1049
1050static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1051{
1052 cancel_delayed_work(&osdc->timeout_work);
1053}
Sage Weilf24e9982009-10-06 11:31:10 -07001054
1055/*
1056 * Register request, assign tid. If this is the first request, set up
1057 * the timeout event.
1058 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001059static void __register_request(struct ceph_osd_client *osdc,
1060 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001061{
Sage Weilf24e9982009-10-06 11:31:10 -07001062 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -08001063 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -07001064 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001065 __insert_request(osdc, req);
1066 ceph_osdc_get_request(req);
1067 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -07001068 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001069 dout(" first request, scheduling timeout\n");
1070 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001071 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001072}
1073
Sage Weilf24e9982009-10-06 11:31:10 -07001074/*
1075 * called under osdc->request_mutex
1076 */
1077static void __unregister_request(struct ceph_osd_client *osdc,
1078 struct ceph_osd_request *req)
1079{
Sage Weil35f9f8a2012-05-16 15:16:38 -05001080 if (RB_EMPTY_NODE(&req->r_node)) {
1081 dout("__unregister_request %p tid %lld not registered\n",
1082 req, req->r_tid);
1083 return;
1084 }
1085
Sage Weilf24e9982009-10-06 11:31:10 -07001086 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1087 rb_erase(&req->r_node, &osdc->requests);
1088 osdc->num_requests--;
1089
Sage Weil0ba64782009-10-08 16:57:16 -07001090 if (req->r_osd) {
1091 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -05001092 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -07001093
1094 list_del_init(&req->r_osd_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001095 if (list_empty(&req->r_osd->o_requests) &&
1096 list_empty(&req->r_osd->o_linger_requests)) {
1097 dout("moving osd to %p lru\n", req->r_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001098 __move_osd_to_lru(osdc, req->r_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001099 }
Sage Weilfbdb9192011-03-29 12:11:06 -07001100 if (list_empty(&req->r_linger_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001101 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -07001102 }
Sage Weilf24e9982009-10-06 11:31:10 -07001103
Alex Elder7d5f2482012-11-29 08:37:03 -06001104 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001105 ceph_osdc_put_request(req);
1106
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001107 if (osdc->num_requests == 0) {
1108 dout(" no requests, canceling timeout\n");
1109 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001110 }
1111}
1112
1113/*
1114 * Cancel a previously queued request message
1115 */
1116static void __cancel_request(struct ceph_osd_request *req)
1117{
Sage Weil6bc18872010-09-27 10:18:52 -07001118 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -05001119 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001120 req->r_sent = 0;
1121 }
1122}
1123
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001124static void __register_linger_request(struct ceph_osd_client *osdc,
1125 struct ceph_osd_request *req)
1126{
1127 dout("__register_linger_request %p\n", req);
1128 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -07001129 if (req->r_osd)
1130 list_add_tail(&req->r_linger_osd,
1131 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001132}
1133
1134static void __unregister_linger_request(struct ceph_osd_client *osdc,
1135 struct ceph_osd_request *req)
1136{
1137 dout("__unregister_linger_request %p\n", req);
Alex Elder61c74032012-12-06 09:37:23 -06001138 list_del_init(&req->r_linger_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001139 if (req->r_osd) {
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001140 list_del_init(&req->r_linger_osd);
1141
1142 if (list_empty(&req->r_osd->o_requests) &&
1143 list_empty(&req->r_osd->o_linger_requests)) {
1144 dout("moving osd to %p lru\n", req->r_osd);
1145 __move_osd_to_lru(osdc, req->r_osd);
1146 }
Sage Weilfbdb9192011-03-29 12:11:06 -07001147 if (list_empty(&req->r_osd_item))
1148 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001149 }
1150}
1151
1152void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
1153 struct ceph_osd_request *req)
1154{
1155 mutex_lock(&osdc->request_mutex);
1156 if (req->r_linger) {
1157 __unregister_linger_request(osdc, req);
1158 ceph_osdc_put_request(req);
1159 }
1160 mutex_unlock(&osdc->request_mutex);
1161}
1162EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
1163
1164void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1165 struct ceph_osd_request *req)
1166{
1167 if (!req->r_linger) {
1168 dout("set_request_linger %p\n", req);
1169 req->r_linger = 1;
1170 /*
1171 * caller is now responsible for calling
1172 * unregister_linger_request
1173 */
1174 ceph_osdc_get_request(req);
1175 }
1176}
1177EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1178
Sage Weilf24e9982009-10-06 11:31:10 -07001179/*
1180 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1181 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001182 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -08001183 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -07001184 *
1185 * Return 0 if unchanged, 1 if changed, or negative on error.
1186 *
1187 * Caller should hold map_sem for read and request_mutex.
1188 */
Sage Weil6f6c7002011-01-17 20:34:08 -08001189static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -07001190 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -07001191{
Sage Weil5b191d92013-02-23 10:38:16 -08001192 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -07001193 int acting[CEPH_PG_MAX_SIZE];
1194 int o = -1, num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001195 int err;
Sage Weilf24e9982009-10-06 11:31:10 -07001196
Sage Weil6f6c7002011-01-17 20:34:08 -08001197 dout("map_request %p tid %lld\n", req, req->r_tid);
Alex Elder41766f82013-03-01 18:00:15 -06001198 err = ceph_calc_ceph_pg(&pgid, req->r_oid, osdc->osdmap,
1199 ceph_file_layout_pg_pool(req->r_file_layout));
Sage Weil6f6c7002011-01-17 20:34:08 -08001200 if (err) {
1201 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -07001202 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -08001203 }
Sage Weil7740a422010-01-08 15:58:25 -08001204 req->r_pgid = pgid;
1205
Sage Weild85b7052010-05-10 10:24:48 -07001206 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
1207 if (err > 0) {
1208 o = acting[0];
1209 num = err;
1210 }
Sage Weilf24e9982009-10-06 11:31:10 -07001211
Sage Weil38d64532011-10-14 13:33:55 -07001212 if ((!force_resend &&
1213 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -07001214 req->r_sent >= req->r_osd->o_incarnation &&
1215 req->r_num_pg_osds == num &&
1216 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001217 (req->r_osd == NULL && o == -1))
1218 return 0; /* no change */
1219
Sage Weil5b191d92013-02-23 10:38:16 -08001220 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1221 req->r_tid, pgid.pool, pgid.seed, o,
Sage Weilf24e9982009-10-06 11:31:10 -07001222 req->r_osd ? req->r_osd->o_osd : -1);
1223
Sage Weild85b7052010-05-10 10:24:48 -07001224 /* record full pg acting set */
1225 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1226 req->r_num_pg_osds = num;
1227
Sage Weilf24e9982009-10-06 11:31:10 -07001228 if (req->r_osd) {
1229 __cancel_request(req);
1230 list_del_init(&req->r_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001231 req->r_osd = NULL;
1232 }
1233
1234 req->r_osd = __lookup_osd(osdc, o);
1235 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -08001236 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -05001237 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -08001238 if (!req->r_osd) {
1239 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001240 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001241 }
Sage Weilf24e9982009-10-06 11:31:10 -07001242
Sage Weil6f6c7002011-01-17 20:34:08 -08001243 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001244 __insert_osd(osdc, req->r_osd);
1245
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001246 ceph_con_open(&req->r_osd->o_con,
1247 CEPH_ENTITY_TYPE_OSD, o,
1248 &osdc->osdmap->osd_addr[o]);
Sage Weilf24e9982009-10-06 11:31:10 -07001249 }
1250
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001251 if (req->r_osd) {
1252 __remove_osd_from_lru(req->r_osd);
Alex Elderad885922013-03-25 18:16:11 -05001253 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1254 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
Sage Weil6f6c7002011-01-17 20:34:08 -08001255 } else {
Alex Elderad885922013-03-25 18:16:11 -05001256 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001257 }
Sage Weild85b7052010-05-10 10:24:48 -07001258 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001259
1260out:
Sage Weilf24e9982009-10-06 11:31:10 -07001261 return err;
1262}
1263
1264/*
1265 * caller should hold map_sem (for read) and request_mutex
1266 */
Sage Weil56e925b2012-01-03 12:34:34 -08001267static void __send_request(struct ceph_osd_client *osdc,
1268 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001269{
Sage Weil1b83bef2013-02-25 16:11:12 -08001270 void *p;
Sage Weilf24e9982009-10-06 11:31:10 -07001271
Sage Weil1b83bef2013-02-25 16:11:12 -08001272 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1273 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1274 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001275
Sage Weil1b83bef2013-02-25 16:11:12 -08001276 /* fill in message content that changes each time we send it */
1277 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1278 put_unaligned_le32(req->r_flags, req->r_request_flags);
1279 put_unaligned_le64(req->r_pgid.pool, req->r_request_pool);
1280 p = req->r_request_pgid;
1281 ceph_encode_64(&p, req->r_pgid.pool);
1282 ceph_encode_32(&p, req->r_pgid.seed);
1283 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1284 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1285 sizeof(req->r_reassert_version));
Sage Weil2169aea2013-02-25 16:13:08 -08001286
Sage Weil3dd72fc2010-03-22 14:42:30 -07001287 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001288 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001289
1290 ceph_msg_get(req->r_request); /* send consumes a ref */
1291 ceph_con_send(&req->r_osd->o_con, req->r_request);
1292 req->r_sent = req->r_osd->o_incarnation;
Sage Weilf24e9982009-10-06 11:31:10 -07001293}
1294
1295/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001296 * Send any requests in the queue (req_unsent).
1297 */
Alex Elderf9d25192013-02-15 11:42:29 -06001298static void __send_queued(struct ceph_osd_client *osdc)
Sage Weil6f6c7002011-01-17 20:34:08 -08001299{
1300 struct ceph_osd_request *req, *tmp;
1301
Alex Elderf9d25192013-02-15 11:42:29 -06001302 dout("__send_queued\n");
1303 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
Sage Weil6f6c7002011-01-17 20:34:08 -08001304 __send_request(osdc, req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001305}
1306
1307/*
Sage Weilf24e9982009-10-06 11:31:10 -07001308 * Timeout callback, called every N seconds when 1 or more osd
1309 * requests has been active for more than N seconds. When this
1310 * happens, we ping all OSDs with requests who have timed out to
1311 * ensure any communications channel reset is detected. Reset the
1312 * request timeouts another N seconds in the future as we go.
1313 * Reschedule the timeout event another N seconds in future (unless
1314 * there are no open requests).
1315 */
1316static void handle_timeout(struct work_struct *work)
1317{
1318 struct ceph_osd_client *osdc =
1319 container_of(work, struct ceph_osd_client, timeout_work.work);
Sage Weil83aff952012-11-28 12:28:24 -08001320 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001321 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001322 unsigned long keepalive =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001323 osdc->client->options->osd_keepalive_timeout * HZ;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001324 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001325 dout("timeout\n");
1326 down_read(&osdc->map_sem);
1327
1328 ceph_monc_request_next_osdmap(&osdc->client->monc);
1329
1330 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001331
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001332 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001333 * ping osds that are a bit slow. this ensures that if there
1334 * is a break in the TCP connection we will notice, and reopen
1335 * a connection with that osd (from the fault callback).
1336 */
1337 INIT_LIST_HEAD(&slow_osds);
1338 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Sage Weil3dd72fc2010-03-22 14:42:30 -07001339 if (time_before(jiffies, req->r_stamp + keepalive))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001340 break;
1341
1342 osd = req->r_osd;
1343 BUG_ON(!osd);
1344 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001345 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001346 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1347 }
1348 while (!list_empty(&slow_osds)) {
1349 osd = list_entry(slow_osds.next, struct ceph_osd,
1350 o_keepalive_item);
1351 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001352 ceph_con_keepalive(&osd->o_con);
1353 }
1354
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001355 __schedule_osd_timeout(osdc);
Alex Elderf9d25192013-02-15 11:42:29 -06001356 __send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001357 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001358 up_read(&osdc->map_sem);
1359}
1360
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001361static void handle_osds_timeout(struct work_struct *work)
1362{
1363 struct ceph_osd_client *osdc =
1364 container_of(work, struct ceph_osd_client,
1365 osds_timeout_work.work);
1366 unsigned long delay =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001367 osdc->client->options->osd_idle_ttl * HZ >> 2;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001368
1369 dout("osds timeout\n");
1370 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001371 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001372 up_read(&osdc->map_sem);
1373
1374 schedule_delayed_work(&osdc->osds_timeout_work,
1375 round_jiffies_relative(delay));
1376}
1377
Sage Weil25845472011-06-03 09:37:09 -07001378static void complete_request(struct ceph_osd_request *req)
1379{
1380 if (req->r_safe_callback)
1381 req->r_safe_callback(req, NULL);
1382 complete_all(&req->r_safe_completion); /* fsync waiter */
1383}
1384
Sage Weilf24e9982009-10-06 11:31:10 -07001385/*
1386 * handle osd op reply. either call the callback if it is specified,
1387 * or do the completion to wake up the waiting thread.
1388 */
Sage Weil350b1c32009-12-22 10:45:45 -08001389static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1390 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -07001391{
Sage Weil1b83bef2013-02-25 16:11:12 -08001392 void *p, *end;
Sage Weilf24e9982009-10-06 11:31:10 -07001393 struct ceph_osd_request *req;
1394 u64 tid;
Sage Weil1b83bef2013-02-25 16:11:12 -08001395 int object_len;
Alex Elder79528732013-04-03 21:32:51 -05001396 unsigned int numops;
1397 int payload_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001398 s32 result;
Sage Weil1b83bef2013-02-25 16:11:12 -08001399 s32 retry_attempt;
1400 struct ceph_pg pg;
1401 int err;
1402 u32 reassert_epoch;
1403 u64 reassert_version;
1404 u32 osdmap_epoch;
Alex Elder0d5af162013-02-27 10:26:25 -06001405 int already_completed;
Alex Elder9fc6e062013-04-03 01:28:57 -05001406 u32 bytes;
Alex Elder79528732013-04-03 21:32:51 -05001407 unsigned int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001408
Sage Weil6df058c2009-12-22 11:24:33 -08001409 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil1b83bef2013-02-25 16:11:12 -08001410 dout("handle_reply %p tid %llu\n", msg, tid);
1411
1412 p = msg->front.iov_base;
1413 end = p + msg->front.iov_len;
1414
1415 ceph_decode_need(&p, end, 4, bad);
1416 object_len = ceph_decode_32(&p);
1417 ceph_decode_need(&p, end, object_len, bad);
1418 p += object_len;
1419
Alex Elderef4859d2013-04-01 18:58:26 -05001420 err = ceph_decode_pgid(&p, end, &pg);
Sage Weil1b83bef2013-02-25 16:11:12 -08001421 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001422 goto bad;
Sage Weil1b83bef2013-02-25 16:11:12 -08001423
1424 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1425 flags = ceph_decode_64(&p);
1426 result = ceph_decode_32(&p);
1427 reassert_epoch = ceph_decode_32(&p);
1428 reassert_version = ceph_decode_64(&p);
1429 osdmap_epoch = ceph_decode_32(&p);
1430
Sage Weilf24e9982009-10-06 11:31:10 -07001431 /* lookup */
1432 mutex_lock(&osdc->request_mutex);
1433 req = __lookup_request(osdc, tid);
1434 if (req == NULL) {
1435 dout("handle_reply tid %llu dne\n", tid);
Alex Elder8058fd42013-04-01 18:58:26 -05001436 goto bad_mutex;
Sage Weilf24e9982009-10-06 11:31:10 -07001437 }
1438 ceph_osdc_get_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001439
1440 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1441 req, result);
1442
1443 ceph_decode_need(&p, end, 4, bad);
1444 numops = ceph_decode_32(&p);
1445 if (numops > CEPH_OSD_MAX_OP)
1446 goto bad_put;
1447 if (numops != req->r_num_ops)
1448 goto bad_put;
1449 payload_len = 0;
1450 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad);
1451 for (i = 0; i < numops; i++) {
1452 struct ceph_osd_op *op = p;
1453 int len;
1454
1455 len = le32_to_cpu(op->payload_len);
1456 req->r_reply_op_len[i] = len;
1457 dout(" op %d has %d bytes\n", i, len);
1458 payload_len += len;
1459 p += sizeof(*op);
1460 }
Alex Elder9fc6e062013-04-03 01:28:57 -05001461 bytes = le32_to_cpu(msg->hdr.data_len);
1462 if (payload_len != bytes) {
Sage Weil1b83bef2013-02-25 16:11:12 -08001463 pr_warning("sum of op payload lens %d != data_len %d",
Alex Elder9fc6e062013-04-03 01:28:57 -05001464 payload_len, bytes);
Sage Weil1b83bef2013-02-25 16:11:12 -08001465 goto bad_put;
1466 }
1467
1468 ceph_decode_need(&p, end, 4 + numops * 4, bad);
1469 retry_attempt = ceph_decode_32(&p);
1470 for (i = 0; i < numops; i++)
1471 req->r_reply_op_result[i] = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001472
Sage Weilf24e9982009-10-06 11:31:10 -07001473 if (!req->r_got_reply) {
Sage Weilf24e9982009-10-06 11:31:10 -07001474
Sage Weil1b83bef2013-02-25 16:11:12 -08001475 req->r_result = result;
Sage Weilf24e9982009-10-06 11:31:10 -07001476 dout("handle_reply result %d bytes %d\n", req->r_result,
1477 bytes);
1478 if (req->r_result == 0)
1479 req->r_result = bytes;
1480
1481 /* in case this is a write and we need to replay, */
Sage Weil1b83bef2013-02-25 16:11:12 -08001482 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1483 req->r_reassert_version.version = cpu_to_le64(reassert_version);
Sage Weilf24e9982009-10-06 11:31:10 -07001484
1485 req->r_got_reply = 1;
1486 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1487 dout("handle_reply tid %llu dup ack\n", tid);
Sage Weil34b43a52009-12-01 12:23:54 -08001488 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001489 goto done;
1490 }
1491
1492 dout("handle_reply tid %llu flags %d\n", tid, flags);
1493
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001494 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1495 __register_linger_request(osdc, req);
1496
Sage Weilf24e9982009-10-06 11:31:10 -07001497 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001498 if (result < 0 ||
1499 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001500 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1501 __unregister_request(osdc, req);
1502
Alex Elder0d5af162013-02-27 10:26:25 -06001503 already_completed = req->r_completed;
1504 req->r_completed = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07001505 mutex_unlock(&osdc->request_mutex);
Alex Elder0d5af162013-02-27 10:26:25 -06001506 if (already_completed)
1507 goto done;
Sage Weilf24e9982009-10-06 11:31:10 -07001508
1509 if (req->r_callback)
1510 req->r_callback(req, msg);
1511 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001512 complete_all(&req->r_completion);
Sage Weilf24e9982009-10-06 11:31:10 -07001513
Sage Weil25845472011-06-03 09:37:09 -07001514 if (flags & CEPH_OSD_FLAG_ONDISK)
1515 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001516
1517done:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001518 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001519 ceph_osdc_put_request(req);
1520 return;
1521
Sage Weil1b83bef2013-02-25 16:11:12 -08001522bad_put:
1523 ceph_osdc_put_request(req);
Alex Elder8058fd42013-04-01 18:58:26 -05001524bad_mutex:
1525 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001526bad:
Sage Weil1b83bef2013-02-25 16:11:12 -08001527 pr_err("corrupt osd_op_reply got %d %d\n",
1528 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001529 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001530}
1531
Sage Weil6f6c7002011-01-17 20:34:08 -08001532static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001533{
Sage Weilf24e9982009-10-06 11:31:10 -07001534 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001535
Sage Weil6f6c7002011-01-17 20:34:08 -08001536 for (p = rb_first(&osdc->osds); p; p = n) {
1537 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001538
Sage Weil6f6c7002011-01-17 20:34:08 -08001539 n = rb_next(p);
1540 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1541 memcmp(&osd->o_con.peer_addr,
1542 ceph_osd_addr(osdc->osdmap,
1543 osd->o_osd),
1544 sizeof(struct ceph_entity_addr)) != 0)
1545 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001546 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001547}
1548
1549/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001550 * Requeue requests whose mapping to an OSD has changed. If requests map to
1551 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001552 *
Alex Eldere6d50f62012-12-26 14:31:40 -06001553 * Caller should hold map_sem for read.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001554 */
Sage Weil38d64532011-10-14 13:33:55 -07001555static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001556{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001557 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001558 struct rb_node *p;
1559 int needmap = 0;
1560 int err;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001561
Sage Weil38d64532011-10-14 13:33:55 -07001562 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001563 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07001564 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08001565 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07001566 p = rb_next(p);
Alex Elderab60b162012-12-19 15:52:36 -06001567
1568 /*
1569 * For linger requests that have not yet been
1570 * registered, move them to the linger list; they'll
1571 * be sent to the osd in the loop below. Unregister
1572 * the request before re-registering it as a linger
1573 * request to ensure the __map_request() below
1574 * will decide it needs to be sent.
1575 */
1576 if (req->r_linger && list_empty(&req->r_linger_item)) {
1577 dout("%p tid %llu restart on osd%d\n",
1578 req, req->r_tid,
1579 req->r_osd ? req->r_osd->o_osd : -1);
1580 __unregister_request(osdc, req);
1581 __register_linger_request(osdc, req);
1582 continue;
1583 }
1584
Sage Weil38d64532011-10-14 13:33:55 -07001585 err = __map_request(osdc, req, force_resend);
Sage Weil6f6c7002011-01-17 20:34:08 -08001586 if (err < 0)
1587 continue; /* error */
1588 if (req->r_osd == NULL) {
1589 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1590 needmap++; /* request a newer map */
1591 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07001592 if (!req->r_linger) {
1593 dout("%p tid %llu requeued on osd%d\n", req,
1594 req->r_tid,
1595 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001596 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07001597 }
1598 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001599 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001600
1601 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1602 r_linger_item) {
1603 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1604
Sage Weil38d64532011-10-14 13:33:55 -07001605 err = __map_request(osdc, req, force_resend);
Alex Elderab60b162012-12-19 15:52:36 -06001606 dout("__map_request returned %d\n", err);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001607 if (err == 0)
1608 continue; /* no change and no osd was specified */
1609 if (err < 0)
1610 continue; /* hrm! */
1611 if (req->r_osd == NULL) {
1612 dout("tid %llu maps to no valid osd\n", req->r_tid);
1613 needmap++; /* request a newer map */
1614 continue;
1615 }
1616
1617 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1618 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001619 __register_request(osdc, req);
Alex Elderc89ce052012-12-06 07:22:04 -06001620 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001621 }
Sage Weilf24e9982009-10-06 11:31:10 -07001622 mutex_unlock(&osdc->request_mutex);
1623
1624 if (needmap) {
1625 dout("%d requests for down osds, need new map\n", needmap);
1626 ceph_monc_request_next_osdmap(&osdc->client->monc);
1627 }
Alex Eldere6d50f62012-12-26 14:31:40 -06001628 reset_changed_osds(osdc);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001629}
Sage Weil6f6c7002011-01-17 20:34:08 -08001630
1631
Sage Weilf24e9982009-10-06 11:31:10 -07001632/*
1633 * Process updated osd map.
1634 *
1635 * The message contains any number of incremental and full maps, normally
1636 * indicating some sort of topology change in the cluster. Kick requests
1637 * off to different OSDs as needed.
1638 */
1639void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1640{
1641 void *p, *end, *next;
1642 u32 nr_maps, maplen;
1643 u32 epoch;
1644 struct ceph_osdmap *newmap = NULL, *oldmap;
1645 int err;
1646 struct ceph_fsid fsid;
1647
1648 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1649 p = msg->front.iov_base;
1650 end = p + msg->front.iov_len;
1651
1652 /* verify fsid */
1653 ceph_decode_need(&p, end, sizeof(fsid), bad);
1654 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08001655 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1656 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001657
1658 down_write(&osdc->map_sem);
1659
1660 /* incremental maps */
1661 ceph_decode_32_safe(&p, end, nr_maps, bad);
1662 dout(" %d inc maps\n", nr_maps);
1663 while (nr_maps > 0) {
1664 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001665 epoch = ceph_decode_32(&p);
1666 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001667 ceph_decode_need(&p, end, maplen, bad);
1668 next = p + maplen;
1669 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1670 dout("applying incremental map %u len %d\n",
1671 epoch, maplen);
1672 newmap = osdmap_apply_incremental(&p, next,
1673 osdc->osdmap,
Alex Elder15d98822012-05-26 23:26:43 -05001674 &osdc->client->msgr);
Sage Weilf24e9982009-10-06 11:31:10 -07001675 if (IS_ERR(newmap)) {
1676 err = PTR_ERR(newmap);
1677 goto bad;
1678 }
Sage Weil30dc6382009-12-21 14:49:37 -08001679 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001680 if (newmap != osdc->osdmap) {
1681 ceph_osdmap_destroy(osdc->osdmap);
1682 osdc->osdmap = newmap;
1683 }
Sage Weil38d64532011-10-14 13:33:55 -07001684 kick_requests(osdc, 0);
Sage Weilf24e9982009-10-06 11:31:10 -07001685 } else {
1686 dout("ignoring incremental map %u len %d\n",
1687 epoch, maplen);
1688 }
1689 p = next;
1690 nr_maps--;
1691 }
1692 if (newmap)
1693 goto done;
1694
1695 /* full maps */
1696 ceph_decode_32_safe(&p, end, nr_maps, bad);
1697 dout(" %d full maps\n", nr_maps);
1698 while (nr_maps) {
1699 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001700 epoch = ceph_decode_32(&p);
1701 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001702 ceph_decode_need(&p, end, maplen, bad);
1703 if (nr_maps > 1) {
1704 dout("skipping non-latest full map %u len %d\n",
1705 epoch, maplen);
1706 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1707 dout("skipping full map %u len %d, "
1708 "older than our %u\n", epoch, maplen,
1709 osdc->osdmap->epoch);
1710 } else {
Sage Weil38d64532011-10-14 13:33:55 -07001711 int skipped_map = 0;
1712
Sage Weilf24e9982009-10-06 11:31:10 -07001713 dout("taking full map %u len %d\n", epoch, maplen);
1714 newmap = osdmap_decode(&p, p+maplen);
1715 if (IS_ERR(newmap)) {
1716 err = PTR_ERR(newmap);
1717 goto bad;
1718 }
Sage Weil30dc6382009-12-21 14:49:37 -08001719 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001720 oldmap = osdc->osdmap;
1721 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07001722 if (oldmap) {
1723 if (oldmap->epoch + 1 < newmap->epoch)
1724 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07001725 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07001726 }
1727 kick_requests(osdc, skipped_map);
Sage Weilf24e9982009-10-06 11:31:10 -07001728 }
1729 p += maplen;
1730 nr_maps--;
1731 }
1732
1733done:
1734 downgrade_write(&osdc->map_sem);
1735 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07001736
1737 /*
1738 * subscribe to subsequent osdmap updates if full to ensure
1739 * we find out when we are no longer full and stop returning
1740 * ENOSPC.
1741 */
1742 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1743 ceph_monc_request_next_osdmap(&osdc->client->monc);
1744
Alex Elderf9d25192013-02-15 11:42:29 -06001745 mutex_lock(&osdc->request_mutex);
1746 __send_queued(osdc);
1747 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001748 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001749 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001750 return;
1751
1752bad:
1753 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001754 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001755 up_write(&osdc->map_sem);
1756 return;
1757}
1758
Sage Weilf24e9982009-10-06 11:31:10 -07001759/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001760 * watch/notify callback event infrastructure
1761 *
1762 * These callbacks are used both for watch and notify operations.
1763 */
1764static void __release_event(struct kref *kref)
1765{
1766 struct ceph_osd_event *event =
1767 container_of(kref, struct ceph_osd_event, kref);
1768
1769 dout("__release_event %p\n", event);
1770 kfree(event);
1771}
1772
1773static void get_event(struct ceph_osd_event *event)
1774{
1775 kref_get(&event->kref);
1776}
1777
1778void ceph_osdc_put_event(struct ceph_osd_event *event)
1779{
1780 kref_put(&event->kref, __release_event);
1781}
1782EXPORT_SYMBOL(ceph_osdc_put_event);
1783
1784static void __insert_event(struct ceph_osd_client *osdc,
1785 struct ceph_osd_event *new)
1786{
1787 struct rb_node **p = &osdc->event_tree.rb_node;
1788 struct rb_node *parent = NULL;
1789 struct ceph_osd_event *event = NULL;
1790
1791 while (*p) {
1792 parent = *p;
1793 event = rb_entry(parent, struct ceph_osd_event, node);
1794 if (new->cookie < event->cookie)
1795 p = &(*p)->rb_left;
1796 else if (new->cookie > event->cookie)
1797 p = &(*p)->rb_right;
1798 else
1799 BUG();
1800 }
1801
1802 rb_link_node(&new->node, parent, p);
1803 rb_insert_color(&new->node, &osdc->event_tree);
1804}
1805
1806static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1807 u64 cookie)
1808{
1809 struct rb_node **p = &osdc->event_tree.rb_node;
1810 struct rb_node *parent = NULL;
1811 struct ceph_osd_event *event = NULL;
1812
1813 while (*p) {
1814 parent = *p;
1815 event = rb_entry(parent, struct ceph_osd_event, node);
1816 if (cookie < event->cookie)
1817 p = &(*p)->rb_left;
1818 else if (cookie > event->cookie)
1819 p = &(*p)->rb_right;
1820 else
1821 return event;
1822 }
1823 return NULL;
1824}
1825
1826static void __remove_event(struct ceph_osd_event *event)
1827{
1828 struct ceph_osd_client *osdc = event->osdc;
1829
1830 if (!RB_EMPTY_NODE(&event->node)) {
1831 dout("__remove_event removed %p\n", event);
1832 rb_erase(&event->node, &osdc->event_tree);
1833 ceph_osdc_put_event(event);
1834 } else {
1835 dout("__remove_event didn't remove %p\n", event);
1836 }
1837}
1838
1839int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1840 void (*event_cb)(u64, u64, u8, void *),
Alex Elder3c663bb2013-02-15 11:42:30 -06001841 void *data, struct ceph_osd_event **pevent)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001842{
1843 struct ceph_osd_event *event;
1844
1845 event = kmalloc(sizeof(*event), GFP_NOIO);
1846 if (!event)
1847 return -ENOMEM;
1848
1849 dout("create_event %p\n", event);
1850 event->cb = event_cb;
Alex Elder3c663bb2013-02-15 11:42:30 -06001851 event->one_shot = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001852 event->data = data;
1853 event->osdc = osdc;
1854 INIT_LIST_HEAD(&event->osd_node);
Alex Elder3ee52342012-12-17 12:23:48 -06001855 RB_CLEAR_NODE(&event->node);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001856 kref_init(&event->kref); /* one ref for us */
1857 kref_get(&event->kref); /* one ref for the caller */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001858
1859 spin_lock(&osdc->event_lock);
1860 event->cookie = ++osdc->event_count;
1861 __insert_event(osdc, event);
1862 spin_unlock(&osdc->event_lock);
1863
1864 *pevent = event;
1865 return 0;
1866}
1867EXPORT_SYMBOL(ceph_osdc_create_event);
1868
1869void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1870{
1871 struct ceph_osd_client *osdc = event->osdc;
1872
1873 dout("cancel_event %p\n", event);
1874 spin_lock(&osdc->event_lock);
1875 __remove_event(event);
1876 spin_unlock(&osdc->event_lock);
1877 ceph_osdc_put_event(event); /* caller's */
1878}
1879EXPORT_SYMBOL(ceph_osdc_cancel_event);
1880
1881
1882static void do_event_work(struct work_struct *work)
1883{
1884 struct ceph_osd_event_work *event_work =
1885 container_of(work, struct ceph_osd_event_work, work);
1886 struct ceph_osd_event *event = event_work->event;
1887 u64 ver = event_work->ver;
1888 u64 notify_id = event_work->notify_id;
1889 u8 opcode = event_work->opcode;
1890
1891 dout("do_event_work completing %p\n", event);
1892 event->cb(ver, notify_id, opcode, event->data);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001893 dout("do_event_work completed %p\n", event);
1894 ceph_osdc_put_event(event);
1895 kfree(event_work);
1896}
1897
1898
1899/*
1900 * Process osd watch notifications
1901 */
Alex Elder3c663bb2013-02-15 11:42:30 -06001902static void handle_watch_notify(struct ceph_osd_client *osdc,
1903 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001904{
1905 void *p, *end;
1906 u8 proto_ver;
1907 u64 cookie, ver, notify_id;
1908 u8 opcode;
1909 struct ceph_osd_event *event;
1910 struct ceph_osd_event_work *event_work;
1911
1912 p = msg->front.iov_base;
1913 end = p + msg->front.iov_len;
1914
1915 ceph_decode_8_safe(&p, end, proto_ver, bad);
1916 ceph_decode_8_safe(&p, end, opcode, bad);
1917 ceph_decode_64_safe(&p, end, cookie, bad);
1918 ceph_decode_64_safe(&p, end, ver, bad);
1919 ceph_decode_64_safe(&p, end, notify_id, bad);
1920
1921 spin_lock(&osdc->event_lock);
1922 event = __find_event(osdc, cookie);
1923 if (event) {
Alex Elder3c663bb2013-02-15 11:42:30 -06001924 BUG_ON(event->one_shot);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001925 get_event(event);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001926 }
1927 spin_unlock(&osdc->event_lock);
1928 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1929 cookie, ver, event);
1930 if (event) {
1931 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001932 if (!event_work) {
1933 dout("ERROR: could not allocate event_work\n");
1934 goto done_err;
1935 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01001936 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001937 event_work->event = event;
1938 event_work->ver = ver;
1939 event_work->notify_id = notify_id;
1940 event_work->opcode = opcode;
1941 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1942 dout("WARNING: failed to queue notify event work\n");
1943 goto done_err;
1944 }
1945 }
1946
1947 return;
1948
1949done_err:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001950 ceph_osdc_put_event(event);
1951 return;
1952
1953bad:
1954 pr_err("osdc handle_watch_notify corrupt msg\n");
1955 return;
1956}
1957
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001958/*
Alex Eldere65550f2013-04-05 01:27:12 -05001959 * build new request AND message
1960 *
1961 */
1962void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
1963 struct ceph_snap_context *snapc, u64 snap_id,
1964 struct timespec *mtime)
1965{
1966 struct ceph_msg *msg = req->r_request;
1967 void *p;
1968 size_t msg_size;
1969 int flags = req->r_flags;
1970 u64 data_len;
1971 unsigned int i;
1972
1973 req->r_snapid = snap_id;
1974 req->r_snapc = ceph_get_snap_context(snapc);
1975
1976 /* encode request */
1977 msg->hdr.version = cpu_to_le16(4);
1978
1979 p = msg->front.iov_base;
1980 ceph_encode_32(&p, 1); /* client_inc is always 1 */
1981 req->r_request_osdmap_epoch = p;
1982 p += 4;
1983 req->r_request_flags = p;
1984 p += 4;
1985 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
1986 ceph_encode_timespec(p, mtime);
1987 p += sizeof(struct ceph_timespec);
1988 req->r_request_reassert_version = p;
1989 p += sizeof(struct ceph_eversion); /* will get filled in */
1990
1991 /* oloc */
1992 ceph_encode_8(&p, 4);
1993 ceph_encode_8(&p, 4);
1994 ceph_encode_32(&p, 8 + 4 + 4);
1995 req->r_request_pool = p;
1996 p += 8;
1997 ceph_encode_32(&p, -1); /* preferred */
1998 ceph_encode_32(&p, 0); /* key len */
1999
2000 ceph_encode_8(&p, 1);
2001 req->r_request_pgid = p;
2002 p += 8 + 4;
2003 ceph_encode_32(&p, -1); /* preferred */
2004
2005 /* oid */
2006 ceph_encode_32(&p, req->r_oid_len);
2007 memcpy(p, req->r_oid, req->r_oid_len);
2008 dout("oid '%.*s' len %d\n", req->r_oid_len, req->r_oid, req->r_oid_len);
2009 p += req->r_oid_len;
2010
2011 /* ops--can imply data */
2012 ceph_encode_16(&p, (u16)req->r_num_ops);
2013 data_len = 0;
2014 for (i = 0; i < req->r_num_ops; i++) {
2015 data_len += osd_req_encode_op(req, p, i);
2016 p += sizeof(struct ceph_osd_op);
2017 }
2018
2019 /* snaps */
2020 ceph_encode_64(&p, req->r_snapid);
2021 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2022 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2023 if (req->r_snapc) {
2024 for (i = 0; i < snapc->num_snaps; i++) {
2025 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2026 }
2027 }
2028
2029 req->r_request_attempts = p;
2030 p += 4;
2031
2032 /* data */
2033 if (flags & CEPH_OSD_FLAG_WRITE) {
2034 u16 data_off;
2035
2036 /*
2037 * The header "data_off" is a hint to the receiver
2038 * allowing it to align received data into its
2039 * buffers such that there's no need to re-copy
2040 * it before writing it to disk (direct I/O).
2041 */
2042 data_off = (u16) (off & 0xffff);
2043 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2044 }
2045 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2046
2047 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2048 msg_size = p - msg->front.iov_base;
2049 msg->front.iov_len = msg_size;
2050 msg->hdr.front_len = cpu_to_le32(msg_size);
2051
2052 dout("build_request msg_size was %d\n", (int)msg_size);
2053}
2054EXPORT_SYMBOL(ceph_osdc_build_request);
2055
2056/*
Sage Weilf24e9982009-10-06 11:31:10 -07002057 * Register request, send initial attempt.
2058 */
2059int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2060 struct ceph_osd_request *req,
2061 bool nofail)
2062{
Sage Weilc1ea8822009-10-08 16:55:47 -07002063 int rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07002064
Sage Weilf24e9982009-10-06 11:31:10 -07002065 down_read(&osdc->map_sem);
2066 mutex_lock(&osdc->request_mutex);
Alex Elderdc4b8702013-03-25 18:16:11 -05002067 __register_request(osdc, req);
Alex Elder92451b42013-03-25 18:16:11 -05002068 WARN_ON(req->r_sent);
2069 rc = __map_request(osdc, req, 0);
2070 if (rc < 0) {
2071 if (nofail) {
2072 dout("osdc_start_request failed map, "
2073 " will retry %lld\n", req->r_tid);
2074 rc = 0;
Sage Weil9d6fcb02011-05-12 15:48:16 -07002075 }
Alex Elder92451b42013-03-25 18:16:11 -05002076 goto out_unlock;
Sage Weilf24e9982009-10-06 11:31:10 -07002077 }
Alex Elder92451b42013-03-25 18:16:11 -05002078 if (req->r_osd == NULL) {
2079 dout("send_request %p no up osds in pg\n", req);
2080 ceph_monc_request_next_osdmap(&osdc->client->monc);
2081 } else {
Alex Elder7e2766a2013-03-25 18:16:11 -05002082 __send_queued(osdc);
Alex Elder92451b42013-03-25 18:16:11 -05002083 }
2084 rc = 0;
Dan Carpenter234af26f2011-03-29 06:25:59 +03002085out_unlock:
Sage Weilf24e9982009-10-06 11:31:10 -07002086 mutex_unlock(&osdc->request_mutex);
2087 up_read(&osdc->map_sem);
2088 return rc;
2089}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002090EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002091
2092/*
2093 * wait for a request to complete
2094 */
2095int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2096 struct ceph_osd_request *req)
2097{
2098 int rc;
2099
2100 rc = wait_for_completion_interruptible(&req->r_completion);
2101 if (rc < 0) {
2102 mutex_lock(&osdc->request_mutex);
2103 __cancel_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08002104 __unregister_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07002105 mutex_unlock(&osdc->request_mutex);
Sage Weil25845472011-06-03 09:37:09 -07002106 complete_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08002107 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07002108 return rc;
2109 }
2110
2111 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
2112 return req->r_result;
2113}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002114EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002115
2116/*
2117 * sync - wait for all in-flight requests to flush. avoid starvation.
2118 */
2119void ceph_osdc_sync(struct ceph_osd_client *osdc)
2120{
2121 struct ceph_osd_request *req;
2122 u64 last_tid, next_tid = 0;
2123
2124 mutex_lock(&osdc->request_mutex);
2125 last_tid = osdc->last_tid;
2126 while (1) {
2127 req = __lookup_request_ge(osdc, next_tid);
2128 if (!req)
2129 break;
2130 if (req->r_tid > last_tid)
2131 break;
2132
2133 next_tid = req->r_tid + 1;
2134 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2135 continue;
2136
2137 ceph_osdc_get_request(req);
2138 mutex_unlock(&osdc->request_mutex);
2139 dout("sync waiting on tid %llu (last is %llu)\n",
2140 req->r_tid, last_tid);
2141 wait_for_completion(&req->r_safe_completion);
2142 mutex_lock(&osdc->request_mutex);
2143 ceph_osdc_put_request(req);
2144 }
2145 mutex_unlock(&osdc->request_mutex);
2146 dout("sync done (thru tid %llu)\n", last_tid);
2147}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002148EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07002149
2150/*
2151 * init, shutdown
2152 */
2153int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2154{
2155 int err;
2156
2157 dout("init\n");
2158 osdc->client = client;
2159 osdc->osdmap = NULL;
2160 init_rwsem(&osdc->map_sem);
2161 init_completion(&osdc->map_waiters);
2162 osdc->last_requested_map = 0;
2163 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002164 osdc->last_tid = 0;
2165 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002166 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07002167 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002168 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08002169 INIT_LIST_HEAD(&osdc->req_unsent);
2170 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002171 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07002172 osdc->num_requests = 0;
2173 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002174 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002175 spin_lock_init(&osdc->event_lock);
2176 osdc->event_tree = RB_ROOT;
2177 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002178
2179 schedule_delayed_work(&osdc->osds_timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002180 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07002181
Sage Weil5f44f142009-11-18 14:52:18 -08002182 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07002183 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2184 sizeof(struct ceph_osd_request));
2185 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08002186 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07002187
Sage Weild50b4092012-07-09 14:22:34 -07002188 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2189 OSD_OP_FRONT_LEN, 10, true,
Sage Weil4f482802010-04-24 09:56:35 -07002190 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07002191 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08002192 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07002193 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Sage Weil4f482802010-04-24 09:56:35 -07002194 OSD_OPREPLY_FRONT_LEN, 10, true,
2195 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08002196 if (err < 0)
2197 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002198
2199 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
2200 if (IS_ERR(osdc->notify_wq)) {
2201 err = PTR_ERR(osdc->notify_wq);
2202 osdc->notify_wq = NULL;
2203 goto out_msgpool;
2204 }
Sage Weilf24e9982009-10-06 11:31:10 -07002205 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08002206
Sage Weilc16e7862010-03-01 13:02:00 -08002207out_msgpool:
2208 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08002209out_mempool:
2210 mempool_destroy(osdc->req_mempool);
2211out:
2212 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07002213}
2214
2215void ceph_osdc_stop(struct ceph_osd_client *osdc)
2216{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002217 flush_workqueue(osdc->notify_wq);
2218 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002219 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002220 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07002221 if (osdc->osdmap) {
2222 ceph_osdmap_destroy(osdc->osdmap);
2223 osdc->osdmap = NULL;
2224 }
Sage Weilaca420b2011-08-31 14:45:53 -07002225 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002226 mempool_destroy(osdc->req_mempool);
2227 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08002228 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07002229}
2230
2231/*
2232 * Read some contiguous pages. If we cross a stripe boundary, shorten
2233 * *plen. Return number of bytes read, or error.
2234 */
2235int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2236 struct ceph_vino vino, struct ceph_file_layout *layout,
2237 u64 off, u64 *plen,
2238 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08002239 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07002240{
2241 struct ceph_osd_request *req;
2242 int rc = 0;
2243
2244 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2245 vino.snap, off, *plen);
Alex Elder79528732013-04-03 21:32:51 -05002246 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002247 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
Alex Elderacead002013-03-14 14:09:05 -05002248 NULL, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002249 false);
Sage Weil68162822012-09-24 21:01:02 -07002250 if (IS_ERR(req))
2251 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002252
2253 /* it may be a short read due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06002254
Alex Eldera4ce40a2013-04-05 01:27:12 -05002255 osd_req_op_extent_osd_data_pages(req, 0, false,
2256 pages, *plen, page_align, false, false);
Sage Weilf24e9982009-10-06 11:31:10 -07002257
Alex Eldere0c59482013-03-07 15:38:25 -06002258 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
Alex Elder43bfe5d2013-04-03 01:28:57 -05002259 off, *plen, *plen, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07002260
Alex Elder79528732013-04-03 21:32:51 -05002261 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
Alex Elder02ee07d2013-03-14 14:09:06 -05002262
Sage Weilf24e9982009-10-06 11:31:10 -07002263 rc = ceph_osdc_start_request(osdc, req, false);
2264 if (!rc)
2265 rc = ceph_osdc_wait_request(osdc, req);
2266
2267 ceph_osdc_put_request(req);
2268 dout("readpages result %d\n", rc);
2269 return rc;
2270}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002271EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07002272
2273/*
2274 * do a synchronous write on N pages
2275 */
2276int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2277 struct ceph_file_layout *layout,
2278 struct ceph_snap_context *snapc,
2279 u64 off, u64 len,
2280 u32 truncate_seq, u64 truncate_size,
2281 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06002282 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07002283{
2284 struct ceph_osd_request *req;
2285 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08002286 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07002287
Alex Elderacead002013-03-14 14:09:05 -05002288 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
Alex Elder79528732013-04-03 21:32:51 -05002289 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002290 CEPH_OSD_OP_WRITE,
Alex Elder24808822013-02-15 11:42:29 -06002291 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Alex Elderacead002013-03-14 14:09:05 -05002292 snapc, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002293 true);
Sage Weil68162822012-09-24 21:01:02 -07002294 if (IS_ERR(req))
2295 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002296
2297 /* it may be a short write due to an object boundary */
Alex Eldera4ce40a2013-04-05 01:27:12 -05002298 osd_req_op_extent_osd_data_pages(req, 0, true, pages, len, page_align,
Alex Elder43bfe5d2013-04-03 01:28:57 -05002299 false, false);
2300 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
Sage Weilf24e9982009-10-06 11:31:10 -07002301
Alex Elder79528732013-04-03 21:32:51 -05002302 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
Alex Elder02ee07d2013-03-14 14:09:06 -05002303
Alex Elder87f979d2013-02-15 11:42:29 -06002304 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07002305 if (!rc)
2306 rc = ceph_osdc_wait_request(osdc, req);
2307
2308 ceph_osdc_put_request(req);
2309 if (rc == 0)
2310 rc = len;
2311 dout("writepages result %d\n", rc);
2312 return rc;
2313}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002314EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07002315
2316/*
2317 * handle incoming message
2318 */
2319static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2320{
2321 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01002322 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002323 int type = le16_to_cpu(msg->hdr.type);
2324
2325 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07002326 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01002327 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002328
2329 switch (type) {
2330 case CEPH_MSG_OSD_MAP:
2331 ceph_osdc_handle_map(osdc, msg);
2332 break;
2333 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08002334 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07002335 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002336 case CEPH_MSG_WATCH_NOTIFY:
2337 handle_watch_notify(osdc, msg);
2338 break;
Sage Weilf24e9982009-10-06 11:31:10 -07002339
2340 default:
2341 pr_err("received unknown message type %d %s\n", type,
2342 ceph_msg_type_name(type));
2343 }
Sage Weil4a32f932010-06-13 10:27:53 -07002344out:
Sage Weilf24e9982009-10-06 11:31:10 -07002345 ceph_msg_put(msg);
2346}
2347
Sage Weil5b3a4db2010-02-19 21:43:23 -08002348/*
Sage Weil21b667f2010-03-04 10:22:59 -08002349 * lookup and return message for incoming reply. set up reply message
2350 * pages.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002351 */
2352static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002353 struct ceph_msg_header *hdr,
2354 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002355{
2356 struct ceph_osd *osd = con->private;
2357 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002358 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002359 struct ceph_osd_request *req;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002360 int front = le32_to_cpu(hdr->front_len);
2361 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002362 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002363
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002364 tid = le64_to_cpu(hdr->tid);
2365 mutex_lock(&osdc->request_mutex);
2366 req = __lookup_request(osdc, tid);
2367 if (!req) {
2368 *skip = 1;
2369 m = NULL;
Sage Weil756a16a2012-07-30 16:26:13 -07002370 dout("get_reply unknown tid %llu from osd%d\n", tid,
2371 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002372 goto out;
2373 }
Sage Weilc16e7862010-03-01 13:02:00 -08002374
Alex Elderace6d3a2013-04-01 16:12:14 -05002375 if (req->r_reply->con)
Alex Elder8921d112012-06-01 14:56:43 -05002376 dout("%s revoking msg %p from old con %p\n", __func__,
Alex Elderace6d3a2013-04-01 16:12:14 -05002377 req->r_reply, req->r_reply->con);
2378 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002379
Sage Weilc16e7862010-03-01 13:02:00 -08002380 if (front > req->r_reply->front.iov_len) {
2381 pr_warning("get_reply front %d > preallocated %d\n",
2382 front, (int)req->r_reply->front.iov_len);
Sage Weilb61c2762011-08-09 15:03:46 -07002383 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002384 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002385 goto out;
2386 ceph_msg_put(req->r_reply);
2387 req->r_reply = m;
2388 }
2389 m = ceph_msg_get(req->r_reply);
2390
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002391 if (data_len > 0) {
Alex Eldera4ce40a2013-04-05 01:27:12 -05002392 struct ceph_osd_data *osd_data;
Alex Elder0fff87e2013-02-14 12:16:43 -06002393
Alex Eldera4ce40a2013-04-05 01:27:12 -05002394 /*
2395 * XXX This is assuming there is only one op containing
2396 * XXX page data. Probably OK for reads, but this
2397 * XXX ought to be done more generally.
2398 */
2399 osd_data = osd_req_op_extent_osd_data(req, 0, false);
Alex Elder0fff87e2013-02-14 12:16:43 -06002400 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
Alex Elder0fff87e2013-02-14 12:16:43 -06002401 if (osd_data->pages &&
Alex Eldere0c59482013-03-07 15:38:25 -06002402 unlikely(osd_data->length < data_len)) {
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002403
Alex Eldere0c59482013-03-07 15:38:25 -06002404 pr_warning("tid %lld reply has %d bytes "
2405 "we had only %llu bytes ready\n",
2406 tid, data_len, osd_data->length);
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002407 *skip = 1;
2408 ceph_msg_put(m);
2409 m = NULL;
2410 goto out;
2411 }
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002412 }
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002413 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08002414 *skip = 0;
Sage Weilc16e7862010-03-01 13:02:00 -08002415 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002416
2417out:
2418 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002419 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002420
2421}
2422
2423static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2424 struct ceph_msg_header *hdr,
2425 int *skip)
2426{
2427 struct ceph_osd *osd = con->private;
2428 int type = le16_to_cpu(hdr->type);
2429 int front = le32_to_cpu(hdr->front_len);
2430
Alex Elder1c20f2d2012-06-04 14:43:32 -05002431 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002432 switch (type) {
2433 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002434 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002435 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002436 case CEPH_MSG_OSD_OPREPLY:
2437 return get_reply(con, hdr, skip);
2438 default:
2439 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2440 osd->o_osd);
2441 *skip = 1;
2442 return NULL;
2443 }
Sage Weilf24e9982009-10-06 11:31:10 -07002444}
2445
2446/*
2447 * Wrappers to refcount containing ceph_osd struct
2448 */
2449static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2450{
2451 struct ceph_osd *osd = con->private;
2452 if (get_osd(osd))
2453 return con;
2454 return NULL;
2455}
2456
2457static void put_osd_con(struct ceph_connection *con)
2458{
2459 struct ceph_osd *osd = con->private;
2460 put_osd(osd);
2461}
2462
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002463/*
2464 * authentication
2465 */
Alex Eldera3530df2012-05-16 15:16:39 -05002466/*
2467 * Note: returned pointer is the address of a structure that's
2468 * managed separately. Caller must *not* attempt to free it.
2469 */
2470static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002471 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002472{
2473 struct ceph_osd *o = con->private;
2474 struct ceph_osd_client *osdc = o->o_osdc;
2475 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002476 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002477
Alex Elder74f18692012-05-16 15:16:39 -05002478 if (force_new && auth->authorizer) {
Sage Weil27859f92013-03-25 10:26:14 -07002479 ceph_auth_destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002480 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002481 }
Sage Weil27859f92013-03-25 10:26:14 -07002482 if (!auth->authorizer) {
2483 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2484 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002485 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002486 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07002487 } else {
2488 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07002489 auth);
2490 if (ret)
2491 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002492 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002493 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05002494
Alex Eldera3530df2012-05-16 15:16:39 -05002495 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002496}
2497
2498
2499static int verify_authorizer_reply(struct ceph_connection *con, int len)
2500{
2501 struct ceph_osd *o = con->private;
2502 struct ceph_osd_client *osdc = o->o_osdc;
2503 struct ceph_auth_client *ac = osdc->client->monc.auth;
2504
Sage Weil27859f92013-03-25 10:26:14 -07002505 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002506}
2507
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002508static int invalidate_authorizer(struct ceph_connection *con)
2509{
2510 struct ceph_osd *o = con->private;
2511 struct ceph_osd_client *osdc = o->o_osdc;
2512 struct ceph_auth_client *ac = osdc->client->monc.auth;
2513
Sage Weil27859f92013-03-25 10:26:14 -07002514 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002515 return ceph_monc_validate_auth(&osdc->client->monc);
2516}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002517
Tobias Klauser9e327892010-05-20 10:40:19 +02002518static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07002519 .get = get_osd_con,
2520 .put = put_osd_con,
2521 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002522 .get_authorizer = get_authorizer,
2523 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002524 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07002525 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07002526 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07002527};