mon_client.c 33.2 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
#include <linux/ceph/ceph_debug.h>
S
Sage Weil 已提交
3

4
#include <linux/module.h>
S
Sage Weil 已提交
5
#include <linux/types.h>
6
#include <linux/slab.h>
S
Sage Weil 已提交
7 8 9
#include <linux/random.h>
#include <linux/sched.h>

10
#include <linux/ceph/ceph_features.h>
11 12
#include <linux/ceph/mon_client.h>
#include <linux/ceph/libceph.h>
13
#include <linux/ceph/debugfs.h>
14 15
#include <linux/ceph/decode.h>
#include <linux/ceph/auth.h>
S
Sage Weil 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

/*
 * 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.
 */

35
static const struct ceph_connection_operations mon_con_ops;
S
Sage Weil 已提交
36

37 38
static int __validate_auth(struct ceph_mon_client *monc);

S
Sage Weil 已提交
39 40 41
/*
 * Decode a monmap blob (e.g., during mount).
 */
J
Jeff Layton 已提交
42
static struct ceph_monmap *ceph_monmap_decode(void *p, void *end)
S
Sage Weil 已提交
43 44 45 46 47
{
	struct ceph_monmap *m = NULL;
	int i, err = -EINVAL;
	struct ceph_fsid fsid;
	u32 epoch, num_mon;
48 49 50 51
	u32 len;

	ceph_decode_32_safe(&p, end, len, bad);
	ceph_decode_need(&p, end, len, bad);
S
Sage Weil 已提交
52

J
Jeff Layton 已提交
53
	dout("monmap_decode %p %p len %d (%d)\n", p, end, len, (int)(end-p));
54
	p += sizeof(u16);  /* skip version */
S
Sage Weil 已提交
55 56 57

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

60
	num_mon = ceph_decode_32(&p);
S
Sage Weil 已提交
61

62
	if (num_mon > CEPH_MAX_MON)
S
Sage Weil 已提交
63
		goto bad;
64
	m = kmalloc(struct_size(m, mon_inst, num_mon), GFP_NOFS);
S
Sage Weil 已提交
65 66 67 68 69
	if (m == NULL)
		return ERR_PTR(-ENOMEM);
	m->fsid = fsid;
	m->epoch = epoch;
	m->num_mon = num_mon;
J
Jeff Layton 已提交
70 71 72 73 74 75 76 77 78 79
	for (i = 0; i < num_mon; ++i) {
		struct ceph_entity_inst *inst = &m->mon_inst[i];

		/* copy name portion */
		ceph_decode_copy_safe(&p, end, &inst->name,
					sizeof(inst->name), bad);
		err = ceph_decode_entity_addr(&p, end, &inst->addr);
		if (err)
			goto bad;
	}
S
Sage Weil 已提交
80 81 82 83
	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,
84
		     ceph_pr_addr(&m->mon_inst[i].addr));
S
Sage Weil 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
	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 已提交
100
		if (memcmp(addr, &m->mon_inst[i].addr, sizeof(*addr)) == 0)
S
Sage Weil 已提交
101 102 103 104
			return 1;
	return 0;
}

105 106 107 108 109 110 111 112
/*
 * Send an auth request.
 */
static void __send_prepared_auth_request(struct ceph_mon_client *monc, int len)
{
	monc->pending_auth = 1;
	monc->m_auth->front.iov_len = len;
	monc->m_auth->hdr.front_len = cpu_to_le32(len);
113
	ceph_msg_revoke(monc->m_auth);
114
	ceph_msg_get(monc->m_auth);  /* keep our ref */
115
	ceph_con_send(&monc->con, monc->m_auth);
116 117
}

S
Sage Weil 已提交
118 119 120 121 122
/*
 * Close monitor session, if any.
 */
static void __close_session(struct ceph_mon_client *monc)
{
123
	dout("__close_session closing mon%d\n", monc->cur_mon);
124
	ceph_msg_revoke(monc->m_auth);
125 126 127
	ceph_msg_revoke_incoming(monc->m_auth_reply);
	ceph_msg_revoke(monc->m_subscribe);
	ceph_msg_revoke_incoming(monc->m_subscribe_ack);
128
	ceph_con_close(&monc->con);
129

130 131
	monc->pending_auth = 0;
	ceph_auth_reset(monc->auth);
S
Sage Weil 已提交
132 133 134
}

/*
135 136
 * Pick a new monitor at random and set cur_mon.  If we are repicking
 * (i.e. cur_mon is already set), be sure to pick a different one.
S
Sage Weil 已提交
137
 */
138
static void pick_new_mon(struct ceph_mon_client *monc)
S
Sage Weil 已提交
139
{
140
	int old_mon = monc->cur_mon;
S
Sage Weil 已提交
141

142
	BUG_ON(monc->monmap->num_mon < 1);
S
Sage Weil 已提交
143

144 145 146 147 148 149 150 151 152 153 154 155 156
	if (monc->monmap->num_mon == 1) {
		monc->cur_mon = 0;
	} else {
		int max = monc->monmap->num_mon;
		int o = -1;
		int n;

		if (monc->cur_mon >= 0) {
			if (monc->cur_mon < monc->monmap->num_mon)
				o = monc->cur_mon;
			if (o >= 0)
				max--;
		}
157

158 159 160
		n = prandom_u32() % max;
		if (o >= 0 && n >= o)
			n++;
161

162
		monc->cur_mon = n;
S
Sage Weil 已提交
163
	}
164 165 166 167 168 169 170 171 172 173 174 175 176 177

	dout("%s mon%d -> mon%d out of %d mons\n", __func__, old_mon,
	     monc->cur_mon, monc->monmap->num_mon);
}

/*
 * Open a session with a new monitor.
 */
