mon_client.c 27.4 KB
Newer Older
1
#include <linux/ceph/ceph_debug.h>
S
Sage Weil 已提交
2

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

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

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

33
static const struct ceph_connection_operations mon_con_ops;
S
Sage Weil 已提交
34

35 36
static int __validate_auth(struct ceph_mon_client *monc);

S
Sage Weil 已提交
37 38 39 40 41 42 43 44 45 46
/*
 * 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;
47 48 49 50
	u32 len;

	ceph_decode_32_safe(&p, end, len, bad);
	ceph_decode_need(&p, end, len, bad);
S
Sage Weil 已提交
51 52 53 54 55 56 57

	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));
58
	epoch = ceph_decode_32(&p);
S
Sage Weil 已提交
59

60
	num_mon = ceph_decode_32(&p);
S
Sage Weil 已提交
61 62 63 64 65 66 67 68 69 70 71
	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]));
72 73
	for (i = 0; i < num_mon; i++)
		ceph_decode_addr(&m->mon_inst[i].addr);
S
Sage Weil 已提交
74 75 76 77 78

	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,
79
		     ceph_pr_addr(&m->mon_inst[i].addr.in_addr));
S
Sage Weil 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
	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 已提交
96
		if (memcmp(addr, &m->mon_inst[i].addr, sizeof(*addr)) == 0)
S
Sage Weil 已提交
97 98 99 100
			return 1;
	return 0;
}

101 102 103 104 105 106 107 108
/*
 * 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);
109
	ceph_msg_revoke(monc->m_auth);
110
	ceph_msg_get(monc->m_auth);  /* keep our ref */
111
	ceph_con_send(&monc->con, monc->m_auth);
112 113
}

S
Sage Weil 已提交
114 115 116 117 118
/*
 * Close monitor session, if any.
 */
static void __close_session(struct ceph_mon_client *monc)
{
119
	dout("__close_session closing mon%d\n", monc->cur_mon);
120
	ceph_msg_revoke(monc->m_auth);
121 122 123
	ceph_msg_revoke_incoming(monc->m_auth_reply);
	ceph_msg_revoke(monc->m_subscribe);
	ceph_msg_revoke_incoming(monc->m_subscribe_ack);
124
	ceph_con_close(&monc->con);
125 126 127
	monc->cur_mon = -1;
	monc->pending_auth = 0;
	ceph_auth_reset(monc->auth);
S
Sage Weil 已提交
128 129 130 131 132 133 134 135
}

/*
 * Open a session with a (new) monitor.
 */
static int __open_session(struct ceph_mon_client *monc)
{
	char r;
136
	int ret;
S
Sage Weil 已提交
137 138 139 140 141 142 143 144 145 146

	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;

147
		dout("open_session mon%d opening\n", monc->cur_mon);
148
		ceph_con_open(&monc->con,
149
			      CEPH_ENTITY_TYPE_MON, monc->cur_mon,
S
Sage Weil 已提交
150
			      &monc->monmap->mon_inst[monc->cur_mon].addr);
151

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

156 157 158
		/* initiatiate authentication handshake */
		ret = ceph_auth_build_hello(monc->auth,
					    monc->m_auth->front.iov_base,
159
					    monc->m_auth->front_alloc_len);
160
		__send_prepared_auth_request(monc, ret);
S
Sage Weil 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
	} 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)
{
177 178
	struct ceph_options *opt = monc->client->options;
	unsigned long delay;
S
Sage Weil 已提交
179

180
	if (monc->cur_mon < 0 || __sub_expired(monc)) {
S
Sage Weil 已提交
181
		delay = 10 * HZ;
182
	} else {
S
Sage Weil 已提交
183
		delay = 20 * HZ;
184 185 186 187 188 189
		if (opt->monc_ping_timeout > 0)
			delay = min(delay, opt->monc_ping_timeout / 3);
	}
	dout("__schedule_delayed after %lu\n", delay);
	schedule_delayed_work(&monc->delayed_work,
			      round_jiffies_relative(delay));
S
Sage Weil 已提交
190 191 192 193 194 195 196 197
}

