]> bbs.cooldavid.org Git - net-next-2.6.git/blob - fs/ceph/mon_client.c
223e8bc207e39aeffb6983ec1b7ab94a51024b15
[net-next-2.6.git] / fs / ceph / mon_client.c
1 #include "ceph_debug.h"
2
3 #include <linux/types.h>
4 #include <linux/random.h>
5 #include <linux/sched.h>
6
7 #include "mon_client.h"
8 #include "super.h"
9 #include "auth.h"
10 #include "decode.h"
11
12 /*
13  * Interact with Ceph monitor cluster.  Handle requests for new map
14  * versions, and periodically resend as needed.  Also implement
15  * statfs() and umount().
16  *
17  * A small cluster of Ceph "monitors" are responsible for managing critical
18  * cluster configuration and state information.  An odd number (e.g., 3, 5)
19  * of cmon daemons use a modified version of the Paxos part-time parliament
20  * algorithm to manage the MDS map (mds cluster membership), OSD map, and
21  * list of clients who have mounted the file system.
22  *
23  * We maintain an open, active session with a monitor at all times in order to
24  * receive timely MDSMap updates.  We periodically send a keepalive byte on the
25  * TCP socket to ensure we detect a failure.  If the connection does break, we
26  * randomly hunt for a new monitor.  Once the connection is reestablished, we
27  * resend any outstanding requests.
28  */
29
30 const static struct ceph_connection_operations mon_con_ops;
31
32 /*
33  * Decode a monmap blob (e.g., during mount).
34  */
35 struct ceph_monmap *ceph_monmap_decode(void *p, void *end)
36 {
37         struct ceph_monmap *m = NULL;
38         int i, err = -EINVAL;
39         struct ceph_fsid fsid;
40         u32 epoch, num_mon;
41         u16 version;
42         u32 len;
43
44         ceph_decode_32_safe(&p, end, len, bad);
45         ceph_decode_need(&p, end, len, bad);
46
47         dout("monmap_decode %p %p len %d\n", p, end, (int)(end-p));
48
49         ceph_decode_16_safe(&p, end, version, bad);
50
51         ceph_decode_need(&p, end, sizeof(fsid) + 2*sizeof(u32), bad);
52         ceph_decode_copy(&p, &fsid, sizeof(fsid));
53         epoch = ceph_decode_32(&p);
54
55         num_mon = ceph_decode_32(&p);
56         ceph_decode_need(&p, end, num_mon*sizeof(m->mon_inst[0]), bad);
57
58         if (num_mon >= CEPH_MAX_MON)
59                 goto bad;
60         m = kmalloc(sizeof(*m) + sizeof(m->mon_inst[0])*num_mon, GFP_NOFS);
61         if (m == NULL)
62                 return ERR_PTR(-ENOMEM);
63         m->fsid = fsid;
64         m->epoch = epoch;
65         m->num_mon = num_mon;
66         ceph_decode_copy(&p, m->mon_inst, num_mon*sizeof(m->mon_inst[0]));
67         for (i = 0; i < num_mon; i++)
68                 ceph_decode_addr(&m->mon_inst[i].addr);
69
70         dout("monmap_decode epoch %d, num_mon %d\n", m->epoch,
71              m->num_mon);
72         for (i = 0; i < m->num_mon; i++)
73                 dout("monmap_decode  mon%d is %s\n", i,
74                      pr_addr(&m->mon_inst[i].addr.in_addr));
75         return m;
76
77 bad:
78         dout("monmap_decode failed with %d\n", err);
79         kfree(m);
80         return ERR_PTR(err);
81 }
82
83 /*
84  * return true if *addr is included in the monmap.
85  */
86 int ceph_monmap_contains(struct ceph_monmap *m, struct ceph_entity_addr *addr)
87 {
88         int i;
89
90         for (i = 0; i < m->num_mon; i++)
91                 if (memcmp(addr, &m->mon_inst[i].addr, sizeof(*addr)) == 0)
92                         return 1;
93         return 0;
94 }
95
96 /*
97  * Close monitor session, if any.
98  */
99 static void __close_session(struct ceph_mon_client *monc)
100 {
101         if (monc->con) {
102                 dout("__close_session closing mon%d\n", monc->cur_mon);
103                 ceph_con_revoke(monc->con, monc->m_auth);
104                 ceph_con_close(monc->con);
105                 monc->cur_mon = -1;
106                 ceph_auth_reset(monc->auth);
107         }
108 }
109
110 /*
111  * Open a session with a (new) monitor.
112  */
113 static int __open_session(struct ceph_mon_client *monc)
114 {
115         char r;
116         int ret;
117
118         if (monc->cur_mon < 0) {
119                 get_random_bytes(&r, 1);
120                 monc->cur_mon = r % monc->monmap->num_mon;
121                 dout("open_session num=%d r=%d -> mon%d\n",
122                      monc->monmap->num_mon, r, monc->cur_mon);
123                 monc->sub_sent = 0;
124                 monc->sub_renew_after = jiffies;  /* i.e., expired */
125                 monc->want_next_osdmap = !!monc->want_next_osdmap;
126
127                 dout("open_session mon%d opening\n", monc->cur_mon);
128                 monc->con->peer_name.type = CEPH_ENTITY_TYPE_MON;
129                 monc->con->peer_name.num = cpu_to_le64(monc->cur_mon);
130                 ceph_con_open(monc->con,
131                               &monc->monmap->mon_inst[monc->cur_mon].addr);
132
133                 /* initiatiate authentication handshake */
134                 ret = ceph_auth_build_hello(monc->auth,
135                                             monc->m_auth->front.iov_base,
136                                             monc->m_auth->front_max);
137                 monc->m_auth->front.iov_len = ret;
138                 monc->m_auth->hdr.front_len = cpu_to_le32(ret);
139                 ceph_msg_get(monc->m_auth);  /* keep our ref */
140                 ceph_con_send(monc->con, monc->m_auth);
141         } else {
142                 dout("open_session mon%d already open\n", monc->cur_mon);
143         }
144         return 0;
145 }
146
147 static bool __sub_expired(struct ceph_mon_client *monc)
148 {
149         return time_after_eq(jiffies, monc->sub_renew_after);
150 }
151
152 /*
153  * Reschedule delayed work timer.
154  */
155 static void __schedule_delayed(struct ceph_mon_client *monc)
156 {
157         unsigned delay;
158
159         if (monc->cur_mon < 0 || __sub_expired(monc))
160                 delay = 10 * HZ;
161         else
162                 delay = 20 * HZ;
163         dout("__schedule_delayed after %u\n", delay);
164         schedule_delayed_work(&monc->delayed_work, delay);
165 }
166
167 /*
168  * Send subscribe request for mdsmap and/or osdmap.
169  */
170 static void __send_subscribe(struct ceph_mon_client *monc)
171 {
172         dout("__send_subscribe sub_sent=%u exp=%u want_osd=%d\n",
173              (unsigned)monc->sub_sent, __sub_expired(monc),
174              monc->want_next_osdmap);
175         if ((__sub_expired(monc) && !monc->sub_sent) ||
176             monc->want_next_osdmap == 1) {
177                 struct ceph_msg *msg;
178                 struct ceph_mon_subscribe_item *i;
179                 void *p, *end;
180
181                 msg = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 96, 0, 0, NULL);
182                 if (!msg)
183                         return;
184
185                 p = msg->front.iov_base;
186                 end = p + msg->front.iov_len;
187
188                 dout("__send_subscribe to 'mdsmap' %u+\n",
189                      (unsigned)monc->have_mdsmap);
190                 if (monc->want_next_osdmap) {
191                         dout("__send_subscribe to 'osdmap' %u\n",
192                              (unsigned)monc->have_osdmap);
193                         ceph_encode_32(&p, 3);
194                         ceph_encode_string(&p, end, "osdmap", 6);
195                         i = p;
196                         i->have = cpu_to_le64(monc->have_osdmap);
197                         i->onetime = 1;
198                         p += sizeof(*i);
199                         monc->want_next_osdmap = 2;  /* requested */
200                 } else {
201                         ceph_encode_32(&p, 2);
202                 }
203                 ceph_encode_string(&p, end, "mdsmap", 6);
204                 i = p;
205                 i->have = cpu_to_le64(monc->have_mdsmap);
206                 i->onetime = 0;
207                 p += sizeof(*i);
208                 ceph_encode_string(&p, end, "monmap", 6);
209                 i = p;
210                 i->have = 0;
211                 i->onetime = 0;
212                 p += sizeof(*i);
213
214                 msg->front.iov_len = p - msg->front.iov_base;
215                 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
216                 ceph_con_send(monc->con, msg);
217
218                 monc->sub_sent = jiffies | 1;  /* never 0 */
219         }
220 }
221
222 static void handle_subscribe_ack(struct ceph_mon_client *monc,
223                                  struct ceph_msg *msg)
224 {
225         unsigned seconds;
226         struct ceph_mon_subscribe_ack *h = msg->front.iov_base;
227
228         if (msg->front.iov_len < sizeof(*h))
229                 goto bad;
230         seconds = le32_to_cpu(h->duration);
231
232         mutex_lock(&monc->mutex);
233         if (monc->hunting) {
234                 pr_info("mon%d %s session established\n",
235                         monc->cur_mon, pr_addr(&monc->con->peer_addr.in_addr));
236                 monc->hunting = false;
237         }
238         dout("handle_subscribe_ack after %d seconds\n", seconds);
239         monc->sub_renew_after = monc->sub_sent + (seconds >> 1)*HZ - 1;
240         monc->sub_sent = 0;
241         mutex_unlock(&monc->mutex);
242         return;
243 bad:
244         pr_err("got corrupt subscribe-ack msg\n");
245         ceph_msg_dump(msg);
246 }
247
248 /*
249  * Keep track of which maps we have
250  */
251 int ceph_monc_got_mdsmap(struct ceph_mon_client *monc, u32 got)
252 {
253         mutex_lock(&monc->mutex);
254         monc->have_mdsmap = got;
255         mutex_unlock(&monc->mutex);
256         return 0;
257 }
258
259 int ceph_monc_got_osdmap(struct ceph_mon_client *monc, u32 got)
260 {
261         mutex_lock(&monc->mutex);
262         monc->have_osdmap = got;
263         monc->want_next_osdmap = 0;
264         mutex_unlock(&monc->mutex);
265         return 0;
266 }
267
268 /*
269  * Register interest in the next osdmap
270  */
271 void ceph_monc_request_next_osdmap(struct ceph_mon_client *monc)
272 {
273         dout("request_next_osdmap have %u\n", monc->have_osdmap);
274         mutex_lock(&monc->mutex);
275         if (!monc->want_next_osdmap)
276                 monc->want_next_osdmap = 1;
277         if (monc->want_next_osdmap < 2)
278                 __send_subscribe(monc);
279         mutex_unlock(&monc->mutex);
280 }
281
282 /*
283  *
284  */
285 int ceph_monc_open_session(struct ceph_mon_client *monc)
286 {
287         if (!monc->con) {
288                 monc->con = kmalloc(sizeof(*monc->con), GFP_KERNEL);
289                 if (!monc->con)
290                         return -ENOMEM;
291                 ceph_con_init(monc->client->msgr, monc->con);
292                 monc->con->private = monc;
293                 monc->con->ops = &mon_con_ops;
294         }
295
296         mutex_lock(&monc->mutex);
297         __open_session(monc);
298         __schedule_delayed(monc);
299         mutex_unlock(&monc->mutex);
300         return 0;
301 }
302
303 /*
304  * The monitor responds with mount ack indicate mount success.  The
305  * included client ticket allows the client to talk to MDSs and OSDs.
306  */
307 static void ceph_monc_handle_map(struct ceph_mon_client *monc,
308                                  struct ceph_msg *msg)
309 {
310         struct ceph_client *client = monc->client;
311         struct ceph_monmap *monmap = NULL, *old = monc->monmap;
312         void *p, *end;
313
314         mutex_lock(&monc->mutex);
315
316         dout("handle_monmap\n");
317         p = msg->front.iov_base;
318         end = p + msg->front.iov_len;
319
320         monmap = ceph_monmap_decode(p, end);
321         if (IS_ERR(monmap)) {
322                 pr_err("problem decoding monmap, %d\n",
323                        (int)PTR_ERR(monmap));
324                 goto out;
325         }
326
327         if (ceph_check_fsid(monc->client, &monmap->fsid) < 0) {
328                 kfree(monmap);
329                 goto out;
330         }
331
332         client->monc.monmap = monmap;
333         kfree(old);
334
335 out:
336         mutex_unlock(&monc->mutex);
337         wake_up(&client->mount_wq);
338 }
339
340 /*
341  * statfs
342  */
343 static void handle_statfs_reply(struct ceph_mon_client *monc,
344                                 struct ceph_msg *msg)
345 {
346         struct ceph_mon_statfs_request *req;
347         struct ceph_mon_statfs_reply *reply = msg->front.iov_base;
348         u64 tid;
349
350         if (msg->front.iov_len != sizeof(*reply))
351                 goto bad;
352         tid = le64_to_cpu(msg->hdr.tid);
353         dout("handle_statfs_reply %p tid %llu\n", msg, tid);
354
355         mutex_lock(&monc->mutex);
356         req = radix_tree_lookup(&monc->statfs_request_tree, tid);
357         if (req) {
358                 *req->buf = reply->st;
359                 req->result = 0;
360         }
361         mutex_unlock(&monc->mutex);
362         if (req)
363                 complete(&req->completion);
364         return;
365
366 bad:
367         pr_err("corrupt statfs reply, no tid\n");
368         ceph_msg_dump(msg);
369 }
370
371 /*
372  * (re)send a statfs request
373  */
374 static int send_statfs(struct ceph_mon_client *monc,
375                        struct ceph_mon_statfs_request *req)
376 {
377         struct ceph_msg *msg;
378         struct ceph_mon_statfs *h;
379
380         dout("send_statfs tid %llu\n", req->tid);
381         msg = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), 0, 0, NULL);
382         if (IS_ERR(msg))
383                 return PTR_ERR(msg);
384         req->request = msg;
385         msg->hdr.tid = cpu_to_le64(req->tid);
386         h = msg->front.iov_base;
387         h->monhdr.have_version = 0;
388         h->monhdr.session_mon = cpu_to_le16(-1);
389         h->monhdr.session_mon_tid = 0;
390         h->fsid = monc->monmap->fsid;
391         ceph_con_send(monc->con, msg);
392         return 0;
393 }
394
395 /*
396  * Do a synchronous statfs().
397  */
398 int ceph_monc_do_statfs(struct ceph_mon_client *monc, struct ceph_statfs *buf)
399 {
400         struct ceph_mon_statfs_request req;
401         int err;
402
403         req.buf = buf;
404         init_completion(&req.completion);
405
406         /* allocate memory for reply */
407         err = ceph_msgpool_resv(&monc->msgpool_statfs_reply, 1);
408         if (err)
409                 return err;
410
411         /* register request */
412         mutex_lock(&monc->mutex);
413         req.tid = ++monc->last_tid;
414         req.last_attempt = jiffies;
415         req.delay = BASE_DELAY_INTERVAL;
416         if (radix_tree_insert(&monc->statfs_request_tree, req.tid, &req) < 0) {
417                 mutex_unlock(&monc->mutex);
418                 pr_err("ENOMEM in do_statfs\n");
419                 return -ENOMEM;
420         }
421         monc->num_statfs_requests++;
422         mutex_unlock(&monc->mutex);
423
424         /* send request and wait */
425         err = send_statfs(monc, &req);
426         if (!err)
427                 err = wait_for_completion_interruptible(&req.completion);
428
429         mutex_lock(&monc->mutex);
430         radix_tree_delete(&monc->statfs_request_tree, req.tid);
431         monc->num_statfs_requests--;
432         ceph_msgpool_resv(&monc->msgpool_statfs_reply, -1);
433         mutex_unlock(&monc->mutex);
434
435         if (!err)
436                 err = req.result;
437         return err;
438 }
439
440 /*
441  * Resend pending statfs requests.
442  */
443 static void __resend_statfs(struct ceph_mon_client *monc)
444 {
445         u64 next_tid = 0;
446         int got;
447         int did = 0;
448         struct ceph_mon_statfs_request *req;
449
450         while (1) {
451                 got = radix_tree_gang_lookup(&monc->statfs_request_tree,
452                                              (void **)&req,
453                                              next_tid, 1);
454                 if (got == 0)
455                         break;
456                 did++;
457                 next_tid = req->tid + 1;
458
459                 send_statfs(monc, req);
460         }
461 }
462
463 /*
464  * Delayed work.  If we haven't mounted yet, retry.  Otherwise,
465  * renew/retry subscription as needed (in case it is timing out, or we
466  * got an ENOMEM).  And keep the monitor connection alive.
467  */
468 static void delayed_work(struct work_struct *work)
469 {
470         struct ceph_mon_client *monc =
471                 container_of(work, struct ceph_mon_client, delayed_work.work);
472
473         dout("monc delayed_work\n");
474         mutex_lock(&monc->mutex);
475         if (monc->hunting) {
476                 __close_session(monc);
477                 __open_session(monc);  /* continue hunting */
478         } else {
479                 ceph_con_keepalive(monc->con);
480                 if (monc->auth->ops->is_authenticated(monc->auth))
481                         __send_subscribe(monc);
482         }
483         __schedule_delayed(monc);
484         mutex_unlock(&monc->mutex);
485 }
486
487 /*
488  * On startup, we build a temporary monmap populated with the IPs
489  * provided by mount(2).
490  */
491 static int build_initial_monmap(struct ceph_mon_client *monc)
492 {
493         struct ceph_mount_args *args = monc->client->mount_args;
494         struct ceph_entity_addr *mon_addr = args->mon_addr;
495         int num_mon = args->num_mon;
496         int i;
497
498         /* build initial monmap */
499         monc->monmap = kzalloc(sizeof(*monc->monmap) +
500                                num_mon*sizeof(monc->monmap->mon_inst[0]),
501                                GFP_KERNEL);
502         if (!monc->monmap)
503                 return -ENOMEM;
504         for (i = 0; i < num_mon; i++) {
505                 monc->monmap->mon_inst[i].addr = mon_addr[i];
506                 monc->monmap->mon_inst[i].addr.nonce = 0;
507                 monc->monmap->mon_inst[i].name.type =
508                         CEPH_ENTITY_TYPE_MON;
509                 monc->monmap->mon_inst[i].name.num = cpu_to_le64(i);
510         }
511         monc->monmap->num_mon = num_mon;
512         monc->have_fsid = false;
513
514         /* release addr memory */
515         kfree(args->mon_addr);
516         args->mon_addr = NULL;
517         args->num_mon = 0;
518         return 0;
519 }
520
521 int ceph_monc_init(struct ceph_mon_client *monc, struct ceph_client *cl)
522 {
523         int err = 0;
524
525         dout("init\n");
526         memset(monc, 0, sizeof(*monc));
527         monc->client = cl;
528         monc->monmap = NULL;
529         mutex_init(&monc->mutex);
530
531         err = build_initial_monmap(monc);
532         if (err)
533                 goto out;
534
535         monc->con = NULL;
536
537         /* authentication */
538         monc->auth = ceph_auth_init(cl->mount_args->name,
539                                     cl->mount_args->secret);
540         if (IS_ERR(monc->auth))
541                 return PTR_ERR(monc->auth);
542         monc->auth->want_keys =
543                 CEPH_ENTITY_TYPE_AUTH | CEPH_ENTITY_TYPE_MON |
544                 CEPH_ENTITY_TYPE_OSD | CEPH_ENTITY_TYPE_MDS;
545
546         /* msg pools */
547         err = ceph_msgpool_init(&monc->msgpool_subscribe_ack,
548                                sizeof(struct ceph_mon_subscribe_ack), 1, false);
549         if (err < 0)
550                 goto out_monmap;
551         err = ceph_msgpool_init(&monc->msgpool_statfs_reply,
552                                 sizeof(struct ceph_mon_statfs_reply), 0, false);
553         if (err < 0)
554                 goto out_pool1;
555         err = ceph_msgpool_init(&monc->msgpool_auth_reply, 4096, 1, false);
556         if (err < 0)
557                 goto out_pool2;
558
559         monc->m_auth = ceph_msg_new(CEPH_MSG_AUTH, 4096, 0, 0, NULL);
560         if (IS_ERR(monc->m_auth)) {
561                 err = PTR_ERR(monc->m_auth);
562                 monc->m_auth = NULL;
563                 goto out_pool3;
564         }
565
566         monc->cur_mon = -1;
567         monc->hunting = true;
568         monc->sub_renew_after = jiffies;
569         monc->sub_sent = 0;
570
571         INIT_DELAYED_WORK(&monc->delayed_work, delayed_work);
572         INIT_RADIX_TREE(&monc->statfs_request_tree, GFP_NOFS);
573         monc->num_statfs_requests = 0;
574         monc->last_tid = 0;
575
576         monc->have_mdsmap = 0;
577         monc->have_osdmap = 0;
578         monc->want_next_osdmap = 1;
579         return 0;
580
581 out_pool3:
582         ceph_msgpool_destroy(&monc->msgpool_auth_reply);
583 out_pool2:
584         ceph_msgpool_destroy(&monc->msgpool_subscribe_ack);
585 out_pool1:
586         ceph_msgpool_destroy(&monc->msgpool_statfs_reply);
587 out_monmap:
588         kfree(monc->monmap);
589 out:
590         return err;
591 }
592
593 void ceph_monc_stop(struct ceph_mon_client *monc)
594 {
595         dout("stop\n");
596         cancel_delayed_work_sync(&monc->delayed_work);
597
598         mutex_lock(&monc->mutex);
599         __close_session(monc);
600         if (monc->con) {
601                 monc->con->private = NULL;
602                 monc->con->ops->put(monc->con);
603                 monc->con = NULL;
604         }
605         mutex_unlock(&monc->mutex);
606
607         ceph_auth_destroy(monc->auth);
608
609         ceph_msg_put(monc->m_auth);
610         ceph_msgpool_destroy(&monc->msgpool_subscribe_ack);
611         ceph_msgpool_destroy(&monc->msgpool_statfs_reply);
612         ceph_msgpool_destroy(&monc->msgpool_auth_reply);
613
614         kfree(monc->monmap);
615 }
616
617
618 static void handle_auth_reply(struct ceph_mon_client *monc,
619                               struct ceph_msg *msg)
620 {
621         int ret;
622
623         mutex_lock(&monc->mutex);
624         ret = ceph_handle_auth_reply(monc->auth, msg->front.iov_base,
625                                      msg->front.iov_len,
626                                      monc->m_auth->front.iov_base,
627                                      monc->m_auth->front_max);
628         if (ret < 0) {
629                 monc->client->mount_err = ret;
630                 wake_up(&monc->client->mount_wq);
631         } else if (ret > 0) {
632                 monc->m_auth->front.iov_len = ret;
633                 monc->m_auth->hdr.front_len = cpu_to_le32(ret);
634                 ceph_msg_get(monc->m_auth);  /* keep our ref */
635                 ceph_con_send(monc->con, monc->m_auth);
636         } else if (monc->auth->ops->is_authenticated(monc->auth)) {
637                 dout("authenticated, starting session\n");
638
639                 monc->client->msgr->inst.name.type = CEPH_ENTITY_TYPE_CLIENT;
640                 monc->client->msgr->inst.name.num = monc->auth->global_id;
641
642                 __send_subscribe(monc);
643                 __resend_statfs(monc);
644         }
645         mutex_unlock(&monc->mutex);
646 }
647
648 /*
649  * handle incoming message
650  */
651 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
652 {
653         struct ceph_mon_client *monc = con->private;
654         int type = le16_to_cpu(msg->hdr.type);
655
656         if (!monc)
657                 return;
658
659         switch (type) {
660         case CEPH_MSG_AUTH_REPLY:
661                 handle_auth_reply(monc, msg);
662                 break;
663
664         case CEPH_MSG_MON_SUBSCRIBE_ACK:
665                 handle_subscribe_ack(monc, msg);
666                 break;
667
668         case CEPH_MSG_STATFS_REPLY:
669                 handle_statfs_reply(monc, msg);
670                 break;
671
672         case CEPH_MSG_MON_MAP:
673                 ceph_monc_handle_map(monc, msg);
674                 break;
675
676         case CEPH_MSG_MDS_MAP:
677                 ceph_mdsc_handle_map(&monc->client->mdsc, msg);
678                 break;
679
680         case CEPH_MSG_OSD_MAP:
681                 ceph_osdc_handle_map(&monc->client->osdc, msg);
682                 break;
683
684         default:
685                 pr_err("received unknown message type %d %s\n", type,
686                        ceph_msg_type_name(type));
687         }
688         ceph_msg_put(msg);
689 }
690
691 /*
692  * Allocate memory for incoming message
693  */
694 static struct ceph_msg *mon_alloc_msg(struct ceph_connection *con,
695                                       struct ceph_msg_header *hdr)
696 {
697         struct ceph_mon_client *monc = con->private;
698         int type = le16_to_cpu(hdr->type);
699         int front = le32_to_cpu(hdr->front_len);
700
701         switch (type) {
702         case CEPH_MSG_MON_SUBSCRIBE_ACK:
703                 return ceph_msgpool_get(&monc->msgpool_subscribe_ack, front);
704         case CEPH_MSG_STATFS_REPLY:
705                 return ceph_msgpool_get(&monc->msgpool_statfs_reply, front);
706         case CEPH_MSG_AUTH_REPLY:
707                 return ceph_msgpool_get(&monc->msgpool_auth_reply, front);
708         }
709         return ceph_alloc_msg(con, hdr);
710 }
711
712 /*
713  * If the monitor connection resets, pick a new monitor and resubmit
714  * any pending requests.
715  */
716 static void mon_fault(struct ceph_connection *con)
717 {
718         struct ceph_mon_client *monc = con->private;
719
720         if (!monc)
721                 return;
722
723         dout("mon_fault\n");
724         mutex_lock(&monc->mutex);
725         if (!con->private)
726                 goto out;
727
728         if (monc->con && !monc->hunting)
729                 pr_info("mon%d %s session lost, "
730                         "hunting for new mon\n", monc->cur_mon,
731                         pr_addr(&monc->con->peer_addr.in_addr));
732
733         __close_session(monc);
734         if (!monc->hunting) {
735                 /* start hunting */
736                 monc->hunting = true;
737                 __open_session(monc);
738         } else {
739                 /* already hunting, let's wait a bit */
740                 __schedule_delayed(monc);
741         }
742 out:
743         mutex_unlock(&monc->mutex);
744 }
745
746 const static struct ceph_connection_operations mon_con_ops = {
747         .get = ceph_con_get,
748         .put = ceph_con_put,
749         .dispatch = dispatch,
750         .fault = mon_fault,
751         .alloc_msg = mon_alloc_msg,
752         .alloc_middle = ceph_alloc_middle,
753 };