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