blob: 0520bf9825aa3edd3436c70bdee15cc9bede07cc [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Sage Weilba75bb92009-10-06 11:31:11 -07003
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004#include <linux/module.h>
Sage Weilba75bb92009-10-06 11:31:11 -07005#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09006#include <linux/slab.h>
Sage Weilba75bb92009-10-06 11:31:11 -07007#include <linux/random.h>
8#include <linux/sched.h>
9
Ilya Dryomov220abf52017-06-05 14:45:00 +020010#include <linux/ceph/ceph_features.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070011#include <linux/ceph/mon_client.h>
12#include <linux/ceph/libceph.h>
Sage Weilab434b62012-01-13 22:22:03 -080013#include <linux/ceph/debugfs.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070014#include <linux/ceph/decode.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070015#include <linux/ceph/auth.h>
Sage Weilba75bb92009-10-06 11:31:11 -070016
17/*
18 * Interact with Ceph monitor cluster. Handle requests for new map
19 * versions, and periodically resend as needed. Also implement
20 * statfs() and umount().
21 *
22 * A small cluster of Ceph "monitors" are responsible for managing critical
23 * cluster configuration and state information. An odd number (e.g., 3, 5)
24 * of cmon daemons use a modified version of the Paxos part-time parliament
25 * algorithm to manage the MDS map (mds cluster membership), OSD map, and
26 * list of clients who have mounted the file system.
27 *
28 * We maintain an open, active session with a monitor at all times in order to
29 * receive timely MDSMap updates. We periodically send a keepalive byte on the
30 * TCP socket to ensure we detect a failure. If the connection does break, we
31 * randomly hunt for a new monitor. Once the connection is reestablished, we
32 * resend any outstanding requests.
33 */
34
Tobias Klauser9e327892010-05-20 10:40:19 +020035static const struct ceph_connection_operations mon_con_ops;
Sage Weilba75bb92009-10-06 11:31:11 -070036
Sage Weil9bd2e6f2010-02-02 16:21:06 -080037static int __validate_auth(struct ceph_mon_client *monc);
38
Sage Weilba75bb92009-10-06 11:31:11 -070039/*
40 * Decode a monmap blob (e.g., during mount).
41 */
Jeff Layton0bfb0f22019-05-31 15:32:28 -040042static struct ceph_monmap *ceph_monmap_decode(void *p, void *end)
Sage Weilba75bb92009-10-06 11:31:11 -070043{
44 struct ceph_monmap *m = NULL;
45 int i, err = -EINVAL;
46 struct ceph_fsid fsid;
47 u32 epoch, num_mon;
Sage Weil4e7a5dc2009-11-18 16:19:57 -080048 u32 len;
49
50 ceph_decode_32_safe(&p, end, len, bad);
51 ceph_decode_need(&p, end, len, bad);
Sage Weilba75bb92009-10-06 11:31:11 -070052
Jeff Layton0bfb0f22019-05-31 15:32:28 -040053 dout("monmap_decode %p %p len %d (%d)\n", p, end, len, (int)(end-p));
Ilya Dryomovf3b4e552017-05-19 11:59:22 +020054 p += sizeof(u16); /* skip version */
Sage Weilba75bb92009-10-06 11:31:11 -070055
56 ceph_decode_need(&p, end, sizeof(fsid) + 2*sizeof(u32), bad);
57 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weilc89136e2009-10-14 09:59:09 -070058 epoch = ceph_decode_32(&p);
Sage Weilba75bb92009-10-06 11:31:11 -070059
Sage Weilc89136e2009-10-14 09:59:09 -070060 num_mon = ceph_decode_32(&p);
Sage Weilba75bb92009-10-06 11:31:11 -070061
Chengguang Xu73773242018-02-11 09:33:28 +080062 if (num_mon > CEPH_MAX_MON)
Sage Weilba75bb92009-10-06 11:31:11 -070063 goto bad;
Kees Cookacafe7e2018-05-08 13:45:50 -070064 m = kmalloc(struct_size(m, mon_inst, num_mon), GFP_NOFS);
Sage Weilba75bb92009-10-06 11:31:11 -070065 if (m == NULL)
66 return ERR_PTR(-ENOMEM);
67 m->fsid = fsid;
68 m->epoch = epoch;
69 m->num_mon = num_mon;
Jeff Layton0bfb0f22019-05-31 15:32:28 -040070 for (i = 0; i < num_mon; ++i) {
71 struct ceph_entity_inst *inst = &m->mon_inst[i];
Sage Weilba75bb92009-10-06 11:31:11 -070072
Jeff Layton0bfb0f22019-05-31 15:32:28 -040073 /* copy name portion */
74 ceph_decode_copy_safe(&p, end, &inst->name,
75 sizeof(inst->name), bad);
76 err = ceph_decode_entity_addr(&p, end, &inst->addr);
77 if (err)
78 goto bad;
79 }
Sage Weilba75bb92009-10-06 11:31:11 -070080 dout("monmap_decode epoch %d, num_mon %d\n", m->epoch,
81 m->num_mon);
82 for (i = 0; i < m->num_mon; i++)
83 dout("monmap_decode mon%d is %s\n", i,
Jeff Laytonb726ec92019-05-06 09:38:47 -040084 ceph_pr_addr(&m->mon_inst[i].addr));
Sage Weilba75bb92009-10-06 11:31:11 -070085 return m;
Sage Weilba75bb92009-10-06 11:31:11 -070086bad:
87 dout("monmap_decode failed with %d\n", err);
88 kfree(m);
89 return ERR_PTR(err);
90}
91
92/*
93 * return true if *addr is included in the monmap.
94 */
95int ceph_monmap_contains(struct ceph_monmap *m, struct ceph_entity_addr *addr)
96{
97 int i;
98
99 for (i = 0; i < m->num_mon; i++)
Sage Weil103e2d32010-01-07 16:12:36 -0800100 if (memcmp(addr, &m->mon_inst[i].addr, sizeof(*addr)) == 0)
Sage Weilba75bb92009-10-06 11:31:11 -0700101 return 1;
102 return 0;
103}
104
105/*
Sage Weil5ce6e9d2010-02-15 16:22:28 -0800106 * Send an auth request.
107 */
108static void __send_prepared_auth_request(struct ceph_mon_client *monc, int len)
109{
110 monc->pending_auth = 1;
111 monc->m_auth->front.iov_len = len;
112 monc->m_auth->hdr.front_len = cpu_to_le32(len);
Alex Elder6740a842012-06-01 14:56:43 -0500113 ceph_msg_revoke(monc->m_auth);
Sage Weil5ce6e9d2010-02-15 16:22:28 -0800114 ceph_msg_get(monc->m_auth); /* keep our ref */
Alex Elder67130932012-05-26 23:26:43 -0500115 ceph_con_send(&monc->con, monc->m_auth);
Sage Weil5ce6e9d2010-02-15 16:22:28 -0800116}
117
118/*
Sage Weilba75bb92009-10-06 11:31:11 -0700119 * Close monitor session, if any.
120 */
121static void __close_session(struct ceph_mon_client *monc)
122{
Sage Weilf6a2f5b2011-08-09 09:27:44 -0700123 dout("__close_session closing mon%d\n", monc->cur_mon);
Alex Elder6740a842012-06-01 14:56:43 -0500124 ceph_msg_revoke(monc->m_auth);
Sage Weil4f471e42012-07-30 18:16:40 -0700125 ceph_msg_revoke_incoming(monc->m_auth_reply);
126 ceph_msg_revoke(monc->m_subscribe);
127 ceph_msg_revoke_incoming(monc->m_subscribe_ack);
Alex Elder67130932012-05-26 23:26:43 -0500128 ceph_con_close(&monc->con);
Ilya Dryomov0e04dc22016-01-20 17:50:31 +0100129
Sage Weilf6a2f5b2011-08-09 09:27:44 -0700130 monc->pending_auth = 0;
131 ceph_auth_reset(monc->auth);
Sage Weilba75bb92009-10-06 11:31:11 -0700132}
133
134/*
Ilya Dryomov0e04dc22016-01-20 17:50:31 +0100135 * Pick a new monitor at random and set cur_mon. If we are repicking
136 * (i.e. cur_mon is already set), be sure to pick a different one.
Sage Weilba75bb92009-10-06 11:31:11 -0700137 */
Ilya Dryomov0e04dc22016-01-20 17:50:31 +0100138static void pick_new_mon(struct ceph_mon_client *monc)
Sage Weilba75bb92009-10-06 11:31:11 -0700139{
Ilya Dryomov0e04dc22016-01-20 17:50:31 +0100140 int old_mon = monc->cur_mon;
141
142 BUG_ON(monc->monmap->num_mon < 1);
143
144 if (monc->monmap->num_mon == 1) {
145 monc->cur_mon = 0;
146 } else {
147 int max = monc->monmap->num_mon;
148 int o = -1;
149 int n;
150
151 if (monc->cur_mon >= 0) {
152 if (monc->cur_mon < monc->monmap->num_mon)
153 o = monc->cur_mon;
154 if (o >= 0)
155 max--;
156 }
157
158 n = prandom_u32() % max;
159 if (o >= 0 && n >= o)
160 n++;
161
162 monc->cur_mon = n;
163 }
164
165 dout("%s mon%d -> mon%d out of %d mons\n", __func__, old_mon,
166 monc->cur_mon, monc->monmap->num_mon);
167}
168
169/*
170 * Open a session with a new monitor.
171 */
172static void __open_session(struct ceph_mon_client *monc)
173{
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800174 int ret;
Sage Weilba75bb92009-10-06 11:31:11 -0700175
Ilya Dryomov0e04dc22016-01-20 17:50:31 +0100176 pick_new_mon(monc);
Sage Weilba75bb92009-10-06 11:31:11 -0700177
Ilya Dryomov1752b502016-01-21 16:33:25 +0100178 monc->hunting = true;
Ilya Dryomov168b9092016-01-21 16:33:19 +0100179 if (monc->had_a_connection) {
180 monc->hunt_mult *= CEPH_MONC_HUNT_BACKOFF;
181 if (monc->hunt_mult > CEPH_MONC_HUNT_MAX_MULT)
182 monc->hunt_mult = CEPH_MONC_HUNT_MAX_MULT;
183 }
184
Ilya Dryomov0e04dc22016-01-20 17:50:31 +0100185 monc->sub_renew_after = jiffies; /* i.e., expired */
186 monc->sub_renew_sent = 0;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800187
Ilya Dryomov0e04dc22016-01-20 17:50:31 +0100188 dout("%s opening mon%d\n", __func__, monc->cur_mon);
189 ceph_con_open(&monc->con, CEPH_ENTITY_TYPE_MON, monc->cur_mon,
190 &monc->monmap->mon_inst[monc->cur_mon].addr);
Yan, Zheng8b9558a2015-09-01 17:19:38 +0800191
Ilya Dryomov0e04dc22016-01-20 17:50:31 +0100192 /*
193 * send an initial keepalive to ensure our timestamp is valid
194 * by the time we are in an OPENED state
195 */
196 ceph_con_keepalive(&monc->con);
197
198 /* initiate authentication handshake */
199 ret = ceph_auth_build_hello(monc->auth,
200 monc->m_auth->front.iov_base,
201 monc->m_auth->front_alloc_len);
202 BUG_ON(ret <= 0);
203 __send_prepared_auth_request(monc, ret);
Sage Weilba75bb92009-10-06 11:31:11 -0700204}
205
Ilya Dryomov1752b502016-01-21 16:33:25 +0100206static void reopen_session(struct ceph_mon_client *monc)
207{
208 if (!monc->hunting)
209 pr_info("mon%d %s session lost, hunting for new mon\n",
Jeff Laytonb726ec92019-05-06 09:38:47 -0400210 monc->cur_mon, ceph_pr_addr(&monc->con.peer_addr));
Ilya Dryomov1752b502016-01-21 16:33:25 +0100211
212 __close_session(monc);
213 __open_session(monc);
214}
215
Ilya Dryomovfacb9f62018-04-23 15:25:10 +0200216static void un_backoff(struct ceph_mon_client *monc)
217{
218 monc->hunt_mult /= 2; /* reduce by 50% */
219 if (monc->hunt_mult < 1)
220 monc->hunt_mult = 1;
221 dout("%s hunt_mult now %d\n", __func__, monc->hunt_mult);
222}
223
Sage Weilba75bb92009-10-06 11:31:11 -0700224/*
225 * Reschedule delayed work timer.
226 */
227static void __schedule_delayed(struct ceph_mon_client *monc)
228{
Yan, Zheng8b9558a2015-09-01 17:19:38 +0800229 unsigned long delay;
Sage Weilba75bb92009-10-06 11:31:11 -0700230
Ilya Dryomov168b9092016-01-21 16:33:19 +0100231 if (monc->hunting)
232 delay = CEPH_MONC_HUNT_INTERVAL * monc->hunt_mult;
233 else
Ilya Dryomov58d81b12016-01-21 16:33:15 +0100234 delay = CEPH_MONC_PING_INTERVAL;
Ilya Dryomov168b9092016-01-21 16:33:19 +0100235
Yan, Zheng8b9558a2015-09-01 17:19:38 +0800236 dout("__schedule_delayed after %lu\n", delay);
Ilya Dryomovbee3a372016-01-22 18:42:17 +0100237 mod_delayed_work(system_wq, &monc->delayed_work,
238 round_jiffies_relative(delay));
Sage Weilba75bb92009-10-06 11:31:11 -0700239}
240
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100241const char *ceph_sub_str[] = {
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100242 [CEPH_SUB_MONMAP] = "monmap",
243 [CEPH_SUB_OSDMAP] = "osdmap",
Yan, Zheng0cabbd92016-04-07 11:34:43 +0800244 [CEPH_SUB_FSMAP] = "fsmap.user",
245 [CEPH_SUB_MDSMAP] = "mdsmap",
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100246};
247
Sage Weilba75bb92009-10-06 11:31:11 -0700248/*
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100249 * Send subscribe request for one or more maps, according to
250 * monc->subs.
Sage Weilba75bb92009-10-06 11:31:11 -0700251 */
252static void __send_subscribe(struct ceph_mon_client *monc)
253{
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100254 struct ceph_msg *msg = monc->m_subscribe;
255 void *p = msg->front.iov_base;
256 void *const end = p + msg->front_alloc_len;
257 int num = 0;
258 int i;
Sage Weilba75bb92009-10-06 11:31:11 -0700259
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100260 dout("%s sent %lu\n", __func__, monc->sub_renew_sent);
Sage Weilba75bb92009-10-06 11:31:11 -0700261
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100262 BUG_ON(monc->cur_mon < 0);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700263
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100264 if (!monc->sub_renew_sent)
265 monc->sub_renew_sent = jiffies | 1; /* never 0 */
Sage Weilba75bb92009-10-06 11:31:11 -0700266
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100267 msg->hdr.version = cpu_to_le16(2);
Sage Weilba75bb92009-10-06 11:31:11 -0700268
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100269 for (i = 0; i < ARRAY_SIZE(monc->subs); i++) {
270 if (monc->subs[i].want)
271 num++;
Sage Weilba75bb92009-10-06 11:31:11 -0700272 }
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100273 BUG_ON(num < 1); /* monmap sub is always there */
274 ceph_encode_32(&p, num);
275 for (i = 0; i < ARRAY_SIZE(monc->subs); i++) {
Ilya Dryomov737cc81e2016-05-26 00:05:01 +0200276 char buf[32];
277 int len;
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100278
279 if (!monc->subs[i].want)
280 continue;
281
Ilya Dryomov737cc81e2016-05-26 00:05:01 +0200282 len = sprintf(buf, "%s", ceph_sub_str[i]);
283 if (i == CEPH_SUB_MDSMAP &&
284 monc->fs_cluster_id != CEPH_FS_CLUSTER_ID_NONE)
285 len += sprintf(buf + len, ".%d", monc->fs_cluster_id);
286
287 dout("%s %s start %llu flags 0x%x\n", __func__, buf,
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100288 le64_to_cpu(monc->subs[i].item.start),
289 monc->subs[i].item.flags);
Ilya Dryomov737cc81e2016-05-26 00:05:01 +0200290 ceph_encode_string(&p, end, buf, len);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100291 memcpy(p, &monc->subs[i].item, sizeof(monc->subs[i].item));
292 p += sizeof(monc->subs[i].item);
293 }
294
Ilya Dryomov737cc81e2016-05-26 00:05:01 +0200295 BUG_ON(p > end);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100296 msg->front.iov_len = p - msg->front.iov_base;
297 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
298 ceph_msg_revoke(msg);
299 ceph_con_send(&monc->con, ceph_msg_get(msg));
Sage Weilba75bb92009-10-06 11:31:11 -0700300}
301
302static void handle_subscribe_ack(struct ceph_mon_client *monc,
303 struct ceph_msg *msg)
304{
Eric Dumazet95c96172012-04-15 05:58:06 +0000305 unsigned int seconds;
Sage Weil07bd10f2009-10-14 17:26:40 -0700306 struct ceph_mon_subscribe_ack *h = msg->front.iov_base;
Sage Weilba75bb92009-10-06 11:31:11 -0700307
Sage Weil07bd10f2009-10-14 17:26:40 -0700308 if (msg->front.iov_len < sizeof(*h))
309 goto bad;
310 seconds = le32_to_cpu(h->duration);
311
Sage Weilba75bb92009-10-06 11:31:11 -0700312 mutex_lock(&monc->mutex);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100313 if (monc->sub_renew_sent) {
Ilya Dryomov220abf52017-06-05 14:45:00 +0200314 /*
315 * This is only needed for legacy (infernalis or older)
316 * MONs -- see delayed_work().
317 */
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100318 monc->sub_renew_after = monc->sub_renew_sent +
319 (seconds >> 1) * HZ - 1;
320 dout("%s sent %lu duration %d renew after %lu\n", __func__,
321 monc->sub_renew_sent, seconds, monc->sub_renew_after);
322 monc->sub_renew_sent = 0;
323 } else {
324 dout("%s sent %lu renew after %lu, ignoring\n", __func__,
325 monc->sub_renew_sent, monc->sub_renew_after);
326 }
Sage Weilba75bb92009-10-06 11:31:11 -0700327 mutex_unlock(&monc->mutex);
328 return;
329bad:
330 pr_err("got corrupt subscribe-ack msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -0800331 ceph_msg_dump(msg);
Sage Weilba75bb92009-10-06 11:31:11 -0700332}
333
334/*
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100335 * Register interest in a map
336 *
337 * @sub: one of CEPH_SUB_*
338 * @epoch: X for "every map since X", or 0 for "just the latest"
Sage Weilba75bb92009-10-06 11:31:11 -0700339 */
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100340static bool __ceph_monc_want_map(struct ceph_mon_client *monc, int sub,
341 u32 epoch, bool continuous)
Sage Weilba75bb92009-10-06 11:31:11 -0700342{
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100343 __le64 start = cpu_to_le64(epoch);
344 u8 flags = !continuous ? CEPH_SUBSCRIBE_ONETIME : 0;
Sage Weilba75bb92009-10-06 11:31:11 -0700345
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100346 dout("%s %s epoch %u continuous %d\n", __func__, ceph_sub_str[sub],
347 epoch, continuous);
348
349 if (monc->subs[sub].want &&
350 monc->subs[sub].item.start == start &&
351 monc->subs[sub].item.flags == flags)
352 return false;
353
354 monc->subs[sub].item.start = start;
355 monc->subs[sub].item.flags = flags;
356 monc->subs[sub].want = true;
357
358 return true;
359}
360
361bool ceph_monc_want_map(struct ceph_mon_client *monc, int sub, u32 epoch,
362 bool continuous)
363{
364 bool need_request;
365
366 mutex_lock(&monc->mutex);
367 need_request = __ceph_monc_want_map(monc, sub, epoch, continuous);
368 mutex_unlock(&monc->mutex);
369
370 return need_request;
371}
372EXPORT_SYMBOL(ceph_monc_want_map);
373
374/*
375 * Keep track of which maps we have
376 *
377 * @sub: one of CEPH_SUB_*
378 */
379static void __ceph_monc_got_map(struct ceph_mon_client *monc, int sub,
380 u32 epoch)
381{
382 dout("%s %s epoch %u\n", __func__, ceph_sub_str[sub], epoch);
383
384 if (monc->subs[sub].want) {
385 if (monc->subs[sub].item.flags & CEPH_SUBSCRIBE_ONETIME)
386 monc->subs[sub].want = false;
387 else
388 monc->subs[sub].item.start = cpu_to_le64(epoch + 1);
389 }
390
391 monc->subs[sub].have = epoch;
392}
393
394void ceph_monc_got_map(struct ceph_mon_client *monc, int sub, u32 epoch)
Sage Weilba75bb92009-10-06 11:31:11 -0700395{
396 mutex_lock(&monc->mutex);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100397 __ceph_monc_got_map(monc, sub, epoch);
Sage Weilba75bb92009-10-06 11:31:11 -0700398 mutex_unlock(&monc->mutex);
Sage Weilba75bb92009-10-06 11:31:11 -0700399}
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100400EXPORT_SYMBOL(ceph_monc_got_map);
Sage Weilba75bb92009-10-06 11:31:11 -0700401
Ilya Dryomov42c1b122016-04-28 16:07:25 +0200402void ceph_monc_renew_subs(struct ceph_mon_client *monc)
403{
404 mutex_lock(&monc->mutex);
405 __send_subscribe(monc);
406 mutex_unlock(&monc->mutex);
407}
408EXPORT_SYMBOL(ceph_monc_renew_subs);
409
Sage Weilba75bb92009-10-06 11:31:11 -0700410/*
Ilya Dryomova319bf52015-05-15 12:02:17 +0300411 * Wait for an osdmap with a given epoch.
412 *
413 * @epoch: epoch to wait for
414 * @timeout: in jiffies, 0 means "wait forever"
415 */
Ilya Dryomov6044cde2014-05-13 11:19:27 +0400416int ceph_monc_wait_osdmap(struct ceph_mon_client *monc, u32 epoch,
417 unsigned long timeout)
418{
419 unsigned long started = jiffies;
Ilya Dryomov216639d2015-05-19 12:03:33 +0300420 long ret;
Ilya Dryomov6044cde2014-05-13 11:19:27 +0400421
422 mutex_lock(&monc->mutex);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100423 while (monc->subs[CEPH_SUB_OSDMAP].have < epoch) {
Ilya Dryomov6044cde2014-05-13 11:19:27 +0400424 mutex_unlock(&monc->mutex);
425
Ilya Dryomova319bf52015-05-15 12:02:17 +0300426 if (timeout && time_after_eq(jiffies, started + timeout))
Ilya Dryomov6044cde2014-05-13 11:19:27 +0400427 return -ETIMEDOUT;
428
429 ret = wait_event_interruptible_timeout(monc->client->auth_wq,
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100430 monc->subs[CEPH_SUB_OSDMAP].have >= epoch,
431 ceph_timeout_jiffies(timeout));
Ilya Dryomov6044cde2014-05-13 11:19:27 +0400432 if (ret < 0)
433 return ret;
434
435 mutex_lock(&monc->mutex);
436 }
437
438 mutex_unlock(&monc->mutex);
439 return 0;
440}
441EXPORT_SYMBOL(ceph_monc_wait_osdmap);
Sage Weilba75bb92009-10-06 11:31:11 -0700442
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800443/*
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100444 * Open a session with a random monitor. Request monmap and osdmap,
445 * which are waited upon in __ceph_open_session().
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800446 */
447int ceph_monc_open_session(struct ceph_mon_client *monc)
Sage Weilba75bb92009-10-06 11:31:11 -0700448{
Sage Weilba75bb92009-10-06 11:31:11 -0700449 mutex_lock(&monc->mutex);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100450 __ceph_monc_want_map(monc, CEPH_SUB_MONMAP, 0, true);
451 __ceph_monc_want_map(monc, CEPH_SUB_OSDMAP, 0, false);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800452 __open_session(monc);
Sage Weilba75bb92009-10-06 11:31:11 -0700453 __schedule_delayed(monc);
454 mutex_unlock(&monc->mutex);
455 return 0;
456}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700457EXPORT_SYMBOL(ceph_monc_open_session);
Sage Weilba75bb92009-10-06 11:31:11 -0700458
Sage Weil07433042009-11-18 16:50:41 -0800459static void ceph_monc_handle_map(struct ceph_mon_client *monc,
460 struct ceph_msg *msg)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800461{
462 struct ceph_client *client = monc->client;
463 struct ceph_monmap *monmap = NULL, *old = monc->monmap;
464 void *p, *end;
465
466 mutex_lock(&monc->mutex);
467
468 dout("handle_monmap\n");
469 p = msg->front.iov_base;
470 end = p + msg->front.iov_len;
471
472 monmap = ceph_monmap_decode(p, end);
473 if (IS_ERR(monmap)) {
474 pr_err("problem decoding monmap, %d\n",
475 (int)PTR_ERR(monmap));
Jeff Layton0bfb0f22019-05-31 15:32:28 -0400476 ceph_msg_dump(msg);
Sage Weild4a780c2009-12-11 08:55:23 -0800477 goto out;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800478 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800479
Sage Weil07433042009-11-18 16:50:41 -0800480 if (ceph_check_fsid(monc->client, &monmap->fsid) < 0) {
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800481 kfree(monmap);
Sage Weild4a780c2009-12-11 08:55:23 -0800482 goto out;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800483 }
484
485 client->monc.monmap = monmap;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800486 kfree(old);
487
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100488 __ceph_monc_got_map(monc, CEPH_SUB_MONMAP, monc->monmap->epoch);
Ilya Dryomov02ac9562016-01-06 12:56:21 +0300489 client->have_fsid = true;
Sage Weild1c338a2012-08-19 12:29:16 -0700490
Sage Weild4a780c2009-12-11 08:55:23 -0800491out:
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800492 mutex_unlock(&monc->mutex);
Yehuda Sadeh03066f22010-07-27 13:11:08 -0700493 wake_up_all(&client->auth_wq);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800494}
Sage Weilba75bb92009-10-06 11:31:11 -0700495
Sage Weilba75bb92009-10-06 11:31:11 -0700496/*
Ilya Dryomov7a6fdeb2014-12-22 19:14:26 +0300497 * generic requests (currently statfs, mon_get_version)
Sage Weilba75bb92009-10-06 11:31:11 -0700498 */
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200499DEFINE_RB_FUNCS(generic_request, struct ceph_mon_generic_request, tid, node)
Sage Weil85ff03f2010-02-15 14:47:28 -0800500
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700501static void release_generic_request(struct kref *kref)
Sage Weil3143edd2010-03-24 21:43:33 -0700502{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700503 struct ceph_mon_generic_request *req =
504 container_of(kref, struct ceph_mon_generic_request, kref);
Sage Weil3143edd2010-03-24 21:43:33 -0700505
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200506 dout("%s greq %p request %p reply %p\n", __func__, req, req->request,
507 req->reply);
508 WARN_ON(!RB_EMPTY_NODE(&req->node));
509
Sage Weil3143edd2010-03-24 21:43:33 -0700510 if (req->reply)
511 ceph_msg_put(req->reply);
512 if (req->request)
513 ceph_msg_put(req->request);
Yehuda Sadeh20547562010-06-01 10:37:40 -0700514
515 kfree(req);
Sage Weil3143edd2010-03-24 21:43:33 -0700516}
517
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700518static void put_generic_request(struct ceph_mon_generic_request *req)
Sage Weil3143edd2010-03-24 21:43:33 -0700519{
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200520 if (req)
521 kref_put(&req->kref, release_generic_request);
Sage Weil3143edd2010-03-24 21:43:33 -0700522}
523
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700524static void get_generic_request(struct ceph_mon_generic_request *req)
Sage Weil3143edd2010-03-24 21:43:33 -0700525{
526 kref_get(&req->kref);
527}
528
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200529static struct ceph_mon_generic_request *
530alloc_generic_request(struct ceph_mon_client *monc, gfp_t gfp)
531{
532 struct ceph_mon_generic_request *req;
533
534 req = kzalloc(sizeof(*req), gfp);
535 if (!req)
536 return NULL;
537
538 req->monc = monc;
539 kref_init(&req->kref);
540 RB_CLEAR_NODE(&req->node);
541 init_completion(&req->completion);
542
543 dout("%s greq %p\n", __func__, req);
544 return req;
545}
546
547static void register_generic_request(struct ceph_mon_generic_request *req)
548{
549 struct ceph_mon_client *monc = req->monc;
550
551 WARN_ON(req->tid);
552
553 get_generic_request(req);
554 req->tid = ++monc->last_tid;
555 insert_generic_request(&monc->generic_request_tree, req);
556}
557
558static void send_generic_request(struct ceph_mon_client *monc,
559 struct ceph_mon_generic_request *req)
560{
561 WARN_ON(!req->tid);
562
563 dout("%s greq %p tid %llu\n", __func__, req, req->tid);
564 req->request->hdr.tid = cpu_to_le64(req->tid);
565 ceph_con_send(&monc->con, ceph_msg_get(req->request));
566}
567
568static void __finish_generic_request(struct ceph_mon_generic_request *req)
569{
570 struct ceph_mon_client *monc = req->monc;
571
572 dout("%s greq %p tid %llu\n", __func__, req, req->tid);
573 erase_generic_request(&monc->generic_request_tree, req);
574
575 ceph_msg_revoke(req->request);
576 ceph_msg_revoke_incoming(req->reply);
577}
578
579static void finish_generic_request(struct ceph_mon_generic_request *req)
580{
581 __finish_generic_request(req);
582 put_generic_request(req);
583}
584
585static void complete_generic_request(struct ceph_mon_generic_request *req)
586{
587 if (req->complete_cb)
588 req->complete_cb(req);
589 else
590 complete_all(&req->completion);
591 put_generic_request(req);
592}
593
Wei Yongjunf52ec332016-07-30 00:37:31 +0000594static void cancel_generic_request(struct ceph_mon_generic_request *req)
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200595{
596 struct ceph_mon_client *monc = req->monc;
597 struct ceph_mon_generic_request *lookup_req;
598
599 dout("%s greq %p tid %llu\n", __func__, req, req->tid);
600
601 mutex_lock(&monc->mutex);
602 lookup_req = lookup_generic_request(&monc->generic_request_tree,
603 req->tid);
604 if (lookup_req) {
605 WARN_ON(lookup_req != req);
606 finish_generic_request(req);
607 }
608
609 mutex_unlock(&monc->mutex);
610}
611
612static int wait_generic_request(struct ceph_mon_generic_request *req)
613{
614 int ret;
615
616 dout("%s greq %p tid %llu\n", __func__, req, req->tid);
617 ret = wait_for_completion_interruptible(&req->completion);
618 if (ret)
619 cancel_generic_request(req);
620 else
621 ret = req->result; /* completed */
622
623 return ret;
624}
625
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700626static struct ceph_msg *get_generic_reply(struct ceph_connection *con,
Sage Weil3143edd2010-03-24 21:43:33 -0700627 struct ceph_msg_header *hdr,
628 int *skip)
629{
630 struct ceph_mon_client *monc = con->private;
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700631 struct ceph_mon_generic_request *req;
Sage Weil3143edd2010-03-24 21:43:33 -0700632 u64 tid = le64_to_cpu(hdr->tid);
633 struct ceph_msg *m;
634
635 mutex_lock(&monc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200636 req = lookup_generic_request(&monc->generic_request_tree, tid);
Sage Weil3143edd2010-03-24 21:43:33 -0700637 if (!req) {
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700638 dout("get_generic_reply %lld dne\n", tid);
Sage Weil3143edd2010-03-24 21:43:33 -0700639 *skip = 1;
640 m = NULL;
641 } else {
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700642 dout("get_generic_reply %lld got %p\n", tid, req->reply);
Alex Elder1c20f2d2012-06-04 14:43:32 -0500643 *skip = 0;
Sage Weil3143edd2010-03-24 21:43:33 -0700644 m = ceph_msg_get(req->reply);
645 /*
646 * we don't need to track the connection reading into
647 * this reply because we only have one open connection
648 * at a time, ever.
649 */
650 }
651 mutex_unlock(&monc->mutex);
652 return m;
653}
654
Yehuda Sadehe56fa102010-05-17 12:40:28 -0700655/*
656 * statfs
657 */
Sage Weilba75bb92009-10-06 11:31:11 -0700658static void handle_statfs_reply(struct ceph_mon_client *monc,
659 struct ceph_msg *msg)
660{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700661 struct ceph_mon_generic_request *req;
Sage Weilba75bb92009-10-06 11:31:11 -0700662 struct ceph_mon_statfs_reply *reply = msg->front.iov_base;
Sage Weil3143edd2010-03-24 21:43:33 -0700663 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weilba75bb92009-10-06 11:31:11 -0700664
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200665 dout("%s msg %p tid %llu\n", __func__, msg, tid);
666
Sage Weilba75bb92009-10-06 11:31:11 -0700667 if (msg->front.iov_len != sizeof(*reply))
668 goto bad;
Sage Weilba75bb92009-10-06 11:31:11 -0700669
670 mutex_lock(&monc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200671 req = lookup_generic_request(&monc->generic_request_tree, tid);
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200672 if (!req) {
673 mutex_unlock(&monc->mutex);
674 return;
Sage Weilba75bb92009-10-06 11:31:11 -0700675 }
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200676
677 req->result = 0;
678 *req->u.st = reply->st; /* struct */
679 __finish_generic_request(req);
Sage Weilba75bb92009-10-06 11:31:11 -0700680 mutex_unlock(&monc->mutex);
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200681
682 complete_generic_request(req);
Sage Weilba75bb92009-10-06 11:31:11 -0700683 return;
684
685bad:
Ilya Dryomov7a6fdeb2014-12-22 19:14:26 +0300686 pr_err("corrupt statfs reply, tid %llu\n", tid);
Sage Weil9ec7cab2009-12-14 15:13:47 -0800687 ceph_msg_dump(msg);
Sage Weilba75bb92009-10-06 11:31:11 -0700688}
689
690/*
Sage Weilba75bb92009-10-06 11:31:11 -0700691 * Do a synchronous statfs().
692 */
Douglas Fuller06d74372017-08-16 10:19:27 -0400693int ceph_monc_do_statfs(struct ceph_mon_client *monc, u64 data_pool,
694 struct ceph_statfs *buf)
Sage Weilba75bb92009-10-06 11:31:11 -0700695{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700696 struct ceph_mon_generic_request *req;
Sage Weil3143edd2010-03-24 21:43:33 -0700697 struct ceph_mon_statfs *h;
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200698 int ret = -ENOMEM;
Sage Weilba75bb92009-10-06 11:31:11 -0700699
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200700 req = alloc_generic_request(monc, GFP_NOFS);
Sage Weil3143edd2010-03-24 21:43:33 -0700701 if (!req)
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200702 goto out;
Sage Weilba75bb92009-10-06 11:31:11 -0700703
Sage Weilb61c2762011-08-09 15:03:46 -0700704 req->request = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), GFP_NOFS,
705 true);
Sage Weila79832f2010-04-01 16:06:19 -0700706 if (!req->request)
Sage Weil3143edd2010-03-24 21:43:33 -0700707 goto out;
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200708
709 req->reply = ceph_msg_new(CEPH_MSG_STATFS_REPLY, 64, GFP_NOFS, true);
Sage Weila79832f2010-04-01 16:06:19 -0700710 if (!req->reply)
Sage Weil3143edd2010-03-24 21:43:33 -0700711 goto out;
Sage Weil3143edd2010-03-24 21:43:33 -0700712
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200713 req->u.st = buf;
Douglas Fuller06d74372017-08-16 10:19:27 -0400714 req->request->hdr.version = cpu_to_le16(2);
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200715
716 mutex_lock(&monc->mutex);
717 register_generic_request(req);
Sage Weil3143edd2010-03-24 21:43:33 -0700718 /* fill out request */
719 h = req->request->front.iov_base;
720 h->monhdr.have_version = 0;
721 h->monhdr.session_mon = cpu_to_le16(-1);
722 h->monhdr.session_mon_tid = 0;
723 h->fsid = monc->monmap->fsid;
Douglas Fuller06d74372017-08-16 10:19:27 -0400724 h->contains_data_pool = (data_pool != CEPH_NOPOOL);
725 h->data_pool = cpu_to_le64(data_pool);
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200726 send_generic_request(monc, req);
727 mutex_unlock(&monc->mutex);
Sage Weilba75bb92009-10-06 11:31:11 -0700728
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200729 ret = wait_generic_request(req);
Sage Weil3143edd2010-03-24 21:43:33 -0700730out:
Ilya Dryomovf6469122014-12-22 19:35:17 +0300731 put_generic_request(req);
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200732 return ret;
Sage Weilba75bb92009-10-06 11:31:11 -0700733}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700734EXPORT_SYMBOL(ceph_monc_do_statfs);
Sage Weilba75bb92009-10-06 11:31:11 -0700735
Ilya Dryomov513a8242014-05-13 11:19:26 +0400736static void handle_get_version_reply(struct ceph_mon_client *monc,
737 struct ceph_msg *msg)
738{
739 struct ceph_mon_generic_request *req;
740 u64 tid = le64_to_cpu(msg->hdr.tid);
741 void *p = msg->front.iov_base;
742 void *end = p + msg->front_alloc_len;
743 u64 handle;
744
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200745 dout("%s msg %p tid %llu\n", __func__, msg, tid);
Ilya Dryomov513a8242014-05-13 11:19:26 +0400746
747 ceph_decode_need(&p, end, 2*sizeof(u64), bad);
748 handle = ceph_decode_64(&p);
749 if (tid != 0 && tid != handle)
750 goto bad;
751
752 mutex_lock(&monc->mutex);
Ilya Dryomovfcd00b62016-04-28 16:07:22 +0200753 req = lookup_generic_request(&monc->generic_request_tree, handle);
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200754 if (!req) {
755 mutex_unlock(&monc->mutex);
756 return;
Ilya Dryomov513a8242014-05-13 11:19:26 +0400757 }
758
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200759 req->result = 0;
760 req->u.newest = ceph_decode_64(&p);
761 __finish_generic_request(req);
762 mutex_unlock(&monc->mutex);
763
764 complete_generic_request(req);
Ilya Dryomov513a8242014-05-13 11:19:26 +0400765 return;
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200766
Ilya Dryomov513a8242014-05-13 11:19:26 +0400767bad:
Ilya Dryomov7a6fdeb2014-12-22 19:14:26 +0300768 pr_err("corrupt mon_get_version reply, tid %llu\n", tid);
Ilya Dryomov513a8242014-05-13 11:19:26 +0400769 ceph_msg_dump(msg);
770}
771
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200772static struct ceph_mon_generic_request *
773__ceph_monc_get_version(struct ceph_mon_client *monc, const char *what,
774 ceph_monc_callback_t cb, u64 private_data)
775{
776 struct ceph_mon_generic_request *req;
777
778 req = alloc_generic_request(monc, GFP_NOIO);
779 if (!req)
780 goto err_put_req;
781
782 req->request = ceph_msg_new(CEPH_MSG_MON_GET_VERSION,
783 sizeof(u64) + sizeof(u32) + strlen(what),
784 GFP_NOIO, true);
785 if (!req->request)
786 goto err_put_req;
787
788 req->reply = ceph_msg_new(CEPH_MSG_MON_GET_VERSION_REPLY, 32, GFP_NOIO,
789 true);
790 if (!req->reply)
791 goto err_put_req;
792
793 req->complete_cb = cb;
794 req->private_data = private_data;
795
796 mutex_lock(&monc->mutex);
797 register_generic_request(req);
798 {
799 void *p = req->request->front.iov_base;
800 void *const end = p + req->request->front_alloc_len;
801
802 ceph_encode_64(&p, req->tid); /* handle */
803 ceph_encode_string(&p, end, what, strlen(what));
804 WARN_ON(p != end);
805 }
806 send_generic_request(monc, req);
807 mutex_unlock(&monc->mutex);
808
809 return req;
810
811err_put_req:
812 put_generic_request(req);
813 return ERR_PTR(-ENOMEM);
814}
815
Ilya Dryomov513a8242014-05-13 11:19:26 +0400816/*
817 * Send MMonGetVersion and wait for the reply.
818 *
819 * @what: one of "mdsmap", "osdmap" or "monmap"
820 */
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200821int ceph_monc_get_version(struct ceph_mon_client *monc, const char *what,
822 u64 *newest)
Ilya Dryomov513a8242014-05-13 11:19:26 +0400823{
824 struct ceph_mon_generic_request *req;
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200825 int ret;
Ilya Dryomov513a8242014-05-13 11:19:26 +0400826
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200827 req = __ceph_monc_get_version(monc, what, NULL, 0);
828 if (IS_ERR(req))
829 return PTR_ERR(req);
Ilya Dryomov513a8242014-05-13 11:19:26 +0400830
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200831 ret = wait_generic_request(req);
832 if (!ret)
833 *newest = req->u.newest;
Ilya Dryomov513a8242014-05-13 11:19:26 +0400834
Ilya Dryomovf6469122014-12-22 19:35:17 +0300835 put_generic_request(req);
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200836 return ret;
Ilya Dryomov513a8242014-05-13 11:19:26 +0400837}
Ilya Dryomovd0b19702016-04-28 16:07:27 +0200838EXPORT_SYMBOL(ceph_monc_get_version);
839
840/*
841 * Send MMonGetVersion,
842 *
843 * @what: one of "mdsmap", "osdmap" or "monmap"
844 */
845int ceph_monc_get_version_async(struct ceph_mon_client *monc, const char *what,
846 ceph_monc_callback_t cb, u64 private_data)
847{
848 struct ceph_mon_generic_request *req;
849
850 req = __ceph_monc_get_version(monc, what, cb, private_data);
851 if (IS_ERR(req))
852 return PTR_ERR(req);
853
854 put_generic_request(req);
855 return 0;
856}
857EXPORT_SYMBOL(ceph_monc_get_version_async);
Ilya Dryomov513a8242014-05-13 11:19:26 +0400858
Douglas Fuller6305a3b2015-07-22 20:59:52 -0400859static void handle_command_ack(struct ceph_mon_client *monc,
860 struct ceph_msg *msg)
861{
862 struct ceph_mon_generic_request *req;
863 void *p = msg->front.iov_base;
864 void *const end = p + msg->front_alloc_len;
865 u64 tid = le64_to_cpu(msg->hdr.tid);
866
867 dout("%s msg %p tid %llu\n", __func__, msg, tid);
868
869 ceph_decode_need(&p, end, sizeof(struct ceph_mon_request_header) +
870 sizeof(u32), bad);
871 p += sizeof(struct ceph_mon_request_header);
872
873 mutex_lock(&monc->mutex);
874 req = lookup_generic_request(&monc->generic_request_tree, tid);
875 if (!req) {
876 mutex_unlock(&monc->mutex);
877 return;
878 }
879
880 req->result = ceph_decode_32(&p);
881 __finish_generic_request(req);
882 mutex_unlock(&monc->mutex);
883
884 complete_generic_request(req);
885 return;
886
887bad:
888 pr_err("corrupt mon_command ack, tid %llu\n", tid);
889 ceph_msg_dump(msg);
890}
891
892int ceph_monc_blacklist_add(struct ceph_mon_client *monc,
893 struct ceph_entity_addr *client_addr)
894{
895 struct ceph_mon_generic_request *req;
896 struct ceph_mon_command *h;
897 int ret = -ENOMEM;
898 int len;
899
900 req = alloc_generic_request(monc, GFP_NOIO);
901 if (!req)
902 goto out;
903
904 req->request = ceph_msg_new(CEPH_MSG_MON_COMMAND, 256, GFP_NOIO, true);
905 if (!req->request)
906 goto out;
907
908 req->reply = ceph_msg_new(CEPH_MSG_MON_COMMAND_ACK, 512, GFP_NOIO,
909 true);
910 if (!req->reply)
911 goto out;
912
913 mutex_lock(&monc->mutex);
914 register_generic_request(req);
915 h = req->request->front.iov_base;
916 h->monhdr.have_version = 0;
917 h->monhdr.session_mon = cpu_to_le16(-1);
918 h->monhdr.session_mon_tid = 0;
919 h->fsid = monc->monmap->fsid;
920 h->num_strs = cpu_to_le32(1);
921 len = sprintf(h->str, "{ \"prefix\": \"osd blacklist\", \
922 \"blacklistop\": \"add\", \
923 \"addr\": \"%pISpc/%u\" }",
924 &client_addr->in_addr, le32_to_cpu(client_addr->nonce));
925 h->str_len = cpu_to_le32(len);
926 send_generic_request(monc, req);
927 mutex_unlock(&monc->mutex);
928
929 ret = wait_generic_request(req);
Ilya Dryomovbb229bb2019-03-20 09:46:58 +0100930 if (!ret)
931 /*
932 * Make sure we have the osdmap that includes the blacklist
933 * entry. This is needed to ensure that the OSDs pick up the
934 * new blacklist before processing any future requests from
935 * this client.
936 */
937 ret = ceph_wait_for_latest_osdmap(monc->client, 0);
938
Douglas Fuller6305a3b2015-07-22 20:59:52 -0400939out:
940 put_generic_request(req);
941 return ret;
942}
943EXPORT_SYMBOL(ceph_monc_blacklist_add);
944
Sage Weilba75bb92009-10-06 11:31:11 -0700945/*
Yehuda Sadehe56fa102010-05-17 12:40:28 -0700946 * Resend pending generic requests.
Sage Weilba75bb92009-10-06 11:31:11 -0700947 */
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700948static void __resend_generic_request(struct ceph_mon_client *monc)
Sage Weilba75bb92009-10-06 11:31:11 -0700949{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700950 struct ceph_mon_generic_request *req;
Sage Weil85ff03f2010-02-15 14:47:28 -0800951 struct rb_node *p;
Sage Weilba75bb92009-10-06 11:31:11 -0700952
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700953 for (p = rb_first(&monc->generic_request_tree); p; p = rb_next(p)) {
954 req = rb_entry(p, struct ceph_mon_generic_request, node);
Alex Elder6740a842012-06-01 14:56:43 -0500955 ceph_msg_revoke(req->request);
Sage Weil4f471e42012-07-30 18:16:40 -0700956 ceph_msg_revoke_incoming(req->reply);
Alex Elder67130932012-05-26 23:26:43 -0500957 ceph_con_send(&monc->con, ceph_msg_get(req->request));
Sage Weilba75bb92009-10-06 11:31:11 -0700958 }
959}
960
961/*
962 * Delayed work. If we haven't mounted yet, retry. Otherwise,
963 * renew/retry subscription as needed (in case it is timing out, or we
964 * got an ENOMEM). And keep the monitor connection alive.
965 */
966static void delayed_work(struct work_struct *work)
967{
968 struct ceph_mon_client *monc =
969 container_of(work, struct ceph_mon_client, delayed_work.work);
970
971 dout("monc delayed_work\n");
972 mutex_lock(&monc->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800973 if (monc->hunting) {
Ilya Dryomov1752b502016-01-21 16:33:25 +0100974 dout("%s continuing hunt\n", __func__);
975 reopen_session(monc);
Sage Weilba75bb92009-10-06 11:31:11 -0700976 } else {
Yan, Zheng8b9558a2015-09-01 17:19:38 +0800977 int is_auth = ceph_auth_is_authenticated(monc->auth);
978 if (ceph_con_keepalive_expired(&monc->con,
Ilya Dryomov58d81b12016-01-21 16:33:15 +0100979 CEPH_MONC_PING_TIMEOUT)) {
Yan, Zheng8b9558a2015-09-01 17:19:38 +0800980 dout("monc keepalive timeout\n");
981 is_auth = 0;
Ilya Dryomov1752b502016-01-21 16:33:25 +0100982 reopen_session(monc);
Yan, Zheng8b9558a2015-09-01 17:19:38 +0800983 }
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800984
Yan, Zheng8b9558a2015-09-01 17:19:38 +0800985 if (!monc->hunting) {
986 ceph_con_keepalive(&monc->con);
987 __validate_auth(monc);
Ilya Dryomovfacb9f62018-04-23 15:25:10 +0200988 un_backoff(monc);
Yan, Zheng8b9558a2015-09-01 17:19:38 +0800989 }
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800990
Ilya Dryomov220abf52017-06-05 14:45:00 +0200991 if (is_auth &&
992 !(monc->con.peer_features & CEPH_FEATURE_MON_STATEFUL_SUB)) {
Ilya Dryomov82dcaba2016-01-19 16:19:06 +0100993 unsigned long now = jiffies;
994
995 dout("%s renew subs? now %lu renew after %lu\n",
996 __func__, now, monc->sub_renew_after);
997 if (time_after_eq(now, monc->sub_renew_after))
998 __send_subscribe(monc);
999 }
Sage Weilba75bb92009-10-06 11:31:11 -07001000 }
Sage Weilba75bb92009-10-06 11:31:11 -07001001 __schedule_delayed(monc);
1002 mutex_unlock(&monc->mutex);
1003}
1004
Sage Weil6b805182009-10-27 11:50:50 -07001005/*
1006 * On startup, we build a temporary monmap populated with the IPs
1007 * provided by mount(2).
1008 */
1009static int build_initial_monmap(struct ceph_mon_client *monc)
1010{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001011 struct ceph_options *opt = monc->client->options;
1012 struct ceph_entity_addr *mon_addr = opt->mon_addr;
1013 int num_mon = opt->num_mon;
Sage Weil6b805182009-10-27 11:50:50 -07001014 int i;
1015
1016 /* build initial monmap */
Kees Cookacafe7e2018-05-08 13:45:50 -07001017 monc->monmap = kzalloc(struct_size(monc->monmap, mon_inst, num_mon),
Sage Weil6b805182009-10-27 11:50:50 -07001018 GFP_KERNEL);
1019 if (!monc->monmap)
1020 return -ENOMEM;
1021 for (i = 0; i < num_mon; i++) {
1022 monc->monmap->mon_inst[i].addr = mon_addr[i];
Sage Weil6b805182009-10-27 11:50:50 -07001023 monc->monmap->mon_inst[i].addr.nonce = 0;
1024 monc->monmap->mon_inst[i].name.type =
1025 CEPH_ENTITY_TYPE_MON;
1026 monc->monmap->mon_inst[i].name.num = cpu_to_le64(i);
1027 }
1028 monc->monmap->num_mon = num_mon;
Sage Weil6b805182009-10-27 11:50:50 -07001029 return 0;
1030}
1031
Sage Weilba75bb92009-10-06 11:31:11 -07001032int ceph_monc_init(struct ceph_mon_client *monc, struct ceph_client *cl)
1033{
1034 int err = 0;
1035
1036 dout("init\n");
1037 memset(monc, 0, sizeof(*monc));
1038 monc->client = cl;
1039 monc->monmap = NULL;
1040 mutex_init(&monc->mutex);
1041
Sage Weil6b805182009-10-27 11:50:50 -07001042 err = build_initial_monmap(monc);
1043 if (err)
1044 goto out;
1045
Sage Weilf6a2f5b2011-08-09 09:27:44 -07001046 /* connection */
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001047 /* authentication */
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001048 monc->auth = ceph_auth_init(cl->options->name,
Tommi Virtanen8323c3a2011-03-25 16:32:57 -07001049 cl->options->key);
Noah Watkins49d92242011-09-12 14:51:58 -07001050 if (IS_ERR(monc->auth)) {
1051 err = PTR_ERR(monc->auth);
Alex Elder67130932012-05-26 23:26:43 -05001052 goto out_monmap;
Noah Watkins49d92242011-09-12 14:51:58 -07001053 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001054 monc->auth->want_keys =
1055 CEPH_ENTITY_TYPE_AUTH | CEPH_ENTITY_TYPE_MON |
1056 CEPH_ENTITY_TYPE_OSD | CEPH_ENTITY_TYPE_MDS;
1057
Sage Weil240ed682010-05-21 14:57:25 -07001058 /* msgs */
Sage Weila79832f2010-04-01 16:06:19 -07001059 err = -ENOMEM;
Sage Weil7c315c52010-03-24 21:52:30 -07001060 monc->m_subscribe_ack = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE_ACK,
Yehuda Sadeh34d23762010-04-06 14:33:58 -07001061 sizeof(struct ceph_mon_subscribe_ack),
Ilya Dryomov5418d0a2016-12-02 16:35:08 +01001062 GFP_KERNEL, true);
Sage Weila79832f2010-04-01 16:06:19 -07001063 if (!monc->m_subscribe_ack)
Noah Watkins49d92242011-09-12 14:51:58 -07001064 goto out_auth;
Sage Weil6694d6b2010-03-24 21:48:05 -07001065
Ilya Dryomov5418d0a2016-12-02 16:35:08 +01001066 monc->m_subscribe = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 128,
1067 GFP_KERNEL, true);
Sage Weil240ed682010-05-21 14:57:25 -07001068 if (!monc->m_subscribe)
1069 goto out_subscribe_ack;
1070
Ilya Dryomov5418d0a2016-12-02 16:35:08 +01001071 monc->m_auth_reply = ceph_msg_new(CEPH_MSG_AUTH_REPLY, 4096,
1072 GFP_KERNEL, true);
Sage Weila79832f2010-04-01 16:06:19 -07001073 if (!monc->m_auth_reply)
Sage Weil240ed682010-05-21 14:57:25 -07001074 goto out_subscribe;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001075
Ilya Dryomov5418d0a2016-12-02 16:35:08 +01001076 monc->m_auth = ceph_msg_new(CEPH_MSG_AUTH, 4096, GFP_KERNEL, true);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08001077 monc->pending_auth = 0;
Sage Weila79832f2010-04-01 16:06:19 -07001078 if (!monc->m_auth)
Sage Weil6694d6b2010-03-24 21:48:05 -07001079 goto out_auth_reply;
Sage Weilba75bb92009-10-06 11:31:11 -07001080
Sage Weil735a72e2012-06-27 12:24:34 -07001081 ceph_con_init(&monc->con, monc, &mon_con_ops,
1082 &monc->client->msgr);
1083
Sage Weilba75bb92009-10-06 11:31:11 -07001084 monc->cur_mon = -1;
Ilya Dryomov168b9092016-01-21 16:33:19 +01001085 monc->had_a_connection = false;
1086 monc->hunt_mult = 1;
Sage Weilba75bb92009-10-06 11:31:11 -07001087
1088 INIT_DELAYED_WORK(&monc->delayed_work, delayed_work);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -07001089 monc->generic_request_tree = RB_ROOT;
Sage Weilba75bb92009-10-06 11:31:11 -07001090 monc->last_tid = 0;
1091
Ilya Dryomov737cc81e2016-05-26 00:05:01 +02001092 monc->fs_cluster_id = CEPH_FS_CLUSTER_ID_NONE;
1093
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001094 return 0;
1095
Sage Weil6694d6b2010-03-24 21:48:05 -07001096out_auth_reply:
1097 ceph_msg_put(monc->m_auth_reply);
Sage Weil240ed682010-05-21 14:57:25 -07001098out_subscribe:
1099 ceph_msg_put(monc->m_subscribe);
Sage Weil7c315c52010-03-24 21:52:30 -07001100out_subscribe_ack:
1101 ceph_msg_put(monc->m_subscribe_ack);
Noah Watkins49d92242011-09-12 14:51:58 -07001102out_auth:
1103 ceph_auth_destroy(monc->auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001104out_monmap:
1105 kfree(monc->monmap);
Sage Weilba75bb92009-10-06 11:31:11 -07001106out:
1107 return err;
1108}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001109EXPORT_SYMBOL(ceph_monc_init);
Sage Weilba75bb92009-10-06 11:31:11 -07001110
1111void ceph_monc_stop(struct ceph_mon_client *monc)
1112{
1113 dout("stop\n");
1114 cancel_delayed_work_sync(&monc->delayed_work);
1115
1116 mutex_lock(&monc->mutex);
1117 __close_session(monc);
Ilya Dryomov0e04dc22016-01-20 17:50:31 +01001118 monc->cur_mon = -1;
Sage Weilba75bb92009-10-06 11:31:11 -07001119 mutex_unlock(&monc->mutex);
1120
Sage Weilf3dea7e2012-06-10 20:43:56 -07001121 /*
1122 * flush msgr queue before we destroy ourselves to ensure that:
1123 * - any work that references our embedded con is finished.
1124 * - any osd_client or other work that may reference an authorizer
1125 * finishes before we shut down the auth subsystem.
1126 */
1127 ceph_msgr_flush();
1128
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001129 ceph_auth_destroy(monc->auth);
1130
Ilya Dryomovd0b19702016-04-28 16:07:27 +02001131 WARN_ON(!RB_EMPTY_ROOT(&monc->generic_request_tree));
1132
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001133 ceph_msg_put(monc->m_auth);
Sage Weil6694d6b2010-03-24 21:48:05 -07001134 ceph_msg_put(monc->m_auth_reply);
Sage Weil240ed682010-05-21 14:57:25 -07001135 ceph_msg_put(monc->m_subscribe);
Sage Weil7c315c52010-03-24 21:52:30 -07001136 ceph_msg_put(monc->m_subscribe_ack);
Sage Weilba75bb92009-10-06 11:31:11 -07001137
1138 kfree(monc->monmap);
1139}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001140EXPORT_SYMBOL(ceph_monc_stop);
Sage Weilba75bb92009-10-06 11:31:11 -07001141
Ilya Dryomov0f9af162016-01-08 21:17:22 +03001142static void finish_hunting(struct ceph_mon_client *monc)
1143{
1144 if (monc->hunting) {
1145 dout("%s found mon%d\n", __func__, monc->cur_mon);
1146 monc->hunting = false;
Ilya Dryomov168b9092016-01-21 16:33:19 +01001147 monc->had_a_connection = true;
Ilya Dryomovfacb9f62018-04-23 15:25:10 +02001148 un_backoff(monc);
Ilya Dryomov7b4c4432018-04-23 15:25:10 +02001149 __schedule_delayed(monc);
Ilya Dryomov0f9af162016-01-08 21:17:22 +03001150 }
1151}
1152
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001153static void handle_auth_reply(struct ceph_mon_client *monc,
1154 struct ceph_msg *msg)
1155{
1156 int ret;
Sage Weil09c4d6a2010-05-25 15:38:06 -07001157 int was_auth = 0;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001158
1159 mutex_lock(&monc->mutex);
Sage Weil27859f92013-03-25 10:26:14 -07001160 was_auth = ceph_auth_is_authenticated(monc->auth);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08001161 monc->pending_auth = 0;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001162 ret = ceph_handle_auth_reply(monc->auth, msg->front.iov_base,
1163 msg->front.iov_len,
1164 monc->m_auth->front.iov_base,
Ilya Dryomov3cea4c32014-01-09 20:08:21 +02001165 monc->m_auth->front_alloc_len);
Ilya Dryomov0f9af162016-01-08 21:17:22 +03001166 if (ret > 0) {
1167 __send_prepared_auth_request(monc, ret);
1168 goto out;
1169 }
1170
1171 finish_hunting(monc);
1172
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001173 if (ret < 0) {
Sage Weil9bd2e6f2010-02-02 16:21:06 -08001174 monc->client->auth_err = ret;
Sage Weil27859f92013-03-25 10:26:14 -07001175 } else if (!was_auth && ceph_auth_is_authenticated(monc->auth)) {
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001176 dout("authenticated, starting session\n");
Sage Weil07433042009-11-18 16:50:41 -08001177
Alex Elder15d98822012-05-26 23:26:43 -05001178 monc->client->msgr.inst.name.type = CEPH_ENTITY_TYPE_CLIENT;
1179 monc->client->msgr.inst.name.num =
Yehuda Sadeh0cf55372010-06-11 15:57:06 -07001180 cpu_to_le64(monc->auth->global_id);
Sage Weil07433042009-11-18 16:50:41 -08001181
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001182 __send_subscribe(monc);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -07001183 __resend_generic_request(monc);
Ilya Dryomov0f9af162016-01-08 21:17:22 +03001184
1185 pr_info("mon%d %s session established\n", monc->cur_mon,
Jeff Laytonb726ec92019-05-06 09:38:47 -04001186 ceph_pr_addr(&monc->con.peer_addr));
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001187 }
Ilya Dryomov0f9af162016-01-08 21:17:22 +03001188
1189out:
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001190 mutex_unlock(&monc->mutex);
Ilya Dryomov0f9af162016-01-08 21:17:22 +03001191 if (monc->client->auth_err < 0)
1192 wake_up_all(&monc->client->auth_wq);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001193}
1194
Sage Weil9bd2e6f2010-02-02 16:21:06 -08001195static int __validate_auth(struct ceph_mon_client *monc)
1196{
1197 int ret;
1198
1199 if (monc->pending_auth)
1200 return 0;
1201
1202 ret = ceph_build_auth(monc->auth, monc->m_auth->front.iov_base,
Ilya Dryomov3cea4c32014-01-09 20:08:21 +02001203 monc->m_auth->front_alloc_len);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08001204 if (ret <= 0)
1205 return ret; /* either an error, or no need to authenticate */
1206 __send_prepared_auth_request(monc, ret);
1207 return 0;
1208}
1209
1210int ceph_monc_validate_auth(struct ceph_mon_client *monc)
1211{
1212 int ret;
1213
1214 mutex_lock(&monc->mutex);
1215 ret = __validate_auth(monc);
1216 mutex_unlock(&monc->mutex);
1217 return ret;
1218}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001219EXPORT_SYMBOL(ceph_monc_validate_auth);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08001220
Sage Weilba75bb92009-10-06 11:31:11 -07001221/*
1222 * handle incoming message
1223 */
1224static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1225{
1226 struct ceph_mon_client *monc = con->private;
1227 int type = le16_to_cpu(msg->hdr.type);
1228
1229 if (!monc)
1230 return;
1231
1232 switch (type) {
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001233 case CEPH_MSG_AUTH_REPLY:
1234 handle_auth_reply(monc, msg);
Sage Weilba75bb92009-10-06 11:31:11 -07001235 break;
1236
1237 case CEPH_MSG_MON_SUBSCRIBE_ACK:
1238 handle_subscribe_ack(monc, msg);
1239 break;
1240
1241 case CEPH_MSG_STATFS_REPLY:
1242 handle_statfs_reply(monc, msg);
1243 break;
1244
Ilya Dryomov513a8242014-05-13 11:19:26 +04001245 case CEPH_MSG_MON_GET_VERSION_REPLY:
1246 handle_get_version_reply(monc, msg);
1247 break;
1248
Douglas Fuller6305a3b2015-07-22 20:59:52 -04001249 case CEPH_MSG_MON_COMMAND_ACK:
1250 handle_command_ack(monc, msg);
1251 break;
1252
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001253 case CEPH_MSG_MON_MAP:
1254 ceph_monc_handle_map(monc, msg);
1255 break;
1256
Sage Weilba75bb92009-10-06 11:31:11 -07001257 case CEPH_MSG_OSD_MAP:
1258 ceph_osdc_handle_map(&monc->client->osdc, msg);
1259 break;
1260
1261 default:
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001262 /* can the chained handler handle it? */
1263 if (monc->client->extra_mon_dispatch &&
1264 monc->client->extra_mon_dispatch(monc->client, msg) == 0)
1265 break;
Stephen Hemminger24e1dd62018-07-24 12:29:07 -07001266
Sage Weilba75bb92009-10-06 11:31:11 -07001267 pr_err("received unknown message type %d %s\n", type,
1268 ceph_msg_type_name(type));
1269 }
1270 ceph_msg_put(msg);
1271}
1272
1273/*
1274 * Allocate memory for incoming message
1275 */
1276static struct ceph_msg *mon_alloc_msg(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001277 struct ceph_msg_header *hdr,
1278 int *skip)
Sage Weilba75bb92009-10-06 11:31:11 -07001279{
1280 struct ceph_mon_client *monc = con->private;
1281 int type = le16_to_cpu(hdr->type);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001282 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -08001283 struct ceph_msg *m = NULL;
Sage Weilba75bb92009-10-06 11:31:11 -07001284
Yehuda Sadeh24504182010-01-08 13:58:34 -08001285 *skip = 0;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001286
Sage Weilba75bb92009-10-06 11:31:11 -07001287 switch (type) {
Sage Weilba75bb92009-10-06 11:31:11 -07001288 case CEPH_MSG_MON_SUBSCRIBE_ACK:
Sage Weil7c315c52010-03-24 21:52:30 -07001289 m = ceph_msg_get(monc->m_subscribe_ack);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001290 break;
Sage Weilba75bb92009-10-06 11:31:11 -07001291 case CEPH_MSG_STATFS_REPLY:
Douglas Fuller6305a3b2015-07-22 20:59:52 -04001292 case CEPH_MSG_MON_COMMAND_ACK:
Yehuda Sadehf8c76f62010-04-22 15:40:37 -07001293 return get_generic_reply(con, hdr, skip);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001294 case CEPH_MSG_AUTH_REPLY:
Sage Weil6694d6b2010-03-24 21:48:05 -07001295 m = ceph_msg_get(monc->m_auth_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001296 break;
Ilya Dryomov513a8242014-05-13 11:19:26 +04001297 case CEPH_MSG_MON_GET_VERSION_REPLY:
1298 if (le64_to_cpu(hdr->tid) != 0)
1299 return get_generic_reply(con, hdr, skip);
1300
1301 /*
1302 * Older OSDs don't set reply tid even if the orignal
Gustavo A. R. Silva18370b32017-10-15 12:55:23 -05001303 * request had a non-zero tid. Work around this weirdness
1304 * by allocating a new message.
Ilya Dryomov513a8242014-05-13 11:19:26 +04001305 */
Gustavo A. R. Silva18370b32017-10-15 12:55:23 -05001306 /* fall through */
Sage Weil5b3a4db2010-02-19 21:43:23 -08001307 case CEPH_MSG_MON_MAP:
1308 case CEPH_MSG_MDS_MAP:
1309 case CEPH_MSG_OSD_MAP:
Yan, Zheng0cabbd92016-04-07 11:34:43 +08001310 case CEPH_MSG_FS_MAP_USER:
Sage Weilb61c2762011-08-09 15:03:46 -07001311 m = ceph_msg_new(type, front_len, GFP_NOFS, false);
Alex Elder1c20f2d2012-06-04 14:43:32 -05001312 if (!m)
1313 return NULL; /* ENOMEM--return skip == 0 */
Sage Weil5b3a4db2010-02-19 21:43:23 -08001314 break;
Sage Weilba75bb92009-10-06 11:31:11 -07001315 }
Yehuda Sadeh24504182010-01-08 13:58:34 -08001316
Sage Weil5b3a4db2010-02-19 21:43:23 -08001317 if (!m) {
1318 pr_info("alloc_msg unknown type %d\n", type);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001319 *skip = 1;
Sage Weil73c3d482014-08-04 07:01:54 -07001320 } else if (front_len > m->front_alloc_len) {
Joe Perchesb9a67892014-09-09 21:17:29 -07001321 pr_warn("mon_alloc_msg front %d > prealloc %d (%u#%llu)\n",
1322 front_len, m->front_alloc_len,
1323 (unsigned int)con->peer_name.type,
1324 le64_to_cpu(con->peer_name.num));
Sage Weil73c3d482014-08-04 07:01:54 -07001325 ceph_msg_put(m);
1326 m = ceph_msg_new(type, front_len, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08001327 }
Sage Weil73c3d482014-08-04 07:01:54 -07001328
Yehuda Sadeh24504182010-01-08 13:58:34 -08001329 return m;
Sage Weilba75bb92009-10-06 11:31:11 -07001330}
1331
1332/*
1333 * If the monitor connection resets, pick a new monitor and resubmit
1334 * any pending requests.
1335 */
1336static void mon_fault(struct ceph_connection *con)
1337{
1338 struct ceph_mon_client *monc = con->private;
1339
Sage Weilba75bb92009-10-06 11:31:11 -07001340 mutex_lock(&monc->mutex);
Ilya Dryomovb5d91702016-01-23 15:57:51 +01001341 dout("%s mon%d\n", __func__, monc->cur_mon);
1342 if (monc->cur_mon >= 0) {
1343 if (!monc->hunting) {
1344 dout("%s hunting for new mon\n", __func__);
1345 reopen_session(monc);
1346 __schedule_delayed(monc);
1347 } else {
1348 dout("%s already hunting\n", __func__);
1349 }
Sage Weilba75bb92009-10-06 11:31:11 -07001350 }
Sage Weilba75bb92009-10-06 11:31:11 -07001351 mutex_unlock(&monc->mutex);
1352}
1353
Sage Weilec87ef42012-05-31 20:27:50 -07001354/*
1355 * We can ignore refcounting on the connection struct, as all references
1356 * will come from the messenger workqueue, which is drained prior to
1357 * mon_client destruction.
1358 */
1359static struct ceph_connection *con_get(struct ceph_connection *con)
1360{
1361 return con;
1362}
1363
1364static void con_put(struct ceph_connection *con)
1365{
1366}
1367
Tobias Klauser9e327892010-05-20 10:40:19 +02001368static const struct ceph_connection_operations mon_con_ops = {
Sage Weilec87ef42012-05-31 20:27:50 -07001369 .get = con_get,
1370 .put = con_put,
Sage Weilba75bb92009-10-06 11:31:11 -07001371 .dispatch = dispatch,
1372 .fault = mon_fault,
1373 .alloc_msg = mon_alloc_msg,
Sage Weilba75bb92009-10-06 11:31:11 -07001374};