mon_client.c 24.8 KB
Newer Older
1
#include <linux/ceph/ceph_debug.h>
Sage Weil's avatar
Sage Weil committed
2

3
#include <linux/module.h>
Sage Weil's avatar
Sage Weil committed
4
#include <linux/types.h>
5
#include <linux/slab.h>
Sage Weil's avatar
Sage Weil committed
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>
Sage Weil's avatar
Sage Weil committed
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;
Sage Weil's avatar
Sage Weil committed
34

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

Sage Weil's avatar
Sage Weil committed
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);
Sage Weil's avatar
Sage Weil committed
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);
Sage Weil's avatar
Sage Weil committed
59

60
	num_mon = ceph_decode_32(&p);
Sage Weil's avatar
Sage Weil committed
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);
Sage Weil's avatar
Sage Weil committed
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));
Sage Weil's avatar
Sage Weil committed
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++)
Sage Weil's avatar
Sage Weil committed
96
		if (memcmp(addr, &m->mon_inst[i].addr, sizeof(*addr)) == 0)
Sage Weil's avatar
Sage Weil committed
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
}

Sage Weil's avatar
Sage Weil committed
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
	ceph_con_close(&monc->con);
122
	monc->con.private = NULL;
123 124 125
	monc->cur_mon = -1;
	monc->pending_auth = 0;
	ceph_auth_reset(monc->auth);
Sage Weil's avatar
Sage Weil committed
126 127 128 129 130 131 132 133
}

/*
 * Open a session with a (new) monitor.
 */
static int __open_session(struct ceph_mon_client *monc)
{
	char r;
134
	int ret;
Sage Weil's avatar
Sage Weil committed
135 136 137 138 139 140 141 142 143 144

	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;

145 146 147
		ceph_con_init(&monc->con, monc, &mon_con_ops,
			&monc->client->msgr,
			CEPH_ENTITY_TYPE_MON, monc->cur_mon);
148 149

		dout("open_session mon%d opening\n", monc->cur_mon);
150
		ceph_con_open(&monc->con,
Sage Weil's avatar
Sage Weil committed
151
			      &monc->monmap->mon_inst[monc->cur_mon].addr);
152 153 154 155 156

		/* initiatiate authentication handshake */
		ret = ceph_auth_build_hello(monc->auth,
					    monc->m_auth->front.iov_base,
					    monc->m_auth->front_max);
157
		__send_prepared_auth_request(monc, ret);
Sage Weil's avatar
Sage Weil committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	} 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;

176
	if (monc->cur_mon < 0 || __sub_expired(monc))
Sage Weil's avatar
Sage Weil committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
		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) {
194
		struct ceph_msg *msg = monc->m_subscribe;
Sage Weil's avatar
Sage Weil committed
195 196
		struct ceph_mon_subscribe_item *i;
		void *p, *end;
197
		int num;
Sage Weil's avatar
Sage Weil committed
198 199

		p = msg->front.iov_base;
200
		end = p + msg->front_max;
Sage Weil's avatar
Sage Weil committed
201

202 203 204
		num = 1 + !!monc->want_next_osdmap + !!monc->want_mdsmap;
		ceph_encode_32(&p, num);

Sage Weil's avatar
Sage Weil committed
205 206 207 208 209 210 211 212 213 214
		if (monc->want_next_osdmap) {
			dout("__send_subscribe to 'osdmap' %u\n",
			     (unsigned)monc->have_osdmap);
			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 */
		}
215 216 217 218 219 220 221 222 223
		if (monc->want_mdsmap) {
			dout("__send_subscribe to 'mdsmap' %u+\n",
			     (unsigned)monc->have_mdsmap);
			ceph_encode_string(&p, end, "mdsmap", 6);
			i = p;
			i->have = cpu_to_le64(monc->have_mdsmap);
			i->onetime = 0;
			p += sizeof(*i);
		}
224 225 226 227 228
		ceph_encode_string(&p, end, "monmap", 6);
		i = p;
		i->have = 0;
		i->onetime = 0;
		p += sizeof(*i);
Sage Weil's avatar
Sage Weil committed
229 230 231

		msg->front.iov_len = p - msg->front.iov_base;
		msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
232
		ceph_msg_revoke(msg);
233
		ceph_con_send(&monc->con, ceph_msg_get(msg));
Sage Weil's avatar
Sage Weil committed
234 235 236 237 238 239 240 241 242

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

static void handle_subscribe_ack(struct ceph_mon_client *monc,
				 struct ceph_msg *msg)
{
	unsigned seconds;
243 244 245 246 247
	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);
Sage Weil's avatar
Sage Weil committed
248 249 250 251

	mutex_lock(&monc->mutex);
	if (monc->hunting) {
		pr_info("mon%d %s session established\n",
252
			monc->cur_mon,
253
			ceph_pr_addr(&monc->con.peer_addr.in_addr));
Sage Weil's avatar
Sage Weil committed
254 255 256
		monc->hunting = false;
	}
	dout("handle_subscribe_ack after %d seconds\n", seconds);
257
	monc->sub_renew_after = monc->sub_sent + (seconds >> 1)*HZ - 1;
Sage Weil's avatar
Sage Weil committed
258 259 260 261 262
	monc->sub_sent = 0;
	mutex_unlock(&monc->mutex);
	return;
bad:
	pr_err("got corrupt subscribe-ack msg\n");
263
	ceph_msg_dump(msg);
Sage Weil's avatar
Sage Weil committed
264 265 266 267 268 269 270 271 272 273 274 275
}

