libceph: init event->node in ceph_osdc_create_event()
[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);
685a7555 584 if (err)
6f6c7006
SW
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
f24e9980 611/*
81b024e7 612 * If the osd connection drops, we need to resubmit all requests.
f24e9980
SW
613 */
614static void osd_reset(struct ceph_connection *con)
615{
616 struct ceph_osd *osd = con->private;
617 struct ceph_osd_client *osdc;
618
619 if (!osd)
620 return;
621 dout("osd_reset osd%d\n", osd->o_osd);
622 osdc = osd->o_osdc;
f24e9980 623 down_read(&osdc->map_sem);
83aff95e
SW
624 mutex_lock(&osdc->request_mutex);
625 __kick_osd_requests(osdc, osd);
626 mutex_unlock(&osdc->request_mutex);
6f6c7006 627 send_queued(osdc);
f24e9980
SW
628 up_read(&osdc->map_sem);
629}
630
631/*
632 * Track open sessions with osds.
633 */
e10006f8 634static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
f24e9980
SW
635{
636 struct ceph_osd *osd;
637
638 osd = kzalloc(sizeof(*osd), GFP_NOFS);
639 if (!osd)
640 return NULL;
641
642 atomic_set(&osd->o_ref, 1);
643 osd->o_osdc = osdc;
e10006f8 644 osd->o_osd = onum;
f407731d 645 RB_CLEAR_NODE(&osd->o_node);
f24e9980 646 INIT_LIST_HEAD(&osd->o_requests);
a40c4f10 647 INIT_LIST_HEAD(&osd->o_linger_requests);
f5a2041b 648 INIT_LIST_HEAD(&osd->o_osd_lru);
f24e9980
SW
649 osd->o_incarnation = 1;
650
b7a9e5dd 651 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
4e7a5dcd 652
422d2cb8 653 INIT_LIST_HEAD(&osd->o_keepalive_item);
f24e9980
SW
654 return osd;
655}
656
657static struct ceph_osd *get_osd(struct ceph_osd *osd)
658{
659 if (atomic_inc_not_zero(&osd->o_ref)) {
660 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
661 atomic_read(&osd->o_ref));
662 return osd;
663 } else {
664 dout("get_osd %p FAIL\n", osd);
665 return NULL;
666 }
667}
668
669static void put_osd(struct ceph_osd *osd)
670{
671 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
672 atomic_read(&osd->o_ref) - 1);
a255651d 673 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
79494d1b
SW
674 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
675
a255651d 676 if (ac->ops && ac->ops->destroy_authorizer)
6c4a1915 677 ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
f24e9980 678 kfree(osd);
79494d1b 679 }
f24e9980
SW
680}
681
682/*
683 * remove an osd from our map
684 */
f5a2041b 685static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
f24e9980 686{
f5a2041b 687 dout("__remove_osd %p\n", osd);
f24e9980
SW
688 BUG_ON(!list_empty(&osd->o_requests));
689 rb_erase(&osd->o_node, &osdc->osds);
f5a2041b 690 list_del_init(&osd->o_osd_lru);
f24e9980
SW
691 ceph_con_close(&osd->o_con);
692 put_osd(osd);
693}
694
aca420bc
SW
695static void remove_all_osds(struct ceph_osd_client *osdc)
696{
048a9d2d 697 dout("%s %p\n", __func__, osdc);
aca420bc
SW
698 mutex_lock(&osdc->request_mutex);
699 while (!RB_EMPTY_ROOT(&osdc->osds)) {
700 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
701 struct ceph_osd, o_node);
702 __remove_osd(osdc, osd);
703 }
704 mutex_unlock(&osdc->request_mutex);
705}
706
f5a2041b
YS
707static void __move_osd_to_lru(struct ceph_osd_client *osdc,
708 struct ceph_osd *osd)
709{
710 dout("__move_osd_to_lru %p\n", osd);
711 BUG_ON(!list_empty(&osd->o_osd_lru));
712 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
3d14c5d2 713 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
f5a2041b
YS
714}
715
716static void __remove_osd_from_lru(struct ceph_osd *osd)
717{
718 dout("__remove_osd_from_lru %p\n", osd);
719 if (!list_empty(&osd->o_osd_lru))
720 list_del_init(&osd->o_osd_lru);
721}
722
aca420bc 723static void remove_old_osds(struct ceph_osd_client *osdc)
f5a2041b
YS
724{
725 struct ceph_osd *osd, *nosd;
726
727 dout("__remove_old_osds %p\n", osdc);
728 mutex_lock(&osdc->request_mutex);
729 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
aca420bc 730 if (time_before(jiffies, osd->lru_ttl))
f5a2041b
YS
731 break;
732 __remove_osd(osdc, osd);
733 }
734 mutex_unlock(&osdc->request_mutex);
735}
736
f24e9980
SW
737/*
738 * reset osd connect
739 */
f5a2041b 740static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
f24e9980 741{
87b315a5 742 struct ceph_osd_request *req;
f24e9980
SW
743 int ret = 0;
744
f5a2041b 745 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
a40c4f10
YS
746 if (list_empty(&osd->o_requests) &&
747 list_empty(&osd->o_linger_requests)) {
f5a2041b 748 __remove_osd(osdc, osd);
685a7555 749 ret = -ENODEV;
87b315a5
SW
750 } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd],
751 &osd->o_con.peer_addr,
752 sizeof(osd->o_con.peer_addr)) == 0 &&
753 !ceph_con_opened(&osd->o_con)) {
754 dout(" osd addr hasn't changed and connection never opened,"
755 " letting msgr retry");
756 /* touch each r_stamp for handle_timeout()'s benfit */
757 list_for_each_entry(req, &osd->o_requests, r_osd_item)
758 req->r_stamp = jiffies;
759 ret = -EAGAIN;
f24e9980
SW
760 } else {
761 ceph_con_close(&osd->o_con);
b7a9e5dd
SW
762 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
763 &osdc->osdmap->osd_addr[osd->o_osd]);
f24e9980
SW
764 osd->o_incarnation++;
765 }
766 return ret;
767}
768
769static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
770{
771 struct rb_node **p = &osdc->osds.rb_node;
772 struct rb_node *parent = NULL;
773 struct ceph_osd *osd = NULL;
774
aca420bc 775 dout("__insert_osd %p osd%d\n", new, new->o_osd);
f24e9980
SW
776 while (*p) {
777 parent = *p;
778 osd = rb_entry(parent, struct ceph_osd, o_node);
779 if (new->o_osd < osd->o_osd)
780 p = &(*p)->rb_left;
781 else if (new->o_osd > osd->o_osd)
782 p = &(*p)->rb_right;
783 else
784 BUG();
785 }
786
787 rb_link_node(&new->o_node, parent, p);
788 rb_insert_color(&new->o_node, &osdc->osds);
789}
790
791static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
792{
793 struct ceph_osd *osd;
794 struct rb_node *n = osdc->osds.rb_node;
795
796 while (n) {
797 osd = rb_entry(n, struct ceph_osd, o_node);
798 if (o < osd->o_osd)
799 n = n->rb_left;
800 else if (o > osd->o_osd)
801 n = n->rb_right;
802 else
803 return osd;
804 }
805 return NULL;
806}
807
422d2cb8
YS
808static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
809{
810 schedule_delayed_work(&osdc->timeout_work,
3d14c5d2 811 osdc->client->options->osd_keepalive_timeout * HZ);
422d2cb8
YS
812}
813
814static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
815{
816 cancel_delayed_work(&osdc->timeout_work);
817}
f24e9980
SW
818
819/*
820 * Register request, assign tid. If this is the first request, set up
821 * the timeout event.
822 */
a40c4f10
YS
823static void __register_request(struct ceph_osd_client *osdc,
824 struct ceph_osd_request *req)
f24e9980 825{
f24e9980 826 req->r_tid = ++osdc->last_tid;
6df058c0 827 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
77f38e0e 828 dout("__register_request %p tid %lld\n", req, req->r_tid);
f24e9980
SW
829 __insert_request(osdc, req);
830 ceph_osdc_get_request(req);
831 osdc->num_requests++;
f24e9980 832 if (osdc->num_requests == 1) {
422d2cb8
YS
833 dout(" first request, scheduling timeout\n");
834 __schedule_osd_timeout(osdc);
f24e9980 835 }
a40c4f10
YS
836}
837
838static void register_request(struct ceph_osd_client *osdc,
839 struct ceph_osd_request *req)
840{
841 mutex_lock(&osdc->request_mutex);
842 __register_request(osdc, req);
f24e9980
SW
843 mutex_unlock(&osdc->request_mutex);
844}
845
846/*
847 * called under osdc->request_mutex
848 */
849static void __unregister_request(struct ceph_osd_client *osdc,
850 struct ceph_osd_request *req)
851{
35f9f8a0
SW
852 if (RB_EMPTY_NODE(&req->r_node)) {
853 dout("__unregister_request %p tid %lld not registered\n",
854 req, req->r_tid);
855 return;
856 }
857
f24e9980
SW
858 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
859 rb_erase(&req->r_node, &osdc->requests);
860 osdc->num_requests--;
861
0ba6478d
SW
862 if (req->r_osd) {
863 /* make sure the original request isn't in flight. */
6740a845 864 ceph_msg_revoke(req->r_request);
0ba6478d
SW
865
866 list_del_init(&req->r_osd_item);
a40c4f10
YS
867 if (list_empty(&req->r_osd->o_requests) &&
868 list_empty(&req->r_osd->o_linger_requests)) {
869 dout("moving osd to %p lru\n", req->r_osd);
f5a2041b 870 __move_osd_to_lru(osdc, req->r_osd);
a40c4f10 871 }
fbdb9190 872 if (list_empty(&req->r_linger_item))
a40c4f10 873 req->r_osd = NULL;
0ba6478d 874 }
f24e9980 875
7d5f2481 876 list_del_init(&req->r_req_lru_item);
f24e9980
SW
877 ceph_osdc_put_request(req);
878
422d2cb8
YS
879 if (osdc->num_requests == 0) {
880 dout(" no requests, canceling timeout\n");
881 __cancel_osd_timeout(osdc);
f24e9980
SW
882 }
883}
884
885/*
886 * Cancel a previously queued request message
887 */
888static void __cancel_request(struct ceph_osd_request *req)
889{
6bc18876 890 if (req->r_sent && req->r_osd) {
6740a845 891 ceph_msg_revoke(req->r_request);
f24e9980
SW
892 req->r_sent = 0;
893 }
894}
895
a40c4f10
YS
896static void __register_linger_request(struct ceph_osd_client *osdc,
897 struct ceph_osd_request *req)
898{
899 dout("__register_linger_request %p\n", req);
900 list_add_tail(&req->r_linger_item, &osdc->req_linger);
6194ea89
SW
901 if (req->r_osd)
902 list_add_tail(&req->r_linger_osd,
903 &req->r_osd->o_linger_requests);
a40c4f10
YS
904}
905
906static void __unregister_linger_request(struct ceph_osd_client *osdc,
907 struct ceph_osd_request *req)
908{
909 dout("__unregister_linger_request %p\n", req);
61c74035 910 list_del_init(&req->r_linger_item);
a40c4f10 911 if (req->r_osd) {
a40c4f10
YS
912 list_del_init(&req->r_linger_osd);
913
914 if (list_empty(&req->r_osd->o_requests) &&
915 list_empty(&req->r_osd->o_linger_requests)) {
916 dout("moving osd to %p lru\n", req->r_osd);
917 __move_osd_to_lru(osdc, req->r_osd);
918 }
fbdb9190
SW
919 if (list_empty(&req->r_osd_item))
920 req->r_osd = NULL;
a40c4f10
YS
921 }
922}
923
924void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
925 struct ceph_osd_request *req)
926{
927 mutex_lock(&osdc->request_mutex);
928 if (req->r_linger) {
929 __unregister_linger_request(osdc, req);
930 ceph_osdc_put_request(req);
931 }
932 mutex_unlock(&osdc->request_mutex);
933}
934EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
935
936void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
937 struct ceph_osd_request *req)
938{
939 if (!req->r_linger) {
940 dout("set_request_linger %p\n", req);
941 req->r_linger = 1;
942 /*
943 * caller is now responsible for calling
944 * unregister_linger_request
945 */
946 ceph_osdc_get_request(req);
947 }
948}
949EXPORT_SYMBOL(ceph_osdc_set_request_linger);
950
f24e9980
SW
951/*
952 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
953 * (as needed), and set the request r_osd appropriately. If there is
25985edc 954 * no up osd, set r_osd to NULL. Move the request to the appropriate list
6f6c7006 955 * (unsent, homeless) or leave on in-flight lru.
f24e9980
SW
956 *
957 * Return 0 if unchanged, 1 if changed, or negative on error.
958 *
959 * Caller should hold map_sem for read and request_mutex.
960 */
6f6c7006 961static int __map_request(struct ceph_osd_client *osdc,
38d6453c 962 struct ceph_osd_request *req, int force_resend)
f24e9980
SW
963{
964 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
51042122 965 struct ceph_pg pgid;
d85b7056
SW
966 int acting[CEPH_PG_MAX_SIZE];
967 int o = -1, num = 0;
f24e9980 968 int err;
f24e9980 969
6f6c7006 970 dout("map_request %p tid %lld\n", req, req->r_tid);
f24e9980
SW
971 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
972 &req->r_file_layout, osdc->osdmap);
6f6c7006
SW
973 if (err) {
974 list_move(&req->r_req_lru_item, &osdc->req_notarget);
f24e9980 975 return err;
6f6c7006 976 }
51042122 977 pgid = reqhead->layout.ol_pgid;
7740a42f
SW
978 req->r_pgid = pgid;
979
d85b7056
SW
980 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
981 if (err > 0) {
982 o = acting[0];
983 num = err;
984 }
f24e9980 985
38d6453c
SW
986 if ((!force_resend &&
987 req->r_osd && req->r_osd->o_osd == o &&
d85b7056
SW
988 req->r_sent >= req->r_osd->o_incarnation &&
989 req->r_num_pg_osds == num &&
990 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
f24e9980
SW
991 (req->r_osd == NULL && o == -1))
992 return 0; /* no change */
993
6f6c7006 994 dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
51042122 995 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
f24e9980
SW
996 req->r_osd ? req->r_osd->o_osd : -1);
997
d85b7056
SW
998 /* record full pg acting set */
999 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1000 req->r_num_pg_osds = num;
1001
f24e9980
SW
1002 if (req->r_osd) {
1003 __cancel_request(req);
1004 list_del_init(&req->r_osd_item);
f24e9980
SW
1005 req->r_osd = NULL;
1006 }
1007
1008 req->r_osd = __lookup_osd(osdc, o);
1009 if (!req->r_osd && o >= 0) {
c99eb1c7 1010 err = -ENOMEM;
e10006f8 1011 req->r_osd = create_osd(osdc, o);
6f6c7006
SW
1012 if (!req->r_osd) {
1013 list_move(&req->r_req_lru_item, &osdc->req_notarget);
c99eb1c7 1014 goto out;
6f6c7006 1015 }
f24e9980 1016
6f6c7006 1017 dout("map_request osd %p is osd%d\n", req->r_osd, o);
f24e9980
SW
1018 __insert_osd(osdc, req->r_osd);
1019
b7a9e5dd
SW
1020 ceph_con_open(&req->r_osd->o_con,
1021 CEPH_ENTITY_TYPE_OSD, o,
1022 &osdc->osdmap->osd_addr[o]);
f24e9980
SW
1023 }
1024
f5a2041b
YS
1025 if (req->r_osd) {
1026 __remove_osd_from_lru(req->r_osd);
f24e9980 1027 list_add(&req->r_osd_item, &req->r_osd->o_requests);
6f6c7006
SW
1028 list_move(&req->r_req_lru_item, &osdc->req_unsent);
1029 } else {
1030 list_move(&req->r_req_lru_item, &osdc->req_notarget);
f5a2041b 1031 }
d85b7056 1032 err = 1; /* osd or pg changed */
f24e9980
SW
1033
1034out:
f24e9980
SW
1035 return err;
1036}
1037
1038/*
1039 * caller should hold map_sem (for read) and request_mutex
1040 */
56e925b6
SW
1041static void __send_request(struct ceph_osd_client *osdc,
1042 struct ceph_osd_request *req)
f24e9980
SW
1043{
1044 struct ceph_osd_request_head *reqhead;
f24e9980
SW
1045
1046 dout("send_request %p tid %llu to osd%d flags %d\n",
1047 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
1048
1049 reqhead = req->r_request->front.iov_base;
1050 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
1051 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
1052 reqhead->reassert_version = req->r_reassert_version;
1053
3dd72fc0 1054 req->r_stamp = jiffies;
07a27e22 1055 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
f24e9980
SW
1056
1057 ceph_msg_get(req->r_request); /* send consumes a ref */
1058 ceph_con_send(&req->r_osd->o_con, req->r_request);
1059 req->r_sent = req->r_osd->o_incarnation;
f24e9980
SW
1060}
1061
6f6c7006
SW
1062/*
1063 * Send any requests in the queue (req_unsent).
1064 */
1065static void send_queued(struct ceph_osd_client *osdc)
1066{
1067 struct ceph_osd_request *req, *tmp;
1068
1069 dout("send_queued\n");
1070 mutex_lock(&osdc->request_mutex);
1071 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
1072 __send_request(osdc, req);
1073 }
1074 mutex_unlock(&osdc->request_mutex);
1075}
1076
f24e9980
SW
1077/*
1078 * Timeout callback, called every N seconds when 1 or more osd
1079 * requests has been active for more than N seconds. When this
1080 * happens, we ping all OSDs with requests who have timed out to
1081 * ensure any communications channel reset is detected. Reset the
1082 * request timeouts another N seconds in the future as we go.
1083 * Reschedule the timeout event another N seconds in future (unless
1084 * there are no open requests).
1085 */
1086static void handle_timeout(struct work_struct *work)
1087{
1088 struct ceph_osd_client *osdc =
1089 container_of(work, struct ceph_osd_client, timeout_work.work);
83aff95e 1090 struct ceph_osd_request *req;
f24e9980 1091 struct ceph_osd *osd;
422d2cb8 1092 unsigned long keepalive =
3d14c5d2 1093 osdc->client->options->osd_keepalive_timeout * HZ;
422d2cb8 1094 struct list_head slow_osds;
f24e9980
SW
1095 dout("timeout\n");
1096 down_read(&osdc->map_sem);
1097
1098 ceph_monc_request_next_osdmap(&osdc->client->monc);
1099
1100 mutex_lock(&osdc->request_mutex);
f24e9980 1101
422d2cb8
YS
1102 /*
1103 * ping osds that are a bit slow. this ensures that if there
1104 * is a break in the TCP connection we will notice, and reopen
1105 * a connection with that osd (from the fault callback).
1106 */
1107 INIT_LIST_HEAD(&slow_osds);
1108 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
3dd72fc0 1109 if (time_before(jiffies, req->r_stamp + keepalive))
422d2cb8
YS
1110 break;
1111
1112 osd = req->r_osd;
1113 BUG_ON(!osd);
1114 dout(" tid %llu is slow, will send keepalive on osd%d\n",
f24e9980 1115 req->r_tid, osd->o_osd);
422d2cb8
YS
1116 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1117 }
1118 while (!list_empty(&slow_osds)) {
1119 osd = list_entry(slow_osds.next, struct ceph_osd,
1120 o_keepalive_item);
1121 list_del_init(&osd->o_keepalive_item);
f24e9980
SW
1122 ceph_con_keepalive(&osd->o_con);
1123 }
1124
422d2cb8 1125 __schedule_osd_timeout(osdc);
f24e9980 1126 mutex_unlock(&osdc->request_mutex);
6f6c7006 1127 send_queued(osdc);
f24e9980
SW
1128 up_read(&osdc->map_sem);
1129}
1130
f5a2041b
YS
1131static void handle_osds_timeout(struct work_struct *work)
1132{
1133 struct ceph_osd_client *osdc =
1134 container_of(work, struct ceph_osd_client,
1135 osds_timeout_work.work);
1136 unsigned long delay =
3d14c5d2 1137 osdc->client->options->osd_idle_ttl * HZ >> 2;
f5a2041b
YS
1138
1139 dout("osds timeout\n");
1140 down_read(&osdc->map_sem);
aca420bc 1141 remove_old_osds(osdc);
f5a2041b
YS
1142 up_read(&osdc->map_sem);
1143
1144 schedule_delayed_work(&osdc->osds_timeout_work,
1145 round_jiffies_relative(delay));
1146}
1147
25845472
SW
1148static void complete_request(struct ceph_osd_request *req)
1149{
1150 if (req->r_safe_callback)
1151 req->r_safe_callback(req, NULL);
1152 complete_all(&req->r_safe_completion); /* fsync waiter */
1153}
1154
f24e9980
SW
1155/*
1156 * handle osd op reply. either call the callback if it is specified,
1157 * or do the completion to wake up the waiting thread.
1158 */
350b1c32
SW
1159static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1160 struct ceph_connection *con)
f24e9980
SW
1161{
1162 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
1163 struct ceph_osd_request *req;
1164 u64 tid;
1165 int numops, object_len, flags;
0ceed5db 1166 s32 result;
f24e9980 1167
6df058c0 1168 tid = le64_to_cpu(msg->hdr.tid);
f24e9980
SW
1169 if (msg->front.iov_len < sizeof(*rhead))
1170 goto bad;
f24e9980
SW
1171 numops = le32_to_cpu(rhead->num_ops);
1172 object_len = le32_to_cpu(rhead->object_len);
0ceed5db 1173 result = le32_to_cpu(rhead->result);
f24e9980
SW
1174 if (msg->front.iov_len != sizeof(*rhead) + object_len +
1175 numops * sizeof(struct ceph_osd_op))
1176 goto bad;
0ceed5db 1177 dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
f24e9980
SW
1178 /* lookup */
1179 mutex_lock(&osdc->request_mutex);
1180 req = __lookup_request(osdc, tid);
1181 if (req == NULL) {
1182 dout("handle_reply tid %llu dne\n", tid);
1183 mutex_unlock(&osdc->request_mutex);
1184 return;
1185 }
1186 ceph_osdc_get_request(req);
1187 flags = le32_to_cpu(rhead->flags);
1188
350b1c32 1189 /*
0d59ab81 1190 * if this connection filled our message, drop our reference now, to
350b1c32
SW
1191 * avoid a (safe but slower) revoke later.
1192 */
0d59ab81 1193 if (req->r_con_filling_msg == con && req->r_reply == msg) {
c16e7869 1194 dout(" dropping con_filling_msg ref %p\n", con);
0d59ab81 1195 req->r_con_filling_msg = NULL;
0d47766f 1196 con->ops->put(con);
350b1c32
SW
1197 }
1198
f24e9980 1199 if (!req->r_got_reply) {
95c96174 1200 unsigned int bytes;
f24e9980
SW
1201
1202 req->r_result = le32_to_cpu(rhead->result);
1203 bytes = le32_to_cpu(msg->hdr.data_len);
1204 dout("handle_reply result %d bytes %d\n", req->r_result,
1205 bytes);
1206 if (req->r_result == 0)
1207 req->r_result = bytes;
1208
1209 /* in case this is a write and we need to replay, */
1210 req->r_reassert_version = rhead->reassert_version;
1211
1212 req->r_got_reply = 1;
1213 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1214 dout("handle_reply tid %llu dup ack\n", tid);
34b43a56 1215 mutex_unlock(&osdc->request_mutex);
f24e9980
SW
1216 goto done;
1217 }
1218
1219 dout("handle_reply tid %llu flags %d\n", tid, flags);
1220
a40c4f10
YS
1221 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1222 __register_linger_request(osdc, req);
1223
f24e9980 1224 /* either this is a read, or we got the safe response */
0ceed5db
SW
1225 if (result < 0 ||
1226 (flags & CEPH_OSD_FLAG_ONDISK) ||
f24e9980
SW
1227 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1228 __unregister_request(osdc, req);
1229
1230 mutex_unlock(&osdc->request_mutex);
1231
1232 if (req->r_callback)
1233 req->r_callback(req, msg);
1234 else
03066f23 1235 complete_all(&req->r_completion);
f24e9980 1236
25845472
SW
1237 if (flags & CEPH_OSD_FLAG_ONDISK)
1238 complete_request(req);
f24e9980
SW
1239
1240done:
a40c4f10 1241 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
f24e9980
SW
1242 ceph_osdc_put_request(req);
1243 return;
1244
1245bad:
1246 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1247 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
1248 (int)sizeof(*rhead));
9ec7cab1 1249 ceph_msg_dump(msg);
f24e9980
SW
1250}
1251
6f6c7006 1252static void reset_changed_osds(struct ceph_osd_client *osdc)
f24e9980 1253{
f24e9980 1254 struct rb_node *p, *n;
f24e9980 1255
6f6c7006
SW
1256 for (p = rb_first(&osdc->osds); p; p = n) {
1257 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
f24e9980 1258
6f6c7006
SW
1259 n = rb_next(p);
1260 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1261 memcmp(&osd->o_con.peer_addr,
1262 ceph_osd_addr(osdc->osdmap,
1263 osd->o_osd),
1264 sizeof(struct ceph_entity_addr)) != 0)
1265 __reset_osd(osdc, osd);
f24e9980 1266 }
422d2cb8
YS
1267}
1268
1269/*
6f6c7006
SW
1270 * Requeue requests whose mapping to an OSD has changed. If requests map to
1271 * no osd, request a new map.
422d2cb8
YS
1272 *
1273 * Caller should hold map_sem for read and request_mutex.
1274 */
38d6453c 1275static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
422d2cb8 1276{
a40c4f10 1277 struct ceph_osd_request *req, *nreq;
6f6c7006
SW
1278 struct rb_node *p;
1279 int needmap = 0;
1280 int err;
422d2cb8 1281
38d6453c 1282 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
422d2cb8 1283 mutex_lock(&osdc->request_mutex);
6194ea89 1284 for (p = rb_first(&osdc->requests); p; ) {
6f6c7006 1285 req = rb_entry(p, struct ceph_osd_request, r_node);
6194ea89 1286 p = rb_next(p);
38d6453c 1287 err = __map_request(osdc, req, force_resend);
6f6c7006
SW
1288 if (err < 0)
1289 continue; /* error */
1290 if (req->r_osd == NULL) {
1291 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1292 needmap++; /* request a newer map */
1293 } else if (err > 0) {
6194ea89
SW
1294 if (!req->r_linger) {
1295 dout("%p tid %llu requeued on osd%d\n", req,
1296 req->r_tid,
1297 req->r_osd ? req->r_osd->o_osd : -1);
a40c4f10 1298 req->r_flags |= CEPH_OSD_FLAG_RETRY;
6194ea89
SW
1299 }
1300 }
1301 if (req->r_linger && list_empty(&req->r_linger_item)) {
1302 /*
1303 * register as a linger so that we will
1304 * re-submit below and get a new tid
1305 */
1306 dout("%p tid %llu restart on osd%d\n",
1307 req, req->r_tid,
1308 req->r_osd ? req->r_osd->o_osd : -1);
1309 __register_linger_request(osdc, req);
1310 __unregister_request(osdc, req);
a40c4f10
YS
1311 }
1312 }
1313
1314 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1315 r_linger_item) {
1316 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1317
38d6453c 1318 err = __map_request(osdc, req, force_resend);
a40c4f10
YS
1319 if (err == 0)
1320 continue; /* no change and no osd was specified */
1321 if (err < 0)
1322 continue; /* hrm! */
1323 if (req->r_osd == NULL) {
1324 dout("tid %llu maps to no valid osd\n", req->r_tid);
1325 needmap++; /* request a newer map */
1326 continue;
6f6c7006 1327 }
a40c4f10
YS
1328
1329 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1330 req->r_osd ? req->r_osd->o_osd : -1);
1331 __unregister_linger_request(osdc, req);
1332 __register_request(osdc, req);
6f6c7006 1333 }
f24e9980
SW
1334 mutex_unlock(&osdc->request_mutex);
1335
1336 if (needmap) {
1337 dout("%d requests for down osds, need new map\n", needmap);
1338 ceph_monc_request_next_osdmap(&osdc->client->monc);
1339 }
422d2cb8 1340}
6f6c7006
SW
1341
1342
f24e9980
SW
1343/*
1344 * Process updated osd map.
1345 *
1346 * The message contains any number of incremental and full maps, normally
1347 * indicating some sort of topology change in the cluster. Kick requests
1348 * off to different OSDs as needed.
1349 */
1350void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1351{
1352 void *p, *end, *next;
1353 u32 nr_maps, maplen;
1354 u32 epoch;
1355 struct ceph_osdmap *newmap = NULL, *oldmap;
1356 int err;
1357 struct ceph_fsid fsid;
1358
1359 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1360 p = msg->front.iov_base;
1361 end = p + msg->front.iov_len;
1362
1363 /* verify fsid */
1364 ceph_decode_need(&p, end, sizeof(fsid), bad);
1365 ceph_decode_copy(&p, &fsid, sizeof(fsid));
0743304d
SW
1366 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1367 return;
f24e9980
SW
1368
1369 down_write(&osdc->map_sem);
1370
1371 /* incremental maps */
1372 ceph_decode_32_safe(&p, end, nr_maps, bad);
1373 dout(" %d inc maps\n", nr_maps);
1374 while (nr_maps > 0) {
1375 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
c89136ea
SW
1376 epoch = ceph_decode_32(&p);
1377 maplen = ceph_decode_32(&p);
f24e9980
SW
1378 ceph_decode_need(&p, end, maplen, bad);
1379 next = p + maplen;
1380 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1381 dout("applying incremental map %u len %d\n",
1382 epoch, maplen);
1383 newmap = osdmap_apply_incremental(&p, next,
1384 osdc->osdmap,
15d9882c 1385 &osdc->client->msgr);
f24e9980
SW
1386 if (IS_ERR(newmap)) {
1387 err = PTR_ERR(newmap);
1388 goto bad;
1389 }
30dc6381 1390 BUG_ON(!newmap);
f24e9980
SW
1391 if (newmap != osdc->osdmap) {
1392 ceph_osdmap_destroy(osdc->osdmap);
1393 osdc->osdmap = newmap;
1394 }
38d6453c 1395 kick_requests(osdc, 0);
6f6c7006 1396 reset_changed_osds(osdc);
f24e9980
SW
1397 } else {
1398 dout("ignoring incremental map %u len %d\n",
1399 epoch, maplen);
1400 }
1401 p = next;
1402 nr_maps--;
1403 }
1404 if (newmap)
1405 goto done;
1406
1407 /* full maps */
1408 ceph_decode_32_safe(&p, end, nr_maps, bad);
1409 dout(" %d full maps\n", nr_maps);
1410 while (nr_maps) {
1411 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
c89136ea
SW
1412 epoch = ceph_decode_32(&p);
1413 maplen = ceph_decode_32(&p);
f24e9980
SW
1414 ceph_decode_need(&p, end, maplen, bad);
1415 if (nr_maps > 1) {
1416 dout("skipping non-latest full map %u len %d\n",
1417 epoch, maplen);
1418 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1419 dout("skipping full map %u len %d, "
1420 "older than our %u\n", epoch, maplen,
1421 osdc->osdmap->epoch);
1422 } else {
38d6453c
SW
1423 int skipped_map = 0;
1424
f24e9980
SW
1425 dout("taking full map %u len %d\n", epoch, maplen);
1426 newmap = osdmap_decode(&p, p+maplen);
1427 if (IS_ERR(newmap)) {
1428 err = PTR_ERR(newmap);
1429 goto bad;
1430 }
30dc6381 1431 BUG_ON(!newmap);
f24e9980
SW
1432 oldmap = osdc->osdmap;
1433 osdc->osdmap = newmap;
38d6453c
SW
1434 if (oldmap) {
1435 if (oldmap->epoch + 1 < newmap->epoch)
1436 skipped_map = 1;
f24e9980 1437 ceph_osdmap_destroy(oldmap);
38d6453c
SW
1438 }
1439 kick_requests(osdc, skipped_map);
f24e9980
SW
1440 }
1441 p += maplen;
1442 nr_maps--;
1443 }
1444
1445done:
1446 downgrade_write(&osdc->map_sem);
1447 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
cd634fb6
SW
1448
1449 /*
1450 * subscribe to subsequent osdmap updates if full to ensure
1451 * we find out when we are no longer full and stop returning
1452 * ENOSPC.
1453 */
1454 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1455 ceph_monc_request_next_osdmap(&osdc->client->monc);
1456
6f6c7006 1457 send_queued(osdc);
f24e9980 1458 up_read(&osdc->map_sem);
03066f23 1459 wake_up_all(&osdc->client->auth_wq);
f24e9980
SW
1460 return;
1461
1462bad:
1463 pr_err("osdc handle_map corrupt msg\n");
9ec7cab1 1464 ceph_msg_dump(msg);
f24e9980
SW
1465 up_write(&osdc->map_sem);
1466 return;
1467}
1468
a40c4f10
YS
1469/*
1470 * watch/notify callback event infrastructure
1471 *
1472 * These callbacks are used both for watch and notify operations.
1473 */
1474static void __release_event(struct kref *kref)
1475{
1476 struct ceph_osd_event *event =
1477 container_of(kref, struct ceph_osd_event, kref);
1478
1479 dout("__release_event %p\n", event);
1480 kfree(event);
1481}
1482
1483static void get_event(struct ceph_osd_event *event)
1484{
1485 kref_get(&event->kref);
1486}
1487
1488void ceph_osdc_put_event(struct ceph_osd_event *event)
1489{
1490 kref_put(&event->kref, __release_event);
1491}
1492EXPORT_SYMBOL(ceph_osdc_put_event);
1493
1494static void __insert_event(struct ceph_osd_client *osdc,
1495 struct ceph_osd_event *new)
1496{
1497 struct rb_node **p = &osdc->event_tree.rb_node;
1498 struct rb_node *parent = NULL;
1499 struct ceph_osd_event *event = NULL;
1500
1501 while (*p) {
1502 parent = *p;
1503 event = rb_entry(parent, struct ceph_osd_event, node);
1504 if (new->cookie < event->cookie)
1505 p = &(*p)->rb_left;
1506 else if (new->cookie > event->cookie)
1507 p = &(*p)->rb_right;
1508 else
1509 BUG();
1510 }
1511
1512 rb_link_node(&new->node, parent, p);
1513 rb_insert_color(&new->node, &osdc->event_tree);
1514}
1515
1516static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1517 u64 cookie)
1518{
1519 struct rb_node **p = &osdc->event_tree.rb_node;
1520 struct rb_node *parent = NULL;
1521 struct ceph_osd_event *event = NULL;
1522
1523 while (*p) {
1524 parent = *p;
1525 event = rb_entry(parent, struct ceph_osd_event, node);
1526 if (cookie < event->cookie)
1527 p = &(*p)->rb_left;
1528 else if (cookie > event->cookie)
1529 p = &(*p)->rb_right;
1530 else
1531 return event;
1532 }
1533 return NULL;
1534}
1535
1536static void __remove_event(struct ceph_osd_event *event)
1537{
1538 struct ceph_osd_client *osdc = event->osdc;
1539
1540 if (!RB_EMPTY_NODE(&event->node)) {
1541 dout("__remove_event removed %p\n", event);
1542 rb_erase(&event->node, &osdc->event_tree);
1543 ceph_osdc_put_event(event);
1544 } else {
1545 dout("__remove_event didn't remove %p\n", event);
1546 }
1547}
1548
1549int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1550 void (*event_cb)(u64, u64, u8, void *),
1551 int one_shot, void *data,
1552 struct ceph_osd_event **pevent)
1553{
1554 struct ceph_osd_event *event;
1555
1556 event = kmalloc(sizeof(*event), GFP_NOIO);
1557 if (!event)
1558 return -ENOMEM;
1559
1560 dout("create_event %p\n", event);
1561 event->cb = event_cb;
1562 event->one_shot = one_shot;
1563 event->data = data;
1564 event->osdc = osdc;
1565 INIT_LIST_HEAD(&event->osd_node);
3ee5234d 1566 RB_CLEAR_NODE(&event->node);
a40c4f10
YS
1567 kref_init(&event->kref); /* one ref for us */
1568 kref_get(&event->kref); /* one ref for the caller */
1569 init_completion(&event->completion);
1570
1571 spin_lock(&osdc->event_lock);
1572 event->cookie = ++osdc->event_count;
1573 __insert_event(osdc, event);
1574 spin_unlock(&osdc->event_lock);
1575
1576 *pevent = event;
1577 return 0;
1578}
1579EXPORT_SYMBOL(ceph_osdc_create_event);
1580
1581void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1582{
1583 struct ceph_osd_client *osdc = event->osdc;
1584
1585 dout("cancel_event %p\n", event);
1586 spin_lock(&osdc->event_lock);
1587 __remove_event(event);
1588 spin_unlock(&osdc->event_lock);
1589 ceph_osdc_put_event(event); /* caller's */
1590}
1591EXPORT_SYMBOL(ceph_osdc_cancel_event);
1592
1593
1594static void do_event_work(struct work_struct *work)
1595{
1596 struct ceph_osd_event_work *event_work =
1597 container_of(work, struct ceph_osd_event_work, work);
1598 struct ceph_osd_event *event = event_work->event;
1599 u64 ver = event_work->ver;
1600 u64 notify_id = event_work->notify_id;
1601 u8 opcode = event_work->opcode;
1602
1603 dout("do_event_work completing %p\n", event);
1604 event->cb(ver, notify_id, opcode, event->data);
1605 complete(&event->completion);
1606 dout("do_event_work completed %p\n", event);
1607 ceph_osdc_put_event(event);
1608 kfree(event_work);
1609}
1610
1611
1612/*
1613 * Process osd watch notifications
1614 */
1615void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1616{
1617 void *p, *end;
1618 u8 proto_ver;
1619 u64 cookie, ver, notify_id;
1620 u8 opcode;
1621 struct ceph_osd_event *event;
1622 struct ceph_osd_event_work *event_work;
1623
1624 p = msg->front.iov_base;
1625 end = p + msg->front.iov_len;
1626
1627 ceph_decode_8_safe(&p, end, proto_ver, bad);
1628 ceph_decode_8_safe(&p, end, opcode, bad);
1629 ceph_decode_64_safe(&p, end, cookie, bad);
1630 ceph_decode_64_safe(&p, end, ver, bad);
1631 ceph_decode_64_safe(&p, end, notify_id, bad);
1632
1633 spin_lock(&osdc->event_lock);
1634 event = __find_event(osdc, cookie);
1635 if (event) {
1636 get_event(event);
1637 if (event->one_shot)
1638 __remove_event(event);
1639 }
1640 spin_unlock(&osdc->event_lock);
1641 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1642 cookie, ver, event);
1643 if (event) {
1644 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
a40c4f10
YS
1645 if (!event_work) {
1646 dout("ERROR: could not allocate event_work\n");
1647 goto done_err;
1648 }
6b0ae409 1649 INIT_WORK(&event_work->work, do_event_work);
a40c4f10
YS
1650 event_work->event = event;
1651 event_work->ver = ver;
1652 event_work->notify_id = notify_id;
1653 event_work->opcode = opcode;
1654 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1655 dout("WARNING: failed to queue notify event work\n");
1656 goto done_err;
1657 }
1658 }
1659
1660 return;
1661
1662done_err:
1663 complete(&event->completion);
1664 ceph_osdc_put_event(event);
1665 return;
1666
1667bad:
1668 pr_err("osdc handle_watch_notify corrupt msg\n");
1669 return;
1670}
1671
1672int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
1673{
1674 int err;
1675
1676 dout("wait_event %p\n", event);
1677 err = wait_for_completion_interruptible_timeout(&event->completion,
1678 timeout * HZ);
1679 ceph_osdc_put_event(event);
1680 if (err > 0)
1681 err = 0;
1682 dout("wait_event %p returns %d\n", event, err);
1683 return err;
1684}
1685EXPORT_SYMBOL(ceph_osdc_wait_event);
1686
f24e9980
SW
1687/*
1688 * Register request, send initial attempt.
1689 */
1690int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1691 struct ceph_osd_request *req,
1692 bool nofail)
1693{
c1ea8823 1694 int rc = 0;
f24e9980
SW
1695
1696 req->r_request->pages = req->r_pages;
1697 req->r_request->nr_pages = req->r_num_pages;
68b4476b
YS
1698#ifdef CONFIG_BLOCK
1699 req->r_request->bio = req->r_bio;
1700#endif
1701 req->r_request->trail = req->r_trail;
f24e9980
SW
1702
1703 register_request(osdc, req);
1704
1705 down_read(&osdc->map_sem);
1706 mutex_lock(&osdc->request_mutex);
c1ea8823
SW
1707 /*
1708 * a racing kick_requests() may have sent the message for us
1709 * while we dropped request_mutex above, so only send now if
1710 * the request still han't been touched yet.
1711 */
1712 if (req->r_sent == 0) {
38d6453c 1713 rc = __map_request(osdc, req, 0);
9d6fcb08
SW
1714 if (rc < 0) {
1715 if (nofail) {
1716 dout("osdc_start_request failed map, "
1717 " will retry %lld\n", req->r_tid);
1718 rc = 0;
1719 }
234af26f 1720 goto out_unlock;
9d6fcb08 1721 }
6f6c7006
SW
1722 if (req->r_osd == NULL) {
1723 dout("send_request %p no up osds in pg\n", req);
1724 ceph_monc_request_next_osdmap(&osdc->client->monc);
1725 } else {
56e925b6 1726 __send_request(osdc, req);
f24e9980 1727 }
56e925b6 1728 rc = 0;
f24e9980 1729 }
234af26f
DC
1730
1731out_unlock:
f24e9980
SW
1732 mutex_unlock(&osdc->request_mutex);
1733 up_read(&osdc->map_sem);
1734 return rc;
1735}
3d14c5d2 1736EXPORT_SYMBOL(ceph_osdc_start_request);
f24e9980
SW
1737
1738/*
1739 * wait for a request to complete
1740 */
1741int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1742 struct ceph_osd_request *req)
1743{
1744 int rc;
1745
1746 rc = wait_for_completion_interruptible(&req->r_completion);
1747 if (rc < 0) {
1748 mutex_lock(&osdc->request_mutex);
1749 __cancel_request(req);
529cfcc4 1750 __unregister_request(osdc, req);
f24e9980 1751 mutex_unlock(&osdc->request_mutex);
25845472 1752 complete_request(req);
529cfcc4 1753 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
f24e9980
SW
1754 return rc;
1755 }
1756
1757 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1758 return req->r_result;
1759}
3d14c5d2 1760EXPORT_SYMBOL(ceph_osdc_wait_request);
f24e9980
SW
1761
1762/*
1763 * sync - wait for all in-flight requests to flush. avoid starvation.
1764 */
1765void ceph_osdc_sync(struct ceph_osd_client *osdc)
1766{
1767 struct ceph_osd_request *req;
1768 u64 last_tid, next_tid = 0;
1769
1770 mutex_lock(&osdc->request_mutex);
1771 last_tid = osdc->last_tid;
1772 while (1) {
1773 req = __lookup_request_ge(osdc, next_tid);
1774 if (!req)
1775 break;
1776 if (req->r_tid > last_tid)
1777 break;
1778
1779 next_tid = req->r_tid + 1;
1780 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1781 continue;
1782
1783 ceph_osdc_get_request(req);
1784 mutex_unlock(&osdc->request_mutex);
1785 dout("sync waiting on tid %llu (last is %llu)\n",
1786 req->r_tid, last_tid);
1787 wait_for_completion(&req->r_safe_completion);
1788 mutex_lock(&osdc->request_mutex);
1789 ceph_osdc_put_request(req);
1790 }
1791 mutex_unlock(&osdc->request_mutex);
1792 dout("sync done (thru tid %llu)\n", last_tid);
1793}
3d14c5d2 1794EXPORT_SYMBOL(ceph_osdc_sync);
f24e9980
SW
1795
1796/*
1797 * init, shutdown
1798 */
1799int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1800{
1801 int err;
1802
1803 dout("init\n");
1804 osdc->client = client;
1805 osdc->osdmap = NULL;
1806 init_rwsem(&osdc->map_sem);
1807 init_completion(&osdc->map_waiters);
1808 osdc->last_requested_map = 0;
1809 mutex_init(&osdc->request_mutex);
f24e9980
SW
1810 osdc->last_tid = 0;
1811 osdc->osds = RB_ROOT;
f5a2041b 1812 INIT_LIST_HEAD(&osdc->osd_lru);
f24e9980 1813 osdc->requests = RB_ROOT;
422d2cb8 1814 INIT_LIST_HEAD(&osdc->req_lru);
6f6c7006
SW
1815 INIT_LIST_HEAD(&osdc->req_unsent);
1816 INIT_LIST_HEAD(&osdc->req_notarget);
a40c4f10 1817 INIT_LIST_HEAD(&osdc->req_linger);
f24e9980
SW
1818 osdc->num_requests = 0;
1819 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
f5a2041b 1820 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
a40c4f10
YS
1821 spin_lock_init(&osdc->event_lock);
1822 osdc->event_tree = RB_ROOT;
1823 osdc->event_count = 0;
f5a2041b
YS
1824
1825 schedule_delayed_work(&osdc->osds_timeout_work,
3d14c5d2 1826 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
f24e9980 1827
5f44f142 1828 err = -ENOMEM;
f24e9980
SW
1829 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1830 sizeof(struct ceph_osd_request));
1831 if (!osdc->req_mempool)
5f44f142 1832 goto out;
f24e9980 1833
d50b409f
SW
1834 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
1835 OSD_OP_FRONT_LEN, 10, true,
4f48280e 1836 "osd_op");
f24e9980 1837 if (err < 0)
5f44f142 1838 goto out_mempool;
d50b409f 1839 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
4f48280e
SW
1840 OSD_OPREPLY_FRONT_LEN, 10, true,
1841 "osd_op_reply");
c16e7869
SW
1842 if (err < 0)
1843 goto out_msgpool;
a40c4f10
YS
1844
1845 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1846 if (IS_ERR(osdc->notify_wq)) {
1847 err = PTR_ERR(osdc->notify_wq);
1848 osdc->notify_wq = NULL;
1849 goto out_msgpool;
1850 }
f24e9980 1851 return 0;
5f44f142 1852
c16e7869
SW
1853out_msgpool:
1854 ceph_msgpool_destroy(&osdc->msgpool_op);
5f44f142
SW
1855out_mempool:
1856 mempool_destroy(osdc->req_mempool);
1857out:
1858 return err;
f24e9980 1859}
3d14c5d2 1860EXPORT_SYMBOL(ceph_osdc_init);
f24e9980
SW
1861
1862void ceph_osdc_stop(struct ceph_osd_client *osdc)
1863{
a40c4f10
YS
1864 flush_workqueue(osdc->notify_wq);
1865 destroy_workqueue(osdc->notify_wq);
f24e9980 1866 cancel_delayed_work_sync(&osdc->timeout_work);
f5a2041b 1867 cancel_delayed_work_sync(&osdc->osds_timeout_work);
f24e9980
SW
1868 if (osdc->osdmap) {
1869 ceph_osdmap_destroy(osdc->osdmap);
1870 osdc->osdmap = NULL;
1871 }
aca420bc 1872 remove_all_osds(osdc);
f24e9980
SW
1873 mempool_destroy(osdc->req_mempool);
1874 ceph_msgpool_destroy(&osdc->msgpool_op);
c16e7869 1875 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
f24e9980 1876}
3d14c5d2 1877EXPORT_SYMBOL(ceph_osdc_stop);
f24e9980
SW
1878
1879/*
1880 * Read some contiguous pages. If we cross a stripe boundary, shorten
1881 * *plen. Return number of bytes read, or error.
1882 */
1883int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1884 struct ceph_vino vino, struct ceph_file_layout *layout,
1885 u64 off, u64 *plen,
1886 u32 truncate_seq, u64 truncate_size,
b7495fc2 1887 struct page **pages, int num_pages, int page_align)
f24e9980
SW
1888{
1889 struct ceph_osd_request *req;
1890 int rc = 0;
1891
1892 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1893 vino.snap, off, *plen);
1894 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1895 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1896 NULL, 0, truncate_seq, truncate_size, NULL,
b7495fc2 1897 false, 1, page_align);
6816282d
SW
1898 if (IS_ERR(req))
1899 return PTR_ERR(req);
f24e9980
SW
1900
1901 /* it may be a short read due to an object boundary */
1902 req->r_pages = pages;
f24e9980 1903
b7495fc2
SW
1904 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1905 off, *plen, req->r_num_pages, page_align);
f24e9980
SW
1906
1907 rc = ceph_osdc_start_request(osdc, req, false);
1908 if (!rc)
1909 rc = ceph_osdc_wait_request(osdc, req);
1910
1911 ceph_osdc_put_request(req);
1912 dout("readpages result %d\n", rc);
1913 return rc;
1914}
3d14c5d2 1915EXPORT_SYMBOL(ceph_osdc_readpages);
f24e9980
SW
1916
1917/*
1918 * do a synchronous write on N pages
1919 */
1920int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1921 struct ceph_file_layout *layout,
1922 struct ceph_snap_context *snapc,
1923 u64 off, u64 len,
1924 u32 truncate_seq, u64 truncate_size,
1925 struct timespec *mtime,
1926 struct page **pages, int num_pages,
1927 int flags, int do_sync, bool nofail)
1928{
1929 struct ceph_osd_request *req;
1930 int rc = 0;
b7495fc2 1931 int page_align = off & ~PAGE_MASK;
f24e9980
SW
1932
1933 BUG_ON(vino.snap != CEPH_NOSNAP);
1934 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1935 CEPH_OSD_OP_WRITE,
1936 flags | CEPH_OSD_FLAG_ONDISK |
1937 CEPH_OSD_FLAG_WRITE,
1938 snapc, do_sync,
1939 truncate_seq, truncate_size, mtime,
b7495fc2 1940 nofail, 1, page_align);
6816282d
SW
1941 if (IS_ERR(req))
1942 return PTR_ERR(req);
f24e9980
SW
1943
1944 /* it may be a short write due to an object boundary */
1945 req->r_pages = pages;
f24e9980
SW
1946 dout("writepages %llu~%llu (%d pages)\n", off, len,
1947 req->r_num_pages);
1948
1949 rc = ceph_osdc_start_request(osdc, req, nofail);
1950 if (!rc)
1951 rc = ceph_osdc_wait_request(osdc, req);
1952
1953 ceph_osdc_put_request(req);
1954 if (rc == 0)
1955 rc = len;
1956 dout("writepages result %d\n", rc);
1957 return rc;
1958}
3d14c5d2 1959EXPORT_SYMBOL(ceph_osdc_writepages);
f24e9980
SW
1960
1961/*
1962 * handle incoming message
1963 */
1964static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1965{
1966 struct ceph_osd *osd = con->private;
32c895e7 1967 struct ceph_osd_client *osdc;
f24e9980
SW
1968 int type = le16_to_cpu(msg->hdr.type);
1969
1970 if (!osd)
4a32f93d 1971 goto out;
32c895e7 1972 osdc = osd->o_osdc;
f24e9980
SW
1973
1974 switch (type) {
1975 case CEPH_MSG_OSD_MAP:
1976 ceph_osdc_handle_map(osdc, msg);
1977 break;
1978 case CEPH_MSG_OSD_OPREPLY:
350b1c32 1979 handle_reply(osdc, msg, con);
f24e9980 1980 break;
a40c4f10
YS
1981 case CEPH_MSG_WATCH_NOTIFY:
1982 handle_watch_notify(osdc, msg);
1983 break;
f24e9980
SW
1984
1985 default:
1986 pr_err("received unknown message type %d %s\n", type,
1987 ceph_msg_type_name(type));
1988 }
4a32f93d 1989out:
f24e9980
SW
1990 ceph_msg_put(msg);
1991}
1992
5b3a4db3 1993/*
21b667f6
SW
1994 * lookup and return message for incoming reply. set up reply message
1995 * pages.
5b3a4db3
SW
1996 */
1997static struct ceph_msg *get_reply(struct ceph_connection *con,
2450418c
YS
1998 struct ceph_msg_header *hdr,
1999 int *skip)
f24e9980
SW
2000{
2001 struct ceph_osd *osd = con->private;
2002 struct ceph_osd_client *osdc = osd->o_osdc;
2450418c 2003 struct ceph_msg *m;
0547a9b3 2004 struct ceph_osd_request *req;
5b3a4db3
SW
2005 int front = le32_to_cpu(hdr->front_len);
2006 int data_len = le32_to_cpu(hdr->data_len);
0547a9b3 2007 u64 tid;
f24e9980 2008
0547a9b3
YS
2009 tid = le64_to_cpu(hdr->tid);
2010 mutex_lock(&osdc->request_mutex);
2011 req = __lookup_request(osdc, tid);
2012 if (!req) {
2013 *skip = 1;
2014 m = NULL;
756a16a5
SW
2015 dout("get_reply unknown tid %llu from osd%d\n", tid,
2016 osd->o_osd);
0547a9b3
YS
2017 goto out;
2018 }
c16e7869
SW
2019
2020 if (req->r_con_filling_msg) {
8921d114 2021 dout("%s revoking msg %p from old con %p\n", __func__,
c16e7869 2022 req->r_reply, req->r_con_filling_msg);
8921d114 2023 ceph_msg_revoke_incoming(req->r_reply);
0d47766f 2024 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
6f46cb29 2025 req->r_con_filling_msg = NULL;
0547a9b3
YS
2026 }
2027
c16e7869
SW
2028 if (front > req->r_reply->front.iov_len) {
2029 pr_warning("get_reply front %d > preallocated %d\n",
2030 front, (int)req->r_reply->front.iov_len);
b61c2763 2031 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
a79832f2 2032 if (!m)
c16e7869
SW
2033 goto out;
2034 ceph_msg_put(req->r_reply);
2035 req->r_reply = m;
2036 }
2037 m = ceph_msg_get(req->r_reply);
2038
0547a9b3 2039 if (data_len > 0) {
b7495fc2 2040 int want = calc_pages_for(req->r_page_alignment, data_len);
21b667f6
SW
2041
2042 if (unlikely(req->r_num_pages < want)) {
9bb0ce2b
SW
2043 pr_warning("tid %lld reply has %d bytes %d pages, we"
2044 " had only %d pages ready\n", tid, data_len,
2045 want, req->r_num_pages);
0547a9b3
YS
2046 *skip = 1;
2047 ceph_msg_put(m);
a79832f2 2048 m = NULL;
21b667f6 2049 goto out;
0547a9b3 2050 }
21b667f6
SW
2051 m->pages = req->r_pages;
2052 m->nr_pages = req->r_num_pages;
c5c6b19d 2053 m->page_alignment = req->r_page_alignment;
68b4476b
YS
2054#ifdef CONFIG_BLOCK
2055 m->bio = req->r_bio;
2056#endif
0547a9b3 2057 }
5b3a4db3 2058 *skip = 0;
0d47766f 2059 req->r_con_filling_msg = con->ops->get(con);
c16e7869 2060 dout("get_reply tid %lld %p\n", tid, m);
0547a9b3
YS
2061
2062out:
2063 mutex_unlock(&osdc->request_mutex);
2450418c 2064 return m;
5b3a4db3
SW
2065
2066}
2067
2068static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2069 struct ceph_msg_header *hdr,
2070 int *skip)
2071{
2072 struct ceph_osd *osd = con->private;
2073 int type = le16_to_cpu(hdr->type);
2074 int front = le32_to_cpu(hdr->front_len);
2075
1c20f2d2 2076 *skip = 0;
5b3a4db3
SW
2077 switch (type) {
2078 case CEPH_MSG_OSD_MAP:
a40c4f10 2079 case CEPH_MSG_WATCH_NOTIFY:
b61c2763 2080 return ceph_msg_new(type, front, GFP_NOFS, false);
5b3a4db3
SW
2081 case CEPH_MSG_OSD_OPREPLY:
2082 return get_reply(con, hdr, skip);
2083 default:
2084 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2085 osd->o_osd);
2086 *skip = 1;
2087 return NULL;
2088 }
f24e9980
SW
2089}
2090
2091/*
2092 * Wrappers to refcount containing ceph_osd struct
2093 */
2094static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2095{
2096 struct ceph_osd *osd = con->private;
2097 if (get_osd(osd))
2098 return con;
2099 return NULL;
2100}
2101
2102static void put_osd_con(struct ceph_connection *con)
2103{
2104 struct ceph_osd *osd = con->private;
2105 put_osd(osd);
2106}
2107
4e7a5dcd
SW
2108/*
2109 * authentication
2110 */
a3530df3
AE
2111/*
2112 * Note: returned pointer is the address of a structure that's
2113 * managed separately. Caller must *not* attempt to free it.
2114 */
2115static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
8f43fb53 2116 int *proto, int force_new)
4e7a5dcd
SW
2117{
2118 struct ceph_osd *o = con->private;
2119 struct ceph_osd_client *osdc = o->o_osdc;
2120 struct ceph_auth_client *ac = osdc->client->monc.auth;
74f1869f 2121 struct ceph_auth_handshake *auth = &o->o_auth;
4e7a5dcd 2122
74f1869f 2123 if (force_new && auth->authorizer) {
a255651d
AE
2124 if (ac->ops && ac->ops->destroy_authorizer)
2125 ac->ops->destroy_authorizer(ac, auth->authorizer);
74f1869f
AE
2126 auth->authorizer = NULL;
2127 }
a255651d 2128 if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
a3530df3
AE
2129 int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2130 auth);
4e7a5dcd 2131 if (ret)
a3530df3 2132 return ERR_PTR(ret);
4e7a5dcd 2133 }
4e7a5dcd 2134 *proto = ac->protocol;
74f1869f 2135
a3530df3 2136 return auth;
4e7a5dcd
SW
2137}
2138
2139
2140static int verify_authorizer_reply(struct ceph_connection *con, int len)
2141{
2142 struct ceph_osd *o = con->private;
2143 struct ceph_osd_client *osdc = o->o_osdc;
2144 struct ceph_auth_client *ac = osdc->client->monc.auth;
2145
a255651d
AE
2146 /*
2147 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2148 * XXX which do we do: succeed or fail?
2149 */
6c4a1915 2150 return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
4e7a5dcd
SW
2151}
2152
9bd2e6f8
SW
2153static int invalidate_authorizer(struct ceph_connection *con)
2154{
2155 struct ceph_osd *o = con->private;
2156 struct ceph_osd_client *osdc = o->o_osdc;
2157 struct ceph_auth_client *ac = osdc->client->monc.auth;
2158
a255651d 2159 if (ac->ops && ac->ops->invalidate_authorizer)
9bd2e6f8
SW
2160 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2161
2162 return ceph_monc_validate_auth(&osdc->client->monc);
2163}
4e7a5dcd 2164
9e32789f 2165static const struct ceph_connection_operations osd_con_ops = {
f24e9980
SW
2166 .get = get_osd_con,
2167 .put = put_osd_con,
2168 .dispatch = dispatch,
4e7a5dcd
SW
2169 .get_authorizer = get_authorizer,
2170 .verify_authorizer_reply = verify_authorizer_reply,
9bd2e6f8 2171 .invalidate_authorizer = invalidate_authorizer,
f24e9980 2172 .alloc_msg = alloc_msg,
81b024e7 2173 .fault = osd_reset,
f24e9980 2174};