mon_client.c 18.2 KB
Newer Older
S
Sage Weil 已提交
1 2 3 4 5 6 7 8
#include "ceph_debug.h"

#include <linux/types.h>
#include <linux/random.h>
#include <linux/sched.h>

#include "mon_client.h"
#include "super.h"
9
#include "auth.h"
S
Sage Weil 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include "decode.h"

/*
 * Interact with Ceph monitor cluster.  Handle requests for new map
 * versions, and periodically resend as needed.  Also implement
 * statfs() and umount().
 *
 * A small cluster of Ceph "monitors" are responsible for managing critical
 * cluster configuration and state information.  An odd number (e.g., 3, 5)
 * of cmon daemons use a modified version of the Paxos part-time parliament
 * algorithm to manage the MDS map (mds cluster membership), OSD map, and
 * list of clients who have mounted the file system.
 *
 * We maintain an open, active session with a monitor at all times in order to
 * receive timely MDSMap updates.  We periodically send a keepalive byte on the
 * TCP socket to ensure we detect a failure.  If the connection does break, we
 * randomly hunt for a new monitor.  Once the connection is reestablished, we
 * resend any outstanding requests.
 */

const static struct ceph_connection_operations mon_con_ops;

/*
 * Decode a monmap blob (e.g., during mount).
 */
struct ceph_monmap *ceph_monmap_decode(void *p, void *end)
{
	struct ceph_monmap *m = NULL;
	int i, err = -EINVAL;
	struct ceph_fsid fsid;
	u32 epoch, num_mon;
	u16 version;
42 43 44 45
	u32 len;

	ceph_decode_32_safe(&p, end, len, bad);
	ceph_decode_need(&p, end, len, bad);
S
Sage Weil 已提交
46 47 48 49 50 51 52

	dout("monmap_decode %p %p len %d\n", p, end, (int)(end-p));

	ceph_decode_16_safe(&p, end, version, bad);

	ceph_decode_need(&p, end, sizeof(fsid) + 2*sizeof(u32), bad);
	ceph_decode_copy(&p, &fsid, sizeof(fsid));
53
	epoch = ceph_decode_32(&p);
S
Sage Weil 已提交
54

55
	num_mon = ceph_decode_32(&p);
S
Sage Weil 已提交
56 57 58 59 60 61 62 63 64 65 66
	ceph_decode_need(&p, end, num_mon*sizeof(m->mon_inst[0]), bad);

	if (num_mon >= CEPH_MAX_MON)
		goto bad;
	m = kmalloc(sizeof(*m) + sizeof(m->mon_inst[0])*num_mon, GFP_NOFS);
	if (m == NULL)
		return ERR_PTR(-ENOMEM);
	m->fsid = fsid;
	m->epoch = epoch;
	m->num_mon = num_mon;
	ceph_decode_copy(&p, m->mon_inst, num_mon*sizeof(m->mon_inst[0]));
67 68
	for (i = 0; i < num_mon; i++)
		ceph_decode_addr(&m->mon_inst[i].addr);
S
Sage Weil 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

	dout("monmap_decode epoch %d, num_mon %d\n", m->epoch,
	     m->num_mon);
	for (i = 0; i < m->num_mon; i++)
		dout("monmap_decode  mon%d is %s\n", i,
		     pr_addr(&m->mon_inst[i].addr.in_addr));
	return m;

bad:
	dout("monmap_decode failed with %d\n", err);
	kfree(m);
	return ERR_PTR(err);
}

/*
 * return true if *addr is included in the monmap.
 */
int ceph_monmap_contains(struct ceph_monmap *m, struct ceph_entity_addr *addr)
{
	int i;

	for (i = 0; i < m->num_mon; i++)
S
Sage Weil 已提交
91
		if (memcmp(addr, &m->mon_inst[i].addr, sizeof(*addr)) == 0)
S
Sage Weil 已提交
92 93 94 95 96 97 98 99 100 101 102
			return 1;
	return 0;
}

/*
 * Close monitor session, if any.
 */
