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