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