static void __open_session(struct ceph_mon_client *monc)
{
	int ret;

	pick_new_mon(monc);

178
	monc->hunting = true;
179 180 181 182 183 184
	if (monc->had_a_connection) {
		monc->hunt_mult *= CEPH_MONC_HUNT_BACKOFF;
		if (monc->hunt_mult > CEPH_MONC_HUNT_MAX_MULT)
			monc->hunt_mult = CEPH_MONC_HUNT_MAX_MULT;
	}

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
	monc->sub_renew_after = jiffies; /* i.e., expired */
	monc->sub_renew_sent = 0;

	dout("%s opening mon%d\n", __func__, monc->cur_mon);
	ceph_con_open(&monc->con, CEPH_ENTITY_TYPE_MON, monc->cur_mon,
		      &monc->monmap->mon_inst[monc->cur_mon].addr);

	/*
	 * send an initial keepalive to ensure our timestamp is valid
	 * by the time we are in an OPENED state
	 */
	ceph_con_keepalive(&monc->con);

	/* initiate authentication handshake */
	ret = ceph_auth_build_hello(monc->auth,
				    monc->m_auth->front.iov_base,
				    monc->m_auth->front_alloc_len);
	BUG_ON(ret <= 0);
	__send_prepared_auth_request(monc, ret);
S
Sage Weil 已提交
204 205
}

206 207 208 209
static void reopen_session(struct ceph_mon_client *monc)
{
	if (!monc->hunting)
		pr_info("mon%d %s session lost, hunting for new mon\n",
210
		    monc->cur_mon, ceph_pr_addr(&monc->con.peer_addr));
211 212 213 214 215

	__close_session(monc);
	__open_session(monc);
}

216 217 218 219 220 221 222
void ceph_monc_reopen_session(struct ceph_mon_client *monc)
{
	mutex_lock(&monc->mutex);
	reopen_session(monc);
	mutex_unlock(&monc->mutex);
}

223 224 225 226 227 228 229 230
static void un_backoff(struct ceph_mon_client *monc)
{
	monc->hunt_mult /= 2; /* reduce by 50% */
	if (monc->hunt_mult < 1)
		monc->hunt_mult = 1;
	dout("%s hunt_mult now %d\n", __func__, monc->hunt_mult);
}

S
Sage Weil 已提交
231 232 233 234 235
/*
 * Reschedule delayed work timer.
 */
static void __schedule_delayed(struct ceph_mon_client *monc)
{
236
	unsigned long delay;
S
Sage Weil 已提交
237

238 239 240
	if (monc->hunting)
		delay = CEPH_MONC_HUNT_INTERVAL * monc->hunt_mult;
	else
I
Ilya Dryomov 已提交
241
		delay = CEPH_MONC_PING_INTERVAL;
242

243
	dout("__schedule_delayed after %lu\n", delay);
244 245
	mod_delayed_work(system_wq, &monc->delayed_work,
			 round_jiffies_relative(delay));
S
Sage Weil 已提交
246 247
}

248 249 250
const char *ceph_sub_str[] = {
	[CEPH_SUB_MONMAP] = "monmap",
	[CEPH_SUB_OSDMAP] = "osdmap",
251 252
	[CEPH_SUB_FSMAP]  = "fsmap.user",
	[CEPH_SUB_MDSMAP] = "mdsmap",
253 254
};

S
Sage Weil 已提交
255
/*
256 257
 * Send subscribe request for one or more maps, according to
 * monc->subs.
S
Sage Weil 已提交
258 259 260
 */
static void __send_subscribe(struct ceph_mon_client *monc)
{
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
	struct ceph_msg *msg = monc->m_subscribe;
	void *p = msg->front.iov_base;
	void *const end = p + msg->front_alloc_len;
	int num = 0;
	int i;

	dout("%s sent %lu\n", __func__, monc->sub_renew_sent);

	BUG_ON(monc->cur_mon < 0);

	if (!monc->sub_renew_sent)
		monc->sub_renew_sent = jiffies | 1; /* never 0 */

	msg->hdr.version = cpu_to_le16(2);

	for (i = 0; i < ARRAY_SIZE(monc->subs); i++) {
		if (monc->subs[i].want)
			num++;
	}
	BUG_ON(num < 1); /* monmap sub is always there */
	ceph_encode_32(&p, num);
	for (i = 0; i < ARRAY_SIZE(monc->subs); i++) {
283 284
		char buf[32];
		int len;
285 286 287 288

		if (!monc->subs[i].want)
			continue;

289 290 291 292 293 294
		len = sprintf(buf, "%s", ceph_sub_str[i]);
		if (i == CEPH_SUB_MDSMAP &&
		    monc->fs_cluster_id != CEPH_FS_CLUSTER_ID_NONE)
			len += sprintf(buf + len, ".%d", monc->fs_cluster_id);

		dout("%s %s start %llu flags 0x%x\n", __func__, buf,
295 296
		     le64_to_cpu(monc->subs[i].item.start),
		     monc->subs[i].item.flags);
297
		ceph_encode_string(&p, end, buf, len);
298 299
		memcpy(p, &monc->subs[i].item, sizeof(monc->subs[i].item));
		p += sizeof(monc->subs[i].item);
S
Sage Weil 已提交
300
	}
301

302
	BUG_ON(p > end);
303 304 305 306
	msg->front.iov_len = p - msg->front.iov_base;
	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
	ceph_msg_revoke(msg);
	ceph_con_send(&monc->con, ceph_msg_get(msg));
S
Sage Weil 已提交
307 308 309 310 311
}

static void handle_subscribe_ack(struct ceph_mon_client *monc,
				 struct ceph_msg *msg)
{
312
	unsigned int seconds;
313 314 315 316 317
	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 已提交
318 319

	mutex_lock(&monc->mutex);
320
	if (monc->sub_renew_sent) {
321 322 323 324
		/*
		 * This is only needed for legacy (infernalis or older)
		 * MONs -- see delayed_work().
		 */
325 326 327 328 329 330 331 332 333
		monc->sub_renew_after = monc->sub_renew_sent +
					    (seconds >> 1) * HZ - 1;
		dout("%s sent %lu duration %d renew after %lu\n", __func__,
		     monc->sub_renew_sent, seconds, monc->sub_renew_after);
		monc->sub_renew_sent = 0;
	} else {
		dout("%s sent %lu renew after %lu, ignoring\n", __func__,
		     monc->sub_renew_sent, monc->sub_renew_after);
	}
S
Sage Weil 已提交
334 335 336 337
	mutex_unlock(&monc->mutex);
	return;
bad:
	pr_err("got corrupt subscribe-ack msg\n");
338
	ceph_msg_dump(msg);
S
Sage Weil 已提交
339 340 341
}