static void __close_session(struct ceph_mon_client *monc)
{
	if (monc->con) {
		dout("__close_session closing mon%d\n", monc->cur_mon);
103
		ceph_con_revoke(monc->con, monc->m_auth);
S
Sage Weil 已提交
104 105
		ceph_con_close(monc->con);
		monc->cur_mon = -1;
106
		ceph_auth_reset(monc->auth);
S
Sage Weil 已提交
107 108 109 110 111 112 113 114 115
	}
}

/*
 * Open a session with a (new) monitor.
 */
static int __open_session(struct ceph_mon_client *monc)
{
	char r;
116
	int ret;
S
Sage Weil 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

	if (monc->cur_mon < 0) {
		get_random_bytes(&r, 1);
		monc->cur_mon = r % monc->monmap->num_mon;
		dout("open_session num=%d r=%d -> mon%d\n",
		     monc->monmap->num_mon, r, monc->cur_mon);
		monc->sub_sent = 0;
		monc->sub_renew_after = jiffies;  /* i.e., expired */
		monc->want_next_osdmap = !!monc->want_next_osdmap;

		dout("open_session mon%d opening\n", monc->cur_mon);
		monc->con->peer_name.type = CEPH_ENTITY_TYPE_MON;
		monc->con->peer_name.num = cpu_to_le64(monc->cur_mon);
		ceph_con_open(monc->con,
			      &monc->monmap->mon_inst[monc->cur_mon].addr);
132 133 134 135 136 137 138 139 140

		/* initiatiate authentication handshake */
		ret = ceph_auth_build_hello(monc->auth,
					    monc->m_auth->front.iov_base,
					    monc->m_auth->front_max);
		monc->m_auth->front.iov_len = ret;
		monc->m_auth->hdr.front_len = cpu_to_le32(ret);
		ceph_msg_get(monc->m_auth);  /* keep our ref */
		ceph_con_send(monc->con, monc->m_auth);
S
Sage Weil 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
	} else {
		dout("open_session mon%d already open\n", monc->cur_mon);
	}
	return 0;
}

static bool __sub_expired(struct ceph_mon_client *monc)
{
	return time_after_eq(jiffies, monc->sub_renew_after);
}

/*
 * Reschedule delayed work timer.
 */
static void __schedule_delayed(struct ceph_mon_client *monc)
{
	unsigned delay;

159
	if (monc->cur_mon < 0 || __sub_expired(monc))
S
Sage Weil 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
		delay = 10 * HZ;
	else
		delay = 20 * HZ;
	dout("__schedule_delayed after %u\n", delay);
	schedule_delayed_work(&monc->delayed_work, delay);
}

/*
 * Send subscribe request for mdsmap and/or osdmap.
 */
static void __send_subscribe(struct ceph_mon_client *monc)
{
	dout("__send_subscribe sub_sent=%u exp=%u want_osd=%d\n",
	     (unsigned)monc->sub_sent, __sub_expired(monc),
	     monc->want_next_osdmap);
	if ((__sub_expired(monc) && !monc->sub_sent) ||
	    monc->want_next_osdmap == 1) {
		struct ceph_msg *msg;
		struct ceph_mon_subscribe_item *i;
		void *p, *end;

181
		msg = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 96, 0, 0, NULL);
S
Sage Weil 已提交
182 183 184 185 186 187 188 189 190 191 192
		if (!msg)
			return;

		p = msg->front.iov_base;
		end = p + msg->front.iov_len;

		dout("__send_subscribe to 'mdsmap' %u+\n",
		     (unsigned)monc->have_mdsmap);
		if (monc->want_next_osdmap) {
			dout("__send_subscribe to 'osdmap' %u\n",
			     (unsigned)monc->have_osdmap);
193
			ceph_encode_32(&p, 3);
S
Sage Weil 已提交
194 195 196 197 198 199 200
			ceph_encode_string(&p, end, "osdmap", 6);
			i = p;
			i->have = cpu_to_le64(monc->have_osdmap);
			i->onetime = 1;
			p += sizeof(*i);
			monc->want_next_osdmap = 2;  /* requested */
		} else {
201
			ceph_encode_32(&p, 2);
S
Sage Weil 已提交
202 203 204 205 206 207
		}
		ceph_encode_string(&p, end, "mdsmap", 6);
		i = p;
		i->have = cpu_to_le64(monc->have_mdsmap);
		i->onetime = 0;
		p += sizeof(*i);
208 209 210 211 212
		ceph_encode_string(&p, end, "monmap", 6);
		i = p;
		i->have = 0;
		i->onetime = 0;
		p += sizeof(*i);
S
Sage Weil 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225

		msg->front.iov_len = p - msg->front.iov_base;
		msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
		ceph_con_send(monc->con, msg);

		monc->sub_sent = jiffies | 1;  /* never 0 */
	}
}

