ceph: negotiate authentication protocol; implement AUTH_NONE protocol
[deliverable/linux.git] / fs / ceph / mon_client.c
CommitLineData
ba75bb98
SW
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"
4e7a5dcd 9#include "auth.h"
ba75bb98
SW
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
30const static struct ceph_connection_operations mon_con_ops;
31
32/*
33 * Decode a monmap blob (e.g., during mount).
34 */
35struct 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;
4e7a5dcd
SW
42 u32 len;
43
44 ceph_decode_32_safe(&p, end, len, bad);
45 ceph_decode_need(&p, end, len, bad);
ba75bb98
SW
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));
c89136ea 53 epoch = ceph_decode_32(&p);
ba75bb98 54
c89136ea 55 num_mon = ceph_decode_32(&p);
ba75bb98
SW
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]));
63f2d211
SW
67 for (i = 0; i < num_mon; i++)
68 ceph_decode_addr(&m->mon_inst[i].addr);
ba75bb98 69
ba75bb98
SW
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
77bad:
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 */
86int 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 (ceph_entity_addr_equal(addr, &m->mon_inst[i].addr))
92 return 1;
93 return 0;
94}
95
96/*
97 * Close monitor session, if any.
98 */
99static void __close_session(struct ceph_mon_client *monc)
100{
101 if (monc->con) {
102 dout("__close_session closing mon%d\n", monc->cur_mon);
4e7a5dcd 103 ceph_con_revoke(monc->con, monc->m_auth);
ba75bb98
SW
104 ceph_con_close(monc->con);
105 monc->cur_mon = -1;
4e7a5dcd 106 ceph_auth_reset(monc->auth);
ba75bb98
SW
107 }
108}
109
110/*
111 * Open a session with a (new) monitor.
112 */
113static int __open_session(struct ceph_mon_client *monc)
114{
115 char r;
4e7a5dcd 116 int ret;
ba75bb98
SW
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);
4e7a5dcd
SW
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);
ba75bb98
SW
141 } else {
142 dout("open_session mon%d already open\n", monc->cur_mon);
143 }
144 return 0;
145}
146
147static 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 */
155static void __schedule_delayed(struct ceph_mon_client *monc)
156{
157 unsigned delay;
158
4e7a5dcd 159 if (monc->cur_mon < 0 || __sub_expired(monc))
ba75bb98
SW
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 */
170static 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
4e7a5dcd 181 msg = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 96, 0, 0, NULL);
ba75bb98
SW
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);
4e7a5dcd 193 ceph_encode_32(&p, 3);
ba75bb98
SW
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 {
4e7a5dcd 201 ceph_encode_32(&p, 2);
ba75bb98
SW
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);
4e7a5dcd
SW
208 ceph_encode_string(&p, end, "monmap", 6);
209 i = p;
210 i->have = 0;
211 i->onetime = 0;
212 p += sizeof(*i);
ba75bb98
SW
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
222static void handle_subscribe_ack(struct ceph_mon_client *monc,
223 struct ceph_msg *msg)
224{
225 unsigned seconds;
07bd10fb
SW
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);
ba75bb98 231
ba75bb98
SW
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);
0656d11b 239 monc->sub_renew_after = monc->sub_sent + (seconds >> 1)*HZ - 1;
ba75bb98
SW
240 monc->sub_sent = 0;
241 mutex_unlock(&monc->mutex);
242 return;
243bad:
244 pr_err("got corrupt subscribe-ack msg\n");
245}
246
247/*
248 * Keep track of which maps we have
249 */
250int ceph_monc_got_mdsmap(struct ceph_mon_client *monc, u32 got)
251{
252 mutex_lock(&monc->mutex);
253 monc->have_mdsmap = got;
254 mutex_unlock(&monc->mutex);
255 return 0;
256}
257
258int ceph_monc_got_osdmap(struct ceph_mon_client *monc, u32 got)
259{
260 mutex_lock(&monc->mutex);
261 monc->have_osdmap = got;
262 monc->want_next_osdmap = 0;
263 mutex_unlock(&monc->mutex);
264 return 0;
265}
266
267/*
268 * Register interest in the next osdmap
269 */
270void ceph_monc_request_next_osdmap(struct ceph_mon_client *monc)
271{
272 dout("request_next_osdmap have %u\n", monc->have_osdmap);
273 mutex_lock(&monc->mutex);
274 if (!monc->want_next_osdmap)
275 monc->want_next_osdmap = 1;
276 if (monc->want_next_osdmap < 2)
277 __send_subscribe(monc);
278 mutex_unlock(&monc->mutex);
279}
280
4e7a5dcd 281#if 0
ba75bb98
SW
282/*
283 * mount
284 */
285static void __request_mount(struct ceph_mon_client *monc)
286{
287 struct ceph_msg *msg;
288 struct ceph_client_mount *h;
ba75bb98
SW
289
290 dout("__request_mount\n");
ba75bb98
SW
291 msg = ceph_msg_new(CEPH_MSG_CLIENT_MOUNT, sizeof(*h), 0, 0, NULL);
292 if (IS_ERR(msg))
293 return;
294 h = msg->front.iov_base;
13e38c8a
SW
295 h->monhdr.have_version = 0;
296 h->monhdr.session_mon = cpu_to_le16(-1);
297 h->monhdr.session_mon_tid = 0;
ba75bb98
SW
298 ceph_con_send(monc->con, msg);
299}
4e7a5dcd 300#endif
ba75bb98 301
4e7a5dcd
SW
302/*
303 *
304 */
305int ceph_monc_open_session(struct ceph_mon_client *monc)
ba75bb98
SW
306{
307 if (!monc->con) {
308 monc->con = kmalloc(sizeof(*monc->con), GFP_KERNEL);
309 if (!monc->con)
310 return -ENOMEM;
311 ceph_con_init(monc->client->msgr, monc->con);
312 monc->con->private = monc;
313 monc->con->ops = &mon_con_ops;
314 }
315
316 mutex_lock(&monc->mutex);
4e7a5dcd 317 __open_session(monc);
ba75bb98
SW
318 __schedule_delayed(monc);
319 mutex_unlock(&monc->mutex);
320 return 0;
321}
322
4e7a5dcd
SW
323#if 0
324
ba75bb98
SW
325/*
326 * The monitor responds with mount ack indicate mount success. The
327 * included client ticket allows the client to talk to MDSs and OSDs.
328 */
329static void handle_mount_ack(struct ceph_mon_client *monc, struct ceph_msg *msg)
330{
331 struct ceph_client *client = monc->client;
332 struct ceph_monmap *monmap = NULL, *old = monc->monmap;
333 void *p, *end;
334 s32 result;
335 u32 len;
336 s64 cnum;
337 int err = -EINVAL;
338
339 if (client->whoami >= 0) {
340 dout("handle_mount_ack - already mounted\n");
341 return;
342 }
343
344 mutex_lock(&monc->mutex);
345
346 dout("handle_mount_ack\n");
347 p = msg->front.iov_base;
348 end = p + msg->front.iov_len;
349
350 ceph_decode_64_safe(&p, end, cnum, bad);
351 ceph_decode_32_safe(&p, end, result, bad);
352 ceph_decode_32_safe(&p, end, len, bad);
353 if (result) {
354 pr_err("mount denied: %.*s (%d)\n", len, (char *)p,
355 result);
356 err = result;
357 goto out;
358 }
359 p += len;
360
361 ceph_decode_32_safe(&p, end, len, bad);
362 ceph_decode_need(&p, end, len, bad);
363 monmap = ceph_monmap_decode(p, p + len);
364 if (IS_ERR(monmap)) {
365 pr_err("problem decoding monmap, %d\n",
366 (int)PTR_ERR(monmap));
367 err = -EINVAL;
368 goto out;
369 }
370 p += len;
371
372 client->monc.monmap = monmap;
373 kfree(old);
374
375 client->signed_ticket = NULL;
376 client->signed_ticket_len = 0;
377
378 monc->want_mount = false;
379
380 client->whoami = cnum;
381 client->msgr->inst.name.type = CEPH_ENTITY_TYPE_CLIENT;
382 client->msgr->inst.name.num = cpu_to_le64(cnum);
383 pr_info("client%lld fsid " FSID_FORMAT "\n",
384 client->whoami, PR_FSID(&client->monc.monmap->fsid));
385
386 ceph_debugfs_client_init(client);
387 __send_subscribe(monc);
388
389 err = 0;
390 goto out;
391
392bad:
393 pr_err("error decoding mount_ack message\n");
394out:
395 client->mount_err = err;
396 mutex_unlock(&monc->mutex);
397 wake_up(&client->mount_wq);
398}
4e7a5dcd
SW
399#endif
400
401/*
402 * The monitor responds with mount ack indicate mount success. The
403 * included client ticket allows the client to talk to MDSs and OSDs.
404 */
405static void ceph_monc_handle_map(struct ceph_mon_client *monc, struct ceph_msg *msg)
406{
407 struct ceph_client *client = monc->client;
408 struct ceph_monmap *monmap = NULL, *old = monc->monmap;
409 void *p, *end;
410
411 mutex_lock(&monc->mutex);
412
413 dout("handle_monmap\n");
414 p = msg->front.iov_base;
415 end = p + msg->front.iov_len;
416
417 monmap = ceph_monmap_decode(p, end);
418 if (IS_ERR(monmap)) {
419 pr_err("problem decoding monmap, %d\n",
420 (int)PTR_ERR(monmap));
421 return;
422 }
423 if (monc->have_fsid &&
424 ceph_fsid_compare(&monmap->fsid, &monc->monmap->fsid)) {
425 print_hex_dump(KERN_ERR, "monmap->fsid: ", DUMP_PREFIX_NONE, 16, 1,
426 (void *)&monmap->fsid, 16, 0);
427 print_hex_dump(KERN_ERR, "monc->monmap->fsid: ", DUMP_PREFIX_NONE, 16, 1,
428 (void *)&monc->monmap->fsid, 16, 0);
429
430 pr_err("fsid mismatch, got a previous map with different fsid");
431 kfree(monmap);
432 return;
433 }
434
435 client->monc.monmap = monmap;
436 client->monc.have_fsid = true;
437 kfree(old);
438
439 mutex_unlock(&monc->mutex);
440 wake_up(&client->mount_wq);
441}
442
ba75bb98 443
4e7a5dcd
SW
444/*
445 * init client info after authentication
446 */
447static void __init_authenticated_client(struct ceph_mon_client *monc)
448{
449 struct ceph_client *client = monc->client;
ba75bb98 450
4e7a5dcd
SW
451 client->signed_ticket = NULL;
452 client->signed_ticket_len = 0;
453 client->msgr->inst.name.type = CEPH_ENTITY_TYPE_CLIENT;
454 client->msgr->inst.name.num = monc->auth->global_id;
ba75bb98 455
4e7a5dcd
SW
456 ceph_debugfs_client_init(client);
457}
ba75bb98
SW
458
459/*
460 * statfs
461 */
462static void handle_statfs_reply(struct ceph_mon_client *monc,
463 struct ceph_msg *msg)
464{
465 struct ceph_mon_statfs_request *req;
466 struct ceph_mon_statfs_reply *reply = msg->front.iov_base;
467 u64 tid;
468
469 if (msg->front.iov_len != sizeof(*reply))
470 goto bad;
471 tid = le64_to_cpu(reply->tid);
472 dout("handle_statfs_reply %p tid %llu\n", msg, tid);
473
474 mutex_lock(&monc->mutex);
475 req = radix_tree_lookup(&monc->statfs_request_tree, tid);
476 if (req) {
477 *req->buf = reply->st;
478 req->result = 0;
479 }
480 mutex_unlock(&monc->mutex);
481 if (req)
482 complete(&req->completion);
483 return;
484
485bad:
486 pr_err("corrupt statfs reply, no tid\n");
487}
488
489/*
490 * (re)send a statfs request
491 */
492static int send_statfs(struct ceph_mon_client *monc,
493 struct ceph_mon_statfs_request *req)
494{
495 struct ceph_msg *msg;
496 struct ceph_mon_statfs *h;
ba75bb98
SW
497
498 dout("send_statfs tid %llu\n", req->tid);
ba75bb98
SW
499 msg = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), 0, 0, NULL);
500 if (IS_ERR(msg))
501 return PTR_ERR(msg);
502 req->request = msg;
503 h = msg->front.iov_base;
13e38c8a
SW
504 h->monhdr.have_version = 0;
505 h->monhdr.session_mon = cpu_to_le16(-1);
506 h->monhdr.session_mon_tid = 0;
ba75bb98
SW
507 h->fsid = monc->monmap->fsid;
508 h->tid = cpu_to_le64(req->tid);
509 ceph_con_send(monc->con, msg);
510 return 0;
511}
512
513/*
514 * Do a synchronous statfs().
515 */
516int ceph_monc_do_statfs(struct ceph_mon_client *monc, struct ceph_statfs *buf)
517{
518 struct ceph_mon_statfs_request req;
519 int err;
520
521 req.buf = buf;
522 init_completion(&req.completion);
523
524 /* allocate memory for reply */
525 err = ceph_msgpool_resv(&monc->msgpool_statfs_reply, 1);
526 if (err)
527 return err;
528
529 /* register request */
530 mutex_lock(&monc->mutex);
531 req.tid = ++monc->last_tid;
532 req.last_attempt = jiffies;
533 req.delay = BASE_DELAY_INTERVAL;
534 if (radix_tree_insert(&monc->statfs_request_tree, req.tid, &req) < 0) {
535 mutex_unlock(&monc->mutex);
536 pr_err("ENOMEM in do_statfs\n");
537 return -ENOMEM;
538 }
539 monc->num_statfs_requests++;
540 mutex_unlock(&monc->mutex);
541
542 /* send request and wait */
543 err = send_statfs(monc, &req);
544 if (!err)
545 err = wait_for_completion_interruptible(&req.completion);
546
547 mutex_lock(&monc->mutex);
548 radix_tree_delete(&monc->statfs_request_tree, req.tid);
549 monc->num_statfs_requests--;
550 ceph_msgpool_resv(&monc->msgpool_statfs_reply, -1);
551 mutex_unlock(&monc->mutex);
552
553 if (!err)
554 err = req.result;
555 return err;
556}
557
558/*
559 * Resend pending statfs requests.
560 */
561static void __resend_statfs(struct ceph_mon_client *monc)
562{
563 u64 next_tid = 0;
564 int got;
565 int did = 0;
566 struct ceph_mon_statfs_request *req;
567
568 while (1) {
569 got = radix_tree_gang_lookup(&monc->statfs_request_tree,
570 (void **)&req,
571 next_tid, 1);
572 if (got == 0)
573 break;
574 did++;
575 next_tid = req->tid + 1;
576
577 send_statfs(monc, req);
578 }
579}
580
581/*
582 * Delayed work. If we haven't mounted yet, retry. Otherwise,
583 * renew/retry subscription as needed (in case it is timing out, or we
584 * got an ENOMEM). And keep the monitor connection alive.
585 */
586static void delayed_work(struct work_struct *work)
587{
588 struct ceph_mon_client *monc =
589 container_of(work, struct ceph_mon_client, delayed_work.work);
590
591 dout("monc delayed_work\n");
592 mutex_lock(&monc->mutex);
4e7a5dcd
SW
593 if (monc->hunting) {
594 __close_session(monc);
595 __open_session(monc); /* continue hunting */
ba75bb98 596 } else {
4e7a5dcd
SW
597 ceph_con_keepalive(monc->con);
598 if (monc->auth->ops->is_authenticated(monc->auth))
599 __send_subscribe(monc);
ba75bb98 600 }
ba75bb98
SW
601 __schedule_delayed(monc);
602 mutex_unlock(&monc->mutex);
603}
604
6b805185
SW
605/*
606 * On startup, we build a temporary monmap populated with the IPs
607 * provided by mount(2).
608 */
609static int build_initial_monmap(struct ceph_mon_client *monc)
610{
611 struct ceph_mount_args *args = monc->client->mount_args;
612 struct ceph_entity_addr *mon_addr = args->mon_addr;
613 int num_mon = args->num_mon;
614 int i;
615
616 /* build initial monmap */
617 monc->monmap = kzalloc(sizeof(*monc->monmap) +
618 num_mon*sizeof(monc->monmap->mon_inst[0]),
619 GFP_KERNEL);
620 if (!monc->monmap)
621 return -ENOMEM;
622 for (i = 0; i < num_mon; i++) {
623 monc->monmap->mon_inst[i].addr = mon_addr[i];
624 monc->monmap->mon_inst[i].addr.erank = 0;
625 monc->monmap->mon_inst[i].addr.nonce = 0;
626 monc->monmap->mon_inst[i].name.type =
627 CEPH_ENTITY_TYPE_MON;
628 monc->monmap->mon_inst[i].name.num = cpu_to_le64(i);
629 }
630 monc->monmap->num_mon = num_mon;
4e7a5dcd 631 monc->have_fsid = false;
6b805185
SW
632
633 /* release addr memory */
634 kfree(args->mon_addr);
635 args->mon_addr = NULL;
636 args->num_mon = 0;
637 return 0;
638}
639
ba75bb98
SW
640int ceph_monc_init(struct ceph_mon_client *monc, struct ceph_client *cl)
641{
642 int err = 0;
643
644 dout("init\n");
645 memset(monc, 0, sizeof(*monc));
646 monc->client = cl;
647 monc->monmap = NULL;
648 mutex_init(&monc->mutex);
649
6b805185
SW
650 err = build_initial_monmap(monc);
651 if (err)
652 goto out;
653
ba75bb98
SW
654 monc->con = NULL;
655
4e7a5dcd
SW
656 /* authentication */
657 monc->auth = ceph_auth_init(cl->mount_args->name,
658 cl->mount_args->secret);
659 if (IS_ERR(monc->auth))
660 return PTR_ERR(monc->auth);
661 monc->auth->want_keys =
662 CEPH_ENTITY_TYPE_AUTH | CEPH_ENTITY_TYPE_MON |
663 CEPH_ENTITY_TYPE_OSD | CEPH_ENTITY_TYPE_MDS;
664
ba75bb98 665 /* msg pools */
07bd10fb
SW
666 err = ceph_msgpool_init(&monc->msgpool_subscribe_ack,
667 sizeof(struct ceph_mon_subscribe_ack), 1, false);
ba75bb98 668 if (err < 0)
4e7a5dcd 669 goto out_monmap;
ba75bb98
SW
670 err = ceph_msgpool_init(&monc->msgpool_statfs_reply,
671 sizeof(struct ceph_mon_statfs_reply), 0, false);
672 if (err < 0)
4e7a5dcd
SW
673 goto out_pool1;
674 err = ceph_msgpool_init(&monc->msgpool_auth_reply, 4096, 1, false);
675 if (err < 0)
676 goto out_pool2;
677
678 monc->m_auth = ceph_msg_new(CEPH_MSG_AUTH, 4096, 0, 0, NULL);
679 if (IS_ERR(monc->m_auth)) {
680 err = PTR_ERR(monc->m_auth);
681 monc->m_auth = NULL;
682 goto out_pool3;
683 }
ba75bb98
SW
684
685 monc->cur_mon = -1;
4e7a5dcd 686 monc->hunting = true;
ba75bb98
SW
687 monc->sub_renew_after = jiffies;
688 monc->sub_sent = 0;
689
690 INIT_DELAYED_WORK(&monc->delayed_work, delayed_work);
691 INIT_RADIX_TREE(&monc->statfs_request_tree, GFP_NOFS);
692 monc->num_statfs_requests = 0;
693 monc->last_tid = 0;
694
695 monc->have_mdsmap = 0;
696 monc->have_osdmap = 0;
697 monc->want_next_osdmap = 1;
4e7a5dcd
SW
698 return 0;
699
700out_pool3:
701 ceph_msgpool_destroy(&monc->msgpool_auth_reply);
702out_pool2:
703 ceph_msgpool_destroy(&monc->msgpool_subscribe_ack);
704out_pool1:
705 ceph_msgpool_destroy(&monc->msgpool_statfs_reply);
706out_monmap:
707 kfree(monc->monmap);
ba75bb98
SW
708out:
709 return err;
710}
711
712void ceph_monc_stop(struct ceph_mon_client *monc)
713{
714 dout("stop\n");
715 cancel_delayed_work_sync(&monc->delayed_work);
716
717 mutex_lock(&monc->mutex);
718 __close_session(monc);
719 if (monc->con) {
720 monc->con->private = NULL;
721 monc->con->ops->put(monc->con);
722 monc->con = NULL;
723 }
724 mutex_unlock(&monc->mutex);
725
4e7a5dcd
SW
726 ceph_auth_destroy(monc->auth);
727
728 ceph_msg_put(monc->m_auth);
ba75bb98
SW
729 ceph_msgpool_destroy(&monc->msgpool_subscribe_ack);
730 ceph_msgpool_destroy(&monc->msgpool_statfs_reply);
4e7a5dcd 731 ceph_msgpool_destroy(&monc->msgpool_auth_reply);
ba75bb98
SW
732
733 kfree(monc->monmap);
734}
735
736
4e7a5dcd
SW
737static void handle_auth_reply(struct ceph_mon_client *monc,
738 struct ceph_msg *msg)
739{
740 int ret;
741
742 mutex_lock(&monc->mutex);
743 ret = ceph_handle_auth_reply(monc->auth, msg->front.iov_base,
744 msg->front.iov_len,
745 monc->m_auth->front.iov_base,
746 monc->m_auth->front_max);
747 if (ret < 0) {
748 monc->client->mount_err = ret;
749 wake_up(&monc->client->mount_wq);
750 } else if (ret > 0) {
751 monc->m_auth->front.iov_len = ret;
752 monc->m_auth->hdr.front_len = cpu_to_le32(ret);
753 ceph_msg_get(monc->m_auth); /* keep our ref */
754 ceph_con_send(monc->con, monc->m_auth);
755 } else if (monc->auth->ops->is_authenticated(monc->auth)) {
756 dout("authenticated, starting session\n");
757 __init_authenticated_client(monc);
758 __send_subscribe(monc);
759 __resend_statfs(monc);
760 }
761 mutex_unlock(&monc->mutex);
762}
763
ba75bb98
SW
764/*
765 * handle incoming message
766 */
767static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
768{
769 struct ceph_mon_client *monc = con->private;
770 int type = le16_to_cpu(msg->hdr.type);
771
772 if (!monc)
773 return;
774
775 switch (type) {
4e7a5dcd
SW
776 case CEPH_MSG_AUTH_REPLY:
777 handle_auth_reply(monc, msg);
ba75bb98
SW
778 break;
779
780 case CEPH_MSG_MON_SUBSCRIBE_ACK:
781 handle_subscribe_ack(monc, msg);
782 break;
783
784 case CEPH_MSG_STATFS_REPLY:
785 handle_statfs_reply(monc, msg);
786 break;
787
4e7a5dcd
SW
788 case CEPH_MSG_MON_MAP:
789 ceph_monc_handle_map(monc, msg);
790 break;
791
ba75bb98
SW
792 case CEPH_MSG_MDS_MAP:
793 ceph_mdsc_handle_map(&monc->client->mdsc, msg);
794 break;
795
796 case CEPH_MSG_OSD_MAP:
797 ceph_osdc_handle_map(&monc->client->osdc, msg);
798 break;
799
800 default:
801 pr_err("received unknown message type %d %s\n", type,
802 ceph_msg_type_name(type));
803 }
804 ceph_msg_put(msg);
805}
806
807/*
808 * Allocate memory for incoming message
809 */
810static struct ceph_msg *mon_alloc_msg(struct ceph_connection *con,
811 struct ceph_msg_header *hdr)
812{
813 struct ceph_mon_client *monc = con->private;
814 int type = le16_to_cpu(hdr->type);
8f3bc053 815 int front = le32_to_cpu(hdr->front_len);
ba75bb98
SW
816
817 switch (type) {
ba75bb98 818 case CEPH_MSG_MON_SUBSCRIBE_ACK:
8f3bc053 819 return ceph_msgpool_get(&monc->msgpool_subscribe_ack, front);
ba75bb98 820 case CEPH_MSG_STATFS_REPLY:
8f3bc053 821 return ceph_msgpool_get(&monc->msgpool_statfs_reply, front);
4e7a5dcd
SW
822 case CEPH_MSG_AUTH_REPLY:
823 return ceph_msgpool_get(&monc->msgpool_auth_reply, front);
ba75bb98
SW
824 }
825 return ceph_alloc_msg(con, hdr);
826}
827
828/*
829 * If the monitor connection resets, pick a new monitor and resubmit
830 * any pending requests.
831 */
832static void mon_fault(struct ceph_connection *con)
833{
834 struct ceph_mon_client *monc = con->private;
835
836 if (!monc)
837 return;
838
839 dout("mon_fault\n");
840 mutex_lock(&monc->mutex);
841 if (!con->private)
842 goto out;
843
844 if (monc->con && !monc->hunting)
845 pr_info("mon%d %s session lost, "
846 "hunting for new mon\n", monc->cur_mon,
847 pr_addr(&monc->con->peer_addr.in_addr));
848
849 __close_session(monc);
850 if (!monc->hunting) {
851 /* start hunting */
852 monc->hunting = true;
4e7a5dcd 853 __open_session(monc);
ba75bb98
SW
854 } else {
855 /* already hunting, let's wait a bit */
856 __schedule_delayed(monc);
857 }
858out:
859 mutex_unlock(&monc->mutex);
860}
861
862const static struct ceph_connection_operations mon_con_ops = {
863 .get = ceph_con_get,
864 .put = ceph_con_put,
865 .dispatch = dispatch,
866 .fault = mon_fault,
867 .alloc_msg = mon_alloc_msg,
868 .alloc_middle = ceph_alloc_middle,
869};
This page took 0.088527 seconds and 5 git commands to generate.