/*
342 343 344 345
 * Register interest in a map
 *
 * @sub: one of CEPH_SUB_*
 * @epoch: X for "every map since X", or 0 for "just the latest"
S
Sage Weil 已提交
346
 */
347 348
static bool __ceph_monc_want_map(struct ceph_mon_client *monc, int sub,
				 u32 epoch, bool continuous)
S
Sage Weil 已提交
349
{
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
	__le64 start = cpu_to_le64(epoch);
	u8 flags = !continuous ? CEPH_SUBSCRIBE_ONETIME : 0;

	dout("%s %s epoch %u continuous %d\n", __func__, ceph_sub_str[sub],
	     epoch, continuous);

	if (monc->subs[sub].want &&
	    monc->subs[sub].item.start == start &&
	    monc->subs[sub].item.flags == flags)
		return false;

	monc->subs[sub].item.start = start;
	monc->subs[sub].item.flags = flags;
	monc->subs[sub].want = true;

	return true;
}

bool ceph_monc_want_map(struct ceph_mon_client *monc, int sub, u32 epoch,
			bool continuous)
{
	bool need_request;

S
Sage Weil 已提交
373
	mutex_lock(&monc->mutex);
374
	need_request = __ceph_monc_want_map(monc, sub, epoch, continuous);
S
Sage Weil 已提交
375
	mutex_unlock(&monc->mutex);
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398

	return need_request;
}
EXPORT_SYMBOL(ceph_monc_want_map);

/*
 * Keep track of which maps we have
 *
 * @sub: one of CEPH_SUB_*
 */
static void __ceph_monc_got_map(struct ceph_mon_client *monc, int sub,
				u32 epoch)
{
	dout("%s %s epoch %u\n", __func__, ceph_sub_str[sub], epoch);

	if (monc->subs[sub].want) {
		if (monc->subs[sub].item.flags & CEPH_SUBSCRIBE_ONETIME)
			monc->subs[sub].want = false;
		else
			monc->subs[sub].item.start = cpu_to_le64(epoch + 1);
	}

	monc->subs[sub].have = epoch;
S
Sage Weil 已提交
399 400
}

401
void ceph_monc_got_map(struct ceph_mon_client *monc, int sub, u32 epoch)
S
Sage Weil 已提交
402 403
{
	mutex_lock(&monc->mutex);
404
	__ceph_monc_got_map(monc, sub, epoch);
S
Sage Weil 已提交
405 406
	mutex_unlock(&monc->mutex);
}
407
EXPORT_SYMBOL(ceph_monc_got_map);
S
Sage Weil 已提交
408

I
Ilya Dryomov 已提交
409 410 411 412 413 414 415 416
void ceph_monc_renew_subs(struct ceph_mon_client *monc)
{
	mutex_lock(&monc->mutex);
	__send_subscribe(monc);
	mutex_unlock(&monc->mutex);
}
EXPORT_SYMBOL(ceph_monc_renew_subs);

417 418 419 420 421 422
/*
 * Wait for an osdmap with a given epoch.
 *
 * @epoch: epoch to wait for
 * @timeout: in jiffies, 0 means "wait forever"
 */
423 424 425 426
int ceph_monc_wait_osdmap(struct ceph_mon_client *monc, u32 epoch,
			  unsigned long timeout)
{
	unsigned long started = jiffies;
427
	long ret;
428 429

	mutex_lock(&monc->mutex);
430
	while (monc->subs[CEPH_SUB_OSDMAP].have < epoch) {
431 432
		mutex_unlock(&monc->mutex);

433
		if (timeout && time_after_eq(jiffies, started + timeout))
434 435 436
			return -ETIMEDOUT;

		ret = wait_event_interruptible_timeout(monc->client->auth_wq,
437 438
				     monc->subs[CEPH_SUB_OSDMAP].have >= epoch,
				     ceph_timeout_jiffies(timeout));
439 440 441 442 443 444 445 446 447 448
		if (ret < 0)
			return ret;

		mutex_lock(&monc->mutex);
	}

	mutex_unlock(&monc->mutex);
	return 0;
}
EXPORT_SYMBOL(ceph_monc_wait_osdmap);
S
Sage Weil 已提交
449

450
/*
451 452
 * Open a session with a random monitor.  Request monmap and osdmap,
 * which are waited upon in __ceph_open_session().
453 454
 */
int ceph_monc_open_session(struct ceph_mon_client *monc)
S
Sage Weil 已提交
455 456
{
	mutex_lock(&monc->mutex);
457 458
	__ceph_monc_want_map(monc, CEPH_SUB_MONMAP, 0, true);
	__ceph_monc_want_map(monc, CEPH_SUB_OSDMAP, 0, false);
459
	__open_session(monc);
S
Sage Weil 已提交
460 461 462 463
	__schedule_delayed(monc);
	mutex_unlock(&monc->mutex);
	return 0;
}
464
EXPORT_SYMBOL(ceph_monc_open_session);
S
Sage Weil 已提交
465

466 467
static void ceph_monc_handle_map(struct ceph_mon_client *monc,
				 struct ceph_msg *msg)
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
{
	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));
J
Jeff Layton 已提交
483
		ceph_msg_dump(msg);
S
Sage Weil 已提交
484
		goto out;
485
	}
486 487

	if (ceph_check_fsid(monc->client, &monmap->fsid) < 0) {
488
		kfree(monmap);
S
Sage Weil 已提交
489
		goto out;
490 491 492 493 494
	}

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

495
	__ceph_monc_got_map(monc, CEPH_SUB_MONMAP, monc->monmap->epoch);
496
	client->have_fsid = true;
497

S
Sage Weil 已提交
498
out:
499
	mutex_unlock(&monc->mutex);
500
	wake_up_all(&client->auth_wq);
501 502
}

S
Sage Weil 已提交
503
/*
504
 * generic requests (currently statfs, mon_get_version)
S
Sage Weil 已提交
505
 */
I
Ilya Dryomov 已提交
506
DEFINE_RB_FUNCS(generic_request, struct ceph_mon_generic_request, tid, node)
507