static void handle_subscribe_ack(struct ceph_mon_client *monc,
				 struct ceph_msg *msg)
{
	unsigned seconds;
226 227 228 229 230
	struct ceph_mon_subscribe_ack *h = msg->front.iov_base;

	if (msg->front.iov_len < sizeof(*h))
		goto bad;
	seconds = le32_to_cpu(h->duration);
S
Sage Weil 已提交
231 232 233 234 235 236 237 238

	mutex_lock(&monc->mutex);
	if (monc->hunting) {
		pr_info("mon%d %s session established\n",
			monc->cur_mon, pr_addr(&monc->con->peer_addr.in_addr));
		monc->hunting = false;
	}
	dout("handle_subscribe_ack after %d seconds\n", seconds);
239
	monc->sub_renew_after = monc->sub_sent + (seconds >> 1)*HZ - 1;
S
Sage Weil 已提交
240 241 242 243 244
	monc->sub_sent = 0;
	mutex_unlock(&monc->mutex);
	return;
bad:
	pr_err("got corrupt subscribe-ack msg\n");
245
	ceph_msg_dump(msg);
S
Sage Weil 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
}

/*
 * Keep track of which maps we have
 */
int ceph_monc_got_mdsmap(struct ceph_mon_client *monc, u32 got)
{
	mutex_lock(&monc->mutex);
	monc->have_mdsmap = got;
	mutex_unlock(&monc->mutex);
	return 0;
}

int ceph_monc_got_osdmap(struct ceph_mon_client *monc, u32 got)
{
	mutex_lock(&monc->mutex);
	monc->have_osdmap = got;
	monc->want_next_osdmap = 0;
	mutex_unlock(&monc->mutex);
	return 0;
}

/*
 * Register interest in the next osdmap
 */
void ceph_monc_request_next_osdmap(struct ceph_mon_client *monc)
{
	dout("request_next_osdmap have %u\n", monc->have_osdmap);
	mutex_lock(&monc->mutex);
	if (!monc->want_next_osdmap)
		monc->want_next_osdmap = 1;
	if (monc->want_next_osdmap < 2)
		__send_subscribe(monc);
	mutex_unlock(&monc->mutex);
}

282
/*
S
Sage Weil 已提交
283
 *
284 285
 */
int ceph_monc_open_session(struct ceph_mon_client *monc)
S
Sage Weil 已提交
286 287 288 289 290 291 292 293 294 295 296
{
	if (!monc->con) {
		monc->con = kmalloc(sizeof(*monc->con), GFP_KERNEL);
		if (!monc->con)
			return -ENOMEM;
		ceph_con_init(monc->client->msgr, monc->con);
		monc->con->private = monc;
		monc->con->ops = &mon_con_ops;
	}

	mutex_lock(&monc->mutex);
297
	__open_session(monc);
S
Sage Weil 已提交
298 299 300 301 302
	__schedule_delayed(monc);
	mutex_unlock(&monc->mutex);
	return 0;
}

303 304 305 306
/*
 * The monitor responds with mount ack indicate mount success.  The
 * included client ticket allows the client to talk to MDSs and OSDs.
 */
