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