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