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