307 308
static void ceph_monc_handle_map(struct ceph_mon_client *monc,
				 struct ceph_msg *msg)
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
{
	struct ceph_client *client = monc->client;
	struct ceph_monmap *monmap = NULL, *old = monc->monmap;
	void *p, *end;

	mutex_lock(&monc->mutex);

	dout("handle_monmap\n");
	p = msg->front.iov_base;
	end = p + msg->front.iov_len;

	monmap = ceph_monmap_decode(p, end);
	if (IS_ERR(monmap)) {
		pr_err("problem decoding monmap, %d\n",
		       (int)PTR_ERR(monmap));
S
Sage Weil 已提交
324
		goto out;
325
	}
326 327

	if (ceph_check_fsid(monc->client, &monmap->fsid) < 0) {
328
		kfree(monmap);
S
Sage Weil 已提交
329
		goto out;
330 331 332 333 334
	}

	client->monc.monmap = monmap;
	kfree(old);

S
Sage Weil 已提交
335
out:
336 337 338 339
	mutex_unlock(&monc->mutex);
	wake_up(&client->mount_wq);
}

S
Sage Weil 已提交
340 341 342 343 344 345 346 347 348 349 350 351
/*
 * statfs
 */
static void handle_statfs_reply(struct ceph_mon_client *monc,
				struct ceph_msg *msg)
{
	struct ceph_mon_statfs_request *req;
	struct ceph_mon_statfs_reply *reply = msg->front.iov_base;
	u64 tid;

	if (msg->front.iov_len != sizeof(*reply))
		goto bad;
352
	tid = le64_to_cpu(msg->hdr.tid);
S
Sage Weil 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
	dout("handle_statfs_reply %p tid %llu\n", msg, tid);

	mutex_lock(&monc->mutex);
	req = radix_tree_lookup(&monc->statfs_request_tree, tid);
	if (req) {
		*req->buf = reply->st;
		req->result = 0;
	}
	mutex_unlock(&monc->mutex);
	if (req)
		complete(&req->completion);
	return;

bad:
	pr_err("corrupt statfs reply, no tid\n");
368
	ceph_msg_dump(msg);
S
Sage Weil 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
}

/*
 * (re)send a statfs request
 */
static int send_statfs(struct ceph_mon_client *monc,
		       struct ceph_mon_statfs_request *req)
{
	struct ceph_msg *msg;
	struct ceph_mon_statfs *h;

	dout("send_statfs tid %llu\n", req->tid);
	msg = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), 0, 0, NULL);
	if (IS_ERR(msg))
		return PTR_ERR(msg);
	req->request = msg;
385
	msg->hdr.tid = cpu_to_le64(req->tid);
S
Sage Weil 已提交
386
	h = msg->front.iov_base;
387 388 389
	h->monhdr.have_version = 0;
	h->monhdr.session_mon = cpu_to_le16(-1);
	h->monhdr.session_mon_tid = 0;
S
Sage Weil 已提交
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
	h->fsid = monc->monmap->fsid;
	ceph_con_send(monc->con, msg);
	return 0;
}

/*
 * Do a synchronous statfs().
 */
int ceph_monc_do_statfs(struct ceph_mon_client *monc, struct ceph_statfs *buf)
{
	struct ceph_mon_statfs_request req;
	int err;

	req.buf = buf;
	init_completion(&req.completion);

	/* allocate memory for reply */
	err = ceph_msgpool_resv(&monc->msgpool_statfs_reply, 1);
	if (err)
		return err;

	/* register request */
	mutex_lock(&monc->mutex);
	req.tid = ++monc->last_tid;
	req.last_attempt = jiffies;
	req.delay = BASE_DELAY_INTERVAL;
	if (radix_tree_insert(&monc->statfs_request_tree, req.tid, &req) < 0) {
		mutex_unlock(&monc->mutex);
		pr_err("ENOMEM in do_statfs\n");
		return -ENOMEM;
	}
	monc->num_statfs_requests++;
	mutex_unlock(&monc->mutex);

	/* send request and wait */
	err = send_statfs(monc, &req);
	if (!err)
		err = wait_for_completion_interruptible(&req.completion);

	mutex_lock(&monc->mutex);
	radix_tree_delete(&monc->statfs_request_tree, req.tid);
	monc->num_statfs_requests--;
	ceph_msgpool_resv(&monc->msgpool_statfs_reply, -1);
	mutex_unlock(&monc->mutex);

	if (!err)
		err = req.result;
	return err;
}

