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