blob: 21c62e9b7d1d6d4c816d252a3155706d25752ccf [file] [log] [blame]
Sage Weilba75bb92009-10-06 11:31:11 -07001#include "ceph_debug.h"
2
3#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09004#include <linux/slab.h>
Sage Weilba75bb92009-10-06 11:31:11 -07005#include <linux/random.h>
6#include <linux/sched.h>
7
8#include "mon_client.h"
9#include "super.h"
Sage Weil4e7a5dc2009-11-18 16:19:57 -080010#include "auth.h"
Sage Weilba75bb92009-10-06 11:31:11 -070011#include "decode.h"
12
13/*
14 * Interact with Ceph monitor cluster. Handle requests for new map
15 * versions, and periodically resend as needed. Also implement
16 * statfs() and umount().
17 *
18 * A small cluster of Ceph "monitors" are responsible for managing critical
19 * cluster configuration and state information. An odd number (e.g., 3, 5)
20 * of cmon daemons use a modified version of the Paxos part-time parliament
21 * algorithm to manage the MDS map (mds cluster membership), OSD map, and
22 * list of clients who have mounted the file system.
23 *
24 * We maintain an open, active session with a monitor at all times in order to
25 * receive timely MDSMap updates. We periodically send a keepalive byte on the
26 * TCP socket to ensure we detect a failure. If the connection does break, we
27 * randomly hunt for a new monitor. Once the connection is reestablished, we
28 * resend any outstanding requests.
29 */
30
Tobias Klauser9e327892010-05-20 10:40:19 +020031static const struct ceph_connection_operations mon_con_ops;
Sage Weilba75bb92009-10-06 11:31:11 -070032
Sage Weil9bd2e6f2010-02-02 16:21:06 -080033static int __validate_auth(struct ceph_mon_client *monc);
34
Sage Weilba75bb92009-10-06 11:31:11 -070035/*
36 * Decode a monmap blob (e.g., during mount).
37 */
38struct ceph_monmap *ceph_monmap_decode(void *p, void *end)
39{
40 struct ceph_monmap *m = NULL;
41 int i, err = -EINVAL;
42 struct ceph_fsid fsid;
43 u32 epoch, num_mon;
44 u16 version;
Sage Weil4e7a5dc2009-11-18 16:19:57 -080045 u32 len;
46
47 ceph_decode_32_safe(&p, end, len, bad);
48 ceph_decode_need(&p, end, len, bad);
Sage Weilba75bb92009-10-06 11:31:11 -070049
50 dout("monmap_decode %p %p len %d\n", p, end, (int)(end-p));
51
52 ceph_decode_16_safe(&p, end, version, bad);
53
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,
77 pr_addr(&m->mon_inst[i].addr.in_addr));
78 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);
Sage Weil97069002010-05-21 12:31:49 -0700107 ceph_con_revoke(monc->con, monc->m_auth);
Sage Weil5ce6e9d2010-02-15 16:22:28 -0800108 ceph_msg_get(monc->m_auth); /* keep our ref */
109 ceph_con_send(monc->con, monc->m_auth);
110}
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{
117 if (monc->con) {
118 dout("__close_session closing mon%d\n", monc->cur_mon);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800119 ceph_con_revoke(monc->con, monc->m_auth);
Sage Weilba75bb92009-10-06 11:31:11 -0700120 ceph_con_close(monc->con);
121 monc->cur_mon = -1;
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800122 monc->pending_auth = 0;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800123 ceph_auth_reset(monc->auth);
Sage Weilba75bb92009-10-06 11:31:11 -0700124 }
125}
126
127/*
128 * Open a session with a (new) monitor.
129 */
130static int __open_session(struct ceph_mon_client *monc)
131{
132 char r;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800133 int ret;
Sage Weilba75bb92009-10-06 11:31:11 -0700134
135 if (monc->cur_mon < 0) {
136 get_random_bytes(&r, 1);
137 monc->cur_mon = r % monc->monmap->num_mon;
138 dout("open_session num=%d r=%d -> mon%d\n",
139 monc->monmap->num_mon, r, monc->cur_mon);
140 monc->sub_sent = 0;
141 monc->sub_renew_after = jiffies; /* i.e., expired */
142 monc->want_next_osdmap = !!monc->want_next_osdmap;
143
144 dout("open_session mon%d opening\n", monc->cur_mon);
145 monc->con->peer_name.type = CEPH_ENTITY_TYPE_MON;
146 monc->con->peer_name.num = cpu_to_le64(monc->cur_mon);
147 ceph_con_open(monc->con,
148 &monc->monmap->mon_inst[monc->cur_mon].addr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800149
150 /* initiatiate authentication handshake */
151 ret = ceph_auth_build_hello(monc->auth,
152 monc->m_auth->front.iov_base,
153 monc->m_auth->front_max);
Sage Weil5ce6e9d2010-02-15 16:22:28 -0800154 __send_prepared_auth_request(monc, ret);
Sage Weilba75bb92009-10-06 11:31:11 -0700155 } else {
156 dout("open_session mon%d already open\n", monc->cur_mon);
157 }
158 return 0;
159}
160
161static bool __sub_expired(struct ceph_mon_client *monc)
162{
163 return time_after_eq(jiffies, monc->sub_renew_after);
164}
165
166/*
167 * Reschedule delayed work timer.
168 */
169static void __schedule_delayed(struct ceph_mon_client *monc)
170{
171 unsigned delay;
172
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800173 if (monc->cur_mon < 0 || __sub_expired(monc))
Sage Weilba75bb92009-10-06 11:31:11 -0700174 delay = 10 * HZ;
175 else
176 delay = 20 * HZ;
177 dout("__schedule_delayed after %u\n", delay);
178 schedule_delayed_work(&monc->delayed_work, delay);
179}
180
181/*
182 * Send subscribe request for mdsmap and/or osdmap.
183 */
184static void __send_subscribe(struct ceph_mon_client *monc)
185{
186 dout("__send_subscribe sub_sent=%u exp=%u want_osd=%d\n",
187 (unsigned)monc->sub_sent, __sub_expired(monc),
188 monc->want_next_osdmap);
189 if ((__sub_expired(monc) && !monc->sub_sent) ||
190 monc->want_next_osdmap == 1) {
Sage Weil240ed682010-05-21 14:57:25 -0700191 struct ceph_msg *msg = monc->m_subscribe;
Sage Weilba75bb92009-10-06 11:31:11 -0700192 struct ceph_mon_subscribe_item *i;
193 void *p, *end;
194
Sage Weilba75bb92009-10-06 11:31:11 -0700195 p = msg->front.iov_base;
Sage Weil240ed682010-05-21 14:57:25 -0700196 end = p + msg->front_max;
Sage Weilba75bb92009-10-06 11:31:11 -0700197
198 dout("__send_subscribe to 'mdsmap' %u+\n",
199 (unsigned)monc->have_mdsmap);
200 if (monc->want_next_osdmap) {
201 dout("__send_subscribe to 'osdmap' %u\n",
202 (unsigned)monc->have_osdmap);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800203 ceph_encode_32(&p, 3);
Sage Weilba75bb92009-10-06 11:31:11 -0700204 ceph_encode_string(&p, end, "osdmap", 6);
205 i = p;
206 i->have = cpu_to_le64(monc->have_osdmap);
207 i->onetime = 1;
208 p += sizeof(*i);
209 monc->want_next_osdmap = 2; /* requested */
210 } else {
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800211 ceph_encode_32(&p, 2);
Sage Weilba75bb92009-10-06 11:31:11 -0700212 }
213 ceph_encode_string(&p, end, "mdsmap", 6);
214 i = p;
215 i->have = cpu_to_le64(monc->have_mdsmap);
216 i->onetime = 0;
217 p += sizeof(*i);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800218 ceph_encode_string(&p, end, "monmap", 6);
219 i = p;
220 i->have = 0;
221 i->onetime = 0;
222 p += sizeof(*i);
Sage Weilba75bb92009-10-06 11:31:11 -0700223
224 msg->front.iov_len = p - msg->front.iov_base;
225 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
Sage Weil240ed682010-05-21 14:57:25 -0700226 ceph_con_revoke(monc->con, msg);
227 ceph_con_send(monc->con, ceph_msg_get(msg));
Sage Weilba75bb92009-10-06 11:31:11 -0700228
229 monc->sub_sent = jiffies | 1; /* never 0 */
230 }
231}
232
233static void handle_subscribe_ack(struct ceph_mon_client *monc,
234 struct ceph_msg *msg)
235{
236 unsigned seconds;
Sage Weil07bd10f2009-10-14 17:26:40 -0700237 struct ceph_mon_subscribe_ack *h = msg->front.iov_base;
Sage Weilba75bb92009-10-06 11:31:11 -0700238
Sage Weil07bd10f2009-10-14 17:26:40 -0700239 if (msg->front.iov_len < sizeof(*h))
240 goto bad;
241 seconds = le32_to_cpu(h->duration);
242
Sage Weilba75bb92009-10-06 11:31:11 -0700243 mutex_lock(&monc->mutex);
244 if (monc->hunting) {
245 pr_info("mon%d %s session established\n",
246 monc->cur_mon, pr_addr(&monc->con->peer_addr.in_addr));
247 monc->hunting = false;
248 }
249 dout("handle_subscribe_ack after %d seconds\n", seconds);
Sage Weil0656d112009-10-08 10:25:46 -0700250 monc->sub_renew_after = monc->sub_sent + (seconds >> 1)*HZ - 1;
Sage Weilba75bb92009-10-06 11:31:11 -0700251 monc->sub_sent = 0;
252 mutex_unlock(&monc->mutex);
253 return;
254bad:
255 pr_err("got corrupt subscribe-ack msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -0800256 ceph_msg_dump(msg);
Sage Weilba75bb92009-10-06 11:31:11 -0700257}
258
259/*
260 * Keep track of which maps we have
261 */
262int ceph_monc_got_mdsmap(struct ceph_mon_client *monc, u32 got)
263{
264 mutex_lock(&monc->mutex);
265 monc->have_mdsmap = got;
266 mutex_unlock(&monc->mutex);
267 return 0;
268}
269
270int ceph_monc_got_osdmap(struct ceph_mon_client *monc, u32 got)
271{
272 mutex_lock(&monc->mutex);
273 monc->have_osdmap = got;
274 monc->want_next_osdmap = 0;
275 mutex_unlock(&monc->mutex);
276 return 0;
277}
278
279/*
280 * Register interest in the next osdmap
281 */
282void ceph_monc_request_next_osdmap(struct ceph_mon_client *monc)
283{
284 dout("request_next_osdmap have %u\n", monc->have_osdmap);
285 mutex_lock(&monc->mutex);
286 if (!monc->want_next_osdmap)
287 monc->want_next_osdmap = 1;
288 if (monc->want_next_osdmap < 2)
289 __send_subscribe(monc);
290 mutex_unlock(&monc->mutex);
291}
292
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800293/*
Sage Weil50b885b2009-12-01 14:12:07 -0800294 *
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800295 */
296int ceph_monc_open_session(struct ceph_mon_client *monc)
Sage Weilba75bb92009-10-06 11:31:11 -0700297{
298 if (!monc->con) {
299 monc->con = kmalloc(sizeof(*monc->con), GFP_KERNEL);
300 if (!monc->con)
301 return -ENOMEM;
302 ceph_con_init(monc->client->msgr, monc->con);
303 monc->con->private = monc;
304 monc->con->ops = &mon_con_ops;
305 }
306
307 mutex_lock(&monc->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800308 __open_session(monc);
Sage Weilba75bb92009-10-06 11:31:11 -0700309 __schedule_delayed(monc);
310 mutex_unlock(&monc->mutex);
311 return 0;
312}
313
314/*
315 * The monitor responds with mount ack indicate mount success. The
316 * included client ticket allows the client to talk to MDSs and OSDs.
317 */
Sage Weil07433042009-11-18 16:50:41 -0800318static void ceph_monc_handle_map(struct ceph_mon_client *monc,
319 struct ceph_msg *msg)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800320{
321 struct ceph_client *client = monc->client;
322 struct ceph_monmap *monmap = NULL, *old = monc->monmap;
323 void *p, *end;
324
325 mutex_lock(&monc->mutex);
326
327 dout("handle_monmap\n");
328 p = msg->front.iov_base;
329 end = p + msg->front.iov_len;
330
331 monmap = ceph_monmap_decode(p, end);
332 if (IS_ERR(monmap)) {
333 pr_err("problem decoding monmap, %d\n",
334 (int)PTR_ERR(monmap));
Sage Weild4a780c2009-12-11 08:55:23 -0800335 goto out;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800336 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800337
Sage Weil07433042009-11-18 16:50:41 -0800338 if (ceph_check_fsid(monc->client, &monmap->fsid) < 0) {
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800339 kfree(monmap);
Sage Weild4a780c2009-12-11 08:55:23 -0800340 goto out;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800341 }
342
343 client->monc.monmap = monmap;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800344 kfree(old);
345
Sage Weild4a780c2009-12-11 08:55:23 -0800346out:
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800347 mutex_unlock(&monc->mutex);
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800348 wake_up(&client->auth_wq);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800349}
Sage Weilba75bb92009-10-06 11:31:11 -0700350
Sage Weilba75bb92009-10-06 11:31:11 -0700351/*
352 * statfs
353 */
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700354static struct ceph_mon_generic_request *__lookup_generic_req(
Sage Weil85ff03f2010-02-15 14:47:28 -0800355 struct ceph_mon_client *monc, u64 tid)
356{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700357 struct ceph_mon_generic_request *req;
358 struct rb_node *n = monc->generic_request_tree.rb_node;
Sage Weil85ff03f2010-02-15 14:47:28 -0800359
360 while (n) {
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700361 req = rb_entry(n, struct ceph_mon_generic_request, node);
Sage Weil85ff03f2010-02-15 14:47:28 -0800362 if (tid < req->tid)
363 n = n->rb_left;
364 else if (tid > req->tid)
365 n = n->rb_right;
366 else
367 return req;
368 }
369 return NULL;
370}
371
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700372static void __insert_generic_request(struct ceph_mon_client *monc,
373 struct ceph_mon_generic_request *new)
Sage Weil85ff03f2010-02-15 14:47:28 -0800374{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700375 struct rb_node **p = &monc->generic_request_tree.rb_node;
Sage Weil85ff03f2010-02-15 14:47:28 -0800376 struct rb_node *parent = NULL;
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700377 struct ceph_mon_generic_request *req = NULL;
Sage Weil85ff03f2010-02-15 14:47:28 -0800378
379 while (*p) {
380 parent = *p;
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700381 req = rb_entry(parent, struct ceph_mon_generic_request, node);
Sage Weil85ff03f2010-02-15 14:47:28 -0800382 if (new->tid < req->tid)
383 p = &(*p)->rb_left;
384 else if (new->tid > req->tid)
385 p = &(*p)->rb_right;
386 else
387 BUG();
388 }
389
390 rb_link_node(&new->node, parent, p);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700391 rb_insert_color(&new->node, &monc->generic_request_tree);
Sage Weil85ff03f2010-02-15 14:47:28 -0800392}
393
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700394static void release_generic_request(struct kref *kref)
Sage Weil3143edd2010-03-24 21:43:33 -0700395{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700396 struct ceph_mon_generic_request *req =
397 container_of(kref, struct ceph_mon_generic_request, kref);
Sage Weil3143edd2010-03-24 21:43:33 -0700398
399 if (req->reply)
400 ceph_msg_put(req->reply);
401 if (req->request)
402 ceph_msg_put(req->request);
403}
404
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700405static void put_generic_request(struct ceph_mon_generic_request *req)
Sage Weil3143edd2010-03-24 21:43:33 -0700406{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700407 kref_put(&req->kref, release_generic_request);
Sage Weil3143edd2010-03-24 21:43:33 -0700408}
409
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700410static void get_generic_request(struct ceph_mon_generic_request *req)
Sage Weil3143edd2010-03-24 21:43:33 -0700411{
412 kref_get(&req->kref);
413}
414
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700415static struct ceph_msg *get_generic_reply(struct ceph_connection *con,
Sage Weil3143edd2010-03-24 21:43:33 -0700416 struct ceph_msg_header *hdr,
417 int *skip)
418{
419 struct ceph_mon_client *monc = con->private;
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700420 struct ceph_mon_generic_request *req;
Sage Weil3143edd2010-03-24 21:43:33 -0700421 u64 tid = le64_to_cpu(hdr->tid);
422 struct ceph_msg *m;
423
424 mutex_lock(&monc->mutex);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700425 req = __lookup_generic_req(monc, tid);
Sage Weil3143edd2010-03-24 21:43:33 -0700426 if (!req) {
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700427 dout("get_generic_reply %lld dne\n", tid);
Sage Weil3143edd2010-03-24 21:43:33 -0700428 *skip = 1;
429 m = NULL;
430 } else {
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700431 dout("get_generic_reply %lld got %p\n", tid, req->reply);
Sage Weil3143edd2010-03-24 21:43:33 -0700432 m = ceph_msg_get(req->reply);
433 /*
434 * we don't need to track the connection reading into
435 * this reply because we only have one open connection
436 * at a time, ever.
437 */
438 }
439 mutex_unlock(&monc->mutex);
440 return m;
441}
442
Sage Weilba75bb92009-10-06 11:31:11 -0700443static void handle_statfs_reply(struct ceph_mon_client *monc,
444 struct ceph_msg *msg)
445{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700446 struct ceph_mon_generic_request *req;
Sage Weilba75bb92009-10-06 11:31:11 -0700447 struct ceph_mon_statfs_reply *reply = msg->front.iov_base;
Sage Weil3143edd2010-03-24 21:43:33 -0700448 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weilba75bb92009-10-06 11:31:11 -0700449
450 if (msg->front.iov_len != sizeof(*reply))
451 goto bad;
Sage Weilba75bb92009-10-06 11:31:11 -0700452 dout("handle_statfs_reply %p tid %llu\n", msg, tid);
453
454 mutex_lock(&monc->mutex);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700455 req = __lookup_generic_req(monc, tid);
Sage Weilba75bb92009-10-06 11:31:11 -0700456 if (req) {
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700457 *(struct ceph_statfs *)req->buf = reply->st;
Sage Weilba75bb92009-10-06 11:31:11 -0700458 req->result = 0;
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700459 get_generic_request(req);
Sage Weilba75bb92009-10-06 11:31:11 -0700460 }
461 mutex_unlock(&monc->mutex);
Sage Weil3143edd2010-03-24 21:43:33 -0700462 if (req) {
Sage Weilba75bb92009-10-06 11:31:11 -0700463 complete(&req->completion);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700464 put_generic_request(req);
Sage Weil3143edd2010-03-24 21:43:33 -0700465 }
Sage Weilba75bb92009-10-06 11:31:11 -0700466 return;
467
468bad:
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700469 pr_err("corrupt generic reply, no tid\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -0800470 ceph_msg_dump(msg);
Sage Weilba75bb92009-10-06 11:31:11 -0700471}
472
473/*
Sage Weilba75bb92009-10-06 11:31:11 -0700474 * Do a synchronous statfs().
475 */
476int ceph_monc_do_statfs(struct ceph_mon_client *monc, struct ceph_statfs *buf)
477{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700478 struct ceph_mon_generic_request *req;
Sage Weil3143edd2010-03-24 21:43:33 -0700479 struct ceph_mon_statfs *h;
Sage Weilba75bb92009-10-06 11:31:11 -0700480 int err;
481
Julia Lawallcffe7b62010-05-13 22:07:29 +0200482 req = kzalloc(sizeof(*req), GFP_NOFS);
Sage Weil3143edd2010-03-24 21:43:33 -0700483 if (!req)
484 return -ENOMEM;
Sage Weilba75bb92009-10-06 11:31:11 -0700485
Sage Weil3143edd2010-03-24 21:43:33 -0700486 kref_init(&req->kref);
487 req->buf = buf;
488 init_completion(&req->completion);
489
Sage Weila79832f2010-04-01 16:06:19 -0700490 err = -ENOMEM;
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700491 req->request = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -0700492 if (!req->request)
Sage Weil3143edd2010-03-24 21:43:33 -0700493 goto out;
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700494 req->reply = ceph_msg_new(CEPH_MSG_STATFS_REPLY, 1024, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -0700495 if (!req->reply)
Sage Weil3143edd2010-03-24 21:43:33 -0700496 goto out;
Sage Weil3143edd2010-03-24 21:43:33 -0700497
498 /* fill out request */
499 h = req->request->front.iov_base;
500 h->monhdr.have_version = 0;
501 h->monhdr.session_mon = cpu_to_le16(-1);
502 h->monhdr.session_mon_tid = 0;
503 h->fsid = monc->monmap->fsid;
Sage Weilba75bb92009-10-06 11:31:11 -0700504
505 /* register request */
506 mutex_lock(&monc->mutex);
Sage Weil3143edd2010-03-24 21:43:33 -0700507 req->tid = ++monc->last_tid;
508 req->request->hdr.tid = cpu_to_le64(req->tid);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700509 __insert_generic_request(monc, req);
510 monc->num_generic_requests++;
Sage Weilba75bb92009-10-06 11:31:11 -0700511 mutex_unlock(&monc->mutex);
512
513 /* send request and wait */
Sage Weil3143edd2010-03-24 21:43:33 -0700514 ceph_con_send(monc->con, ceph_msg_get(req->request));
515 err = wait_for_completion_interruptible(&req->completion);
Sage Weilba75bb92009-10-06 11:31:11 -0700516
517 mutex_lock(&monc->mutex);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700518 rb_erase(&req->node, &monc->generic_request_tree);
519 monc->num_generic_requests--;
Sage Weilba75bb92009-10-06 11:31:11 -0700520 mutex_unlock(&monc->mutex);
521
522 if (!err)
Sage Weil3143edd2010-03-24 21:43:33 -0700523 err = req->result;
524
525out:
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700526 kref_put(&req->kref, release_generic_request);
Sage Weilba75bb92009-10-06 11:31:11 -0700527 return err;
528}
529
530/*
531 * Resend pending statfs requests.
532 */
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700533static void __resend_generic_request(struct ceph_mon_client *monc)
Sage Weilba75bb92009-10-06 11:31:11 -0700534{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700535 struct ceph_mon_generic_request *req;
Sage Weil85ff03f2010-02-15 14:47:28 -0800536 struct rb_node *p;
Sage Weilba75bb92009-10-06 11:31:11 -0700537
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700538 for (p = rb_first(&monc->generic_request_tree); p; p = rb_next(p)) {
539 req = rb_entry(p, struct ceph_mon_generic_request, node);
Sage Weil97069002010-05-21 12:31:49 -0700540 ceph_con_revoke(monc->con, req->request);
Sage Weil3143edd2010-03-24 21:43:33 -0700541 ceph_con_send(monc->con, ceph_msg_get(req->request));
Sage Weilba75bb92009-10-06 11:31:11 -0700542 }
543}
544
545/*
546 * Delayed work. If we haven't mounted yet, retry. Otherwise,
547 * renew/retry subscription as needed (in case it is timing out, or we
548 * got an ENOMEM). And keep the monitor connection alive.
549 */
550static void delayed_work(struct work_struct *work)
551{
552 struct ceph_mon_client *monc =
553 container_of(work, struct ceph_mon_client, delayed_work.work);
554
555 dout("monc delayed_work\n");
556 mutex_lock(&monc->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800557 if (monc->hunting) {
558 __close_session(monc);
559 __open_session(monc); /* continue hunting */
Sage Weilba75bb92009-10-06 11:31:11 -0700560 } else {
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800561 ceph_con_keepalive(monc->con);
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800562
563 __validate_auth(monc);
564
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800565 if (monc->auth->ops->is_authenticated(monc->auth))
566 __send_subscribe(monc);
Sage Weilba75bb92009-10-06 11:31:11 -0700567 }
Sage Weilba75bb92009-10-06 11:31:11 -0700568 __schedule_delayed(monc);
569 mutex_unlock(&monc->mutex);
570}
571
Sage Weil6b805182009-10-27 11:50:50 -0700572/*
573 * On startup, we build a temporary monmap populated with the IPs
574 * provided by mount(2).
575 */
576static int build_initial_monmap(struct ceph_mon_client *monc)
577{
578 struct ceph_mount_args *args = monc->client->mount_args;
579 struct ceph_entity_addr *mon_addr = args->mon_addr;
580 int num_mon = args->num_mon;
581 int i;
582
583 /* build initial monmap */
584 monc->monmap = kzalloc(sizeof(*monc->monmap) +
585 num_mon*sizeof(monc->monmap->mon_inst[0]),
586 GFP_KERNEL);
587 if (!monc->monmap)
588 return -ENOMEM;
589 for (i = 0; i < num_mon; i++) {
590 monc->monmap->mon_inst[i].addr = mon_addr[i];
Sage Weil6b805182009-10-27 11:50:50 -0700591 monc->monmap->mon_inst[i].addr.nonce = 0;
592 monc->monmap->mon_inst[i].name.type =
593 CEPH_ENTITY_TYPE_MON;
594 monc->monmap->mon_inst[i].name.num = cpu_to_le64(i);
595 }
596 monc->monmap->num_mon = num_mon;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800597 monc->have_fsid = false;
Sage Weil6b805182009-10-27 11:50:50 -0700598
599 /* release addr memory */
600 kfree(args->mon_addr);
601 args->mon_addr = NULL;
602 args->num_mon = 0;
603 return 0;
604}
605
Sage Weilba75bb92009-10-06 11:31:11 -0700606int ceph_monc_init(struct ceph_mon_client *monc, struct ceph_client *cl)
607{
608 int err = 0;
609
610 dout("init\n");
611 memset(monc, 0, sizeof(*monc));
612 monc->client = cl;
613 monc->monmap = NULL;
614 mutex_init(&monc->mutex);
615
Sage Weil6b805182009-10-27 11:50:50 -0700616 err = build_initial_monmap(monc);
617 if (err)
618 goto out;
619
Sage Weilba75bb92009-10-06 11:31:11 -0700620 monc->con = NULL;
621
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800622 /* authentication */
623 monc->auth = ceph_auth_init(cl->mount_args->name,
624 cl->mount_args->secret);
625 if (IS_ERR(monc->auth))
626 return PTR_ERR(monc->auth);
627 monc->auth->want_keys =
628 CEPH_ENTITY_TYPE_AUTH | CEPH_ENTITY_TYPE_MON |
629 CEPH_ENTITY_TYPE_OSD | CEPH_ENTITY_TYPE_MDS;
630
Sage Weil240ed682010-05-21 14:57:25 -0700631 /* msgs */
Sage Weila79832f2010-04-01 16:06:19 -0700632 err = -ENOMEM;
Sage Weil7c315c52010-03-24 21:52:30 -0700633 monc->m_subscribe_ack = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE_ACK,
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700634 sizeof(struct ceph_mon_subscribe_ack),
635 GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -0700636 if (!monc->m_subscribe_ack)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800637 goto out_monmap;
Sage Weil6694d6b2010-03-24 21:48:05 -0700638
Sage Weil240ed682010-05-21 14:57:25 -0700639 monc->m_subscribe = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 96, GFP_NOFS);
640 if (!monc->m_subscribe)
641 goto out_subscribe_ack;
642
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700643 monc->m_auth_reply = ceph_msg_new(CEPH_MSG_AUTH_REPLY, 4096, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -0700644 if (!monc->m_auth_reply)
Sage Weil240ed682010-05-21 14:57:25 -0700645 goto out_subscribe;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800646
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700647 monc->m_auth = ceph_msg_new(CEPH_MSG_AUTH, 4096, GFP_NOFS);
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800648 monc->pending_auth = 0;
Sage Weila79832f2010-04-01 16:06:19 -0700649 if (!monc->m_auth)
Sage Weil6694d6b2010-03-24 21:48:05 -0700650 goto out_auth_reply;
Sage Weilba75bb92009-10-06 11:31:11 -0700651
652 monc->cur_mon = -1;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800653 monc->hunting = true;
Sage Weilba75bb92009-10-06 11:31:11 -0700654 monc->sub_renew_after = jiffies;
655 monc->sub_sent = 0;
656
657 INIT_DELAYED_WORK(&monc->delayed_work, delayed_work);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700658 monc->generic_request_tree = RB_ROOT;
659 monc->num_generic_requests = 0;
Sage Weilba75bb92009-10-06 11:31:11 -0700660 monc->last_tid = 0;
661
662 monc->have_mdsmap = 0;
663 monc->have_osdmap = 0;
664 monc->want_next_osdmap = 1;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800665 return 0;
666
Sage Weil6694d6b2010-03-24 21:48:05 -0700667out_auth_reply:
668 ceph_msg_put(monc->m_auth_reply);
Sage Weil240ed682010-05-21 14:57:25 -0700669out_subscribe:
670 ceph_msg_put(monc->m_subscribe);
Sage Weil7c315c52010-03-24 21:52:30 -0700671out_subscribe_ack:
672 ceph_msg_put(monc->m_subscribe_ack);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800673out_monmap:
674 kfree(monc->monmap);
Sage Weilba75bb92009-10-06 11:31:11 -0700675out:
676 return err;
677}
678
679void ceph_monc_stop(struct ceph_mon_client *monc)
680{
681 dout("stop\n");
682 cancel_delayed_work_sync(&monc->delayed_work);
683
684 mutex_lock(&monc->mutex);
685 __close_session(monc);
686 if (monc->con) {
687 monc->con->private = NULL;
688 monc->con->ops->put(monc->con);
689 monc->con = NULL;
690 }
691 mutex_unlock(&monc->mutex);
692
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800693 ceph_auth_destroy(monc->auth);
694
695 ceph_msg_put(monc->m_auth);
Sage Weil6694d6b2010-03-24 21:48:05 -0700696 ceph_msg_put(monc->m_auth_reply);
Sage Weil240ed682010-05-21 14:57:25 -0700697 ceph_msg_put(monc->m_subscribe);
Sage Weil7c315c52010-03-24 21:52:30 -0700698 ceph_msg_put(monc->m_subscribe_ack);
Sage Weilba75bb92009-10-06 11:31:11 -0700699
700 kfree(monc->monmap);
701}
702
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800703static void handle_auth_reply(struct ceph_mon_client *monc,
704 struct ceph_msg *msg)
705{
706 int ret;
Sage Weil09c4d6a2010-05-25 15:38:06 -0700707 int was_auth = 0;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800708
709 mutex_lock(&monc->mutex);
Sage Weil09c4d6a2010-05-25 15:38:06 -0700710 if (monc->auth->ops)
711 was_auth = monc->auth->ops->is_authenticated(monc->auth);
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800712 monc->pending_auth = 0;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800713 ret = ceph_handle_auth_reply(monc->auth, msg->front.iov_base,
714 msg->front.iov_len,
715 monc->m_auth->front.iov_base,
716 monc->m_auth->front_max);
717 if (ret < 0) {
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800718 monc->client->auth_err = ret;
719 wake_up(&monc->client->auth_wq);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800720 } else if (ret > 0) {
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800721 __send_prepared_auth_request(monc, ret);
Sage Weil09c4d6a2010-05-25 15:38:06 -0700722 } else if (!was_auth && monc->auth->ops->is_authenticated(monc->auth)) {
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800723 dout("authenticated, starting session\n");
Sage Weil07433042009-11-18 16:50:41 -0800724
725 monc->client->msgr->inst.name.type = CEPH_ENTITY_TYPE_CLIENT;
726 monc->client->msgr->inst.name.num = monc->auth->global_id;
727
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800728 __send_subscribe(monc);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700729 __resend_generic_request(monc);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800730 }
731 mutex_unlock(&monc->mutex);
732}
733
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800734static int __validate_auth(struct ceph_mon_client *monc)
735{
736 int ret;
737
738 if (monc->pending_auth)
739 return 0;
740
741 ret = ceph_build_auth(monc->auth, monc->m_auth->front.iov_base,
742 monc->m_auth->front_max);
743 if (ret <= 0)
744 return ret; /* either an error, or no need to authenticate */
745 __send_prepared_auth_request(monc, ret);
746 return 0;
747}
748
749int ceph_monc_validate_auth(struct ceph_mon_client *monc)
750{
751 int ret;
752
753 mutex_lock(&monc->mutex);
754 ret = __validate_auth(monc);
755 mutex_unlock(&monc->mutex);
756 return ret;
757}
758
Sage Weilba75bb92009-10-06 11:31:11 -0700759/*
760 * handle incoming message
761 */
762static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
763{
764 struct ceph_mon_client *monc = con->private;
765 int type = le16_to_cpu(msg->hdr.type);
766
767 if (!monc)
768 return;
769
770 switch (type) {
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800771 case CEPH_MSG_AUTH_REPLY:
772 handle_auth_reply(monc, msg);
Sage Weilba75bb92009-10-06 11:31:11 -0700773 break;
774
775 case CEPH_MSG_MON_SUBSCRIBE_ACK:
776 handle_subscribe_ack(monc, msg);
777 break;
778
779 case CEPH_MSG_STATFS_REPLY:
780 handle_statfs_reply(monc, msg);
781 break;
782
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800783 case CEPH_MSG_MON_MAP:
784 ceph_monc_handle_map(monc, msg);
785 break;
786
Sage Weilba75bb92009-10-06 11:31:11 -0700787 case CEPH_MSG_MDS_MAP:
788 ceph_mdsc_handle_map(&monc->client->mdsc, msg);
789 break;
790
791 case CEPH_MSG_OSD_MAP:
792 ceph_osdc_handle_map(&monc->client->osdc, msg);
793 break;
794
795 default:
796 pr_err("received unknown message type %d %s\n", type,
797 ceph_msg_type_name(type));
798 }
799 ceph_msg_put(msg);
800}
801
802/*
803 * Allocate memory for incoming message
804 */
805static struct ceph_msg *mon_alloc_msg(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -0800806 struct ceph_msg_header *hdr,
807 int *skip)
Sage Weilba75bb92009-10-06 11:31:11 -0700808{
809 struct ceph_mon_client *monc = con->private;
810 int type = le16_to_cpu(hdr->type);
Yehuda Sadeh24504182010-01-08 13:58:34 -0800811 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -0800812 struct ceph_msg *m = NULL;
Sage Weilba75bb92009-10-06 11:31:11 -0700813
Yehuda Sadeh24504182010-01-08 13:58:34 -0800814 *skip = 0;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -0800815
Sage Weilba75bb92009-10-06 11:31:11 -0700816 switch (type) {
Sage Weilba75bb92009-10-06 11:31:11 -0700817 case CEPH_MSG_MON_SUBSCRIBE_ACK:
Sage Weil7c315c52010-03-24 21:52:30 -0700818 m = ceph_msg_get(monc->m_subscribe_ack);
Yehuda Sadeh24504182010-01-08 13:58:34 -0800819 break;
Sage Weilba75bb92009-10-06 11:31:11 -0700820 case CEPH_MSG_STATFS_REPLY:
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700821 return get_generic_reply(con, hdr, skip);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800822 case CEPH_MSG_AUTH_REPLY:
Sage Weil6694d6b2010-03-24 21:48:05 -0700823 m = ceph_msg_get(monc->m_auth_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -0800824 break;
Sage Weil5b3a4db2010-02-19 21:43:23 -0800825 case CEPH_MSG_MON_MAP:
826 case CEPH_MSG_MDS_MAP:
827 case CEPH_MSG_OSD_MAP:
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700828 m = ceph_msg_new(type, front_len, GFP_NOFS);
Sage Weil5b3a4db2010-02-19 21:43:23 -0800829 break;
Sage Weilba75bb92009-10-06 11:31:11 -0700830 }
Yehuda Sadeh24504182010-01-08 13:58:34 -0800831
Sage Weil5b3a4db2010-02-19 21:43:23 -0800832 if (!m) {
833 pr_info("alloc_msg unknown type %d\n", type);
Yehuda Sadeh24504182010-01-08 13:58:34 -0800834 *skip = 1;
Sage Weil5b3a4db2010-02-19 21:43:23 -0800835 }
Yehuda Sadeh24504182010-01-08 13:58:34 -0800836 return m;
Sage Weilba75bb92009-10-06 11:31:11 -0700837}
838
839/*
840 * If the monitor connection resets, pick a new monitor and resubmit
841 * any pending requests.
842 */
843static void mon_fault(struct ceph_connection *con)
844{
845 struct ceph_mon_client *monc = con->private;
846
847 if (!monc)
848 return;
849
850 dout("mon_fault\n");
851 mutex_lock(&monc->mutex);
852 if (!con->private)
853 goto out;
854
855 if (monc->con && !monc->hunting)
856 pr_info("mon%d %s session lost, "
857 "hunting for new mon\n", monc->cur_mon,
858 pr_addr(&monc->con->peer_addr.in_addr));
859
860 __close_session(monc);
861 if (!monc->hunting) {
862 /* start hunting */
863 monc->hunting = true;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800864 __open_session(monc);
Sage Weilba75bb92009-10-06 11:31:11 -0700865 } else {
866 /* already hunting, let's wait a bit */
867 __schedule_delayed(monc);
868 }
869out:
870 mutex_unlock(&monc->mutex);
871}
872
Tobias Klauser9e327892010-05-20 10:40:19 +0200873static const struct ceph_connection_operations mon_con_ops = {
Sage Weilba75bb92009-10-06 11:31:11 -0700874 .get = ceph_con_get,
875 .put = ceph_con_put,
876 .dispatch = dispatch,
877 .fault = mon_fault,
878 .alloc_msg = mon_alloc_msg,
Sage Weilba75bb92009-10-06 11:31:11 -0700879};