blob: 12d94f24ee9786f991114ed1c0f8cb01b8b1260c [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) {
191 struct ceph_msg *msg;
192 struct ceph_mon_subscribe_item *i;
193 void *p, *end;
194
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700195 msg = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 96, GFP_NOFS);
Sage Weilba75bb92009-10-06 11:31:11 -0700196 if (!msg)
197 return;
198
199 p = msg->front.iov_base;
200 end = p + msg->front.iov_len;
201
202 dout("__send_subscribe to 'mdsmap' %u+\n",
203 (unsigned)monc->have_mdsmap);
204 if (monc->want_next_osdmap) {
205 dout("__send_subscribe to 'osdmap' %u\n",
206 (unsigned)monc->have_osdmap);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800207 ceph_encode_32(&p, 3);
Sage Weilba75bb92009-10-06 11:31:11 -0700208 ceph_encode_string(&p, end, "osdmap", 6);
209 i = p;
210 i->have = cpu_to_le64(monc->have_osdmap);
211 i->onetime = 1;
212 p += sizeof(*i);
213 monc->want_next_osdmap = 2; /* requested */
214 } else {
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800215 ceph_encode_32(&p, 2);
Sage Weilba75bb92009-10-06 11:31:11 -0700216 }
217 ceph_encode_string(&p, end, "mdsmap", 6);
218 i = p;
219 i->have = cpu_to_le64(monc->have_mdsmap);
220 i->onetime = 0;
221 p += sizeof(*i);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800222 ceph_encode_string(&p, end, "monmap", 6);
223 i = p;
224 i->have = 0;
225 i->onetime = 0;
226 p += sizeof(*i);
Sage Weilba75bb92009-10-06 11:31:11 -0700227
228 msg->front.iov_len = p - msg->front.iov_base;
229 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
230 ceph_con_send(monc->con, msg);
231
232 monc->sub_sent = jiffies | 1; /* never 0 */
233 }
234}
235
236static void handle_subscribe_ack(struct ceph_mon_client *monc,
237 struct ceph_msg *msg)
238{
239 unsigned seconds;
Sage Weil07bd10f2009-10-14 17:26:40 -0700240 struct ceph_mon_subscribe_ack *h = msg->front.iov_base;
Sage Weilba75bb92009-10-06 11:31:11 -0700241
Sage Weil07bd10f2009-10-14 17:26:40 -0700242 if (msg->front.iov_len < sizeof(*h))
243 goto bad;
244 seconds = le32_to_cpu(h->duration);
245
Sage Weilba75bb92009-10-06 11:31:11 -0700246 mutex_lock(&monc->mutex);
247 if (monc->hunting) {
248 pr_info("mon%d %s session established\n",
249 monc->cur_mon, pr_addr(&monc->con->peer_addr.in_addr));
250 monc->hunting = false;
251 }
252 dout("handle_subscribe_ack after %d seconds\n", seconds);
Sage Weil0656d112009-10-08 10:25:46 -0700253 monc->sub_renew_after = monc->sub_sent + (seconds >> 1)*HZ - 1;
Sage Weilba75bb92009-10-06 11:31:11 -0700254 monc->sub_sent = 0;
255 mutex_unlock(&monc->mutex);
256 return;
257bad:
258 pr_err("got corrupt subscribe-ack msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -0800259 ceph_msg_dump(msg);
Sage Weilba75bb92009-10-06 11:31:11 -0700260}
261
262/*
263 * Keep track of which maps we have
264 */
265int ceph_monc_got_mdsmap(struct ceph_mon_client *monc, u32 got)
266{
267 mutex_lock(&monc->mutex);
268 monc->have_mdsmap = got;
269 mutex_unlock(&monc->mutex);
270 return 0;
271}
272
273int ceph_monc_got_osdmap(struct ceph_mon_client *monc, u32 got)
274{
275 mutex_lock(&monc->mutex);
276 monc->have_osdmap = got;
277 monc->want_next_osdmap = 0;
278 mutex_unlock(&monc->mutex);
279 return 0;
280}
281
282/*
283 * Register interest in the next osdmap
284 */
285void ceph_monc_request_next_osdmap(struct ceph_mon_client *monc)
286{
287 dout("request_next_osdmap have %u\n", monc->have_osdmap);
288 mutex_lock(&monc->mutex);
289 if (!monc->want_next_osdmap)
290 monc->want_next_osdmap = 1;
291 if (monc->want_next_osdmap < 2)
292 __send_subscribe(monc);
293 mutex_unlock(&monc->mutex);
294}
295
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800296/*
Sage Weil50b885b2009-12-01 14:12:07 -0800297 *
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800298 */
299int ceph_monc_open_session(struct ceph_mon_client *monc)
Sage Weilba75bb92009-10-06 11:31:11 -0700300{
301 if (!monc->con) {
302 monc->con = kmalloc(sizeof(*monc->con), GFP_KERNEL);
303 if (!monc->con)
304 return -ENOMEM;
305 ceph_con_init(monc->client->msgr, monc->con);
306 monc->con->private = monc;
307 monc->con->ops = &mon_con_ops;
308 }
309
310 mutex_lock(&monc->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800311 __open_session(monc);
Sage Weilba75bb92009-10-06 11:31:11 -0700312 __schedule_delayed(monc);
313 mutex_unlock(&monc->mutex);
314 return 0;
315}
316
317/*
318 * The monitor responds with mount ack indicate mount success. The
319 * included client ticket allows the client to talk to MDSs and OSDs.
320 */
Sage Weil07433042009-11-18 16:50:41 -0800321static void ceph_monc_handle_map(struct ceph_mon_client *monc,
322 struct ceph_msg *msg)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800323{
324 struct ceph_client *client = monc->client;
325 struct ceph_monmap *monmap = NULL, *old = monc->monmap;
326 void *p, *end;
327
328 mutex_lock(&monc->mutex);
329
330 dout("handle_monmap\n");
331 p = msg->front.iov_base;
332 end = p + msg->front.iov_len;
333
334 monmap = ceph_monmap_decode(p, end);
335 if (IS_ERR(monmap)) {
336 pr_err("problem decoding monmap, %d\n",
337 (int)PTR_ERR(monmap));
Sage Weild4a780c2009-12-11 08:55:23 -0800338 goto out;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800339 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800340
Sage Weil07433042009-11-18 16:50:41 -0800341 if (ceph_check_fsid(monc->client, &monmap->fsid) < 0) {
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800342 kfree(monmap);
Sage Weild4a780c2009-12-11 08:55:23 -0800343 goto out;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800344 }
345
346 client->monc.monmap = monmap;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800347 kfree(old);
348
Sage Weild4a780c2009-12-11 08:55:23 -0800349out:
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800350 mutex_unlock(&monc->mutex);
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800351 wake_up(&client->auth_wq);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800352}
Sage Weilba75bb92009-10-06 11:31:11 -0700353
Sage Weilba75bb92009-10-06 11:31:11 -0700354/*
355 * statfs
356 */
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700357static struct ceph_mon_generic_request *__lookup_generic_req(
Sage Weil85ff03f2010-02-15 14:47:28 -0800358 struct ceph_mon_client *monc, u64 tid)
359{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700360 struct ceph_mon_generic_request *req;
361 struct rb_node *n = monc->generic_request_tree.rb_node;
Sage Weil85ff03f2010-02-15 14:47:28 -0800362
363 while (n) {
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700364 req = rb_entry(n, struct ceph_mon_generic_request, node);
Sage Weil85ff03f2010-02-15 14:47:28 -0800365 if (tid < req->tid)
366 n = n->rb_left;
367 else if (tid > req->tid)
368 n = n->rb_right;
369 else
370 return req;
371 }
372 return NULL;
373}
374
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700375static void __insert_generic_request(struct ceph_mon_client *monc,
376 struct ceph_mon_generic_request *new)
Sage Weil85ff03f2010-02-15 14:47:28 -0800377{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700378 struct rb_node **p = &monc->generic_request_tree.rb_node;
Sage Weil85ff03f2010-02-15 14:47:28 -0800379 struct rb_node *parent = NULL;
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700380 struct ceph_mon_generic_request *req = NULL;
Sage Weil85ff03f2010-02-15 14:47:28 -0800381
382 while (*p) {
383 parent = *p;
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700384 req = rb_entry(parent, struct ceph_mon_generic_request, node);
Sage Weil85ff03f2010-02-15 14:47:28 -0800385 if (new->tid < req->tid)
386 p = &(*p)->rb_left;
387 else if (new->tid > req->tid)
388 p = &(*p)->rb_right;
389 else
390 BUG();
391 }
392
393 rb_link_node(&new->node, parent, p);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700394 rb_insert_color(&new->node, &monc->generic_request_tree);
Sage Weil85ff03f2010-02-15 14:47:28 -0800395}
396
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700397static void release_generic_request(struct kref *kref)
Sage Weil3143edd2010-03-24 21:43:33 -0700398{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700399 struct ceph_mon_generic_request *req =
400 container_of(kref, struct ceph_mon_generic_request, kref);
Sage Weil3143edd2010-03-24 21:43:33 -0700401
402 if (req->reply)
403 ceph_msg_put(req->reply);
404 if (req->request)
405 ceph_msg_put(req->request);
406}
407
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700408static void put_generic_request(struct ceph_mon_generic_request *req)
Sage Weil3143edd2010-03-24 21:43:33 -0700409{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700410 kref_put(&req->kref, release_generic_request);
Sage Weil3143edd2010-03-24 21:43:33 -0700411}
412
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700413static void get_generic_request(struct ceph_mon_generic_request *req)
Sage Weil3143edd2010-03-24 21:43:33 -0700414{
415 kref_get(&req->kref);
416}
417
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700418static struct ceph_msg *get_generic_reply(struct ceph_connection *con,
Sage Weil3143edd2010-03-24 21:43:33 -0700419 struct ceph_msg_header *hdr,
420 int *skip)
421{
422 struct ceph_mon_client *monc = con->private;
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700423 struct ceph_mon_generic_request *req;
Sage Weil3143edd2010-03-24 21:43:33 -0700424 u64 tid = le64_to_cpu(hdr->tid);
425 struct ceph_msg *m;
426
427 mutex_lock(&monc->mutex);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700428 req = __lookup_generic_req(monc, tid);
Sage Weil3143edd2010-03-24 21:43:33 -0700429 if (!req) {
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700430 dout("get_generic_reply %lld dne\n", tid);
Sage Weil3143edd2010-03-24 21:43:33 -0700431 *skip = 1;
432 m = NULL;
433 } else {
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700434 dout("get_generic_reply %lld got %p\n", tid, req->reply);
Sage Weil3143edd2010-03-24 21:43:33 -0700435 m = ceph_msg_get(req->reply);
436 /*
437 * we don't need to track the connection reading into
438 * this reply because we only have one open connection
439 * at a time, ever.
440 */
441 }
442 mutex_unlock(&monc->mutex);
443 return m;
444}
445
Sage Weilba75bb92009-10-06 11:31:11 -0700446static void handle_statfs_reply(struct ceph_mon_client *monc,
447 struct ceph_msg *msg)
448{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700449 struct ceph_mon_generic_request *req;
Sage Weilba75bb92009-10-06 11:31:11 -0700450 struct ceph_mon_statfs_reply *reply = msg->front.iov_base;
Sage Weil3143edd2010-03-24 21:43:33 -0700451 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weilba75bb92009-10-06 11:31:11 -0700452
453 if (msg->front.iov_len != sizeof(*reply))
454 goto bad;
Sage Weilba75bb92009-10-06 11:31:11 -0700455 dout("handle_statfs_reply %p tid %llu\n", msg, tid);
456
457 mutex_lock(&monc->mutex);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700458 req = __lookup_generic_req(monc, tid);
Sage Weilba75bb92009-10-06 11:31:11 -0700459 if (req) {
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700460 *(struct ceph_statfs *)req->buf = reply->st;
Sage Weilba75bb92009-10-06 11:31:11 -0700461 req->result = 0;
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700462 get_generic_request(req);
Sage Weilba75bb92009-10-06 11:31:11 -0700463 }
464 mutex_unlock(&monc->mutex);
Sage Weil3143edd2010-03-24 21:43:33 -0700465 if (req) {
Sage Weilba75bb92009-10-06 11:31:11 -0700466 complete(&req->completion);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700467 put_generic_request(req);
Sage Weil3143edd2010-03-24 21:43:33 -0700468 }
Sage Weilba75bb92009-10-06 11:31:11 -0700469 return;
470
471bad:
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700472 pr_err("corrupt generic reply, no tid\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -0800473 ceph_msg_dump(msg);
Sage Weilba75bb92009-10-06 11:31:11 -0700474}
475
476/*
Sage Weilba75bb92009-10-06 11:31:11 -0700477 * Do a synchronous statfs().
478 */
479int ceph_monc_do_statfs(struct ceph_mon_client *monc, struct ceph_statfs *buf)
480{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700481 struct ceph_mon_generic_request *req;
Sage Weil3143edd2010-03-24 21:43:33 -0700482 struct ceph_mon_statfs *h;
Sage Weilba75bb92009-10-06 11:31:11 -0700483 int err;
484
Julia Lawallcffe7b62010-05-13 22:07:29 +0200485 req = kzalloc(sizeof(*req), GFP_NOFS);
Sage Weil3143edd2010-03-24 21:43:33 -0700486 if (!req)
487 return -ENOMEM;
Sage Weilba75bb92009-10-06 11:31:11 -0700488
Sage Weil3143edd2010-03-24 21:43:33 -0700489 kref_init(&req->kref);
490 req->buf = buf;
491 init_completion(&req->completion);
492
Sage Weila79832f2010-04-01 16:06:19 -0700493 err = -ENOMEM;
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700494 req->request = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -0700495 if (!req->request)
Sage Weil3143edd2010-03-24 21:43:33 -0700496 goto out;
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700497 req->reply = ceph_msg_new(CEPH_MSG_STATFS_REPLY, 1024, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -0700498 if (!req->reply)
Sage Weil3143edd2010-03-24 21:43:33 -0700499 goto out;
Sage Weil3143edd2010-03-24 21:43:33 -0700500
501 /* fill out request */
502 h = req->request->front.iov_base;
503 h->monhdr.have_version = 0;
504 h->monhdr.session_mon = cpu_to_le16(-1);
505 h->monhdr.session_mon_tid = 0;
506 h->fsid = monc->monmap->fsid;
Sage Weilba75bb92009-10-06 11:31:11 -0700507
508 /* register request */
509 mutex_lock(&monc->mutex);
Sage Weil3143edd2010-03-24 21:43:33 -0700510 req->tid = ++monc->last_tid;
511 req->request->hdr.tid = cpu_to_le64(req->tid);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700512 __insert_generic_request(monc, req);
513 monc->num_generic_requests++;
Sage Weilba75bb92009-10-06 11:31:11 -0700514 mutex_unlock(&monc->mutex);
515
516 /* send request and wait */
Sage Weil3143edd2010-03-24 21:43:33 -0700517 ceph_con_send(monc->con, ceph_msg_get(req->request));
518 err = wait_for_completion_interruptible(&req->completion);
Sage Weilba75bb92009-10-06 11:31:11 -0700519
520 mutex_lock(&monc->mutex);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700521 rb_erase(&req->node, &monc->generic_request_tree);
522 monc->num_generic_requests--;
Sage Weilba75bb92009-10-06 11:31:11 -0700523 mutex_unlock(&monc->mutex);
524
525 if (!err)
Sage Weil3143edd2010-03-24 21:43:33 -0700526 err = req->result;
527
528out:
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700529 kref_put(&req->kref, release_generic_request);
Sage Weilba75bb92009-10-06 11:31:11 -0700530 return err;
531}
532
533/*
534 * Resend pending statfs requests.
535 */
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700536static void __resend_generic_request(struct ceph_mon_client *monc)
Sage Weilba75bb92009-10-06 11:31:11 -0700537{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700538 struct ceph_mon_generic_request *req;
Sage Weil85ff03f2010-02-15 14:47:28 -0800539 struct rb_node *p;
Sage Weilba75bb92009-10-06 11:31:11 -0700540
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700541 for (p = rb_first(&monc->generic_request_tree); p; p = rb_next(p)) {
542 req = rb_entry(p, struct ceph_mon_generic_request, node);
Sage Weil97069002010-05-21 12:31:49 -0700543 ceph_con_revoke(monc->con, req->request);
Sage Weil3143edd2010-03-24 21:43:33 -0700544 ceph_con_send(monc->con, ceph_msg_get(req->request));
Sage Weilba75bb92009-10-06 11:31:11 -0700545 }
546}
547
548/*
549 * Delayed work. If we haven't mounted yet, retry. Otherwise,
550 * renew/retry subscription as needed (in case it is timing out, or we
551 * got an ENOMEM). And keep the monitor connection alive.
552 */
553static void delayed_work(struct work_struct *work)
554{
555 struct ceph_mon_client *monc =
556 container_of(work, struct ceph_mon_client, delayed_work.work);
557
558 dout("monc delayed_work\n");
559 mutex_lock(&monc->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800560 if (monc->hunting) {
561 __close_session(monc);
562 __open_session(monc); /* continue hunting */
Sage Weilba75bb92009-10-06 11:31:11 -0700563 } else {
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800564 ceph_con_keepalive(monc->con);
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800565
566 __validate_auth(monc);
567
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800568 if (monc->auth->ops->is_authenticated(monc->auth))
569 __send_subscribe(monc);
Sage Weilba75bb92009-10-06 11:31:11 -0700570 }
Sage Weilba75bb92009-10-06 11:31:11 -0700571 __schedule_delayed(monc);
572 mutex_unlock(&monc->mutex);
573}
574
Sage Weil6b805182009-10-27 11:50:50 -0700575/*
576 * On startup, we build a temporary monmap populated with the IPs
577 * provided by mount(2).
578 */
579static int build_initial_monmap(struct ceph_mon_client *monc)
580{
581 struct ceph_mount_args *args = monc->client->mount_args;
582 struct ceph_entity_addr *mon_addr = args->mon_addr;
583 int num_mon = args->num_mon;
584 int i;
585
586 /* build initial monmap */
587 monc->monmap = kzalloc(sizeof(*monc->monmap) +
588 num_mon*sizeof(monc->monmap->mon_inst[0]),
589 GFP_KERNEL);
590 if (!monc->monmap)
591 return -ENOMEM;
592 for (i = 0; i < num_mon; i++) {
593 monc->monmap->mon_inst[i].addr = mon_addr[i];
Sage Weil6b805182009-10-27 11:50:50 -0700594 monc->monmap->mon_inst[i].addr.nonce = 0;
595 monc->monmap->mon_inst[i].name.type =
596 CEPH_ENTITY_TYPE_MON;
597 monc->monmap->mon_inst[i].name.num = cpu_to_le64(i);
598 }
599 monc->monmap->num_mon = num_mon;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800600 monc->have_fsid = false;
Sage Weil6b805182009-10-27 11:50:50 -0700601
602 /* release addr memory */
603 kfree(args->mon_addr);
604 args->mon_addr = NULL;
605 args->num_mon = 0;
606 return 0;
607}
608
Sage Weilba75bb92009-10-06 11:31:11 -0700609int ceph_monc_init(struct ceph_mon_client *monc, struct ceph_client *cl)
610{
611 int err = 0;
612
613 dout("init\n");
614 memset(monc, 0, sizeof(*monc));
615 monc->client = cl;
616 monc->monmap = NULL;
617 mutex_init(&monc->mutex);
618
Sage Weil6b805182009-10-27 11:50:50 -0700619 err = build_initial_monmap(monc);
620 if (err)
621 goto out;
622
Sage Weilba75bb92009-10-06 11:31:11 -0700623 monc->con = NULL;
624
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800625 /* authentication */
626 monc->auth = ceph_auth_init(cl->mount_args->name,
627 cl->mount_args->secret);
628 if (IS_ERR(monc->auth))
629 return PTR_ERR(monc->auth);
630 monc->auth->want_keys =
631 CEPH_ENTITY_TYPE_AUTH | CEPH_ENTITY_TYPE_MON |
632 CEPH_ENTITY_TYPE_OSD | CEPH_ENTITY_TYPE_MDS;
633
Sage Weilba75bb92009-10-06 11:31:11 -0700634 /* msg pools */
Sage Weila79832f2010-04-01 16:06:19 -0700635 err = -ENOMEM;
Sage Weil7c315c52010-03-24 21:52:30 -0700636 monc->m_subscribe_ack = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE_ACK,
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700637 sizeof(struct ceph_mon_subscribe_ack),
638 GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -0700639 if (!monc->m_subscribe_ack)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800640 goto out_monmap;
Sage Weil6694d6b2010-03-24 21:48:05 -0700641
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700642 monc->m_auth_reply = ceph_msg_new(CEPH_MSG_AUTH_REPLY, 4096, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -0700643 if (!monc->m_auth_reply)
Sage Weil7c315c52010-03-24 21:52:30 -0700644 goto out_subscribe_ack;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800645
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700646 monc->m_auth = ceph_msg_new(CEPH_MSG_AUTH, 4096, GFP_NOFS);
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800647 monc->pending_auth = 0;
Sage Weila79832f2010-04-01 16:06:19 -0700648 if (!monc->m_auth)
Sage Weil6694d6b2010-03-24 21:48:05 -0700649 goto out_auth_reply;
Sage Weilba75bb92009-10-06 11:31:11 -0700650
651 monc->cur_mon = -1;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800652 monc->hunting = true;
Sage Weilba75bb92009-10-06 11:31:11 -0700653 monc->sub_renew_after = jiffies;
654 monc->sub_sent = 0;
655
656 INIT_DELAYED_WORK(&monc->delayed_work, delayed_work);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700657 monc->generic_request_tree = RB_ROOT;
658 monc->num_generic_requests = 0;
Sage Weilba75bb92009-10-06 11:31:11 -0700659 monc->last_tid = 0;
660
661 monc->have_mdsmap = 0;
662 monc->have_osdmap = 0;
663 monc->want_next_osdmap = 1;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800664 return 0;
665
Sage Weil6694d6b2010-03-24 21:48:05 -0700666out_auth_reply:
667 ceph_msg_put(monc->m_auth_reply);
Sage Weil7c315c52010-03-24 21:52:30 -0700668out_subscribe_ack:
669 ceph_msg_put(monc->m_subscribe_ack);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800670out_monmap:
671 kfree(monc->monmap);
Sage Weilba75bb92009-10-06 11:31:11 -0700672out:
673 return err;
674}
675
676void ceph_monc_stop(struct ceph_mon_client *monc)
677{
678 dout("stop\n");
679 cancel_delayed_work_sync(&monc->delayed_work);
680
681 mutex_lock(&monc->mutex);
682 __close_session(monc);
683 if (monc->con) {
684 monc->con->private = NULL;
685 monc->con->ops->put(monc->con);
686 monc->con = NULL;
687 }
688 mutex_unlock(&monc->mutex);
689
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800690 ceph_auth_destroy(monc->auth);
691
692 ceph_msg_put(monc->m_auth);
Sage Weil6694d6b2010-03-24 21:48:05 -0700693 ceph_msg_put(monc->m_auth_reply);
Sage Weil7c315c52010-03-24 21:52:30 -0700694 ceph_msg_put(monc->m_subscribe_ack);
Sage Weilba75bb92009-10-06 11:31:11 -0700695
696 kfree(monc->monmap);
697}
698
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800699static void handle_auth_reply(struct ceph_mon_client *monc,
700 struct ceph_msg *msg)
701{
702 int ret;
703
704 mutex_lock(&monc->mutex);
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800705 monc->pending_auth = 0;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800706 ret = ceph_handle_auth_reply(monc->auth, msg->front.iov_base,
707 msg->front.iov_len,
708 monc->m_auth->front.iov_base,
709 monc->m_auth->front_max);
710 if (ret < 0) {
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800711 monc->client->auth_err = ret;
712 wake_up(&monc->client->auth_wq);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800713 } else if (ret > 0) {
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800714 __send_prepared_auth_request(monc, ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800715 } else if (monc->auth->ops->is_authenticated(monc->auth)) {
716 dout("authenticated, starting session\n");
Sage Weil07433042009-11-18 16:50:41 -0800717
718 monc->client->msgr->inst.name.type = CEPH_ENTITY_TYPE_CLIENT;
719 monc->client->msgr->inst.name.num = monc->auth->global_id;
720
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800721 __send_subscribe(monc);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700722 __resend_generic_request(monc);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800723 }
724 mutex_unlock(&monc->mutex);
725}
726
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800727static int __validate_auth(struct ceph_mon_client *monc)
728{
729 int ret;
730
731 if (monc->pending_auth)
732 return 0;
733
734 ret = ceph_build_auth(monc->auth, monc->m_auth->front.iov_base,
735 monc->m_auth->front_max);
736 if (ret <= 0)
737 return ret; /* either an error, or no need to authenticate */
738 __send_prepared_auth_request(monc, ret);
739 return 0;
740}
741
742int ceph_monc_validate_auth(struct ceph_mon_client *monc)
743{
744 int ret;
745
746 mutex_lock(&monc->mutex);
747 ret = __validate_auth(monc);
748 mutex_unlock(&monc->mutex);
749 return ret;
750}
751
Sage Weilba75bb92009-10-06 11:31:11 -0700752/*
753 * handle incoming message
754 */
755static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
756{
757 struct ceph_mon_client *monc = con->private;
758 int type = le16_to_cpu(msg->hdr.type);
759
760 if (!monc)
761 return;
762
763 switch (type) {
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800764 case CEPH_MSG_AUTH_REPLY:
765 handle_auth_reply(monc, msg);
Sage Weilba75bb92009-10-06 11:31:11 -0700766 break;
767
768 case CEPH_MSG_MON_SUBSCRIBE_ACK:
769 handle_subscribe_ack(monc, msg);
770 break;
771
772 case CEPH_MSG_STATFS_REPLY:
773 handle_statfs_reply(monc, msg);
774 break;
775
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800776 case CEPH_MSG_MON_MAP:
777 ceph_monc_handle_map(monc, msg);
778 break;
779
Sage Weilba75bb92009-10-06 11:31:11 -0700780 case CEPH_MSG_MDS_MAP:
781 ceph_mdsc_handle_map(&monc->client->mdsc, msg);
782 break;
783
784 case CEPH_MSG_OSD_MAP:
785 ceph_osdc_handle_map(&monc->client->osdc, msg);
786 break;
787
788 default:
789 pr_err("received unknown message type %d %s\n", type,
790 ceph_msg_type_name(type));
791 }
792 ceph_msg_put(msg);
793}
794
795/*
796 * Allocate memory for incoming message
797 */
798static struct ceph_msg *mon_alloc_msg(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -0800799 struct ceph_msg_header *hdr,
800 int *skip)
Sage Weilba75bb92009-10-06 11:31:11 -0700801{
802 struct ceph_mon_client *monc = con->private;
803 int type = le16_to_cpu(hdr->type);
Yehuda Sadeh24504182010-01-08 13:58:34 -0800804 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -0800805 struct ceph_msg *m = NULL;
Sage Weilba75bb92009-10-06 11:31:11 -0700806
Yehuda Sadeh24504182010-01-08 13:58:34 -0800807 *skip = 0;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -0800808
Sage Weilba75bb92009-10-06 11:31:11 -0700809 switch (type) {
Sage Weilba75bb92009-10-06 11:31:11 -0700810 case CEPH_MSG_MON_SUBSCRIBE_ACK:
Sage Weil7c315c52010-03-24 21:52:30 -0700811 m = ceph_msg_get(monc->m_subscribe_ack);
Yehuda Sadeh24504182010-01-08 13:58:34 -0800812 break;
Sage Weilba75bb92009-10-06 11:31:11 -0700813 case CEPH_MSG_STATFS_REPLY:
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700814 return get_generic_reply(con, hdr, skip);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800815 case CEPH_MSG_AUTH_REPLY:
Sage Weil6694d6b2010-03-24 21:48:05 -0700816 m = ceph_msg_get(monc->m_auth_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -0800817 break;
Sage Weil5b3a4db2010-02-19 21:43:23 -0800818 case CEPH_MSG_MON_MAP:
819 case CEPH_MSG_MDS_MAP:
820 case CEPH_MSG_OSD_MAP:
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700821 m = ceph_msg_new(type, front_len, GFP_NOFS);
Sage Weil5b3a4db2010-02-19 21:43:23 -0800822 break;
Sage Weilba75bb92009-10-06 11:31:11 -0700823 }
Yehuda Sadeh24504182010-01-08 13:58:34 -0800824
Sage Weil5b3a4db2010-02-19 21:43:23 -0800825 if (!m) {
826 pr_info("alloc_msg unknown type %d\n", type);
Yehuda Sadeh24504182010-01-08 13:58:34 -0800827 *skip = 1;
Sage Weil5b3a4db2010-02-19 21:43:23 -0800828 }
Yehuda Sadeh24504182010-01-08 13:58:34 -0800829 return m;
Sage Weilba75bb92009-10-06 11:31:11 -0700830}
831
832/*
833 * If the monitor connection resets, pick a new monitor and resubmit
834 * any pending requests.
835 */
836static void mon_fault(struct ceph_connection *con)
837{
838 struct ceph_mon_client *monc = con->private;
839
840 if (!monc)
841 return;
842
843 dout("mon_fault\n");
844 mutex_lock(&monc->mutex);
845 if (!con->private)
846 goto out;
847
848 if (monc->con && !monc->hunting)
849 pr_info("mon%d %s session lost, "
850 "hunting for new mon\n", monc->cur_mon,
851 pr_addr(&monc->con->peer_addr.in_addr));
852
853 __close_session(monc);
854 if (!monc->hunting) {
855 /* start hunting */
856 monc->hunting = true;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800857 __open_session(monc);
Sage Weilba75bb92009-10-06 11:31:11 -0700858 } else {
859 /* already hunting, let's wait a bit */
860 __schedule_delayed(monc);
861 }
862out:
863 mutex_unlock(&monc->mutex);
864}
865
Tobias Klauser9e327892010-05-20 10:40:19 +0200866static const struct ceph_connection_operations mon_con_ops = {
Sage Weilba75bb92009-10-06 11:31:11 -0700867 .get = ceph_con_get,
868 .put = ceph_con_put,
869 .dispatch = dispatch,
870 .fault = mon_fault,
871 .alloc_msg = mon_alloc_msg,
Sage Weilba75bb92009-10-06 11:31:11 -0700872};