/*
 * 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;
}
276
EXPORT_SYMBOL(ceph_monc_got_mdsmap);
Sage Weil's avatar
Sage Weil committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

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

301
/*
Sage Weil's avatar
Sage Weil committed
302
 *
303 304
 */
int ceph_monc_open_session(struct ceph_mon_client *monc)
Sage Weil's avatar
Sage Weil committed
305 306
{
	mutex_lock(&monc->mutex);
307
	__open_session(monc);
Sage Weil's avatar
Sage Weil committed
308 309 310 311
	__schedule_delayed(monc);
	mutex_unlock(&monc->mutex);
	return 0;
}
312
EXPORT_SYMBOL(ceph_monc_open_session);
Sage Weil's avatar
Sage Weil committed
313

314 315 316 317
/*
 * The monitor responds with mount ack indicate mount success.  The
 * included client ticket allows the client to talk to MDSs and OSDs.
 */
318 319
static void ceph_monc_handle_map(struct ceph_mon_client *monc,
				 struct ceph_msg *msg)
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
{
	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));
Sage Weil's avatar
Sage Weil committed
335
		goto out;
336
	}
337 338

	if (ceph_check_fsid(monc->client, &monmap->fsid) < 0) {
339
		kfree(monmap);
Sage Weil's avatar
Sage Weil committed
340
		goto out;
341 342 343 344 345
	}

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

346 347 348 349 350 351 352 353 354 355
	if (!client->have_fsid) {
		client->have_fsid = true;
		mutex_unlock(&monc->mutex);
		/*
		 * do debugfs initialization without mutex to avoid
		 * creating a locking dependency
		 */
		ceph_debugfs_client_init(client);
		goto out_unlocked;
	}
Sage Weil's avatar
Sage Weil committed
356
out:
357
	mutex_unlock(&monc->mutex);
358
out_unlocked:
359
	wake_up_all(&client->auth_wq);
360 361
}

Sage Weil's avatar
Sage Weil committed
362
/*
363
 * generic requests (e.g., statfs, poolop)
Sage Weil's avatar
Sage Weil committed
364
 */
365
static struct ceph_mon_generic_request *__lookup_generic_req(
366 367
	struct ceph_mon_client *monc, u64 tid)
{
368 369
	struct ceph_mon_generic_request *req;
	struct rb_node *n = monc->generic_request_tree.rb_node;
370 371

	while (n) {
372
		req = rb_entry(n, struct ceph_mon_generic_request, node);
373 374 375 376 377 378 379 380 381 382
		if (tid < req->tid)
			n = n->rb_left;
		else if (tid > req->tid)
			n = n->rb_right;
		else
			return req;
	}
	return NULL;
}

383 384
static void __insert_generic_request(struct ceph_mon_client *monc,
			    struct ceph_mon_generic_request *new)