508
static void release_generic_request(struct kref *kref)
S
Sage Weil 已提交
509
{
510 511
	struct ceph_mon_generic_request *req =
		container_of(kref, struct ceph_mon_generic_request, kref);
S
Sage Weil 已提交
512

513 514 515 516
	dout("%s greq %p request %p reply %p\n", __func__, req, req->request,
	     req->reply);
	WARN_ON(!RB_EMPTY_NODE(&req->node));

S
Sage Weil 已提交
517 518 519 520
	if (req->reply)
		ceph_msg_put(req->reply);
	if (req->request)
		ceph_msg_put(req->request);
Y
Yehuda Sadeh 已提交
521 522

	kfree(req);
S
Sage Weil 已提交
523 524
}

525
static void put_generic_request(struct ceph_mon_generic_request *req)
S
Sage Weil 已提交
526
{
527 528
	if (req)
		kref_put(&req->kref, release_generic_request);
S
Sage Weil 已提交
529 530
}

531
static void get_generic_request(struct ceph_mon_generic_request *req)
S
Sage Weil 已提交
532 533 534 535
{
	kref_get(&req->kref);
}

536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
static struct ceph_mon_generic_request *
alloc_generic_request(struct ceph_mon_client *monc, gfp_t gfp)
{
	struct ceph_mon_generic_request *req;

	req = kzalloc(sizeof(*req), gfp);
	if (!req)
		return NULL;

	req->monc = monc;
	kref_init(&req->kref);
	RB_CLEAR_NODE(&req->node);
	init_completion(&req->completion);

	dout("%s greq %p\n", __func__, req);
	return req;
}

static void register_generic_request(struct ceph_mon_generic_request *req)
{
	struct ceph_mon_client *monc = req->monc;

	WARN_ON(req->tid);

	get_generic_request(req);
	req->tid = ++monc->last_tid;
	insert_generic_request(&monc->generic_request_tree, req);
}

static void send_generic_request(struct ceph_mon_client *monc,
				 struct ceph_mon_generic_request *req)
{
	WARN_ON(!req->tid);

	dout("%s greq %p tid %llu\n", __func__, req, req->tid);
	req->request->hdr.tid = cpu_to_le64(req->tid);
	ceph_con_send(&monc->con, ceph_msg_get(req->request));
}

static void __finish_generic_request(struct ceph_mon_generic_request *req)
{
	struct ceph_mon_client *monc = req->monc;

	dout("%s greq %p tid %llu\n", __func__, req, req->tid);
	erase_generic_request(&monc->generic_request_tree, req);

	ceph_msg_revoke(req->request);
	ceph_msg_revoke_incoming(req->reply);
}

static void finish_generic_request(struct ceph_mon_generic_request *req)
{
	__finish_generic_request(req);
	put_generic_request(req);
}

static void complete_generic_request(struct ceph_mon_generic_request *req)
{
	if (req->complete_cb)
		req->complete_cb(req);
	else
		complete_all(&req->completion);
	put_generic_request(req);
}

601
static void cancel_generic_request(struct ceph_mon_generic_request *req)
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
{
	struct ceph_mon_client *monc = req->monc;
	struct ceph_mon_generic_request *lookup_req;

	dout("%s greq %p tid %llu\n", __func__, req, req->tid);

	mutex_lock(&monc->mutex);
	lookup_req = lookup_generic_request(&monc->generic_request_tree,
					    req->tid);
	if (lookup_req) {
		WARN_ON(lookup_req != req);
		finish_generic_request(req);
	}

	mutex_unlock(&monc->mutex);
}

static int wait_generic_request(struct ceph_mon_generic_request *req)
{
	int ret;

	dout("%s greq %p tid %llu\n", __func__, req, req->tid);
	ret = wait_for_completion_interruptible(&req->completion);
	if (ret)
		cancel_generic_request(req);
	else
		ret = req->result; /* completed */

	return ret;
}

633
static struct ceph_msg *get_generic_reply(struct ceph_connection *con,
S
Sage Weil 已提交
634 635 636 637
					 struct ceph_msg_header *hdr,
					 int *skip)
{
	struct ceph_mon_client *monc = con->private;
638
	struct ceph_mon_generic_request *req;
S
Sage Weil 已提交
639 640 641 642
	u64 tid = le64_to_cpu(hdr->tid);
	struct ceph_msg *m;

	mutex_lock(&monc->mutex);
I
Ilya Dryomov 已提交
643
	req = lookup_generic_request(&monc->generic_request_tree, tid);
S
Sage Weil 已提交
644
	if (!req) {
645
		dout("get_generic_reply %lld dne\n", tid);
S
Sage Weil 已提交
646 647 648
		*skip = 1;
		m = NULL;
	} else {
649
		dout("get_generic_reply %lld got %p\n", tid, req->reply);
A
Alex Elder 已提交
650
		*skip = 0;
S
Sage Weil 已提交
651 652 653 654 655 656 657 658 659 660 661
		m = ceph_msg_get(req->reply);
		/*
		 * we don't need to track the connection reading into
		 * this reply because we only have one open connection
		 * at a time, ever.
		 */
	}
	mutex_unlock(&monc->mutex);
	return m;
}

662 663 664
/*
 * statfs
 */
S
Sage Weil 已提交
665 666 667
static void handle_statfs_reply(struct ceph_mon_client *monc,
				struct ceph_msg *msg)
{
668
	struct ceph_mon_generic_request *req;
S
Sage Weil 已提交
669
	struct ceph_mon_statfs_reply *reply = msg->front.iov_base;
S
Sage Weil 已提交
670
	u64 tid = le64_to_cpu(msg->hdr.tid);
S
Sage Weil 已提交
671

672 673
	dout("%s msg %p tid %llu\n", __func__, msg, tid);

S
Sage Weil 已提交
674 675 676 677
	if (msg->front.iov_len != sizeof(*reply))
		goto bad;

	mutex_lock(&monc->mutex);
I
Ilya Dryomov 已提交
678
	req = lookup_generic_request(&monc->generic_request_tree, tid);
679 680 681
	if (!req) {
		mutex_unlock(&monc->mutex);
		return;
S
Sage Weil 已提交
682
	}
683 684 685 686

