blob: 250f11f786092d902b52da5af9a228d9786acf62 [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weilba75bb92009-10-06 11:31:11 -07002
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003#include <linux/module.h>
Sage Weilba75bb92009-10-06 11:31:11 -07004#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09005#include <linux/slab.h>
Sage Weilba75bb92009-10-06 11:31:11 -07006#include <linux/random.h>
7#include <linux/sched.h>
8
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07009#include <linux/ceph/mon_client.h>
10#include <linux/ceph/libceph.h>
Sage Weilab434b62012-01-13 22:22:03 -080011#include <linux/ceph/debugfs.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070012#include <linux/ceph/decode.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070013#include <linux/ceph/auth.h>
Sage Weilba75bb92009-10-06 11:31:11 -070014
15/*
16 * Interact with Ceph monitor cluster. Handle requests for new map
17 * versions, and periodically resend as needed. Also implement
18 * statfs() and umount().
19 *
20 * A small cluster of Ceph "monitors" are responsible for managing critical
21 * cluster configuration and state information. An odd number (e.g., 3, 5)
22 * of cmon daemons use a modified version of the Paxos part-time parliament
23 * algorithm to manage the MDS map (mds cluster membership), OSD map, and
24 * list of clients who have mounted the file system.
25 *
26 * We maintain an open, active session with a monitor at all times in order to
27 * receive timely MDSMap updates. We periodically send a keepalive byte on the
28 * TCP socket to ensure we detect a failure. If the connection does break, we
29 * randomly hunt for a new monitor. Once the connection is reestablished, we
30 * resend any outstanding requests.
31 */
32
Tobias Klauser9e327892010-05-20 10:40:19 +020033static const struct ceph_connection_operations mon_con_ops;
Sage Weilba75bb92009-10-06 11:31:11 -070034
Sage Weil9bd2e6f2010-02-02 16:21:06 -080035static int __validate_auth(struct ceph_mon_client *monc);
36
Sage Weilba75bb92009-10-06 11:31:11 -070037/*
38 * Decode a monmap blob (e.g., during mount).
39 */
40struct ceph_monmap *ceph_monmap_decode(void *p, void *end)
41{
42 struct ceph_monmap *m = NULL;
43 int i, err = -EINVAL;
44 struct ceph_fsid fsid;
45 u32 epoch, num_mon;
Sage Weil4e7a5dc2009-11-18 16:19:57 -080046 u32 len;
47
48 ceph_decode_32_safe(&p, end, len, bad);
49 ceph_decode_need(&p, end, len, bad);
Sage Weilba75bb92009-10-06 11:31:11 -070050
51 dout("monmap_decode %p %p len %d\n", p, end, (int)(end-p));
Ilya Dryomovf3b4e552017-05-19 11:59:22 +020052 p += sizeof(u16); /* skip version */
Sage Weilba75bb92009-10-06 11:31:11 -070053
54 ceph_decode_need(&p, end, sizeof(fsid) + 2*sizeof(u32), bad);
55 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weilc89136e2009-10-14 09:59:09 -070056 epoch = ceph_decode_32(&p);
Sage Weilba75bb92009-10-06 11:31:11 -070057
Sage Weilc89136e2009-10-14 09:59:09 -070058 num_mon = ceph_decode_32(&p);
Sage Weilba75bb92009-10-06 11:31:11 -070059 ceph_decode_need(&p, end, num_mon*sizeof(m->mon_inst[0]), bad);
60
61 if (num_mon >= CEPH_MAX_MON)
62 goto bad;
63 m = kmalloc(sizeof(*m) + sizeof(m->mon_inst[0])*num_mon, GFP_NOFS);
64 if (m == NULL)
65 return ERR_PTR(-ENOMEM);
66 m->fsid = fsid;
67 m->epoch = epoch;
68 m->num_mon = num_mon;
69 ceph_decode_copy(&p, m->mon_inst, num_mon*sizeof(m->mon_inst[0]));
Sage Weil63f2d212009-11-03 15:17:56 -080070 for (i = 0; i < num_mon; i++)
71 ceph_decode_addr(&m->mon_inst[i].addr);
Sage Weilba75bb92009-10-06 11:31:11 -070072
Sage Weilba75bb92009-10-06 11:31:11 -070073 dout("monmap_decode epoch %d, num_mon %d\n", m->epoch,
74 m->num_mon);
75 for (i = 0; i < m->num_mon; i++)
76 dout("monmap_decode mon%d is %s\n", i,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070077 ceph_pr_addr(&m->mon_inst[i].addr.in_addr));
Sage Weilba75bb92009-10-06 11:31:11 -070078 return m;
79
80bad:
81 dout("monmap_decode failed with %d\n", err);
82 kfree(m);
83 return ERR_PTR(err);
84}
85
86/*
87 * return true if *addr is included in the monmap.
88 */
89int ceph_monmap_contains(struct ceph_monmap *m, struct ceph_entity_addr *addr)
90{
91 int i;
92
93 for (i = 0; i < m->num_mon; i++)
Sage Weil103e2d32010-01-07 16:12:36 -080094 if (memcmp(addr, &m->mon_inst[i].addr, sizeof(*addr)) == 0)
Sage Weilba75bb92009-10-06 11:31:11 -070095 return 1;
96 return 0;
97}
98
99/*
Sage Weil5ce6e9d2010-02-15 16:22:28 -0800100 * Send an auth request.
101 */
102static void __send_prepared_auth_request(struct ceph_mon_client *monc, int len)
103{
104 monc->pending_auth = 1;
105 monc->m_auth->front.iov_len = len;
106 monc->m_auth->hdr.front_len = cpu_to_le32(len);
Alex Elder6740a842012-06-01 14:56:43 -0500107 ceph_msg_revoke(monc->m_auth);
Sage Weil5ce6e9d2010-02-15 16:22:28 -0800108 ceph_msg_get(monc->m_auth); /* keep our ref */
Alex Elder67130932012-05-26 23:26:43 -0500109 ceph_con_send(&monc->con, monc->m_auth);
Sage Weil5ce6e9d2010-02-15 16:22:28 -0800110}
111
112/*
Sage Weilba75bb92009-10-06 11:31:11 -0700113 * Close monitor session, if any.
114 */
115static void __close_session(struct ceph_mon_client *monc)
116{
Sage Weilf6a2f5b2011-08-09 09:27:44 -0700117 dout("__close_session closing mon%d\n", monc->cur_mon);
Alex Elder6740a842012-06-01 14:56:43 -0500118 ceph_msg_revoke(monc->m_auth);
Sage Weil4f471e42012-07-30 18:16:40 -0700119 ceph_msg_revoke_incoming(monc->m_auth_reply);
120 ceph_msg_revoke(monc->m_subscribe);
121 ceph_msg_revoke_incoming(monc->m_subscribe_ack);
Alex Elder67130932012-05-26 23:26:43 -0500122 ceph_con_close(&monc->con);
Ilya Dryomov0e04dc22016-01-20 17:50:31 +0100123
Sage Weilf6a2f5b2011-08-09 09:27:44 -0700124 monc->pending_auth = 0;
125 ceph_auth_reset(monc->auth);
Sage Weilba75bb92009-10-06 11:31:11 -0700126}
127
128/*
Ilya Dryomov0e04dc22016-01-20 17:50:31 +0100129 * Pick a new monitor at random and set cur_mon. If we are repicking
130 * (i.e. cur_mon is already set), be sure to pick a different one.
Sage Weilba75bb92009-10-06 11:31:11 -0700131 */
Ilya Dryomov0e04dc22016-01-20 17:50:31 +0100132static void pick_new_mon(struct ceph_mon_client *monc)
Sage Weilba75bb92009-10-06 11:31:11 -0700133{
Ilya Dryomov0e04dc22016-01-20 17:50:31 +0100134 int old_mon = monc->cur_mon;
135
136 BUG_ON(monc->monmap->num_mon < 1);
137
138 if (monc->monmap->num_mon == 1) {
139 monc->cur_mon = 0;
140 } else {
141 int max = monc->monmap->num_mon;
142 int o = -1;
143 int n;
144
145 if (monc->cur_mon >= 0) {
146 if (monc->cur_mon < monc->monmap->num_mon)
147 o = monc->cur_mon;
148 if (o >= 0)
149 max--;
150 }
151
152 n = prandom_u32() % max;
153 if (o >= 0 && n >= o)
154 n++;
155
156 monc->cur_mon = n;
157 }
158
159 dout("%s mon%d -> mon%d out of %d mons\n", __func__, old_mon,
160 monc->cur_mon, monc->monmap->num_mon);
161}
162
163/*
164 * Open a session with a new monitor.
165 */
166static void __open_session(struct ceph_mon_client *monc)
167{
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800168 int ret;
Sage Weilba75bb92009-10-06 11:31:11 -0700169
Ilya Dryomov0e04dc22016-01-20 17:50:31 +0100170 pick_new_mon(monc);
Sage Weilba75bb92009-10-06 11:31:11 -0700171
Ilya Dryomov1752b502016-01-21 16:33:25 +0100172 monc->hunting = true;
Ilya Dryomov168b9092016-01-21 16:33:19 +0100173 if (monc->had_a_connection) {
174 monc->hunt_mult *= CEPH_MONC_HUNT_BACKOFF;
175 if (monc->hunt_mult > CEPH_MONC_HUNT_MAX_MULT)
176 monc->hunt_mult = CEPH_MONC_HUNT_MAX_MULT;
177 }
178
Ilya Dryomov0e04dc22016-01-20 17:50:31 +0100179 monc->sub_renew_after = jiffies; /* i.e., expired */
180 monc->sub_renew_sent = 0;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800181
Ilya Dryomov0e04dc22016-01-20 17:50:31 +0100182 dout("%s opening mon%d\n", __func__, monc->cur_mon);
183 ceph_con_open(&monc->con, CEPH_ENTITY_TYPE_MON, monc->cur_mon,
184 &monc->monmap->mon_inst[monc->cur_mon].addr);
Yan, Zheng8b9558a2015-09-01 17:19:38 +0800185
Ilya Dryomov0e04dc22016-01-20 17:50:31 +0100186 /*
187 * send an initial keepalive to ensure our timestamp is valid
188 * by the time we are in an OPENED state
189 */
190 ceph_con_keepalive(&monc->con);
191
192 /* initiate authentication handshake */
193 ret = ceph_auth_build_hello(monc->auth,
194 monc->m_auth->front.iov_base,
195 monc->m_auth->front_alloc_len);
196 BUG_ON(ret <= 0);
197 __send_prepared_auth_request(monc, ret);
Sage Weilba75bb92009-10-06 11:31:11 -0700198}
199
Ilya Dryomov1752b502016-01-21 16:33:25 +0100200static void reopen_session(struct ceph_mon_client *monc)
201{
202 if (!monc->hunting)
203 pr_info("mon%d %s session lost, hunting for new mon\n",
204 monc->cur_mon, ceph_pr_addr(&monc->con.peer_addr.in_addr));
205
206 __close_session(monc);
207 __open_session(monc);
208}
209
Sage Weilba75bb92009-10-06 11:31:11 -0700210/*
211 * Reschedule delayed work timer.
212 */
213static void __schedule_delayed(struct ceph_mon_client *monc)
214{
Yan, Zheng8b9558a2015-09-01 17:19:38 +0800215 unsigned long delay;
Sage Weilba75bb92009-10-06 11:31:11 -0700216
Ilya Dryomov168b9092016-01-21 16:33:19 +0100217 if (monc->hunting)
218 delay = CEPH_MONC_HUNT_INTERVAL * monc->hunt_mult;
219 else
Ilya Dryomov58d81b12016-01-21 16:33:15 +0100220 delay = CEPH_MONC_PING_INTERVAL;
Ilya Dryomov168b9092016-01-21 16:33:19 +0100221
Yan, Zheng8b9558a2015-09-01 17:19:38 +0800222 dout("__schedule_delayed after %lu\n", delay);
Ilya Dryomovbee3a372016-01-22 18:42:17 +0100223 mod_delayed_work(system_wq, &monc->delayed_work,
224 round_jiffies_relative(delay));
Sage Weilba75bb92009-10-06 11:31:11 -0700225}
226
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100227const char *ceph_sub_str[] = {
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100228 [CEPH_SUB_MONMAP] = "monmap",
229 [CEPH_SUB_OSDMAP] = "osdmap",
Yan, Zheng0cabbd92016-04-07 11:34:43 +0800230 [CEPH_SUB_FSMAP] = "fsmap.user",
231 [CEPH_SUB_MDSMAP] = "mdsmap",
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100232};
233
Sage Weilba75bb92009-10-06 11:31:11 -0700234/*
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100235 * Send subscribe request for one or more maps, according to
236 * monc->subs.
Sage Weilba75bb92009-10-06 11:31:11 -0700237 */
238static void __send_subscribe(struct ceph_mon_client *monc)
239{
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100240 struct ceph_msg *msg = monc->m_subscribe;
241 void *p = msg->front.iov_base;
242 void *const end = p + msg->front_alloc_len;
243 int num = 0;
244 int i;
Sage Weilba75bb92009-10-06 11:31:11 -0700245
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100246 dout("%s sent %lu\n", __func__, monc->sub_renew_sent);
Sage Weilba75bb92009-10-06 11:31:11 -0700247
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100248 BUG_ON(monc->cur_mon < 0);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700249
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100250 if (!monc->sub_renew_sent)
251 monc->sub_renew_sent = jiffies | 1; /* never 0 */
Sage Weilba75bb92009-10-06 11:31:11 -0700252
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100253 msg->hdr.version = cpu_to_le16(2);
Sage Weilba75bb92009-10-06 11:31:11 -0700254
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100255 for (i = 0; i < ARRAY_SIZE(monc->subs); i++) {
256 if (monc->subs[i].want)
257 num++;
Sage Weilba75bb92009-10-06 11:31:11 -0700258 }
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100259 BUG_ON(num < 1); /* monmap sub is always there */
260 ceph_encode_32(&p, num);
261 for (i = 0; i < ARRAY_SIZE(monc->subs); i++) {
Ilya Dryomov737cc81e2016-05-26 00:05:01 +0200262 char buf[32];
263 int len;
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100264
265 if (!monc->subs[i].want)
266 continue;
267
Ilya Dryomov737cc81e2016-05-26 00:05:01 +0200268 len = sprintf(buf, "%s", ceph_sub_str[i]);
269 if (i == CEPH_SUB_MDSMAP &&
270 monc->fs_cluster_id != CEPH_FS_CLUSTER_ID_NONE)
271 len += sprintf(buf + len, ".%d", monc->fs_cluster_id);
272
273 dout("%s %s start %llu flags 0x%x\n", __func__, buf,
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100274 le64_to_cpu(monc->subs[i].item.start),
275 monc->subs[i].item.flags);
Ilya Dryomov737cc81e2016-05-26 00:05:01 +0200276 ceph_encode_string(&p, end, buf, len);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100277 memcpy(p, &monc->subs[i].item, sizeof(monc->subs[i].item));
278 p += sizeof(monc->subs[i].item);
279 }
280
Ilya Dryomov737cc81e2016-05-26 00:05:01 +0200281 BUG_ON(p > end);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100282 msg->front.iov_len = p - msg->front.iov_base;
283 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
284 ceph_msg_revoke(msg);
285 ceph_con_send(&monc->con, ceph_msg_get(msg));
Sage Weilba75bb92009-10-06 11:31:11 -0700286}
287
288static void handle_subscribe_ack(struct ceph_mon_client *monc,
289 struct ceph_msg *msg)
290{
Eric Dumazet95c96172012-04-15 05:58:06 +0000291 unsigned int seconds;
Sage Weil07bd10f2009-10-14 17:26:40 -0700292 struct ceph_mon_subscribe_ack *h = msg->front.iov_base;
Sage Weilba75bb92009-10-06 11:31:11 -0700293
Sage Weil07bd10f2009-10-14 17:26:40 -0700294 if (msg->front.iov_len < sizeof(*h))
295 goto bad;
296 seconds = le32_to_cpu(h->duration);
297
Sage Weilba75bb92009-10-06 11:31:11 -0700298 mutex_lock(&monc->mutex);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100299 if (monc->sub_renew_sent) {
300 monc->sub_renew_after = monc->sub_renew_sent +
301 (seconds >> 1) * HZ - 1;
302 dout("%s sent %lu duration %d renew after %lu\n", __func__,
303 monc->sub_renew_sent, seconds, monc->sub_renew_after);
304 monc->sub_renew_sent = 0;
305 } else {
306 dout("%s sent %lu renew after %lu, ignoring\n", __func__,
307 monc->sub_renew_sent, monc->sub_renew_after);
308 }
Sage Weilba75bb92009-10-06 11:31:11 -0700309 mutex_unlock(&monc->mutex);
310 return;
311bad:
312 pr_err("got corrupt subscribe-ack msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -0800313 ceph_msg_dump(msg);
Sage Weilba75bb92009-10-06 11:31:11 -0700314}
315
316/*
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100317 * Register interest in a map
318 *
319 * @sub: one of CEPH_SUB_*
320 * @epoch: X for "every map since X", or 0 for "just the latest"
Sage Weilba75bb92009-10-06 11:31:11 -0700321 */
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100322static bool __ceph_monc_want_map(struct ceph_mon_client *monc, int sub,
323 u32 epoch, bool continuous)
Sage Weilba75bb92009-10-06 11:31:11 -0700324{
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100325 __le64 start = cpu_to_le64(epoch);
326 u8 flags = !continuous ? CEPH_SUBSCRIBE_ONETIME : 0;
Sage Weilba75bb92009-10-06 11:31:11 -0700327
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100328 dout("%s %s epoch %u continuous %d\n", __func__, ceph_sub_str[sub],
329 epoch, continuous);
330
331 if (monc->subs[sub].want &&
332 monc->subs[sub].item.start == start &&
333 monc->subs[sub].item.flags == flags)
334 return false;
335
336 monc->subs[sub].item.start = start;
337 monc->subs[sub].item.flags = flags;
338 monc->subs[sub].want = true;
339
340 return true;
341}
342
343bool ceph_monc_want_map(struct ceph_mon_client *monc, int sub, u32 epoch,
344 bool continuous)
345{
346 bool need_request;
347
348 mutex_lock(&monc->mutex);
349 need_request = __ceph_monc_want_map(monc, sub, epoch, continuous);
350 mutex_unlock(&monc->mutex);
351
352 return need_request;
353}
354EXPORT_SYMBOL(ceph_monc_want_map);
355
356/*
357 * Keep track of which maps we have
358 *
359 * @sub: one of CEPH_SUB_*
360 */
361static void __ceph_monc_got_map(struct ceph_mon_client *monc, int sub,
362 u32 epoch)
363{
364 dout("%s %s epoch %u\n", __func__, ceph_sub_str[sub], epoch);
365
366 if (monc->subs[sub].want) {
367 if (monc->subs[sub].item.flags & CEPH_SUBSCRIBE_ONETIME)
368 monc->subs[sub].want = false;
369 else
370 monc->subs[sub].item.start = cpu_to_le64(epoch + 1);
371 }
372
373 monc->subs[sub].have = epoch;
374}
375
376void ceph_monc_got_map(struct ceph_mon_client *monc, int sub, u32 epoch)
Sage Weilba75bb92009-10-06 11:31:11 -0700377{
378 mutex_lock(&monc->mutex);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100379 __ceph_monc_got_map(monc, sub, epoch);
Sage Weilba75bb92009-10-06 11:31:11 -0700380 mutex_unlock(&monc->mutex);
Sage Weilba75bb92009-10-06 11:31:11 -0700381}
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100382EXPORT_SYMBOL(ceph_monc_got_map);
Sage Weilba75bb92009-10-06 11:31:11 -0700383
Ilya Dryomov42c1b122016-04-28 16:07:25 +0200384void ceph_monc_renew_subs(struct ceph_mon_client *monc)
385{
386 mutex_lock(&monc->mutex);
387 __send_subscribe(monc);
388 mutex_unlock(&monc->mutex);
389}
390EXPORT_SYMBOL(ceph_monc_renew_subs);
391
Sage Weilba75bb92009-10-06 11:31:11 -0700392/*
Ilya Dryomova319bf52015-05-15 12:02:17 +0300393 * Wait for an osdmap with a given epoch.
394 *
395 * @epoch: epoch to wait for
396 * @timeout: in jiffies, 0 means "wait forever"
397 */
Ilya Dryomov6044cde2014-05-13 11:19:27 +0400398int ceph_monc_wait_osdmap(struct ceph_mon_client *monc, u32 epoch,
399 unsigned long timeout)
400{
401 unsigned long started = jiffies;
Ilya Dryomov216639d2015-05-19 12:03:33 +0300402 long ret;
Ilya Dryomov6044cde2014-05-13 11:19:27 +0400403
404 mutex_lock(&monc->mutex);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100405 while (monc->subs[CEPH_SUB_OSDMAP].have < epoch) {
Ilya Dryomov6044cde2014-05-13 11:19:27 +0400406 mutex_unlock(&monc->mutex);
407
Ilya Dryomova319bf52015-05-15 12:02:17 +0300408 if (timeout && time_after_eq(jiffies, started + timeout))
Ilya Dryomov6044cde2014-05-13 11:19:27 +0400409 return -ETIMEDOUT;
410
411 ret = wait_event_interruptible_timeout(monc->client->auth_wq,
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100412 monc->subs[CEPH_SUB_OSDMAP].have >= epoch,
413 ceph_timeout_jiffies(timeout));
Ilya Dryomov6044cde2014-05-13 11:19:27 +0400414 if (ret < 0)
415 return ret;
416
417 mutex_lock(&monc->mutex);
418 }
419
420 mutex_unlock(&monc->mutex);
421 return 0;
422}
423EXPORT_SYMBOL(ceph_monc_wait_osdmap);
Sage Weilba75bb92009-10-06 11:31:11 -0700424
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800425/*
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100426 * Open a session with a random monitor. Request monmap and osdmap,
427 * which are waited upon in __ceph_open_session().
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800428 */
429int ceph_monc_open_session(struct ceph_mon_client *monc)
Sage Weilba75bb92009-10-06 11:31:11 -0700430{
Sage Weilba75bb92009-10-06 11:31:11 -0700431 mutex_lock(&monc->mutex);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100432 __ceph_monc_want_map(monc, CEPH_SUB_MONMAP, 0, true);
433 __ceph_monc_want_map(monc, CEPH_SUB_OSDMAP, 0, false);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800434 __open_session(monc);
Sage Weilba75bb92009-10-06 11:31:11 -0700435 __schedule_delayed(monc);
436 mutex_unlock(&monc->mutex);
437 return 0;
438}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700439EXPORT_SYMBOL(ceph_monc_open_session);
Sage Weilba75bb92009-10-06 11:31:11 -0700440
Sage Weil07433042009-11-18 16:50:41 -0800441static void ceph_monc_handle_map(struct ceph_mon_client *monc,
442 struct ceph_msg *msg)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800443{
444 struct ceph_client *client = monc->client;
445 struct ceph_monmap *monmap = NULL, *old = monc->monmap;
446 void *p, *end;
447
448 mutex_lock(&monc->mutex);
449
450 dout("handle_monmap\n");
451 p = msg->front.iov_base;
452 end = p + msg->front.iov_len;
453
454 monmap = ceph_monmap_decode(p, end);
455 if (IS_ERR(monmap)) {
456 pr_err("problem decoding monmap, %d\n",
457 (int)PTR_ERR(monmap));
Sage Weild4a780c2009-12-11 08:55:23 -0800458 goto out;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800459 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800460
Sage Weil07433042009-11-18 16:50:41 -0800461 if (ceph_check_fsid(monc->client, &monmap->fsid) < 0) {
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800462 kfree(monmap);
Sage Weild4a780c2009-12-11 08:55:23 -0800463 goto out;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800464 }
465
466 client->monc.monmap = monmap;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800467 kfree(old);
468
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100469 __ceph_monc_got_map(monc, CEPH_SUB_MONMAP, monc->monmap->epoch);
Ilya Dryomov02ac9562016-01-06 12:56:21 +0300470 client->have_fsid = true;
Sage Weild1c338a2012-08-19 12:29:16 -0700471
Sage Weild4a780c2009-12-11 08:55:23 -0800472out:
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800473 mutex_unlock(&monc->mutex);
Yehuda Sadeh03066f22010-07-27 13:11:08 -0700474 wake_up_all(&client->auth_wq);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800475}
Sage Weilba75bb92009-10-06 11:31:11 -0700476
Sage Weilba75bb92009-10-06 11:31:11 -0700477/*
Ilya Dryomov7a6fdeb2014-12-22 19:14:26 +0300478 * generic requests (currently statfs, mon_get_version)
Sage Weilba75bb92009-10-06 11:31:11 -0700479 */
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200480DEFINE_RB_FUNCS(generic_request, struct ceph_mon_generic_request, tid, node)
Sage Weil85ff03f2010-02-15 14:47:28 -0800481
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700482static void release_generic_request(struct kref *kref)
Sage Weil3143edd2010-03-24 21:43:33 -0700483{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700484 struct ceph_mon_generic_request *req =
485 container_of(kref, struct ceph_mon_generic_request, kref);
Sage Weil3143edd2010-03-24 21:43:33 -0700486
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200487 dout("%s greq %p request %p reply %p\n", __func__, req, req->request,
488 req->reply);
489 WARN_ON(!RB_EMPTY_NODE(&req->node));
490
Sage Weil3143edd2010-03-24 21:43:33 -0700491 if (req->reply)
492 ceph_msg_put(req->reply);
493 if (req->request)
494 ceph_msg_put(req->request);
Yehuda Sadeh20547562010-06-01 10:37:40 -0700495
496 kfree(req);
Sage Weil3143edd2010-03-24 21:43:33 -0700497}
498
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700499static void put_generic_request(struct ceph_mon_generic_request *req)
Sage Weil3143edd2010-03-24 21:43:33 -0700500{
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200501 if (req)
502 kref_put(&req->kref, release_generic_request);
Sage Weil3143edd2010-03-24 21:43:33 -0700503}
504
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700505static void get_generic_request(struct ceph_mon_generic_request *req)
Sage Weil3143edd2010-03-24 21:43:33 -0700506{
507 kref_get(&req->kref);
508}
509
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200510static struct ceph_mon_generic_request *
511alloc_generic_request(struct ceph_mon_client *monc, gfp_t gfp)
512{
513 struct ceph_mon_generic_request *req;
514
515 req = kzalloc(sizeof(*req), gfp);
516 if (!req)
517 return NULL;
518
519 req->monc = monc;
520 kref_init(&req->kref);
521 RB_CLEAR_NODE(&req->node);
522 init_completion(&req->completion);
523
524 dout("%s greq %p\n", __func__, req);
525 return req;
526}
527
528static void register_generic_request(struct ceph_mon_generic_request *req)
529{
530 struct ceph_mon_client *monc = req->monc;
531
532 WARN_ON(req->tid);
533
534 get_generic_request(req);
535 req->tid = ++monc->last_tid;
536 insert_generic_request(&monc->generic_request_tree, req);
537}
538
539static void send_generic_request(struct ceph_mon_client *monc,
540 struct ceph_mon_generic_request *req)
541{
542 WARN_ON(!req->tid);
543
544 dout("%s greq %p tid %llu\n", __func__, req, req->tid);
545 req->request->hdr.tid = cpu_to_le64(req->tid);
546 ceph_con_send(&monc->con, ceph_msg_get(req->request));
547}
548
549static void __finish_generic_request(struct ceph_mon_generic_request *req)
550{
551 struct ceph_mon_client *monc = req->monc;
552
553 dout("%s greq %p tid %llu\n", __func__, req, req->tid);
554 erase_generic_request(&monc->generic_request_tree, req);
555
556 ceph_msg_revoke(req->request);
557 ceph_msg_revoke_incoming(req->reply);
558}
559
560static void finish_generic_request(struct ceph_mon_generic_request *req)
561{
562 __finish_generic_request(req);
563 put_generic_request(req);
564}
565
566static void complete_generic_request(struct ceph_mon_generic_request *req)
567{
568 if (req->complete_cb)
569 req->complete_cb(req);
570 else
571 complete_all(&req->completion);
572 put_generic_request(req);
573}
574
Wei Yongjunf52ec332016-07-30 00:37:31 +0000575static void cancel_generic_request(struct ceph_mon_generic_request *req)
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200576{
577 struct ceph_mon_client *monc = req->monc;
578 struct ceph_mon_generic_request *lookup_req;
579
580 dout("%s greq %p tid %llu\n", __func__, req, req->tid);
581
582 mutex_lock(&monc->mutex);
583 lookup_req = lookup_generic_request(&monc->generic_request_tree,
584 req->tid);
585 if (lookup_req) {
586 WARN_ON(lookup_req != req);
587 finish_generic_request(req);
588 }
589
590 mutex_unlock(&monc->mutex);
591}
592
593static int wait_generic_request(struct ceph_mon_generic_request *req)
594{
595 int ret;
596
597 dout("%s greq %p tid %llu\n", __func__, req, req->tid);
598 ret = wait_for_completion_interruptible(&req->completion);
599 if (ret)
600 cancel_generic_request(req);
601 else
602 ret = req->result; /* completed */
603
604 return ret;
605}
606
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700607static struct ceph_msg *get_generic_reply(struct ceph_connection *con,
Sage Weil3143edd2010-03-24 21:43:33 -0700608 struct ceph_msg_header *hdr,
609 int *skip)
610{
611 struct ceph_mon_client *monc = con->private;
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700612 struct ceph_mon_generic_request *req;
Sage Weil3143edd2010-03-24 21:43:33 -0700613 u64 tid = le64_to_cpu(hdr->tid);
614 struct ceph_msg *m;
615
616 mutex_lock(&monc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200617 req = lookup_generic_request(&monc->generic_request_tree, tid);
Sage Weil3143edd2010-03-24 21:43:33 -0700618 if (!req) {
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700619 dout("get_generic_reply %lld dne\n", tid);
Sage Weil3143edd2010-03-24 21:43:33 -0700620 *skip = 1;
621 m = NULL;
622 } else {
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700623 dout("get_generic_reply %lld got %p\n", tid, req->reply);
Alex Elder1c20f2d2012-06-04 14:43:32 -0500624 *skip = 0;
Sage Weil3143edd2010-03-24 21:43:33 -0700625 m = ceph_msg_get(req->reply);
626 /*
627 * we don't need to track the connection reading into
628 * this reply because we only have one open connection
629 * at a time, ever.
630 */
631 }
632 mutex_unlock(&monc->mutex);
633 return m;
634}
635
Yehuda Sadehe56fa102010-05-17 12:40:28 -0700636/*
637 * statfs
638 */
Sage Weilba75bb92009-10-06 11:31:11 -0700639static void handle_statfs_reply(struct ceph_mon_client *monc,
640 struct ceph_msg *msg)
641{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700642 struct ceph_mon_generic_request *req;
Sage Weilba75bb92009-10-06 11:31:11 -0700643 struct ceph_mon_statfs_reply *reply = msg->front.iov_base;
Sage Weil3143edd2010-03-24 21:43:33 -0700644 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weilba75bb92009-10-06 11:31:11 -0700645
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200646 dout("%s msg %p tid %llu\n", __func__, msg, tid);
647
Sage Weilba75bb92009-10-06 11:31:11 -0700648 if (msg->front.iov_len != sizeof(*reply))
649 goto bad;
Sage Weilba75bb92009-10-06 11:31:11 -0700650
651 mutex_lock(&monc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200652 req = lookup_generic_request(&monc->generic_request_tree, tid);
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200653 if (!req) {
654 mutex_unlock(&monc->mutex);
655 return;
Sage Weilba75bb92009-10-06 11:31:11 -0700656 }
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200657
658 req->result = 0;
659 *req->u.st = reply->st; /* struct */
660 __finish_generic_request(req);
Sage Weilba75bb92009-10-06 11:31:11 -0700661 mutex_unlock(&monc->mutex);
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200662
663 complete_generic_request(req);
Sage Weilba75bb92009-10-06 11:31:11 -0700664 return;
665
666bad:
Ilya Dryomov7a6fdeb2014-12-22 19:14:26 +0300667 pr_err("corrupt statfs reply, tid %llu\n", tid);
Sage Weil9ec7cab2009-12-14 15:13:47 -0800668 ceph_msg_dump(msg);
Sage Weilba75bb92009-10-06 11:31:11 -0700669}
670
671/*
Sage Weilba75bb92009-10-06 11:31:11 -0700672 * Do a synchronous statfs().
673 */
674int ceph_monc_do_statfs(struct ceph_mon_client *monc, struct ceph_statfs *buf)
675{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700676 struct ceph_mon_generic_request *req;
Sage Weil3143edd2010-03-24 21:43:33 -0700677 struct ceph_mon_statfs *h;
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200678 int ret = -ENOMEM;
Sage Weilba75bb92009-10-06 11:31:11 -0700679
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200680 req = alloc_generic_request(monc, GFP_NOFS);
Sage Weil3143edd2010-03-24 21:43:33 -0700681 if (!req)
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200682 goto out;
Sage Weilba75bb92009-10-06 11:31:11 -0700683
Sage Weilb61c2762011-08-09 15:03:46 -0700684 req->request = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), GFP_NOFS,
685 true);
Sage Weila79832f2010-04-01 16:06:19 -0700686 if (!req->request)
Sage Weil3143edd2010-03-24 21:43:33 -0700687 goto out;
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200688
689 req->reply = ceph_msg_new(CEPH_MSG_STATFS_REPLY, 64, GFP_NOFS, true);
Sage Weila79832f2010-04-01 16:06:19 -0700690 if (!req->reply)
Sage Weil3143edd2010-03-24 21:43:33 -0700691 goto out;
Sage Weil3143edd2010-03-24 21:43:33 -0700692
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200693 req->u.st = buf;
694
695 mutex_lock(&monc->mutex);
696 register_generic_request(req);
Sage Weil3143edd2010-03-24 21:43:33 -0700697 /* fill out request */
698 h = req->request->front.iov_base;
699 h->monhdr.have_version = 0;
700 h->monhdr.session_mon = cpu_to_le16(-1);
701 h->monhdr.session_mon_tid = 0;
702 h->fsid = monc->monmap->fsid;
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200703 send_generic_request(monc, req);
704 mutex_unlock(&monc->mutex);
Sage Weilba75bb92009-10-06 11:31:11 -0700705
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200706 ret = wait_generic_request(req);
Sage Weil3143edd2010-03-24 21:43:33 -0700707out:
Ilya Dryomovf6469122014-12-22 19:35:17 +0300708 put_generic_request(req);
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200709 return ret;
Sage Weilba75bb92009-10-06 11:31:11 -0700710}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700711EXPORT_SYMBOL(ceph_monc_do_statfs);
Sage Weilba75bb92009-10-06 11:31:11 -0700712
Ilya Dryomov513a8242014-05-13 11:19:26 +0400713static void handle_get_version_reply(struct ceph_mon_client *monc,
714 struct ceph_msg *msg)
715{
716 struct ceph_mon_generic_request *req;
717 u64 tid = le64_to_cpu(msg->hdr.tid);
718 void *p = msg->front.iov_base;
719 void *end = p + msg->front_alloc_len;
720 u64 handle;
721
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200722 dout("%s msg %p tid %llu\n", __func__, msg, tid);
Ilya Dryomov513a8242014-05-13 11:19:26 +0400723
724 ceph_decode_need(&p, end, 2*sizeof(u64), bad);
725 handle = ceph_decode_64(&p);
726 if (tid != 0 && tid != handle)
727 goto bad;
728
729 mutex_lock(&monc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200730 req = lookup_generic_request(&monc->generic_request_tree, handle);
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200731 if (!req) {
732 mutex_unlock(&monc->mutex);
733 return;
Ilya Dryomov513a8242014-05-13 11:19:26 +0400734 }
735
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200736 req->result = 0;
737 req->u.newest = ceph_decode_64(&p);
738 __finish_generic_request(req);
739 mutex_unlock(&monc->mutex);
740
741 complete_generic_request(req);
Ilya Dryomov513a8242014-05-13 11:19:26 +0400742 return;
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200743
Ilya Dryomov513a8242014-05-13 11:19:26 +0400744bad:
Ilya Dryomov7a6fdeb2014-12-22 19:14:26 +0300745 pr_err("corrupt mon_get_version reply, tid %llu\n", tid);
Ilya Dryomov513a8242014-05-13 11:19:26 +0400746 ceph_msg_dump(msg);
747}
748
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200749static struct ceph_mon_generic_request *
750__ceph_monc_get_version(struct ceph_mon_client *monc, const char *what,
751 ceph_monc_callback_t cb, u64 private_data)
752{
753 struct ceph_mon_generic_request *req;
754
755 req = alloc_generic_request(monc, GFP_NOIO);
756 if (!req)
757 goto err_put_req;
758
759 req->request = ceph_msg_new(CEPH_MSG_MON_GET_VERSION,
760 sizeof(u64) + sizeof(u32) + strlen(what),
761 GFP_NOIO, true);
762 if (!req->request)
763 goto err_put_req;
764
765 req->reply = ceph_msg_new(CEPH_MSG_MON_GET_VERSION_REPLY, 32, GFP_NOIO,
766 true);
767 if (!req->reply)
768 goto err_put_req;
769
770 req->complete_cb = cb;
771 req->private_data = private_data;
772
773 mutex_lock(&monc->mutex);
774 register_generic_request(req);
775 {
776 void *p = req->request->front.iov_base;
777 void *const end = p + req->request->front_alloc_len;
778
779 ceph_encode_64(&p, req->tid); /* handle */
780 ceph_encode_string(&p, end, what, strlen(what));
781 WARN_ON(p != end);
782 }
783 send_generic_request(monc, req);
784 mutex_unlock(&monc->mutex);
785
786 return req;
787
788err_put_req:
789 put_generic_request(req);
790 return ERR_PTR(-ENOMEM);
791}
792
Ilya Dryomov513a8242014-05-13 11:19:26 +0400793/*
794 * Send MMonGetVersion and wait for the reply.
795 *
796 * @what: one of "mdsmap", "osdmap" or "monmap"
797 */
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200798int ceph_monc_get_version(struct ceph_mon_client *monc, const char *what,
799 u64 *newest)
Ilya Dryomov513a8242014-05-13 11:19:26 +0400800{
801 struct ceph_mon_generic_request *req;
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200802 int ret;
Ilya Dryomov513a8242014-05-13 11:19:26 +0400803
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200804 req = __ceph_monc_get_version(monc, what, NULL, 0);
805 if (IS_ERR(req))
806 return PTR_ERR(req);
Ilya Dryomov513a8242014-05-13 11:19:26 +0400807
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200808 ret = wait_generic_request(req);
809 if (!ret)
810 *newest = req->u.newest;
Ilya Dryomov513a8242014-05-13 11:19:26 +0400811
Ilya Dryomovf6469122014-12-22 19:35:17 +0300812 put_generic_request(req);
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200813 return ret;
Ilya Dryomov513a8242014-05-13 11:19:26 +0400814}
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200815EXPORT_SYMBOL(ceph_monc_get_version);
816
817/*
818 * Send MMonGetVersion,
819 *
820 * @what: one of "mdsmap", "osdmap" or "monmap"
821 */
822int ceph_monc_get_version_async(struct ceph_mon_client *monc, const char *what,
823 ceph_monc_callback_t cb, u64 private_data)
824{
825 struct ceph_mon_generic_request *req;
826
827 req = __ceph_monc_get_version(monc, what, cb, private_data);
828 if (IS_ERR(req))
829 return PTR_ERR(req);
830
831 put_generic_request(req);
832 return 0;
833}
834EXPORT_SYMBOL(ceph_monc_get_version_async);
Ilya Dryomov513a8242014-05-13 11:19:26 +0400835
Douglas Fuller6305a3b2015-07-22 20:59:52 -0400836static void handle_command_ack(struct ceph_mon_client *monc,
837 struct ceph_msg *msg)
838{
839 struct ceph_mon_generic_request *req;
840 void *p = msg->front.iov_base;
841 void *const end = p + msg->front_alloc_len;
842 u64 tid = le64_to_cpu(msg->hdr.tid);
843
844 dout("%s msg %p tid %llu\n", __func__, msg, tid);
845
846 ceph_decode_need(&p, end, sizeof(struct ceph_mon_request_header) +
847 sizeof(u32), bad);
848 p += sizeof(struct ceph_mon_request_header);
849
850 mutex_lock(&monc->mutex);
851 req = lookup_generic_request(&monc->generic_request_tree, tid);
852 if (!req) {
853 mutex_unlock(&monc->mutex);
854 return;
855 }
856
857 req->result = ceph_decode_32(&p);
858 __finish_generic_request(req);
859 mutex_unlock(&monc->mutex);
860
861 complete_generic_request(req);
862 return;
863
864bad:
865 pr_err("corrupt mon_command ack, tid %llu\n", tid);
866 ceph_msg_dump(msg);
867}
868
869int ceph_monc_blacklist_add(struct ceph_mon_client *monc,
870 struct ceph_entity_addr *client_addr)
871{
872 struct ceph_mon_generic_request *req;
873 struct ceph_mon_command *h;
874 int ret = -ENOMEM;
875 int len;
876
877 req = alloc_generic_request(monc, GFP_NOIO);
878 if (!req)
879 goto out;
880
881 req->request = ceph_msg_new(CEPH_MSG_MON_COMMAND, 256, GFP_NOIO, true);
882 if (!req->request)
883 goto out;
884
885 req->reply = ceph_msg_new(CEPH_MSG_MON_COMMAND_ACK, 512, GFP_NOIO,
886 true);
887 if (!req->reply)
888 goto out;
889
890 mutex_lock(&monc->mutex);
891 register_generic_request(req);
892 h = req->request->front.iov_base;
893 h->monhdr.have_version = 0;
894 h->monhdr.session_mon = cpu_to_le16(-1);
895 h->monhdr.session_mon_tid = 0;
896 h->fsid = monc->monmap->fsid;
897 h->num_strs = cpu_to_le32(1);
898 len = sprintf(h->str, "{ \"prefix\": \"osd blacklist\", \
899 \"blacklistop\": \"add\", \
900 \"addr\": \"%pISpc/%u\" }",
901 &client_addr->in_addr, le32_to_cpu(client_addr->nonce));
902 h->str_len = cpu_to_le32(len);
903 send_generic_request(monc, req);
904 mutex_unlock(&monc->mutex);
905
906 ret = wait_generic_request(req);
907out:
908 put_generic_request(req);
909 return ret;
910}
911EXPORT_SYMBOL(ceph_monc_blacklist_add);
912
Sage Weilba75bb92009-10-06 11:31:11 -0700913/*
Yehuda Sadehe56fa102010-05-17 12:40:28 -0700914 * Resend pending generic requests.
Sage Weilba75bb92009-10-06 11:31:11 -0700915 */
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700916static void __resend_generic_request(struct ceph_mon_client *monc)
Sage Weilba75bb92009-10-06 11:31:11 -0700917{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700918 struct ceph_mon_generic_request *req;
Sage Weil85ff03f2010-02-15 14:47:28 -0800919 struct rb_node *p;
Sage Weilba75bb92009-10-06 11:31:11 -0700920
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700921 for (p = rb_first(&monc->generic_request_tree); p; p = rb_next(p)) {
922 req = rb_entry(p, struct ceph_mon_generic_request, node);
Alex Elder6740a842012-06-01 14:56:43 -0500923 ceph_msg_revoke(req->request);
Sage Weil4f471e42012-07-30 18:16:40 -0700924 ceph_msg_revoke_incoming(req->reply);
Alex Elder67130932012-05-26 23:26:43 -0500925 ceph_con_send(&monc->con, ceph_msg_get(req->request));
Sage Weilba75bb92009-10-06 11:31:11 -0700926 }
927}
928
929/*
930 * Delayed work. If we haven't mounted yet, retry. Otherwise,
931 * renew/retry subscription as needed (in case it is timing out, or we
932 * got an ENOMEM). And keep the monitor connection alive.
933 */
934static void delayed_work(struct work_struct *work)
935{
936 struct ceph_mon_client *monc =
937 container_of(work, struct ceph_mon_client, delayed_work.work);
938
939 dout("monc delayed_work\n");
940 mutex_lock(&monc->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800941 if (monc->hunting) {
Ilya Dryomov1752b502016-01-21 16:33:25 +0100942 dout("%s continuing hunt\n", __func__);
943 reopen_session(monc);
Sage Weilba75bb92009-10-06 11:31:11 -0700944 } else {
Yan, Zheng8b9558a2015-09-01 17:19:38 +0800945 int is_auth = ceph_auth_is_authenticated(monc->auth);
946 if (ceph_con_keepalive_expired(&monc->con,
Ilya Dryomov58d81b12016-01-21 16:33:15 +0100947 CEPH_MONC_PING_TIMEOUT)) {
Yan, Zheng8b9558a2015-09-01 17:19:38 +0800948 dout("monc keepalive timeout\n");
949 is_auth = 0;
Ilya Dryomov1752b502016-01-21 16:33:25 +0100950 reopen_session(monc);
Yan, Zheng8b9558a2015-09-01 17:19:38 +0800951 }
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800952
Yan, Zheng8b9558a2015-09-01 17:19:38 +0800953 if (!monc->hunting) {
954 ceph_con_keepalive(&monc->con);
955 __validate_auth(monc);
956 }
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800957
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100958 if (is_auth) {
959 unsigned long now = jiffies;
960
961 dout("%s renew subs? now %lu renew after %lu\n",
962 __func__, now, monc->sub_renew_after);
963 if (time_after_eq(now, monc->sub_renew_after))
964 __send_subscribe(monc);
965 }
Sage Weilba75bb92009-10-06 11:31:11 -0700966 }
Sage Weilba75bb92009-10-06 11:31:11 -0700967 __schedule_delayed(monc);
968 mutex_unlock(&monc->mutex);
969}
970
Sage Weil6b805182009-10-27 11:50:50 -0700971/*
972 * On startup, we build a temporary monmap populated with the IPs
973 * provided by mount(2).
974 */
975static int build_initial_monmap(struct ceph_mon_client *monc)
976{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700977 struct ceph_options *opt = monc->client->options;
978 struct ceph_entity_addr *mon_addr = opt->mon_addr;
979 int num_mon = opt->num_mon;
Sage Weil6b805182009-10-27 11:50:50 -0700980 int i;
981
982 /* build initial monmap */
983 monc->monmap = kzalloc(sizeof(*monc->monmap) +
984 num_mon*sizeof(monc->monmap->mon_inst[0]),
985 GFP_KERNEL);
986 if (!monc->monmap)
987 return -ENOMEM;
988 for (i = 0; i < num_mon; i++) {
989 monc->monmap->mon_inst[i].addr = mon_addr[i];
Sage Weil6b805182009-10-27 11:50:50 -0700990 monc->monmap->mon_inst[i].addr.nonce = 0;
991 monc->monmap->mon_inst[i].name.type =
992 CEPH_ENTITY_TYPE_MON;
993 monc->monmap->mon_inst[i].name.num = cpu_to_le64(i);
994 }
995 monc->monmap->num_mon = num_mon;
Sage Weil6b805182009-10-27 11:50:50 -0700996 return 0;
997}
998
Sage Weilba75bb92009-10-06 11:31:11 -0700999int ceph_monc_init(struct ceph_mon_client *monc, struct ceph_client *cl)
1000{
1001 int err = 0;
1002
1003 dout("init\n");
1004 memset(monc, 0, sizeof(*monc));
1005 monc->client = cl;
1006 monc->monmap = NULL;
1007 mutex_init(&monc->mutex);
1008
Sage Weil6b805182009-10-27 11:50:50 -07001009 err = build_initial_monmap(monc);
1010 if (err)
1011 goto out;
1012
Sage Weilf6a2f5b2011-08-09 09:27:44 -07001013 /* connection */
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001014 /* authentication */
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001015 monc->auth = ceph_auth_init(cl->options->name,
Tommi Virtanen8323c3a2011-03-25 16:32:57 -07001016 cl->options->key);
Noah Watkins49d92242011-09-12 14:51:58 -07001017 if (IS_ERR(monc->auth)) {
1018 err = PTR_ERR(monc->auth);
Alex Elder67130932012-05-26 23:26:43 -05001019 goto out_monmap;
Noah Watkins49d92242011-09-12 14:51:58 -07001020 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001021 monc->auth->want_keys =
1022 CEPH_ENTITY_TYPE_AUTH | CEPH_ENTITY_TYPE_MON |
1023 CEPH_ENTITY_TYPE_OSD | CEPH_ENTITY_TYPE_MDS;
1024
Sage Weil240ed682010-05-21 14:57:25 -07001025 /* msgs */
Sage Weila79832f2010-04-01 16:06:19 -07001026 err = -ENOMEM;
Sage Weil7c315c52010-03-24 21:52:30 -07001027 monc->m_subscribe_ack = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE_ACK,
Yehuda Sadeh34d23762010-04-06 14:33:58 -07001028 sizeof(struct ceph_mon_subscribe_ack),
Ilya Dryomov5418d0a22016-12-02 16:35:08 +01001029 GFP_KERNEL, true);
Sage Weila79832f2010-04-01 16:06:19 -07001030 if (!monc->m_subscribe_ack)
Noah Watkins49d92242011-09-12 14:51:58 -07001031 goto out_auth;
Sage Weil6694d6b2010-03-24 21:48:05 -07001032
Ilya Dryomov5418d0a22016-12-02 16:35:08 +01001033 monc->m_subscribe = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 128,
1034 GFP_KERNEL, true);
Sage Weil240ed682010-05-21 14:57:25 -07001035 if (!monc->m_subscribe)
1036 goto out_subscribe_ack;
1037
Ilya Dryomov5418d0a22016-12-02 16:35:08 +01001038 monc->m_auth_reply = ceph_msg_new(CEPH_MSG_AUTH_REPLY, 4096,
1039 GFP_KERNEL, true);
Sage Weila79832f2010-04-01 16:06:19 -07001040 if (!monc->m_auth_reply)
Sage Weil240ed682010-05-21 14:57:25 -07001041 goto out_subscribe;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001042
Ilya Dryomov5418d0a22016-12-02 16:35:08 +01001043 monc->m_auth = ceph_msg_new(CEPH_MSG_AUTH, 4096, GFP_KERNEL, true);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08001044 monc->pending_auth = 0;
Sage Weila79832f2010-04-01 16:06:19 -07001045 if (!monc->m_auth)
Sage Weil6694d6b2010-03-24 21:48:05 -07001046 goto out_auth_reply;
Sage Weilba75bb92009-10-06 11:31:11 -07001047
Sage Weil735a72e2012-06-27 12:24:34 -07001048 ceph_con_init(&monc->con, monc, &mon_con_ops,
1049 &monc->client->msgr);
1050
Sage Weilba75bb92009-10-06 11:31:11 -07001051 monc->cur_mon = -1;
Ilya Dryomov168b9092016-01-21 16:33:19 +01001052 monc->had_a_connection = false;
1053 monc->hunt_mult = 1;
Sage Weilba75bb92009-10-06 11:31:11 -07001054
1055 INIT_DELAYED_WORK(&monc->delayed_work, delayed_work);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -07001056 monc->generic_request_tree = RB_ROOT;
Sage Weilba75bb92009-10-06 11:31:11 -07001057 monc->last_tid = 0;
1058
Ilya Dryomov737cc81e2016-05-26 00:05:01 +02001059 monc->fs_cluster_id = CEPH_FS_CLUSTER_ID_NONE;
1060
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001061 return 0;
1062
Sage Weil6694d6b2010-03-24 21:48:05 -07001063out_auth_reply:
1064 ceph_msg_put(monc->m_auth_reply);
Sage Weil240ed682010-05-21 14:57:25 -07001065out_subscribe:
1066 ceph_msg_put(monc->m_subscribe);
Sage Weil7c315c52010-03-24 21:52:30 -07001067out_subscribe_ack:
1068 ceph_msg_put(monc->m_subscribe_ack);
Noah Watkins49d92242011-09-12 14:51:58 -07001069out_auth:
1070 ceph_auth_destroy(monc->auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001071out_monmap:
1072 kfree(monc->monmap);
Sage Weilba75bb92009-10-06 11:31:11 -07001073out:
1074 return err;
1075}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001076EXPORT_SYMBOL(ceph_monc_init);
Sage Weilba75bb92009-10-06 11:31:11 -07001077
1078void ceph_monc_stop(struct ceph_mon_client *monc)
1079{
1080 dout("stop\n");
1081 cancel_delayed_work_sync(&monc->delayed_work);
1082
1083 mutex_lock(&monc->mutex);
1084 __close_session(monc);
Ilya Dryomov0e04dc22016-01-20 17:50:31 +01001085 monc->cur_mon = -1;
Sage Weilba75bb92009-10-06 11:31:11 -07001086 mutex_unlock(&monc->mutex);
1087
Sage Weilf3dea7e2012-06-10 20:43:56 -07001088 /*
1089 * flush msgr queue before we destroy ourselves to ensure that:
1090 * - any work that references our embedded con is finished.
1091 * - any osd_client or other work that may reference an authorizer
1092 * finishes before we shut down the auth subsystem.
1093 */
1094 ceph_msgr_flush();
1095
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001096 ceph_auth_destroy(monc->auth);
1097
Ilya Dryomovd0b19702016-04-28 16:07:27 +02001098 WARN_ON(!RB_EMPTY_ROOT(&monc->generic_request_tree));
1099
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001100 ceph_msg_put(monc->m_auth);
Sage Weil6694d6b2010-03-24 21:48:05 -07001101 ceph_msg_put(monc->m_auth_reply);
Sage Weil240ed682010-05-21 14:57:25 -07001102 ceph_msg_put(monc->m_subscribe);
Sage Weil7c315c52010-03-24 21:52:30 -07001103 ceph_msg_put(monc->m_subscribe_ack);
Sage Weilba75bb92009-10-06 11:31:11 -07001104
1105 kfree(monc->monmap);
1106}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001107EXPORT_SYMBOL(ceph_monc_stop);
Sage Weilba75bb92009-10-06 11:31:11 -07001108
Ilya Dryomov0f9af162016-01-08 21:17:22 +03001109static void finish_hunting(struct ceph_mon_client *monc)
1110{
1111 if (monc->hunting) {
1112 dout("%s found mon%d\n", __func__, monc->cur_mon);
1113 monc->hunting = false;
Ilya Dryomov168b9092016-01-21 16:33:19 +01001114 monc->had_a_connection = true;
1115 monc->hunt_mult /= 2; /* reduce by 50% */
1116 if (monc->hunt_mult < 1)
1117 monc->hunt_mult = 1;
Ilya Dryomov0f9af162016-01-08 21:17:22 +03001118 }
1119}
1120
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001121static void handle_auth_reply(struct ceph_mon_client *monc,
1122 struct ceph_msg *msg)
1123{
1124 int ret;
Sage Weil09c4d6a2010-05-25 15:38:06 -07001125 int was_auth = 0;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001126
1127 mutex_lock(&monc->mutex);
Sage Weil27859f92013-03-25 10:26:14 -07001128 was_auth = ceph_auth_is_authenticated(monc->auth);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08001129 monc->pending_auth = 0;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001130 ret = ceph_handle_auth_reply(monc->auth, msg->front.iov_base,
1131 msg->front.iov_len,
1132 monc->m_auth->front.iov_base,
Ilya Dryomov3cea4c32014-01-09 20:08:21 +02001133 monc->m_auth->front_alloc_len);
Ilya Dryomov0f9af162016-01-08 21:17:22 +03001134 if (ret > 0) {
1135 __send_prepared_auth_request(monc, ret);
1136 goto out;
1137 }
1138
1139 finish_hunting(monc);
1140
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001141 if (ret < 0) {
Sage Weil9bd2e6f2010-02-02 16:21:06 -08001142 monc->client->auth_err = ret;
Sage Weil27859f92013-03-25 10:26:14 -07001143 } else if (!was_auth && ceph_auth_is_authenticated(monc->auth)) {
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001144 dout("authenticated, starting session\n");
Sage Weil07433042009-11-18 16:50:41 -08001145
Alex Elder15d98822012-05-26 23:26:43 -05001146 monc->client->msgr.inst.name.type = CEPH_ENTITY_TYPE_CLIENT;
1147 monc->client->msgr.inst.name.num =
Yehuda Sadeh0cf55372010-06-11 15:57:06 -07001148 cpu_to_le64(monc->auth->global_id);
Sage Weil07433042009-11-18 16:50:41 -08001149
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001150 __send_subscribe(monc);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -07001151 __resend_generic_request(monc);
Ilya Dryomov0f9af162016-01-08 21:17:22 +03001152
1153 pr_info("mon%d %s session established\n", monc->cur_mon,
1154 ceph_pr_addr(&monc->con.peer_addr.in_addr));
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001155 }
Ilya Dryomov0f9af162016-01-08 21:17:22 +03001156
1157out:
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001158 mutex_unlock(&monc->mutex);
Ilya Dryomov0f9af162016-01-08 21:17:22 +03001159 if (monc->client->auth_err < 0)
1160 wake_up_all(&monc->client->auth_wq);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001161}
1162
Sage Weil9bd2e6f2010-02-02 16:21:06 -08001163static int __validate_auth(struct ceph_mon_client *monc)
1164{
1165 int ret;
1166
1167 if (monc->pending_auth)
1168 return 0;
1169
1170 ret = ceph_build_auth(monc->auth, monc->m_auth->front.iov_base,
Ilya Dryomov3cea4c32014-01-09 20:08:21 +02001171 monc->m_auth->front_alloc_len);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08001172 if (ret <= 0)
1173 return ret; /* either an error, or no need to authenticate */
1174 __send_prepared_auth_request(monc, ret);
1175 return 0;
1176}
1177
1178int ceph_monc_validate_auth(struct ceph_mon_client *monc)
1179{
1180 int ret;
1181
1182 mutex_lock(&monc->mutex);
1183 ret = __validate_auth(monc);
1184 mutex_unlock(&monc->mutex);
1185 return ret;
1186}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001187EXPORT_SYMBOL(ceph_monc_validate_auth);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08001188
Sage Weilba75bb92009-10-06 11:31:11 -07001189/*
1190 * handle incoming message
1191 */
1192static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1193{
1194 struct ceph_mon_client *monc = con->private;
1195 int type = le16_to_cpu(msg->hdr.type);
1196
1197 if (!monc)
1198 return;
1199
1200 switch (type) {
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001201 case CEPH_MSG_AUTH_REPLY:
1202 handle_auth_reply(monc, msg);
Sage Weilba75bb92009-10-06 11:31:11 -07001203 break;
1204
1205 case CEPH_MSG_MON_SUBSCRIBE_ACK:
1206 handle_subscribe_ack(monc, msg);
1207 break;
1208
1209 case CEPH_MSG_STATFS_REPLY:
1210 handle_statfs_reply(monc, msg);
1211 break;
1212
Ilya Dryomov513a8242014-05-13 11:19:26 +04001213 case CEPH_MSG_MON_GET_VERSION_REPLY:
1214 handle_get_version_reply(monc, msg);
1215 break;
1216
Douglas Fuller6305a3b2015-07-22 20:59:52 -04001217 case CEPH_MSG_MON_COMMAND_ACK:
1218 handle_command_ack(monc, msg);
1219 break;
1220
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001221 case CEPH_MSG_MON_MAP:
1222 ceph_monc_handle_map(monc, msg);
1223 break;
1224
Sage Weilba75bb92009-10-06 11:31:11 -07001225 case CEPH_MSG_OSD_MAP:
1226 ceph_osdc_handle_map(&monc->client->osdc, msg);
1227 break;
1228
1229 default:
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001230 /* can the chained handler handle it? */
1231 if (monc->client->extra_mon_dispatch &&
1232 monc->client->extra_mon_dispatch(monc->client, msg) == 0)
1233 break;
1234
Sage Weilba75bb92009-10-06 11:31:11 -07001235 pr_err("received unknown message type %d %s\n", type,
1236 ceph_msg_type_name(type));
1237 }
1238 ceph_msg_put(msg);
1239}
1240
1241/*
1242 * Allocate memory for incoming message
1243 */
1244static struct ceph_msg *mon_alloc_msg(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001245 struct ceph_msg_header *hdr,
1246 int *skip)
Sage Weilba75bb92009-10-06 11:31:11 -07001247{
1248 struct ceph_mon_client *monc = con->private;
1249 int type = le16_to_cpu(hdr->type);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001250 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -08001251 struct ceph_msg *m = NULL;
Sage Weilba75bb92009-10-06 11:31:11 -07001252
Yehuda Sadeh24504182010-01-08 13:58:34 -08001253 *skip = 0;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001254
Sage Weilba75bb92009-10-06 11:31:11 -07001255 switch (type) {
Sage Weilba75bb92009-10-06 11:31:11 -07001256 case CEPH_MSG_MON_SUBSCRIBE_ACK:
Sage Weil7c315c52010-03-24 21:52:30 -07001257 m = ceph_msg_get(monc->m_subscribe_ack);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001258 break;
Sage Weilba75bb92009-10-06 11:31:11 -07001259 case CEPH_MSG_STATFS_REPLY:
Douglas Fuller6305a3b2015-07-22 20:59:52 -04001260 case CEPH_MSG_MON_COMMAND_ACK:
Yehuda Sadehf8c76f62010-04-22 15:40:37 -07001261 return get_generic_reply(con, hdr, skip);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001262 case CEPH_MSG_AUTH_REPLY:
Sage Weil6694d6b2010-03-24 21:48:05 -07001263 m = ceph_msg_get(monc->m_auth_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001264 break;
Ilya Dryomov513a8242014-05-13 11:19:26 +04001265 case CEPH_MSG_MON_GET_VERSION_REPLY:
1266 if (le64_to_cpu(hdr->tid) != 0)
1267 return get_generic_reply(con, hdr, skip);
1268
1269 /*
1270 * Older OSDs don't set reply tid even if the orignal
1271 * request had a non-zero tid. Workaround this weirdness
1272 * by falling through to the allocate case.
1273 */
Sage Weil5b3a4db2010-02-19 21:43:23 -08001274 case CEPH_MSG_MON_MAP:
1275 case CEPH_MSG_MDS_MAP:
1276 case CEPH_MSG_OSD_MAP:
Yan, Zheng0cabbd92016-04-07 11:34:43 +08001277 case CEPH_MSG_FS_MAP_USER:
Sage Weilb61c2762011-08-09 15:03:46 -07001278 m = ceph_msg_new(type, front_len, GFP_NOFS, false);
Alex Elder1c20f2d2012-06-04 14:43:32 -05001279 if (!m)
1280 return NULL; /* ENOMEM--return skip == 0 */
Sage Weil5b3a4db2010-02-19 21:43:23 -08001281 break;
Sage Weilba75bb92009-10-06 11:31:11 -07001282 }
Yehuda Sadeh24504182010-01-08 13:58:34 -08001283
Sage Weil5b3a4db2010-02-19 21:43:23 -08001284 if (!m) {
1285 pr_info("alloc_msg unknown type %d\n", type);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001286 *skip = 1;
Sage Weil73c3d482014-08-04 07:01:54 -07001287 } else if (front_len > m->front_alloc_len) {
Joe Perchesb9a67892014-09-09 21:17:29 -07001288 pr_warn("mon_alloc_msg front %d > prealloc %d (%u#%llu)\n",
1289 front_len, m->front_alloc_len,
1290 (unsigned int)con->peer_name.type,
1291 le64_to_cpu(con->peer_name.num));
Sage Weil73c3d482014-08-04 07:01:54 -07001292 ceph_msg_put(m);
1293 m = ceph_msg_new(type, front_len, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08001294 }
Sage Weil73c3d482014-08-04 07:01:54 -07001295
Yehuda Sadeh24504182010-01-08 13:58:34 -08001296 return m;
Sage Weilba75bb92009-10-06 11:31:11 -07001297}
1298
1299/*
1300 * If the monitor connection resets, pick a new monitor and resubmit
1301 * any pending requests.
1302 */
1303static void mon_fault(struct ceph_connection *con)
1304{
1305 struct ceph_mon_client *monc = con->private;
1306
Sage Weilba75bb92009-10-06 11:31:11 -07001307 mutex_lock(&monc->mutex);
Ilya Dryomovb5d91702016-01-23 15:57:51 +01001308 dout("%s mon%d\n", __func__, monc->cur_mon);
1309 if (monc->cur_mon >= 0) {
1310 if (!monc->hunting) {
1311 dout("%s hunting for new mon\n", __func__);
1312 reopen_session(monc);
1313 __schedule_delayed(monc);
1314 } else {
1315 dout("%s already hunting\n", __func__);
1316 }
Sage Weilba75bb92009-10-06 11:31:11 -07001317 }
Sage Weilba75bb92009-10-06 11:31:11 -07001318 mutex_unlock(&monc->mutex);
1319}
1320
Sage Weilec87ef42012-05-31 20:27:50 -07001321/*
1322 * We can ignore refcounting on the connection struct, as all references
1323 * will come from the messenger workqueue, which is drained prior to
1324 * mon_client destruction.
1325 */
1326static struct ceph_connection *con_get(struct ceph_connection *con)
1327{
1328 return con;
1329}
1330
1331static void con_put(struct ceph_connection *con)
1332{
1333}
1334
Tobias Klauser9e327892010-05-20 10:40:19 +02001335static const struct ceph_connection_operations mon_con_ops = {
Sage Weilec87ef42012-05-31 20:27:50 -07001336 .get = con_get,
1337 .put = con_put,
Sage Weilba75bb92009-10-06 11:31:11 -07001338 .dispatch = dispatch,
1339 .fault = mon_fault,
1340 .alloc_msg = mon_alloc_msg,
Sage Weilba75bb92009-10-06 11:31:11 -07001341};