/*
 * Resend pending statfs requests.
 */
static void __resend_statfs(struct ceph_mon_client *monc)
{
	u64 next_tid = 0;
	int got;
	int did = 0;
	struct ceph_mon_statfs_request *req;

	while (1) {
		got = radix_tree_gang_lookup(&monc->statfs_request_tree,
					     (void **)&req,
					     next_tid, 1);
		if (got == 0)
			break;
		did++;
		next_tid = req->tid + 1;

		send_statfs(monc, req);
	}
}

/*
 * Delayed work.  If we haven't mounted yet, retry.  Otherwise,
 * renew/retry subscription as needed (in case it is timing out, or we
 * got an ENOMEM).  And keep the monitor connection alive.
 */
static void delayed_work(struct work_struct *work)
{
	struct ceph_mon_client *monc =
		container_of(work, struct ceph_mon_client, delayed_work.work);

	dout("monc delayed_work\n");
	mutex_lock(&monc->mutex);
475 476 477
	if (monc->hunting) {
		__close_session(monc);
		__open_session(monc);  /* continue hunting */
S
Sage Weil 已提交
478
	} else {
479 480 481
		ceph_con_keepalive(monc->con);
		if (monc->auth->ops->is_authenticated(monc->auth))
			__send_subscribe(monc);
S
Sage Weil 已提交
482 483 484 485 486
	}
	__schedule_delayed(monc);
	mutex_unlock(&monc->mutex);
}

487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
/*
 * On startup, we build a temporary monmap populated with the IPs
 * provided by mount(2).
 */
static int build_initial_monmap(struct ceph_mon_client *monc)
{
	struct ceph_mount_args *args = monc->client->mount_args;
	struct ceph_entity_addr *mon_addr = args->mon_addr;
	int num_mon = args->num_mon;
	int i;

	/* build initial monmap */
	monc->monmap = kzalloc(sizeof(*monc->monmap) +
			       num_mon*sizeof(monc->monmap->mon_inst[0]),
			       GFP_KERNEL);
	if (!monc->monmap)
		return -ENOMEM;
	for (i = 0; i < num_mon; i++) {
		monc->monmap->mon_inst[i].addr = mon_addr[i];
		monc->monmap->mon_inst[i].addr.nonce = 0;
		monc->monmap->mon_inst[i].name.type =
			CEPH_ENTITY_TYPE_MON;
		monc->monmap->mon_inst[i].name.num = cpu_to_le64(i);
	}
	monc->monmap->num_mon = num_mon;
512
	monc->have_fsid = false;
513 514 515 516 517 518 519 520

	/* release addr memory */
	kfree(args->mon_addr);
	args->mon_addr = NULL;
	args->num_mon = 0;
	return 0;
}

