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