xref: /OK3568_Linux_fs/kernel/net/ceph/mon_client.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include <linux/ceph/ceph_debug.h>
3*4882a593Smuzhiyun 
4*4882a593Smuzhiyun #include <linux/module.h>
5*4882a593Smuzhiyun #include <linux/types.h>
6*4882a593Smuzhiyun #include <linux/slab.h>
7*4882a593Smuzhiyun #include <linux/random.h>
8*4882a593Smuzhiyun #include <linux/sched.h>
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/ceph/ceph_features.h>
11*4882a593Smuzhiyun #include <linux/ceph/mon_client.h>
12*4882a593Smuzhiyun #include <linux/ceph/libceph.h>
13*4882a593Smuzhiyun #include <linux/ceph/debugfs.h>
14*4882a593Smuzhiyun #include <linux/ceph/decode.h>
15*4882a593Smuzhiyun #include <linux/ceph/auth.h>
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun /*
18*4882a593Smuzhiyun  * Interact with Ceph monitor cluster.  Handle requests for new map
19*4882a593Smuzhiyun  * versions, and periodically resend as needed.  Also implement
20*4882a593Smuzhiyun  * statfs() and umount().
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * A small cluster of Ceph "monitors" are responsible for managing critical
23*4882a593Smuzhiyun  * cluster configuration and state information.  An odd number (e.g., 3, 5)
24*4882a593Smuzhiyun  * of cmon daemons use a modified version of the Paxos part-time parliament
25*4882a593Smuzhiyun  * algorithm to manage the MDS map (mds cluster membership), OSD map, and
26*4882a593Smuzhiyun  * list of clients who have mounted the file system.
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * We maintain an open, active session with a monitor at all times in order to
29*4882a593Smuzhiyun  * receive timely MDSMap updates.  We periodically send a keepalive byte on the
30*4882a593Smuzhiyun  * TCP socket to ensure we detect a failure.  If the connection does break, we
31*4882a593Smuzhiyun  * randomly hunt for a new monitor.  Once the connection is reestablished, we
32*4882a593Smuzhiyun  * resend any outstanding requests.
33*4882a593Smuzhiyun  */
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun static const struct ceph_connection_operations mon_con_ops;
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun static int __validate_auth(struct ceph_mon_client *monc);
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun /*
40*4882a593Smuzhiyun  * Decode a monmap blob (e.g., during mount).
41*4882a593Smuzhiyun  */
ceph_monmap_decode(void * p,void * end)42*4882a593Smuzhiyun static struct ceph_monmap *ceph_monmap_decode(void *p, void *end)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun 	struct ceph_monmap *m = NULL;
45*4882a593Smuzhiyun 	int i, err = -EINVAL;
46*4882a593Smuzhiyun 	struct ceph_fsid fsid;
47*4882a593Smuzhiyun 	u32 epoch, num_mon;
48*4882a593Smuzhiyun 	u32 len;
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	ceph_decode_32_safe(&p, end, len, bad);
51*4882a593Smuzhiyun 	ceph_decode_need(&p, end, len, bad);
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	dout("monmap_decode %p %p len %d (%d)\n", p, end, len, (int)(end-p));
54*4882a593Smuzhiyun 	p += sizeof(u16);  /* skip version */
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	ceph_decode_need(&p, end, sizeof(fsid) + 2*sizeof(u32), bad);
57*4882a593Smuzhiyun 	ceph_decode_copy(&p, &fsid, sizeof(fsid));
58*4882a593Smuzhiyun 	epoch = ceph_decode_32(&p);
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	num_mon = ceph_decode_32(&p);
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	if (num_mon > CEPH_MAX_MON)
63*4882a593Smuzhiyun 		goto bad;
64*4882a593Smuzhiyun 	m = kmalloc(struct_size(m, mon_inst, num_mon), GFP_NOFS);
65*4882a593Smuzhiyun 	if (m == NULL)
66*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
67*4882a593Smuzhiyun 	m->fsid = fsid;
68*4882a593Smuzhiyun 	m->epoch = epoch;
69*4882a593Smuzhiyun 	m->num_mon = num_mon;
70*4882a593Smuzhiyun 	for (i = 0; i < num_mon; ++i) {
71*4882a593Smuzhiyun 		struct ceph_entity_inst *inst = &m->mon_inst[i];
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 		/* copy name portion */
74*4882a593Smuzhiyun 		ceph_decode_copy_safe(&p, end, &inst->name,
75*4882a593Smuzhiyun 					sizeof(inst->name), bad);
76*4882a593Smuzhiyun 		err = ceph_decode_entity_addr(&p, end, &inst->addr);
77*4882a593Smuzhiyun 		if (err)
78*4882a593Smuzhiyun 			goto bad;
79*4882a593Smuzhiyun 	}
80*4882a593Smuzhiyun 	dout("monmap_decode epoch %d, num_mon %d\n", m->epoch,
81*4882a593Smuzhiyun 	     m->num_mon);
82*4882a593Smuzhiyun 	for (i = 0; i < m->num_mon; i++)
83*4882a593Smuzhiyun 		dout("monmap_decode  mon%d is %s\n", i,
84*4882a593Smuzhiyun 		     ceph_pr_addr(&m->mon_inst[i].addr));
85*4882a593Smuzhiyun 	return m;
86*4882a593Smuzhiyun bad:
87*4882a593Smuzhiyun 	dout("monmap_decode failed with %d\n", err);
88*4882a593Smuzhiyun 	kfree(m);
89*4882a593Smuzhiyun 	return ERR_PTR(err);
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun /*
93*4882a593Smuzhiyun  * return true if *addr is included in the monmap.
94*4882a593Smuzhiyun  */
ceph_monmap_contains(struct ceph_monmap * m,struct ceph_entity_addr * addr)95*4882a593Smuzhiyun int ceph_monmap_contains(struct ceph_monmap *m, struct ceph_entity_addr *addr)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun 	int i;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	for (i = 0; i < m->num_mon; i++)
100*4882a593Smuzhiyun 		if (memcmp(addr, &m->mon_inst[i].addr, sizeof(*addr)) == 0)
101*4882a593Smuzhiyun 			return 1;
102*4882a593Smuzhiyun 	return 0;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun /*
106*4882a593Smuzhiyun  * Send an auth request.
107*4882a593Smuzhiyun  */
__send_prepared_auth_request(struct ceph_mon_client * monc,int len)108*4882a593Smuzhiyun static void __send_prepared_auth_request(struct ceph_mon_client *monc, int len)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun 	monc->pending_auth = 1;
111*4882a593Smuzhiyun 	monc->m_auth->front.iov_len = len;
112*4882a593Smuzhiyun 	monc->m_auth->hdr.front_len = cpu_to_le32(len);
113*4882a593Smuzhiyun 	ceph_msg_revoke(monc->m_auth);
114*4882a593Smuzhiyun 	ceph_msg_get(monc->m_auth);  /* keep our ref */
115*4882a593Smuzhiyun 	ceph_con_send(&monc->con, monc->m_auth);
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun /*
119*4882a593Smuzhiyun  * Close monitor session, if any.
120*4882a593Smuzhiyun  */
__close_session(struct ceph_mon_client * monc)121*4882a593Smuzhiyun static void __close_session(struct ceph_mon_client *monc)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun 	dout("__close_session closing mon%d\n", monc->cur_mon);
124*4882a593Smuzhiyun 	ceph_msg_revoke(monc->m_auth);
125*4882a593Smuzhiyun 	ceph_msg_revoke_incoming(monc->m_auth_reply);
126*4882a593Smuzhiyun 	ceph_msg_revoke(monc->m_subscribe);
127*4882a593Smuzhiyun 	ceph_msg_revoke_incoming(monc->m_subscribe_ack);
128*4882a593Smuzhiyun 	ceph_con_close(&monc->con);
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	monc->pending_auth = 0;
131*4882a593Smuzhiyun 	ceph_auth_reset(monc->auth);
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun /*
135*4882a593Smuzhiyun  * Pick a new monitor at random and set cur_mon.  If we are repicking
136*4882a593Smuzhiyun  * (i.e. cur_mon is already set), be sure to pick a different one.
137*4882a593Smuzhiyun  */
pick_new_mon(struct ceph_mon_client * monc)138*4882a593Smuzhiyun static void pick_new_mon(struct ceph_mon_client *monc)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun 	int old_mon = monc->cur_mon;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	BUG_ON(monc->monmap->num_mon < 1);
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	if (monc->monmap->num_mon == 1) {
145*4882a593Smuzhiyun 		monc->cur_mon = 0;
146*4882a593Smuzhiyun 	} else {
147*4882a593Smuzhiyun 		int max = monc->monmap->num_mon;
148*4882a593Smuzhiyun 		int o = -1;
149*4882a593Smuzhiyun 		int n;
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 		if (monc->cur_mon >= 0) {
152*4882a593Smuzhiyun 			if (monc->cur_mon < monc->monmap->num_mon)
153*4882a593Smuzhiyun 				o = monc->cur_mon;
154*4882a593Smuzhiyun 			if (o >= 0)
155*4882a593Smuzhiyun 				max--;
156*4882a593Smuzhiyun 		}
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 		n = prandom_u32() % max;
159*4882a593Smuzhiyun 		if (o >= 0 && n >= o)
160*4882a593Smuzhiyun 			n++;
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 		monc->cur_mon = n;
163*4882a593Smuzhiyun 	}
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	dout("%s mon%d -> mon%d out of %d mons\n", __func__, old_mon,
166*4882a593Smuzhiyun 	     monc->cur_mon, monc->monmap->num_mon);
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun /*
170*4882a593Smuzhiyun  * Open a session with a new monitor.
171*4882a593Smuzhiyun  */
__open_session(struct ceph_mon_client * monc)172*4882a593Smuzhiyun static void __open_session(struct ceph_mon_client *monc)
173*4882a593Smuzhiyun {
174*4882a593Smuzhiyun 	int ret;
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	pick_new_mon(monc);
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	monc->hunting = true;
179*4882a593Smuzhiyun 	if (monc->had_a_connection) {
180*4882a593Smuzhiyun 		monc->hunt_mult *= CEPH_MONC_HUNT_BACKOFF;
181*4882a593Smuzhiyun 		if (monc->hunt_mult > CEPH_MONC_HUNT_MAX_MULT)
182*4882a593Smuzhiyun 			monc->hunt_mult = CEPH_MONC_HUNT_MAX_MULT;
183*4882a593Smuzhiyun 	}
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	monc->sub_renew_after = jiffies; /* i.e., expired */
186*4882a593Smuzhiyun 	monc->sub_renew_sent = 0;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	dout("%s opening mon%d\n", __func__, monc->cur_mon);
189*4882a593Smuzhiyun 	ceph_con_open(&monc->con, CEPH_ENTITY_TYPE_MON, monc->cur_mon,
190*4882a593Smuzhiyun 		      &monc->monmap->mon_inst[monc->cur_mon].addr);
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	/*
193*4882a593Smuzhiyun 	 * send an initial keepalive to ensure our timestamp is valid
194*4882a593Smuzhiyun 	 * by the time we are in an OPENED state
195*4882a593Smuzhiyun 	 */
196*4882a593Smuzhiyun 	ceph_con_keepalive(&monc->con);
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	/* initiate authentication handshake */
199*4882a593Smuzhiyun 	ret = ceph_auth_build_hello(monc->auth,
200*4882a593Smuzhiyun 				    monc->m_auth->front.iov_base,
201*4882a593Smuzhiyun 				    monc->m_auth->front_alloc_len);
202*4882a593Smuzhiyun 	BUG_ON(ret <= 0);
203*4882a593Smuzhiyun 	__send_prepared_auth_request(monc, ret);
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun 
reopen_session(struct ceph_mon_client * monc)206*4882a593Smuzhiyun static void reopen_session(struct ceph_mon_client *monc)
207*4882a593Smuzhiyun {
208*4882a593Smuzhiyun 	if (!monc->hunting)
209*4882a593Smuzhiyun 		pr_info("mon%d %s session lost, hunting for new mon\n",
210*4882a593Smuzhiyun 		    monc->cur_mon, ceph_pr_addr(&monc->con.peer_addr));
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	__close_session(monc);
213*4882a593Smuzhiyun 	__open_session(monc);
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun 
ceph_monc_reopen_session(struct ceph_mon_client * monc)216*4882a593Smuzhiyun void ceph_monc_reopen_session(struct ceph_mon_client *monc)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun 	mutex_lock(&monc->mutex);
219*4882a593Smuzhiyun 	reopen_session(monc);
220*4882a593Smuzhiyun 	mutex_unlock(&monc->mutex);
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun 
un_backoff(struct ceph_mon_client * monc)223*4882a593Smuzhiyun static void un_backoff(struct ceph_mon_client *monc)
224*4882a593Smuzhiyun {
225*4882a593Smuzhiyun 	monc->hunt_mult /= 2; /* reduce by 50% */
226*4882a593Smuzhiyun 	if (monc->hunt_mult < 1)
227*4882a593Smuzhiyun 		monc->hunt_mult = 1;
228*4882a593Smuzhiyun 	dout("%s hunt_mult now %d\n", __func__, monc->hunt_mult);
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun /*
232*4882a593Smuzhiyun  * Reschedule delayed work timer.
233*4882a593Smuzhiyun  */
__schedule_delayed(struct ceph_mon_client * monc)234*4882a593Smuzhiyun static void __schedule_delayed(struct ceph_mon_client *monc)
235*4882a593Smuzhiyun {
236*4882a593Smuzhiyun 	unsigned long delay;
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	if (monc->hunting)
239*4882a593Smuzhiyun 		delay = CEPH_MONC_HUNT_INTERVAL * monc->hunt_mult;
240*4882a593Smuzhiyun 	else
241*4882a593Smuzhiyun 		delay = CEPH_MONC_PING_INTERVAL;
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	dout("__schedule_delayed after %lu\n", delay);
244*4882a593Smuzhiyun 	mod_delayed_work(system_wq, &monc->delayed_work,
245*4882a593Smuzhiyun 			 round_jiffies_relative(delay));
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun const char *ceph_sub_str[] = {
249*4882a593Smuzhiyun 	[CEPH_SUB_MONMAP] = "monmap",
250*4882a593Smuzhiyun 	[CEPH_SUB_OSDMAP] = "osdmap",
251*4882a593Smuzhiyun 	[CEPH_SUB_FSMAP]  = "fsmap.user",
252*4882a593Smuzhiyun 	[CEPH_SUB_MDSMAP] = "mdsmap",
253*4882a593Smuzhiyun };
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun /*
256*4882a593Smuzhiyun  * Send subscribe request for one or more maps, according to
257*4882a593Smuzhiyun  * monc->subs.
258*4882a593Smuzhiyun  */
__send_subscribe(struct ceph_mon_client * monc)259*4882a593Smuzhiyun static void __send_subscribe(struct ceph_mon_client *monc)
260*4882a593Smuzhiyun {
261*4882a593Smuzhiyun 	struct ceph_msg *msg = monc->m_subscribe;
262*4882a593Smuzhiyun 	void *p = msg->front.iov_base;
263*4882a593Smuzhiyun 	void *const end = p + msg->front_alloc_len;
264*4882a593Smuzhiyun 	int num = 0;
265*4882a593Smuzhiyun 	int i;
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	dout("%s sent %lu\n", __func__, monc->sub_renew_sent);
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	BUG_ON(monc->cur_mon < 0);
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 	if (!monc->sub_renew_sent)
272*4882a593Smuzhiyun 		monc->sub_renew_sent = jiffies | 1; /* never 0 */
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	msg->hdr.version = cpu_to_le16(2);
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(monc->subs); i++) {
277*4882a593Smuzhiyun 		if (monc->subs[i].want)
278*4882a593Smuzhiyun 			num++;
279*4882a593Smuzhiyun 	}
280*4882a593Smuzhiyun 	BUG_ON(num < 1); /* monmap sub is always there */
281*4882a593Smuzhiyun 	ceph_encode_32(&p, num);
282*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(monc->subs); i++) {
283*4882a593Smuzhiyun 		char buf[32];
284*4882a593Smuzhiyun 		int len;
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 		if (!monc->subs[i].want)
287*4882a593Smuzhiyun 			continue;
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 		len = sprintf(buf, "%s", ceph_sub_str[i]);
290*4882a593Smuzhiyun 		if (i == CEPH_SUB_MDSMAP &&
291*4882a593Smuzhiyun 		    monc->fs_cluster_id != CEPH_FS_CLUSTER_ID_NONE)
292*4882a593Smuzhiyun 			len += sprintf(buf + len, ".%d", monc->fs_cluster_id);
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 		dout("%s %s start %llu flags 0x%x\n", __func__, buf,
295*4882a593Smuzhiyun 		     le64_to_cpu(monc->subs[i].item.start),
296*4882a593Smuzhiyun 		     monc->subs[i].item.flags);
297*4882a593Smuzhiyun 		ceph_encode_string(&p, end, buf, len);
298*4882a593Smuzhiyun 		memcpy(p, &monc->subs[i].item, sizeof(monc->subs[i].item));
299*4882a593Smuzhiyun 		p += sizeof(monc->subs[i].item);
300*4882a593Smuzhiyun 	}
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	BUG_ON(p > end);
303*4882a593Smuzhiyun 	msg->front.iov_len = p - msg->front.iov_base;
304*4882a593Smuzhiyun 	msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
305*4882a593Smuzhiyun 	ceph_msg_revoke(msg);
306*4882a593Smuzhiyun 	ceph_con_send(&monc->con, ceph_msg_get(msg));
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun 
handle_subscribe_ack(struct ceph_mon_client * monc,struct ceph_msg * msg)309*4882a593Smuzhiyun static void handle_subscribe_ack(struct ceph_mon_client *monc,
310*4882a593Smuzhiyun 				 struct ceph_msg *msg)
311*4882a593Smuzhiyun {
312*4882a593Smuzhiyun 	unsigned int seconds;
313*4882a593Smuzhiyun 	struct ceph_mon_subscribe_ack *h = msg->front.iov_base;
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 	if (msg->front.iov_len < sizeof(*h))
316*4882a593Smuzhiyun 		goto bad;
317*4882a593Smuzhiyun 	seconds = le32_to_cpu(h->duration);
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	mutex_lock(&monc->mutex);
320*4882a593Smuzhiyun 	if (monc->sub_renew_sent) {
321*4882a593Smuzhiyun 		/*
322*4882a593Smuzhiyun 		 * This is only needed for legacy (infernalis or older)
323*4882a593Smuzhiyun 		 * MONs -- see delayed_work().
324*4882a593Smuzhiyun 		 */
325*4882a593Smuzhiyun 		monc->sub_renew_after = monc->sub_renew_sent +
326*4882a593Smuzhiyun 					    (seconds >> 1) * HZ - 1;
327*4882a593Smuzhiyun 		dout("%s sent %lu duration %d renew after %lu\n", __func__,
328*4882a593Smuzhiyun 		     monc->sub_renew_sent, seconds, monc->sub_renew_after);
329*4882a593Smuzhiyun 		monc->sub_renew_sent = 0;
330*4882a593Smuzhiyun 	} else {
331*4882a593Smuzhiyun 		dout("%s sent %lu renew after %lu, ignoring\n", __func__,
332*4882a593Smuzhiyun 		     monc->sub_renew_sent, monc->sub_renew_after);
333*4882a593Smuzhiyun 	}
334*4882a593Smuzhiyun 	mutex_unlock(&monc->mutex);
335*4882a593Smuzhiyun 	return;
336*4882a593Smuzhiyun bad:
337*4882a593Smuzhiyun 	pr_err("got corrupt subscribe-ack msg\n");
338*4882a593Smuzhiyun 	ceph_msg_dump(msg);
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun /*
342*4882a593Smuzhiyun  * Register interest in a map
343*4882a593Smuzhiyun  *
344*4882a593Smuzhiyun  * @sub: one of CEPH_SUB_*
345*4882a593Smuzhiyun  * @epoch: X for "every map since X", or 0 for "just the latest"
346*4882a593Smuzhiyun  */
__ceph_monc_want_map(struct ceph_mon_client * monc,int sub,u32 epoch,bool continuous)347*4882a593Smuzhiyun static bool __ceph_monc_want_map(struct ceph_mon_client *monc, int sub,
348*4882a593Smuzhiyun 				 u32 epoch, bool continuous)
349*4882a593Smuzhiyun {
350*4882a593Smuzhiyun 	__le64 start = cpu_to_le64(epoch);
351*4882a593Smuzhiyun 	u8 flags = !continuous ? CEPH_SUBSCRIBE_ONETIME : 0;
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	dout("%s %s epoch %u continuous %d\n", __func__, ceph_sub_str[sub],
354*4882a593Smuzhiyun 	     epoch, continuous);
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	if (monc->subs[sub].want &&
357*4882a593Smuzhiyun 	    monc->subs[sub].item.start == start &&
358*4882a593Smuzhiyun 	    monc->subs[sub].item.flags == flags)
359*4882a593Smuzhiyun 		return false;
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	monc->subs[sub].item.start = start;
362*4882a593Smuzhiyun 	monc->subs[sub].item.flags = flags;
363*4882a593Smuzhiyun 	monc->subs[sub].want = true;
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun 	return true;
366*4882a593Smuzhiyun }
367*4882a593Smuzhiyun 
ceph_monc_want_map(struct ceph_mon_client * monc,int sub,u32 epoch,bool continuous)368*4882a593Smuzhiyun bool ceph_monc_want_map(struct ceph_mon_client *monc, int sub, u32 epoch,
369*4882a593Smuzhiyun 			bool continuous)
370*4882a593Smuzhiyun {
371*4882a593Smuzhiyun 	bool need_request;
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	mutex_lock(&monc->mutex);
374*4882a593Smuzhiyun 	need_request = __ceph_monc_want_map(monc, sub, epoch, continuous);
375*4882a593Smuzhiyun 	mutex_unlock(&monc->mutex);
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 	return need_request;
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun EXPORT_SYMBOL(ceph_monc_want_map);
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun /*
382*4882a593Smuzhiyun  * Keep track of which maps we have
383*4882a593Smuzhiyun  *
384*4882a593Smuzhiyun  * @sub: one of CEPH_SUB_*
385*4882a593Smuzhiyun  */
__ceph_monc_got_map(struct ceph_mon_client * monc,int sub,u32 epoch)386*4882a593Smuzhiyun static void __ceph_monc_got_map(struct ceph_mon_client *monc, int sub,
387*4882a593Smuzhiyun 				u32 epoch)
388*4882a593Smuzhiyun {
389*4882a593Smuzhiyun 	dout("%s %s epoch %u\n", __func__, ceph_sub_str[sub], epoch);
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 	if (monc->subs[sub].want) {
392*4882a593Smuzhiyun 		if (monc->subs[sub].item.flags & CEPH_SUBSCRIBE_ONETIME)
393*4882a593Smuzhiyun 			monc->subs[sub].want = false;
394*4882a593Smuzhiyun 		else
395*4882a593Smuzhiyun 			monc->subs[sub].item.start = cpu_to_le64(epoch + 1);
396*4882a593Smuzhiyun 	}
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun 	monc->subs[sub].have = epoch;
399*4882a593Smuzhiyun }
400*4882a593Smuzhiyun 
ceph_monc_got_map(struct ceph_mon_client * monc,int sub,u32 epoch)401*4882a593Smuzhiyun void ceph_monc_got_map(struct ceph_mon_client *monc, int sub, u32 epoch)
402*4882a593Smuzhiyun {
403*4882a593Smuzhiyun 	mutex_lock(&monc->mutex);
404*4882a593Smuzhiyun 	__ceph_monc_got_map(monc, sub, epoch);
405*4882a593Smuzhiyun 	mutex_unlock(&monc->mutex);
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun EXPORT_SYMBOL(ceph_monc_got_map);
408*4882a593Smuzhiyun 
ceph_monc_renew_subs(struct ceph_mon_client * monc)409*4882a593Smuzhiyun void ceph_monc_renew_subs(struct ceph_mon_client *monc)
410*4882a593Smuzhiyun {
411*4882a593Smuzhiyun 	mutex_lock(&monc->mutex);
412*4882a593Smuzhiyun 	__send_subscribe(monc);
413*4882a593Smuzhiyun 	mutex_unlock(&monc->mutex);
414*4882a593Smuzhiyun }
415*4882a593Smuzhiyun EXPORT_SYMBOL(ceph_monc_renew_subs);
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun /*
418*4882a593Smuzhiyun  * Wait for an osdmap with a given epoch.
419*4882a593Smuzhiyun  *
420*4882a593Smuzhiyun  * @epoch: epoch to wait for
421*4882a593Smuzhiyun  * @timeout: in jiffies, 0 means "wait forever"
422*4882a593Smuzhiyun  */
ceph_monc_wait_osdmap(struct ceph_mon_client * monc,u32 epoch,unsigned long timeout)423*4882a593Smuzhiyun int ceph_monc_wait_osdmap(struct ceph_mon_client *monc, u32 epoch,
424*4882a593Smuzhiyun 			  unsigned long timeout)
425*4882a593Smuzhiyun {
426*4882a593Smuzhiyun 	unsigned long started = jiffies;
427*4882a593Smuzhiyun 	long ret;
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 	mutex_lock(&monc->mutex);
430*4882a593Smuzhiyun 	while (monc->subs[CEPH_SUB_OSDMAP].have < epoch) {
431*4882a593Smuzhiyun 		mutex_unlock(&monc->mutex);
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 		if (timeout && time_after_eq(jiffies, started + timeout))
434*4882a593Smuzhiyun 			return -ETIMEDOUT;
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 		ret = wait_event_interruptible_timeout(monc->client->auth_wq,
437*4882a593Smuzhiyun 				     monc->subs[CEPH_SUB_OSDMAP].have >= epoch,
438*4882a593Smuzhiyun 				     ceph_timeout_jiffies(timeout));
439*4882a593Smuzhiyun 		if (ret < 0)
440*4882a593Smuzhiyun 			return ret;
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 		mutex_lock(&monc->mutex);
443*4882a593Smuzhiyun 	}
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 	mutex_unlock(&monc->mutex);
446*4882a593Smuzhiyun 	return 0;
447*4882a593Smuzhiyun }
448*4882a593Smuzhiyun EXPORT_SYMBOL(ceph_monc_wait_osdmap);
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun /*
451*4882a593Smuzhiyun  * Open a session with a random monitor.  Request monmap and osdmap,
452*4882a593Smuzhiyun  * which are waited upon in __ceph_open_session().
453*4882a593Smuzhiyun  */
ceph_monc_open_session(struct ceph_mon_client * monc)454*4882a593Smuzhiyun int ceph_monc_open_session(struct ceph_mon_client *monc)
455*4882a593Smuzhiyun {
456*4882a593Smuzhiyun 	mutex_lock(&monc->mutex);
457*4882a593Smuzhiyun 	__ceph_monc_want_map(monc, CEPH_SUB_MONMAP, 0, true);
458*4882a593Smuzhiyun 	__ceph_monc_want_map(monc, CEPH_SUB_OSDMAP, 0, false);
459*4882a593Smuzhiyun 	__open_session(monc);
460*4882a593Smuzhiyun 	__schedule_delayed(monc);
461*4882a593Smuzhiyun 	mutex_unlock(&monc->mutex);
462*4882a593Smuzhiyun 	return 0;
463*4882a593Smuzhiyun }
464*4882a593Smuzhiyun EXPORT_SYMBOL(ceph_monc_open_session);
465*4882a593Smuzhiyun 
ceph_monc_handle_map(struct ceph_mon_client * monc,struct ceph_msg * msg)466*4882a593Smuzhiyun static void ceph_monc_handle_map(struct ceph_mon_client *monc,
467*4882a593Smuzhiyun 				 struct ceph_msg *msg)
468*4882a593Smuzhiyun {
469*4882a593Smuzhiyun 	struct ceph_client *client = monc->client;
470*4882a593Smuzhiyun 	struct ceph_monmap *monmap;
471*4882a593Smuzhiyun 	void *p, *end;
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	mutex_lock(&monc->mutex);
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 	dout("handle_monmap\n");
476*4882a593Smuzhiyun 	p = msg->front.iov_base;
477*4882a593Smuzhiyun 	end = p + msg->front.iov_len;
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 	monmap = ceph_monmap_decode(p, end);
480*4882a593Smuzhiyun 	if (IS_ERR(monmap)) {
481*4882a593Smuzhiyun 		pr_err("problem decoding monmap, %d\n",
482*4882a593Smuzhiyun 		       (int)PTR_ERR(monmap));
483*4882a593Smuzhiyun 		ceph_msg_dump(msg);
484*4882a593Smuzhiyun 		goto out;
485*4882a593Smuzhiyun 	}
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun 	if (ceph_check_fsid(client, &monmap->fsid) < 0) {
488*4882a593Smuzhiyun 		kfree(monmap);
489*4882a593Smuzhiyun 		goto out;
490*4882a593Smuzhiyun 	}
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 	kfree(monc->monmap);
493*4882a593Smuzhiyun 	monc->monmap = monmap;
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 	__ceph_monc_got_map(monc, CEPH_SUB_MONMAP, monc->monmap->epoch);
496*4882a593Smuzhiyun 	client->have_fsid = true;
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun out:
499*4882a593Smuzhiyun 	mutex_unlock(&monc->mutex);
500*4882a593Smuzhiyun 	wake_up_all(&client->auth_wq);
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun /*
504*4882a593Smuzhiyun  * generic requests (currently statfs, mon_get_version)
505*4882a593Smuzhiyun  */
DEFINE_RB_FUNCS(generic_request,struct ceph_mon_generic_request,tid,node)506*4882a593Smuzhiyun DEFINE_RB_FUNCS(generic_request, struct ceph_mon_generic_request, tid, node)
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun static void release_generic_request(struct kref *kref)
509*4882a593Smuzhiyun {
510*4882a593Smuzhiyun 	struct ceph_mon_generic_request *req =
511*4882a593Smuzhiyun 		container_of(kref, struct ceph_mon_generic_request, kref);
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 	dout("%s greq %p request %p reply %p\n", __func__, req, req->request,
514*4882a593Smuzhiyun 	     req->reply);
515*4882a593Smuzhiyun 	WARN_ON(!RB_EMPTY_NODE(&req->node));
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun 	if (req->reply)
518*4882a593Smuzhiyun 		ceph_msg_put(req->reply);
519*4882a593Smuzhiyun 	if (req->request)
520*4882a593Smuzhiyun 		ceph_msg_put(req->request);
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 	kfree(req);
523*4882a593Smuzhiyun }
524*4882a593Smuzhiyun 
put_generic_request(struct ceph_mon_generic_request * req)525*4882a593Smuzhiyun static void put_generic_request(struct ceph_mon_generic_request *req)
526*4882a593Smuzhiyun {
527*4882a593Smuzhiyun 	if (req)
528*4882a593Smuzhiyun 		kref_put(&req->kref, release_generic_request);
529*4882a593Smuzhiyun }
530*4882a593Smuzhiyun 
get_generic_request(struct ceph_mon_generic_request * req)531*4882a593Smuzhiyun static void get_generic_request(struct ceph_mon_generic_request *req)
532*4882a593Smuzhiyun {
533*4882a593Smuzhiyun 	kref_get(&req->kref);
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun static struct ceph_mon_generic_request *
alloc_generic_request(struct ceph_mon_client * monc,gfp_t gfp)537*4882a593Smuzhiyun alloc_generic_request(struct ceph_mon_client *monc, gfp_t gfp)
538*4882a593Smuzhiyun {
539*4882a593Smuzhiyun 	struct ceph_mon_generic_request *req;
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	req = kzalloc(sizeof(*req), gfp);
542*4882a593Smuzhiyun 	if (!req)
543*4882a593Smuzhiyun 		return NULL;
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	req->monc = monc;
546*4882a593Smuzhiyun 	kref_init(&req->kref);
547*4882a593Smuzhiyun 	RB_CLEAR_NODE(&req->node);
548*4882a593Smuzhiyun 	init_completion(&req->completion);
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 	dout("%s greq %p\n", __func__, req);
551*4882a593Smuzhiyun 	return req;
552*4882a593Smuzhiyun }
553*4882a593Smuzhiyun 
register_generic_request(struct ceph_mon_generic_request * req)554*4882a593Smuzhiyun static void register_generic_request(struct ceph_mon_generic_request *req)
555*4882a593Smuzhiyun {
556*4882a593Smuzhiyun 	struct ceph_mon_client *monc = req->monc;
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun 	WARN_ON(req->tid);
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 	get_generic_request(req);
561*4882a593Smuzhiyun 	req->tid = ++monc->last_tid;
562*4882a593Smuzhiyun 	insert_generic_request(&monc->generic_request_tree, req);
563*4882a593Smuzhiyun }
564*4882a593Smuzhiyun 
send_generic_request(struct ceph_mon_client * monc,struct ceph_mon_generic_request * req)565*4882a593Smuzhiyun static void send_generic_request(struct ceph_mon_client *monc,
566*4882a593Smuzhiyun 				 struct ceph_mon_generic_request *req)
567*4882a593Smuzhiyun {
568*4882a593Smuzhiyun 	WARN_ON(!req->tid);
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 	dout("%s greq %p tid %llu\n", __func__, req, req->tid);
571*4882a593Smuzhiyun 	req->request->hdr.tid = cpu_to_le64(req->tid);
572*4882a593Smuzhiyun 	ceph_con_send(&monc->con, ceph_msg_get(req->request));
573*4882a593Smuzhiyun }
574*4882a593Smuzhiyun 
__finish_generic_request(struct ceph_mon_generic_request * req)575*4882a593Smuzhiyun static void __finish_generic_request(struct ceph_mon_generic_request *req)
576*4882a593Smuzhiyun {
577*4882a593Smuzhiyun 	struct ceph_mon_client *monc = req->monc;
578*4882a593Smuzhiyun 
579*4882a593Smuzhiyun 	dout("%s greq %p tid %llu\n", __func__, req, req->tid);
580*4882a593Smuzhiyun 	erase_generic_request(&monc->generic_request_tree, req);
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun 	ceph_msg_revoke(req->request);
583*4882a593Smuzhiyun 	ceph_msg_revoke_incoming(req->reply);
584*4882a593Smuzhiyun }
585*4882a593Smuzhiyun 
finish_generic_request(struct ceph_mon_generic_request * req)586*4882a593Smuzhiyun static void finish_generic_request(struct ceph_mon_generic_request *req)
587*4882a593Smuzhiyun {
588*4882a593Smuzhiyun 	__finish_generic_request(req);
589*4882a593Smuzhiyun 	put_generic_request(req);
590*4882a593Smuzhiyun }
591*4882a593Smuzhiyun 
complete_generic_request(struct ceph_mon_generic_request * req)592*4882a593Smuzhiyun static void complete_generic_request(struct ceph_mon_generic_request *req)
593*4882a593Smuzhiyun {
594*4882a593Smuzhiyun 	if (req->complete_cb)
595*4882a593Smuzhiyun 		req->complete_cb(req);
596*4882a593Smuzhiyun 	else
597*4882a593Smuzhiyun 		complete_all(&req->completion);
598*4882a593Smuzhiyun 	put_generic_request(req);
599*4882a593Smuzhiyun }
600*4882a593Smuzhiyun 
cancel_generic_request(struct ceph_mon_generic_request * req)601*4882a593Smuzhiyun static void cancel_generic_request(struct ceph_mon_generic_request *req)
602*4882a593Smuzhiyun {
603*4882a593Smuzhiyun 	struct ceph_mon_client *monc = req->monc;
604*4882a593Smuzhiyun 	struct ceph_mon_generic_request *lookup_req;
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun 	dout("%s greq %p tid %llu\n", __func__, req, req->tid);
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 	mutex_lock(&monc->mutex);
609*4882a593Smuzhiyun 	lookup_req = lookup_generic_request(&monc->generic_request_tree,
610*4882a593Smuzhiyun 					    req->tid);
611*4882a593Smuzhiyun 	if (lookup_req) {
612*4882a593Smuzhiyun 		WARN_ON(lookup_req != req);
613*4882a593Smuzhiyun 		finish_generic_request(req);
614*4882a593Smuzhiyun 	}
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun 	mutex_unlock(&monc->mutex);
617*4882a593Smuzhiyun }
618*4882a593Smuzhiyun 
wait_generic_request(struct ceph_mon_generic_request * req)619*4882a593Smuzhiyun static int wait_generic_request(struct ceph_mon_generic_request *req)
620*4882a593Smuzhiyun {
621*4882a593Smuzhiyun 	int ret;
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun 	dout("%s greq %p tid %llu\n", __func__, req, req->tid);
624*4882a593Smuzhiyun 	ret = wait_for_completion_interruptible(&req->completion);
625*4882a593Smuzhiyun 	if (ret)
626*4882a593Smuzhiyun 		cancel_generic_request(req);
627*4882a593Smuzhiyun 	else
628*4882a593Smuzhiyun 		ret = req->result; /* completed */
629*4882a593Smuzhiyun 
630*4882a593Smuzhiyun 	return ret;
631*4882a593Smuzhiyun }
632*4882a593Smuzhiyun 
get_generic_reply(struct ceph_connection * con,struct ceph_msg_header * hdr,int * skip)633*4882a593Smuzhiyun static struct ceph_msg *get_generic_reply(struct ceph_connection *con,
634*4882a593Smuzhiyun 					 struct ceph_msg_header *hdr,
635*4882a593Smuzhiyun 					 int *skip)
636*4882a593Smuzhiyun {
637*4882a593Smuzhiyun 	struct ceph_mon_client *monc = con->private;
638*4882a593Smuzhiyun 	struct ceph_mon_generic_request *req;
639*4882a593Smuzhiyun 	u64 tid = le64_to_cpu(hdr->tid);
640*4882a593Smuzhiyun 	struct ceph_msg *m;
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun 	mutex_lock(&monc->mutex);
643*4882a593Smuzhiyun 	req = lookup_generic_request(&monc->generic_request_tree, tid);
644*4882a593Smuzhiyun 	if (!req) {
645*4882a593Smuzhiyun 		dout("get_generic_reply %lld dne\n", tid);
646*4882a593Smuzhiyun 		*skip = 1;
647*4882a593Smuzhiyun 		m = NULL;
648*4882a593Smuzhiyun 	} else {
649*4882a593Smuzhiyun 		dout("get_generic_reply %lld got %p\n", tid, req->reply);
650*4882a593Smuzhiyun 		*skip = 0;
651*4882a593Smuzhiyun 		m = ceph_msg_get(req->reply);
652*4882a593Smuzhiyun 		/*
653*4882a593Smuzhiyun 		 * we don't need to track the connection reading into
654*4882a593Smuzhiyun 		 * this reply because we only have one open connection
655*4882a593Smuzhiyun 		 * at a time, ever.
656*4882a593Smuzhiyun 		 */
657*4882a593Smuzhiyun 	}
658*4882a593Smuzhiyun 	mutex_unlock(&monc->mutex);
659*4882a593Smuzhiyun 	return m;
660*4882a593Smuzhiyun }
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun /*
663*4882a593Smuzhiyun  * statfs
664*4882a593Smuzhiyun  */
handle_statfs_reply(struct ceph_mon_client * monc,struct ceph_msg * msg)665*4882a593Smuzhiyun static void handle_statfs_reply(struct ceph_mon_client *monc,
666*4882a593Smuzhiyun 				struct ceph_msg *msg)
667*4882a593Smuzhiyun {
668*4882a593Smuzhiyun 	struct ceph_mon_generic_request *req;
669*4882a593Smuzhiyun 	struct ceph_mon_statfs_reply *reply = msg->front.iov_base;
670*4882a593Smuzhiyun 	u64 tid = le64_to_cpu(msg->hdr.tid);
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun 	dout("%s msg %p tid %llu\n", __func__, msg, tid);
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun 	if (msg->front.iov_len != sizeof(*reply))
675*4882a593Smuzhiyun 		goto bad;
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 	mutex_lock(&monc->mutex);
678*4882a593Smuzhiyun 	req = lookup_generic_request(&monc->generic_request_tree, tid);
679*4882a593Smuzhiyun 	if (!req) {
680*4882a593Smuzhiyun 		mutex_unlock(&monc->mutex);
681*4882a593Smuzhiyun 		return;
682*4882a593Smuzhiyun 	}
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun 	req->result = 0;
685*4882a593Smuzhiyun 	*req->u.st = reply->st; /* struct */
686*4882a593Smuzhiyun 	__finish_generic_request(req);
687*4882a593Smuzhiyun 	mutex_unlock(&monc->mutex);
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 	complete_generic_request(req);
690*4882a593Smuzhiyun 	return;
691*4882a593Smuzhiyun 
692*4882a593Smuzhiyun bad:
693*4882a593Smuzhiyun 	pr_err("corrupt statfs reply, tid %llu\n", tid);
694*4882a593Smuzhiyun 	ceph_msg_dump(msg);
695*4882a593Smuzhiyun }
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun /*
698*4882a593Smuzhiyun  * Do a synchronous statfs().
699*4882a593Smuzhiyun  */
ceph_monc_do_statfs(struct ceph_mon_client * monc,u64 data_pool,struct ceph_statfs * buf)700*4882a593Smuzhiyun int ceph_monc_do_statfs(struct ceph_mon_client *monc, u64 data_pool,
701*4882a593Smuzhiyun 			struct ceph_statfs *buf)
702*4882a593Smuzhiyun {
703*4882a593Smuzhiyun 	struct ceph_mon_generic_request *req;
704*4882a593Smuzhiyun 	struct ceph_mon_statfs *h;
705*4882a593Smuzhiyun 	int ret = -ENOMEM;
706*4882a593Smuzhiyun 
707*4882a593Smuzhiyun 	req = alloc_generic_request(monc, GFP_NOFS);
708*4882a593Smuzhiyun 	if (!req)
709*4882a593Smuzhiyun 		goto out;
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun 	req->request = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), GFP_NOFS,
712*4882a593Smuzhiyun 				    true);
713*4882a593Smuzhiyun 	if (!req->request)
714*4882a593Smuzhiyun 		goto out;
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 	req->reply = ceph_msg_new(CEPH_MSG_STATFS_REPLY, 64, GFP_NOFS, true);
717*4882a593Smuzhiyun 	if (!req->reply)
718*4882a593Smuzhiyun 		goto out;
719*4882a593Smuzhiyun 
720*4882a593Smuzhiyun 	req->u.st = buf;
721*4882a593Smuzhiyun 	req->request->hdr.version = cpu_to_le16(2);
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun 	mutex_lock(&monc->mutex);
724*4882a593Smuzhiyun 	register_generic_request(req);
725*4882a593Smuzhiyun 	/* fill out request */
726*4882a593Smuzhiyun 	h = req->request->front.iov_base;
727*4882a593Smuzhiyun 	h->monhdr.have_version = 0;
728*4882a593Smuzhiyun 	h->monhdr.session_mon = cpu_to_le16(-1);
729*4882a593Smuzhiyun 	h->monhdr.session_mon_tid = 0;
730*4882a593Smuzhiyun 	h->fsid = monc->monmap->fsid;
731*4882a593Smuzhiyun 	h->contains_data_pool = (data_pool != CEPH_NOPOOL);
732*4882a593Smuzhiyun 	h->data_pool = cpu_to_le64(data_pool);
733*4882a593Smuzhiyun 	send_generic_request(monc, req);
734*4882a593Smuzhiyun 	mutex_unlock(&monc->mutex);
735*4882a593Smuzhiyun 
736*4882a593Smuzhiyun 	ret = wait_generic_request(req);
737*4882a593Smuzhiyun out:
738*4882a593Smuzhiyun 	put_generic_request(req);
739*4882a593Smuzhiyun 	return ret;
740*4882a593Smuzhiyun }
741*4882a593Smuzhiyun EXPORT_SYMBOL(ceph_monc_do_statfs);
742*4882a593Smuzhiyun 
handle_get_version_reply(struct ceph_mon_client * monc,struct ceph_msg * msg)743*4882a593Smuzhiyun static void handle_get_version_reply(struct ceph_mon_client *monc,
744*4882a593Smuzhiyun 				     struct ceph_msg *msg)
745*4882a593Smuzhiyun {
746*4882a593Smuzhiyun 	struct ceph_mon_generic_request *req;
747*4882a593Smuzhiyun 	u64 tid = le64_to_cpu(msg->hdr.tid);
748*4882a593Smuzhiyun 	void *p = msg->front.iov_base;
749*4882a593Smuzhiyun 	void *end = p + msg->front_alloc_len;
750*4882a593Smuzhiyun 	u64 handle;
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun 	dout("%s msg %p tid %llu\n", __func__, msg, tid);
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 	ceph_decode_need(&p, end, 2*sizeof(u64), bad);
755*4882a593Smuzhiyun 	handle = ceph_decode_64(&p);
756*4882a593Smuzhiyun 	if (tid != 0 && tid != handle)
757*4882a593Smuzhiyun 		goto bad;
758*4882a593Smuzhiyun 
759*4882a593Smuzhiyun 	mutex_lock(&monc->mutex);
760*4882a593Smuzhiyun 	req = lookup_generic_request(&monc->generic_request_tree, handle);
761*4882a593Smuzhiyun 	if (!req) {
762*4882a593Smuzhiyun 		mutex_unlock(&monc->mutex);
763*4882a593Smuzhiyun 		return;
764*4882a593Smuzhiyun 	}
765*4882a593Smuzhiyun 
766*4882a593Smuzhiyun 	req->result = 0;
767*4882a593Smuzhiyun 	req->u.newest = ceph_decode_64(&p);
768*4882a593Smuzhiyun 	__finish_generic_request(req);
769*4882a593Smuzhiyun 	mutex_unlock(&monc->mutex);
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 	complete_generic_request(req);
772*4882a593Smuzhiyun 	return;
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun bad:
775*4882a593Smuzhiyun 	pr_err("corrupt mon_get_version reply, tid %llu\n", tid);
776*4882a593Smuzhiyun 	ceph_msg_dump(msg);
777*4882a593Smuzhiyun }
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun 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)780*4882a593Smuzhiyun __ceph_monc_get_version(struct ceph_mon_client *monc, const char *what,
781*4882a593Smuzhiyun 			ceph_monc_callback_t cb, u64 private_data)
782*4882a593Smuzhiyun {
783*4882a593Smuzhiyun 	struct ceph_mon_generic_request *req;
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun 	req = alloc_generic_request(monc, GFP_NOIO);
786*4882a593Smuzhiyun 	if (!req)
787*4882a593Smuzhiyun 		goto err_put_req;
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun 	req->request = ceph_msg_new(CEPH_MSG_MON_GET_VERSION,
790*4882a593Smuzhiyun 				    sizeof(u64) + sizeof(u32) + strlen(what),
791*4882a593Smuzhiyun 				    GFP_NOIO, true);
792*4882a593Smuzhiyun 	if (!req->request)
793*4882a593Smuzhiyun 		goto err_put_req;
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun 	req->reply = ceph_msg_new(CEPH_MSG_MON_GET_VERSION_REPLY, 32, GFP_NOIO,
796*4882a593Smuzhiyun 				  true);
797*4882a593Smuzhiyun 	if (!req->reply)
798*4882a593Smuzhiyun 		goto err_put_req;
799*4882a593Smuzhiyun 
800*4882a593Smuzhiyun 	req->complete_cb = cb;
801*4882a593Smuzhiyun 	req->private_data = private_data;
802*4882a593Smuzhiyun 
803*4882a593Smuzhiyun 	mutex_lock(&monc->mutex);
804*4882a593Smuzhiyun 	register_generic_request(req);
805*4882a593Smuzhiyun 	{
806*4882a593Smuzhiyun 		void *p = req->request->front.iov_base;
807*4882a593Smuzhiyun 		void *const end = p + req->request->front_alloc_len;
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun 		ceph_encode_64(&p, req->tid); /* handle */
810*4882a593Smuzhiyun 		ceph_encode_string(&p, end, what, strlen(what));
811*4882a593Smuzhiyun 		WARN_ON(p != end);
812*4882a593Smuzhiyun 	}
813*4882a593Smuzhiyun 	send_generic_request(monc, req);
814*4882a593Smuzhiyun 	mutex_unlock(&monc->mutex);
815*4882a593Smuzhiyun 
816*4882a593Smuzhiyun 	return req;
817*4882a593Smuzhiyun 
818*4882a593Smuzhiyun err_put_req:
819*4882a593Smuzhiyun 	put_generic_request(req);
820*4882a593Smuzhiyun 	return ERR_PTR(-ENOMEM);
821*4882a593Smuzhiyun }
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun /*
824*4882a593Smuzhiyun  * Send MMonGetVersion and wait for the reply.
825*4882a593Smuzhiyun  *
826*4882a593Smuzhiyun  * @what: one of "mdsmap", "osdmap" or "monmap"
827*4882a593Smuzhiyun  */
ceph_monc_get_version(struct ceph_mon_client * monc,const char * what,u64 * newest)828*4882a593Smuzhiyun int ceph_monc_get_version(struct ceph_mon_client *monc, const char *what,
829*4882a593Smuzhiyun 			  u64 *newest)
830*4882a593Smuzhiyun {
831*4882a593Smuzhiyun 	struct ceph_mon_generic_request *req;
832*4882a593Smuzhiyun 	int ret;
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun 	req = __ceph_monc_get_version(monc, what, NULL, 0);
835*4882a593Smuzhiyun 	if (IS_ERR(req))
836*4882a593Smuzhiyun 		return PTR_ERR(req);
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun 	ret = wait_generic_request(req);
839*4882a593Smuzhiyun 	if (!ret)
840*4882a593Smuzhiyun 		*newest = req->u.newest;
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun 	put_generic_request(req);
843*4882a593Smuzhiyun 	return ret;
844*4882a593Smuzhiyun }
845*4882a593Smuzhiyun EXPORT_SYMBOL(ceph_monc_get_version);
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun /*
848*4882a593Smuzhiyun  * Send MMonGetVersion,
849*4882a593Smuzhiyun  *
850*4882a593Smuzhiyun  * @what: one of "mdsmap", "osdmap" or "monmap"
851*4882a593Smuzhiyun  */
ceph_monc_get_version_async(struct ceph_mon_client * monc,const char * what,ceph_monc_callback_t cb,u64 private_data)852*4882a593Smuzhiyun int ceph_monc_get_version_async(struct ceph_mon_client *monc, const char *what,
853*4882a593Smuzhiyun 				ceph_monc_callback_t cb, u64 private_data)
854*4882a593Smuzhiyun {
855*4882a593Smuzhiyun 	struct ceph_mon_generic_request *req;
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun 	req = __ceph_monc_get_version(monc, what, cb, private_data);
858*4882a593Smuzhiyun 	if (IS_ERR(req))
859*4882a593Smuzhiyun 		return PTR_ERR(req);
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun 	put_generic_request(req);
862*4882a593Smuzhiyun 	return 0;
863*4882a593Smuzhiyun }
864*4882a593Smuzhiyun EXPORT_SYMBOL(ceph_monc_get_version_async);
865*4882a593Smuzhiyun 
handle_command_ack(struct ceph_mon_client * monc,struct ceph_msg * msg)866*4882a593Smuzhiyun static void handle_command_ack(struct ceph_mon_client *monc,
867*4882a593Smuzhiyun 			       struct ceph_msg *msg)
868*4882a593Smuzhiyun {
869*4882a593Smuzhiyun 	struct ceph_mon_generic_request *req;
870*4882a593Smuzhiyun 	void *p = msg->front.iov_base;
871*4882a593Smuzhiyun 	void *const end = p + msg->front_alloc_len;
872*4882a593Smuzhiyun 	u64 tid = le64_to_cpu(msg->hdr.tid);
873*4882a593Smuzhiyun 
874*4882a593Smuzhiyun 	dout("%s msg %p tid %llu\n", __func__, msg, tid);
875*4882a593Smuzhiyun 
876*4882a593Smuzhiyun 	ceph_decode_need(&p, end, sizeof(struct ceph_mon_request_header) +
877*4882a593Smuzhiyun 							    sizeof(u32), bad);
878*4882a593Smuzhiyun 	p += sizeof(struct ceph_mon_request_header);
879*4882a593Smuzhiyun 
880*4882a593Smuzhiyun 	mutex_lock(&monc->mutex);
881*4882a593Smuzhiyun 	req = lookup_generic_request(&monc->generic_request_tree, tid);
882*4882a593Smuzhiyun 	if (!req) {
883*4882a593Smuzhiyun 		mutex_unlock(&monc->mutex);
884*4882a593Smuzhiyun 		return;
885*4882a593Smuzhiyun 	}
886*4882a593Smuzhiyun 
887*4882a593Smuzhiyun 	req->result = ceph_decode_32(&p);
888*4882a593Smuzhiyun 	__finish_generic_request(req);
889*4882a593Smuzhiyun 	mutex_unlock(&monc->mutex);
890*4882a593Smuzhiyun 
891*4882a593Smuzhiyun 	complete_generic_request(req);
892*4882a593Smuzhiyun 	return;
893*4882a593Smuzhiyun 
894*4882a593Smuzhiyun bad:
895*4882a593Smuzhiyun 	pr_err("corrupt mon_command ack, tid %llu\n", tid);
896*4882a593Smuzhiyun 	ceph_msg_dump(msg);
897*4882a593Smuzhiyun }
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun static __printf(2, 0)
do_mon_command_vargs(struct ceph_mon_client * monc,const char * fmt,va_list ap)900*4882a593Smuzhiyun int do_mon_command_vargs(struct ceph_mon_client *monc, const char *fmt,
901*4882a593Smuzhiyun 			 va_list ap)
902*4882a593Smuzhiyun {
903*4882a593Smuzhiyun 	struct ceph_mon_generic_request *req;
904*4882a593Smuzhiyun 	struct ceph_mon_command *h;
905*4882a593Smuzhiyun 	int ret = -ENOMEM;
906*4882a593Smuzhiyun 	int len;
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun 	req = alloc_generic_request(monc, GFP_NOIO);
909*4882a593Smuzhiyun 	if (!req)
910*4882a593Smuzhiyun 		goto out;
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun 	req->request = ceph_msg_new(CEPH_MSG_MON_COMMAND, 256, GFP_NOIO, true);
913*4882a593Smuzhiyun 	if (!req->request)
914*4882a593Smuzhiyun 		goto out;
915*4882a593Smuzhiyun 
916*4882a593Smuzhiyun 	req->reply = ceph_msg_new(CEPH_MSG_MON_COMMAND_ACK, 512, GFP_NOIO,
917*4882a593Smuzhiyun 				  true);
918*4882a593Smuzhiyun 	if (!req->reply)
919*4882a593Smuzhiyun 		goto out;
920*4882a593Smuzhiyun 
921*4882a593Smuzhiyun 	mutex_lock(&monc->mutex);
922*4882a593Smuzhiyun 	register_generic_request(req);
923*4882a593Smuzhiyun 	h = req->request->front.iov_base;
924*4882a593Smuzhiyun 	h->monhdr.have_version = 0;
925*4882a593Smuzhiyun 	h->monhdr.session_mon = cpu_to_le16(-1);
926*4882a593Smuzhiyun 	h->monhdr.session_mon_tid = 0;
927*4882a593Smuzhiyun 	h->fsid = monc->monmap->fsid;
928*4882a593Smuzhiyun 	h->num_strs = cpu_to_le32(1);
929*4882a593Smuzhiyun 	len = vsprintf(h->str, fmt, ap);
930*4882a593Smuzhiyun 	h->str_len = cpu_to_le32(len);
931*4882a593Smuzhiyun 	send_generic_request(monc, req);
932*4882a593Smuzhiyun 	mutex_unlock(&monc->mutex);
933*4882a593Smuzhiyun 
934*4882a593Smuzhiyun 	ret = wait_generic_request(req);
935*4882a593Smuzhiyun out:
936*4882a593Smuzhiyun 	put_generic_request(req);
937*4882a593Smuzhiyun 	return ret;
938*4882a593Smuzhiyun }
939*4882a593Smuzhiyun 
940*4882a593Smuzhiyun static __printf(2, 3)
do_mon_command(struct ceph_mon_client * monc,const char * fmt,...)941*4882a593Smuzhiyun int do_mon_command(struct ceph_mon_client *monc, const char *fmt, ...)
942*4882a593Smuzhiyun {
943*4882a593Smuzhiyun 	va_list ap;
944*4882a593Smuzhiyun 	int ret;
945*4882a593Smuzhiyun 
946*4882a593Smuzhiyun 	va_start(ap, fmt);
947*4882a593Smuzhiyun 	ret = do_mon_command_vargs(monc, fmt, ap);
948*4882a593Smuzhiyun 	va_end(ap);
949*4882a593Smuzhiyun 	return ret;
950*4882a593Smuzhiyun }
951*4882a593Smuzhiyun 
ceph_monc_blocklist_add(struct ceph_mon_client * monc,struct ceph_entity_addr * client_addr)952*4882a593Smuzhiyun int ceph_monc_blocklist_add(struct ceph_mon_client *monc,
953*4882a593Smuzhiyun 			    struct ceph_entity_addr *client_addr)
954*4882a593Smuzhiyun {
955*4882a593Smuzhiyun 	int ret;
956*4882a593Smuzhiyun 
957*4882a593Smuzhiyun 	ret = do_mon_command(monc,
958*4882a593Smuzhiyun 			     "{ \"prefix\": \"osd blocklist\", \
959*4882a593Smuzhiyun 				\"blocklistop\": \"add\", \
960*4882a593Smuzhiyun 				\"addr\": \"%pISpc/%u\" }",
961*4882a593Smuzhiyun 			     &client_addr->in_addr,
962*4882a593Smuzhiyun 			     le32_to_cpu(client_addr->nonce));
963*4882a593Smuzhiyun 	if (ret == -EINVAL) {
964*4882a593Smuzhiyun 		/*
965*4882a593Smuzhiyun 		 * The monitor returns EINVAL on an unrecognized command.
966*4882a593Smuzhiyun 		 * Try the legacy command -- it is exactly the same except
967*4882a593Smuzhiyun 		 * for the name.
968*4882a593Smuzhiyun 		 */
969*4882a593Smuzhiyun 		ret = do_mon_command(monc,
970*4882a593Smuzhiyun 				     "{ \"prefix\": \"osd blacklist\", \
971*4882a593Smuzhiyun 					\"blacklistop\": \"add\", \
972*4882a593Smuzhiyun 					\"addr\": \"%pISpc/%u\" }",
973*4882a593Smuzhiyun 				     &client_addr->in_addr,
974*4882a593Smuzhiyun 				     le32_to_cpu(client_addr->nonce));
975*4882a593Smuzhiyun 	}
976*4882a593Smuzhiyun 	if (ret)
977*4882a593Smuzhiyun 		return ret;
978*4882a593Smuzhiyun 
979*4882a593Smuzhiyun 	/*
980*4882a593Smuzhiyun 	 * Make sure we have the osdmap that includes the blocklist
981*4882a593Smuzhiyun 	 * entry.  This is needed to ensure that the OSDs pick up the
982*4882a593Smuzhiyun 	 * new blocklist before processing any future requests from
983*4882a593Smuzhiyun 	 * this client.
984*4882a593Smuzhiyun 	 */
985*4882a593Smuzhiyun 	return ceph_wait_for_latest_osdmap(monc->client, 0);
986*4882a593Smuzhiyun }
987*4882a593Smuzhiyun EXPORT_SYMBOL(ceph_monc_blocklist_add);
988*4882a593Smuzhiyun 
989*4882a593Smuzhiyun /*
990*4882a593Smuzhiyun  * Resend pending generic requests.
991*4882a593Smuzhiyun  */
__resend_generic_request(struct ceph_mon_client * monc)992*4882a593Smuzhiyun static void __resend_generic_request(struct ceph_mon_client *monc)
993*4882a593Smuzhiyun {
994*4882a593Smuzhiyun 	struct ceph_mon_generic_request *req;
995*4882a593Smuzhiyun 	struct rb_node *p;
996*4882a593Smuzhiyun 
997*4882a593Smuzhiyun 	for (p = rb_first(&monc->generic_request_tree); p; p = rb_next(p)) {
998*4882a593Smuzhiyun 		req = rb_entry(p, struct ceph_mon_generic_request, node);
999*4882a593Smuzhiyun 		ceph_msg_revoke(req->request);
1000*4882a593Smuzhiyun 		ceph_msg_revoke_incoming(req->reply);
1001*4882a593Smuzhiyun 		ceph_con_send(&monc->con, ceph_msg_get(req->request));
1002*4882a593Smuzhiyun 	}
1003*4882a593Smuzhiyun }
1004*4882a593Smuzhiyun 
1005*4882a593Smuzhiyun /*
1006*4882a593Smuzhiyun  * Delayed work.  If we haven't mounted yet, retry.  Otherwise,
1007*4882a593Smuzhiyun  * renew/retry subscription as needed (in case it is timing out, or we
1008*4882a593Smuzhiyun  * got an ENOMEM).  And keep the monitor connection alive.
1009*4882a593Smuzhiyun  */
delayed_work(struct work_struct * work)1010*4882a593Smuzhiyun static void delayed_work(struct work_struct *work)
1011*4882a593Smuzhiyun {
1012*4882a593Smuzhiyun 	struct ceph_mon_client *monc =
1013*4882a593Smuzhiyun 		container_of(work, struct ceph_mon_client, delayed_work.work);
1014*4882a593Smuzhiyun 
1015*4882a593Smuzhiyun 	dout("monc delayed_work\n");
1016*4882a593Smuzhiyun 	mutex_lock(&monc->mutex);
1017*4882a593Smuzhiyun 	if (monc->hunting) {
1018*4882a593Smuzhiyun 		dout("%s continuing hunt\n", __func__);
1019*4882a593Smuzhiyun 		reopen_session(monc);
1020*4882a593Smuzhiyun 	} else {
1021*4882a593Smuzhiyun 		int is_auth = ceph_auth_is_authenticated(monc->auth);
1022*4882a593Smuzhiyun 		if (ceph_con_keepalive_expired(&monc->con,
1023*4882a593Smuzhiyun 					       CEPH_MONC_PING_TIMEOUT)) {
1024*4882a593Smuzhiyun 			dout("monc keepalive timeout\n");
1025*4882a593Smuzhiyun 			is_auth = 0;
1026*4882a593Smuzhiyun 			reopen_session(monc);
1027*4882a593Smuzhiyun 		}
1028*4882a593Smuzhiyun 
1029*4882a593Smuzhiyun 		if (!monc->hunting) {
1030*4882a593Smuzhiyun 			ceph_con_keepalive(&monc->con);
1031*4882a593Smuzhiyun 			__validate_auth(monc);
1032*4882a593Smuzhiyun 			un_backoff(monc);
1033*4882a593Smuzhiyun 		}
1034*4882a593Smuzhiyun 
1035*4882a593Smuzhiyun 		if (is_auth &&
1036*4882a593Smuzhiyun 		    !(monc->con.peer_features & CEPH_FEATURE_MON_STATEFUL_SUB)) {
1037*4882a593Smuzhiyun 			unsigned long now = jiffies;
1038*4882a593Smuzhiyun 
1039*4882a593Smuzhiyun 			dout("%s renew subs? now %lu renew after %lu\n",
1040*4882a593Smuzhiyun 			     __func__, now, monc->sub_renew_after);
1041*4882a593Smuzhiyun 			if (time_after_eq(now, monc->sub_renew_after))
1042*4882a593Smuzhiyun 				__send_subscribe(monc);
1043*4882a593Smuzhiyun 		}
1044*4882a593Smuzhiyun 	}
1045*4882a593Smuzhiyun 	__schedule_delayed(monc);
1046*4882a593Smuzhiyun 	mutex_unlock(&monc->mutex);
1047*4882a593Smuzhiyun }
1048*4882a593Smuzhiyun 
1049*4882a593Smuzhiyun /*
1050*4882a593Smuzhiyun  * On startup, we build a temporary monmap populated with the IPs
1051*4882a593Smuzhiyun  * provided by mount(2).
1052*4882a593Smuzhiyun  */
build_initial_monmap(struct ceph_mon_client * monc)1053*4882a593Smuzhiyun static int build_initial_monmap(struct ceph_mon_client *monc)
1054*4882a593Smuzhiyun {
1055*4882a593Smuzhiyun 	struct ceph_options *opt = monc->client->options;
1056*4882a593Smuzhiyun 	struct ceph_entity_addr *mon_addr = opt->mon_addr;
1057*4882a593Smuzhiyun 	int num_mon = opt->num_mon;
1058*4882a593Smuzhiyun 	int i;
1059*4882a593Smuzhiyun 
1060*4882a593Smuzhiyun 	/* build initial monmap */
1061*4882a593Smuzhiyun 	monc->monmap = kzalloc(struct_size(monc->monmap, mon_inst, num_mon),
1062*4882a593Smuzhiyun 			       GFP_KERNEL);
1063*4882a593Smuzhiyun 	if (!monc->monmap)
1064*4882a593Smuzhiyun 		return -ENOMEM;
1065*4882a593Smuzhiyun 	for (i = 0; i < num_mon; i++) {
1066*4882a593Smuzhiyun 		monc->monmap->mon_inst[i].addr = mon_addr[i];
1067*4882a593Smuzhiyun 		monc->monmap->mon_inst[i].addr.nonce = 0;
1068*4882a593Smuzhiyun 		monc->monmap->mon_inst[i].name.type =
1069*4882a593Smuzhiyun 			CEPH_ENTITY_TYPE_MON;
1070*4882a593Smuzhiyun 		monc->monmap->mon_inst[i].name.num = cpu_to_le64(i);
1071*4882a593Smuzhiyun 	}
1072*4882a593Smuzhiyun 	monc->monmap->num_mon = num_mon;
1073*4882a593Smuzhiyun 	return 0;
1074*4882a593Smuzhiyun }
1075*4882a593Smuzhiyun 
ceph_monc_init(struct ceph_mon_client * monc,struct ceph_client * cl)1076*4882a593Smuzhiyun int ceph_monc_init(struct ceph_mon_client *monc, struct ceph_client *cl)
1077*4882a593Smuzhiyun {
1078*4882a593Smuzhiyun 	int err = 0;
1079*4882a593Smuzhiyun 
1080*4882a593Smuzhiyun 	dout("init\n");
1081*4882a593Smuzhiyun 	memset(monc, 0, sizeof(*monc));
1082*4882a593Smuzhiyun 	monc->client = cl;
1083*4882a593Smuzhiyun 	monc->monmap = NULL;
1084*4882a593Smuzhiyun 	mutex_init(&monc->mutex);
1085*4882a593Smuzhiyun 
1086*4882a593Smuzhiyun 	err = build_initial_monmap(monc);
1087*4882a593Smuzhiyun 	if (err)
1088*4882a593Smuzhiyun 		goto out;
1089*4882a593Smuzhiyun 
1090*4882a593Smuzhiyun 	/* connection */
1091*4882a593Smuzhiyun 	/* authentication */
1092*4882a593Smuzhiyun 	monc->auth = ceph_auth_init(cl->options->name,
1093*4882a593Smuzhiyun 				    cl->options->key);
1094*4882a593Smuzhiyun 	if (IS_ERR(monc->auth)) {
1095*4882a593Smuzhiyun 		err = PTR_ERR(monc->auth);
1096*4882a593Smuzhiyun 		goto out_monmap;
1097*4882a593Smuzhiyun 	}
1098*4882a593Smuzhiyun 	monc->auth->want_keys =
1099*4882a593Smuzhiyun 		CEPH_ENTITY_TYPE_AUTH | CEPH_ENTITY_TYPE_MON |
1100*4882a593Smuzhiyun 		CEPH_ENTITY_TYPE_OSD | CEPH_ENTITY_TYPE_MDS;
1101*4882a593Smuzhiyun 
1102*4882a593Smuzhiyun 	/* msgs */
1103*4882a593Smuzhiyun 	err = -ENOMEM;
1104*4882a593Smuzhiyun 	monc->m_subscribe_ack = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE_ACK,
1105*4882a593Smuzhiyun 				     sizeof(struct ceph_mon_subscribe_ack),
1106*4882a593Smuzhiyun 				     GFP_KERNEL, true);
1107*4882a593Smuzhiyun 	if (!monc->m_subscribe_ack)
1108*4882a593Smuzhiyun 		goto out_auth;
1109*4882a593Smuzhiyun 
1110*4882a593Smuzhiyun 	monc->m_subscribe = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 128,
1111*4882a593Smuzhiyun 					 GFP_KERNEL, true);
1112*4882a593Smuzhiyun 	if (!monc->m_subscribe)
1113*4882a593Smuzhiyun 		goto out_subscribe_ack;
1114*4882a593Smuzhiyun 
1115*4882a593Smuzhiyun 	monc->m_auth_reply = ceph_msg_new(CEPH_MSG_AUTH_REPLY, 4096,
1116*4882a593Smuzhiyun 					  GFP_KERNEL, true);
1117*4882a593Smuzhiyun 	if (!monc->m_auth_reply)
1118*4882a593Smuzhiyun 		goto out_subscribe;
1119*4882a593Smuzhiyun 
1120*4882a593Smuzhiyun 	monc->m_auth = ceph_msg_new(CEPH_MSG_AUTH, 4096, GFP_KERNEL, true);
1121*4882a593Smuzhiyun 	monc->pending_auth = 0;
1122*4882a593Smuzhiyun 	if (!monc->m_auth)
1123*4882a593Smuzhiyun 		goto out_auth_reply;
1124*4882a593Smuzhiyun 
1125*4882a593Smuzhiyun 	ceph_con_init(&monc->con, monc, &mon_con_ops,
1126*4882a593Smuzhiyun 		      &monc->client->msgr);
1127*4882a593Smuzhiyun 
1128*4882a593Smuzhiyun 	monc->cur_mon = -1;
1129*4882a593Smuzhiyun 	monc->had_a_connection = false;
1130*4882a593Smuzhiyun 	monc->hunt_mult = 1;
1131*4882a593Smuzhiyun 
1132*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&monc->delayed_work, delayed_work);
1133*4882a593Smuzhiyun 	monc->generic_request_tree = RB_ROOT;
1134*4882a593Smuzhiyun 	monc->last_tid = 0;
1135*4882a593Smuzhiyun 
1136*4882a593Smuzhiyun 	monc->fs_cluster_id = CEPH_FS_CLUSTER_ID_NONE;
1137*4882a593Smuzhiyun 
1138*4882a593Smuzhiyun 	return 0;
1139*4882a593Smuzhiyun 
1140*4882a593Smuzhiyun out_auth_reply:
1141*4882a593Smuzhiyun 	ceph_msg_put(monc->m_auth_reply);
1142*4882a593Smuzhiyun out_subscribe:
1143*4882a593Smuzhiyun 	ceph_msg_put(monc->m_subscribe);
1144*4882a593Smuzhiyun out_subscribe_ack:
1145*4882a593Smuzhiyun 	ceph_msg_put(monc->m_subscribe_ack);
1146*4882a593Smuzhiyun out_auth:
1147*4882a593Smuzhiyun 	ceph_auth_destroy(monc->auth);
1148*4882a593Smuzhiyun out_monmap:
1149*4882a593Smuzhiyun 	kfree(monc->monmap);
1150*4882a593Smuzhiyun out:
1151*4882a593Smuzhiyun 	return err;
1152*4882a593Smuzhiyun }
1153*4882a593Smuzhiyun EXPORT_SYMBOL(ceph_monc_init);
1154*4882a593Smuzhiyun 
ceph_monc_stop(struct ceph_mon_client * monc)1155*4882a593Smuzhiyun void ceph_monc_stop(struct ceph_mon_client *monc)
1156*4882a593Smuzhiyun {
1157*4882a593Smuzhiyun 	dout("stop\n");
1158*4882a593Smuzhiyun 	cancel_delayed_work_sync(&monc->delayed_work);
1159*4882a593Smuzhiyun 
1160*4882a593Smuzhiyun 	mutex_lock(&monc->mutex);
1161*4882a593Smuzhiyun 	__close_session(monc);
1162*4882a593Smuzhiyun 	monc->cur_mon = -1;
1163*4882a593Smuzhiyun 	mutex_unlock(&monc->mutex);
1164*4882a593Smuzhiyun 
1165*4882a593Smuzhiyun 	/*
1166*4882a593Smuzhiyun 	 * flush msgr queue before we destroy ourselves to ensure that:
1167*4882a593Smuzhiyun 	 *  - any work that references our embedded con is finished.
1168*4882a593Smuzhiyun 	 *  - any osd_client or other work that may reference an authorizer
1169*4882a593Smuzhiyun 	 *    finishes before we shut down the auth subsystem.
1170*4882a593Smuzhiyun 	 */
1171*4882a593Smuzhiyun 	ceph_msgr_flush();
1172*4882a593Smuzhiyun 
1173*4882a593Smuzhiyun 	ceph_auth_destroy(monc->auth);
1174*4882a593Smuzhiyun 
1175*4882a593Smuzhiyun 	WARN_ON(!RB_EMPTY_ROOT(&monc->generic_request_tree));
1176*4882a593Smuzhiyun 
1177*4882a593Smuzhiyun 	ceph_msg_put(monc->m_auth);
1178*4882a593Smuzhiyun 	ceph_msg_put(monc->m_auth_reply);
1179*4882a593Smuzhiyun 	ceph_msg_put(monc->m_subscribe);
1180*4882a593Smuzhiyun 	ceph_msg_put(monc->m_subscribe_ack);
1181*4882a593Smuzhiyun 
1182*4882a593Smuzhiyun 	kfree(monc->monmap);
1183*4882a593Smuzhiyun }
1184*4882a593Smuzhiyun EXPORT_SYMBOL(ceph_monc_stop);
1185*4882a593Smuzhiyun 
finish_hunting(struct ceph_mon_client * monc)1186*4882a593Smuzhiyun static void finish_hunting(struct ceph_mon_client *monc)
1187*4882a593Smuzhiyun {
1188*4882a593Smuzhiyun 	if (monc->hunting) {
1189*4882a593Smuzhiyun 		dout("%s found mon%d\n", __func__, monc->cur_mon);
1190*4882a593Smuzhiyun 		monc->hunting = false;
1191*4882a593Smuzhiyun 		monc->had_a_connection = true;
1192*4882a593Smuzhiyun 		un_backoff(monc);
1193*4882a593Smuzhiyun 		__schedule_delayed(monc);
1194*4882a593Smuzhiyun 	}
1195*4882a593Smuzhiyun }
1196*4882a593Smuzhiyun 
handle_auth_reply(struct ceph_mon_client * monc,struct ceph_msg * msg)1197*4882a593Smuzhiyun static void handle_auth_reply(struct ceph_mon_client *monc,
1198*4882a593Smuzhiyun 			      struct ceph_msg *msg)
1199*4882a593Smuzhiyun {
1200*4882a593Smuzhiyun 	int ret;
1201*4882a593Smuzhiyun 	int was_auth = 0;
1202*4882a593Smuzhiyun 
1203*4882a593Smuzhiyun 	mutex_lock(&monc->mutex);
1204*4882a593Smuzhiyun 	was_auth = ceph_auth_is_authenticated(monc->auth);
1205*4882a593Smuzhiyun 	monc->pending_auth = 0;
1206*4882a593Smuzhiyun 	ret = ceph_handle_auth_reply(monc->auth, msg->front.iov_base,
1207*4882a593Smuzhiyun 				     msg->front.iov_len,
1208*4882a593Smuzhiyun 				     monc->m_auth->front.iov_base,
1209*4882a593Smuzhiyun 				     monc->m_auth->front_alloc_len);
1210*4882a593Smuzhiyun 	if (ret > 0) {
1211*4882a593Smuzhiyun 		__send_prepared_auth_request(monc, ret);
1212*4882a593Smuzhiyun 		goto out;
1213*4882a593Smuzhiyun 	}
1214*4882a593Smuzhiyun 
1215*4882a593Smuzhiyun 	finish_hunting(monc);
1216*4882a593Smuzhiyun 
1217*4882a593Smuzhiyun 	if (ret < 0) {
1218*4882a593Smuzhiyun 		monc->client->auth_err = ret;
1219*4882a593Smuzhiyun 	} else if (!was_auth && ceph_auth_is_authenticated(monc->auth)) {
1220*4882a593Smuzhiyun 		dout("authenticated, starting session\n");
1221*4882a593Smuzhiyun 
1222*4882a593Smuzhiyun 		monc->client->msgr.inst.name.type = CEPH_ENTITY_TYPE_CLIENT;
1223*4882a593Smuzhiyun 		monc->client->msgr.inst.name.num =
1224*4882a593Smuzhiyun 					cpu_to_le64(monc->auth->global_id);
1225*4882a593Smuzhiyun 
1226*4882a593Smuzhiyun 		__send_subscribe(monc);
1227*4882a593Smuzhiyun 		__resend_generic_request(monc);
1228*4882a593Smuzhiyun 
1229*4882a593Smuzhiyun 		pr_info("mon%d %s session established\n", monc->cur_mon,
1230*4882a593Smuzhiyun 			ceph_pr_addr(&monc->con.peer_addr));
1231*4882a593Smuzhiyun 	}
1232*4882a593Smuzhiyun 
1233*4882a593Smuzhiyun out:
1234*4882a593Smuzhiyun 	mutex_unlock(&monc->mutex);
1235*4882a593Smuzhiyun 	if (monc->client->auth_err < 0)
1236*4882a593Smuzhiyun 		wake_up_all(&monc->client->auth_wq);
1237*4882a593Smuzhiyun }
1238*4882a593Smuzhiyun 
__validate_auth(struct ceph_mon_client * monc)1239*4882a593Smuzhiyun static int __validate_auth(struct ceph_mon_client *monc)
1240*4882a593Smuzhiyun {
1241*4882a593Smuzhiyun 	int ret;
1242*4882a593Smuzhiyun 
1243*4882a593Smuzhiyun 	if (monc->pending_auth)
1244*4882a593Smuzhiyun 		return 0;
1245*4882a593Smuzhiyun 
1246*4882a593Smuzhiyun 	ret = ceph_build_auth(monc->auth, monc->m_auth->front.iov_base,
1247*4882a593Smuzhiyun 			      monc->m_auth->front_alloc_len);
1248*4882a593Smuzhiyun 	if (ret <= 0)
1249*4882a593Smuzhiyun 		return ret; /* either an error, or no need to authenticate */
1250*4882a593Smuzhiyun 	__send_prepared_auth_request(monc, ret);
1251*4882a593Smuzhiyun 	return 0;
1252*4882a593Smuzhiyun }
1253*4882a593Smuzhiyun 
ceph_monc_validate_auth(struct ceph_mon_client * monc)1254*4882a593Smuzhiyun int ceph_monc_validate_auth(struct ceph_mon_client *monc)
1255*4882a593Smuzhiyun {
1256*4882a593Smuzhiyun 	int ret;
1257*4882a593Smuzhiyun 
1258*4882a593Smuzhiyun 	mutex_lock(&monc->mutex);
1259*4882a593Smuzhiyun 	ret = __validate_auth(monc);
1260*4882a593Smuzhiyun 	mutex_unlock(&monc->mutex);
1261*4882a593Smuzhiyun 	return ret;
1262*4882a593Smuzhiyun }
1263*4882a593Smuzhiyun EXPORT_SYMBOL(ceph_monc_validate_auth);
1264*4882a593Smuzhiyun 
1265*4882a593Smuzhiyun /*
1266*4882a593Smuzhiyun  * handle incoming message
1267*4882a593Smuzhiyun  */
dispatch(struct ceph_connection * con,struct ceph_msg * msg)1268*4882a593Smuzhiyun static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1269*4882a593Smuzhiyun {
1270*4882a593Smuzhiyun 	struct ceph_mon_client *monc = con->private;
1271*4882a593Smuzhiyun 	int type = le16_to_cpu(msg->hdr.type);
1272*4882a593Smuzhiyun 
1273*4882a593Smuzhiyun 	switch (type) {
1274*4882a593Smuzhiyun 	case CEPH_MSG_AUTH_REPLY:
1275*4882a593Smuzhiyun 		handle_auth_reply(monc, msg);
1276*4882a593Smuzhiyun 		break;
1277*4882a593Smuzhiyun 
1278*4882a593Smuzhiyun 	case CEPH_MSG_MON_SUBSCRIBE_ACK:
1279*4882a593Smuzhiyun 		handle_subscribe_ack(monc, msg);
1280*4882a593Smuzhiyun 		break;
1281*4882a593Smuzhiyun 
1282*4882a593Smuzhiyun 	case CEPH_MSG_STATFS_REPLY:
1283*4882a593Smuzhiyun 		handle_statfs_reply(monc, msg);
1284*4882a593Smuzhiyun 		break;
1285*4882a593Smuzhiyun 
1286*4882a593Smuzhiyun 	case CEPH_MSG_MON_GET_VERSION_REPLY:
1287*4882a593Smuzhiyun 		handle_get_version_reply(monc, msg);
1288*4882a593Smuzhiyun 		break;
1289*4882a593Smuzhiyun 
1290*4882a593Smuzhiyun 	case CEPH_MSG_MON_COMMAND_ACK:
1291*4882a593Smuzhiyun 		handle_command_ack(monc, msg);
1292*4882a593Smuzhiyun 		break;
1293*4882a593Smuzhiyun 
1294*4882a593Smuzhiyun 	case CEPH_MSG_MON_MAP:
1295*4882a593Smuzhiyun 		ceph_monc_handle_map(monc, msg);
1296*4882a593Smuzhiyun 		break;
1297*4882a593Smuzhiyun 
1298*4882a593Smuzhiyun 	case CEPH_MSG_OSD_MAP:
1299*4882a593Smuzhiyun 		ceph_osdc_handle_map(&monc->client->osdc, msg);
1300*4882a593Smuzhiyun 		break;
1301*4882a593Smuzhiyun 
1302*4882a593Smuzhiyun 	default:
1303*4882a593Smuzhiyun 		/* can the chained handler handle it? */
1304*4882a593Smuzhiyun 		if (monc->client->extra_mon_dispatch &&
1305*4882a593Smuzhiyun 		    monc->client->extra_mon_dispatch(monc->client, msg) == 0)
1306*4882a593Smuzhiyun 			break;
1307*4882a593Smuzhiyun 
1308*4882a593Smuzhiyun 		pr_err("received unknown message type %d %s\n", type,
1309*4882a593Smuzhiyun 		       ceph_msg_type_name(type));
1310*4882a593Smuzhiyun 	}
1311*4882a593Smuzhiyun 	ceph_msg_put(msg);
1312*4882a593Smuzhiyun }
1313*4882a593Smuzhiyun 
1314*4882a593Smuzhiyun /*
1315*4882a593Smuzhiyun  * Allocate memory for incoming message
1316*4882a593Smuzhiyun  */
mon_alloc_msg(struct ceph_connection * con,struct ceph_msg_header * hdr,int * skip)1317*4882a593Smuzhiyun static struct ceph_msg *mon_alloc_msg(struct ceph_connection *con,
1318*4882a593Smuzhiyun 				      struct ceph_msg_header *hdr,
1319*4882a593Smuzhiyun 				      int *skip)
1320*4882a593Smuzhiyun {
1321*4882a593Smuzhiyun 	struct ceph_mon_client *monc = con->private;
1322*4882a593Smuzhiyun 	int type = le16_to_cpu(hdr->type);
1323*4882a593Smuzhiyun 	int front_len = le32_to_cpu(hdr->front_len);
1324*4882a593Smuzhiyun 	struct ceph_msg *m = NULL;
1325*4882a593Smuzhiyun 
1326*4882a593Smuzhiyun 	*skip = 0;
1327*4882a593Smuzhiyun 
1328*4882a593Smuzhiyun 	switch (type) {
1329*4882a593Smuzhiyun 	case CEPH_MSG_MON_SUBSCRIBE_ACK:
1330*4882a593Smuzhiyun 		m = ceph_msg_get(monc->m_subscribe_ack);
1331*4882a593Smuzhiyun 		break;
1332*4882a593Smuzhiyun 	case CEPH_MSG_STATFS_REPLY:
1333*4882a593Smuzhiyun 	case CEPH_MSG_MON_COMMAND_ACK:
1334*4882a593Smuzhiyun 		return get_generic_reply(con, hdr, skip);
1335*4882a593Smuzhiyun 	case CEPH_MSG_AUTH_REPLY:
1336*4882a593Smuzhiyun 		m = ceph_msg_get(monc->m_auth_reply);
1337*4882a593Smuzhiyun 		break;
1338*4882a593Smuzhiyun 	case CEPH_MSG_MON_GET_VERSION_REPLY:
1339*4882a593Smuzhiyun 		if (le64_to_cpu(hdr->tid) != 0)
1340*4882a593Smuzhiyun 			return get_generic_reply(con, hdr, skip);
1341*4882a593Smuzhiyun 
1342*4882a593Smuzhiyun 		/*
1343*4882a593Smuzhiyun 		 * Older OSDs don't set reply tid even if the orignal
1344*4882a593Smuzhiyun 		 * request had a non-zero tid.  Work around this weirdness
1345*4882a593Smuzhiyun 		 * by allocating a new message.
1346*4882a593Smuzhiyun 		 */
1347*4882a593Smuzhiyun 		fallthrough;
1348*4882a593Smuzhiyun 	case CEPH_MSG_MON_MAP:
1349*4882a593Smuzhiyun 	case CEPH_MSG_MDS_MAP:
1350*4882a593Smuzhiyun 	case CEPH_MSG_OSD_MAP:
1351*4882a593Smuzhiyun 	case CEPH_MSG_FS_MAP_USER:
1352*4882a593Smuzhiyun 		m = ceph_msg_new(type, front_len, GFP_NOFS, false);
1353*4882a593Smuzhiyun 		if (!m)
1354*4882a593Smuzhiyun 			return NULL;	/* ENOMEM--return skip == 0 */
1355*4882a593Smuzhiyun 		break;
1356*4882a593Smuzhiyun 	}
1357*4882a593Smuzhiyun 
1358*4882a593Smuzhiyun 	if (!m) {
1359*4882a593Smuzhiyun 		pr_info("alloc_msg unknown type %d\n", type);
1360*4882a593Smuzhiyun 		*skip = 1;
1361*4882a593Smuzhiyun 	} else if (front_len > m->front_alloc_len) {
1362*4882a593Smuzhiyun 		pr_warn("mon_alloc_msg front %d > prealloc %d (%u#%llu)\n",
1363*4882a593Smuzhiyun 			front_len, m->front_alloc_len,
1364*4882a593Smuzhiyun 			(unsigned int)con->peer_name.type,
1365*4882a593Smuzhiyun 			le64_to_cpu(con->peer_name.num));
1366*4882a593Smuzhiyun 		ceph_msg_put(m);
1367*4882a593Smuzhiyun 		m = ceph_msg_new(type, front_len, GFP_NOFS, false);
1368*4882a593Smuzhiyun 	}
1369*4882a593Smuzhiyun 
1370*4882a593Smuzhiyun 	return m;
1371*4882a593Smuzhiyun }
1372*4882a593Smuzhiyun 
1373*4882a593Smuzhiyun /*
1374*4882a593Smuzhiyun  * If the monitor connection resets, pick a new monitor and resubmit
1375*4882a593Smuzhiyun  * any pending requests.
1376*4882a593Smuzhiyun  */
mon_fault(struct ceph_connection * con)1377*4882a593Smuzhiyun static void mon_fault(struct ceph_connection *con)
1378*4882a593Smuzhiyun {
1379*4882a593Smuzhiyun 	struct ceph_mon_client *monc = con->private;
1380*4882a593Smuzhiyun 
1381*4882a593Smuzhiyun 	mutex_lock(&monc->mutex);
1382*4882a593Smuzhiyun 	dout("%s mon%d\n", __func__, monc->cur_mon);
1383*4882a593Smuzhiyun 	if (monc->cur_mon >= 0) {
1384*4882a593Smuzhiyun 		if (!monc->hunting) {
1385*4882a593Smuzhiyun 			dout("%s hunting for new mon\n", __func__);
1386*4882a593Smuzhiyun 			reopen_session(monc);
1387*4882a593Smuzhiyun 			__schedule_delayed(monc);
1388*4882a593Smuzhiyun 		} else {
1389*4882a593Smuzhiyun 			dout("%s already hunting\n", __func__);
1390*4882a593Smuzhiyun 		}
1391*4882a593Smuzhiyun 	}
1392*4882a593Smuzhiyun 	mutex_unlock(&monc->mutex);
1393*4882a593Smuzhiyun }
1394*4882a593Smuzhiyun 
1395*4882a593Smuzhiyun /*
1396*4882a593Smuzhiyun  * We can ignore refcounting on the connection struct, as all references
1397*4882a593Smuzhiyun  * will come from the messenger workqueue, which is drained prior to
1398*4882a593Smuzhiyun  * mon_client destruction.
1399*4882a593Smuzhiyun  */
con_get(struct ceph_connection * con)1400*4882a593Smuzhiyun static struct ceph_connection *con_get(struct ceph_connection *con)
1401*4882a593Smuzhiyun {
1402*4882a593Smuzhiyun 	return con;
1403*4882a593Smuzhiyun }
1404*4882a593Smuzhiyun 
con_put(struct ceph_connection * con)1405*4882a593Smuzhiyun static void con_put(struct ceph_connection *con)
1406*4882a593Smuzhiyun {
1407*4882a593Smuzhiyun }
1408*4882a593Smuzhiyun 
1409*4882a593Smuzhiyun static const struct ceph_connection_operations mon_con_ops = {
1410*4882a593Smuzhiyun 	.get = con_get,
1411*4882a593Smuzhiyun 	.put = con_put,
1412*4882a593Smuzhiyun 	.dispatch = dispatch,
1413*4882a593Smuzhiyun 	.fault = mon_fault,
1414*4882a593Smuzhiyun 	.alloc_msg = mon_alloc_msg,
1415*4882a593Smuzhiyun };
1416