S
Sage Weil 已提交
521 522 523 524 525 526 527 528 529 530
int ceph_monc_init(struct ceph_mon_client *monc, struct ceph_client *cl)
{
	int err = 0;

	dout("init\n");
	memset(monc, 0, sizeof(*monc));
	monc->client = cl;
	monc->monmap = NULL;
	mutex_init(&monc->mutex);

531 532 533 534
	err = build_initial_monmap(monc);
	if (err)
		goto out;

S
Sage Weil 已提交
535 536
	monc->con = NULL;

537 538 539 540 541 542 543 544 545
	/* authentication */
	monc->auth = ceph_auth_init(cl->mount_args->name,
				    cl->mount_args->secret);
	if (IS_ERR(monc->auth))
		return PTR_ERR(monc->auth);
	monc->auth->want_keys =
		CEPH_ENTITY_TYPE_AUTH | CEPH_ENTITY_TYPE_MON |
		CEPH_ENTITY_TYPE_OSD | CEPH_ENTITY_TYPE_MDS;

S
Sage Weil 已提交
546
	/* msg pools */
547 548
	err = ceph_msgpool_init(&monc->msgpool_subscribe_ack,
			       sizeof(struct ceph_mon_subscribe_ack), 1, false);
S
Sage Weil 已提交
549
	if (err < 0)
550
		goto out_monmap;
S
Sage Weil 已提交
551 552 553
	err = ceph_msgpool_init(&monc->msgpool_statfs_reply,
				sizeof(struct ceph_mon_statfs_reply), 0, false);
	if (err < 0)
554 555 556 557 558 559 560 561 562 563 564
		goto out_pool1;
	err = ceph_msgpool_init(&monc->msgpool_auth_reply, 4096, 1, false);
	if (err < 0)
		goto out_pool2;

	monc->m_auth = ceph_msg_new(CEPH_MSG_AUTH, 4096, 0, 0, NULL);
	if (IS_ERR(monc->m_auth)) {
		err = PTR_ERR(monc->m_auth);
		monc->m_auth = NULL;
		goto out_pool3;
	}
S
Sage Weil 已提交
565 566

	monc->cur_mon = -1;
567
	monc->hunting = true;
S
Sage Weil 已提交
568 569 570 571 572 573 574 575 576 577 578
	monc->sub_renew_after = jiffies;
	monc->sub_sent = 0;

	INIT_DELAYED_WORK(&monc->delayed_work, delayed_work);
	INIT_RADIX_TREE(&monc->statfs_request_tree, GFP_NOFS);
	monc->num_statfs_requests = 0;
	monc->last_tid = 0;

	monc->have_mdsmap = 0;
	monc->have_osdmap = 0;
	monc->want_next_osdmap = 1;
579 580 581 582 583 584 585 586 587 588
	return 0;

out_pool3:
	ceph_msgpool_destroy(&monc->msgpool_auth_reply);
out_pool2:
	ceph_msgpool_destroy(&monc->msgpool_subscribe_ack);
out_pool1:
	ceph_msgpool_destroy(&monc->msgpool_statfs_reply);
out_monmap:
	kfree(monc->monmap);
S
Sage Weil 已提交
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
out:
	return err;
}

void ceph_monc_stop(struct ceph_mon_client *monc)
{
	dout("stop\n");
	cancel_delayed_work_sync(&monc->delayed_work);

	mutex_lock(&monc->mutex);
	__close_session(monc);
	if (monc->con) {
		monc->con->private = NULL;
		monc->con->ops->put(monc->con);
		monc->con = NULL;
	}
	mutex_unlock(&monc->mutex);

607 608 609
	ceph_auth_destroy(monc->auth);

	ceph_msg_put(monc->m_auth);
S
Sage Weil 已提交
610 611
	ceph_msgpool_destroy(&monc->msgpool_subscribe_ack);
	ceph_msgpool_destroy(&monc->msgpool_statfs_reply);
612
	ceph_msgpool_destroy(&monc->msgpool_auth_reply);
S
Sage Weil 已提交
613 614 615 616 617

	kfree(monc->monmap);
}


618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
static void handle_auth_reply(struct ceph_mon_client *monc,
			      struct ceph_msg *msg)
{
	int ret;

	mutex_lock(&monc->mutex);
	ret = ceph_handle_auth_reply(monc->auth, msg->front.iov_base,
				     msg->front.iov_len,
				     monc->m_auth->front.iov_base,
				     monc->m_auth->front_max);
	if (ret < 0) {
		monc->client->mount_err = ret;
		wake_up(&monc->client->mount_wq);
	} else if (ret > 0) {
		monc->m_auth->front.iov_len = ret;
		monc->m_auth->hdr.front_len = cpu_to_le32(ret);
		ceph_msg_get(monc->m_auth);  /* keep our ref */
		ceph_con_send(monc->con, monc->m_auth);
	} else if (monc->auth->ops->is_authenticated(monc->auth)) {
		dout("authenticated, starting session\n");
638 639 640 641

		monc->client->msgr->inst.name.type = CEPH_ENTITY_TYPE_CLIENT;
		monc->client->msgr->inst.name.num = monc->auth->global_id;

642 643 644 645 646 647
		__send_subscribe(monc);
		__resend_statfs(monc);
	}
	mutex_unlock(&monc->mutex);
}

