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