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