385
{
386
	struct rb_node **p = &monc->generic_request_tree.rb_node;
387
	struct rb_node *parent = NULL;
388
	struct ceph_mon_generic_request *req = NULL;
389 390 391

	while (*p) {
		parent = *p;
392
		req = rb_entry(parent, struct ceph_mon_generic_request, node);
393 394 395 396 397 398 399 400 401
		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);
402
	rb_insert_color(&new->node, &monc->generic_request_tree);
403 404
}

405
static void release_generic_request(struct kref *kref)
Sage Weil's avatar
Sage Weil committed
406
{
407 408
	struct ceph_mon_generic_request *req =
		container_of(kref, struct ceph_mon_generic_request, kref);
Sage Weil's avatar
Sage Weil committed
409 410 411 412 413

	if (req->reply)
		ceph_msg_put(req->reply);
	if (req->request)
		ceph_msg_put(req->request);
414 415

	kfree(req);
Sage Weil's avatar
Sage Weil committed
416 417
}

418
static void put_generic_request(struct ceph_mon_generic_request *req)
Sage Weil's avatar
Sage Weil committed
419
{
420
	kref_put(&req->kref, release_generic_request);
Sage Weil's avatar
Sage Weil committed
421 422
}

423
static void get_generic_request(struct ceph_mon_generic_request *req)
Sage Weil's avatar
Sage Weil committed
424 425 426 427
{
	kref_get(&req->kref);
}

428
static struct ceph_msg *get_generic_reply(struct ceph_connection *con,
Sage Weil's avatar
Sage Weil committed
429 430 431 432
					 struct ceph_msg_header *hdr,
					 int *skip)
{
	struct ceph_mon_client *monc = con->private;
433
	struct ceph_mon_generic_request *req;
Sage Weil's avatar
Sage Weil committed
434 435 436 437
	u64 tid = le64_to_cpu(hdr->tid);
	struct ceph_msg *m;

	mutex_lock(&monc->mutex);
438
	req = __lookup_generic_req(monc, tid);
Sage Weil's avatar
Sage Weil committed
439
	if (!req) {
440
		dout("get_generic_reply %lld dne\n", tid);
Sage Weil's avatar
Sage Weil committed
441 442 443
		*skip = 1;
		m = NULL;
	} else {
444
		dout("get_generic_reply %lld got %p\n", tid, req->reply);
Alex Elder's avatar
Alex Elder committed
445
		*skip = 0;
Sage Weil's avatar
Sage Weil committed
446 447 448 449 450 451 452 453 454 455 456
		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;
}

457 458 459 460 461 462 463 464 465 466 467
static int do_generic_request(struct ceph_mon_client *monc,
			      struct ceph_mon_generic_request *req)
{
	int err;

	/* register request */
	mutex_lock(&monc->mutex);
	req->tid = ++monc->last_tid;
	req->request->hdr.tid = cpu_to_le64(req->tid);
	__insert_generic_request(monc, req);
	monc->num_generic_requests++;
468
	ceph_con_send(&monc->con, ceph_msg_get(req->request));
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
	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--;
	mutex_unlock(&monc->mutex);

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

/*
 * statfs
 */
Sage Weil's avatar
Sage Weil committed
486 487 488
static void handle_statfs_reply(struct ceph_mon_client *monc,
				struct ceph_msg *msg)
{
489
	struct ceph_mon_generic_request *req;
Sage Weil's avatar
Sage Weil committed
490
	struct ceph_mon_statfs_reply *reply = msg->front.iov_base;
Sage Weil's avatar
Sage Weil committed
491
	u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil's avatar
Sage Weil committed
492 493 494 495 496 497

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

	mutex_lock(&monc->mutex);
498
	req = __lookup_generic_req(monc, tid);
Sage Weil's avatar
Sage Weil committed
499
	if (req) {
500
		*(struct ceph_statfs *)req->buf = reply->st;
Sage Weil's avatar
Sage Weil committed
501
		req->result = 0;
502
		get_generic_request(req);
Sage Weil's avatar
Sage Weil committed
503 504
	}
	mutex_unlock(&monc->mutex);
Sage Weil's avatar
Sage Weil committed
505
	if (req) {
506
		complete_all(&req->completion);
507
		put_generic_request(req);
Sage Weil's avatar
Sage Weil committed
508
	}
Sage Weil's avatar
Sage Weil committed
509 510 511
	return;

bad:
512
	pr_err("corrupt generic reply, tid %llu\n", tid);
513
	ceph_msg_dump(msg);
Sage Weil's avatar
Sage Weil committed
514 515 516
}

/*
Sage Weil's avatar
Sage Weil committed
517
 * Do a synchronous statfs().
Sage Weil's avatar
Sage Weil committed
518
 */
Sage Weil's avatar
Sage Weil committed
519
int ceph_monc_do_statfs(struct ceph_mon_client *monc, struct ceph_statfs *buf)
Sage Weil's avatar
Sage Weil committed
520
{
521
	struct ceph_mon_generic_request *req;
Sage Weil's avatar
Sage Weil committed
522
	struct ceph_mon_statfs *h;
Sage Weil's avatar
Sage Weil committed
523 524
	int err;

Julia Lawall's avatar
Julia Lawall committed
525
	req = kzalloc(sizeof(*req), GFP_NOFS);
Sage Weil's avatar
Sage Weil committed
526 527 528 529 530
	if (!req)
		return -ENOMEM;

	kref_init(&req->kref);
	req->buf = buf;
531
	req->buf_len = sizeof(*buf);
Sage Weil's avatar
Sage Weil committed
532
	init_completion(&req->completion);
Sage Weil's avatar
Sage Weil committed
533

534
	err = -ENOMEM;
535 536
	req->request = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), GFP_NOFS,
				    true);
537
	if (!req->request)
Sage Weil's avatar
Sage Weil committed
538
		goto out;
539 540
	req->reply = ceph_msg_new(CEPH_MSG_STATFS_REPLY, 1024, GFP_NOFS,
				  true);
541
	if (!req->reply)
Sage Weil's avatar
Sage Weil committed
542 543 544 545
		goto out;

	/* fill out request */
	h = req->request->front.iov_base;
546 547 548
	h->monhdr.have_version = 0;
	h->monhdr.session_mon = cpu_to_le16(-1);
	h->monhdr.session_mon_tid = 0;
Sage Weil's avatar
Sage Weil committed
549 550
	h->fsid = monc->monmap->fsid;

551
	err = do_generic_request(monc, req);
Sage Weil's avatar
Sage Weil committed
552

553 554 555 556
out:
	kref_put(&req->kref, release_generic_request);
	return err;
}
557
EXPORT_SYMBOL(ceph_monc_do_statfs);
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