	req->result = 0;
	*req->u.st = reply->st; /* struct */
	__finish_generic_request(req);
S
Sage Weil 已提交
687
	mutex_unlock(&monc->mutex);
688 689

	complete_generic_request(req);
S
Sage Weil 已提交
690 691 692
	return;

bad:
693
	pr_err("corrupt statfs reply, tid %llu\n", tid);
694
	ceph_msg_dump(msg);
S
Sage Weil 已提交
695 696 697
}

/*
S
Sage Weil 已提交
698
 * Do a synchronous statfs().
S
Sage Weil 已提交
699
 */
D
Douglas Fuller 已提交
700 701
int ceph_monc_do_statfs(struct ceph_mon_client *monc, u64 data_pool,
			struct ceph_statfs *buf)
S
Sage Weil 已提交
702
{
703
	struct ceph_mon_generic_request *req;
S
Sage Weil 已提交
704
	struct ceph_mon_statfs *h;
705
	int ret = -ENOMEM;
S
Sage Weil 已提交
706

707
	req = alloc_generic_request(monc, GFP_NOFS);
S
Sage Weil 已提交
708
	if (!req)
709
		goto out;
S
Sage Weil 已提交
710

711 712
	req->request = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), GFP_NOFS,
				    true);
713
	if (!req->request)
S
Sage Weil 已提交
714
		goto out;
715 716

	req->reply = ceph_msg_new(CEPH_MSG_STATFS_REPLY, 64, GFP_NOFS, true);
717
	if (!req->reply)
S
Sage Weil 已提交
718 719
		goto out;

720
	req->u.st = buf;
D
Douglas Fuller 已提交
721
	req->request->hdr.version = cpu_to_le16(2);
722 723 724

	mutex_lock(&monc->mutex);
	register_generic_request(req);
S
Sage Weil 已提交
725 726
	/* fill out request */
	h = req->request->front.iov_base;
727 728 729
	h->monhdr.have_version = 0;
	h->monhdr.session_mon = cpu_to_le16(-1);
	h->monhdr.session_mon_tid = 0;
S
Sage Weil 已提交
730
	h->fsid = monc->monmap->fsid;
D
Douglas Fuller 已提交
731 732
	h->contains_data_pool = (data_pool != CEPH_NOPOOL);
	h->data_pool = cpu_to_le64(data_pool);
733 734
	send_generic_request(monc, req);
	mutex_unlock(&monc->mutex);
S
Sage Weil 已提交
735

736
	ret = wait_generic_request(req);
737
out:
738
	put_generic_request(req);
739
	return ret;
740
}
741
EXPORT_SYMBOL(ceph_monc_do_statfs);
742

743 744 745 746 747 748 749 750 751
static void handle_get_version_reply(struct ceph_mon_client *monc,
				     struct ceph_msg *msg)
{
	struct ceph_mon_generic_request *req;
	u64 tid = le64_to_cpu(msg->hdr.tid);
	void *p = msg->front.iov_base;
	void *end = p + msg->front_alloc_len;
	u64 handle;

752
	dout("%s msg %p tid %llu\n", __func__, msg, tid);
753 754 755 756 757 758 759

	ceph_decode_need(&p, end, 2*sizeof(u64), bad);
	handle = ceph_decode_64(&p);
	if (tid != 0 && tid != handle)
		goto bad;

	mutex_lock(&monc->mutex);
I
Ilya Dryomov 已提交
760
	req = lookup_generic_request(&monc->generic_request_tree, handle);
761 762 763
	if (!req) {
		mutex_unlock(&monc->mutex);
		return;
764
	}
765 766 767 768

	req->result = 0;
	req->u.newest = ceph_decode_64(&p);
	__finish_generic_request(req);
769 770
	mutex_unlock(&monc->mutex);

771
	complete_generic_request(req);
772
	return;
773

774
bad:
775
	pr_err("corrupt mon_get_version reply, tid %llu\n", tid);
776 777 778
	ceph_msg_dump(msg);
}

779 780 781
static struct ceph_mon_generic_request *
__ceph_monc_get_version(struct ceph_mon_client *monc, const char *what,
			ceph_monc_callback_t cb, u64 private_data)
782 783 784
{
	struct ceph_mon_generic_request *req;

785
	req = alloc_generic_request(monc, GFP_NOIO);
786
	if (!req)
787
		goto err_put_req;
788 789 790

	req->request = ceph_msg_new(CEPH_MSG_MON_GET_VERSION,
				    sizeof(u64) + sizeof(u32) + strlen(what),
791 792 793
				    GFP_NOIO, true);
	if (!req->request)
		goto err_put_req;
794

795 796 797 798
	req->reply = ceph_msg_new(CEPH_MSG_MON_GET_VERSION_REPLY, 32, GFP_NOIO,
				  true);
	if (!req->reply)
		goto err_put_req;
799

800 801
	req->complete_cb = cb;
	req->private_data = private_data;
802 803

	mutex_lock(&monc->mutex);
804 805 806 807 808 809 810 811 812 813 814
	register_generic_request(req);
	{
		void *p = req->request->front.iov_base;
		void *const end = p + req->request->front_alloc_len;

		ceph_encode_64(&p, req->tid); /* handle */
		ceph_encode_string(&p, end, what, strlen(what));
		WARN_ON(p != end);
	}
	send_generic_request(monc, req);
	mutex_unlock(&monc->mutex);
815

816
	return req;
817

818
err_put_req:
819
	put_generic_request(req);
820
	return ERR_PTR(-ENOMEM);
821
}
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864

/*
 * Send MMonGetVersion and wait for the reply.
 *
 * @what: one of "mdsmap", "osdmap" or "monmap"
 */
int ceph_monc_get_version(struct ceph_mon_client *monc, const char *what,
			  u64 *newest)
{
	struct ceph_mon_generic_request *req;
	int ret;

	req = __ceph_monc_get_version(monc, what, NULL, 0);
	if (IS_ERR(req))
		return PTR_ERR(req);

	ret = wait_generic_request(req);
	if (!ret)
		*newest = req->u.newest;

	put_generic_request(req);
	return ret;
}
EXPORT_SYMBOL(ceph_monc_get_version);