S
Sage Weil 已提交
648 649 650 651 652 653 654 655 656 657 658 659
/*
 * handle incoming message
 */
static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
{
	struct ceph_mon_client *monc = con->private;
	int type = le16_to_cpu(msg->hdr.type);

	if (!monc)
		return;

	switch (type) {
660 661
	case CEPH_MSG_AUTH_REPLY:
		handle_auth_reply(monc, msg);
S
Sage Weil 已提交
662 663 664 665 666 667 668 669 670 671
		break;

	case CEPH_MSG_MON_SUBSCRIBE_ACK:
		handle_subscribe_ack(monc, msg);
		break;

	case CEPH_MSG_STATFS_REPLY:
		handle_statfs_reply(monc, msg);
		break;

672 673 674 675
	case CEPH_MSG_MON_MAP:
		ceph_monc_handle_map(monc, msg);
		break;

S
Sage Weil 已提交
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
	case CEPH_MSG_MDS_MAP:
		ceph_mdsc_handle_map(&monc->client->mdsc, msg);
		break;

	case CEPH_MSG_OSD_MAP:
		ceph_osdc_handle_map(&monc->client->osdc, msg);
		break;

	default:
		pr_err("received unknown message type %d %s\n", type,
		       ceph_msg_type_name(type));
	}
	ceph_msg_put(msg);
}

/*
 * Allocate memory for incoming message
 */
static struct ceph_msg *mon_alloc_msg(struct ceph_connection *con,
695 696
				      struct ceph_msg_header *hdr,
				      int *skip)
S
Sage Weil 已提交
697 698 699
{
	struct ceph_mon_client *monc = con->private;
	int type = le16_to_cpu(hdr->type);
700 701
	int front_len = le32_to_cpu(hdr->front_len);
	struct ceph_msg *m;
S
Sage Weil 已提交
702

703
	*skip = 0;
704

S
Sage Weil 已提交
705 706
	switch (type) {
	case CEPH_MSG_MON_SUBSCRIBE_ACK:
707 708
		m = ceph_msgpool_get(&monc->msgpool_subscribe_ack, front_len);
		break;
S
Sage Weil 已提交
709
	case CEPH_MSG_STATFS_REPLY:
710 711
		m = ceph_msgpool_get(&monc->msgpool_statfs_reply, front_len);
		break;
712
	case CEPH_MSG_AUTH_REPLY:
713 714 715 716
		m = ceph_msgpool_get(&monc->msgpool_auth_reply, front_len);
		break;
	default:
		return NULL;
S
Sage Weil 已提交
717
	}
718 719 720 721 722

	if (!m)
		*skip = 1;

	return m;
S
Sage Weil 已提交
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
}

/*
 * If the monitor connection resets, pick a new monitor and resubmit
 * any pending requests.
 */
static void mon_fault(struct ceph_connection *con)
{
	struct ceph_mon_client *monc = con->private;

	if (!monc)
		return;

	dout("mon_fault\n");
	mutex_lock(&monc->mutex);
	if (!con->private)
		goto out;

	if (monc->con && !monc->hunting)
		pr_info("mon%d %s session lost, "
			"hunting for new mon\n", monc->cur_mon,
			pr_addr(&monc->con->peer_addr.in_addr));

	__close_session(monc);
	if (!monc->hunting) {
		/* start hunting */
		monc->hunting = true;
750
		__open_session(monc);
S
Sage Weil 已提交
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
	} else {
		/* already hunting, let's wait a bit */
		__schedule_delayed(monc);
	}
out:
	mutex_unlock(&monc->mutex);
}

const static struct ceph_connection_operations mon_con_ops = {
	.get = ceph_con_get,
	.put = ceph_con_put,
	.dispatch = dispatch,
	.fault = mon_fault,
	.alloc_msg = mon_alloc_msg,
};