/*
 * pool ops
 */
static int get_poolop_reply_buf(const char *src, size_t src_len,
				char *dst, size_t dst_len)
{
	u32 buf_len;

	if (src_len != sizeof(u32) + dst_len)
		return -EINVAL;

	buf_len = le32_to_cpu(*(u32 *)src);
	if (buf_len != dst_len)
		return -EINVAL;

	memcpy(dst, src + sizeof(u32), dst_len);
	return 0;
}

static void handle_poolop_reply(struct ceph_mon_client *monc,
				struct ceph_msg *msg)
{
	struct ceph_mon_generic_request *req;
	struct ceph_mon_poolop_reply *reply = msg->front.iov_base;
	u64 tid = le64_to_cpu(msg->hdr.tid);

	if (msg->front.iov_len < sizeof(*reply))
		goto bad;
	dout("handle_poolop_reply %p tid %llu\n", msg, tid);
Sage Weil's avatar
Sage Weil committed
588 589

	mutex_lock(&monc->mutex);
590 591 592 593 594 595 596 597 598 599 600 601
	req = __lookup_generic_req(monc, tid);
	if (req) {
		if (req->buf_len &&
		    get_poolop_reply_buf(msg->front.iov_base + sizeof(*reply),
				     msg->front.iov_len - sizeof(*reply),
				     req->buf, req->buf_len) < 0) {
			mutex_unlock(&monc->mutex);
			goto bad;
		}
		req->result = le32_to_cpu(reply->reply_code);
		get_generic_request(req);
	}
Sage Weil's avatar
Sage Weil committed
602
	mutex_unlock(&monc->mutex);
603 604 605 606 607
	if (req) {
		complete(&req->completion);
		put_generic_request(req);
	}
	return;
Sage Weil's avatar
Sage Weil committed
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 633 634
bad:
	pr_err("corrupt generic reply, tid %llu\n", tid);
	ceph_msg_dump(msg);
}

