libceph: make r_request msg_size calculation clearer
[linux-2.6-block.git] / net / ceph / osd_client.c
CommitLineData
a4ce40a9 1
3d14c5d2 2#include <linux/ceph/ceph_debug.h>
f24e9980 3
3d14c5d2 4#include <linux/module.h>
f24e9980
SW
5#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>
68b4476b
YS
11#ifdef CONFIG_BLOCK
12#include <linux/bio.h>
13#endif
f24e9980 14
3d14c5d2
YS
15#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>
f24e9980 21
c16e7869
SW
22#define OSD_OP_FRONT_LEN 4096
23#define OSD_OPREPLY_FRONT_LEN 512
0d59ab81 24
5522ae0b
AE
25static struct kmem_cache *ceph_osd_request_cache;
26
9e32789f 27static const struct ceph_connection_operations osd_con_ops;
f24e9980 28
f9d25199 29static void __send_queued(struct ceph_osd_client *osdc);
6f6c7006 30static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
a40c4f10
YS
31static void __register_request(struct ceph_osd_client *osdc,
32 struct ceph_osd_request *req);
2cc6128a
ID
33static void __unregister_request(struct ceph_osd_client *osdc,
34 struct ceph_osd_request *req);
a40c4f10
YS
35static void __unregister_linger_request(struct ceph_osd_client *osdc,
36 struct ceph_osd_request *req);
2cc6128a 37static void __enqueue_request(struct ceph_osd_request *req);
56e925b6
SW
38static void __send_request(struct ceph_osd_client *osdc,
39 struct ceph_osd_request *req);
f24e9980
SW
40
41/*
42 * Implement client access to distributed object storage cluster.
43 *
44 * All data objects are stored within a cluster/cloud of OSDs, or
45 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
46 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
47 * remote daemons serving up and coordinating consistent and safe
48 * access to storage.
49 *
50 * Cluster membership and the mapping of data objects onto storage devices
51 * are described by the osd map.
52 *
53 * We keep track of pending OSD requests (read, write), resubmit
54 * requests to different OSDs when the cluster topology/data layout
55 * change, or retry the affected requests when the communications
56 * channel with an OSD is reset.
57 */
58
59/*
60 * calculate the mapping of a file extent onto an object, and fill out the
61 * request accordingly. shorten extent as necessary if it crosses an
62 * object boundary.
63 *
64 * fill osd op in request message.
65 */
dbe0fc41 66static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
a19dadfb 67 u64 *objnum, u64 *objoff, u64 *objlen)
f24e9980 68{
60e56f13 69 u64 orig_len = *plen;
d63b77f4 70 int r;
f24e9980 71
60e56f13 72 /* object extent? */
75d1c941
AE
73 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
74 objoff, objlen);
d63b77f4
SW
75 if (r < 0)
76 return r;
75d1c941
AE
77 if (*objlen < orig_len) {
78 *plen = *objlen;
60e56f13
AE
79 dout(" skipping last %llu, final file extent %llu~%llu\n",
80 orig_len - *plen, off, *plen);
81 }
82
75d1c941 83 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
f24e9980 84
3ff5f385 85 return 0;
f24e9980
SW
86}
87
c54d47bf
AE
88static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
89{
90 memset(osd_data, 0, sizeof (*osd_data));
91 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
92}
93
a4ce40a9 94static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
43bfe5de
AE
95 struct page **pages, u64 length, u32 alignment,
96 bool pages_from_pool, bool own_pages)
97{
98 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
99 osd_data->pages = pages;
100 osd_data->length = length;
101 osd_data->alignment = alignment;
102 osd_data->pages_from_pool = pages_from_pool;
103 osd_data->own_pages = own_pages;
104}
43bfe5de 105
a4ce40a9 106static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
43bfe5de
AE
107 struct ceph_pagelist *pagelist)
108{
109 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
110 osd_data->pagelist = pagelist;
111}
43bfe5de
AE
112
113#ifdef CONFIG_BLOCK
a4ce40a9 114static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
43bfe5de
AE
115 struct bio *bio, size_t bio_length)
116{
117 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
118 osd_data->bio = bio;
119 osd_data->bio_length = bio_length;
120}
43bfe5de
AE
121#endif /* CONFIG_BLOCK */
122
8a703a38
IC
123#define osd_req_op_data(oreq, whch, typ, fld) \
124({ \
125 struct ceph_osd_request *__oreq = (oreq); \
126 unsigned int __whch = (whch); \
127 BUG_ON(__whch >= __oreq->r_num_ops); \
128 &__oreq->r_ops[__whch].typ.fld; \
129})
863c7eb5 130
49719778
AE
131static struct ceph_osd_data *
132osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
133{
134 BUG_ON(which >= osd_req->r_num_ops);
135
136 return &osd_req->r_ops[which].raw_data_in;
137}
138
a4ce40a9
AE
139struct ceph_osd_data *
140osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
406e2c9f 141 unsigned int which)
a4ce40a9 142{
863c7eb5 143 return osd_req_op_data(osd_req, which, extent, osd_data);
a4ce40a9
AE
144}
145EXPORT_SYMBOL(osd_req_op_extent_osd_data);
146
a4ce40a9
AE
147struct ceph_osd_data *
148osd_req_op_cls_response_data(struct ceph_osd_request *osd_req,
149 unsigned int which)
150{
863c7eb5 151 return osd_req_op_data(osd_req, which, cls, response_data);
a4ce40a9
AE
152}
153EXPORT_SYMBOL(osd_req_op_cls_response_data); /* ??? */
154
49719778
AE
155void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
156 unsigned int which, struct page **pages,
157 u64 length, u32 alignment,
158 bool pages_from_pool, bool own_pages)
159{
160 struct ceph_osd_data *osd_data;
161
162 osd_data = osd_req_op_raw_data_in(osd_req, which);
163 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
164 pages_from_pool, own_pages);
165}
166EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
167
a4ce40a9 168void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
406e2c9f
AE
169 unsigned int which, struct page **pages,
170 u64 length, u32 alignment,
a4ce40a9
AE
171 bool pages_from_pool, bool own_pages)
172{
173 struct ceph_osd_data *osd_data;
174
863c7eb5 175 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
a4ce40a9
AE
176 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
177 pages_from_pool, own_pages);
a4ce40a9
AE
178}
179EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
180
181void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
406e2c9f 182 unsigned int which, struct ceph_pagelist *pagelist)
a4ce40a9
AE
183{
184 struct ceph_osd_data *osd_data;
185
863c7eb5 186 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
a4ce40a9 187 ceph_osd_data_pagelist_init(osd_data, pagelist);
a4ce40a9
AE
188}
189EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
190
191#ifdef CONFIG_BLOCK
192void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
406e2c9f 193 unsigned int which, struct bio *bio, size_t bio_length)
a4ce40a9
AE
194{
195 struct ceph_osd_data *osd_data;
863c7eb5
AE
196
197 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
a4ce40a9 198 ceph_osd_data_bio_init(osd_data, bio, bio_length);
a4ce40a9
AE
199}
200EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
201#endif /* CONFIG_BLOCK */
202
203static void osd_req_op_cls_request_info_pagelist(
204 struct ceph_osd_request *osd_req,
205 unsigned int which, struct ceph_pagelist *pagelist)
206{
207 struct ceph_osd_data *osd_data;
208
863c7eb5 209 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
a4ce40a9 210 ceph_osd_data_pagelist_init(osd_data, pagelist);
a4ce40a9
AE
211}
212
04017e29
AE
213void osd_req_op_cls_request_data_pagelist(
214 struct ceph_osd_request *osd_req,
215 unsigned int which, struct ceph_pagelist *pagelist)
216{
217 struct ceph_osd_data *osd_data;
218
863c7eb5 219 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
04017e29
AE
220 ceph_osd_data_pagelist_init(osd_data, pagelist);
221}
222EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
223
6c57b554
AE
224void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
225 unsigned int which, struct page **pages, u64 length,
226 u32 alignment, bool pages_from_pool, bool own_pages)
227{
228 struct ceph_osd_data *osd_data;
229
230 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
231 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
232 pages_from_pool, own_pages);
233}
234EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
235
a4ce40a9
AE
236void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
237 unsigned int which, struct page **pages, u64 length,
238 u32 alignment, bool pages_from_pool, bool own_pages)
239{
240 struct ceph_osd_data *osd_data;
241
863c7eb5 242 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
a4ce40a9
AE
243 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
244 pages_from_pool, own_pages);
a4ce40a9
AE
245}
246EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
247
23c08a9c
AE
248static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
249{
250 switch (osd_data->type) {
251 case CEPH_OSD_DATA_TYPE_NONE:
252 return 0;
253 case CEPH_OSD_DATA_TYPE_PAGES:
254 return osd_data->length;
255 case CEPH_OSD_DATA_TYPE_PAGELIST:
256 return (u64)osd_data->pagelist->length;
257#ifdef CONFIG_BLOCK
258 case CEPH_OSD_DATA_TYPE_BIO:
259 return (u64)osd_data->bio_length;
260#endif /* CONFIG_BLOCK */
261 default:
262 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
263 return 0;
264 }
265}
266
c54d47bf
AE
267static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
268{
5476492f 269 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
c54d47bf
AE
270 int num_pages;
271
272 num_pages = calc_pages_for((u64)osd_data->alignment,
273 (u64)osd_data->length);
274 ceph_release_page_vector(osd_data->pages, num_pages);
275 }
5476492f
AE
276 ceph_osd_data_init(osd_data);
277}
278
279static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
280 unsigned int which)
281{
282 struct ceph_osd_req_op *op;
283
284 BUG_ON(which >= osd_req->r_num_ops);
285 op = &osd_req->r_ops[which];
286
287 switch (op->op) {
288 case CEPH_OSD_OP_READ:
289 case CEPH_OSD_OP_WRITE:
e30b7577 290 case CEPH_OSD_OP_WRITEFULL:
5476492f
AE
291 ceph_osd_data_release(&op->extent.osd_data);
292 break;
293 case CEPH_OSD_OP_CALL:
294 ceph_osd_data_release(&op->cls.request_info);
04017e29 295 ceph_osd_data_release(&op->cls.request_data);
5476492f
AE
296 ceph_osd_data_release(&op->cls.response_data);
297 break;
d74b50be
YZ
298 case CEPH_OSD_OP_SETXATTR:
299 case CEPH_OSD_OP_CMPXATTR:
300 ceph_osd_data_release(&op->xattr.osd_data);
301 break;
66ba609f
YZ
302 case CEPH_OSD_OP_STAT:
303 ceph_osd_data_release(&op->raw_data_in);
304 break;
5476492f
AE
305 default:
306 break;
307 }
c54d47bf
AE
308}
309
f24e9980
SW
310/*
311 * requests
312 */
9e94af20 313static void ceph_osdc_release_request(struct kref *kref)
f24e9980 314{
9e94af20
ID
315 struct ceph_osd_request *req = container_of(kref,
316 struct ceph_osd_request, r_kref);
5476492f 317 unsigned int which;
415e49a9 318
9e94af20
ID
319 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
320 req->r_request, req->r_reply);
6562d661
ID
321 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
322 WARN_ON(!list_empty(&req->r_req_lru_item));
323 WARN_ON(!list_empty(&req->r_osd_item));
324 WARN_ON(!list_empty(&req->r_linger_item));
325 WARN_ON(!list_empty(&req->r_linger_osd_item));
326 WARN_ON(req->r_osd);
9e94af20 327
415e49a9
SW
328 if (req->r_request)
329 ceph_msg_put(req->r_request);
ace6d3a9 330 if (req->r_reply) {
8921d114 331 ceph_msg_revoke_incoming(req->r_reply);
ab8cb34a 332 ceph_msg_put(req->r_reply);
ace6d3a9 333 }
0fff87ec 334
5476492f
AE
335 for (which = 0; which < req->r_num_ops; which++)
336 osd_req_op_data_release(req, which);
0fff87ec 337
415e49a9
SW
338 ceph_put_snap_context(req->r_snapc);
339 if (req->r_mempool)
340 mempool_free(req, req->r_osdc->req_mempool);
341 else
5522ae0b
AE
342 kmem_cache_free(ceph_osd_request_cache, req);
343
f24e9980 344}
9e94af20
ID
345
346void ceph_osdc_get_request(struct ceph_osd_request *req)
347{
348 dout("%s %p (was %d)\n", __func__, req,
349 atomic_read(&req->r_kref.refcount));
350 kref_get(&req->r_kref);
351}
352EXPORT_SYMBOL(ceph_osdc_get_request);
353
354void ceph_osdc_put_request(struct ceph_osd_request *req)
355{
356 dout("%s %p (was %d)\n", __func__, req,
357 atomic_read(&req->r_kref.refcount));
358 kref_put(&req->r_kref, ceph_osdc_release_request);
359}
360EXPORT_SYMBOL(ceph_osdc_put_request);
68b4476b 361
3499e8a5 362struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
f24e9980 363 struct ceph_snap_context *snapc,
1b83bef2 364 unsigned int num_ops,
3499e8a5 365 bool use_mempool,
54a54007 366 gfp_t gfp_flags)
f24e9980
SW
367{
368 struct ceph_osd_request *req;
369 struct ceph_msg *msg;
1b83bef2
SW
370 size_t msg_size;
371
79528734
AE
372 BUILD_BUG_ON(CEPH_OSD_MAX_OP > U16_MAX);
373 BUG_ON(num_ops > CEPH_OSD_MAX_OP);
374
f24e9980 375 if (use_mempool) {
3499e8a5 376 req = mempool_alloc(osdc->req_mempool, gfp_flags);
f24e9980
SW
377 memset(req, 0, sizeof(*req));
378 } else {
5522ae0b 379 req = kmem_cache_zalloc(ceph_osd_request_cache, gfp_flags);
f24e9980
SW
380 }
381 if (req == NULL)
a79832f2 382 return NULL;
f24e9980 383
f24e9980
SW
384 req->r_osdc = osdc;
385 req->r_mempool = use_mempool;
79528734 386 req->r_num_ops = num_ops;
68b4476b 387
415e49a9 388 kref_init(&req->r_kref);
f24e9980
SW
389 init_completion(&req->r_completion);
390 init_completion(&req->r_safe_completion);
a978fa20 391 RB_CLEAR_NODE(&req->r_node);
f24e9980 392 INIT_LIST_HEAD(&req->r_unsafe_item);
a40c4f10 393 INIT_LIST_HEAD(&req->r_linger_item);
1d0326b1 394 INIT_LIST_HEAD(&req->r_linger_osd_item);
935b639a 395 INIT_LIST_HEAD(&req->r_req_lru_item);
cd43045c
SW
396 INIT_LIST_HEAD(&req->r_osd_item);
397
3c972c95 398 req->r_base_oloc.pool = -1;
205ee118 399 req->r_target_oloc.pool = -1;
22116525 400
c16e7869
SW
401 /* create reply message */
402 if (use_mempool)
403 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
404 else
405 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
b61c2763 406 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
a79832f2 407 if (!msg) {
c16e7869 408 ceph_osdc_put_request(req);
a79832f2 409 return NULL;
c16e7869
SW
410 }
411 req->r_reply = msg;
412
ae458f5a
ID
413 msg_size = 4 + 4 + 4; /* client_inc, osdmap_epoch, flags */
414 msg_size += 4 + 4 + 4 + 8; /* mtime, reassert_version */
415 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
416 msg_size += 1 + 8 + 4 + 4; /* pgid */
417 msg_size += 4 + CEPH_MAX_OID_NAME_LEN; /* oid */
418 msg_size += 2 + num_ops * sizeof(struct ceph_osd_op);
419 msg_size += 8; /* snapid */
420 msg_size += 8; /* snap_seq */
421 msg_size += 4 + 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
422 msg_size += 4; /* retry_attempt */
423
c16e7869 424 /* create request message; allow space for oid */
f24e9980 425 if (use_mempool)
8f3bc053 426 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
f24e9980 427 else
b61c2763 428 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
a79832f2 429 if (!msg) {
f24e9980 430 ceph_osdc_put_request(req);
a79832f2 431 return NULL;
f24e9980 432 }
68b4476b 433
f24e9980 434 memset(msg->front.iov_base, 0, msg->front.iov_len);
3499e8a5
YS
435
436 req->r_request = msg;
3499e8a5
YS
437
438 return req;
439}
3d14c5d2 440EXPORT_SYMBOL(ceph_osdc_alloc_request);
3499e8a5 441
a8dd0a37 442static bool osd_req_opcode_valid(u16 opcode)
68b4476b 443{
a8dd0a37 444 switch (opcode) {
70b5bfa3
ID
445#define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
446__CEPH_FORALL_OSD_OPS(GENERATE_CASE)
447#undef GENERATE_CASE
a8dd0a37
AE
448 default:
449 return false;
450 }
451}
452
33803f33
AE
453/*
454 * This is an osd op init function for opcodes that have no data or
455 * other information associated with them. It also serves as a
456 * common init routine for all the other init functions, below.
457 */
c99d2d4a 458static struct ceph_osd_req_op *
49719778 459_osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
144cba14 460 u16 opcode, u32 flags)
33803f33 461{
c99d2d4a
AE
462 struct ceph_osd_req_op *op;
463
464 BUG_ON(which >= osd_req->r_num_ops);
33803f33
AE
465 BUG_ON(!osd_req_opcode_valid(opcode));
466
c99d2d4a 467 op = &osd_req->r_ops[which];
33803f33 468 memset(op, 0, sizeof (*op));
33803f33 469 op->op = opcode;
144cba14 470 op->flags = flags;
c99d2d4a
AE
471
472 return op;
33803f33
AE
473}
474
49719778 475void osd_req_op_init(struct ceph_osd_request *osd_req,
144cba14 476 unsigned int which, u16 opcode, u32 flags)
49719778 477{
144cba14 478 (void)_osd_req_op_init(osd_req, which, opcode, flags);
49719778
AE
479}
480EXPORT_SYMBOL(osd_req_op_init);
481
c99d2d4a
AE
482void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
483 unsigned int which, u16 opcode,
33803f33
AE
484 u64 offset, u64 length,
485 u64 truncate_size, u32 truncate_seq)
486{
144cba14
YZ
487 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
488 opcode, 0);
33803f33
AE
489 size_t payload_len = 0;
490
ad7a60de 491 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
e30b7577
ID
492 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
493 opcode != CEPH_OSD_OP_TRUNCATE);
33803f33 494
33803f33
AE
495 op->extent.offset = offset;
496 op->extent.length = length;
497 op->extent.truncate_size = truncate_size;
498 op->extent.truncate_seq = truncate_seq;
e30b7577 499 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
33803f33
AE
500 payload_len += length;
501
de2aa102 502 op->indata_len = payload_len;
33803f33
AE
503}
504EXPORT_SYMBOL(osd_req_op_extent_init);
505
c99d2d4a
AE
506void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
507 unsigned int which, u64 length)
e5975c7c 508{
c99d2d4a
AE
509 struct ceph_osd_req_op *op;
510 u64 previous;
511
512 BUG_ON(which >= osd_req->r_num_ops);
513 op = &osd_req->r_ops[which];
514 previous = op->extent.length;
e5975c7c
AE
515
516 if (length == previous)
517 return; /* Nothing to do */
518 BUG_ON(length > previous);
519
520 op->extent.length = length;
de2aa102 521 op->indata_len -= previous - length;
e5975c7c
AE
522}
523EXPORT_SYMBOL(osd_req_op_extent_update);
524
c99d2d4a 525void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
04017e29 526 u16 opcode, const char *class, const char *method)
33803f33 527{
144cba14
YZ
528 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
529 opcode, 0);
5f562df5 530 struct ceph_pagelist *pagelist;
33803f33
AE
531 size_t payload_len = 0;
532 size_t size;
533
534 BUG_ON(opcode != CEPH_OSD_OP_CALL);
535
5f562df5
AE
536 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
537 BUG_ON(!pagelist);
538 ceph_pagelist_init(pagelist);
539
33803f33
AE
540 op->cls.class_name = class;
541 size = strlen(class);
542 BUG_ON(size > (size_t) U8_MAX);
543 op->cls.class_len = size;
5f562df5 544 ceph_pagelist_append(pagelist, class, size);
33803f33
AE
545 payload_len += size;
546
547 op->cls.method_name = method;
548 size = strlen(method);
549 BUG_ON(size > (size_t) U8_MAX);
550 op->cls.method_len = size;
5f562df5 551 ceph_pagelist_append(pagelist, method, size);
33803f33
AE
552 payload_len += size;
553
a4ce40a9 554 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
5f562df5 555
33803f33
AE
556 op->cls.argc = 0; /* currently unused */
557
de2aa102 558 op->indata_len = payload_len;
33803f33
AE
559}
560EXPORT_SYMBOL(osd_req_op_cls_init);
8c042b0d 561
d74b50be
YZ
562int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
563 u16 opcode, const char *name, const void *value,
564 size_t size, u8 cmp_op, u8 cmp_mode)
565{
144cba14
YZ
566 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
567 opcode, 0);
d74b50be
YZ
568 struct ceph_pagelist *pagelist;
569 size_t payload_len;
570
571 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
572
573 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
574 if (!pagelist)
575 return -ENOMEM;
576
577 ceph_pagelist_init(pagelist);
578
579 payload_len = strlen(name);
580 op->xattr.name_len = payload_len;
581 ceph_pagelist_append(pagelist, name, payload_len);
582
583 op->xattr.value_len = size;
584 ceph_pagelist_append(pagelist, value, size);
585 payload_len += size;
586
587 op->xattr.cmp_op = cmp_op;
588 op->xattr.cmp_mode = cmp_mode;
589
590 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
de2aa102 591 op->indata_len = payload_len;
d74b50be
YZ
592 return 0;
593}
594EXPORT_SYMBOL(osd_req_op_xattr_init);
595
c99d2d4a
AE
596void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
597 unsigned int which, u16 opcode,
33803f33
AE
598 u64 cookie, u64 version, int flag)
599{
144cba14
YZ
600 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
601 opcode, 0);
33803f33 602
c99d2d4a 603 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
33803f33
AE
604
605 op->watch.cookie = cookie;
9ef1ee5a 606 op->watch.ver = version;
33803f33 607 if (opcode == CEPH_OSD_OP_WATCH && flag)
c99d2d4a 608 op->watch.flag = (u8)1;
33803f33
AE
609}
610EXPORT_SYMBOL(osd_req_op_watch_init);
611
c647b8a8
ID
612void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
613 unsigned int which,
614 u64 expected_object_size,
615 u64 expected_write_size)
616{
617 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
144cba14
YZ
618 CEPH_OSD_OP_SETALLOCHINT,
619 0);
c647b8a8
ID
620
621 op->alloc_hint.expected_object_size = expected_object_size;
622 op->alloc_hint.expected_write_size = expected_write_size;
623
624 /*
625 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
626 * not worth a feature bit. Set FAILOK per-op flag to make
627 * sure older osds don't trip over an unsupported opcode.
628 */
629 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
630}
631EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
632
90af3602 633static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
ec9123c5
AE
634 struct ceph_osd_data *osd_data)
635{
636 u64 length = ceph_osd_data_length(osd_data);
637
638 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
639 BUG_ON(length > (u64) SIZE_MAX);
640 if (length)
90af3602 641 ceph_msg_data_add_pages(msg, osd_data->pages,
ec9123c5
AE
642 length, osd_data->alignment);
643 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
644 BUG_ON(!length);
90af3602 645 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
ec9123c5
AE
646#ifdef CONFIG_BLOCK
647 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
90af3602 648 ceph_msg_data_add_bio(msg, osd_data->bio, length);
ec9123c5
AE
649#endif
650 } else {
651 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
652 }
653}
654
a8dd0a37 655static u64 osd_req_encode_op(struct ceph_osd_request *req,
79528734 656 struct ceph_osd_op *dst, unsigned int which)
a8dd0a37 657{
79528734 658 struct ceph_osd_req_op *src;
04017e29 659 struct ceph_osd_data *osd_data;
54d50649 660 u64 request_data_len = 0;
04017e29 661 u64 data_length;
a8dd0a37 662
79528734
AE
663 BUG_ON(which >= req->r_num_ops);
664 src = &req->r_ops[which];
a8dd0a37
AE
665 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
666 pr_err("unrecognized osd opcode %d\n", src->op);
667
668 return 0;
669 }
670
671 switch (src->op) {
672 case CEPH_OSD_OP_STAT:
49719778
AE
673 osd_data = &src->raw_data_in;
674 ceph_osdc_msg_data_add(req->r_reply, osd_data);
a8dd0a37
AE
675 break;
676 case CEPH_OSD_OP_READ:
677 case CEPH_OSD_OP_WRITE:
e30b7577 678 case CEPH_OSD_OP_WRITEFULL:
ad7a60de 679 case CEPH_OSD_OP_ZERO:
ad7a60de 680 case CEPH_OSD_OP_TRUNCATE:
e30b7577
ID
681 if (src->op == CEPH_OSD_OP_WRITE ||
682 src->op == CEPH_OSD_OP_WRITEFULL)
54d50649 683 request_data_len = src->extent.length;
a8dd0a37
AE
684 dst->extent.offset = cpu_to_le64(src->extent.offset);
685 dst->extent.length = cpu_to_le64(src->extent.length);
686 dst->extent.truncate_size =
687 cpu_to_le64(src->extent.truncate_size);
688 dst->extent.truncate_seq =
689 cpu_to_le32(src->extent.truncate_seq);
04017e29 690 osd_data = &src->extent.osd_data;
e30b7577
ID
691 if (src->op == CEPH_OSD_OP_WRITE ||
692 src->op == CEPH_OSD_OP_WRITEFULL)
04017e29 693 ceph_osdc_msg_data_add(req->r_request, osd_data);
5476492f 694 else
04017e29 695 ceph_osdc_msg_data_add(req->r_reply, osd_data);
a8dd0a37
AE
696 break;
697 case CEPH_OSD_OP_CALL:
a8dd0a37
AE
698 dst->cls.class_len = src->cls.class_len;
699 dst->cls.method_len = src->cls.method_len;
04017e29
AE
700 osd_data = &src->cls.request_info;
701 ceph_osdc_msg_data_add(req->r_request, osd_data);
702 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
703 request_data_len = osd_data->pagelist->length;
704
705 osd_data = &src->cls.request_data;
706 data_length = ceph_osd_data_length(osd_data);
707 if (data_length) {
708 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
709 dst->cls.indata_len = cpu_to_le32(data_length);
710 ceph_osdc_msg_data_add(req->r_request, osd_data);
de2aa102 711 src->indata_len += data_length;
04017e29
AE
712 request_data_len += data_length;
713 }
714 osd_data = &src->cls.response_data;
715 ceph_osdc_msg_data_add(req->r_reply, osd_data);
a8dd0a37
AE
716 break;
717 case CEPH_OSD_OP_STARTSYNC:
718 break;
719 case CEPH_OSD_OP_NOTIFY_ACK:
720 case CEPH_OSD_OP_WATCH:
721 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
722 dst->watch.ver = cpu_to_le64(src->watch.ver);
723 dst->watch.flag = src->watch.flag;
724 break;
c647b8a8
ID
725 case CEPH_OSD_OP_SETALLOCHINT:
726 dst->alloc_hint.expected_object_size =
727 cpu_to_le64(src->alloc_hint.expected_object_size);
728 dst->alloc_hint.expected_write_size =
729 cpu_to_le64(src->alloc_hint.expected_write_size);
730 break;
d74b50be
YZ
731 case CEPH_OSD_OP_SETXATTR:
732 case CEPH_OSD_OP_CMPXATTR:
733 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
734 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
735 dst->xattr.cmp_op = src->xattr.cmp_op;
736 dst->xattr.cmp_mode = src->xattr.cmp_mode;
737 osd_data = &src->xattr.osd_data;
738 ceph_osdc_msg_data_add(req->r_request, osd_data);
739 request_data_len = osd_data->pagelist->length;
740 break;
864e9197
YZ
741 case CEPH_OSD_OP_CREATE:
742 case CEPH_OSD_OP_DELETE:
743 break;
a8dd0a37 744 default:
4c46459c 745 pr_err("unsupported osd opcode %s\n",
8f63ca2d 746 ceph_osd_op_name(src->op));
4c46459c 747 WARN_ON(1);
a8dd0a37
AE
748
749 return 0;
68b4476b 750 }
7b25bf5f 751
a8dd0a37 752 dst->op = cpu_to_le16(src->op);
7b25bf5f 753 dst->flags = cpu_to_le32(src->flags);
de2aa102 754 dst->payload_len = cpu_to_le32(src->indata_len);
175face2 755
54d50649 756 return request_data_len;
68b4476b
YS
757}
758
3499e8a5
YS
759/*
760 * build new request AND message, calculate layout, and adjust file
761 * extent as needed.
762 *
763 * if the file was recently truncated, we include information about its
764 * old and new size so that the object can be updated appropriately. (we
765 * avoid synchronously deleting truncated objects because it's slow.)
766 *
767 * if @do_sync, include a 'startsync' command so that the osd will flush
768 * data quickly.
769 */
770struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
771 struct ceph_file_layout *layout,
772 struct ceph_vino vino,
715e4cd4
YZ
773 u64 off, u64 *plen,
774 unsigned int which, int num_ops,
3499e8a5
YS
775 int opcode, int flags,
776 struct ceph_snap_context *snapc,
3499e8a5
YS
777 u32 truncate_seq,
778 u64 truncate_size,
153e5167 779 bool use_mempool)
3499e8a5 780{
68b4476b 781 struct ceph_osd_request *req;
75d1c941
AE
782 u64 objnum = 0;
783 u64 objoff = 0;
784 u64 objlen = 0;
6816282d 785 int r;
68b4476b 786
ad7a60de 787 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
864e9197
YZ
788 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
789 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
68b4476b 790
acead002 791 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
ae7ca4a3 792 GFP_NOFS);
4ad12621 793 if (!req)
6816282d 794 return ERR_PTR(-ENOMEM);
79528734 795
d178a9e7 796 req->r_flags = flags;
3499e8a5
YS
797
798 /* calculate max write size */
a19dadfb 799 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
3ff5f385
AE
800 if (r < 0) {
801 ceph_osdc_put_request(req);
6816282d 802 return ERR_PTR(r);
3ff5f385 803 }
a19dadfb 804
864e9197 805 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
144cba14 806 osd_req_op_init(req, which, opcode, 0);
864e9197
YZ
807 } else {
808 u32 object_size = le32_to_cpu(layout->fl_object_size);
809 u32 object_base = off - objoff;
810 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
811 if (truncate_size <= object_base) {
812 truncate_size = 0;
813 } else {
814 truncate_size -= object_base;
815 if (truncate_size > object_size)
816 truncate_size = object_size;
817 }
ccca4e37 818 }
715e4cd4 819 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
864e9197
YZ
820 truncate_size, truncate_seq);
821 }
d18d1e28 822
3c972c95 823 req->r_base_oloc.pool = ceph_file_layout_pg_pool(*layout);
3499e8a5 824
3c972c95 825 snprintf(req->r_base_oid.name, sizeof(req->r_base_oid.name),
4295f221 826 "%llx.%08llx", vino.ino, objnum);
3c972c95 827 req->r_base_oid.name_len = strlen(req->r_base_oid.name);
dbe0fc41 828
f24e9980
SW
829 return req;
830}
3d14c5d2 831EXPORT_SYMBOL(ceph_osdc_new_request);
f24e9980
SW
832
833/*
834 * We keep osd requests in an rbtree, sorted by ->r_tid.
835 */
836static void __insert_request(struct ceph_osd_client *osdc,
837 struct ceph_osd_request *new)
838{
839 struct rb_node **p = &osdc->requests.rb_node;
840 struct rb_node *parent = NULL;
841 struct ceph_osd_request *req = NULL;
842
843 while (*p) {
844 parent = *p;
845 req = rb_entry(parent, struct ceph_osd_request, r_node);
846 if (new->r_tid < req->r_tid)
847 p = &(*p)->rb_left;
848 else if (new->r_tid > req->r_tid)
849 p = &(*p)->rb_right;
850 else
851 BUG();
852 }
853
854 rb_link_node(&new->r_node, parent, p);
855 rb_insert_color(&new->r_node, &osdc->requests);
856}
857
858static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
859 u64 tid)
860{
861 struct ceph_osd_request *req;
862 struct rb_node *n = osdc->requests.rb_node;
863
864 while (n) {
865 req = rb_entry(n, struct ceph_osd_request, r_node);
866 if (tid < req->r_tid)
867 n = n->rb_left;
868 else if (tid > req->r_tid)
869 n = n->rb_right;
870 else
871 return req;
872 }
873 return NULL;
874}
875
876static struct ceph_osd_request *
877__lookup_request_ge(struct ceph_osd_client *osdc,
878 u64 tid)
879{
880 struct ceph_osd_request *req;
881 struct rb_node *n = osdc->requests.rb_node;
882
883 while (n) {
884 req = rb_entry(n, struct ceph_osd_request, r_node);
885 if (tid < req->r_tid) {
886 if (!n->rb_left)
887 return req;
888 n = n->rb_left;
889 } else if (tid > req->r_tid) {
890 n = n->rb_right;
891 } else {
892 return req;
893 }
894 }
895 return NULL;
896}
897
2cc6128a
ID
898static void __kick_linger_request(struct ceph_osd_request *req)
899{
900 struct ceph_osd_client *osdc = req->r_osdc;
901 struct ceph_osd *osd = req->r_osd;
902
903 /*
904 * Linger requests need to be resent with a new tid to avoid
905 * the dup op detection logic on the OSDs. Achieve this with
906 * a re-register dance instead of open-coding.
907 */
908 ceph_osdc_get_request(req);
909 if (!list_empty(&req->r_linger_item))
910 __unregister_linger_request(osdc, req);
911 else
912 __unregister_request(osdc, req);
913 __register_request(osdc, req);
914 ceph_osdc_put_request(req);
915
916 /*
917 * Unless request has been registered as both normal and
918 * lingering, __unregister{,_linger}_request clears r_osd.
919 * However, here we need to preserve r_osd to make sure we
920 * requeue on the same OSD.
921 */
922 WARN_ON(req->r_osd || !osd);
923 req->r_osd = osd;
924
925 dout("%s requeueing %p tid %llu\n", __func__, req, req->r_tid);
926 __enqueue_request(req);
927}
928
6f6c7006
SW
929/*
930 * Resubmit requests pending on the given osd.
931 */
932static void __kick_osd_requests(struct ceph_osd_client *osdc,
933 struct ceph_osd *osd)
934{
a40c4f10 935 struct ceph_osd_request *req, *nreq;
e02493c0 936 LIST_HEAD(resend);
2cc6128a 937 LIST_HEAD(resend_linger);
6f6c7006
SW
938 int err;
939
2cc6128a 940 dout("%s osd%d\n", __func__, osd->o_osd);
6f6c7006 941 err = __reset_osd(osdc, osd);
685a7555 942 if (err)
6f6c7006 943 return;
2cc6128a 944
e02493c0
AE
945 /*
946 * Build up a list of requests to resend by traversing the
947 * osd's list of requests. Requests for a given object are
948 * sent in tid order, and that is also the order they're
949 * kept on this list. Therefore all requests that are in
950 * flight will be found first, followed by all requests that
951 * have not yet been sent. And to resend requests while
952 * preserving this order we will want to put any sent
953 * requests back on the front of the osd client's unsent
954 * list.
955 *
956 * So we build a separate ordered list of already-sent
957 * requests for the affected osd and splice it onto the
958 * front of the osd client's unsent list. Once we've seen a
959 * request that has not yet been sent we're done. Those
960 * requests are already sitting right where they belong.
961 */
6f6c7006 962 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
e02493c0
AE
963 if (!req->r_sent)
964 break;
2cc6128a
ID
965
966 if (!req->r_linger) {
967 dout("%s requeueing %p tid %llu\n", __func__, req,
968 req->r_tid);
969 list_move_tail(&req->r_req_lru_item, &resend);
a40c4f10 970 req->r_flags |= CEPH_OSD_FLAG_RETRY;
2cc6128a
ID
971 } else {
972 list_move_tail(&req->r_req_lru_item, &resend_linger);
973 }
a40c4f10 974 }
e02493c0 975 list_splice(&resend, &osdc->req_unsent);
a40c4f10 976
e02493c0 977 /*
2cc6128a
ID
978 * Both registered and not yet registered linger requests are
979 * enqueued with a new tid on the same OSD. We add/move them
980 * to req_unsent/o_requests at the end to keep things in tid
981 * order.
e02493c0 982 */
a40c4f10 983 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
1d0326b1 984 r_linger_osd_item) {
2cc6128a
ID
985 WARN_ON(!list_empty(&req->r_req_lru_item));
986 __kick_linger_request(req);
6f6c7006 987 }
2cc6128a
ID
988
989 list_for_each_entry_safe(req, nreq, &resend_linger, r_req_lru_item)
990 __kick_linger_request(req);
6f6c7006
SW
991}
992
f24e9980 993/*
81b024e7 994 * If the osd connection drops, we need to resubmit all requests.
f24e9980
SW
995 */
996static void osd_reset(struct ceph_connection *con)
997{
998 struct ceph_osd *osd = con->private;
999 struct ceph_osd_client *osdc;
1000
1001 if (!osd)
1002 return;
1003 dout("osd_reset osd%d\n", osd->o_osd);
1004 osdc = osd->o_osdc;
f24e9980 1005 down_read(&osdc->map_sem);
83aff95e
SW
1006 mutex_lock(&osdc->request_mutex);
1007 __kick_osd_requests(osdc, osd);
f9d25199 1008 __send_queued(osdc);
83aff95e 1009 mutex_unlock(&osdc->request_mutex);
f24e9980
SW
1010 up_read(&osdc->map_sem);
1011}
1012
1013/*
1014 * Track open sessions with osds.
1015 */
e10006f8 1016static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
f24e9980
SW
1017{
1018 struct ceph_osd *osd;
1019
1020 osd = kzalloc(sizeof(*osd), GFP_NOFS);
1021 if (!osd)
1022 return NULL;
1023
1024 atomic_set(&osd->o_ref, 1);
1025 osd->o_osdc = osdc;
e10006f8 1026 osd->o_osd = onum;
f407731d 1027 RB_CLEAR_NODE(&osd->o_node);
f24e9980 1028 INIT_LIST_HEAD(&osd->o_requests);
a40c4f10 1029 INIT_LIST_HEAD(&osd->o_linger_requests);
f5a2041b 1030 INIT_LIST_HEAD(&osd->o_osd_lru);
f24e9980
SW
1031 osd->o_incarnation = 1;
1032
b7a9e5dd 1033 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
4e7a5dcd 1034
422d2cb8 1035 INIT_LIST_HEAD(&osd->o_keepalive_item);
f24e9980
SW
1036 return osd;
1037}
1038
1039static struct ceph_osd *get_osd(struct ceph_osd *osd)
1040{
1041 if (atomic_inc_not_zero(&osd->o_ref)) {
1042 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
1043 atomic_read(&osd->o_ref));
1044 return osd;
1045 } else {
1046 dout("get_osd %p FAIL\n", osd);
1047 return NULL;
1048 }
1049}
1050
1051static void put_osd(struct ceph_osd *osd)
1052{
1053 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
1054 atomic_read(&osd->o_ref) - 1);
b28ec2f3 1055 if (atomic_dec_and_test(&osd->o_ref)) {
79494d1b
SW
1056 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
1057
b28ec2f3
ID
1058 if (osd->o_auth.authorizer)
1059 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
f24e9980 1060 kfree(osd);
79494d1b 1061 }
f24e9980
SW
1062}
1063
1064/*
1065 * remove an osd from our map
1066 */
f5a2041b 1067static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
f24e9980 1068{
7eb71e03 1069 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
cc9f1f51
ID
1070 WARN_ON(!list_empty(&osd->o_requests));
1071 WARN_ON(!list_empty(&osd->o_linger_requests));
7c6e6fc5 1072
f5a2041b 1073 list_del_init(&osd->o_osd_lru);
7eb71e03
ID
1074 rb_erase(&osd->o_node, &osdc->osds);
1075 RB_CLEAR_NODE(&osd->o_node);
1076}
1077
1078static void remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1079{
1080 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
1081
1082 if (!RB_EMPTY_NODE(&osd->o_node)) {
1083 ceph_con_close(&osd->o_con);
1084 __remove_osd(osdc, osd);
1085 put_osd(osd);
1086 }
f24e9980
SW
1087}
1088
aca420bc
SW
1089static void remove_all_osds(struct ceph_osd_client *osdc)
1090{
048a9d2d 1091 dout("%s %p\n", __func__, osdc);
aca420bc
SW
1092 mutex_lock(&osdc->request_mutex);
1093 while (!RB_EMPTY_ROOT(&osdc->osds)) {
1094 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
1095 struct ceph_osd, o_node);
7eb71e03 1096 remove_osd(osdc, osd);
aca420bc
SW
1097 }
1098 mutex_unlock(&osdc->request_mutex);
1099}
1100
f5a2041b
YS
1101static void __move_osd_to_lru(struct ceph_osd_client *osdc,
1102 struct ceph_osd *osd)
1103{
bbf37ec3 1104 dout("%s %p\n", __func__, osd);
f5a2041b 1105 BUG_ON(!list_empty(&osd->o_osd_lru));
bbf37ec3 1106
f5a2041b 1107 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
a319bf56 1108 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
f5a2041b
YS
1109}
1110
bbf37ec3
ID
1111static void maybe_move_osd_to_lru(struct ceph_osd_client *osdc,
1112 struct ceph_osd *osd)
1113{
1114 dout("%s %p\n", __func__, osd);
1115
1116 if (list_empty(&osd->o_requests) &&
1117 list_empty(&osd->o_linger_requests))
1118 __move_osd_to_lru(osdc, osd);
1119}
1120
f5a2041b
YS
1121static void __remove_osd_from_lru(struct ceph_osd *osd)
1122{
1123 dout("__remove_osd_from_lru %p\n", osd);
1124 if (!list_empty(&osd->o_osd_lru))
1125 list_del_init(&osd->o_osd_lru);
1126}
1127
aca420bc 1128static void remove_old_osds(struct ceph_osd_client *osdc)
f5a2041b
YS
1129{
1130 struct ceph_osd *osd, *nosd;
1131
1132 dout("__remove_old_osds %p\n", osdc);
1133 mutex_lock(&osdc->request_mutex);
1134 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
aca420bc 1135 if (time_before(jiffies, osd->lru_ttl))
f5a2041b 1136 break;
7eb71e03 1137 remove_osd(osdc, osd);
f5a2041b
YS
1138 }
1139 mutex_unlock(&osdc->request_mutex);
1140}
1141
f24e9980
SW
1142/*
1143 * reset osd connect
1144 */
f5a2041b 1145static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
f24e9980 1146{
c3acb181 1147 struct ceph_entity_addr *peer_addr;
f24e9980 1148
f5a2041b 1149 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
a40c4f10
YS
1150 if (list_empty(&osd->o_requests) &&
1151 list_empty(&osd->o_linger_requests)) {
7eb71e03 1152 remove_osd(osdc, osd);
c3acb181
AE
1153 return -ENODEV;
1154 }
1155
1156 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1157 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1158 !ceph_con_opened(&osd->o_con)) {
1159 struct ceph_osd_request *req;
1160
0b4af2e8
ID
1161 dout("osd addr hasn't changed and connection never opened, "
1162 "letting msgr retry\n");
87b315a5
SW
1163 /* touch each r_stamp for handle_timeout()'s benfit */
1164 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1165 req->r_stamp = jiffies;
c3acb181
AE
1166
1167 return -EAGAIN;
f24e9980 1168 }
c3acb181
AE
1169
1170 ceph_con_close(&osd->o_con);
1171 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1172 osd->o_incarnation++;
1173
1174 return 0;
f24e9980
SW
1175}
1176
1177static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1178{
1179 struct rb_node **p = &osdc->osds.rb_node;
1180 struct rb_node *parent = NULL;
1181 struct ceph_osd *osd = NULL;
1182
aca420bc 1183 dout("__insert_osd %p osd%d\n", new, new->o_osd);
f24e9980
SW
1184 while (*p) {
1185 parent = *p;
1186 osd = rb_entry(parent, struct ceph_osd, o_node);
1187 if (new->o_osd < osd->o_osd)
1188 p = &(*p)->rb_left;
1189 else if (new->o_osd > osd->o_osd)
1190 p = &(*p)->rb_right;
1191 else
1192 BUG();
1193 }
1194
1195 rb_link_node(&new->o_node, parent, p);
1196 rb_insert_color(&new->o_node, &osdc->osds);
1197}
1198
1199static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1200{
1201 struct ceph_osd *osd;
1202 struct rb_node *n = osdc->osds.rb_node;
1203
1204 while (n) {
1205 osd = rb_entry(n, struct ceph_osd, o_node);
1206 if (o < osd->o_osd)
1207 n = n->rb_left;
1208 else if (o > osd->o_osd)
1209 n = n->rb_right;
1210 else
1211 return osd;
1212 }
1213 return NULL;
1214}
1215
422d2cb8
YS
1216static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1217{
1218 schedule_delayed_work(&osdc->timeout_work,
a319bf56 1219 osdc->client->options->osd_keepalive_timeout);
422d2cb8
YS
1220}
1221
1222static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1223{
1224 cancel_delayed_work(&osdc->timeout_work);
1225}
f24e9980
SW
1226
1227/*
1228 * Register request, assign tid. If this is the first request, set up
1229 * the timeout event.
1230 */
a40c4f10
YS
1231static void __register_request(struct ceph_osd_client *osdc,
1232 struct ceph_osd_request *req)
f24e9980 1233{
f24e9980 1234 req->r_tid = ++osdc->last_tid;
6df058c0 1235 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
77f38e0e 1236 dout("__register_request %p tid %lld\n", req, req->r_tid);
f24e9980
SW
1237 __insert_request(osdc, req);
1238 ceph_osdc_get_request(req);
1239 osdc->num_requests++;
f24e9980 1240 if (osdc->num_requests == 1) {
422d2cb8
YS
1241 dout(" first request, scheduling timeout\n");
1242 __schedule_osd_timeout(osdc);
f24e9980 1243 }
a40c4f10
YS
1244}
1245
f24e9980
SW
1246/*
1247 * called under osdc->request_mutex
1248 */
1249static void __unregister_request(struct ceph_osd_client *osdc,
1250 struct ceph_osd_request *req)
1251{
35f9f8a0
SW
1252 if (RB_EMPTY_NODE(&req->r_node)) {
1253 dout("__unregister_request %p tid %lld not registered\n",
1254 req, req->r_tid);
1255 return;
1256 }
1257
f24e9980
SW
1258 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1259 rb_erase(&req->r_node, &osdc->requests);
6562d661 1260 RB_CLEAR_NODE(&req->r_node);
f24e9980
SW
1261 osdc->num_requests--;
1262
0ba6478d
SW
1263 if (req->r_osd) {
1264 /* make sure the original request isn't in flight. */
6740a845 1265 ceph_msg_revoke(req->r_request);
0ba6478d
SW
1266
1267 list_del_init(&req->r_osd_item);
bbf37ec3 1268 maybe_move_osd_to_lru(osdc, req->r_osd);
4f23409e 1269 if (list_empty(&req->r_linger_osd_item))
a40c4f10 1270 req->r_osd = NULL;
0ba6478d 1271 }
f24e9980 1272
7d5f2481 1273 list_del_init(&req->r_req_lru_item);
f24e9980
SW
1274 ceph_osdc_put_request(req);
1275
422d2cb8
YS
1276 if (osdc->num_requests == 0) {
1277 dout(" no requests, canceling timeout\n");
1278 __cancel_osd_timeout(osdc);
f24e9980
SW
1279 }
1280}
1281
1282/*
1283 * Cancel a previously queued request message
1284 */
1285static void __cancel_request(struct ceph_osd_request *req)
1286{
6bc18876 1287 if (req->r_sent && req->r_osd) {
6740a845 1288 ceph_msg_revoke(req->r_request);
f24e9980
SW
1289 req->r_sent = 0;
1290 }
1291}
1292
a40c4f10
YS
1293static void __register_linger_request(struct ceph_osd_client *osdc,
1294 struct ceph_osd_request *req)
1295{
af593064
ID
1296 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1297 WARN_ON(!req->r_linger);
1298
96e4dac6 1299 ceph_osdc_get_request(req);
a40c4f10 1300 list_add_tail(&req->r_linger_item, &osdc->req_linger);
6194ea89 1301 if (req->r_osd)
1d0326b1 1302 list_add_tail(&req->r_linger_osd_item,
6194ea89 1303 &req->r_osd->o_linger_requests);
a40c4f10
YS
1304}
1305
1306static void __unregister_linger_request(struct ceph_osd_client *osdc,
1307 struct ceph_osd_request *req)
1308{
af593064
ID
1309 WARN_ON(!req->r_linger);
1310
1311 if (list_empty(&req->r_linger_item)) {
1312 dout("%s %p tid %llu not registered\n", __func__, req,
1313 req->r_tid);
1314 return;
1315 }
1316
1317 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
61c74035 1318 list_del_init(&req->r_linger_item);
af593064 1319
a40c4f10 1320 if (req->r_osd) {
1d0326b1 1321 list_del_init(&req->r_linger_osd_item);
bbf37ec3 1322 maybe_move_osd_to_lru(osdc, req->r_osd);
fbdb9190
SW
1323 if (list_empty(&req->r_osd_item))
1324 req->r_osd = NULL;
a40c4f10 1325 }
96e4dac6 1326 ceph_osdc_put_request(req);
a40c4f10
YS
1327}
1328
a40c4f10
YS
1329void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1330 struct ceph_osd_request *req)
1331{
1332 if (!req->r_linger) {
1333 dout("set_request_linger %p\n", req);
1334 req->r_linger = 1;
a40c4f10
YS
1335 }
1336}
1337EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1338
d29adb34
JD
1339/*
1340 * Returns whether a request should be blocked from being sent
1341 * based on the current osdmap and osd_client settings.
1342 *
1343 * Caller should hold map_sem for read.
1344 */
1345static bool __req_should_be_paused(struct ceph_osd_client *osdc,
1346 struct ceph_osd_request *req)
1347{
1348 bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
1349 bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
1350 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
1351 return (req->r_flags & CEPH_OSD_FLAG_READ && pauserd) ||
1352 (req->r_flags & CEPH_OSD_FLAG_WRITE && pausewr);
1353}
1354
17a13e40
ID
1355/*
1356 * Calculate mapping of a request to a PG. Takes tiering into account.
1357 */
1358static int __calc_request_pg(struct ceph_osdmap *osdmap,
1359 struct ceph_osd_request *req,
1360 struct ceph_pg *pg_out)
1361{
205ee118
ID
1362 bool need_check_tiering;
1363
1364 need_check_tiering = false;
1365 if (req->r_target_oloc.pool == -1) {
1366 req->r_target_oloc = req->r_base_oloc; /* struct */
1367 need_check_tiering = true;
1368 }
1369 if (req->r_target_oid.name_len == 0) {
1370 ceph_oid_copy(&req->r_target_oid, &req->r_base_oid);
1371 need_check_tiering = true;
1372 }
1373
1374 if (need_check_tiering &&
1375 (req->r_flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
17a13e40
ID
1376 struct ceph_pg_pool_info *pi;
1377
205ee118 1378 pi = ceph_pg_pool_by_id(osdmap, req->r_target_oloc.pool);
17a13e40
ID
1379 if (pi) {
1380 if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
1381 pi->read_tier >= 0)
205ee118 1382 req->r_target_oloc.pool = pi->read_tier;
17a13e40
ID
1383 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1384 pi->write_tier >= 0)
205ee118 1385 req->r_target_oloc.pool = pi->write_tier;
17a13e40
ID
1386 }
1387 /* !pi is caught in ceph_oloc_oid_to_pg() */
1388 }
1389
205ee118
ID
1390 return ceph_oloc_oid_to_pg(osdmap, &req->r_target_oloc,
1391 &req->r_target_oid, pg_out);
17a13e40
ID
1392}
1393
f671b581
ID
1394static void __enqueue_request(struct ceph_osd_request *req)
1395{
1396 struct ceph_osd_client *osdc = req->r_osdc;
1397
1398 dout("%s %p tid %llu to osd%d\n", __func__, req, req->r_tid,
1399 req->r_osd ? req->r_osd->o_osd : -1);
1400
1401 if (req->r_osd) {
1402 __remove_osd_from_lru(req->r_osd);
1403 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1404 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1405 } else {
1406 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1407 }
1408}
1409
f24e9980
SW
1410/*
1411 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1412 * (as needed), and set the request r_osd appropriately. If there is
25985edc 1413 * no up osd, set r_osd to NULL. Move the request to the appropriate list
6f6c7006 1414 * (unsent, homeless) or leave on in-flight lru.
f24e9980
SW
1415 *
1416 * Return 0 if unchanged, 1 if changed, or negative on error.
1417 *
1418 * Caller should hold map_sem for read and request_mutex.
1419 */
6f6c7006 1420static int __map_request(struct ceph_osd_client *osdc,
38d6453c 1421 struct ceph_osd_request *req, int force_resend)
f24e9980 1422{
5b191d99 1423 struct ceph_pg pgid;
d85b7056 1424 int acting[CEPH_PG_MAX_SIZE];
8008ab10 1425 int num, o;
f24e9980 1426 int err;
d29adb34 1427 bool was_paused;
f24e9980 1428
6f6c7006 1429 dout("map_request %p tid %lld\n", req, req->r_tid);
17a13e40
ID
1430
1431 err = __calc_request_pg(osdc->osdmap, req, &pgid);
6f6c7006
SW
1432 if (err) {
1433 list_move(&req->r_req_lru_item, &osdc->req_notarget);
f24e9980 1434 return err;
6f6c7006 1435 }
7740a42f
SW
1436 req->r_pgid = pgid;
1437
8008ab10
ID
1438 num = ceph_calc_pg_acting(osdc->osdmap, pgid, acting, &o);
1439 if (num < 0)
1440 num = 0;
f24e9980 1441
d29adb34
JD
1442 was_paused = req->r_paused;
1443 req->r_paused = __req_should_be_paused(osdc, req);
1444 if (was_paused && !req->r_paused)
1445 force_resend = 1;
1446
38d6453c
SW
1447 if ((!force_resend &&
1448 req->r_osd && req->r_osd->o_osd == o &&
d85b7056
SW
1449 req->r_sent >= req->r_osd->o_incarnation &&
1450 req->r_num_pg_osds == num &&
1451 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
d29adb34
JD
1452 (req->r_osd == NULL && o == -1) ||
1453 req->r_paused)
f24e9980
SW
1454 return 0; /* no change */
1455
5b191d99
SW
1456 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1457 req->r_tid, pgid.pool, pgid.seed, o,
f24e9980
SW
1458 req->r_osd ? req->r_osd->o_osd : -1);
1459
d85b7056
SW
1460 /* record full pg acting set */
1461 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1462 req->r_num_pg_osds = num;
1463
f24e9980
SW
1464 if (req->r_osd) {
1465 __cancel_request(req);
1466 list_del_init(&req->r_osd_item);
a390de02 1467 list_del_init(&req->r_linger_osd_item);
f24e9980
SW
1468 req->r_osd = NULL;
1469 }
1470
1471 req->r_osd = __lookup_osd(osdc, o);
1472 if (!req->r_osd && o >= 0) {
c99eb1c7 1473 err = -ENOMEM;
e10006f8 1474 req->r_osd = create_osd(osdc, o);
6f6c7006
SW
1475 if (!req->r_osd) {
1476 list_move(&req->r_req_lru_item, &osdc->req_notarget);
c99eb1c7 1477 goto out;
6f6c7006 1478 }
f24e9980 1479
6f6c7006 1480 dout("map_request osd %p is osd%d\n", req->r_osd, o);
f24e9980
SW
1481 __insert_osd(osdc, req->r_osd);
1482
b7a9e5dd
SW
1483 ceph_con_open(&req->r_osd->o_con,
1484 CEPH_ENTITY_TYPE_OSD, o,
1485 &osdc->osdmap->osd_addr[o]);
f24e9980
SW
1486 }
1487
f671b581 1488 __enqueue_request(req);
d85b7056 1489 err = 1; /* osd or pg changed */
f24e9980
SW
1490
1491out:
f24e9980
SW
1492 return err;
1493}
1494
1495/*
1496 * caller should hold map_sem (for read) and request_mutex
1497 */
56e925b6
SW
1498static void __send_request(struct ceph_osd_client *osdc,
1499 struct ceph_osd_request *req)
f24e9980 1500{
1b83bef2 1501 void *p;
f24e9980 1502
1b83bef2
SW
1503 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1504 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1505 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
1506
1507 /* fill in message content that changes each time we send it */
1508 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1509 put_unaligned_le32(req->r_flags, req->r_request_flags);
205ee118 1510 put_unaligned_le64(req->r_target_oloc.pool, req->r_request_pool);
1b83bef2
SW
1511 p = req->r_request_pgid;
1512 ceph_encode_64(&p, req->r_pgid.pool);
1513 ceph_encode_32(&p, req->r_pgid.seed);
1514 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1515 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1516 sizeof(req->r_reassert_version));
2169aea6 1517
3dd72fc0 1518 req->r_stamp = jiffies;
07a27e22 1519 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
f24e9980
SW
1520
1521 ceph_msg_get(req->r_request); /* send consumes a ref */
26be8808 1522
f24e9980 1523 req->r_sent = req->r_osd->o_incarnation;
26be8808
AE
1524
1525 ceph_con_send(&req->r_osd->o_con, req->r_request);
f24e9980
SW
1526}
1527
6f6c7006
SW
1528/*
1529 * Send any requests in the queue (req_unsent).
1530 */
f9d25199 1531static void __send_queued(struct ceph_osd_client *osdc)
6f6c7006
SW
1532{
1533 struct ceph_osd_request *req, *tmp;
1534
f9d25199
AE
1535 dout("__send_queued\n");
1536 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
6f6c7006 1537 __send_request(osdc, req);
6f6c7006
SW
1538}
1539
0bbfdfe8
ID
1540/*
1541 * Caller should hold map_sem for read and request_mutex.
1542 */
1543static int __ceph_osdc_start_request(struct ceph_osd_client *osdc,
1544 struct ceph_osd_request *req,
1545 bool nofail)
1546{
1547 int rc;
1548
1549 __register_request(osdc, req);
1550 req->r_sent = 0;
1551 req->r_got_reply = 0;
1552 rc = __map_request(osdc, req, 0);
1553 if (rc < 0) {
1554 if (nofail) {
1555 dout("osdc_start_request failed map, "
1556 " will retry %lld\n", req->r_tid);
1557 rc = 0;
1558 } else {
1559 __unregister_request(osdc, req);
1560 }
1561 return rc;
1562 }
1563
1564 if (req->r_osd == NULL) {
1565 dout("send_request %p no up osds in pg\n", req);
1566 ceph_monc_request_next_osdmap(&osdc->client->monc);
1567 } else {
1568 __send_queued(osdc);
1569 }
1570
1571 return 0;
1572}
1573
f24e9980
SW
1574/*
1575 * Timeout callback, called every N seconds when 1 or more osd
1576 * requests has been active for more than N seconds. When this
1577 * happens, we ping all OSDs with requests who have timed out to
1578 * ensure any communications channel reset is detected. Reset the
1579 * request timeouts another N seconds in the future as we go.
1580 * Reschedule the timeout event another N seconds in future (unless
1581 * there are no open requests).
1582 */
1583static void handle_timeout(struct work_struct *work)
1584{
1585 struct ceph_osd_client *osdc =
1586 container_of(work, struct ceph_osd_client, timeout_work.work);
a319bf56 1587 struct ceph_options *opts = osdc->client->options;
83aff95e 1588 struct ceph_osd_request *req;
f24e9980 1589 struct ceph_osd *osd;
422d2cb8 1590 struct list_head slow_osds;
f24e9980
SW
1591 dout("timeout\n");
1592 down_read(&osdc->map_sem);
1593
1594 ceph_monc_request_next_osdmap(&osdc->client->monc);
1595
1596 mutex_lock(&osdc->request_mutex);
f24e9980 1597
422d2cb8
YS
1598 /*
1599 * ping osds that are a bit slow. this ensures that if there
1600 * is a break in the TCP connection we will notice, and reopen
1601 * a connection with that osd (from the fault callback).
1602 */
1603 INIT_LIST_HEAD(&slow_osds);
1604 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
a319bf56
ID
1605 if (time_before(jiffies,
1606 req->r_stamp + opts->osd_keepalive_timeout))
422d2cb8
YS
1607 break;
1608
1609 osd = req->r_osd;
1610 BUG_ON(!osd);
1611 dout(" tid %llu is slow, will send keepalive on osd%d\n",
f24e9980 1612 req->r_tid, osd->o_osd);
422d2cb8
YS
1613 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1614 }
1615 while (!list_empty(&slow_osds)) {
1616 osd = list_entry(slow_osds.next, struct ceph_osd,
1617 o_keepalive_item);
1618 list_del_init(&osd->o_keepalive_item);
f24e9980
SW
1619 ceph_con_keepalive(&osd->o_con);
1620 }
1621
422d2cb8 1622 __schedule_osd_timeout(osdc);
f9d25199 1623 __send_queued(osdc);
f24e9980 1624 mutex_unlock(&osdc->request_mutex);
f24e9980
SW
1625 up_read(&osdc->map_sem);
1626}
1627
f5a2041b
YS
1628static void handle_osds_timeout(struct work_struct *work)
1629{
1630 struct ceph_osd_client *osdc =
1631 container_of(work, struct ceph_osd_client,
1632 osds_timeout_work.work);
a319bf56 1633 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
f5a2041b
YS
1634
1635 dout("osds timeout\n");
1636 down_read(&osdc->map_sem);
aca420bc 1637 remove_old_osds(osdc);
f5a2041b
YS
1638 up_read(&osdc->map_sem);
1639
1640 schedule_delayed_work(&osdc->osds_timeout_work,
1641 round_jiffies_relative(delay));
1642}
1643
205ee118
ID
1644static int ceph_oloc_decode(void **p, void *end,
1645 struct ceph_object_locator *oloc)
1646{
1647 u8 struct_v, struct_cv;
1648 u32 len;
1649 void *struct_end;
1650 int ret = 0;
1651
1652 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1653 struct_v = ceph_decode_8(p);
1654 struct_cv = ceph_decode_8(p);
1655 if (struct_v < 3) {
1656 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1657 struct_v, struct_cv);
1658 goto e_inval;
1659 }
1660 if (struct_cv > 6) {
1661 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1662 struct_v, struct_cv);
1663 goto e_inval;
1664 }
1665 len = ceph_decode_32(p);
1666 ceph_decode_need(p, end, len, e_inval);
1667 struct_end = *p + len;
1668
1669 oloc->pool = ceph_decode_64(p);
1670 *p += 4; /* skip preferred */
1671
1672 len = ceph_decode_32(p);
1673 if (len > 0) {
1674 pr_warn("ceph_object_locator::key is set\n");
1675 goto e_inval;
1676 }
1677
1678 if (struct_v >= 5) {
1679 len = ceph_decode_32(p);
1680 if (len > 0) {
1681 pr_warn("ceph_object_locator::nspace is set\n");
1682 goto e_inval;
1683 }
1684 }
1685
1686 if (struct_v >= 6) {
1687 s64 hash = ceph_decode_64(p);
1688 if (hash != -1) {
1689 pr_warn("ceph_object_locator::hash is set\n");
1690 goto e_inval;
1691 }
1692 }
1693
1694 /* skip the rest */
1695 *p = struct_end;
1696out:
1697 return ret;
1698
1699e_inval:
1700 ret = -EINVAL;
1701 goto out;
1702}
1703
1704static int ceph_redirect_decode(void **p, void *end,
1705 struct ceph_request_redirect *redir)
1706{
1707 u8 struct_v, struct_cv;
1708 u32 len;
1709 void *struct_end;
1710 int ret;
1711
1712 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1713 struct_v = ceph_decode_8(p);
1714 struct_cv = ceph_decode_8(p);
1715 if (struct_cv > 1) {
1716 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1717 struct_v, struct_cv);
1718 goto e_inval;
1719 }
1720 len = ceph_decode_32(p);
1721 ceph_decode_need(p, end, len, e_inval);
1722 struct_end = *p + len;
1723
1724 ret = ceph_oloc_decode(p, end, &redir->oloc);
1725 if (ret)
1726 goto out;
1727
1728 len = ceph_decode_32(p);
1729 if (len > 0) {
1730 pr_warn("ceph_request_redirect::object_name is set\n");
1731 goto e_inval;
1732 }
1733
1734 len = ceph_decode_32(p);
1735 *p += len; /* skip osd_instructions */
1736
1737 /* skip the rest */
1738 *p = struct_end;
1739out:
1740 return ret;
1741
1742e_inval:
1743 ret = -EINVAL;
1744 goto out;
1745}
1746
25845472
SW
1747static void complete_request(struct ceph_osd_request *req)
1748{
25845472
SW
1749 complete_all(&req->r_safe_completion); /* fsync waiter */
1750}
1751
f24e9980
SW
1752/*
1753 * handle osd op reply. either call the callback if it is specified,
1754 * or do the completion to wake up the waiting thread.
1755 */
70cf052d 1756static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg)
f24e9980 1757{
1b83bef2 1758 void *p, *end;
f24e9980 1759 struct ceph_osd_request *req;
205ee118 1760 struct ceph_request_redirect redir;
f24e9980 1761 u64 tid;
1b83bef2 1762 int object_len;
79528734
AE
1763 unsigned int numops;
1764 int payload_len, flags;
0ceed5db 1765 s32 result;
1b83bef2
SW
1766 s32 retry_attempt;
1767 struct ceph_pg pg;
1768 int err;
1769 u32 reassert_epoch;
1770 u64 reassert_version;
1771 u32 osdmap_epoch;
0d5af164 1772 int already_completed;
9fc6e064 1773 u32 bytes;
b0b31a8f 1774 u8 decode_redir;
79528734 1775 unsigned int i;
f24e9980 1776
6df058c0 1777 tid = le64_to_cpu(msg->hdr.tid);
1b83bef2
SW
1778 dout("handle_reply %p tid %llu\n", msg, tid);
1779
1780 p = msg->front.iov_base;
1781 end = p + msg->front.iov_len;
1782
1783 ceph_decode_need(&p, end, 4, bad);
1784 object_len = ceph_decode_32(&p);
1785 ceph_decode_need(&p, end, object_len, bad);
1786 p += object_len;
1787
ef4859d6 1788 err = ceph_decode_pgid(&p, end, &pg);
1b83bef2 1789 if (err)
f24e9980 1790 goto bad;
1b83bef2
SW
1791
1792 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1793 flags = ceph_decode_64(&p);
1794 result = ceph_decode_32(&p);
1795 reassert_epoch = ceph_decode_32(&p);
1796 reassert_version = ceph_decode_64(&p);
1797 osdmap_epoch = ceph_decode_32(&p);
1798
f24e9980 1799 /* lookup */
ff513ace 1800 down_read(&osdc->map_sem);
f24e9980
SW
1801 mutex_lock(&osdc->request_mutex);
1802 req = __lookup_request(osdc, tid);
1803 if (req == NULL) {
1804 dout("handle_reply tid %llu dne\n", tid);
8058fd45 1805 goto bad_mutex;
f24e9980
SW
1806 }
1807 ceph_osdc_get_request(req);
1b83bef2
SW
1808
1809 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1810 req, result);
1811
18741196 1812 ceph_decode_need(&p, end, 4, bad_put);
1b83bef2
SW
1813 numops = ceph_decode_32(&p);
1814 if (numops > CEPH_OSD_MAX_OP)
1815 goto bad_put;
1816 if (numops != req->r_num_ops)
1817 goto bad_put;
1818 payload_len = 0;
18741196 1819 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad_put);
1b83bef2
SW
1820 for (i = 0; i < numops; i++) {
1821 struct ceph_osd_op *op = p;
1822 int len;
1823
1824 len = le32_to_cpu(op->payload_len);
7665d85b 1825 req->r_ops[i].outdata_len = len;
1b83bef2
SW
1826 dout(" op %d has %d bytes\n", i, len);
1827 payload_len += len;
1828 p += sizeof(*op);
1829 }
9fc6e064
AE
1830 bytes = le32_to_cpu(msg->hdr.data_len);
1831 if (payload_len != bytes) {
b9a67899
JP
1832 pr_warn("sum of op payload lens %d != data_len %d\n",
1833 payload_len, bytes);
1b83bef2
SW
1834 goto bad_put;
1835 }
1836
18741196 1837 ceph_decode_need(&p, end, 4 + numops * 4, bad_put);
1b83bef2
SW
1838 retry_attempt = ceph_decode_32(&p);
1839 for (i = 0; i < numops; i++)
7665d85b 1840 req->r_ops[i].rval = ceph_decode_32(&p);
f24e9980 1841
205ee118
ID
1842 if (le16_to_cpu(msg->hdr.version) >= 6) {
1843 p += 8 + 4; /* skip replay_version */
1844 p += 8; /* skip user_version */
eb845ff1 1845
b0b31a8f
ID
1846 if (le16_to_cpu(msg->hdr.version) >= 7)
1847 ceph_decode_8_safe(&p, end, decode_redir, bad_put);
1848 else
1849 decode_redir = 1;
1850 } else {
1851 decode_redir = 0;
1852 }
1853
1854 if (decode_redir) {
205ee118
ID
1855 err = ceph_redirect_decode(&p, end, &redir);
1856 if (err)
1857 goto bad_put;
1858 } else {
1859 redir.oloc.pool = -1;
1860 }
f24e9980 1861
205ee118
ID
1862 if (redir.oloc.pool != -1) {
1863 dout("redirect pool %lld\n", redir.oloc.pool);
1864
1865 __unregister_request(osdc, req);
205ee118
ID
1866
1867 req->r_target_oloc = redir.oloc; /* struct */
1868
1869 /*
1870 * Start redirect requests with nofail=true. If
1871 * mapping fails, request will end up on the notarget
1872 * list, waiting for the new osdmap (which can take
1873 * a while), even though the original request mapped
1874 * successfully. In the future we might want to follow
1875 * original request's nofail setting here.
1876 */
ff513ace 1877 err = __ceph_osdc_start_request(osdc, req, true);
205ee118
ID
1878 BUG_ON(err);
1879
ff513ace 1880 goto out_unlock;
205ee118
ID
1881 }
1882
1883 already_completed = req->r_got_reply;
1884 if (!req->r_got_reply) {
1b83bef2 1885 req->r_result = result;
f24e9980
SW
1886 dout("handle_reply result %d bytes %d\n", req->r_result,
1887 bytes);
1888 if (req->r_result == 0)
1889 req->r_result = bytes;
1890
1891 /* in case this is a write and we need to replay, */
1b83bef2
SW
1892 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1893 req->r_reassert_version.version = cpu_to_le64(reassert_version);
f24e9980
SW
1894
1895 req->r_got_reply = 1;
1896 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1897 dout("handle_reply tid %llu dup ack\n", tid);
ff513ace 1898 goto out_unlock;
f24e9980
SW
1899 }
1900
1901 dout("handle_reply tid %llu flags %d\n", tid, flags);
1902
a40c4f10
YS
1903 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1904 __register_linger_request(osdc, req);
1905
f24e9980 1906 /* either this is a read, or we got the safe response */
0ceed5db
SW
1907 if (result < 0 ||
1908 (flags & CEPH_OSD_FLAG_ONDISK) ||
f24e9980
SW
1909 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1910 __unregister_request(osdc, req);
1911
1912 mutex_unlock(&osdc->request_mutex);
ff513ace 1913 up_read(&osdc->map_sem);
f24e9980 1914
eb845ff1 1915 if (!already_completed) {
61c5d6bf
YZ
1916 if (req->r_unsafe_callback &&
1917 result >= 0 && !(flags & CEPH_OSD_FLAG_ONDISK))
1918 req->r_unsafe_callback(req, true);
eb845ff1
YZ
1919 if (req->r_callback)
1920 req->r_callback(req, msg);
1921 else
1922 complete_all(&req->r_completion);
1923 }
f24e9980 1924
61c5d6bf
YZ
1925 if (flags & CEPH_OSD_FLAG_ONDISK) {
1926 if (req->r_unsafe_callback && already_completed)
1927 req->r_unsafe_callback(req, false);
25845472 1928 complete_request(req);
61c5d6bf 1929 }
f24e9980 1930
ff513ace 1931out:
a40c4f10 1932 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
f24e9980
SW
1933 ceph_osdc_put_request(req);
1934 return;
ff513ace
ID
1935out_unlock:
1936 mutex_unlock(&osdc->request_mutex);
1937 up_read(&osdc->map_sem);
1938 goto out;
f24e9980 1939
1b83bef2 1940bad_put:
37c89bde
LW
1941 req->r_result = -EIO;
1942 __unregister_request(osdc, req);
1943 if (req->r_callback)
1944 req->r_callback(req, msg);
1945 else
1946 complete_all(&req->r_completion);
1947 complete_request(req);
1b83bef2 1948 ceph_osdc_put_request(req);
8058fd45
AE
1949bad_mutex:
1950 mutex_unlock(&osdc->request_mutex);
ff513ace 1951 up_read(&osdc->map_sem);
f24e9980 1952bad:
1b83bef2
SW
1953 pr_err("corrupt osd_op_reply got %d %d\n",
1954 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
9ec7cab1 1955 ceph_msg_dump(msg);
f24e9980
SW
1956}
1957
6f6c7006 1958static void reset_changed_osds(struct ceph_osd_client *osdc)
f24e9980 1959{
f24e9980 1960 struct rb_node *p, *n;
f24e9980 1961
7eb71e03 1962 dout("%s %p\n", __func__, osdc);
6f6c7006
SW
1963 for (p = rb_first(&osdc->osds); p; p = n) {
1964 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
f24e9980 1965
6f6c7006
SW
1966 n = rb_next(p);
1967 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1968 memcmp(&osd->o_con.peer_addr,
1969 ceph_osd_addr(osdc->osdmap,
1970 osd->o_osd),
1971 sizeof(struct ceph_entity_addr)) != 0)
1972 __reset_osd(osdc, osd);
f24e9980 1973 }
422d2cb8
YS
1974}
1975
1976/*
6f6c7006
SW
1977 * Requeue requests whose mapping to an OSD has changed. If requests map to
1978 * no osd, request a new map.
422d2cb8 1979 *
e6d50f67 1980 * Caller should hold map_sem for read.
422d2cb8 1981 */
9a1ea2db
JD
1982static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
1983 bool force_resend_writes)
422d2cb8 1984{
a40c4f10 1985 struct ceph_osd_request *req, *nreq;
6f6c7006
SW
1986 struct rb_node *p;
1987 int needmap = 0;
1988 int err;
9a1ea2db 1989 bool force_resend_req;
422d2cb8 1990
9a1ea2db
JD
1991 dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
1992 force_resend_writes ? " (force resend writes)" : "");
422d2cb8 1993 mutex_lock(&osdc->request_mutex);
6194ea89 1994 for (p = rb_first(&osdc->requests); p; ) {
6f6c7006 1995 req = rb_entry(p, struct ceph_osd_request, r_node);
6194ea89 1996 p = rb_next(p);
ab60b16d
AE
1997
1998 /*
1999 * For linger requests that have not yet been
2000 * registered, move them to the linger list; they'll
2001 * be sent to the osd in the loop below. Unregister
2002 * the request before re-registering it as a linger
2003 * request to ensure the __map_request() below
2004 * will decide it needs to be sent.
2005 */
2006 if (req->r_linger && list_empty(&req->r_linger_item)) {
2007 dout("%p tid %llu restart on osd%d\n",
2008 req, req->r_tid,
2009 req->r_osd ? req->r_osd->o_osd : -1);
96e4dac6 2010 ceph_osdc_get_request(req);
ab60b16d
AE
2011 __unregister_request(osdc, req);
2012 __register_linger_request(osdc, req);
96e4dac6 2013 ceph_osdc_put_request(req);
ab60b16d
AE
2014 continue;
2015 }
2016
9a1ea2db
JD
2017 force_resend_req = force_resend ||
2018 (force_resend_writes &&
2019 req->r_flags & CEPH_OSD_FLAG_WRITE);
2020 err = __map_request(osdc, req, force_resend_req);
6f6c7006
SW
2021 if (err < 0)
2022 continue; /* error */
2023 if (req->r_osd == NULL) {
2024 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
2025 needmap++; /* request a newer map */
2026 } else if (err > 0) {
6194ea89
SW
2027 if (!req->r_linger) {
2028 dout("%p tid %llu requeued on osd%d\n", req,
2029 req->r_tid,
2030 req->r_osd ? req->r_osd->o_osd : -1);
a40c4f10 2031 req->r_flags |= CEPH_OSD_FLAG_RETRY;
6194ea89
SW
2032 }
2033 }
a40c4f10
YS
2034 }
2035
2036 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
2037 r_linger_item) {
2038 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
2039
9a1ea2db
JD
2040 err = __map_request(osdc, req,
2041 force_resend || force_resend_writes);
ab60b16d 2042 dout("__map_request returned %d\n", err);
a40c4f10
YS
2043 if (err < 0)
2044 continue; /* hrm! */
b0494532
ID
2045 if (req->r_osd == NULL || err > 0) {
2046 if (req->r_osd == NULL) {
2047 dout("lingering %p tid %llu maps to no osd\n",
2048 req, req->r_tid);
2049 /*
2050 * A homeless lingering request makes
2051 * no sense, as it's job is to keep
2052 * a particular OSD connection open.
2053 * Request a newer map and kick the
2054 * request, knowing that it won't be
2055 * resent until we actually get a map
2056 * that can tell us where to send it.
2057 */
2058 needmap++;
2059 }
a40c4f10 2060
b0494532
ID
2061 dout("kicking lingering %p tid %llu osd%d\n", req,
2062 req->r_tid, req->r_osd ? req->r_osd->o_osd : -1);
2063 __register_request(osdc, req);
2064 __unregister_linger_request(osdc, req);
2065 }
6f6c7006 2066 }
14d2f38d 2067 reset_changed_osds(osdc);
f24e9980
SW
2068 mutex_unlock(&osdc->request_mutex);
2069
2070 if (needmap) {
2071 dout("%d requests for down osds, need new map\n", needmap);
2072 ceph_monc_request_next_osdmap(&osdc->client->monc);
2073 }
422d2cb8 2074}
6f6c7006
SW
2075
2076
f24e9980
SW
2077/*
2078 * Process updated osd map.
2079 *
2080 * The message contains any number of incremental and full maps, normally
2081 * indicating some sort of topology change in the cluster. Kick requests
2082 * off to different OSDs as needed.
2083 */
2084void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
2085{
2086 void *p, *end, *next;
2087 u32 nr_maps, maplen;
2088 u32 epoch;
2089 struct ceph_osdmap *newmap = NULL, *oldmap;
2090 int err;
2091 struct ceph_fsid fsid;
9a1ea2db 2092 bool was_full;
f24e9980
SW
2093
2094 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
2095 p = msg->front.iov_base;
2096 end = p + msg->front.iov_len;
2097
2098 /* verify fsid */
2099 ceph_decode_need(&p, end, sizeof(fsid), bad);
2100 ceph_decode_copy(&p, &fsid, sizeof(fsid));
0743304d
SW
2101 if (ceph_check_fsid(osdc->client, &fsid) < 0)
2102 return;
f24e9980
SW
2103
2104 down_write(&osdc->map_sem);
2105
9a1ea2db
JD
2106 was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
2107
f24e9980
SW
2108 /* incremental maps */
2109 ceph_decode_32_safe(&p, end, nr_maps, bad);
2110 dout(" %d inc maps\n", nr_maps);
2111 while (nr_maps > 0) {
2112 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
c89136ea
SW
2113 epoch = ceph_decode_32(&p);
2114 maplen = ceph_decode_32(&p);
f24e9980
SW
2115 ceph_decode_need(&p, end, maplen, bad);
2116 next = p + maplen;
2117 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
2118 dout("applying incremental map %u len %d\n",
2119 epoch, maplen);
2120 newmap = osdmap_apply_incremental(&p, next,
2121 osdc->osdmap,
15d9882c 2122 &osdc->client->msgr);
f24e9980
SW
2123 if (IS_ERR(newmap)) {
2124 err = PTR_ERR(newmap);
2125 goto bad;
2126 }
30dc6381 2127 BUG_ON(!newmap);
f24e9980
SW
2128 if (newmap != osdc->osdmap) {
2129 ceph_osdmap_destroy(osdc->osdmap);
2130 osdc->osdmap = newmap;
2131 }
9a1ea2db
JD
2132 was_full = was_full ||
2133 ceph_osdmap_flag(osdc->osdmap,
2134 CEPH_OSDMAP_FULL);
2135 kick_requests(osdc, 0, was_full);
f24e9980
SW
2136 } else {
2137 dout("ignoring incremental map %u len %d\n",
2138 epoch, maplen);
2139 }
2140 p = next;
2141 nr_maps--;
2142 }
2143 if (newmap)
2144 goto done;
2145
2146 /* full maps */
2147 ceph_decode_32_safe(&p, end, nr_maps, bad);
2148 dout(" %d full maps\n", nr_maps);
2149 while (nr_maps) {
2150 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
c89136ea
SW
2151 epoch = ceph_decode_32(&p);
2152 maplen = ceph_decode_32(&p);
f24e9980
SW
2153 ceph_decode_need(&p, end, maplen, bad);
2154 if (nr_maps > 1) {
2155 dout("skipping non-latest full map %u len %d\n",
2156 epoch, maplen);
2157 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
2158 dout("skipping full map %u len %d, "
2159 "older than our %u\n", epoch, maplen,
2160 osdc->osdmap->epoch);
2161 } else {
38d6453c
SW
2162 int skipped_map = 0;
2163
f24e9980 2164 dout("taking full map %u len %d\n", epoch, maplen);
a2505d63 2165 newmap = ceph_osdmap_decode(&p, p+maplen);
f24e9980
SW
2166 if (IS_ERR(newmap)) {
2167 err = PTR_ERR(newmap);
2168 goto bad;
2169 }
30dc6381 2170 BUG_ON(!newmap);
f24e9980
SW
2171 oldmap = osdc->osdmap;
2172 osdc->osdmap = newmap;
38d6453c
SW
2173 if (oldmap) {
2174 if (oldmap->epoch + 1 < newmap->epoch)
2175 skipped_map = 1;
f24e9980 2176 ceph_osdmap_destroy(oldmap);
38d6453c 2177 }
9a1ea2db
JD
2178 was_full = was_full ||
2179 ceph_osdmap_flag(osdc->osdmap,
2180 CEPH_OSDMAP_FULL);
2181 kick_requests(osdc, skipped_map, was_full);
f24e9980
SW
2182 }
2183 p += maplen;
2184 nr_maps--;
2185 }
2186
b72e19b9
DC
2187 if (!osdc->osdmap)
2188 goto bad;
f24e9980
SW
2189done:
2190 downgrade_write(&osdc->map_sem);
82dcabad
ID
2191 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
2192 osdc->osdmap->epoch);
cd634fb6
SW
2193
2194 /*
2195 * subscribe to subsequent osdmap updates if full to ensure
2196 * we find out when we are no longer full and stop returning
2197 * ENOSPC.
2198 */
d29adb34
JD
2199 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
2200 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
2201 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR))
cd634fb6
SW
2202 ceph_monc_request_next_osdmap(&osdc->client->monc);
2203
f9d25199
AE
2204 mutex_lock(&osdc->request_mutex);
2205 __send_queued(osdc);
2206 mutex_unlock(&osdc->request_mutex);
f24e9980 2207 up_read(&osdc->map_sem);
03066f23 2208 wake_up_all(&osdc->client->auth_wq);
f24e9980
SW
2209 return;
2210
2211bad:
2212 pr_err("osdc handle_map corrupt msg\n");
9ec7cab1 2213 ceph_msg_dump(msg);
f24e9980 2214 up_write(&osdc->map_sem);
f24e9980
SW
2215}
2216
a40c4f10
YS
2217/*
2218 * watch/notify callback event infrastructure
2219 *
2220 * These callbacks are used both for watch and notify operations.
2221 */
2222static void __release_event(struct kref *kref)
2223{
2224 struct ceph_osd_event *event =
2225 container_of(kref, struct ceph_osd_event, kref);
2226
2227 dout("__release_event %p\n", event);
2228 kfree(event);
2229}
2230
2231static void get_event(struct ceph_osd_event *event)
2232{
2233 kref_get(&event->kref);
2234}
2235
2236void ceph_osdc_put_event(struct ceph_osd_event *event)
2237{
2238 kref_put(&event->kref, __release_event);
2239}
2240EXPORT_SYMBOL(ceph_osdc_put_event);
2241
2242static void __insert_event(struct ceph_osd_client *osdc,
2243 struct ceph_osd_event *new)
2244{
2245 struct rb_node **p = &osdc->event_tree.rb_node;
2246 struct rb_node *parent = NULL;
2247 struct ceph_osd_event *event = NULL;
2248
2249 while (*p) {
2250 parent = *p;
2251 event = rb_entry(parent, struct ceph_osd_event, node);
2252 if (new->cookie < event->cookie)
2253 p = &(*p)->rb_left;
2254 else if (new->cookie > event->cookie)
2255 p = &(*p)->rb_right;
2256 else
2257 BUG();
2258 }
2259
2260 rb_link_node(&new->node, parent, p);
2261 rb_insert_color(&new->node, &osdc->event_tree);
2262}
2263
2264static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
2265 u64 cookie)
2266{
2267 struct rb_node **p = &osdc->event_tree.rb_node;
2268 struct rb_node *parent = NULL;
2269 struct ceph_osd_event *event = NULL;
2270
2271 while (*p) {
2272 parent = *p;
2273 event = rb_entry(parent, struct ceph_osd_event, node);
2274 if (cookie < event->cookie)
2275 p = &(*p)->rb_left;
2276 else if (cookie > event->cookie)
2277 p = &(*p)->rb_right;
2278 else
2279 return event;
2280 }
2281 return NULL;
2282}
2283
2284static void __remove_event(struct ceph_osd_event *event)
2285{
2286 struct ceph_osd_client *osdc = event->osdc;
2287
2288 if (!RB_EMPTY_NODE(&event->node)) {
2289 dout("__remove_event removed %p\n", event);
2290 rb_erase(&event->node, &osdc->event_tree);
2291 ceph_osdc_put_event(event);
2292 } else {
2293 dout("__remove_event didn't remove %p\n", event);
2294 }
2295}
2296
2297int ceph_osdc_create_event(struct ceph_osd_client *osdc,
2298 void (*event_cb)(u64, u64, u8, void *),
3c663bbd 2299 void *data, struct ceph_osd_event **pevent)
a40c4f10
YS
2300{
2301 struct ceph_osd_event *event;
2302
2303 event = kmalloc(sizeof(*event), GFP_NOIO);
2304 if (!event)
2305 return -ENOMEM;
2306
2307 dout("create_event %p\n", event);
2308 event->cb = event_cb;
3c663bbd 2309 event->one_shot = 0;
a40c4f10
YS
2310 event->data = data;
2311 event->osdc = osdc;
2312 INIT_LIST_HEAD(&event->osd_node);
3ee5234d 2313 RB_CLEAR_NODE(&event->node);
a40c4f10
YS
2314 kref_init(&event->kref); /* one ref for us */
2315 kref_get(&event->kref); /* one ref for the caller */
a40c4f10
YS
2316
2317 spin_lock(&osdc->event_lock);
2318 event->cookie = ++osdc->event_count;
2319 __insert_event(osdc, event);
2320 spin_unlock(&osdc->event_lock);
2321
2322 *pevent = event;
2323 return 0;
2324}
2325EXPORT_SYMBOL(ceph_osdc_create_event);
2326
2327void ceph_osdc_cancel_event(struct ceph_osd_event *event)
2328{
2329 struct ceph_osd_client *osdc = event->osdc;
2330
2331 dout("cancel_event %p\n", event);
2332 spin_lock(&osdc->event_lock);
2333 __remove_event(event);
2334 spin_unlock(&osdc->event_lock);
2335 ceph_osdc_put_event(event); /* caller's */
2336}
2337EXPORT_SYMBOL(ceph_osdc_cancel_event);
2338
2339
2340static void do_event_work(struct work_struct *work)
2341{
2342 struct ceph_osd_event_work *event_work =
2343 container_of(work, struct ceph_osd_event_work, work);
2344 struct ceph_osd_event *event = event_work->event;
2345 u64 ver = event_work->ver;
2346 u64 notify_id = event_work->notify_id;
2347 u8 opcode = event_work->opcode;
2348
2349 dout("do_event_work completing %p\n", event);
2350 event->cb(ver, notify_id, opcode, event->data);
a40c4f10
YS
2351 dout("do_event_work completed %p\n", event);
2352 ceph_osdc_put_event(event);
2353 kfree(event_work);
2354}
2355
2356
2357/*
2358 * Process osd watch notifications
2359 */
3c663bbd
AE
2360static void handle_watch_notify(struct ceph_osd_client *osdc,
2361 struct ceph_msg *msg)
a40c4f10
YS
2362{
2363 void *p, *end;
2364 u8 proto_ver;
2365 u64 cookie, ver, notify_id;
2366 u8 opcode;
2367 struct ceph_osd_event *event;
2368 struct ceph_osd_event_work *event_work;
2369
2370 p = msg->front.iov_base;
2371 end = p + msg->front.iov_len;
2372
2373 ceph_decode_8_safe(&p, end, proto_ver, bad);
2374 ceph_decode_8_safe(&p, end, opcode, bad);
2375 ceph_decode_64_safe(&p, end, cookie, bad);
2376 ceph_decode_64_safe(&p, end, ver, bad);
2377 ceph_decode_64_safe(&p, end, notify_id, bad);
2378
2379 spin_lock(&osdc->event_lock);
2380 event = __find_event(osdc, cookie);
2381 if (event) {
3c663bbd 2382 BUG_ON(event->one_shot);
a40c4f10 2383 get_event(event);
a40c4f10
YS
2384 }
2385 spin_unlock(&osdc->event_lock);
2386 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2387 cookie, ver, event);
2388 if (event) {
2389 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
a40c4f10 2390 if (!event_work) {
91883cd2
ID
2391 pr_err("couldn't allocate event_work\n");
2392 ceph_osdc_put_event(event);
2393 return;
a40c4f10 2394 }
6b0ae409 2395 INIT_WORK(&event_work->work, do_event_work);
a40c4f10
YS
2396 event_work->event = event;
2397 event_work->ver = ver;
2398 event_work->notify_id = notify_id;
2399 event_work->opcode = opcode;
a40c4f10 2400
91883cd2
ID
2401 queue_work(osdc->notify_wq, &event_work->work);
2402 }
a40c4f10 2403
a40c4f10
YS
2404 return;
2405
2406bad:
2407 pr_err("osdc handle_watch_notify corrupt msg\n");
a40c4f10
YS
2408}
2409
e65550fd
AE
2410/*
2411 * build new request AND message
2412 *
2413 */
2414void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
2415 struct ceph_snap_context *snapc, u64 snap_id,
2416 struct timespec *mtime)
2417{
2418 struct ceph_msg *msg = req->r_request;
2419 void *p;
2420 size_t msg_size;
2421 int flags = req->r_flags;
2422 u64 data_len;
2423 unsigned int i;
2424
2425 req->r_snapid = snap_id;
2426 req->r_snapc = ceph_get_snap_context(snapc);
2427
2428 /* encode request */
2429 msg->hdr.version = cpu_to_le16(4);
2430
2431 p = msg->front.iov_base;
2432 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2433 req->r_request_osdmap_epoch = p;
2434 p += 4;
2435 req->r_request_flags = p;
2436 p += 4;
2437 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2438 ceph_encode_timespec(p, mtime);
2439 p += sizeof(struct ceph_timespec);
2440 req->r_request_reassert_version = p;
2441 p += sizeof(struct ceph_eversion); /* will get filled in */
2442
2443 /* oloc */
2444 ceph_encode_8(&p, 4);
2445 ceph_encode_8(&p, 4);
2446 ceph_encode_32(&p, 8 + 4 + 4);
2447 req->r_request_pool = p;
2448 p += 8;
2449 ceph_encode_32(&p, -1); /* preferred */
2450 ceph_encode_32(&p, 0); /* key len */
2451
2452 ceph_encode_8(&p, 1);
2453 req->r_request_pgid = p;
2454 p += 8 + 4;
2455 ceph_encode_32(&p, -1); /* preferred */
2456
2457 /* oid */
3c972c95
ID
2458 ceph_encode_32(&p, req->r_base_oid.name_len);
2459 memcpy(p, req->r_base_oid.name, req->r_base_oid.name_len);
2460 dout("oid '%.*s' len %d\n", req->r_base_oid.name_len,
2461 req->r_base_oid.name, req->r_base_oid.name_len);
2462 p += req->r_base_oid.name_len;
e65550fd
AE
2463
2464 /* ops--can imply data */
2465 ceph_encode_16(&p, (u16)req->r_num_ops);
2466 data_len = 0;
2467 for (i = 0; i < req->r_num_ops; i++) {
2468 data_len += osd_req_encode_op(req, p, i);
2469 p += sizeof(struct ceph_osd_op);
2470 }
2471
2472 /* snaps */
2473 ceph_encode_64(&p, req->r_snapid);
2474 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2475 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2476 if (req->r_snapc) {
2477 for (i = 0; i < snapc->num_snaps; i++) {
2478 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2479 }
2480 }
2481
2482 req->r_request_attempts = p;
2483 p += 4;
2484
2485 /* data */
2486 if (flags & CEPH_OSD_FLAG_WRITE) {
2487 u16 data_off;
2488
2489 /*
2490 * The header "data_off" is a hint to the receiver
2491 * allowing it to align received data into its
2492 * buffers such that there's no need to re-copy
2493 * it before writing it to disk (direct I/O).
2494 */
2495 data_off = (u16) (off & 0xffff);
2496 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2497 }
2498 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2499
2500 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2501 msg_size = p - msg->front.iov_base;
2502 msg->front.iov_len = msg_size;
2503 msg->hdr.front_len = cpu_to_le32(msg_size);
2504
2505 dout("build_request msg_size was %d\n", (int)msg_size);
2506}
2507EXPORT_SYMBOL(ceph_osdc_build_request);
2508
70636773
AE
2509/*
2510 * Register request, send initial attempt.
2511 */
2512int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2513 struct ceph_osd_request *req,
2514 bool nofail)
2515{
0bbfdfe8 2516 int rc;
70636773 2517
f24e9980
SW
2518 down_read(&osdc->map_sem);
2519 mutex_lock(&osdc->request_mutex);
0bbfdfe8
ID
2520
2521 rc = __ceph_osdc_start_request(osdc, req, nofail);
2522
f24e9980
SW
2523 mutex_unlock(&osdc->request_mutex);
2524 up_read(&osdc->map_sem);
0bbfdfe8 2525
f24e9980
SW
2526 return rc;
2527}
3d14c5d2 2528EXPORT_SYMBOL(ceph_osdc_start_request);
f24e9980 2529
c9f9b93d
ID
2530/*
2531 * Unregister a registered request. The request is not completed (i.e.
2532 * no callbacks or wakeups) - higher layers are supposed to know what
2533 * they are canceling.
2534 */
2535void ceph_osdc_cancel_request(struct ceph_osd_request *req)
2536{
2537 struct ceph_osd_client *osdc = req->r_osdc;
2538
2539 mutex_lock(&osdc->request_mutex);
2540 if (req->r_linger)
2541 __unregister_linger_request(osdc, req);
2542 __unregister_request(osdc, req);
2543 mutex_unlock(&osdc->request_mutex);
2544
2545 dout("%s %p tid %llu canceled\n", __func__, req, req->r_tid);
2546}
2547EXPORT_SYMBOL(ceph_osdc_cancel_request);
2548
f24e9980
SW
2549/*
2550 * wait for a request to complete
2551 */
2552int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2553 struct ceph_osd_request *req)
2554{
2555 int rc;
2556
c9f9b93d
ID
2557 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
2558
f24e9980
SW
2559 rc = wait_for_completion_interruptible(&req->r_completion);
2560 if (rc < 0) {
c9f9b93d
ID
2561 dout("%s %p tid %llu interrupted\n", __func__, req, req->r_tid);
2562 ceph_osdc_cancel_request(req);
25845472 2563 complete_request(req);
f24e9980
SW
2564 return rc;
2565 }
2566
c9f9b93d
ID
2567 dout("%s %p tid %llu result %d\n", __func__, req, req->r_tid,
2568 req->r_result);
f24e9980
SW
2569 return req->r_result;
2570}
3d14c5d2 2571EXPORT_SYMBOL(ceph_osdc_wait_request);
f24e9980
SW
2572
2573/*
2574 * sync - wait for all in-flight requests to flush. avoid starvation.
2575 */
2576void ceph_osdc_sync(struct ceph_osd_client *osdc)
2577{
2578 struct ceph_osd_request *req;
2579 u64 last_tid, next_tid = 0;
2580
2581 mutex_lock(&osdc->request_mutex);
2582 last_tid = osdc->last_tid;
2583 while (1) {
2584 req = __lookup_request_ge(osdc, next_tid);
2585 if (!req)
2586 break;
2587 if (req->r_tid > last_tid)
2588 break;
2589
2590 next_tid = req->r_tid + 1;
2591 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2592 continue;
2593
2594 ceph_osdc_get_request(req);
2595 mutex_unlock(&osdc->request_mutex);
2596 dout("sync waiting on tid %llu (last is %llu)\n",
2597 req->r_tid, last_tid);
2598 wait_for_completion(&req->r_safe_completion);
2599 mutex_lock(&osdc->request_mutex);
2600 ceph_osdc_put_request(req);
2601 }
2602 mutex_unlock(&osdc->request_mutex);
2603 dout("sync done (thru tid %llu)\n", last_tid);
2604}
3d14c5d2 2605EXPORT_SYMBOL(ceph_osdc_sync);
f24e9980 2606
dd935f44
JD
2607/*
2608 * Call all pending notify callbacks - for use after a watch is
2609 * unregistered, to make sure no more callbacks for it will be invoked
2610 */
f6479449 2611void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
dd935f44
JD
2612{
2613 flush_workqueue(osdc->notify_wq);
2614}
2615EXPORT_SYMBOL(ceph_osdc_flush_notifies);
2616
2617
f24e9980
SW
2618/*
2619 * init, shutdown
2620 */
2621int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2622{
2623 int err;
2624
2625 dout("init\n");
2626 osdc->client = client;
2627 osdc->osdmap = NULL;
2628 init_rwsem(&osdc->map_sem);
2629 init_completion(&osdc->map_waiters);
2630 osdc->last_requested_map = 0;
2631 mutex_init(&osdc->request_mutex);
f24e9980
SW
2632 osdc->last_tid = 0;
2633 osdc->osds = RB_ROOT;
f5a2041b 2634 INIT_LIST_HEAD(&osdc->osd_lru);
f24e9980 2635 osdc->requests = RB_ROOT;
422d2cb8 2636 INIT_LIST_HEAD(&osdc->req_lru);
6f6c7006
SW
2637 INIT_LIST_HEAD(&osdc->req_unsent);
2638 INIT_LIST_HEAD(&osdc->req_notarget);
a40c4f10 2639 INIT_LIST_HEAD(&osdc->req_linger);
f24e9980
SW
2640 osdc->num_requests = 0;
2641 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
f5a2041b 2642 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
a40c4f10
YS
2643 spin_lock_init(&osdc->event_lock);
2644 osdc->event_tree = RB_ROOT;
2645 osdc->event_count = 0;
f5a2041b
YS
2646
2647 schedule_delayed_work(&osdc->osds_timeout_work,
a319bf56 2648 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
f24e9980 2649
5f44f142 2650 err = -ENOMEM;
f24e9980
SW
2651 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2652 sizeof(struct ceph_osd_request));
2653 if (!osdc->req_mempool)
5f44f142 2654 goto out;
f24e9980 2655
d50b409f
SW
2656 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2657 OSD_OP_FRONT_LEN, 10, true,
4f48280e 2658 "osd_op");
f24e9980 2659 if (err < 0)
5f44f142 2660 goto out_mempool;
d50b409f 2661 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
4f48280e
SW
2662 OSD_OPREPLY_FRONT_LEN, 10, true,
2663 "osd_op_reply");
c16e7869
SW
2664 if (err < 0)
2665 goto out_msgpool;
a40c4f10 2666
dbcae088 2667 err = -ENOMEM;
a40c4f10 2668 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
dbcae088 2669 if (!osdc->notify_wq)
c172ec5c
ID
2670 goto out_msgpool_reply;
2671
f24e9980 2672 return 0;
5f44f142 2673
c172ec5c
ID
2674out_msgpool_reply:
2675 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
c16e7869
SW
2676out_msgpool:
2677 ceph_msgpool_destroy(&osdc->msgpool_op);
5f44f142
SW
2678out_mempool:
2679 mempool_destroy(osdc->req_mempool);
2680out:
2681 return err;
f24e9980
SW
2682}
2683
2684void ceph_osdc_stop(struct ceph_osd_client *osdc)
2685{
a40c4f10
YS
2686 flush_workqueue(osdc->notify_wq);
2687 destroy_workqueue(osdc->notify_wq);
f24e9980 2688 cancel_delayed_work_sync(&osdc->timeout_work);
f5a2041b 2689 cancel_delayed_work_sync(&osdc->osds_timeout_work);
f24e9980
SW
2690 if (osdc->osdmap) {
2691 ceph_osdmap_destroy(osdc->osdmap);
2692 osdc->osdmap = NULL;
2693 }
aca420bc 2694 remove_all_osds(osdc);
f24e9980
SW
2695 mempool_destroy(osdc->req_mempool);
2696 ceph_msgpool_destroy(&osdc->msgpool_op);
c16e7869 2697 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
f24e9980
SW
2698}
2699
2700/*
2701 * Read some contiguous pages. If we cross a stripe boundary, shorten
2702 * *plen. Return number of bytes read, or error.
2703 */
2704int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2705 struct ceph_vino vino, struct ceph_file_layout *layout,
2706 u64 off, u64 *plen,
2707 u32 truncate_seq, u64 truncate_size,
b7495fc2 2708 struct page **pages, int num_pages, int page_align)
f24e9980
SW
2709{
2710 struct ceph_osd_request *req;
2711 int rc = 0;
2712
2713 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2714 vino.snap, off, *plen);
715e4cd4 2715 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
f24e9980 2716 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
acead002 2717 NULL, truncate_seq, truncate_size,
153e5167 2718 false);
6816282d
SW
2719 if (IS_ERR(req))
2720 return PTR_ERR(req);
f24e9980
SW
2721
2722 /* it may be a short read due to an object boundary */
0fff87ec 2723
406e2c9f 2724 osd_req_op_extent_osd_data_pages(req, 0,
a4ce40a9 2725 pages, *plen, page_align, false, false);
f24e9980 2726
e0c59487 2727 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
43bfe5de 2728 off, *plen, *plen, page_align);
f24e9980 2729
79528734 2730 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
02ee07d3 2731
f24e9980
SW
2732 rc = ceph_osdc_start_request(osdc, req, false);
2733 if (!rc)
2734 rc = ceph_osdc_wait_request(osdc, req);
2735
2736 ceph_osdc_put_request(req);
2737 dout("readpages result %d\n", rc);
2738 return rc;
2739}
3d14c5d2 2740EXPORT_SYMBOL(ceph_osdc_readpages);
f24e9980
SW
2741
2742/*
2743 * do a synchronous write on N pages
2744 */
2745int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2746 struct ceph_file_layout *layout,
2747 struct ceph_snap_context *snapc,
2748 u64 off, u64 len,
2749 u32 truncate_seq, u64 truncate_size,
2750 struct timespec *mtime,
24808826 2751 struct page **pages, int num_pages)
f24e9980
SW
2752{
2753 struct ceph_osd_request *req;
2754 int rc = 0;
b7495fc2 2755 int page_align = off & ~PAGE_MASK;
f24e9980 2756
acead002 2757 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
715e4cd4 2758 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
f24e9980 2759 CEPH_OSD_OP_WRITE,
24808826 2760 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
acead002 2761 snapc, truncate_seq, truncate_size,
153e5167 2762 true);
6816282d
SW
2763 if (IS_ERR(req))
2764 return PTR_ERR(req);
f24e9980
SW
2765
2766 /* it may be a short write due to an object boundary */
406e2c9f 2767 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
43bfe5de
AE
2768 false, false);
2769 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
f24e9980 2770
79528734 2771 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
02ee07d3 2772
87f979d3 2773 rc = ceph_osdc_start_request(osdc, req, true);
f24e9980
SW
2774 if (!rc)
2775 rc = ceph_osdc_wait_request(osdc, req);
2776
2777 ceph_osdc_put_request(req);
2778 if (rc == 0)
2779 rc = len;
2780 dout("writepages result %d\n", rc);
2781 return rc;
2782}
3d14c5d2 2783EXPORT_SYMBOL(ceph_osdc_writepages);
f24e9980 2784
5522ae0b
AE
2785int ceph_osdc_setup(void)
2786{
2787 BUG_ON(ceph_osd_request_cache);
2788 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request",
2789 sizeof (struct ceph_osd_request),
2790 __alignof__(struct ceph_osd_request),
2791 0, NULL);
2792
2793 return ceph_osd_request_cache ? 0 : -ENOMEM;
2794}
2795EXPORT_SYMBOL(ceph_osdc_setup);
2796
2797void ceph_osdc_cleanup(void)
2798{
2799 BUG_ON(!ceph_osd_request_cache);
2800 kmem_cache_destroy(ceph_osd_request_cache);
2801 ceph_osd_request_cache = NULL;
2802}
2803EXPORT_SYMBOL(ceph_osdc_cleanup);
2804
f24e9980
SW
2805/*
2806 * handle incoming message
2807 */
2808static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2809{
2810 struct ceph_osd *osd = con->private;
32c895e7 2811 struct ceph_osd_client *osdc;
f24e9980
SW
2812 int type = le16_to_cpu(msg->hdr.type);
2813
2814 if (!osd)
4a32f93d 2815 goto out;
32c895e7 2816 osdc = osd->o_osdc;
f24e9980
SW
2817
2818 switch (type) {
2819 case CEPH_MSG_OSD_MAP:
2820 ceph_osdc_handle_map(osdc, msg);
2821 break;
2822 case CEPH_MSG_OSD_OPREPLY:
70cf052d 2823 handle_reply(osdc, msg);
f24e9980 2824 break;
a40c4f10
YS
2825 case CEPH_MSG_WATCH_NOTIFY:
2826 handle_watch_notify(osdc, msg);
2827 break;
f24e9980
SW
2828
2829 default:
2830 pr_err("received unknown message type %d %s\n", type,
2831 ceph_msg_type_name(type));
2832 }
4a32f93d 2833out:
f24e9980
SW
2834 ceph_msg_put(msg);
2835}
2836
5b3a4db3 2837/*
d15f9d69
ID
2838 * Lookup and return message for incoming reply. Don't try to do
2839 * anything about a larger than preallocated data portion of the
2840 * message at the moment - for now, just skip the message.
5b3a4db3
SW
2841 */
2842static struct ceph_msg *get_reply(struct ceph_connection *con,
2450418c
YS
2843 struct ceph_msg_header *hdr,
2844 int *skip)
f24e9980
SW
2845{
2846 struct ceph_osd *osd = con->private;
2847 struct ceph_osd_client *osdc = osd->o_osdc;
2450418c 2848 struct ceph_msg *m;
0547a9b3 2849 struct ceph_osd_request *req;
3f0a4ac5 2850 int front_len = le32_to_cpu(hdr->front_len);
5b3a4db3 2851 int data_len = le32_to_cpu(hdr->data_len);
0547a9b3 2852 u64 tid;
f24e9980 2853
0547a9b3
YS
2854 tid = le64_to_cpu(hdr->tid);
2855 mutex_lock(&osdc->request_mutex);
2856 req = __lookup_request(osdc, tid);
2857 if (!req) {
cd8140c6
ID
2858 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
2859 osd->o_osd, tid);
0547a9b3 2860 m = NULL;
d15f9d69 2861 *skip = 1;
0547a9b3
YS
2862 goto out;
2863 }
c16e7869 2864
ace6d3a9 2865 ceph_msg_revoke_incoming(req->r_reply);
0547a9b3 2866
f2be82b0 2867 if (front_len > req->r_reply->front_alloc_len) {
d15f9d69
ID
2868 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
2869 __func__, osd->o_osd, req->r_tid, front_len,
2870 req->r_reply->front_alloc_len);
3f0a4ac5
ID
2871 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
2872 false);
a79832f2 2873 if (!m)
c16e7869
SW
2874 goto out;
2875 ceph_msg_put(req->r_reply);
2876 req->r_reply = m;
2877 }
0fff87ec 2878
d15f9d69
ID
2879 if (data_len > req->r_reply->data_length) {
2880 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
2881 __func__, osd->o_osd, req->r_tid, data_len,
2882 req->r_reply->data_length);
2883 m = NULL;
2884 *skip = 1;
2885 goto out;
0547a9b3 2886 }
d15f9d69
ID
2887
2888 m = ceph_msg_get(req->r_reply);
c16e7869 2889 dout("get_reply tid %lld %p\n", tid, m);
0547a9b3
YS
2890
2891out:
2892 mutex_unlock(&osdc->request_mutex);
2450418c 2893 return m;
5b3a4db3
SW
2894}
2895
2896static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2897 struct ceph_msg_header *hdr,
2898 int *skip)
2899{
2900 struct ceph_osd *osd = con->private;
2901 int type = le16_to_cpu(hdr->type);
2902 int front = le32_to_cpu(hdr->front_len);
2903
1c20f2d2 2904 *skip = 0;
5b3a4db3
SW
2905 switch (type) {
2906 case CEPH_MSG_OSD_MAP:
a40c4f10 2907 case CEPH_MSG_WATCH_NOTIFY:
b61c2763 2908 return ceph_msg_new(type, front, GFP_NOFS, false);
5b3a4db3
SW
2909 case CEPH_MSG_OSD_OPREPLY:
2910 return get_reply(con, hdr, skip);
2911 default:
2912 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2913 osd->o_osd);
2914 *skip = 1;
2915 return NULL;
2916 }
f24e9980
SW
2917}
2918
2919/*
2920 * Wrappers to refcount containing ceph_osd struct
2921 */
2922static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2923{
2924 struct ceph_osd *osd = con->private;
2925 if (get_osd(osd))
2926 return con;
2927 return NULL;
2928}
2929
2930static void put_osd_con(struct ceph_connection *con)
2931{
2932 struct ceph_osd *osd = con->private;
2933 put_osd(osd);
2934}
2935
4e7a5dcd
SW
2936/*
2937 * authentication
2938 */
a3530df3
AE
2939/*
2940 * Note: returned pointer is the address of a structure that's
2941 * managed separately. Caller must *not* attempt to free it.
2942 */
2943static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
8f43fb53 2944 int *proto, int force_new)
4e7a5dcd
SW
2945{
2946 struct ceph_osd *o = con->private;
2947 struct ceph_osd_client *osdc = o->o_osdc;
2948 struct ceph_auth_client *ac = osdc->client->monc.auth;
74f1869f 2949 struct ceph_auth_handshake *auth = &o->o_auth;
4e7a5dcd 2950
74f1869f 2951 if (force_new && auth->authorizer) {
27859f97 2952 ceph_auth_destroy_authorizer(ac, auth->authorizer);
74f1869f
AE
2953 auth->authorizer = NULL;
2954 }
27859f97
SW
2955 if (!auth->authorizer) {
2956 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2957 auth);
4e7a5dcd 2958 if (ret)
a3530df3 2959 return ERR_PTR(ret);
27859f97
SW
2960 } else {
2961 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
0bed9b5c
SW
2962 auth);
2963 if (ret)
2964 return ERR_PTR(ret);
4e7a5dcd 2965 }
4e7a5dcd 2966 *proto = ac->protocol;
74f1869f 2967
a3530df3 2968 return auth;
4e7a5dcd
SW
2969}
2970
2971
2972static int verify_authorizer_reply(struct ceph_connection *con, int len)
2973{
2974 struct ceph_osd *o = con->private;
2975 struct ceph_osd_client *osdc = o->o_osdc;
2976 struct ceph_auth_client *ac = osdc->client->monc.auth;
2977
27859f97 2978 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
4e7a5dcd
SW
2979}
2980
9bd2e6f8
SW
2981static int invalidate_authorizer(struct ceph_connection *con)
2982{
2983 struct ceph_osd *o = con->private;
2984 struct ceph_osd_client *osdc = o->o_osdc;
2985 struct ceph_auth_client *ac = osdc->client->monc.auth;
2986
27859f97 2987 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
9bd2e6f8
SW
2988 return ceph_monc_validate_auth(&osdc->client->monc);
2989}
4e7a5dcd 2990
79dbd1ba 2991static int osd_sign_message(struct ceph_msg *msg)
33d07337 2992{
79dbd1ba 2993 struct ceph_osd *o = msg->con->private;
33d07337 2994 struct ceph_auth_handshake *auth = &o->o_auth;
79dbd1ba 2995
33d07337
YZ
2996 return ceph_auth_sign_message(auth, msg);
2997}
2998
79dbd1ba 2999static int osd_check_message_signature(struct ceph_msg *msg)
33d07337 3000{
79dbd1ba 3001 struct ceph_osd *o = msg->con->private;
33d07337 3002 struct ceph_auth_handshake *auth = &o->o_auth;
79dbd1ba 3003
33d07337
YZ
3004 return ceph_auth_check_message_signature(auth, msg);
3005}
3006
9e32789f 3007static const struct ceph_connection_operations osd_con_ops = {
f24e9980
SW
3008 .get = get_osd_con,
3009 .put = put_osd_con,
3010 .dispatch = dispatch,
4e7a5dcd
SW
3011 .get_authorizer = get_authorizer,
3012 .verify_authorizer_reply = verify_authorizer_reply,
9bd2e6f8 3013 .invalidate_authorizer = invalidate_authorizer,
f24e9980 3014 .alloc_msg = alloc_msg,
79dbd1ba
ID
3015 .sign_message = osd_sign_message,
3016 .check_message_signature = osd_check_message_signature,
81b024e7 3017 .fault = osd_reset,
f24e9980 3018};