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