/*
 * Do a synchronous pool op.
 */
int ceph_monc_do_poolop(struct ceph_mon_client *monc, u32 op,
			u32 pool, u64 snapid,
			char *buf, int len)
{
	struct ceph_mon_generic_request *req;
	struct ceph_mon_poolop *h;
	int err;

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

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

	err = -ENOMEM;
635 636
	req->request = ceph_msg_new(CEPH_MSG_POOLOP, sizeof(*h), GFP_NOFS,
				    true);
637 638
	if (!req->request)
		goto out;
639 640
	req->reply = ceph_msg_new(CEPH_MSG_POOLOP_REPLY, 1024, GFP_NOFS,
				  true);
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
	if (!req->reply)
		goto out;

	/* fill out request */
	req->request->hdr.version = cpu_to_le16(2);
	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->pool = cpu_to_le32(pool);
	h->op = cpu_to_le32(op);
	h->auid = 0;
	h->snapid = cpu_to_le64(snapid);
	h->name_len = 0;

	err = do_generic_request(monc, req);
Sage Weil's avatar
Sage Weil committed
658 659

out:
660
	kref_put(&req->kref, release_generic_request);
Sage Weil's avatar
Sage Weil committed
661 662 663
	return err;
}

664 665 666 667 668 669 670
int ceph_monc_create_snapid(struct ceph_mon_client *monc,
			    u32 pool, u64 *snapid)
{
	return ceph_monc_do_poolop(monc,  POOL_OP_CREATE_UNMANAGED_SNAP,
				   pool, 0, (char *)snapid, sizeof(*snapid));

}
671
EXPORT_SYMBOL(ceph_monc_create_snapid);
672 673 674 675 676 677 678 679 680

int ceph_monc_delete_snapid(struct ceph_mon_client *monc,
			    u32 pool, u64 snapid)
{
	return ceph_monc_do_poolop(monc,  POOL_OP_CREATE_UNMANAGED_SNAP,
				   pool, snapid, 0, 0);

}

Sage Weil's avatar
Sage Weil committed
681
/*
682
 * Resend pending generic requests.
Sage Weil's avatar
Sage Weil committed
683
 */
684
static void __resend_generic_request(struct ceph_mon_client *monc)
Sage Weil's avatar
Sage Weil committed
685
{
686
	struct ceph_mon_generic_request *req;
687
	struct rb_node *p;
Sage Weil's avatar
Sage Weil committed
688

689 690
	for (p = rb_first(&monc->generic_request_tree); p; p = rb_next(p)) {
		req = rb_entry(p, struct ceph_mon_generic_request, node);
691
		ceph_msg_revoke(req->request);
692
		ceph_con_send(&monc->con, ceph_msg_get(req->request));
Sage Weil's avatar
Sage Weil committed
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
	}
}

/*
 * 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);
708 709 710
	if (monc->hunting) {
		__close_session(monc);
		__open_session(monc);  /* continue hunting */
Sage Weil's avatar
Sage Weil committed
711
	} else {
712
		ceph_con_keepalive(&monc->con);
713 714 715

		__validate_auth(monc);

716 717
		if (monc->auth->ops->is_authenticated(monc->auth))
			__send_subscribe(monc);
Sage Weil's avatar
Sage Weil committed
718 719 720 721 722
	}
	__schedule_delayed(monc);
	mutex_unlock(&monc->mutex);
}

723 724 725 726 727 728
/*
 * 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)
{
729 730 731
	struct ceph_options *opt = monc->client->options;
	struct ceph_entity_addr *mon_addr = opt->mon_addr;
	int num_mon = opt->num_mon;
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
	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;
748
	monc->have_fsid = false;
749 750 751
	return 0;
}

Sage Weil's avatar
Sage Weil committed
752 753 754 755 756 757 758 759 760 761
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);

762 763 764 765
	err = build_initial_monmap(monc);
	if (err)
		goto out;

766
	/* connection */
767
	/* authentication */
768
	monc->auth = ceph_auth_init(cl->options->name,
769
				    cl->options->key);
770 771
	if (IS_ERR(monc->auth)) {
		err = PTR_ERR(monc->auth);
772
		goto out_monmap;
773
	}
