ceph: use single osd op reply msg
[safe/jmp/linux-2.6] / fs / ceph / osd_client.c
1 #include "ceph_debug.h"
2
3 #include <linux/err.h>
4 #include <linux/highmem.h>
5 #include <linux/mm.h>
6 #include <linux/pagemap.h>
7 #include <linux/slab.h>
8 #include <linux/uaccess.h>
9
10 #include "super.h"
11 #include "osd_client.h"
12 #include "messenger.h"
13 #include "decode.h"
14 #include "auth.h"
15
16 #define OSD_OP_FRONT_LEN        4096
17 #define OSD_OPREPLY_FRONT_LEN   512
18
19 const static struct ceph_connection_operations osd_con_ops;
20
21 static void kick_requests(struct ceph_osd_client *osdc, struct ceph_osd *osd);
22
23 /*
24  * Implement client access to distributed object storage cluster.
25  *
26  * All data objects are stored within a cluster/cloud of OSDs, or
27  * "object storage devices."  (Note that Ceph OSDs have _nothing_ to
28  * do with the T10 OSD extensions to SCSI.)  Ceph OSDs are simply
29  * remote daemons serving up and coordinating consistent and safe
30  * access to storage.
31  *
32  * Cluster membership and the mapping of data objects onto storage devices
33  * are described by the osd map.
34  *
35  * We keep track of pending OSD requests (read, write), resubmit
36  * requests to different OSDs when the cluster topology/data layout
37  * change, or retry the affected requests when the communications
38  * channel with an OSD is reset.
39  */
40
41 /*
42  * calculate the mapping of a file extent onto an object, and fill out the
43  * request accordingly.  shorten extent as necessary if it crosses an
44  * object boundary.
45  *
46  * fill osd op in request message.
47  */
48 static void calc_layout(struct ceph_osd_client *osdc,
49                         struct ceph_vino vino, struct ceph_file_layout *layout,
50                         u64 off, u64 *plen,
51                         struct ceph_osd_request *req)
52 {
53         struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
54         struct ceph_osd_op *op = (void *)(reqhead + 1);
55         u64 orig_len = *plen;
56         u64 objoff, objlen;    /* extent in object */
57         u64 bno;
58
59         reqhead->snapid = cpu_to_le64(vino.snap);
60
61         /* object extent? */
62         ceph_calc_file_object_mapping(layout, off, plen, &bno,
63                                       &objoff, &objlen);
64         if (*plen < orig_len)
65                 dout(" skipping last %llu, final file extent %llu~%llu\n",
66                      orig_len - *plen, off, *plen);
67
68         sprintf(req->r_oid, "%llx.%08llx", vino.ino, bno);
69         req->r_oid_len = strlen(req->r_oid);
70
71         op->extent.offset = cpu_to_le64(objoff);
72         op->extent.length = cpu_to_le64(objlen);
73         req->r_num_pages = calc_pages_for(off, *plen);
74
75         dout("calc_layout %s (%d) %llu~%llu (%d pages)\n",
76              req->r_oid, req->r_oid_len, objoff, objlen, req->r_num_pages);
77 }
78
79 /*
80  * requests
81  */
82 void ceph_osdc_release_request(struct kref *kref)
83 {
84         struct ceph_osd_request *req = container_of(kref,
85                                                     struct ceph_osd_request,
86                                                     r_kref);
87
88         if (req->r_request)
89                 ceph_msg_put(req->r_request);
90         if (req->r_reply)
91                 ceph_msg_put(req->r_reply);
92         if (req->r_con_filling_msg) {
93                 dout("release_request revoking pages %p from con %p\n",
94                      req->r_pages, req->r_con_filling_msg);
95                 ceph_con_revoke_message(req->r_con_filling_msg,
96                                       req->r_reply);
97                 ceph_con_put(req->r_con_filling_msg);
98         }
99         if (req->r_own_pages)
100                 ceph_release_page_vector(req->r_pages,
101                                          req->r_num_pages);
102         ceph_put_snap_context(req->r_snapc);
103         if (req->r_mempool)
104                 mempool_free(req, req->r_osdc->req_mempool);
105         else
106                 kfree(req);
107 }
108
109 /*
110  * build new request AND message, calculate layout, and adjust file
111  * extent as needed.
112  *
113  * if the file was recently truncated, we include information about its
114  * old and new size so that the object can be updated appropriately.  (we
115  * avoid synchronously deleting truncated objects because it's slow.)
116  *
117  * if @do_sync, include a 'startsync' command so that the osd will flush
118  * data quickly.
119  */
120 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
121                                                struct ceph_file_layout *layout,
122                                                struct ceph_vino vino,
123                                                u64 off, u64 *plen,
124                                                int opcode, int flags,
125                                                struct ceph_snap_context *snapc,
126                                                int do_sync,
127                                                u32 truncate_seq,
128                                                u64 truncate_size,
129                                                struct timespec *mtime,
130                                                bool use_mempool, int num_reply)
131 {
132         struct ceph_osd_request *req;
133         struct ceph_msg *msg;
134         struct ceph_osd_request_head *head;
135         struct ceph_osd_op *op;
136         void *p;
137         int num_op = 1 + do_sync;
138         size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
139         int i;
140
141         if (use_mempool) {
142                 req = mempool_alloc(osdc->req_mempool, GFP_NOFS);
143                 memset(req, 0, sizeof(*req));
144         } else {
145                 req = kzalloc(sizeof(*req), GFP_NOFS);
146         }
147         if (req == NULL)
148                 return ERR_PTR(-ENOMEM);
149
150         req->r_osdc = osdc;
151         req->r_mempool = use_mempool;
152         kref_init(&req->r_kref);
153         init_completion(&req->r_completion);
154         init_completion(&req->r_safe_completion);
155         INIT_LIST_HEAD(&req->r_unsafe_item);
156         req->r_flags = flags;
157
158         WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
159
160         /* create reply message */
161         if (use_mempool)
162                 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
163         else
164                 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
165                                    OSD_OPREPLY_FRONT_LEN, 0, 0, NULL);
166         if (IS_ERR(msg)) {
167                 ceph_osdc_put_request(req);
168                 return ERR_PTR(PTR_ERR(msg));
169         }
170         req->r_reply = msg;
171
172         /* create request message; allow space for oid */
173         msg_size += 40;
174         if (snapc)
175                 msg_size += sizeof(u64) * snapc->num_snaps;
176         if (use_mempool)
177                 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
178         else
179                 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, 0, 0, NULL);
180         if (IS_ERR(msg)) {
181                 ceph_osdc_put_request(req);
182                 return ERR_PTR(PTR_ERR(msg));
183         }
184         msg->hdr.type = cpu_to_le16(CEPH_MSG_OSD_OP);
185         memset(msg->front.iov_base, 0, msg->front.iov_len);
186         head = msg->front.iov_base;
187         op = (void *)(head + 1);
188         p = (void *)(op + num_op);
189
190         req->r_request = msg;
191         req->r_snapc = ceph_get_snap_context(snapc);
192
193         head->client_inc = cpu_to_le32(1); /* always, for now. */
194         head->flags = cpu_to_le32(flags);
195         if (flags & CEPH_OSD_FLAG_WRITE)
196                 ceph_encode_timespec(&head->mtime, mtime);
197         head->num_ops = cpu_to_le16(num_op);
198         op->op = cpu_to_le16(opcode);
199
200         /* calculate max write size */
201         calc_layout(osdc, vino, layout, off, plen, req);
202         req->r_file_layout = *layout;  /* keep a copy */
203
204         if (flags & CEPH_OSD_FLAG_WRITE) {
205                 req->r_request->hdr.data_off = cpu_to_le16(off);
206                 req->r_request->hdr.data_len = cpu_to_le32(*plen);
207                 op->payload_len = cpu_to_le32(*plen);
208         }
209         op->extent.truncate_size = cpu_to_le64(truncate_size);
210         op->extent.truncate_seq = cpu_to_le32(truncate_seq);
211
212         /* fill in oid */
213         head->object_len = cpu_to_le32(req->r_oid_len);
214         memcpy(p, req->r_oid, req->r_oid_len);
215         p += req->r_oid_len;
216
217         if (do_sync) {
218                 op++;
219                 op->op = cpu_to_le16(CEPH_OSD_OP_STARTSYNC);
220         }
221         if (snapc) {
222                 head->snap_seq = cpu_to_le64(snapc->seq);
223                 head->num_snaps = cpu_to_le32(snapc->num_snaps);
224                 for (i = 0; i < snapc->num_snaps; i++) {
225                         put_unaligned_le64(snapc->snaps[i], p);
226                         p += sizeof(u64);
227                 }
228         }
229
230         BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
231         return req;
232 }
233
234 /*
235  * We keep osd requests in an rbtree, sorted by ->r_tid.
236  */
237 static void __insert_request(struct ceph_osd_client *osdc,
238                              struct ceph_osd_request *new)
239 {
240         struct rb_node **p = &osdc->requests.rb_node;
241         struct rb_node *parent = NULL;
242         struct ceph_osd_request *req = NULL;
243
244         while (*p) {
245                 parent = *p;
246                 req = rb_entry(parent, struct ceph_osd_request, r_node);
247                 if (new->r_tid < req->r_tid)
248                         p = &(*p)->rb_left;
249                 else if (new->r_tid > req->r_tid)
250                         p = &(*p)->rb_right;
251                 else
252                         BUG();
253         }
254
255         rb_link_node(&new->r_node, parent, p);
256         rb_insert_color(&new->r_node, &osdc->requests);
257 }
258
259 static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
260                                                  u64 tid)
261 {
262         struct ceph_osd_request *req;
263         struct rb_node *n = osdc->requests.rb_node;
264
265         while (n) {
266                 req = rb_entry(n, struct ceph_osd_request, r_node);
267                 if (tid < req->r_tid)
268                         n = n->rb_left;
269                 else if (tid > req->r_tid)
270                         n = n->rb_right;
271                 else
272                         return req;
273         }
274         return NULL;
275 }
276
277 static struct ceph_osd_request *
278 __lookup_request_ge(struct ceph_osd_client *osdc,
279                     u64 tid)
280 {
281         struct ceph_osd_request *req;
282         struct rb_node *n = osdc->requests.rb_node;
283
284         while (n) {
285                 req = rb_entry(n, struct ceph_osd_request, r_node);
286                 if (tid < req->r_tid) {
287                         if (!n->rb_left)
288                                 return req;
289                         n = n->rb_left;
290                 } else if (tid > req->r_tid) {
291                         n = n->rb_right;
292                 } else {
293                         return req;
294                 }
295         }
296         return NULL;
297 }
298
299
300 /*
301  * If the osd connection drops, we need to resubmit all requests.
302  */
303 static void osd_reset(struct ceph_connection *con)
304 {
305         struct ceph_osd *osd = con->private;
306         struct ceph_osd_client *osdc;
307
308         if (!osd)
309                 return;
310         dout("osd_reset osd%d\n", osd->o_osd);
311         osdc = osd->o_osdc;
312         down_read(&osdc->map_sem);
313         kick_requests(osdc, osd);
314         up_read(&osdc->map_sem);
315 }
316
317 /*
318  * Track open sessions with osds.
319  */
320 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc)
321 {
322         struct ceph_osd *osd;
323
324         osd = kzalloc(sizeof(*osd), GFP_NOFS);
325         if (!osd)
326                 return NULL;
327
328         atomic_set(&osd->o_ref, 1);
329         osd->o_osdc = osdc;
330         INIT_LIST_HEAD(&osd->o_requests);
331         INIT_LIST_HEAD(&osd->o_osd_lru);
332         osd->o_incarnation = 1;
333
334         ceph_con_init(osdc->client->msgr, &osd->o_con);
335         osd->o_con.private = osd;
336         osd->o_con.ops = &osd_con_ops;
337         osd->o_con.peer_name.type = CEPH_ENTITY_TYPE_OSD;
338
339         return osd;
340 }
341
342 static struct ceph_osd *get_osd(struct ceph_osd *osd)
343 {
344         if (atomic_inc_not_zero(&osd->o_ref)) {
345                 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
346                      atomic_read(&osd->o_ref));
347                 return osd;
348         } else {
349                 dout("get_osd %p FAIL\n", osd);
350                 return NULL;
351         }
352 }
353
354 static void put_osd(struct ceph_osd *osd)
355 {
356         dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
357              atomic_read(&osd->o_ref) - 1);
358         if (atomic_dec_and_test(&osd->o_ref))
359                 kfree(osd);
360 }
361
362 /*
363  * remove an osd from our map
364  */
365 static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
366 {
367         dout("__remove_osd %p\n", osd);
368         BUG_ON(!list_empty(&osd->o_requests));
369         rb_erase(&osd->o_node, &osdc->osds);
370         list_del_init(&osd->o_osd_lru);
371         ceph_con_close(&osd->o_con);
372         put_osd(osd);
373 }
374
375 static void __move_osd_to_lru(struct ceph_osd_client *osdc,
376                               struct ceph_osd *osd)
377 {
378         dout("__move_osd_to_lru %p\n", osd);
379         BUG_ON(!list_empty(&osd->o_osd_lru));
380         list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
381         osd->lru_ttl = jiffies + osdc->client->mount_args->osd_idle_ttl * HZ;
382 }
383
384 static void __remove_osd_from_lru(struct ceph_osd *osd)
385 {
386         dout("__remove_osd_from_lru %p\n", osd);
387         if (!list_empty(&osd->o_osd_lru))
388                 list_del_init(&osd->o_osd_lru);
389 }
390
391 static void remove_old_osds(struct ceph_osd_client *osdc, int remove_all)
392 {
393         struct ceph_osd *osd, *nosd;
394
395         dout("__remove_old_osds %p\n", osdc);
396         mutex_lock(&osdc->request_mutex);
397         list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
398                 if (!remove_all && time_before(jiffies, osd->lru_ttl))
399                         break;
400                 __remove_osd(osdc, osd);
401         }
402         mutex_unlock(&osdc->request_mutex);
403 }
404
405 /*
406  * reset osd connect
407  */
408 static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
409 {
410         int ret = 0;
411
412         dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
413         if (list_empty(&osd->o_requests)) {
414                 __remove_osd(osdc, osd);
415         } else {
416                 ceph_con_close(&osd->o_con);
417                 ceph_con_open(&osd->o_con, &osdc->osdmap->osd_addr[osd->o_osd]);
418                 osd->o_incarnation++;
419         }
420         return ret;
421 }
422
423 static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
424 {
425         struct rb_node **p = &osdc->osds.rb_node;
426         struct rb_node *parent = NULL;
427         struct ceph_osd *osd = NULL;
428
429         while (*p) {
430                 parent = *p;
431                 osd = rb_entry(parent, struct ceph_osd, o_node);
432                 if (new->o_osd < osd->o_osd)
433                         p = &(*p)->rb_left;
434                 else if (new->o_osd > osd->o_osd)
435                         p = &(*p)->rb_right;
436                 else
437                         BUG();
438         }
439
440         rb_link_node(&new->o_node, parent, p);
441         rb_insert_color(&new->o_node, &osdc->osds);
442 }
443
444 static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
445 {
446         struct ceph_osd *osd;
447         struct rb_node *n = osdc->osds.rb_node;
448
449         while (n) {
450                 osd = rb_entry(n, struct ceph_osd, o_node);
451                 if (o < osd->o_osd)
452                         n = n->rb_left;
453                 else if (o > osd->o_osd)
454                         n = n->rb_right;
455                 else
456                         return osd;
457         }
458         return NULL;
459 }
460
461
462 /*
463  * Register request, assign tid.  If this is the first request, set up
464  * the timeout event.
465  */
466 static void register_request(struct ceph_osd_client *osdc,
467                              struct ceph_osd_request *req)
468 {
469         mutex_lock(&osdc->request_mutex);
470         req->r_tid = ++osdc->last_tid;
471         req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
472
473         dout("register_request %p tid %lld\n", req, req->r_tid);
474         __insert_request(osdc, req);
475         ceph_osdc_get_request(req);
476         osdc->num_requests++;
477
478         req->r_timeout_stamp =
479                 jiffies + osdc->client->mount_args->osd_timeout*HZ;
480
481         if (osdc->num_requests == 1) {
482                 osdc->timeout_tid = req->r_tid;
483                 dout("  timeout on tid %llu at %lu\n", req->r_tid,
484                      req->r_timeout_stamp);
485                 schedule_delayed_work(&osdc->timeout_work,
486                       round_jiffies_relative(req->r_timeout_stamp - jiffies));
487         }
488         mutex_unlock(&osdc->request_mutex);
489 }
490
491 /*
492  * called under osdc->request_mutex
493  */
494 static void __unregister_request(struct ceph_osd_client *osdc,
495                                  struct ceph_osd_request *req)
496 {
497         dout("__unregister_request %p tid %lld\n", req, req->r_tid);
498         rb_erase(&req->r_node, &osdc->requests);
499         osdc->num_requests--;
500
501         if (req->r_osd) {
502                 /* make sure the original request isn't in flight. */
503                 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
504
505                 list_del_init(&req->r_osd_item);
506                 if (list_empty(&req->r_osd->o_requests))
507                         __move_osd_to_lru(osdc, req->r_osd);
508                 req->r_osd = NULL;
509         }
510
511         ceph_osdc_put_request(req);
512
513         if (req->r_tid == osdc->timeout_tid) {
514                 if (osdc->num_requests == 0) {
515                         dout("no requests, canceling timeout\n");
516                         osdc->timeout_tid = 0;
517                         cancel_delayed_work(&osdc->timeout_work);
518                 } else {
519                         req = rb_entry(rb_first(&osdc->requests),
520                                        struct ceph_osd_request, r_node);
521                         osdc->timeout_tid = req->r_tid;
522                         dout("rescheduled timeout on tid %llu at %lu\n",
523                              req->r_tid, req->r_timeout_stamp);
524                         schedule_delayed_work(&osdc->timeout_work,
525                               round_jiffies_relative(req->r_timeout_stamp -
526                                                      jiffies));
527                 }
528         }
529 }
530
531 /*
532  * Cancel a previously queued request message
533  */
534 static void __cancel_request(struct ceph_osd_request *req)
535 {
536         if (req->r_sent) {
537                 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
538                 req->r_sent = 0;
539         }
540 }
541
542 /*
543  * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
544  * (as needed), and set the request r_osd appropriately.  If there is
545  * no up osd, set r_osd to NULL.
546  *
547  * Return 0 if unchanged, 1 if changed, or negative on error.
548  *
549  * Caller should hold map_sem for read and request_mutex.
550  */
551 static int __map_osds(struct ceph_osd_client *osdc,
552                       struct ceph_osd_request *req)
553 {
554         struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
555         struct ceph_pg pgid;
556         int o = -1;
557         int err;
558
559         dout("map_osds %p tid %lld\n", req, req->r_tid);
560         err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
561                                       &req->r_file_layout, osdc->osdmap);
562         if (err)
563                 return err;
564         pgid = reqhead->layout.ol_pgid;
565         req->r_pgid = pgid;
566
567         o = ceph_calc_pg_primary(osdc->osdmap, pgid);
568
569         if ((req->r_osd && req->r_osd->o_osd == o &&
570              req->r_sent >= req->r_osd->o_incarnation) ||
571             (req->r_osd == NULL && o == -1))
572                 return 0;  /* no change */
573
574         dout("map_osds tid %llu pgid %d.%x osd%d (was osd%d)\n",
575              req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
576              req->r_osd ? req->r_osd->o_osd : -1);
577
578         if (req->r_osd) {
579                 __cancel_request(req);
580                 list_del_init(&req->r_osd_item);
581                 req->r_osd = NULL;
582         }
583
584         req->r_osd = __lookup_osd(osdc, o);
585         if (!req->r_osd && o >= 0) {
586                 err = -ENOMEM;
587                 req->r_osd = create_osd(osdc);
588                 if (!req->r_osd)
589                         goto out;
590
591                 dout("map_osds osd %p is osd%d\n", req->r_osd, o);
592                 req->r_osd->o_osd = o;
593                 req->r_osd->o_con.peer_name.num = cpu_to_le64(o);
594                 __insert_osd(osdc, req->r_osd);
595
596                 ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]);
597         }
598
599         if (req->r_osd) {
600                 __remove_osd_from_lru(req->r_osd);
601                 list_add(&req->r_osd_item, &req->r_osd->o_requests);
602         }
603         err = 1;   /* osd changed */
604
605 out:
606         return err;
607 }
608
609 /*
610  * caller should hold map_sem (for read) and request_mutex
611  */
612 static int __send_request(struct ceph_osd_client *osdc,
613                           struct ceph_osd_request *req)
614 {
615         struct ceph_osd_request_head *reqhead;
616         int err;
617
618         err = __map_osds(osdc, req);
619         if (err < 0)
620                 return err;
621         if (req->r_osd == NULL) {
622                 dout("send_request %p no up osds in pg\n", req);
623                 ceph_monc_request_next_osdmap(&osdc->client->monc);
624                 return 0;
625         }
626
627         dout("send_request %p tid %llu to osd%d flags %d\n",
628              req, req->r_tid, req->r_osd->o_osd, req->r_flags);
629
630         reqhead = req->r_request->front.iov_base;
631         reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
632         reqhead->flags |= cpu_to_le32(req->r_flags);  /* e.g., RETRY */
633         reqhead->reassert_version = req->r_reassert_version;
634
635         req->r_timeout_stamp = jiffies+osdc->client->mount_args->osd_timeout*HZ;
636
637         ceph_msg_get(req->r_request); /* send consumes a ref */
638         ceph_con_send(&req->r_osd->o_con, req->r_request);
639         req->r_sent = req->r_osd->o_incarnation;
640         return 0;
641 }
642
643 /*
644  * Timeout callback, called every N seconds when 1 or more osd
645  * requests has been active for more than N seconds.  When this
646  * happens, we ping all OSDs with requests who have timed out to
647  * ensure any communications channel reset is detected.  Reset the
648  * request timeouts another N seconds in the future as we go.
649  * Reschedule the timeout event another N seconds in future (unless
650  * there are no open requests).
651  */
652 static void handle_timeout(struct work_struct *work)
653 {
654         struct ceph_osd_client *osdc =
655                 container_of(work, struct ceph_osd_client, timeout_work.work);
656         struct ceph_osd_request *req;
657         struct ceph_osd *osd;
658         unsigned long timeout = osdc->client->mount_args->osd_timeout * HZ;
659         unsigned long next_timeout = timeout + jiffies;
660         struct rb_node *p;
661
662         dout("timeout\n");
663         down_read(&osdc->map_sem);
664
665         ceph_monc_request_next_osdmap(&osdc->client->monc);
666
667         mutex_lock(&osdc->request_mutex);
668         for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
669                 req = rb_entry(p, struct ceph_osd_request, r_node);
670
671                 if (req->r_resend) {
672                         int err;
673
674                         dout("osdc resending prev failed %lld\n", req->r_tid);
675                         err = __send_request(osdc, req);
676                         if (err)
677                                 dout("osdc failed again on %lld\n", req->r_tid);
678                         else
679                                 req->r_resend = false;
680                         continue;
681                 }
682         }
683         for (p = rb_first(&osdc->osds); p; p = rb_next(p)) {
684                 osd = rb_entry(p, struct ceph_osd, o_node);
685                 if (list_empty(&osd->o_requests))
686                         continue;
687                 req = list_first_entry(&osd->o_requests,
688                                        struct ceph_osd_request, r_osd_item);
689                 if (time_before(jiffies, req->r_timeout_stamp))
690                         continue;
691
692                 dout(" tid %llu (at least) timed out on osd%d\n",
693                      req->r_tid, osd->o_osd);
694                 req->r_timeout_stamp = next_timeout;
695                 ceph_con_keepalive(&osd->o_con);
696         }
697
698         if (osdc->timeout_tid)
699                 schedule_delayed_work(&osdc->timeout_work,
700                                       round_jiffies_relative(timeout));
701
702         mutex_unlock(&osdc->request_mutex);
703
704         up_read(&osdc->map_sem);
705 }
706
707 static void handle_osds_timeout(struct work_struct *work)
708 {
709         struct ceph_osd_client *osdc =
710                 container_of(work, struct ceph_osd_client,
711                              osds_timeout_work.work);
712         unsigned long delay =
713                 osdc->client->mount_args->osd_idle_ttl * HZ >> 2;
714
715         dout("osds timeout\n");
716         down_read(&osdc->map_sem);
717         remove_old_osds(osdc, 0);
718         up_read(&osdc->map_sem);
719
720         schedule_delayed_work(&osdc->osds_timeout_work,
721                               round_jiffies_relative(delay));
722 }
723
724 /*
725  * handle osd op reply.  either call the callback if it is specified,
726  * or do the completion to wake up the waiting thread.
727  */
728 static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
729                          struct ceph_connection *con)
730 {
731         struct ceph_osd_reply_head *rhead = msg->front.iov_base;
732         struct ceph_osd_request *req;
733         u64 tid;
734         int numops, object_len, flags;
735
736         tid = le64_to_cpu(msg->hdr.tid);
737         if (msg->front.iov_len < sizeof(*rhead))
738                 goto bad;
739         numops = le32_to_cpu(rhead->num_ops);
740         object_len = le32_to_cpu(rhead->object_len);
741         if (msg->front.iov_len != sizeof(*rhead) + object_len +
742             numops * sizeof(struct ceph_osd_op))
743                 goto bad;
744         dout("handle_reply %p tid %llu\n", msg, tid);
745
746         /* lookup */
747         mutex_lock(&osdc->request_mutex);
748         req = __lookup_request(osdc, tid);
749         if (req == NULL) {
750                 dout("handle_reply tid %llu dne\n", tid);
751                 mutex_unlock(&osdc->request_mutex);
752                 return;
753         }
754         ceph_osdc_get_request(req);
755         flags = le32_to_cpu(rhead->flags);
756
757         /*
758          * if this connection filled our message, drop our reference now, to
759          * avoid a (safe but slower) revoke later.
760          */
761         if (req->r_con_filling_msg == con && req->r_reply == msg) {
762                 dout(" dropping con_filling_msg ref %p\n", con);
763                 req->r_con_filling_msg = NULL;
764                 ceph_con_put(con);
765         }
766
767         if (!req->r_got_reply) {
768                 unsigned bytes;
769
770                 req->r_result = le32_to_cpu(rhead->result);
771                 bytes = le32_to_cpu(msg->hdr.data_len);
772                 dout("handle_reply result %d bytes %d\n", req->r_result,
773                      bytes);
774                 if (req->r_result == 0)
775                         req->r_result = bytes;
776
777                 /* in case this is a write and we need to replay, */
778                 req->r_reassert_version = rhead->reassert_version;
779
780                 req->r_got_reply = 1;
781         } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
782                 dout("handle_reply tid %llu dup ack\n", tid);
783                 mutex_unlock(&osdc->request_mutex);
784                 goto done;
785         }
786
787         dout("handle_reply tid %llu flags %d\n", tid, flags);
788
789         /* either this is a read, or we got the safe response */
790         if ((flags & CEPH_OSD_FLAG_ONDISK) ||
791             ((flags & CEPH_OSD_FLAG_WRITE) == 0))
792                 __unregister_request(osdc, req);
793
794         mutex_unlock(&osdc->request_mutex);
795
796         if (req->r_callback)
797                 req->r_callback(req, msg);
798         else
799                 complete(&req->r_completion);
800
801         if (flags & CEPH_OSD_FLAG_ONDISK) {
802                 if (req->r_safe_callback)
803                         req->r_safe_callback(req, msg);
804                 complete(&req->r_safe_completion);  /* fsync waiter */
805         }
806
807 done:
808         ceph_osdc_put_request(req);
809         return;
810
811 bad:
812         pr_err("corrupt osd_op_reply got %d %d expected %d\n",
813                (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
814                (int)sizeof(*rhead));
815         ceph_msg_dump(msg);
816 }
817
818
819 /*
820  * Resubmit osd requests whose osd or osd address has changed.  Request
821  * a new osd map if osds are down, or we are otherwise unable to determine
822  * how to direct a request.
823  *
824  * Close connections to down osds.
825  *
826  * If @who is specified, resubmit requests for that specific osd.
827  *
828  * Caller should hold map_sem for read and request_mutex.
829  */
830 static void kick_requests(struct ceph_osd_client *osdc,
831                           struct ceph_osd *kickosd)
832 {
833         struct ceph_osd_request *req;
834         struct rb_node *p, *n;
835         int needmap = 0;
836         int err;
837
838         dout("kick_requests osd%d\n", kickosd ? kickosd->o_osd : -1);
839         mutex_lock(&osdc->request_mutex);
840         if (kickosd) {
841                 __reset_osd(osdc, kickosd);
842         } else {
843                 for (p = rb_first(&osdc->osds); p; p = n) {
844                         struct ceph_osd *osd =
845                                 rb_entry(p, struct ceph_osd, o_node);
846
847                         n = rb_next(p);
848                         if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
849                             memcmp(&osd->o_con.peer_addr,
850                                    ceph_osd_addr(osdc->osdmap,
851                                                  osd->o_osd),
852                                    sizeof(struct ceph_entity_addr)) != 0)
853                                 __reset_osd(osdc, osd);
854                 }
855         }
856
857         for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
858                 req = rb_entry(p, struct ceph_osd_request, r_node);
859
860                 if (req->r_resend) {
861                         dout(" r_resend set on tid %llu\n", req->r_tid);
862                         __cancel_request(req);
863                         goto kick;
864                 }
865                 if (req->r_osd && kickosd == req->r_osd) {
866                         __cancel_request(req);
867                         goto kick;
868                 }
869
870                 err = __map_osds(osdc, req);
871                 if (err == 0)
872                         continue;  /* no change */
873                 if (err < 0) {
874                         /*
875                          * FIXME: really, we should set the request
876                          * error and fail if this isn't a 'nofail'
877                          * request, but that's a fair bit more
878                          * complicated to do.  So retry!
879                          */
880                         dout(" setting r_resend on %llu\n", req->r_tid);
881                         req->r_resend = true;
882                         continue;
883                 }
884                 if (req->r_osd == NULL) {
885                         dout("tid %llu maps to no valid osd\n", req->r_tid);
886                         needmap++;  /* request a newer map */
887                         continue;
888                 }
889
890 kick:
891                 dout("kicking %p tid %llu osd%d\n", req, req->r_tid,
892                      req->r_osd->o_osd);
893                 req->r_flags |= CEPH_OSD_FLAG_RETRY;
894                 err = __send_request(osdc, req);
895                 if (err) {
896                         dout(" setting r_resend on %llu\n", req->r_tid);
897                         req->r_resend = true;
898                 }
899         }
900         mutex_unlock(&osdc->request_mutex);
901
902         if (needmap) {
903                 dout("%d requests for down osds, need new map\n", needmap);
904                 ceph_monc_request_next_osdmap(&osdc->client->monc);
905         }
906 }
907
908 /*
909  * Process updated osd map.
910  *
911  * The message contains any number of incremental and full maps, normally
912  * indicating some sort of topology change in the cluster.  Kick requests
913  * off to different OSDs as needed.
914  */
915 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
916 {
917         void *p, *end, *next;
918         u32 nr_maps, maplen;
919         u32 epoch;
920         struct ceph_osdmap *newmap = NULL, *oldmap;
921         int err;
922         struct ceph_fsid fsid;
923
924         dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
925         p = msg->front.iov_base;
926         end = p + msg->front.iov_len;
927
928         /* verify fsid */
929         ceph_decode_need(&p, end, sizeof(fsid), bad);
930         ceph_decode_copy(&p, &fsid, sizeof(fsid));
931         if (ceph_check_fsid(osdc->client, &fsid) < 0)
932                 return;
933
934         down_write(&osdc->map_sem);
935
936         /* incremental maps */
937         ceph_decode_32_safe(&p, end, nr_maps, bad);
938         dout(" %d inc maps\n", nr_maps);
939         while (nr_maps > 0) {
940                 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
941                 epoch = ceph_decode_32(&p);
942                 maplen = ceph_decode_32(&p);
943                 ceph_decode_need(&p, end, maplen, bad);
944                 next = p + maplen;
945                 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
946                         dout("applying incremental map %u len %d\n",
947                              epoch, maplen);
948                         newmap = osdmap_apply_incremental(&p, next,
949                                                           osdc->osdmap,
950                                                           osdc->client->msgr);
951                         if (IS_ERR(newmap)) {
952                                 err = PTR_ERR(newmap);
953                                 goto bad;
954                         }
955                         BUG_ON(!newmap);
956                         if (newmap != osdc->osdmap) {
957                                 ceph_osdmap_destroy(osdc->osdmap);
958                                 osdc->osdmap = newmap;
959                         }
960                 } else {
961                         dout("ignoring incremental map %u len %d\n",
962                              epoch, maplen);
963                 }
964                 p = next;
965                 nr_maps--;
966         }
967         if (newmap)
968                 goto done;
969
970         /* full maps */
971         ceph_decode_32_safe(&p, end, nr_maps, bad);
972         dout(" %d full maps\n", nr_maps);
973         while (nr_maps) {
974                 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
975                 epoch = ceph_decode_32(&p);
976                 maplen = ceph_decode_32(&p);
977                 ceph_decode_need(&p, end, maplen, bad);
978                 if (nr_maps > 1) {
979                         dout("skipping non-latest full map %u len %d\n",
980                              epoch, maplen);
981                 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
982                         dout("skipping full map %u len %d, "
983                              "older than our %u\n", epoch, maplen,
984                              osdc->osdmap->epoch);
985                 } else {
986                         dout("taking full map %u len %d\n", epoch, maplen);
987                         newmap = osdmap_decode(&p, p+maplen);
988                         if (IS_ERR(newmap)) {
989                                 err = PTR_ERR(newmap);
990                                 goto bad;
991                         }
992                         BUG_ON(!newmap);
993                         oldmap = osdc->osdmap;
994                         osdc->osdmap = newmap;
995                         if (oldmap)
996                                 ceph_osdmap_destroy(oldmap);
997                 }
998                 p += maplen;
999                 nr_maps--;
1000         }
1001
1002 done:
1003         downgrade_write(&osdc->map_sem);
1004         ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1005         if (newmap)
1006                 kick_requests(osdc, NULL);
1007         up_read(&osdc->map_sem);
1008         return;
1009
1010 bad:
1011         pr_err("osdc handle_map corrupt msg\n");
1012         ceph_msg_dump(msg);
1013         up_write(&osdc->map_sem);
1014         return;
1015 }
1016
1017
1018 /*
1019  * A read request prepares specific pages that data is to be read into.
1020  * When a message is being read off the wire, we call prepare_pages to
1021  * find those pages.
1022  *  0 = success, -1 failure.
1023  */
1024 static int __prepare_pages(struct ceph_connection *con,
1025                          struct ceph_msg_header *hdr,
1026                          struct ceph_osd_request *req,
1027                          u64 tid,
1028                          struct ceph_msg *m)
1029 {
1030         struct ceph_osd *osd = con->private;
1031         struct ceph_osd_client *osdc;
1032         int ret = -1;
1033         int data_len = le32_to_cpu(hdr->data_len);
1034         unsigned data_off = le16_to_cpu(hdr->data_off);
1035
1036         int want = calc_pages_for(data_off & ~PAGE_MASK, data_len);
1037
1038         if (!osd)
1039                 return -1;
1040
1041         osdc = osd->o_osdc;
1042
1043         dout("__prepare_pages on msg %p tid %llu, has %d pages, want %d\n", m,
1044              tid, req->r_num_pages, want);
1045         if (unlikely(req->r_num_pages < want))
1046                 goto out;
1047         m->pages = req->r_pages;
1048         m->nr_pages = req->r_num_pages;
1049         ret = 0; /* success */
1050 out:
1051         BUG_ON(ret < 0 || m->nr_pages < want);
1052
1053         return ret;
1054 }
1055
1056 /*
1057  * Register request, send initial attempt.
1058  */
1059 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1060                             struct ceph_osd_request *req,
1061                             bool nofail)
1062 {
1063         int rc = 0;
1064
1065         req->r_request->pages = req->r_pages;
1066         req->r_request->nr_pages = req->r_num_pages;
1067
1068         register_request(osdc, req);
1069
1070         down_read(&osdc->map_sem);
1071         mutex_lock(&osdc->request_mutex);
1072         /*
1073          * a racing kick_requests() may have sent the message for us
1074          * while we dropped request_mutex above, so only send now if
1075          * the request still han't been touched yet.
1076          */
1077         if (req->r_sent == 0) {
1078                 rc = __send_request(osdc, req);
1079                 if (rc) {
1080                         if (nofail) {
1081                                 dout("osdc_start_request failed send, "
1082                                      " marking %lld\n", req->r_tid);
1083                                 req->r_resend = true;
1084                                 rc = 0;
1085                         } else {
1086                                 __unregister_request(osdc, req);
1087                         }
1088                 }
1089         }
1090         mutex_unlock(&osdc->request_mutex);
1091         up_read(&osdc->map_sem);
1092         return rc;
1093 }
1094
1095 /*
1096  * wait for a request to complete
1097  */
1098 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1099                            struct ceph_osd_request *req)
1100 {
1101         int rc;
1102
1103         rc = wait_for_completion_interruptible(&req->r_completion);
1104         if (rc < 0) {
1105                 mutex_lock(&osdc->request_mutex);
1106                 __cancel_request(req);
1107                 __unregister_request(osdc, req);
1108                 mutex_unlock(&osdc->request_mutex);
1109                 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
1110                 return rc;
1111         }
1112
1113         dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1114         return req->r_result;
1115 }
1116
1117 /*
1118  * sync - wait for all in-flight requests to flush.  avoid starvation.
1119  */
1120 void ceph_osdc_sync(struct ceph_osd_client *osdc)
1121 {
1122         struct ceph_osd_request *req;
1123         u64 last_tid, next_tid = 0;
1124
1125         mutex_lock(&osdc->request_mutex);
1126         last_tid = osdc->last_tid;
1127         while (1) {
1128                 req = __lookup_request_ge(osdc, next_tid);
1129                 if (!req)
1130                         break;
1131                 if (req->r_tid > last_tid)
1132                         break;
1133
1134                 next_tid = req->r_tid + 1;
1135                 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1136                         continue;
1137
1138                 ceph_osdc_get_request(req);
1139                 mutex_unlock(&osdc->request_mutex);
1140                 dout("sync waiting on tid %llu (last is %llu)\n",
1141                      req->r_tid, last_tid);
1142                 wait_for_completion(&req->r_safe_completion);
1143                 mutex_lock(&osdc->request_mutex);
1144                 ceph_osdc_put_request(req);
1145         }
1146         mutex_unlock(&osdc->request_mutex);
1147         dout("sync done (thru tid %llu)\n", last_tid);
1148 }
1149
1150 /*
1151  * init, shutdown
1152  */
1153 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1154 {
1155         int err;
1156
1157         dout("init\n");
1158         osdc->client = client;
1159         osdc->osdmap = NULL;
1160         init_rwsem(&osdc->map_sem);
1161         init_completion(&osdc->map_waiters);
1162         osdc->last_requested_map = 0;
1163         mutex_init(&osdc->request_mutex);
1164         osdc->timeout_tid = 0;
1165         osdc->last_tid = 0;
1166         osdc->osds = RB_ROOT;
1167         INIT_LIST_HEAD(&osdc->osd_lru);
1168         osdc->requests = RB_ROOT;
1169         osdc->num_requests = 0;
1170         INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
1171         INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
1172
1173         schedule_delayed_work(&osdc->osds_timeout_work,
1174            round_jiffies_relative(osdc->client->mount_args->osd_idle_ttl * HZ));
1175
1176         err = -ENOMEM;
1177         osdc->req_mempool = mempool_create_kmalloc_pool(10,
1178                                         sizeof(struct ceph_osd_request));
1179         if (!osdc->req_mempool)
1180                 goto out;
1181
1182         err = ceph_msgpool_init(&osdc->msgpool_op, OSD_OP_FRONT_LEN, 10, true);
1183         if (err < 0)
1184                 goto out_mempool;
1185         err = ceph_msgpool_init(&osdc->msgpool_op_reply,
1186                                 OSD_OPREPLY_FRONT_LEN, 10, true);
1187         if (err < 0)
1188                 goto out_msgpool;
1189         return 0;
1190
1191 out_msgpool:
1192         ceph_msgpool_destroy(&osdc->msgpool_op);
1193 out_mempool:
1194         mempool_destroy(osdc->req_mempool);
1195 out:
1196         return err;
1197 }
1198
1199 void ceph_osdc_stop(struct ceph_osd_client *osdc)
1200 {
1201         cancel_delayed_work_sync(&osdc->timeout_work);
1202         cancel_delayed_work_sync(&osdc->osds_timeout_work);
1203         if (osdc->osdmap) {
1204                 ceph_osdmap_destroy(osdc->osdmap);
1205                 osdc->osdmap = NULL;
1206         }
1207         remove_old_osds(osdc, 1);
1208         mempool_destroy(osdc->req_mempool);
1209         ceph_msgpool_destroy(&osdc->msgpool_op);
1210         ceph_msgpool_destroy(&osdc->msgpool_op_reply);
1211 }
1212
1213 /*
1214  * Read some contiguous pages.  If we cross a stripe boundary, shorten
1215  * *plen.  Return number of bytes read, or error.
1216  */
1217 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1218                         struct ceph_vino vino, struct ceph_file_layout *layout,
1219                         u64 off, u64 *plen,
1220                         u32 truncate_seq, u64 truncate_size,
1221                         struct page **pages, int num_pages)
1222 {
1223         struct ceph_osd_request *req;
1224         int rc = 0;
1225
1226         dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1227              vino.snap, off, *plen);
1228         req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1229                                     CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1230                                     NULL, 0, truncate_seq, truncate_size, NULL,
1231                                     false, 1);
1232         if (IS_ERR(req))
1233                 return PTR_ERR(req);
1234
1235         /* it may be a short read due to an object boundary */
1236         req->r_pages = pages;
1237         num_pages = calc_pages_for(off, *plen);
1238         req->r_num_pages = num_pages;
1239
1240         dout("readpages  final extent is %llu~%llu (%d pages)\n",
1241              off, *plen, req->r_num_pages);
1242
1243         rc = ceph_osdc_start_request(osdc, req, false);
1244         if (!rc)
1245                 rc = ceph_osdc_wait_request(osdc, req);
1246
1247         ceph_osdc_put_request(req);
1248         dout("readpages result %d\n", rc);
1249         return rc;
1250 }
1251
1252 /*
1253  * do a synchronous write on N pages
1254  */
1255 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1256                          struct ceph_file_layout *layout,
1257                          struct ceph_snap_context *snapc,
1258                          u64 off, u64 len,
1259                          u32 truncate_seq, u64 truncate_size,
1260                          struct timespec *mtime,
1261                          struct page **pages, int num_pages,
1262                          int flags, int do_sync, bool nofail)
1263 {
1264         struct ceph_osd_request *req;
1265         int rc = 0;
1266
1267         BUG_ON(vino.snap != CEPH_NOSNAP);
1268         req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1269                                     CEPH_OSD_OP_WRITE,
1270                                     flags | CEPH_OSD_FLAG_ONDISK |
1271                                             CEPH_OSD_FLAG_WRITE,
1272                                     snapc, do_sync,
1273                                     truncate_seq, truncate_size, mtime,
1274                                     nofail, 1);
1275         if (IS_ERR(req))
1276                 return PTR_ERR(req);
1277
1278         /* it may be a short write due to an object boundary */
1279         req->r_pages = pages;
1280         req->r_num_pages = calc_pages_for(off, len);
1281         dout("writepages %llu~%llu (%d pages)\n", off, len,
1282              req->r_num_pages);
1283
1284         rc = ceph_osdc_start_request(osdc, req, nofail);
1285         if (!rc)
1286                 rc = ceph_osdc_wait_request(osdc, req);
1287
1288         ceph_osdc_put_request(req);
1289         if (rc == 0)
1290                 rc = len;
1291         dout("writepages result %d\n", rc);
1292         return rc;
1293 }
1294
1295 /*
1296  * handle incoming message
1297  */
1298 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1299 {
1300         struct ceph_osd *osd = con->private;
1301         struct ceph_osd_client *osdc;
1302         int type = le16_to_cpu(msg->hdr.type);
1303
1304         if (!osd)
1305                 return;
1306         osdc = osd->o_osdc;
1307
1308         switch (type) {
1309         case CEPH_MSG_OSD_MAP:
1310                 ceph_osdc_handle_map(osdc, msg);
1311                 break;
1312         case CEPH_MSG_OSD_OPREPLY:
1313                 handle_reply(osdc, msg, con);
1314                 break;
1315
1316         default:
1317                 pr_err("received unknown message type %d %s\n", type,
1318                        ceph_msg_type_name(type));
1319         }
1320         ceph_msg_put(msg);
1321 }
1322
1323 /*
1324  * lookup and return message for incoming reply
1325  */
1326 static struct ceph_msg *get_reply(struct ceph_connection *con,
1327                                   struct ceph_msg_header *hdr,
1328                                   int *skip)
1329 {
1330         struct ceph_osd *osd = con->private;
1331         struct ceph_osd_client *osdc = osd->o_osdc;
1332         struct ceph_msg *m;
1333         struct ceph_osd_request *req;
1334         int front = le32_to_cpu(hdr->front_len);
1335         int data_len = le32_to_cpu(hdr->data_len);
1336         u64 tid;
1337         int err;
1338
1339         tid = le64_to_cpu(hdr->tid);
1340         mutex_lock(&osdc->request_mutex);
1341         req = __lookup_request(osdc, tid);
1342         if (!req) {
1343                 *skip = 1;
1344                 m = NULL;
1345                 pr_info("get_reply unknown tid %llu from osd%d\n", tid,
1346                         osd->o_osd);
1347                 goto out;
1348         }
1349
1350         if (req->r_con_filling_msg) {
1351                 dout("get_reply revoking msg %p from old con %p\n",
1352                      req->r_reply, req->r_con_filling_msg);
1353                 ceph_con_revoke_message(req->r_con_filling_msg, req->r_reply);
1354                 ceph_con_put(req->r_con_filling_msg);
1355         }
1356
1357         if (front > req->r_reply->front.iov_len) {
1358                 pr_warning("get_reply front %d > preallocated %d\n",
1359                            front, (int)req->r_reply->front.iov_len);
1360                 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, 0, 0, NULL);
1361                 if (IS_ERR(m))
1362                         goto out;
1363                 ceph_msg_put(req->r_reply);
1364                 req->r_reply = m;
1365         }
1366         m = ceph_msg_get(req->r_reply);
1367
1368         if (data_len > 0) {
1369                 err = __prepare_pages(con, hdr, req, tid, m);
1370                 if (err < 0) {
1371                         *skip = 1;
1372                         ceph_msg_put(m);
1373                         m = ERR_PTR(err);
1374                 }
1375         }
1376         *skip = 0;
1377         req->r_con_filling_msg = ceph_con_get(con);
1378         dout("get_reply tid %lld %p\n", tid, m);
1379
1380 out:
1381         mutex_unlock(&osdc->request_mutex);
1382         return m;
1383
1384 }
1385
1386 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
1387                                   struct ceph_msg_header *hdr,
1388                                   int *skip)
1389 {
1390         struct ceph_osd *osd = con->private;
1391         int type = le16_to_cpu(hdr->type);
1392         int front = le32_to_cpu(hdr->front_len);
1393
1394         switch (type) {
1395         case CEPH_MSG_OSD_MAP:
1396                 return ceph_msg_new(type, front, 0, 0, NULL);
1397         case CEPH_MSG_OSD_OPREPLY:
1398                 return get_reply(con, hdr, skip);
1399         default:
1400                 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
1401                         osd->o_osd);
1402                 *skip = 1;
1403                 return NULL;
1404         }
1405 }
1406
1407 /*
1408  * Wrappers to refcount containing ceph_osd struct
1409  */
1410 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
1411 {
1412         struct ceph_osd *osd = con->private;
1413         if (get_osd(osd))
1414                 return con;
1415         return NULL;
1416 }
1417
1418 static void put_osd_con(struct ceph_connection *con)
1419 {
1420         struct ceph_osd *osd = con->private;
1421         put_osd(osd);
1422 }
1423
1424 /*
1425  * authentication
1426  */
1427 static int get_authorizer(struct ceph_connection *con,
1428                           void **buf, int *len, int *proto,
1429                           void **reply_buf, int *reply_len, int force_new)
1430 {
1431         struct ceph_osd *o = con->private;
1432         struct ceph_osd_client *osdc = o->o_osdc;
1433         struct ceph_auth_client *ac = osdc->client->monc.auth;
1434         int ret = 0;
1435
1436         if (force_new && o->o_authorizer) {
1437                 ac->ops->destroy_authorizer(ac, o->o_authorizer);
1438                 o->o_authorizer = NULL;
1439         }
1440         if (o->o_authorizer == NULL) {
1441                 ret = ac->ops->create_authorizer(
1442                         ac, CEPH_ENTITY_TYPE_OSD,
1443                         &o->o_authorizer,
1444                         &o->o_authorizer_buf,
1445                         &o->o_authorizer_buf_len,
1446                         &o->o_authorizer_reply_buf,
1447                         &o->o_authorizer_reply_buf_len);
1448                 if (ret)
1449                 return ret;
1450         }
1451
1452         *proto = ac->protocol;
1453         *buf = o->o_authorizer_buf;
1454         *len = o->o_authorizer_buf_len;
1455         *reply_buf = o->o_authorizer_reply_buf;
1456         *reply_len = o->o_authorizer_reply_buf_len;
1457         return 0;
1458 }
1459
1460
1461 static int verify_authorizer_reply(struct ceph_connection *con, int len)
1462 {
1463         struct ceph_osd *o = con->private;
1464         struct ceph_osd_client *osdc = o->o_osdc;
1465         struct ceph_auth_client *ac = osdc->client->monc.auth;
1466
1467         return ac->ops->verify_authorizer_reply(ac, o->o_authorizer, len);
1468 }
1469
1470 static int invalidate_authorizer(struct ceph_connection *con)
1471 {
1472         struct ceph_osd *o = con->private;
1473         struct ceph_osd_client *osdc = o->o_osdc;
1474         struct ceph_auth_client *ac = osdc->client->monc.auth;
1475
1476         if (ac->ops->invalidate_authorizer)
1477                 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
1478
1479         return ceph_monc_validate_auth(&osdc->client->monc);
1480 }
1481
1482 const static struct ceph_connection_operations osd_con_ops = {
1483         .get = get_osd_con,
1484         .put = put_osd_con,
1485         .dispatch = dispatch,
1486         .get_authorizer = get_authorizer,
1487         .verify_authorizer_reply = verify_authorizer_reply,
1488         .invalidate_authorizer = invalidate_authorizer,
1489         .alloc_msg = alloc_msg,
1490         .fault = osd_reset,
1491 };