/*
 * 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",
198
	     (unsigned int)monc->sub_sent, __sub_expired(monc),
S
Sage Weil 已提交
199 200 201
	     monc->want_next_osdmap);
	if ((__sub_expired(monc) && !monc->sub_sent) ||
	    monc->want_next_osdmap == 1) {
202
		struct ceph_msg *msg = monc->m_subscribe;
S
Sage Weil 已提交
203 204
		struct ceph_mon_subscribe_item *i;
		void *p, *end;
205
		int num;
S
Sage Weil 已提交
206 207

		p = msg->front.iov_base;
208
		end = p + msg->front_alloc_len;
S
Sage Weil 已提交
209

210 211 212
		num = 1 + !!monc->want_next_osdmap + !!monc->want_mdsmap;
		ceph_encode_32(&p, num);

S
Sage Weil 已提交
213 214
		if (monc->want_next_osdmap) {
			dout("__send_subscribe to 'osdmap' %u\n",
215
			     (unsigned int)monc->have_osdmap);
S
Sage Weil 已提交
216 217 218 219 220 221 222
			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 */
		}
223 224
		if (monc->want_mdsmap) {
			dout("__send_subscribe to 'mdsmap' %u+\n",
225
			     (unsigned int)monc->have_mdsmap);
226 227 228 229 230 231
			ceph_encode_string(&p, end, "mdsmap", 6);
			i = p;
			i->have = cpu_to_le64(monc->have_mdsmap);
			i->onetime = 0;
			p += sizeof(*i);
		}
232 233 234 235 236
		ceph_encode_string(&p, end, "monmap", 6);
		i = p;
		i->have = 0;
		i->onetime = 0;
		p += sizeof(*i);
S
Sage Weil 已提交
237 238 239

		msg->front.iov_len = p - msg->front.iov_base;
		msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
240
		ceph_msg_revoke(msg);
241
		ceph_con_send(&monc->con, ceph_msg_get(msg));
S
Sage Weil 已提交
242 243 244 245 246 247 248 249

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

static void handle_subscribe_ack(struct ceph_mon_client *monc,
				 struct ceph_msg *msg)
{
250
	unsigned int seconds;
251 252 253 254 255
	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 已提交
256 257 258 259

	mutex_lock(&monc->mutex);
	if (monc->hunting) {
		pr_info("mon%d %s session established\n",
260
			monc->cur_mon,
261
			ceph_pr_addr(&monc->con.peer_addr.in_addr));
S
Sage Weil 已提交
262 263 264
		monc->hunting = false;
	}
	dout("handle_subscribe_ack after %d seconds\n", seconds);
265
	monc->sub_renew_after = monc->sub_sent + (seconds >> 1)*HZ - 1;
S
Sage Weil 已提交
266 267 268 269 270
	monc->sub_sent = 0;
	mutex_unlock(&monc->mutex);
	return;
bad:
	pr_err("got corrupt subscribe-ack msg\n");
271
	ceph_msg_dump(msg);
S
Sage Weil 已提交
272 273 274 275 276 277 278 279 280 281 282 283
}

/*
 * 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;
}
284
EXPORT_SYMBOL(ceph_monc_got_mdsmap);
S
Sage Weil 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307

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);
}
308 309
EXPORT_SYMBOL(ceph_monc_request_next_osdmap);

310 311 312 313 314 315
/*
 * Wait for an osdmap with a given epoch.
 *
 * @epoch: epoch to wait for
 * @timeout: in jiffies, 0 means "wait forever"
 */