774 775 776 777
	monc->auth->want_keys =
		CEPH_ENTITY_TYPE_AUTH | CEPH_ENTITY_TYPE_MON |
		CEPH_ENTITY_TYPE_OSD | CEPH_ENTITY_TYPE_MDS;

778
	/* msgs */
779
	err = -ENOMEM;
780
	monc->m_subscribe_ack = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE_ACK,
781
				     sizeof(struct ceph_mon_subscribe_ack),
782
				     GFP_NOFS, true);
783
	if (!monc->m_subscribe_ack)
784
		goto out_auth;
785

786 787
	monc->m_subscribe = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 96, GFP_NOFS,
					 true);
788 789 790
	if (!monc->m_subscribe)
		goto out_subscribe_ack;

791 792
	monc->m_auth_reply = ceph_msg_new(CEPH_MSG_AUTH_REPLY, 4096, GFP_NOFS,
					  true);
793
	if (!monc->m_auth_reply)
794
		goto out_subscribe;
795

796
	monc->m_auth = ceph_msg_new(CEPH_MSG_AUTH, 4096, GFP_NOFS, true);
797
	monc->pending_auth = 0;
798
	if (!monc->m_auth)
799
		goto out_auth_reply;
Sage Weil's avatar
Sage Weil committed
800 801

	monc->cur_mon = -1;
802
	monc->hunting = true;
Sage Weil's avatar
Sage Weil committed
803 804 805 806
	monc->sub_renew_after = jiffies;
	monc->sub_sent = 0;

	INIT_DELAYED_WORK(&monc->delayed_work, delayed_work);
807 808
	monc->generic_request_tree = RB_ROOT;
	monc->num_generic_requests = 0;
Sage Weil's avatar
Sage Weil committed
809 810 811 812 813
	monc->last_tid = 0;

	monc->have_mdsmap = 0;
	monc->have_osdmap = 0;
	monc->want_next_osdmap = 1;
814 815
	return 0;

816 817
out_auth_reply:
	ceph_msg_put(monc->m_auth_reply);
818 819
out_subscribe:
	ceph_msg_put(monc->m_subscribe);
820 821
out_subscribe_ack:
	ceph_msg_put(monc->m_subscribe_ack);
822 823
out_auth:
	ceph_auth_destroy(monc->auth);
824 825
out_monmap:
	kfree(monc->monmap);
Sage Weil's avatar
Sage Weil committed
826 827 828
out:
	return err;
}
829
EXPORT_SYMBOL(ceph_monc_init);
Sage Weil's avatar
Sage Weil committed
830 831 832 833 834 835 836 837

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

Sage Weil's avatar
Sage Weil committed
839 840
	mutex_unlock(&monc->mutex);

841 842 843
	ceph_auth_destroy(monc->auth);

	ceph_msg_put(monc->m_auth);
844
	ceph_msg_put(monc->m_auth_reply);
845
	ceph_msg_put(monc->m_subscribe);
846
	ceph_msg_put(monc->m_subscribe_ack);
Sage Weil's avatar
Sage Weil committed
847 848 849

	kfree(monc->monmap);
}
850
EXPORT_SYMBOL(ceph_monc_stop);
Sage Weil's avatar
Sage Weil committed
851

852 853 854 855
static void handle_auth_reply(struct ceph_mon_client *monc,
			      struct ceph_msg *msg)
{
	int ret;
856
	int was_auth = 0;
857 858

	mutex_lock(&monc->mutex);
859 860
	if (monc->auth->ops)
		was_auth = monc->auth->ops->is_authenticated(monc->auth);
861
	monc->pending_auth = 0;
862 863 864 865 866
	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) {
867
		monc->client->auth_err = ret;
868
		wake_up_all(&monc->client->auth_wq);
869
	} else if (ret > 0) {
870
		__send_prepared_auth_request(monc, ret);
871
	} else if (!was_auth && monc->auth->ops->is_authenticated(monc->auth)) {
872
		dout("authenticated, starting session\n");
873

874 875
		monc->client->msgr.inst.name.type = CEPH_ENTITY_TYPE_CLIENT;
		monc->client->msgr.inst.name.num =
Yehuda Sadeh's avatar
Yehuda Sadeh committed
876
					cpu_to_le64(monc->auth->global_id);
877

878
		__send_subscribe(monc);
879
		__resend_generic_request(monc);
880 881 882 883
	}
	mutex_unlock(&monc->mutex);
}

