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