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