1 #include "ceph_debug.h"
3 #include <linux/types.h>
4 #include <linux/random.h>
5 #include <linux/sched.h>
7 #include "mon_client.h"
12 * Interact with Ceph monitor cluster. Handle requests for new map
13 * versions, and periodically resend as needed. Also implement
14 * statfs() and umount().
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.
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.
29 const static struct ceph_connection_operations mon_con_ops;
32 * Decode a monmap blob (e.g., during mount).
34 struct ceph_monmap *ceph_monmap_decode(void *p, void *end)
36 struct ceph_monmap *m = NULL;
38 struct ceph_fsid fsid;
42 dout("monmap_decode %p %p len %d\n", p, end, (int)(end-p));
44 ceph_decode_16_safe(&p, end, version, bad);
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);
50 ceph_decode_32(&p, num_mon);
51 ceph_decode_need(&p, end, num_mon*sizeof(m->mon_inst[0]), bad);
53 if (num_mon >= CEPH_MAX_MON)
55 m = kmalloc(sizeof(*m) + sizeof(m->mon_inst[0])*num_mon, GFP_NOFS);
57 return ERR_PTR(-ENOMEM);
61 ceph_decode_copy(&p, m->mon_inst, num_mon*sizeof(m->mon_inst[0]));
66 dout("monmap_decode epoch %d, num_mon %d\n", m->epoch,
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));
74 dout("monmap_decode failed with %d\n", err);
80 * return true if *addr is included in the monmap.
82 int ceph_monmap_contains(struct ceph_monmap *m, struct ceph_entity_addr *addr)
86 for (i = 0; i < m->num_mon; i++)
87 if (ceph_entity_addr_equal(addr, &m->mon_inst[i].addr))
93 * Close monitor session, if any.
95 static void __close_session(struct ceph_mon_client *monc)
98 dout("__close_session closing mon%d\n", monc->cur_mon);
99 ceph_con_close(monc->con);
105 * Open a session with a (new) monitor.
107 static int __open_session(struct ceph_mon_client *monc)
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);
117 monc->sub_renew_after = jiffies; /* i.e., expired */
118 monc->want_next_osdmap = !!monc->want_next_osdmap;
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);
126 dout("open_session mon%d already open\n", monc->cur_mon);
131 static bool __sub_expired(struct ceph_mon_client *monc)
133 return time_after_eq(jiffies, monc->sub_renew_after);
137 * Reschedule delayed work timer.
139 static void __schedule_delayed(struct ceph_mon_client *monc)
143 if (monc->cur_mon < 0 || monc->want_mount || __sub_expired(monc))
147 dout("__schedule_delayed after %u\n", delay);
148 schedule_delayed_work(&monc->delayed_work, delay);
152 * Send subscribe request for mdsmap and/or osdmap.
154 static void __send_subscribe(struct ceph_mon_client *monc)
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;
165 msg = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 64, 0, 0, NULL);
169 p = msg->front.iov_base;
170 end = p + msg->front.iov_len;
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);
180 i->have = cpu_to_le64(monc->have_osdmap);
183 monc->want_next_osdmap = 2; /* requested */
185 ceph_encode_32(&p, 1);
187 ceph_encode_string(&p, end, "mdsmap", 6);
189 i->have = cpu_to_le64(monc->have_mdsmap);
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);
197 monc->sub_sent = jiffies | 1; /* never 0 */
201 static void handle_subscribe_ack(struct ceph_mon_client *monc,
202 struct ceph_msg *msg)
205 void *p = msg->front.iov_base;
206 void *end = p + msg->front.iov_len;
208 ceph_decode_32_safe(&p, end, seconds, bad);
209 mutex_lock(&monc->mutex);
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;
215 dout("handle_subscribe_ack after %d seconds\n", seconds);
216 monc->sub_renew_after = monc->sub_sent + seconds*HZ - 1;
218 mutex_unlock(&monc->mutex);
221 pr_err("got corrupt subscribe-ack msg\n");
225 * Keep track of which maps we have
227 int ceph_monc_got_mdsmap(struct ceph_mon_client *monc, u32 got)
229 mutex_lock(&monc->mutex);
230 monc->have_mdsmap = got;
231 mutex_unlock(&monc->mutex);
235 int ceph_monc_got_osdmap(struct ceph_mon_client *monc, u32 got)
237 mutex_lock(&monc->mutex);
238 monc->have_osdmap = got;
239 monc->want_next_osdmap = 0;
240 mutex_unlock(&monc->mutex);
245 * Register interest in the next osdmap
247 void ceph_monc_request_next_osdmap(struct ceph_mon_client *monc)
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);
262 static void __request_mount(struct ceph_mon_client *monc)
264 struct ceph_msg *msg;
265 struct ceph_client_mount *h;
268 dout("__request_mount\n");
269 err = __open_session(monc);
272 msg = ceph_msg_new(CEPH_MSG_CLIENT_MOUNT, sizeof(*h), 0, 0, NULL);
275 h = msg->front.iov_base;
277 ceph_con_send(monc->con, msg);
280 int ceph_monc_request_mount(struct ceph_mon_client *monc)
283 monc->con = kmalloc(sizeof(*monc->con), GFP_KERNEL);
286 ceph_con_init(monc->client->msgr, monc->con);
287 monc->con->private = monc;
288 monc->con->ops = &mon_con_ops;
291 mutex_lock(&monc->mutex);
292 __request_mount(monc);
293 __schedule_delayed(monc);
294 mutex_unlock(&monc->mutex);
299 * The monitor responds with mount ack indicate mount success. The
300 * included client ticket allows the client to talk to MDSs and OSDs.
302 static void handle_mount_ack(struct ceph_mon_client *monc, struct ceph_msg *msg)
304 struct ceph_client *client = monc->client;
305 struct ceph_monmap *monmap = NULL, *old = monc->monmap;
312 if (client->whoami >= 0) {
313 dout("handle_mount_ack - already mounted\n");
317 mutex_lock(&monc->mutex);
319 dout("handle_mount_ack\n");
320 p = msg->front.iov_base;
321 end = p + msg->front.iov_len;
323 ceph_decode_64_safe(&p, end, cnum, bad);
324 ceph_decode_32_safe(&p, end, result, bad);
325 ceph_decode_32_safe(&p, end, len, bad);
327 pr_err("mount denied: %.*s (%d)\n", len, (char *)p,
334 ceph_decode_32_safe(&p, end, len, bad);
335 ceph_decode_need(&p, end, len, bad);
336 monmap = ceph_monmap_decode(p, p + len);
337 if (IS_ERR(monmap)) {
338 pr_err("problem decoding monmap, %d\n",
339 (int)PTR_ERR(monmap));
345 client->monc.monmap = monmap;
348 client->signed_ticket = NULL;
349 client->signed_ticket_len = 0;
351 monc->want_mount = false;
353 client->whoami = cnum;
354 client->msgr->inst.name.type = CEPH_ENTITY_TYPE_CLIENT;
355 client->msgr->inst.name.num = cpu_to_le64(cnum);
356 pr_info("client%lld fsid " FSID_FORMAT "\n",
357 client->whoami, PR_FSID(&client->monc.monmap->fsid));
359 ceph_debugfs_client_init(client);
360 __send_subscribe(monc);
366 pr_err("error decoding mount_ack message\n");
368 client->mount_err = err;
369 mutex_unlock(&monc->mutex);
370 wake_up(&client->mount_wq);
379 static void handle_statfs_reply(struct ceph_mon_client *monc,
380 struct ceph_msg *msg)
382 struct ceph_mon_statfs_request *req;
383 struct ceph_mon_statfs_reply *reply = msg->front.iov_base;
386 if (msg->front.iov_len != sizeof(*reply))
388 tid = le64_to_cpu(reply->tid);
389 dout("handle_statfs_reply %p tid %llu\n", msg, tid);
391 mutex_lock(&monc->mutex);
392 req = radix_tree_lookup(&monc->statfs_request_tree, tid);
394 *req->buf = reply->st;
397 mutex_unlock(&monc->mutex);
399 complete(&req->completion);
403 pr_err("corrupt statfs reply, no tid\n");
407 * (re)send a statfs request
409 static int send_statfs(struct ceph_mon_client *monc,
410 struct ceph_mon_statfs_request *req)
412 struct ceph_msg *msg;
413 struct ceph_mon_statfs *h;
416 dout("send_statfs tid %llu\n", req->tid);
417 err = __open_session(monc);
420 msg = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), 0, 0, NULL);
424 h = msg->front.iov_base;
426 h->fsid = monc->monmap->fsid;
427 h->tid = cpu_to_le64(req->tid);
428 ceph_con_send(monc->con, msg);
433 * Do a synchronous statfs().
435 int ceph_monc_do_statfs(struct ceph_mon_client *monc, struct ceph_statfs *buf)
437 struct ceph_mon_statfs_request req;
441 init_completion(&req.completion);
443 /* allocate memory for reply */
444 err = ceph_msgpool_resv(&monc->msgpool_statfs_reply, 1);
448 /* register request */
449 mutex_lock(&monc->mutex);
450 req.tid = ++monc->last_tid;
451 req.last_attempt = jiffies;
452 req.delay = BASE_DELAY_INTERVAL;
453 if (radix_tree_insert(&monc->statfs_request_tree, req.tid, &req) < 0) {
454 mutex_unlock(&monc->mutex);
455 pr_err("ENOMEM in do_statfs\n");
458 monc->num_statfs_requests++;
459 mutex_unlock(&monc->mutex);
461 /* send request and wait */
462 err = send_statfs(monc, &req);
464 err = wait_for_completion_interruptible(&req.completion);
466 mutex_lock(&monc->mutex);
467 radix_tree_delete(&monc->statfs_request_tree, req.tid);
468 monc->num_statfs_requests--;
469 ceph_msgpool_resv(&monc->msgpool_statfs_reply, -1);
470 mutex_unlock(&monc->mutex);
478 * Resend pending statfs requests.
480 static void __resend_statfs(struct ceph_mon_client *monc)
485 struct ceph_mon_statfs_request *req;
488 got = radix_tree_gang_lookup(&monc->statfs_request_tree,
494 next_tid = req->tid + 1;
496 send_statfs(monc, req);
501 * Delayed work. If we haven't mounted yet, retry. Otherwise,
502 * renew/retry subscription as needed (in case it is timing out, or we
503 * got an ENOMEM). And keep the monitor connection alive.
505 static void delayed_work(struct work_struct *work)
507 struct ceph_mon_client *monc =
508 container_of(work, struct ceph_mon_client, delayed_work.work);
510 dout("monc delayed_work\n");
511 mutex_lock(&monc->mutex);
512 if (monc->want_mount) {
513 __request_mount(monc);
515 if (__sub_expired(monc)) {
516 __close_session(monc);
517 __open_session(monc); /* continue hunting */
519 ceph_con_keepalive(monc->con);
522 __send_subscribe(monc);
523 __schedule_delayed(monc);
524 mutex_unlock(&monc->mutex);
527 int ceph_monc_init(struct ceph_mon_client *monc, struct ceph_client *cl)
532 memset(monc, 0, sizeof(*monc));
535 mutex_init(&monc->mutex);
540 err = ceph_msgpool_init(&monc->msgpool_mount_ack, 4096, 1, false);
543 err = ceph_msgpool_init(&monc->msgpool_subscribe_ack, 8, 1, false);
546 err = ceph_msgpool_init(&monc->msgpool_statfs_reply,
547 sizeof(struct ceph_mon_statfs_reply), 0, false);
552 monc->hunting = false; /* not really */
553 monc->sub_renew_after = jiffies;
556 INIT_DELAYED_WORK(&monc->delayed_work, delayed_work);
557 INIT_RADIX_TREE(&monc->statfs_request_tree, GFP_NOFS);
558 monc->num_statfs_requests = 0;
561 monc->have_mdsmap = 0;
562 monc->have_osdmap = 0;
563 monc->want_next_osdmap = 1;
564 monc->want_mount = true;
569 void ceph_monc_stop(struct ceph_mon_client *monc)
572 cancel_delayed_work_sync(&monc->delayed_work);
574 mutex_lock(&monc->mutex);
575 __close_session(monc);
577 monc->con->private = NULL;
578 monc->con->ops->put(monc->con);
581 mutex_unlock(&monc->mutex);
583 ceph_msgpool_destroy(&monc->msgpool_mount_ack);
584 ceph_msgpool_destroy(&monc->msgpool_subscribe_ack);
585 ceph_msgpool_destroy(&monc->msgpool_statfs_reply);
592 * handle incoming message
594 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
596 struct ceph_mon_client *monc = con->private;
597 int type = le16_to_cpu(msg->hdr.type);
603 case CEPH_MSG_CLIENT_MOUNT_ACK:
604 handle_mount_ack(monc, msg);
607 case CEPH_MSG_MON_SUBSCRIBE_ACK:
608 handle_subscribe_ack(monc, msg);
611 case CEPH_MSG_STATFS_REPLY:
612 handle_statfs_reply(monc, msg);
615 case CEPH_MSG_MDS_MAP:
616 ceph_mdsc_handle_map(&monc->client->mdsc, msg);
619 case CEPH_MSG_OSD_MAP:
620 ceph_osdc_handle_map(&monc->client->osdc, msg);
624 pr_err("received unknown message type %d %s\n", type,
625 ceph_msg_type_name(type));
631 * Allocate memory for incoming message
633 static struct ceph_msg *mon_alloc_msg(struct ceph_connection *con,
634 struct ceph_msg_header *hdr)
636 struct ceph_mon_client *monc = con->private;
637 int type = le16_to_cpu(hdr->type);
640 case CEPH_MSG_CLIENT_MOUNT_ACK:
641 return ceph_msgpool_get(&monc->msgpool_mount_ack);
642 case CEPH_MSG_MON_SUBSCRIBE_ACK:
643 return ceph_msgpool_get(&monc->msgpool_subscribe_ack);
644 case CEPH_MSG_STATFS_REPLY:
645 return ceph_msgpool_get(&monc->msgpool_statfs_reply);
647 return ceph_alloc_msg(con, hdr);
651 * If the monitor connection resets, pick a new monitor and resubmit
652 * any pending requests.
654 static void mon_fault(struct ceph_connection *con)
656 struct ceph_mon_client *monc = con->private;
662 mutex_lock(&monc->mutex);
666 if (monc->con && !monc->hunting)
667 pr_info("mon%d %s session lost, "
668 "hunting for new mon\n", monc->cur_mon,
669 pr_addr(&monc->con->peer_addr.in_addr));
671 __close_session(monc);
672 if (!monc->hunting) {
674 monc->hunting = true;
675 if (__open_session(monc) == 0) {
676 __send_subscribe(monc);
677 __resend_statfs(monc);
680 /* already hunting, let's wait a bit */
681 __schedule_delayed(monc);
684 mutex_unlock(&monc->mutex);
687 const static struct ceph_connection_operations mon_con_ops = {
690 .dispatch = dispatch,
692 .alloc_msg = mon_alloc_msg,
693 .alloc_middle = ceph_alloc_middle,