316 317 318 319
int ceph_monc_wait_osdmap(struct ceph_mon_client *monc, u32 epoch,
			  unsigned long timeout)
{
	unsigned long started = jiffies;
320
	long ret;
321 322 323 324 325

	mutex_lock(&monc->mutex);
	while (monc->have_osdmap < epoch) {
		mutex_unlock(&monc->mutex);

326
		if (timeout && time_after_eq(jiffies, started + timeout))
327 328 329
			return -ETIMEDOUT;

		ret = wait_event_interruptible_timeout(monc->client->auth_wq,
330 331
						monc->have_osdmap >= epoch,
						ceph_timeout_jiffies(timeout));
332 333 334 335 336 337 338 339 340 341
		if (ret < 0)
			return ret;

		mutex_lock(&monc->mutex);
	}

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

343
/*
S
Sage Weil 已提交
344
 *
345 346
 */
int ceph_monc_open_session(struct ceph_mon_client *monc)
S
Sage Weil 已提交
347 348
{
	mutex_lock(&monc->mutex);
349
	__open_session(monc);
S
Sage Weil 已提交
350 351 352 353
	__schedule_delayed(monc);
	mutex_unlock(&monc->mutex);
	return 0;
}
354
EXPORT_SYMBOL(ceph_monc_open_session);
S
Sage Weil 已提交
355

356 357 358 359 360 361 362 363 364 365 366
/*
 * We require the fsid and global_id in order to initialize our
 * debugfs dir.
 */
static bool have_debugfs_info(struct ceph_mon_client *monc)
{
	dout("have_debugfs_info fsid %d globalid %lld\n",
	     (int)monc->client->have_fsid, monc->auth->global_id);
	return monc->client->have_fsid && monc->auth->global_id > 0;
}

367 368
static void ceph_monc_handle_map(struct ceph_mon_client *monc,
				 struct ceph_msg *msg)
369 370 371 372
{
	struct ceph_client *client = monc->client;
	struct ceph_monmap *monmap = NULL, *old = monc->monmap;
	void *p, *end;
373
	int had_debugfs_info, init_debugfs = 0;
374 375 376

	mutex_lock(&monc->mutex);

377 378
	had_debugfs_info = have_debugfs_info(monc);

379 380 381 382 383 384 385 386
	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 已提交
387
		goto out;
388
	}
389 390

	if (ceph_check_fsid(monc->client, &monmap->fsid) < 0) {
391
		kfree(monmap);
S
Sage Weil 已提交
392
		goto out;
393 394 395 396 397
	}

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

398 399
	if (!client->have_fsid) {
		client->have_fsid = true;
400 401 402 403 404 405
		if (!had_debugfs_info && have_debugfs_info(monc)) {
			pr_info("client%lld fsid %pU\n",
				ceph_client_id(monc->client),
				&monc->client->fsid);
			init_debugfs = 1;
		}
406
		mutex_unlock(&monc->mutex);
407 408 409 410 411 412 413 414 415

		if (init_debugfs) {
			/*
			 * do debugfs initialization without mutex to avoid
			 * creating a locking dependency
			 */
			ceph_debugfs_client_init(monc->client);
		}

416 417
		goto out_unlocked;
	}
S
Sage Weil 已提交
418
out:
419
	mutex_unlock(&monc->mutex);
420
out_unlocked:
421
	wake_up_all(&client->auth_wq);
422 423
}

S
Sage Weil 已提交
424
/*
425
 * generic requests (currently statfs, mon_get_version)
S
Sage Weil 已提交
426
 */
427
static struct ceph_mon_generic_request *__lookup_generic_req(
428 429
	struct ceph_mon_client *monc, u64 tid)
{
430 431
	struct ceph_mon_generic_request *req;
	struct rb_node *n = monc->generic_request_tree.rb_node;
432 433

	while (n) {
434
		req = rb_entry(n, struct ceph_mon_generic_request, node);
435 436 437 438 439 440 441 442 443 444
		if (tid < req->tid)
			n = n->rb_left;
		else if (tid > req->tid)
			n = n->rb_right;
		else
			return req;
	}
	return NULL;
}

445 446
static void __insert_generic_request(struct ceph_mon_client *monc,
			    struct ceph_mon_generic_request *new)
447
{
448
	struct rb_node **p = &monc->generic_request_tree.rb_node;
449
	struct rb_node *parent = NULL;
450
	struct ceph_mon_generic_request *req = NULL;
451 452 453

	while (*p) {
		parent = *p;
454
		req = rb_entry(parent, struct ceph_mon_generic_request, node);
455 456 457 458 459 460 461 462 463
		if (new->tid < req->tid)
			p = &(*p)->rb_left;
		else if (new->tid > req->tid)
			p = &(*p)->rb_right;
		else
			BUG();
	}

	rb_link_node(&new->node, parent, p);
464
	rb_insert_color(&new->node, &monc->generic_request_tree);
465 466
}