/*
 * Send MMonGetVersion,
 *
 * @what: one of "mdsmap", "osdmap" or "monmap"
 */
int ceph_monc_get_version_async(struct ceph_mon_client *monc, const char *what,
				ceph_monc_callback_t cb, u64 private_data)
{
	struct ceph_mon_generic_request *req;

	req = __ceph_monc_get_version(monc, what, cb, private_data);
	if (IS_ERR(req))
		return PTR_ERR(req);

	put_generic_request(req);
	return 0;
}
EXPORT_SYMBOL(ceph_monc_get_version_async);
865

866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
static void handle_command_ack(struct ceph_mon_client *monc,
			       struct ceph_msg *msg)
{
	struct ceph_mon_generic_request *req;
	void *p = msg->front.iov_base;
	void *const end = p + msg->front_alloc_len;
	u64 tid = le64_to_cpu(msg->hdr.tid);

	dout("%s msg %p tid %llu\n", __func__, msg, tid);

	ceph_decode_need(&p, end, sizeof(struct ceph_mon_request_header) +
							    sizeof(u32), bad);
	p += sizeof(struct ceph_mon_request_header);

	mutex_lock(&monc->mutex);
	req = lookup_generic_request(&monc->generic_request_tree, tid);
	if (!req) {
		mutex_unlock(&monc->mutex);
		return;
	}

	req->result = ceph_decode_32(&p);
	__finish_generic_request(req);
	mutex_unlock(&monc->mutex);

	complete_generic_request(req);
	return;

bad:
	pr_err("corrupt mon_command ack, tid %llu\n", tid);
	ceph_msg_dump(msg);
}

int ceph_monc_blacklist_add(struct ceph_mon_client *monc,
			    struct ceph_entity_addr *client_addr)
{
	struct ceph_mon_generic_request *req;
	struct ceph_mon_command *h;
	int ret = -ENOMEM;
	int len;

	req = alloc_generic_request(monc, GFP_NOIO);
	if (!req)
		goto out;

	req->request = ceph_msg_new(CEPH_MSG_MON_COMMAND, 256, GFP_NOIO, true);
	if (!req->request)
		goto out;

	req->reply = ceph_msg_new(CEPH_MSG_MON_COMMAND_ACK, 512, GFP_NOIO,
				  true);
	if (!req->reply)
		goto out;

	mutex_lock(&monc->mutex);
	register_generic_request(req);
	h = req->request->front.iov_base;
	h->monhdr.have_version = 0;
	h->monhdr.session_mon = cpu_to_le16(-1);
	h->monhdr.session_mon_tid = 0;
	h->fsid = monc->monmap->fsid;
	h->num_strs = cpu_to_le32(1);
	len = sprintf(h->str, "{ \"prefix\": \"osd blacklist\", \
		                 \"blacklistop\": \"add\", \
				 \"addr\": \"%pISpc/%u\" }",
		      &client_addr->in_addr, le32_to_cpu(client_addr->nonce));
	h->str_len = cpu_to_le32(len);
	send_generic_request(monc, req);
	mutex_unlock(&monc->mutex);

	ret = wait_generic_request(req);
937 938 939 940 941 942 943 944 945
	if (!ret)
		/*
		 * Make sure we have the osdmap that includes the blacklist
		 * entry.  This is needed to ensure that the OSDs pick up the
		 * new blacklist before processing any future requests from
		 * this client.
		 */
		ret = ceph_wait_for_latest_osdmap(monc->client, 0);

946 947 948 949 950 951
out:
	put_generic_request(req);
	return ret;
}
EXPORT_SYMBOL(ceph_monc_blacklist_add);

S
Sage Weil 已提交
952
/*
953
 * Resend pending generic requests.
S
Sage Weil 已提交
954
 */
955
static void __resend_generic_request(struct ceph_mon_client *monc)
S
Sage Weil 已提交
956
{
957
	struct ceph_mon_generic_request *req;
958
	struct rb_node *p;
S
Sage Weil 已提交
959

960 961
	for (p = rb_first(&monc->generic_request_tree); p; p = rb_next(p)) {
		req = rb_entry(p, struct ceph_mon_generic_request, node);
962
		ceph_msg_revoke(req->request);
963
		ceph_msg_revoke_incoming(req->reply);
964
		ceph_con_send(&monc->con, ceph_msg_get(req->request));
S
Sage Weil 已提交
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
	}
}

/*
 * 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);
980
	if (monc->hunting) {
981 982
		dout("%s continuing hunt\n", __func__);
		reopen_session(monc);
S
Sage Weil 已提交
983
	} else {
984 985
		int is_auth = ceph_auth_is_authenticated(monc->auth);
		if (ceph_con_keepalive_expired(&monc->con,
I
Ilya Dryomov 已提交
986
					       CEPH_MONC_PING_TIMEOUT)) {
987 988
			dout("monc keepalive timeout\n");
			is_auth = 0;
989
			reopen_session(monc);
990
		}
991

992 993 994
		if (!monc->hunting) {
			ceph_con_keepalive(&monc->con);
			__validate_auth(monc);
995
			un_backoff(monc);
996
		}
997

998 999
		if (is_auth &&
		    !(monc->con.peer_features & CEPH_FEATURE_MON_STATEFUL_SUB)) {
1000 1001 1002 1003 1004 1005 1006
			unsigned long now = jiffies;

			dout("%s renew subs? now %lu renew after %lu\n",
			     __func__, now, monc->sub_renew_after);
			if (time_after_eq(now, monc->sub_renew_after))
				__send_subscribe(monc);
		}
S
Sage Weil 已提交
1007 1008 1009 1010 1011
	}
	__schedule_delayed(monc);
	mutex_unlock(&monc->mutex);
}

1012 1013 1014 1015 1016 1017
/*
 * 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)
{
1018 1019 1020
	struct ceph_options *opt = monc->client->options;
	struct ceph_entity_addr *mon_addr = opt->mon_addr;
	int num_mon = opt->num_mon;
1021 1022 1023
	int i;

	/* build initial monmap */
