ceph: Correct obvious typo of Kconfig variable "CRYPTO_AES"
[linux-2.6-block.git] / fs / ceph / mon_client.c
CommitLineData
ba75bb98
SW
1#include "ceph_debug.h"
2
3#include <linux/types.h>
5a0e3ad6 4#include <linux/slab.h>
ba75bb98
SW
5#include <linux/random.h>
6#include <linux/sched.h>
7
8#include "mon_client.h"
9#include "super.h"
4e7a5dcd 10#include "auth.h"
ba75bb98
SW
11#include "decode.h"
12
13/*
14 * Interact with Ceph monitor cluster. Handle requests for new map
15 * versions, and periodically resend as needed. Also implement
16 * statfs() and umount().
17 *
18 * A small cluster of Ceph "monitors" are responsible for managing critical
19 * cluster configuration and state information. An odd number (e.g., 3, 5)
20 * of cmon daemons use a modified version of the Paxos part-time parliament
21 * algorithm to manage the MDS map (mds cluster membership), OSD map, and
22 * list of clients who have mounted the file system.
23 *
24 * We maintain an open, active session with a monitor at all times in order to
25 * receive timely MDSMap updates. We periodically send a keepalive byte on the
26 * TCP socket to ensure we detect a failure. If the connection does break, we
27 * randomly hunt for a new monitor. Once the connection is reestablished, we
28 * resend any outstanding requests.
29 */
30
9e32789f 31static const struct ceph_connection_operations mon_con_ops;
ba75bb98 32
9bd2e6f8
SW
33static int __validate_auth(struct ceph_mon_client *monc);
34
ba75bb98
SW
35/*
36 * Decode a monmap blob (e.g., during mount).
37 */
38struct ceph_monmap *ceph_monmap_decode(void *p, void *end)
39{
40 struct ceph_monmap *m = NULL;
41 int i, err = -EINVAL;
42 struct ceph_fsid fsid;
43 u32 epoch, num_mon;
44 u16 version;
4e7a5dcd
SW
45 u32 len;
46
47 ceph_decode_32_safe(&p, end, len, bad);
48 ceph_decode_need(&p, end, len, bad);
ba75bb98
SW
49
50 dout("monmap_decode %p %p len %d\n", p, end, (int)(end-p));
51
52 ceph_decode_16_safe(&p, end, version, bad);
53
54 ceph_decode_need(&p, end, sizeof(fsid) + 2*sizeof(u32), bad);
55 ceph_decode_copy(&p, &fsid, sizeof(fsid));
c89136ea 56 epoch = ceph_decode_32(&p);
ba75bb98 57
c89136ea 58 num_mon = ceph_decode_32(&p);
ba75bb98
SW
59 ceph_decode_need(&p, end, num_mon*sizeof(m->mon_inst[0]), bad);
60
61 if (num_mon >= CEPH_MAX_MON)
62 goto bad;
63 m = kmalloc(sizeof(*m) + sizeof(m->mon_inst[0])*num_mon, GFP_NOFS);
64 if (m == NULL)
65 return ERR_PTR(-ENOMEM);
66 m->fsid = fsid;
67 m->epoch = epoch;
68 m->num_mon = num_mon;
69 ceph_decode_copy(&p, m->mon_inst, num_mon*sizeof(m->mon_inst[0]));
63f2d211
SW
70 for (i = 0; i < num_mon; i++)
71 ceph_decode_addr(&m->mon_inst[i].addr);
ba75bb98 72
ba75bb98
SW
73 dout("monmap_decode epoch %d, num_mon %d\n", m->epoch,
74 m->num_mon);
75 for (i = 0; i < m->num_mon; i++)
76 dout("monmap_decode mon%d is %s\n", i,
77 pr_addr(&m->mon_inst[i].addr.in_addr));
78 return m;
79
80bad:
81 dout("monmap_decode failed with %d\n", err);
82 kfree(m);
83 return ERR_PTR(err);
84}
85
86/*
87 * return true if *addr is included in the monmap.
88 */
89int ceph_monmap_contains(struct ceph_monmap *m, struct ceph_entity_addr *addr)
90{
91 int i;
92
93 for (i = 0; i < m->num_mon; i++)
103e2d3a 94 if (memcmp(addr, &m->mon_inst[i].addr, sizeof(*addr)) == 0)
ba75bb98
SW
95 return 1;
96 return 0;
97}
98
5ce6e9db
SW
99/*
100 * Send an auth request.
101 */
102static void __send_prepared_auth_request(struct ceph_mon_client *monc, int len)
103{
104 monc->pending_auth = 1;
105 monc->m_auth->front.iov_len = len;
106 monc->m_auth->hdr.front_len = cpu_to_le32(len);
97069001 107 ceph_con_revoke(monc->con, monc->m_auth);
5ce6e9db
SW
108 ceph_msg_get(monc->m_auth); /* keep our ref */
109 ceph_con_send(monc->con, monc->m_auth);
110}
111
ba75bb98
SW
112/*
113 * Close monitor session, if any.
114 */
115static void __close_session(struct ceph_mon_client *monc)
116{
117 if (monc->con) {
118 dout("__close_session closing mon%d\n", monc->cur_mon);
4e7a5dcd 119 ceph_con_revoke(monc->con, monc->m_auth);
ba75bb98
SW
120 ceph_con_close(monc->con);
121 monc->cur_mon = -1;
9bd2e6f8 122 monc->pending_auth = 0;
4e7a5dcd 123 ceph_auth_reset(monc->auth);
ba75bb98
SW
124 }
125}
126
127/*
128 * Open a session with a (new) monitor.
129 */
130static int __open_session(struct ceph_mon_client *monc)
131{
132 char r;
4e7a5dcd 133 int ret;
ba75bb98
SW
134
135 if (monc->cur_mon < 0) {
136 get_random_bytes(&r, 1);
137 monc->cur_mon = r % monc->monmap->num_mon;
138 dout("open_session num=%d r=%d -> mon%d\n",
139 monc->monmap->num_mon, r, monc->cur_mon);
140 monc->sub_sent = 0;
141 monc->sub_renew_after = jiffies; /* i.e., expired */
142 monc->want_next_osdmap = !!monc->want_next_osdmap;
143
144 dout("open_session mon%d opening\n", monc->cur_mon);
145 monc->con->peer_name.type = CEPH_ENTITY_TYPE_MON;
146 monc->con->peer_name.num = cpu_to_le64(monc->cur_mon);
147 ceph_con_open(monc->con,
148 &monc->monmap->mon_inst[monc->cur_mon].addr);
4e7a5dcd
SW
149
150 /* initiatiate authentication handshake */
151 ret = ceph_auth_build_hello(monc->auth,
152 monc->m_auth->front.iov_base,
153 monc->m_auth->front_max);
5ce6e9db 154 __send_prepared_auth_request(monc, ret);
ba75bb98
SW
155 } else {
156 dout("open_session mon%d already open\n", monc->cur_mon);
157 }
158 return 0;
159}
160
161static bool __sub_expired(struct ceph_mon_client *monc)
162{
163 return time_after_eq(jiffies, monc->sub_renew_after);
164}
165
166/*
167 * Reschedule delayed work timer.
168 */
169static void __schedule_delayed(struct ceph_mon_client *monc)
170{
171 unsigned delay;
172
4e7a5dcd 173 if (monc->cur_mon < 0 || __sub_expired(monc))
ba75bb98
SW
174 delay = 10 * HZ;
175 else
176 delay = 20 * HZ;
177 dout("__schedule_delayed after %u\n", delay);
178 schedule_delayed_work(&monc->delayed_work, delay);
179}
180
181/*
182 * Send subscribe request for mdsmap and/or osdmap.
183 */
184static void __send_subscribe(struct ceph_mon_client *monc)
185{
186 dout("__send_subscribe sub_sent=%u exp=%u want_osd=%d\n",
187 (unsigned)monc->sub_sent, __sub_expired(monc),
188 monc->want_next_osdmap);
189 if ((__sub_expired(monc) && !monc->sub_sent) ||
190 monc->want_next_osdmap == 1) {
240ed68e 191 struct ceph_msg *msg = monc->m_subscribe;
ba75bb98
SW
192 struct ceph_mon_subscribe_item *i;
193 void *p, *end;
194
ba75bb98 195 p = msg->front.iov_base;
240ed68e 196 end = p + msg->front_max;
ba75bb98
SW
197
198 dout("__send_subscribe to 'mdsmap' %u+\n",
199 (unsigned)monc->have_mdsmap);
200 if (monc->want_next_osdmap) {
201 dout("__send_subscribe to 'osdmap' %u\n",
202 (unsigned)monc->have_osdmap);
4e7a5dcd 203 ceph_encode_32(&p, 3);
ba75bb98
SW
204 ceph_encode_string(&p, end, "osdmap", 6);
205 i = p;
206 i->have = cpu_to_le64(monc->have_osdmap);
207 i->onetime = 1;
208 p += sizeof(*i);
209 monc->want_next_osdmap = 2; /* requested */
210 } else {
4e7a5dcd 211 ceph_encode_32(&p, 2);
ba75bb98
SW
212 }
213 ceph_encode_string(&p, end, "mdsmap", 6);
214 i = p;
215 i->have = cpu_to_le64(monc->have_mdsmap);
216 i->onetime = 0;
217 p += sizeof(*i);
4e7a5dcd
SW
218 ceph_encode_string(&p, end, "monmap", 6);
219 i = p;
220 i->have = 0;
221 i->onetime = 0;
222 p += sizeof(*i);
ba75bb98
SW
223
224 msg->front.iov_len = p - msg->front.iov_base;
225 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
240ed68e
SW
226 ceph_con_revoke(monc->con, msg);
227 ceph_con_send(monc->con, ceph_msg_get(msg));
ba75bb98
SW
228
229 monc->sub_sent = jiffies | 1; /* never 0 */
230 }
231}
232
233static void handle_subscribe_ack(struct ceph_mon_client *monc,
234 struct ceph_msg *msg)
235{
236 unsigned seconds;
07bd10fb
SW
237 struct ceph_mon_subscribe_ack *h = msg->front.iov_base;
238
239 if (msg->front.iov_len < sizeof(*h))
240 goto bad;
241 seconds = le32_to_cpu(h->duration);
ba75bb98 242
ba75bb98
SW
243 mutex_lock(&monc->mutex);
244 if (monc->hunting) {
245 pr_info("mon%d %s session established\n",
246 monc->cur_mon, pr_addr(&monc->con->peer_addr.in_addr));
247 monc->hunting = false;
248 }
249 dout("handle_subscribe_ack after %d seconds\n", seconds);
0656d11b 250 monc->sub_renew_after = monc->sub_sent + (seconds >> 1)*HZ - 1;
ba75bb98
SW
251 monc->sub_sent = 0;
252 mutex_unlock(&monc->mutex);
253 return;
254bad:
255 pr_err("got corrupt subscribe-ack msg\n");
9ec7cab1 256 ceph_msg_dump(msg);
ba75bb98
SW
257}
258
259/*
260 * Keep track of which maps we have
261 */
262int ceph_monc_got_mdsmap(struct ceph_mon_client *monc, u32 got)
263{
264 mutex_lock(&monc->mutex);
265 monc->have_mdsmap = got;
266 mutex_unlock(&monc->mutex);
267 return 0;
268}
269
270int ceph_monc_got_osdmap(struct ceph_mon_client *monc, u32 got)
271{
272 mutex_lock(&monc->mutex);
273 monc->have_osdmap = got;
274 monc->want_next_osdmap = 0;
275 mutex_unlock(&monc->mutex);
276 return 0;
277}
278
279/*
280 * Register interest in the next osdmap
281 */
282void ceph_monc_request_next_osdmap(struct ceph_mon_client *monc)
283{
284 dout("request_next_osdmap have %u\n", monc->have_osdmap);
285 mutex_lock(&monc->mutex);
286 if (!monc->want_next_osdmap)
287 monc->want_next_osdmap = 1;
288 if (monc->want_next_osdmap < 2)
289 __send_subscribe(monc);
290 mutex_unlock(&monc->mutex);
291}
292
4e7a5dcd 293/*
50b885b9 294 *
4e7a5dcd
SW
295 */
296int ceph_monc_open_session(struct ceph_mon_client *monc)
ba75bb98
SW
297{
298 if (!monc->con) {
299 monc->con = kmalloc(sizeof(*monc->con), GFP_KERNEL);
300 if (!monc->con)
301 return -ENOMEM;
302 ceph_con_init(monc->client->msgr, monc->con);
303 monc->con->private = monc;
304 monc->con->ops = &mon_con_ops;
305 }
306
307 mutex_lock(&monc->mutex);
4e7a5dcd 308 __open_session(monc);
ba75bb98
SW
309 __schedule_delayed(monc);
310 mutex_unlock(&monc->mutex);
311 return 0;
312}
313
4e7a5dcd
SW
314/*
315 * The monitor responds with mount ack indicate mount success. The
316 * included client ticket allows the client to talk to MDSs and OSDs.
317 */
0743304d
SW
318static void ceph_monc_handle_map(struct ceph_mon_client *monc,
319 struct ceph_msg *msg)
4e7a5dcd
SW
320{
321 struct ceph_client *client = monc->client;
322 struct ceph_monmap *monmap = NULL, *old = monc->monmap;
323 void *p, *end;
324
325 mutex_lock(&monc->mutex);
326
327 dout("handle_monmap\n");
328 p = msg->front.iov_base;
329 end = p + msg->front.iov_len;
330
331 monmap = ceph_monmap_decode(p, end);
332 if (IS_ERR(monmap)) {
333 pr_err("problem decoding monmap, %d\n",
334 (int)PTR_ERR(monmap));
d4a780ce 335 goto out;
4e7a5dcd 336 }
0743304d
SW
337
338 if (ceph_check_fsid(monc->client, &monmap->fsid) < 0) {
4e7a5dcd 339 kfree(monmap);
d4a780ce 340 goto out;
4e7a5dcd
SW
341 }
342
343 client->monc.monmap = monmap;
4e7a5dcd
SW
344 kfree(old);
345
d4a780ce 346out:
4e7a5dcd 347 mutex_unlock(&monc->mutex);
9bd2e6f8 348 wake_up(&client->auth_wq);
4e7a5dcd
SW
349}
350
ba75bb98
SW
351/*
352 * statfs
353 */
f8c76f6f 354static struct ceph_mon_generic_request *__lookup_generic_req(
85ff03f6
SW
355 struct ceph_mon_client *monc, u64 tid)
356{
f8c76f6f
YS
357 struct ceph_mon_generic_request *req;
358 struct rb_node *n = monc->generic_request_tree.rb_node;
85ff03f6
SW
359
360 while (n) {
f8c76f6f 361 req = rb_entry(n, struct ceph_mon_generic_request, node);
85ff03f6
SW
362 if (tid < req->tid)
363 n = n->rb_left;
364 else if (tid > req->tid)
365 n = n->rb_right;
366 else
367 return req;
368 }
369 return NULL;
370}
371
f8c76f6f
YS
372static void __insert_generic_request(struct ceph_mon_client *monc,
373 struct ceph_mon_generic_request *new)
85ff03f6 374{
f8c76f6f 375 struct rb_node **p = &monc->generic_request_tree.rb_node;
85ff03f6 376 struct rb_node *parent = NULL;
f8c76f6f 377 struct ceph_mon_generic_request *req = NULL;
85ff03f6
SW
378
379 while (*p) {
380 parent = *p;
f8c76f6f 381 req = rb_entry(parent, struct ceph_mon_generic_request, node);
85ff03f6
SW
382 if (new->tid < req->tid)
383 p = &(*p)->rb_left;
384 else if (new->tid > req->tid)
385 p = &(*p)->rb_right;
386 else
387 BUG();
388 }
389
390 rb_link_node(&new->node, parent, p);
f8c76f6f 391 rb_insert_color(&new->node, &monc->generic_request_tree);
85ff03f6
SW
392}
393
f8c76f6f 394static void release_generic_request(struct kref *kref)
3143edd3 395{
f8c76f6f
YS
396 struct ceph_mon_generic_request *req =
397 container_of(kref, struct ceph_mon_generic_request, kref);
3143edd3
SW
398
399 if (req->reply)
400 ceph_msg_put(req->reply);
401 if (req->request)
402 ceph_msg_put(req->request);
20547567
YS
403
404 kfree(req);
3143edd3
SW
405}
406
f8c76f6f 407static void put_generic_request(struct ceph_mon_generic_request *req)
3143edd3 408{
f8c76f6f 409 kref_put(&req->kref, release_generic_request);
3143edd3
SW
410}
411
f8c76f6f 412static void get_generic_request(struct ceph_mon_generic_request *req)
3143edd3
SW
413{
414 kref_get(&req->kref);
415}
416
f8c76f6f 417static struct ceph_msg *get_generic_reply(struct ceph_connection *con,
3143edd3
SW
418 struct ceph_msg_header *hdr,
419 int *skip)
420{
421 struct ceph_mon_client *monc = con->private;
f8c76f6f 422 struct ceph_mon_generic_request *req;
3143edd3
SW
423 u64 tid = le64_to_cpu(hdr->tid);
424 struct ceph_msg *m;
425
426 mutex_lock(&monc->mutex);
f8c76f6f 427 req = __lookup_generic_req(monc, tid);
3143edd3 428 if (!req) {
f8c76f6f 429 dout("get_generic_reply %lld dne\n", tid);
3143edd3
SW
430 *skip = 1;
431 m = NULL;
432 } else {
f8c76f6f 433 dout("get_generic_reply %lld got %p\n", tid, req->reply);
3143edd3
SW
434 m = ceph_msg_get(req->reply);
435 /*
436 * we don't need to track the connection reading into
437 * this reply because we only have one open connection
438 * at a time, ever.
439 */
440 }
441 mutex_unlock(&monc->mutex);
442 return m;
443}
444
ba75bb98
SW
445static void handle_statfs_reply(struct ceph_mon_client *monc,
446 struct ceph_msg *msg)
447{
f8c76f6f 448 struct ceph_mon_generic_request *req;
ba75bb98 449 struct ceph_mon_statfs_reply *reply = msg->front.iov_base;
3143edd3 450 u64 tid = le64_to_cpu(msg->hdr.tid);
ba75bb98
SW
451
452 if (msg->front.iov_len != sizeof(*reply))
453 goto bad;
ba75bb98
SW
454 dout("handle_statfs_reply %p tid %llu\n", msg, tid);
455
456 mutex_lock(&monc->mutex);
f8c76f6f 457 req = __lookup_generic_req(monc, tid);
ba75bb98 458 if (req) {
f8c76f6f 459 *(struct ceph_statfs *)req->buf = reply->st;
ba75bb98 460 req->result = 0;
f8c76f6f 461 get_generic_request(req);
ba75bb98
SW
462 }
463 mutex_unlock(&monc->mutex);
3143edd3 464 if (req) {
ba75bb98 465 complete(&req->completion);
f8c76f6f 466 put_generic_request(req);
3143edd3 467 }
ba75bb98
SW
468 return;
469
470bad:
f8c76f6f 471 pr_err("corrupt generic reply, no tid\n");
9ec7cab1 472 ceph_msg_dump(msg);
ba75bb98
SW
473}
474
475/*
3143edd3 476 * Do a synchronous statfs().
ba75bb98 477 */
3143edd3 478int ceph_monc_do_statfs(struct ceph_mon_client *monc, struct ceph_statfs *buf)
ba75bb98 479{
f8c76f6f 480 struct ceph_mon_generic_request *req;
ba75bb98 481 struct ceph_mon_statfs *h;
3143edd3
SW
482 int err;
483
cffe7b6d 484 req = kzalloc(sizeof(*req), GFP_NOFS);
3143edd3
SW
485 if (!req)
486 return -ENOMEM;
487
3143edd3
SW
488 kref_init(&req->kref);
489 req->buf = buf;
490 init_completion(&req->completion);
ba75bb98 491
a79832f2 492 err = -ENOMEM;
34d23762 493 req->request = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), GFP_NOFS);
a79832f2 494 if (!req->request)
3143edd3 495 goto out;
34d23762 496 req->reply = ceph_msg_new(CEPH_MSG_STATFS_REPLY, 1024, GFP_NOFS);
a79832f2 497 if (!req->reply)
3143edd3 498 goto out;
3143edd3
SW
499
500 /* fill out request */
501 h = req->request->front.iov_base;
13e38c8a
SW
502 h->monhdr.have_version = 0;
503 h->monhdr.session_mon = cpu_to_le16(-1);
504 h->monhdr.session_mon_tid = 0;
ba75bb98 505 h->fsid = monc->monmap->fsid;
ba75bb98
SW
506
507 /* register request */
508 mutex_lock(&monc->mutex);
3143edd3
SW
509 req->tid = ++monc->last_tid;
510 req->request->hdr.tid = cpu_to_le64(req->tid);
f8c76f6f
YS
511 __insert_generic_request(monc, req);
512 monc->num_generic_requests++;
ba75bb98
SW
513 mutex_unlock(&monc->mutex);
514
515 /* send request and wait */
3143edd3
SW
516 ceph_con_send(monc->con, ceph_msg_get(req->request));
517 err = wait_for_completion_interruptible(&req->completion);
ba75bb98
SW
518
519 mutex_lock(&monc->mutex);
f8c76f6f
YS
520 rb_erase(&req->node, &monc->generic_request_tree);
521 monc->num_generic_requests--;
ba75bb98
SW
522 mutex_unlock(&monc->mutex);
523
524 if (!err)
3143edd3
SW
525 err = req->result;
526
527out:
f8c76f6f 528 kref_put(&req->kref, release_generic_request);
ba75bb98
SW
529 return err;
530}
531
532/*
533 * Resend pending statfs requests.
534 */
f8c76f6f 535static void __resend_generic_request(struct ceph_mon_client *monc)
ba75bb98 536{
f8c76f6f 537 struct ceph_mon_generic_request *req;
85ff03f6 538 struct rb_node *p;
ba75bb98 539
f8c76f6f
YS
540 for (p = rb_first(&monc->generic_request_tree); p; p = rb_next(p)) {
541 req = rb_entry(p, struct ceph_mon_generic_request, node);
97069001 542 ceph_con_revoke(monc->con, req->request);
3143edd3 543 ceph_con_send(monc->con, ceph_msg_get(req->request));
ba75bb98
SW
544 }
545}
546
547/*
548 * Delayed work. If we haven't mounted yet, retry. Otherwise,
549 * renew/retry subscription as needed (in case it is timing out, or we
550 * got an ENOMEM). And keep the monitor connection alive.
551 */
552static void delayed_work(struct work_struct *work)
553{
554 struct ceph_mon_client *monc =
555 container_of(work, struct ceph_mon_client, delayed_work.work);
556
557 dout("monc delayed_work\n");
558 mutex_lock(&monc->mutex);
4e7a5dcd
SW
559 if (monc->hunting) {
560 __close_session(monc);
561 __open_session(monc); /* continue hunting */
ba75bb98 562 } else {
4e7a5dcd 563 ceph_con_keepalive(monc->con);
9bd2e6f8
SW
564
565 __validate_auth(monc);
566
4e7a5dcd
SW
567 if (monc->auth->ops->is_authenticated(monc->auth))
568 __send_subscribe(monc);
ba75bb98 569 }
ba75bb98
SW
570 __schedule_delayed(monc);
571 mutex_unlock(&monc->mutex);
572}
573
6b805185
SW
574/*
575 * On startup, we build a temporary monmap populated with the IPs
576 * provided by mount(2).
577 */
578static int build_initial_monmap(struct ceph_mon_client *monc)
579{
580 struct ceph_mount_args *args = monc->client->mount_args;
581 struct ceph_entity_addr *mon_addr = args->mon_addr;
582 int num_mon = args->num_mon;
583 int i;
584
585 /* build initial monmap */
586 monc->monmap = kzalloc(sizeof(*monc->monmap) +
587 num_mon*sizeof(monc->monmap->mon_inst[0]),
588 GFP_KERNEL);
589 if (!monc->monmap)
590 return -ENOMEM;
591 for (i = 0; i < num_mon; i++) {
592 monc->monmap->mon_inst[i].addr = mon_addr[i];
6b805185
SW
593 monc->monmap->mon_inst[i].addr.nonce = 0;
594 monc->monmap->mon_inst[i].name.type =
595 CEPH_ENTITY_TYPE_MON;
596 monc->monmap->mon_inst[i].name.num = cpu_to_le64(i);
597 }
598 monc->monmap->num_mon = num_mon;
4e7a5dcd 599 monc->have_fsid = false;
6b805185
SW
600
601 /* release addr memory */
602 kfree(args->mon_addr);
603 args->mon_addr = NULL;
604 args->num_mon = 0;
605 return 0;
606}
607
ba75bb98
SW
608int ceph_monc_init(struct ceph_mon_client *monc, struct ceph_client *cl)
609{
610 int err = 0;
611
612 dout("init\n");
613 memset(monc, 0, sizeof(*monc));
614 monc->client = cl;
615 monc->monmap = NULL;
616 mutex_init(&monc->mutex);
617
6b805185
SW
618 err = build_initial_monmap(monc);
619 if (err)
620 goto out;
621
ba75bb98
SW
622 monc->con = NULL;
623
4e7a5dcd
SW
624 /* authentication */
625 monc->auth = ceph_auth_init(cl->mount_args->name,
626 cl->mount_args->secret);
627 if (IS_ERR(monc->auth))
628 return PTR_ERR(monc->auth);
629 monc->auth->want_keys =
630 CEPH_ENTITY_TYPE_AUTH | CEPH_ENTITY_TYPE_MON |
631 CEPH_ENTITY_TYPE_OSD | CEPH_ENTITY_TYPE_MDS;
632
240ed68e 633 /* msgs */
a79832f2 634 err = -ENOMEM;
7c315c55 635 monc->m_subscribe_ack = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE_ACK,
34d23762
YS
636 sizeof(struct ceph_mon_subscribe_ack),
637 GFP_NOFS);
a79832f2 638 if (!monc->m_subscribe_ack)
4e7a5dcd 639 goto out_monmap;
6694d6b9 640
240ed68e
SW
641 monc->m_subscribe = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 96, GFP_NOFS);
642 if (!monc->m_subscribe)
643 goto out_subscribe_ack;
644
34d23762 645 monc->m_auth_reply = ceph_msg_new(CEPH_MSG_AUTH_REPLY, 4096, GFP_NOFS);
a79832f2 646 if (!monc->m_auth_reply)
240ed68e 647 goto out_subscribe;
4e7a5dcd 648
34d23762 649 monc->m_auth = ceph_msg_new(CEPH_MSG_AUTH, 4096, GFP_NOFS);
9bd2e6f8 650 monc->pending_auth = 0;
a79832f2 651 if (!monc->m_auth)
6694d6b9 652 goto out_auth_reply;
ba75bb98
SW
653
654 monc->cur_mon = -1;
4e7a5dcd 655 monc->hunting = true;
ba75bb98
SW
656 monc->sub_renew_after = jiffies;
657 monc->sub_sent = 0;
658
659 INIT_DELAYED_WORK(&monc->delayed_work, delayed_work);
f8c76f6f
YS
660 monc->generic_request_tree = RB_ROOT;
661 monc->num_generic_requests = 0;
ba75bb98
SW
662 monc->last_tid = 0;
663
664 monc->have_mdsmap = 0;
665 monc->have_osdmap = 0;
666 monc->want_next_osdmap = 1;
4e7a5dcd
SW
667 return 0;
668
6694d6b9
SW
669out_auth_reply:
670 ceph_msg_put(monc->m_auth_reply);
240ed68e
SW
671out_subscribe:
672 ceph_msg_put(monc->m_subscribe);
7c315c55
SW
673out_subscribe_ack:
674 ceph_msg_put(monc->m_subscribe_ack);
4e7a5dcd
SW
675out_monmap:
676 kfree(monc->monmap);
ba75bb98
SW
677out:
678 return err;
679}
680
681void ceph_monc_stop(struct ceph_mon_client *monc)
682{
683 dout("stop\n");
684 cancel_delayed_work_sync(&monc->delayed_work);
685
686 mutex_lock(&monc->mutex);
687 __close_session(monc);
688 if (monc->con) {
689 monc->con->private = NULL;
690 monc->con->ops->put(monc->con);
691 monc->con = NULL;
692 }
693 mutex_unlock(&monc->mutex);
694
4e7a5dcd
SW
695 ceph_auth_destroy(monc->auth);
696
697 ceph_msg_put(monc->m_auth);
6694d6b9 698 ceph_msg_put(monc->m_auth_reply);
240ed68e 699 ceph_msg_put(monc->m_subscribe);
7c315c55 700 ceph_msg_put(monc->m_subscribe_ack);
ba75bb98
SW
701
702 kfree(monc->monmap);
703}
704
4e7a5dcd
SW
705static void handle_auth_reply(struct ceph_mon_client *monc,
706 struct ceph_msg *msg)
707{
708 int ret;
09c4d6a7 709 int was_auth = 0;
4e7a5dcd
SW
710
711 mutex_lock(&monc->mutex);
09c4d6a7
SW
712 if (monc->auth->ops)
713 was_auth = monc->auth->ops->is_authenticated(monc->auth);
9bd2e6f8 714 monc->pending_auth = 0;
4e7a5dcd
SW
715 ret = ceph_handle_auth_reply(monc->auth, msg->front.iov_base,
716 msg->front.iov_len,
717 monc->m_auth->front.iov_base,
718 monc->m_auth->front_max);
719 if (ret < 0) {
9bd2e6f8
SW
720 monc->client->auth_err = ret;
721 wake_up(&monc->client->auth_wq);
4e7a5dcd 722 } else if (ret > 0) {
9bd2e6f8 723 __send_prepared_auth_request(monc, ret);
09c4d6a7 724 } else if (!was_auth && monc->auth->ops->is_authenticated(monc->auth)) {
4e7a5dcd 725 dout("authenticated, starting session\n");
0743304d
SW
726
727 monc->client->msgr->inst.name.type = CEPH_ENTITY_TYPE_CLIENT;
0cf5537b
YS
728 monc->client->msgr->inst.name.num =
729 cpu_to_le64(monc->auth->global_id);
0743304d 730
4e7a5dcd 731 __send_subscribe(monc);
f8c76f6f 732 __resend_generic_request(monc);
4e7a5dcd
SW
733 }
734 mutex_unlock(&monc->mutex);
735}
736
9bd2e6f8
SW
737static int __validate_auth(struct ceph_mon_client *monc)
738{
739 int ret;
740
741 if (monc->pending_auth)
742 return 0;
743
744 ret = ceph_build_auth(monc->auth, monc->m_auth->front.iov_base,
745 monc->m_auth->front_max);
746 if (ret <= 0)
747 return ret; /* either an error, or no need to authenticate */
748 __send_prepared_auth_request(monc, ret);
749 return 0;
750}
751
752int ceph_monc_validate_auth(struct ceph_mon_client *monc)
753{
754 int ret;
755
756 mutex_lock(&monc->mutex);
757 ret = __validate_auth(monc);
758 mutex_unlock(&monc->mutex);
759 return ret;
760}
761
ba75bb98
SW
762/*
763 * handle incoming message
764 */
765static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
766{
767 struct ceph_mon_client *monc = con->private;
768 int type = le16_to_cpu(msg->hdr.type);
769
770 if (!monc)
771 return;
772
773 switch (type) {
4e7a5dcd
SW
774 case CEPH_MSG_AUTH_REPLY:
775 handle_auth_reply(monc, msg);
ba75bb98
SW
776 break;
777
778 case CEPH_MSG_MON_SUBSCRIBE_ACK:
779 handle_subscribe_ack(monc, msg);
780 break;
781
782 case CEPH_MSG_STATFS_REPLY:
783 handle_statfs_reply(monc, msg);
784 break;
785
4e7a5dcd
SW
786 case CEPH_MSG_MON_MAP:
787 ceph_monc_handle_map(monc, msg);
788 break;
789
ba75bb98
SW
790 case CEPH_MSG_MDS_MAP:
791 ceph_mdsc_handle_map(&monc->client->mdsc, msg);
792 break;
793
794 case CEPH_MSG_OSD_MAP:
795 ceph_osdc_handle_map(&monc->client->osdc, msg);
796 break;
797
798 default:
799 pr_err("received unknown message type %d %s\n", type,
800 ceph_msg_type_name(type));
801 }
802 ceph_msg_put(msg);
803}
804
805/*
806 * Allocate memory for incoming message
807 */
808static struct ceph_msg *mon_alloc_msg(struct ceph_connection *con,
2450418c
YS
809 struct ceph_msg_header *hdr,
810 int *skip)
ba75bb98
SW
811{
812 struct ceph_mon_client *monc = con->private;
813 int type = le16_to_cpu(hdr->type);
2450418c 814 int front_len = le32_to_cpu(hdr->front_len);
5b3a4db3 815 struct ceph_msg *m = NULL;
ba75bb98 816
2450418c 817 *skip = 0;
0547a9b3 818
ba75bb98 819 switch (type) {
ba75bb98 820 case CEPH_MSG_MON_SUBSCRIBE_ACK:
7c315c55 821 m = ceph_msg_get(monc->m_subscribe_ack);
2450418c 822 break;
ba75bb98 823 case CEPH_MSG_STATFS_REPLY:
f8c76f6f 824 return get_generic_reply(con, hdr, skip);
4e7a5dcd 825 case CEPH_MSG_AUTH_REPLY:
6694d6b9 826 m = ceph_msg_get(monc->m_auth_reply);
2450418c 827 break;
5b3a4db3
SW
828 case CEPH_MSG_MON_MAP:
829 case CEPH_MSG_MDS_MAP:
830 case CEPH_MSG_OSD_MAP:
34d23762 831 m = ceph_msg_new(type, front_len, GFP_NOFS);
5b3a4db3 832 break;
ba75bb98 833 }
2450418c 834
5b3a4db3
SW
835 if (!m) {
836 pr_info("alloc_msg unknown type %d\n", type);
2450418c 837 *skip = 1;
5b3a4db3 838 }
2450418c 839 return m;
ba75bb98
SW
840}
841
842/*
843 * If the monitor connection resets, pick a new monitor and resubmit
844 * any pending requests.
845 */
846static void mon_fault(struct ceph_connection *con)
847{
848 struct ceph_mon_client *monc = con->private;
849
850 if (!monc)
851 return;
852
853 dout("mon_fault\n");
854 mutex_lock(&monc->mutex);
855 if (!con->private)
856 goto out;
857
858 if (monc->con && !monc->hunting)
859 pr_info("mon%d %s session lost, "
860 "hunting for new mon\n", monc->cur_mon,
861 pr_addr(&monc->con->peer_addr.in_addr));
862
863 __close_session(monc);
864 if (!monc->hunting) {
865 /* start hunting */
866 monc->hunting = true;
4e7a5dcd 867 __open_session(monc);
ba75bb98
SW
868 } else {
869 /* already hunting, let's wait a bit */
870 __schedule_delayed(monc);
871 }
872out:
873 mutex_unlock(&monc->mutex);
874}
875
9e32789f 876static const struct ceph_connection_operations mon_con_ops = {
ba75bb98
SW
877 .get = ceph_con_get,
878 .put = ceph_con_put,
879 .dispatch = dispatch,
880 .fault = mon_fault,
881 .alloc_msg = mon_alloc_msg,
ba75bb98 882};