467
static void release_generic_request(struct kref *kref)
S
Sage Weil 已提交
468
{
469 470
	struct ceph_mon_generic_request *req =
		container_of(kref, struct ceph_mon_generic_request, kref);
S
Sage Weil 已提交
471 472 473 474 475

	if (req->reply)
		ceph_msg_put(req->reply);
	if (req->request)
		ceph_msg_put(req->request);
Y
Yehuda Sadeh 已提交
476 477

	kfree(req);
S
Sage Weil 已提交
478 479
}

480
static void put_generic_request(struct ceph_mon_generic_request *req)
S
Sage Weil 已提交
481
{
482
	kref_put(&req->kref, release_generic_request);
S
Sage Weil 已提交
483 484
}

485
static void get_generic_request(struct ceph_mon_generic_request *req)
S
Sage Weil 已提交
486 487 488 489
{
	kref_get(&req->kref);
}

490
static struct ceph_msg *get_generic_reply(struct ceph_connection *con,
S
Sage Weil 已提交
491 492 493 494
					 struct ceph_msg_header *hdr,
					 int *skip)
{
	struct ceph_mon_client *monc = con->private;
495
	struct ceph_mon_generic_request *req;
S
Sage Weil 已提交
496 497 498 499
	u64 tid = le64_to_cpu(hdr->tid);
	struct ceph_msg *m;

	mutex_lock(&monc->mutex);
500
	req = __lookup_generic_req(monc, tid);
S
Sage Weil 已提交
501
	if (!req) {
502
		dout("get_generic_reply %lld dne\n", tid);
S
Sage Weil 已提交
503 504 505
		*skip = 1;
		m = NULL;
	} else {
506
		dout("get_generic_reply %lld got %p\n", tid, req->reply);
A
Alex Elder 已提交
507
		*skip = 0;
S
Sage Weil 已提交
508 509 510 511 512 513 514 515 516 517 518
		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;
}

519 520
static int __do_generic_request(struct ceph_mon_client *monc, u64 tid,
				struct ceph_mon_generic_request *req)
521 522 523 524
{
	int err;

	/* register request */
525
	req->tid = tid != 0 ? tid : ++monc->last_tid;
526 527 528
	req->request->hdr.tid = cpu_to_le64(req->tid);
	__insert_generic_request(monc, req);
	monc->num_generic_requests++;
529
	ceph_con_send(&monc->con, ceph_msg_get(req->request));
530 531 532 533 534 535 536 537 538 539 540 541 542
	mutex_unlock(&monc->mutex);

	err = wait_for_completion_interruptible(&req->completion);

	mutex_lock(&monc->mutex);
	rb_erase(&req->node, &monc->generic_request_tree);
	monc->num_generic_requests--;

	if (!err)
		err = req->result;
	return err;
}

543 544 545 546 547 548 549 550 551 552 553 554
static int do_generic_request(struct ceph_mon_client *monc,
			      struct ceph_mon_generic_request *req)
{
	int err;

	mutex_lock(&monc->mutex);
	err = __do_generic_request(monc, 0, req);
	mutex_unlock(&monc->mutex);

	return err;
}

555 556 557
/*
 * statfs
 */
S
Sage Weil 已提交
558 559 560
static void handle_statfs_reply(struct ceph_mon_client *monc,
				struct ceph_msg *msg)
{
561
	struct ceph_mon_generic_request *req;
S
Sage Weil 已提交
562
	struct ceph_mon_statfs_reply *reply = msg->front.iov_base;
S
Sage Weil 已提交
563
	u64 tid = le64_to_cpu(msg->hdr.tid);
S
Sage Weil 已提交
564 565 566 567 568 569

	if (msg->front.iov_len != sizeof(*reply))
		goto bad;
	dout("handle_statfs_reply %p tid %llu\n", msg, tid);

	mutex_lock(&monc->mutex);
570
	req = __lookup_generic_req(monc, tid);
S
Sage Weil 已提交
571
	if (req) {
572
		*(struct ceph_statfs *)req->buf = reply->st;
S
Sage Weil 已提交
573
		req->result = 0;
574
		get_generic_request(req);
S
Sage Weil 已提交
575 576
	}
	mutex_unlock(&monc->mutex);
S
Sage Weil 已提交
577
	if (req) {
578
		complete_all(&req->completion);
579
		put_generic_request(req);
S
Sage Weil 已提交
580
	}
S
Sage Weil 已提交
581 582 583
	return;

bad:
584
	pr_err("corrupt statfs reply, tid %llu\n", tid);
585
	ceph_msg_dump(msg);
S
Sage Weil 已提交
586 587 588
}

/*
S
Sage Weil 已提交
589
 * Do a synchronous statfs().
S
Sage Weil 已提交
590
 */
S
Sage Weil 已提交
591
int ceph_monc_do_statfs(struct ceph_mon_client *monc, struct ceph_statfs *buf)
S
Sage Weil 已提交
592
{
593
	struct ceph_mon_generic_request *req;
S
Sage Weil 已提交
594
	struct ceph_mon_statfs *h;
S
Sage Weil 已提交
595 596
	int err;

J
Julia Lawall 已提交
597
	req = kzalloc(sizeof(*req), GFP_NOFS);
S
Sage Weil 已提交
598 599 600 601 602 603
	if (!req)
		return -ENOMEM;

	kref_init(&req->kref);
	req->buf = buf;
	init_completion(&req->completion);
S
Sage Weil 已提交
604

605
	err = -ENOMEM;
606 607
	req->request = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), GFP_NOFS,
				    true);