1024
	monc->monmap = kzalloc(struct_size(monc->monmap, mon_inst, num_mon),
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
			       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;
	return 0;
}

S
Sage Weil 已提交
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
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);

1049 1050 1051 1052
	err = build_initial_monmap(monc);
	if (err)
		goto out;

1053
	/* connection */
1054
	/* authentication */
1055
	monc->auth = ceph_auth_init(cl->options->name,
1056
				    cl->options->key);
1057 1058
	if (IS_ERR(monc->auth)) {
		err = PTR_ERR(monc->auth);
1059
		goto out_monmap;
1060
	}
1061 1062 1063 1064
	monc->auth->want_keys =
		CEPH_ENTITY_TYPE_AUTH | CEPH_ENTITY_TYPE_MON |
		CEPH_ENTITY_TYPE_OSD | CEPH_ENTITY_TYPE_MDS;

1065
	/* msgs */
1066
	err = -ENOMEM;
1067
	monc->m_subscribe_ack = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE_ACK,
1068
				     sizeof(struct ceph_mon_subscribe_ack),
1069
				     GFP_KERNEL, true);
1070
	if (!monc->m_subscribe_ack)
1071
		goto out_auth;
1072

1073 1074
	monc->m_subscribe = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 128,
					 GFP_KERNEL, true);
1075 1076 1077
	if (!monc->m_subscribe)
		goto out_subscribe_ack;

1078 1079
	monc->m_auth_reply = ceph_msg_new(CEPH_MSG_AUTH_REPLY, 4096,
					  GFP_KERNEL, true);
1080
	if (!monc->m_auth_reply)
1081
		goto out_subscribe;
1082

1083
	monc->m_auth = ceph_msg_new(CEPH_MSG_AUTH, 4096, GFP_KERNEL, true);
1084
	monc->pending_auth = 0;
1085
	if (!monc->m_auth)
1086
		goto out_auth_reply;
S
Sage Weil 已提交
1087

1088 1089 1090
	ceph_con_init(&monc->con, monc, &mon_con_ops,
		      &monc->client->msgr);

S
Sage Weil 已提交
1091
	monc->cur_mon = -1;
1092 1093
	monc->had_a_connection = false;
	monc->hunt_mult = 1;
S
Sage Weil 已提交
1094 1095

	INIT_DELAYED_WORK(&monc->delayed_work, delayed_work);
1096
	monc->generic_request_tree = RB_ROOT;
S
Sage Weil 已提交
1097 1098
	monc->last_tid = 0;

1099 1100
	monc->fs_cluster_id = CEPH_FS_CLUSTER_ID_NONE;

1101 1102
	return 0;

1103 1104
out_auth_reply:
	ceph_msg_put(monc->m_auth_reply);
1105 1106
out_subscribe:
	ceph_msg_put(monc->m_subscribe);
1107 1108
out_subscribe_ack:
	ceph_msg_put(monc->m_subscribe_ack);
1109 1110
out_auth:
	ceph_auth_destroy(monc->auth);
1111 1112
out_monmap:
	kfree(monc->monmap);
S
Sage Weil 已提交
1113 1114 1115
out:
	return err;
}
1116
EXPORT_SYMBOL(ceph_monc_init);
S
Sage Weil 已提交
1117 1118 1119 1120 1121 1122 1123 1124

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);
1125
	monc->cur_mon = -1;
S
Sage Weil 已提交
1126 1127
	mutex_unlock(&monc->mutex);

1128 1129 1130 1131 1132 1133 1134 1135
	/*
	 * flush msgr queue before we destroy ourselves to ensure that:
	 *  - any work that references our embedded con is finished.
	 *  - any osd_client or other work that may reference an authorizer
	 *    finishes before we shut down the auth subsystem.
	 */
	ceph_msgr_flush();

1136 1137
	ceph_auth_destroy(monc->auth);

1138 1139
	WARN_ON(!RB_EMPTY_ROOT(&monc->generic_request_tree));

1140
	ceph_msg_put(monc->m_auth);
1141
	ceph_msg_put(monc->m_auth_reply);
1142
	ceph_msg_put(monc->m_subscribe);
1143
	ceph_msg_put(monc->m_subscribe_ack);
S
Sage Weil 已提交
1144 1145 1146

	kfree(monc->monmap);
}
1147
EXPORT_SYMBOL(ceph_monc_stop);
S
Sage Weil 已提交
1148

1149 1150 1151 1152 1153
static void finish_hunting(struct ceph_mon_client *monc)
{
	if (monc->hunting) {
		dout("%s found mon%d\n", __func__, monc->cur_mon);
		monc->hunting = false;
1154
		monc->had_a_connection = true;
1155
		un_backoff(monc);
1156
		__schedule_delayed(monc);
1157 1158 1159
	}
}

1160 1161 1162 1163
static void handle_auth_reply(struct ceph_mon_client *monc,
			      struct ceph_msg *msg)
{
	int ret;
1164
	int was_auth = 0;
1165 1166

	mutex_lock(&monc->mutex);
1167
	was_auth = ceph_auth_is_authenticated(monc->auth);
1168
	monc->pending_auth = 0;
1169 1170 1171
	ret = ceph_handle_auth_reply(monc->auth, msg->front.iov_base,
				     msg->front.iov_len,
				     monc->m_auth->front.iov_base,
1172
				     monc->m_auth->front_alloc_len);
1173 1174 1175 1176 1177 1178 1179
	if (ret > 0) {
		__send_prepared_auth_request(monc, ret);
		goto out;
	}

	finish_hunting(monc);

1180
	if (ret < 0) {
1181
		monc->client->auth_err = ret;
1182
	} else if (!was_auth && ceph_auth_is_authenticated(monc->auth)) {
1183
		dout("authenticated, starting session\n");
1184

1185 1186
		monc->client->msgr.inst.name.type = CEPH_ENTITY_TYPE_CLIENT;
		monc->client->msgr.inst.name.num =
Y
Yehuda Sadeh 已提交
1187
					cpu_to_le64(monc->auth->global_id);
1188

1189
		__send_subscribe(monc);
1190
		__resend_generic_request(monc);
1191 1192

		pr_info("mon%d %s session established\n", monc->cur_mon,
1193
			ceph_pr_addr(&monc->con.peer_addr));
1194
	}
1195 1196

out:
1197
	mutex_unlock(&monc->mutex);
1198 1199
	if (monc->client->auth_err < 0)
		wake_up_all(&monc->client->auth_wq);
1200 1201
}