884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
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,
			      monc->m_auth->front_max);
	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;
}
908
EXPORT_SYMBOL(ceph_monc_validate_auth);
909

Sage Weil's avatar
Sage Weil committed
910 911 912 913 914 915 916 917 918 919 920 921
/*
 * 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) {
922 923
	case CEPH_MSG_AUTH_REPLY:
		handle_auth_reply(monc, msg);
Sage Weil's avatar
Sage Weil committed
924 925 926 927 928 929 930 931 932 933
		break;

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

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

934 935 936 937
	case CEPH_MSG_POOLOP_REPLY:
		handle_poolop_reply(monc, msg);
		break;

938 939 940 941
	case CEPH_MSG_MON_MAP:
		ceph_monc_handle_map(monc, msg);
		break;

Sage Weil's avatar
Sage Weil committed
942 943 944 945 946
	case CEPH_MSG_OSD_MAP:
		ceph_osdc_handle_map(&monc->client->osdc, msg);
		break;

	default:
947 948 949 950 951
		/* can the chained handler handle it? */
		if (monc->client->extra_mon_dispatch &&
		    monc->client->extra_mon_dispatch(monc->client, msg) == 0)
			break;
			
Sage Weil's avatar
Sage Weil committed
952 953 954 955 956 957 958 959 960 961
		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,
962 963
				      struct ceph_msg_header *hdr,
				      int *skip)
Sage Weil's avatar
Sage Weil committed
964 965 966
{
	struct ceph_mon_client *monc = con->private;
	int type = le16_to_cpu(hdr->type);
967
	int front_len = le32_to_cpu(hdr->front_len);
968
	struct ceph_msg *m = NULL;
Sage Weil's avatar
Sage Weil committed
969

970
	*skip = 0;
971

Sage Weil's avatar
Sage Weil committed
972 973
	switch (type) {
	case CEPH_MSG_MON_SUBSCRIBE_ACK:
974
		m = ceph_msg_get(monc->m_subscribe_ack);
975
		break;
976
	case CEPH_MSG_POOLOP_REPLY:
Sage Weil's avatar
Sage Weil committed
977
	case CEPH_MSG_STATFS_REPLY:
978
		return get_generic_reply(con, hdr, skip);
979
	case CEPH_MSG_AUTH_REPLY:
980
		m = ceph_msg_get(monc->m_auth_reply);
981
		break;
982 983 984
	case CEPH_MSG_MON_MAP:
	case CEPH_MSG_MDS_MAP:
	case CEPH_MSG_OSD_MAP:
985
		m = ceph_msg_new(type, front_len, GFP_NOFS, false);
Alex Elder's avatar
Alex Elder committed
986 987
		if (!m)
			return NULL;	/* ENOMEM--return skip == 0 */
988
		break;
Sage Weil's avatar
Sage Weil committed
989
	}
990

991 992
	if (!m) {
		pr_info("alloc_msg unknown type %d\n", type);
993
		*skip = 1;
994
	}
995
	return m;
Sage Weil's avatar
Sage Weil committed
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
}

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

1014
	if (!monc->hunting)
Sage Weil's avatar
Sage Weil committed
1015 1016
		pr_info("mon%d %s session lost, "
			"hunting for new mon\n", monc->cur_mon,
1017
			ceph_pr_addr(&monc->con.peer_addr.in_addr));
Sage Weil's avatar
Sage Weil committed
1018 1019 1020 1021 1022

	__close_session(monc);
	if (!monc->hunting) {
		/* start hunting */
		monc->hunting = true;
1023
		__open_session(monc);
Sage Weil's avatar
Sage Weil committed
1024 1025 1026 1027 1028 1029 1030 1031
	} else {
		/* already hunting, let's wait a bit */
		__schedule_delayed(monc);
	}
out:
	mutex_unlock(&monc->mutex);
}

1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
/*
 * 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)
{
}

1046
static const struct ceph_connection_operations mon_con_ops = {
1047 1048
	.get = con_get,
	.put = con_put,
Sage Weil's avatar
Sage Weil committed
1049 1050 1051 1052
	.dispatch = dispatch,
	.fault = mon_fault,
	.alloc_msg = mon_alloc_msg,
};