608
	if (!req->request)
S
Sage Weil 已提交
609
		goto out;
610 611
	req->reply = ceph_msg_new(CEPH_MSG_STATFS_REPLY, 1024, GFP_NOFS,
				  true);
612
	if (!req->reply)
S
Sage Weil 已提交
613 614 615 616
		goto out;

	/* fill out request */
	h = req->request->front.iov_base;
617 618 619
	h->monhdr.have_version = 0;
	h->monhdr.session_mon = cpu_to_le16(-1);
	h->monhdr.session_mon_tid = 0;
S
Sage Weil 已提交
620 621
	h->fsid = monc->monmap->fsid;

622
	err = do_generic_request(monc, req);
S
Sage Weil 已提交
623

624
out:
625
	put_generic_request(req);
626 627
	return err;
}
628
EXPORT_SYMBOL(ceph_monc_do_statfs);
629

630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
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;

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

	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);
	req = __lookup_generic_req(monc, handle);
	if (req) {
		*(u64 *)req->buf = ceph_decode_64(&p);
		req->result = 0;
		get_generic_request(req);
	}
	mutex_unlock(&monc->mutex);
	if (req) {
		complete_all(&req->completion);
		put_generic_request(req);
	}

	return;
bad:
661
	pr_err("corrupt mon_get_version reply, tid %llu\n", tid);
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
	ceph_msg_dump(msg);
}

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

	req = kzalloc(sizeof(*req), GFP_NOFS);
	if (!req)
		return -ENOMEM;

	kref_init(&req->kref);
	req->buf = newest;
	init_completion(&req->completion);

	req->request = ceph_msg_new(CEPH_MSG_MON_GET_VERSION,
				    sizeof(u64) + sizeof(u32) + strlen(what),
				    GFP_NOFS, true);
	if (!req->request) {
		err = -ENOMEM;
		goto out;
	}

	req->reply = ceph_msg_new(CEPH_MSG_MON_GET_VERSION_REPLY, 1024,
				  GFP_NOFS, true);
	if (!req->reply) {
		err = -ENOMEM;
		goto out;
	}

	p = req->request->front.iov_base;
	end = p + req->request->front_alloc_len;

	/* fill out request */
	mutex_lock(&monc->mutex);
	tid = ++monc->last_tid;
	ceph_encode_64(&p, tid); /* handle */
	ceph_encode_string(&p, end, what, strlen(what));

	err = __do_generic_request(monc, tid, req);

	mutex_unlock(&monc->mutex);
