blob: b742b3b3e0f394f1bd549f4dee54438ec934b2e5 [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"
Sage Weil4e7a5dc2009-11-18 16:19:57 -08009#include "auth.h"
Sage Weilba75bb92009-10-06 11:31:11 -070010#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;
Sage Weil4e7a5dc2009-11-18 16:19:57 -080042 u32 len;
43
44 ceph_decode_32_safe(&p, end, len, bad);
45 ceph_decode_need(&p, end, len, bad);
Sage Weilba75bb92009-10-06 11:31:11 -070046
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));
Sage Weilc89136e2009-10-14 09:59:09 -070053 epoch = ceph_decode_32(&p);
Sage Weilba75bb92009-10-06 11:31:11 -070054
Sage Weilc89136e2009-10-14 09:59:09 -070055 num_mon = ceph_decode_32(&p);
Sage Weilba75bb92009-10-06 11:31:11 -070056 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]));
Sage Weil63f2d212009-11-03 15:17:56 -080067 for (i = 0; i < num_mon; i++)
68 ceph_decode_addr(&m->mon_inst[i].addr);
Sage Weilba75bb92009-10-06 11:31:11 -070069
Sage Weilba75bb92009-10-06 11:31:11 -070070 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);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800103 ceph_con_revoke(monc->con, monc->m_auth);
Sage Weilba75bb92009-10-06 11:31:11 -0700104 ceph_con_close(monc->con);
105 monc->cur_mon = -1;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800106 ceph_auth_reset(monc->auth);
Sage Weilba75bb92009-10-06 11:31:11 -0700107 }
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;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800116 int ret;
Sage Weilba75bb92009-10-06 11:31:11 -0700117
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);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800132
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);
Sage Weilba75bb92009-10-06 11:31:11 -0700141 } 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
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800159 if (monc->cur_mon < 0 || __sub_expired(monc))
Sage Weilba75bb92009-10-06 11:31:11 -0700160 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
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800181 msg = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 96, 0, 0, NULL);
Sage Weilba75bb92009-10-06 11:31:11 -0700182 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);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800193 ceph_encode_32(&p, 3);
Sage Weilba75bb92009-10-06 11:31:11 -0700194 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 {
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800201 ceph_encode_32(&p, 2);
Sage Weilba75bb92009-10-06 11:31:11 -0700202 }
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);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800208 ceph_encode_string(&p, end, "monmap", 6);
209 i = p;
210 i->have = 0;
211 i->onetime = 0;
212 p += sizeof(*i);
Sage Weilba75bb92009-10-06 11:31:11 -0700213
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;
Sage Weil07bd10f2009-10-14 17:26:40 -0700226 struct ceph_mon_subscribe_ack *h = msg->front.iov_base;
Sage Weilba75bb92009-10-06 11:31:11 -0700227
Sage Weil07bd10f2009-10-14 17:26:40 -0700228 if (msg->front.iov_len < sizeof(*h))
229 goto bad;
230 seconds = le32_to_cpu(h->duration);
231
Sage Weilba75bb92009-10-06 11:31:11 -0700232 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);
Sage Weil0656d112009-10-08 10:25:46 -0700239 monc->sub_renew_after = monc->sub_sent + (seconds >> 1)*HZ - 1;
Sage Weilba75bb92009-10-06 11:31:11 -0700240 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
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800281#if 0
Sage Weilba75bb92009-10-06 11:31:11 -0700282/*
283 * mount
284 */
285static void __request_mount(struct ceph_mon_client *monc)
286{
287 struct ceph_msg *msg;
288 struct ceph_client_mount *h;
Sage Weilba75bb92009-10-06 11:31:11 -0700289
290 dout("__request_mount\n");
Sage Weilba75bb92009-10-06 11:31:11 -0700291 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;
Sage Weil13e38c82009-10-09 16:36:34 -0700295 h->monhdr.have_version = 0;
296 h->monhdr.session_mon = cpu_to_le16(-1);
297 h->monhdr.session_mon_tid = 0;
Sage Weilba75bb92009-10-06 11:31:11 -0700298 ceph_con_send(monc->con, msg);
299}
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800300#endif
Sage Weilba75bb92009-10-06 11:31:11 -0700301
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800302/*
303 *
304 */
305int ceph_monc_open_session(struct ceph_mon_client *monc)
Sage Weilba75bb92009-10-06 11:31:11 -0700306{
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);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800317 __open_session(monc);
Sage Weilba75bb92009-10-06 11:31:11 -0700318 __schedule_delayed(monc);
319 mutex_unlock(&monc->mutex);
320 return 0;
321}
322
323/*
324 * The monitor responds with mount ack indicate mount success. The
325 * included client ticket allows the client to talk to MDSs and OSDs.
326 */
Sage Weil07433042009-11-18 16:50:41 -0800327static void ceph_monc_handle_map(struct ceph_mon_client *monc,
328 struct ceph_msg *msg)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800329{
330 struct ceph_client *client = monc->client;
331 struct ceph_monmap *monmap = NULL, *old = monc->monmap;
332 void *p, *end;
333
334 mutex_lock(&monc->mutex);
335
336 dout("handle_monmap\n");
337 p = msg->front.iov_base;
338 end = p + msg->front.iov_len;
339
340 monmap = ceph_monmap_decode(p, end);
341 if (IS_ERR(monmap)) {
342 pr_err("problem decoding monmap, %d\n",
343 (int)PTR_ERR(monmap));
344 return;
345 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800346
Sage Weil07433042009-11-18 16:50:41 -0800347 if (ceph_check_fsid(monc->client, &monmap->fsid) < 0) {
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800348 kfree(monmap);
349 return;
350 }
351
352 client->monc.monmap = monmap;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800353 kfree(old);
354
355 mutex_unlock(&monc->mutex);
356 wake_up(&client->mount_wq);
357}
Sage Weilba75bb92009-10-06 11:31:11 -0700358
Sage Weilba75bb92009-10-06 11:31:11 -0700359/*
360 * statfs
361 */
362static void handle_statfs_reply(struct ceph_mon_client *monc,
363 struct ceph_msg *msg)
364{
365 struct ceph_mon_statfs_request *req;
366 struct ceph_mon_statfs_reply *reply = msg->front.iov_base;
367 u64 tid;
368
369 if (msg->front.iov_len != sizeof(*reply))
370 goto bad;
371 tid = le64_to_cpu(reply->tid);
372 dout("handle_statfs_reply %p tid %llu\n", msg, tid);
373
374 mutex_lock(&monc->mutex);
375 req = radix_tree_lookup(&monc->statfs_request_tree, tid);
376 if (req) {
377 *req->buf = reply->st;
378 req->result = 0;
379 }
380 mutex_unlock(&monc->mutex);
381 if (req)
382 complete(&req->completion);
383 return;
384
385bad:
386 pr_err("corrupt statfs reply, no tid\n");
387}
388
389/*
390 * (re)send a statfs request
391 */
392static int send_statfs(struct ceph_mon_client *monc,
393 struct ceph_mon_statfs_request *req)
394{
395 struct ceph_msg *msg;
396 struct ceph_mon_statfs *h;
Sage Weilba75bb92009-10-06 11:31:11 -0700397
398 dout("send_statfs tid %llu\n", req->tid);
Sage Weilba75bb92009-10-06 11:31:11 -0700399 msg = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), 0, 0, NULL);
400 if (IS_ERR(msg))
401 return PTR_ERR(msg);
402 req->request = msg;
403 h = msg->front.iov_base;
Sage Weil13e38c82009-10-09 16:36:34 -0700404 h->monhdr.have_version = 0;
405 h->monhdr.session_mon = cpu_to_le16(-1);
406 h->monhdr.session_mon_tid = 0;
Sage Weilba75bb92009-10-06 11:31:11 -0700407 h->fsid = monc->monmap->fsid;
408 h->tid = cpu_to_le64(req->tid);
409 ceph_con_send(monc->con, msg);
410 return 0;
411}
412
413/*
414 * Do a synchronous statfs().
415 */
416int ceph_monc_do_statfs(struct ceph_mon_client *monc, struct ceph_statfs *buf)
417{
418 struct ceph_mon_statfs_request req;
419 int err;
420
421 req.buf = buf;
422 init_completion(&req.completion);
423
424 /* allocate memory for reply */
425 err = ceph_msgpool_resv(&monc->msgpool_statfs_reply, 1);
426 if (err)
427 return err;
428
429 /* register request */
430 mutex_lock(&monc->mutex);
431 req.tid = ++monc->last_tid;
432 req.last_attempt = jiffies;
433 req.delay = BASE_DELAY_INTERVAL;
434 if (radix_tree_insert(&monc->statfs_request_tree, req.tid, &req) < 0) {
435 mutex_unlock(&monc->mutex);
436 pr_err("ENOMEM in do_statfs\n");
437 return -ENOMEM;
438 }
439 monc->num_statfs_requests++;
440 mutex_unlock(&monc->mutex);
441
442 /* send request and wait */
443 err = send_statfs(monc, &req);
444 if (!err)
445 err = wait_for_completion_interruptible(&req.completion);
446
447 mutex_lock(&monc->mutex);
448 radix_tree_delete(&monc->statfs_request_tree, req.tid);
449 monc->num_statfs_requests--;
450 ceph_msgpool_resv(&monc->msgpool_statfs_reply, -1);
451 mutex_unlock(&monc->mutex);
452
453 if (!err)
454 err = req.result;
455 return err;
456}
457
458/*
459 * Resend pending statfs requests.
460 */
461static void __resend_statfs(struct ceph_mon_client *monc)
462{
463 u64 next_tid = 0;
464 int got;
465 int did = 0;
466 struct ceph_mon_statfs_request *req;
467
468 while (1) {
469 got = radix_tree_gang_lookup(&monc->statfs_request_tree,
470 (void **)&req,
471 next_tid, 1);
472 if (got == 0)
473 break;
474 did++;
475 next_tid = req->tid + 1;
476
477 send_statfs(monc, req);
478 }
479}
480
481/*
482 * Delayed work. If we haven't mounted yet, retry. Otherwise,
483 * renew/retry subscription as needed (in case it is timing out, or we
484 * got an ENOMEM). And keep the monitor connection alive.
485 */
486static void delayed_work(struct work_struct *work)
487{
488 struct ceph_mon_client *monc =
489 container_of(work, struct ceph_mon_client, delayed_work.work);
490
491 dout("monc delayed_work\n");
492 mutex_lock(&monc->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800493 if (monc->hunting) {
494 __close_session(monc);
495 __open_session(monc); /* continue hunting */
Sage Weilba75bb92009-10-06 11:31:11 -0700496 } else {
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800497 ceph_con_keepalive(monc->con);
498 if (monc->auth->ops->is_authenticated(monc->auth))
499 __send_subscribe(monc);
Sage Weilba75bb92009-10-06 11:31:11 -0700500 }
Sage Weilba75bb92009-10-06 11:31:11 -0700501 __schedule_delayed(monc);
502 mutex_unlock(&monc->mutex);
503}
504
Sage Weil6b805182009-10-27 11:50:50 -0700505/*
506 * On startup, we build a temporary monmap populated with the IPs
507 * provided by mount(2).
508 */
509static int build_initial_monmap(struct ceph_mon_client *monc)
510{
511 struct ceph_mount_args *args = monc->client->mount_args;
512 struct ceph_entity_addr *mon_addr = args->mon_addr;
513 int num_mon = args->num_mon;
514 int i;
515
516 /* build initial monmap */
517 monc->monmap = kzalloc(sizeof(*monc->monmap) +
518 num_mon*sizeof(monc->monmap->mon_inst[0]),
519 GFP_KERNEL);
520 if (!monc->monmap)
521 return -ENOMEM;
522 for (i = 0; i < num_mon; i++) {
523 monc->monmap->mon_inst[i].addr = mon_addr[i];
524 monc->monmap->mon_inst[i].addr.erank = 0;
525 monc->monmap->mon_inst[i].addr.nonce = 0;
526 monc->monmap->mon_inst[i].name.type =
527 CEPH_ENTITY_TYPE_MON;
528 monc->monmap->mon_inst[i].name.num = cpu_to_le64(i);
529 }
530 monc->monmap->num_mon = num_mon;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800531 monc->have_fsid = false;
Sage Weil6b805182009-10-27 11:50:50 -0700532
533 /* release addr memory */
534 kfree(args->mon_addr);
535 args->mon_addr = NULL;
536 args->num_mon = 0;
537 return 0;
538}
539
Sage Weilba75bb92009-10-06 11:31:11 -0700540int ceph_monc_init(struct ceph_mon_client *monc, struct ceph_client *cl)
541{
542 int err = 0;
543
544 dout("init\n");
545 memset(monc, 0, sizeof(*monc));
546 monc->client = cl;
547 monc->monmap = NULL;
548 mutex_init(&monc->mutex);
549
Sage Weil6b805182009-10-27 11:50:50 -0700550 err = build_initial_monmap(monc);
551 if (err)
552 goto out;
553
Sage Weilba75bb92009-10-06 11:31:11 -0700554 monc->con = NULL;
555
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800556 /* authentication */
557 monc->auth = ceph_auth_init(cl->mount_args->name,
558 cl->mount_args->secret);
559 if (IS_ERR(monc->auth))
560 return PTR_ERR(monc->auth);
561 monc->auth->want_keys =
562 CEPH_ENTITY_TYPE_AUTH | CEPH_ENTITY_TYPE_MON |
563 CEPH_ENTITY_TYPE_OSD | CEPH_ENTITY_TYPE_MDS;
564
Sage Weilba75bb92009-10-06 11:31:11 -0700565 /* msg pools */
Sage Weil07bd10f2009-10-14 17:26:40 -0700566 err = ceph_msgpool_init(&monc->msgpool_subscribe_ack,
567 sizeof(struct ceph_mon_subscribe_ack), 1, false);
Sage Weilba75bb92009-10-06 11:31:11 -0700568 if (err < 0)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800569 goto out_monmap;
Sage Weilba75bb92009-10-06 11:31:11 -0700570 err = ceph_msgpool_init(&monc->msgpool_statfs_reply,
571 sizeof(struct ceph_mon_statfs_reply), 0, false);
572 if (err < 0)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800573 goto out_pool1;
574 err = ceph_msgpool_init(&monc->msgpool_auth_reply, 4096, 1, false);
575 if (err < 0)
576 goto out_pool2;
577
578 monc->m_auth = ceph_msg_new(CEPH_MSG_AUTH, 4096, 0, 0, NULL);
579 if (IS_ERR(monc->m_auth)) {
580 err = PTR_ERR(monc->m_auth);
581 monc->m_auth = NULL;
582 goto out_pool3;
583 }
Sage Weilba75bb92009-10-06 11:31:11 -0700584
585 monc->cur_mon = -1;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800586 monc->hunting = true;
Sage Weilba75bb92009-10-06 11:31:11 -0700587 monc->sub_renew_after = jiffies;
588 monc->sub_sent = 0;
589
590 INIT_DELAYED_WORK(&monc->delayed_work, delayed_work);
591 INIT_RADIX_TREE(&monc->statfs_request_tree, GFP_NOFS);
592 monc->num_statfs_requests = 0;
593 monc->last_tid = 0;
594
595 monc->have_mdsmap = 0;
596 monc->have_osdmap = 0;
597 monc->want_next_osdmap = 1;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800598 return 0;
599
600out_pool3:
601 ceph_msgpool_destroy(&monc->msgpool_auth_reply);
602out_pool2:
603 ceph_msgpool_destroy(&monc->msgpool_subscribe_ack);
604out_pool1:
605 ceph_msgpool_destroy(&monc->msgpool_statfs_reply);
606out_monmap:
607 kfree(monc->monmap);
Sage Weilba75bb92009-10-06 11:31:11 -0700608out:
609 return err;
610}
611
612void ceph_monc_stop(struct ceph_mon_client *monc)
613{
614 dout("stop\n");
615 cancel_delayed_work_sync(&monc->delayed_work);
616
617 mutex_lock(&monc->mutex);
618 __close_session(monc);
619 if (monc->con) {
620 monc->con->private = NULL;
621 monc->con->ops->put(monc->con);
622 monc->con = NULL;
623 }
624 mutex_unlock(&monc->mutex);
625
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800626 ceph_auth_destroy(monc->auth);
627
628 ceph_msg_put(monc->m_auth);
Sage Weilba75bb92009-10-06 11:31:11 -0700629 ceph_msgpool_destroy(&monc->msgpool_subscribe_ack);
630 ceph_msgpool_destroy(&monc->msgpool_statfs_reply);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800631 ceph_msgpool_destroy(&monc->msgpool_auth_reply);
Sage Weilba75bb92009-10-06 11:31:11 -0700632
633 kfree(monc->monmap);
634}
635
636
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800637static void handle_auth_reply(struct ceph_mon_client *monc,
638 struct ceph_msg *msg)
639{
640 int ret;
641
642 mutex_lock(&monc->mutex);
643 ret = ceph_handle_auth_reply(monc->auth, msg->front.iov_base,
644 msg->front.iov_len,
645 monc->m_auth->front.iov_base,
646 monc->m_auth->front_max);
647 if (ret < 0) {
648 monc->client->mount_err = ret;
649 wake_up(&monc->client->mount_wq);
650 } else if (ret > 0) {
651 monc->m_auth->front.iov_len = ret;
652 monc->m_auth->hdr.front_len = cpu_to_le32(ret);
653 ceph_msg_get(monc->m_auth); /* keep our ref */
654 ceph_con_send(monc->con, monc->m_auth);
655 } else if (monc->auth->ops->is_authenticated(monc->auth)) {
656 dout("authenticated, starting session\n");
Sage Weil07433042009-11-18 16:50:41 -0800657
658 monc->client->msgr->inst.name.type = CEPH_ENTITY_TYPE_CLIENT;
659 monc->client->msgr->inst.name.num = monc->auth->global_id;
660
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800661 __send_subscribe(monc);
662 __resend_statfs(monc);
663 }
664 mutex_unlock(&monc->mutex);
665}
666
Sage Weilba75bb92009-10-06 11:31:11 -0700667/*
668 * handle incoming message
669 */
670static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
671{
672 struct ceph_mon_client *monc = con->private;
673 int type = le16_to_cpu(msg->hdr.type);
674
675 if (!monc)
676 return;
677
678 switch (type) {
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800679 case CEPH_MSG_AUTH_REPLY:
680 handle_auth_reply(monc, msg);
Sage Weilba75bb92009-10-06 11:31:11 -0700681 break;
682
683 case CEPH_MSG_MON_SUBSCRIBE_ACK:
684 handle_subscribe_ack(monc, msg);
685 break;
686
687 case CEPH_MSG_STATFS_REPLY:
688 handle_statfs_reply(monc, msg);
689 break;
690
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800691 case CEPH_MSG_MON_MAP:
692 ceph_monc_handle_map(monc, msg);
693 break;
694
Sage Weilba75bb92009-10-06 11:31:11 -0700695 case CEPH_MSG_MDS_MAP:
696 ceph_mdsc_handle_map(&monc->client->mdsc, msg);
697 break;
698
699 case CEPH_MSG_OSD_MAP:
700 ceph_osdc_handle_map(&monc->client->osdc, msg);
701 break;
702
703 default:
704 pr_err("received unknown message type %d %s\n", type,
705 ceph_msg_type_name(type));
706 }
707 ceph_msg_put(msg);
708}
709
710/*
711 * Allocate memory for incoming message
712 */
713static struct ceph_msg *mon_alloc_msg(struct ceph_connection *con,
714 struct ceph_msg_header *hdr)
715{
716 struct ceph_mon_client *monc = con->private;
717 int type = le16_to_cpu(hdr->type);
Sage Weil8f3bc052009-10-14 17:36:07 -0700718 int front = le32_to_cpu(hdr->front_len);
Sage Weilba75bb92009-10-06 11:31:11 -0700719
720 switch (type) {
Sage Weilba75bb92009-10-06 11:31:11 -0700721 case CEPH_MSG_MON_SUBSCRIBE_ACK:
Sage Weil8f3bc052009-10-14 17:36:07 -0700722 return ceph_msgpool_get(&monc->msgpool_subscribe_ack, front);
Sage Weilba75bb92009-10-06 11:31:11 -0700723 case CEPH_MSG_STATFS_REPLY:
Sage Weil8f3bc052009-10-14 17:36:07 -0700724 return ceph_msgpool_get(&monc->msgpool_statfs_reply, front);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800725 case CEPH_MSG_AUTH_REPLY:
726 return ceph_msgpool_get(&monc->msgpool_auth_reply, front);
Sage Weilba75bb92009-10-06 11:31:11 -0700727 }
728 return ceph_alloc_msg(con, hdr);
729}
730
731/*
732 * If the monitor connection resets, pick a new monitor and resubmit
733 * any pending requests.
734 */
735static void mon_fault(struct ceph_connection *con)
736{
737 struct ceph_mon_client *monc = con->private;
738
739 if (!monc)
740 return;
741
742 dout("mon_fault\n");
743 mutex_lock(&monc->mutex);
744 if (!con->private)
745 goto out;
746
747 if (monc->con && !monc->hunting)
748 pr_info("mon%d %s session lost, "
749 "hunting for new mon\n", monc->cur_mon,
750 pr_addr(&monc->con->peer_addr.in_addr));
751
752 __close_session(monc);
753 if (!monc->hunting) {
754 /* start hunting */
755 monc->hunting = true;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800756 __open_session(monc);
Sage Weilba75bb92009-10-06 11:31:11 -0700757 } else {
758 /* already hunting, let's wait a bit */
759 __schedule_delayed(monc);
760 }
761out:
762 mutex_unlock(&monc->mutex);
763}
764
765const static struct ceph_connection_operations mon_con_ops = {
766 .get = ceph_con_get,
767 .put = ceph_con_put,
768 .dispatch = dispatch,
769 .fault = mon_fault,
770 .alloc_msg = mon_alloc_msg,
771 .alloc_middle = ceph_alloc_middle,
772};