1202 1203 1204 1205 1206 1207 1208 1209
static int __validate_auth(struct ceph_mon_client *monc)
{
	int ret;

	if (monc->pending_auth)
		return 0;

	ret = ceph_build_auth(monc->auth, monc->m_auth->front.iov_base,
1210
			      monc->m_auth->front_alloc_len);
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
	if (ret <= 0)
		return ret; /* either an error, or no need to authenticate */
	__send_prepared_auth_request(monc, ret);
	return 0;
}

int ceph_monc_validate_auth(struct ceph_mon_client *monc)
{
	int ret;

	mutex_lock(&monc->mutex);
	ret = __validate_auth(monc);
	mutex_unlock(&monc->mutex);
	return ret;
}
1226
EXPORT_SYMBOL(ceph_monc_validate_auth);
1227

S
Sage Weil 已提交
1228 1229 1230 1231 1232 1233 1234 1235 1236
/*
 * 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);

	switch (type) {
1237 1238
	case CEPH_MSG_AUTH_REPLY:
		handle_auth_reply(monc, msg);
S
Sage Weil 已提交
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
		break;

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

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

1249 1250 1251 1252
	case CEPH_MSG_MON_GET_VERSION_REPLY:
		handle_get_version_reply(monc, msg);
		break;

1253 1254 1255 1256
	case CEPH_MSG_MON_COMMAND_ACK:
		handle_command_ack(monc, msg);
		break;

1257 1258 1259 1260
	case CEPH_MSG_MON_MAP:
		ceph_monc_handle_map(monc, msg);
		break;

S
Sage Weil 已提交
1261 1262 1263 1264 1265
	case CEPH_MSG_OSD_MAP:
		ceph_osdc_handle_map(&monc->client->osdc, msg);
		break;

	default:
1266 1267 1268 1269
		/* can the chained handler handle it? */
		if (monc->client->extra_mon_dispatch &&
		    monc->client->extra_mon_dispatch(monc->client, msg) == 0)
			break;
S
Stephen Hemminger 已提交
1270

S
Sage Weil 已提交
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
		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,
1281 1282
				      struct ceph_msg_header *hdr,
				      int *skip)
S
Sage Weil 已提交
1283 1284 1285
{
	struct ceph_mon_client *monc = con->private;
	int type = le16_to_cpu(hdr->type);
1286
	int front_len = le32_to_cpu(hdr->front_len);
1287
	struct ceph_msg *m = NULL;
S
Sage Weil 已提交
1288

1289
	*skip = 0;
1290

S
Sage Weil 已提交
1291 1292
	switch (type) {
	case CEPH_MSG_MON_SUBSCRIBE_ACK:
1293
		m = ceph_msg_get(monc->m_subscribe_ack);
1294
		break;
S
Sage Weil 已提交
1295
	case CEPH_MSG_STATFS_REPLY:
1296
	case CEPH_MSG_MON_COMMAND_ACK:
1297
		return get_generic_reply(con, hdr, skip);
1298
	case CEPH_MSG_AUTH_REPLY:
1299
		m = ceph_msg_get(monc->m_auth_reply);
1300
		break;
1301 1302 1303 1304 1305 1306
	case CEPH_MSG_MON_GET_VERSION_REPLY:
		if (le64_to_cpu(hdr->tid) != 0)
			return get_generic_reply(con, hdr, skip);

		/*
		 * Older OSDs don't set reply tid even if the orignal
1307 1308
		 * request had a non-zero tid.  Work around this weirdness
		 * by allocating a new message.
1309
		 */
1310
		/* fall through */
1311 1312 1313
	case CEPH_MSG_MON_MAP:
	case CEPH_MSG_MDS_MAP:
	case CEPH_MSG_OSD_MAP:
1314
	case CEPH_MSG_FS_MAP_USER:
1315
		m = ceph_msg_new(type, front_len, GFP_NOFS, false);
A
Alex Elder 已提交
1316 1317
		if (!m)
			return NULL;	/* ENOMEM--return skip == 0 */
1318
		break;
S
Sage Weil 已提交
1319
	}
1320

1321 1322
	if (!m) {
		pr_info("alloc_msg unknown type %d\n", type);
1323
		*skip = 1;
1324
	} else if (front_len > m->front_alloc_len) {
1325 1326 1327 1328
		pr_warn("mon_alloc_msg front %d > prealloc %d (%u#%llu)\n",
			front_len, m->front_alloc_len,
			(unsigned int)con->peer_name.type,
			le64_to_cpu(con->peer_name.num));
1329 1330
		ceph_msg_put(m);
		m = ceph_msg_new(type, front_len, GFP_NOFS, false);
1331
	}
1332

1333
	return m;
S
Sage Weil 已提交
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
}

/*
 * 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;

	mutex_lock(&monc->mutex);
1345 1346 1347 1348 1349 1350 1351 1352 1353
	dout("%s mon%d\n", __func__, monc->cur_mon);
	if (monc->cur_mon >= 0) {
		if (!monc->hunting) {
			dout("%s hunting for new mon\n", __func__);
			reopen_session(monc);
			__schedule_delayed(monc);
		} else {
			dout("%s already hunting\n", __func__);
		}
S
Sage Weil 已提交
1354 1355 1356 1357
	}
	mutex_unlock(&monc->mutex);
}

1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
/*
 * We can ignore refcounting on the connection struct, as all references
 * will come from the messenger workqueue, which is drained prior to
 * mon_client destruction.
 */
static struct ceph_connection *con_get(struct ceph_connection *con)
{
	return con;
}

static void con_put(struct ceph_connection *con)
{
}

1372
static const struct ceph_connection_operations mon_con_ops = {
1373 1374
	.get = con_get,
	.put = con_put,
S
Sage Weil 已提交
1375 1376 1377 1378
	.dispatch = dispatch,
	.fault = mon_fault,
	.alloc_msg = mon_alloc_msg,
};