out:
714
	put_generic_request(req);
715 716 717 718
	return err;
}
EXPORT_SYMBOL(ceph_monc_do_get_version);

S
Sage Weil 已提交
719
/*
720
 * Resend pending generic requests.
S
Sage Weil 已提交
721
 */
722
static void __resend_generic_request(struct ceph_mon_client *monc)
S
Sage Weil 已提交
723
{
724
	struct ceph_mon_generic_request *req;
725
	struct rb_node *p;
S
Sage Weil 已提交
726

727 728
	for (p = rb_first(&monc->generic_request_tree); p; p = rb_next(p)) {
		req = rb_entry(p, struct ceph_mon_generic_request, node);
729
		ceph_msg_revoke(req->request);
730
		ceph_msg_revoke_incoming(req->reply);
731
		ceph_con_send(&monc->con, ceph_msg_get(req->request));
S
Sage Weil 已提交
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
	}
}

/*
 * 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);
747 748 749
	if (monc->hunting) {
		__close_session(monc);
		__open_session(monc);  /* continue hunting */
S
Sage Weil 已提交
750
	} else {
751 752 753 754 755 756 757 758 759 760
		struct ceph_options *opt = monc->client->options;
		int is_auth = ceph_auth_is_authenticated(monc->auth);
		if (ceph_con_keepalive_expired(&monc->con,
					       opt->monc_ping_timeout)) {
			dout("monc keepalive timeout\n");
			is_auth = 0;
			__close_session(monc);
			monc->hunting = true;
			__open_session(monc);
		}
761

762 763 764 765
		if (!monc->hunting) {
			ceph_con_keepalive(&monc->con);
			__validate_auth(monc);
		}
766

767
		if (is_auth)
768
			__send_subscribe(monc);
S
Sage Weil 已提交
769 770 771 772 773
	}
	__schedule_delayed(monc);
	mutex_unlock(&monc->mutex);
}

774 775 776 777 778 779
/*
 * 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)
{
780 781 782
	struct ceph_options *opt = monc->client->options;
	struct ceph_entity_addr *mon_addr = opt->mon_addr;
	int num_mon = opt->num_mon;
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
	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;
	return 0;
}

S
Sage Weil 已提交
802 803 804 805 806 807 808 809 810 811
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);

812 813 814 815
	err = build_initial_monmap(monc);
	if (err)
		goto out;

816
	/* connection */
817
	/* authentication */
818
	monc->auth = ceph_auth_init(cl->options->name,
819
				    cl->options->key);
820 821
	if (IS_ERR(monc->auth)) {
		err = PTR_ERR(monc->auth);
822
		goto out_monmap;
823
	}
824 825 826 827
	monc->auth->want_keys =
		CEPH_ENTITY_TYPE_AUTH | CEPH_ENTITY_TYPE_MON |
		CEPH_ENTITY_TYPE_OSD | CEPH_ENTITY_TYPE_MDS;

828
	/* msgs */
829
	err = -ENOMEM;
830
	monc->m_subscribe_ack = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE_ACK,
831
				     sizeof(struct ceph_mon_subscribe_ack),
832
				     GFP_NOFS, true);
833
	if (!monc->m_subscribe_ack)
834
		goto out_auth;
835

836 837
	monc->m_subscribe = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 96, GFP_NOFS,
					 true);
838 839 840
	if (!monc->m_subscribe)
		goto out_subscribe_ack;

841 842
	monc->m_auth_reply = ceph_msg_new(CEPH_MSG_AUTH_REPLY, 4096, GFP_NOFS,
					  true);
843
	if (!monc->m_auth_reply)
844
		goto out_subscribe;
845

846
	monc->m_auth = ceph_msg_new(CEPH_MSG_AUTH, 4096, GFP_NOFS, true);
847
	monc->pending_auth = 0;
848
	if (!monc->m_auth)
849
		goto out_auth_reply;
S
Sage Weil 已提交
850

851 852 853
	ceph_con_init(&monc->con, monc, &mon_con_ops,
		      &monc->client->msgr);

