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