S
Sage Weil 已提交
854
	monc->cur_mon = -1;
855
	monc->hunting = true;
S
Sage Weil 已提交
856 857 858 859
	monc->sub_renew_after = jiffies;
	monc->sub_sent = 0;

	INIT_DELAYED_WORK(&monc->delayed_work, delayed_work);
860 861
	monc->generic_request_tree = RB_ROOT;
	monc->num_generic_requests = 0;
S
Sage Weil 已提交
862 863 864 865 866
	monc->last_tid = 0;

	monc->have_mdsmap = 0;
	monc->have_osdmap = 0;
	monc->want_next_osdmap = 1;
867 868
	return 0;

869 870
out_auth_reply:
	ceph_msg_put(monc->m_auth_reply);
871 872
out_subscribe:
	ceph_msg_put(monc->m_subscribe);
873 874
out_subscribe_ack:
	ceph_msg_put(monc->m_subscribe_ack);
875 876
out_auth:
	ceph_auth_destroy(monc->auth);
877 878
out_monmap:
	kfree(monc->monmap);
S
Sage Weil 已提交
879 880 881
out:
	return err;
}
882
EXPORT_SYMBOL(ceph_monc_init);
S
Sage Weil 已提交
883 884 885 886 887 888 889 890

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);
891

S
Sage Weil 已提交
892 893
	mutex_unlock(&monc->mutex);

894 895 896 897 898 899 900 901
	/*
	 * 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();

902 903 904
	ceph_auth_destroy(monc->auth);

	ceph_msg_put(monc->m_auth);
905
	ceph_msg_put(monc->m_auth_reply);
906
	ceph_msg_put(monc->m_subscribe);
907
	ceph_msg_put(monc->m_subscribe_ack);
S
Sage Weil 已提交
908 909 910

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

913 914 915 916
static void handle_auth_reply(struct ceph_mon_client *monc,
			      struct ceph_msg *msg)
{
	int ret;
917
	int was_auth = 0;
918
	int had_debugfs_info, init_debugfs = 0;
919 920

	mutex_lock(&monc->mutex);
921
	had_debugfs_info = have_debugfs_info(monc);
922
	was_auth = ceph_auth_is_authenticated(monc->auth);
923
	monc->pending_auth = 0;
924 925 926
	ret = ceph_handle_auth_reply(monc->auth, msg->front.iov_base,
				     msg->front.iov_len,
				     monc->m_auth->front.iov_base,
927
				     monc->m_auth->front_alloc_len);
928
	if (ret < 0) {
929
		monc->client->auth_err = ret;
930
		wake_up_all(&monc->client->auth_wq);
931
	} else if (ret > 0) {
932
		__send_prepared_auth_request(monc, ret);
933
	} else if (!was_auth && ceph_auth_is_authenticated(monc->auth)) {
934
		dout("authenticated, starting session\n");
935

936 937
		monc->client->msgr.inst.name.type = CEPH_ENTITY_TYPE_CLIENT;
		monc->client->msgr.inst.name.num =
Y
Yehuda Sadeh 已提交
938
					cpu_to_le64(monc->auth->global_id);
939

940
		__send_subscribe(monc);
941
		__resend_generic_request(monc);
942
	}
943 944 945 946 947 948 949

	if (!had_debugfs_info && have_debugfs_info(monc)) {
		pr_info("client%lld fsid %pU\n",
			ceph_client_id(monc->client),
			&monc->client->fsid);
		init_debugfs = 1;
	}
950
	mutex_unlock(&monc->mutex);
951 952 953 954 955 956 957 958

	if (init_debugfs) {
		/*
		 * do debugfs initialization without mutex to avoid
		 * creating a locking dependency
		 */
		ceph_debugfs_client_init(monc->client);
	}
959 960
}

961 962 963 964 965 966 967 968
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,
969
			      monc->m_auth->front_alloc_len);
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
	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;
}
985
EXPORT_SYMBOL(ceph_monc_validate_auth);
986

S
Sage Weil 已提交
987 988 989 990 991 992 993 994 995 996 997 998
/*
 * 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) {
999 1000
	case CEPH_MSG_AUTH_REPLY:
		handle_auth_reply(monc, msg);
S
Sage Weil 已提交
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
		break;

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

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

1011 1012 1013 1014
	case CEPH_MSG_MON_GET_VERSION_REPLY:
		handle_get_version_reply(monc, msg);
		break;

1015 1016 1017 1018
	case CEPH_MSG_MON_MAP:
		ceph_monc_handle_map(monc, msg);
		break;

S
Sage Weil 已提交
1019 1020 1021 1022 1023
	case CEPH_MSG_OSD_MAP:
		ceph_osdc_handle_map(&monc->client->osdc, msg);
		break;

	default:
1024 1025 1026 1027 1028
		/* can the chained handler handle it? */
		if (monc->client->extra_mon_dispatch &&
		    monc->client->extra_mon_dispatch(monc->client, msg) == 0)
			break;
			
S
Sage Weil 已提交
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
		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,
1039 1040
				      struct ceph_msg_header *hdr,
				      int *skip)
S
Sage Weil 已提交
1041 1042 1043
{
	struct ceph_mon_client *monc = con->private;
	int type = le16_to_cpu(hdr->type);
1044
	int front_len = le32_to_cpu(hdr->front_len);
1045
	struct ceph_msg *m = NULL;
S
Sage Weil 已提交
1046

1047
	*skip = 0;
1048

S
Sage Weil 已提交
1049 1050
	switch (type) {
	case CEPH_MSG_MON_SUBSCRIBE_ACK:
1051
		m = ceph_msg_get(monc->m_subscribe_ack);
1052
		break;
S
Sage Weil 已提交
1053
	case CEPH_MSG_STATFS_REPLY:
1054
		return get_generic_reply(con, hdr, skip);
1055
	case CEPH_MSG_AUTH_REPLY:
1056
		m = ceph_msg_get(monc->m_auth_reply);
1057
		break;
1058 1059 1060 1061 1062 1063 1064 1065 1066
	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
		 * request had a non-zero tid.  Workaround this weirdness
		 * by falling through to the allocate case.
		 */
1067 1068 1069
	case CEPH_MSG_MON_MAP:
	case CEPH_MSG_MDS_MAP:
	case CEPH_MSG_OSD_MAP:
1070
		m = ceph_msg_new(type, front_len, GFP_NOFS, false);
A
Alex Elder 已提交
1071 1072
		if (!m)
			return NULL;	/* ENOMEM--return skip == 0 */
1073
		break;
S
Sage Weil 已提交
1074
	}
1075

1076 1077
	if (!m) {
		pr_info("alloc_msg unknown type %d\n", type);
1078
		*skip = 1;
1079
	} else if (front_len > m->front_alloc_len) {
1080 1081 1082 1083
		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));
1084 1085
		ceph_msg_put(m);
		m = ceph_msg_new(type, front_len, GFP_NOFS, false);
1086
	}
1087

1088
	return m;
S
Sage Weil 已提交
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
}

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

1107
	if (!monc->hunting)
S
Sage Weil 已提交
1108 1109
		pr_info("mon%d %s session lost, "
			"hunting for new mon\n", monc->cur_mon,
1110
			ceph_pr_addr(&monc->con.peer_addr.in_addr));
S
Sage Weil 已提交
1111 1112 1113 1114 1115

	__close_session(monc);
	if (!monc->hunting) {
		/* start hunting */
		monc->hunting = true;
1116
		__open_session(monc);
S
Sage Weil 已提交
1117 1118 1119 1120 1121 1122 1123 1124
	} else {
		/* already hunting, let's wait a bit */
		__schedule_delayed(monc);
	}
out:
	mutex_unlock(&monc->mutex);
}

1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
/*
 * 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)
{
}

1139
static const struct ceph_connection_operations mon_con_ops = {
1140 1141
	.get = con_get,
	.put = con_put,
S
Sage Weil 已提交
1142 1143 1144 1145
	.dispatch = dispatch,
	.fault = mon_fault,
	.alloc_msg = mon_alloc_msg,
};