blob: 61cb11701f109e1fd923906914791b7db954d1c8 [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
31const static struct ceph_connection_operations mon_con_ops;
32
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);
107 ceph_msg_get(monc->m_auth); /* keep our ref */
108 ceph_con_send(monc->con, monc->m_auth);
109}
110
111/*
Sage Weilba75bb92009-10-06 11:31:11 -0700112 * Close monitor session, if any.
113 */
114static void __close_session(struct ceph_mon_client *monc)
115{
116 if (monc->con) {
117 dout("__close_session closing mon%d\n", monc->cur_mon);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800118 ceph_con_revoke(monc->con, monc->m_auth);
Sage Weilba75bb92009-10-06 11:31:11 -0700119 ceph_con_close(monc->con);
120 monc->cur_mon = -1;
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800121 monc->pending_auth = 0;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800122 ceph_auth_reset(monc->auth);
Sage Weilba75bb92009-10-06 11:31:11 -0700123 }
124}
125
126/*
127 * Open a session with a (new) monitor.
128 */
129static int __open_session(struct ceph_mon_client *monc)
130{
131 char r;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800132 int ret;
Sage Weilba75bb92009-10-06 11:31:11 -0700133
134 if (monc->cur_mon < 0) {
135 get_random_bytes(&r, 1);
136 monc->cur_mon = r % monc->monmap->num_mon;
137 dout("open_session num=%d r=%d -> mon%d\n",
138 monc->monmap->num_mon, r, monc->cur_mon);
139 monc->sub_sent = 0;
140 monc->sub_renew_after = jiffies; /* i.e., expired */
141 monc->want_next_osdmap = !!monc->want_next_osdmap;
142
143 dout("open_session mon%d opening\n", monc->cur_mon);
144 monc->con->peer_name.type = CEPH_ENTITY_TYPE_MON;
145 monc->con->peer_name.num = cpu_to_le64(monc->cur_mon);
146 ceph_con_open(monc->con,
147 &monc->monmap->mon_inst[monc->cur_mon].addr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800148
149 /* initiatiate authentication handshake */
150 ret = ceph_auth_build_hello(monc->auth,
151 monc->m_auth->front.iov_base,
152 monc->m_auth->front_max);
Sage Weil5ce6e9d2010-02-15 16:22:28 -0800153 __send_prepared_auth_request(monc, ret);
Sage Weilba75bb92009-10-06 11:31:11 -0700154 } else {
155 dout("open_session mon%d already open\n", monc->cur_mon);
156 }
157 return 0;
158}
159
160static bool __sub_expired(struct ceph_mon_client *monc)
161{
162 return time_after_eq(jiffies, monc->sub_renew_after);
163}
164
165/*
166 * Reschedule delayed work timer.
167 */
168static void __schedule_delayed(struct ceph_mon_client *monc)
169{
170 unsigned delay;
171
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800172 if (monc->cur_mon < 0 || __sub_expired(monc))
Sage Weilba75bb92009-10-06 11:31:11 -0700173 delay = 10 * HZ;
174 else
175 delay = 20 * HZ;
176 dout("__schedule_delayed after %u\n", delay);
177 schedule_delayed_work(&monc->delayed_work, delay);
178}
179
180/*
181 * Send subscribe request for mdsmap and/or osdmap.
182 */
183static void __send_subscribe(struct ceph_mon_client *monc)
184{
185 dout("__send_subscribe sub_sent=%u exp=%u want_osd=%d\n",
186 (unsigned)monc->sub_sent, __sub_expired(monc),
187 monc->want_next_osdmap);
188 if ((__sub_expired(monc) && !monc->sub_sent) ||
189 monc->want_next_osdmap == 1) {
190 struct ceph_msg *msg;
191 struct ceph_mon_subscribe_item *i;
192 void *p, *end;
193
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700194 msg = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 96, GFP_NOFS);
Sage Weilba75bb92009-10-06 11:31:11 -0700195 if (!msg)
196 return;
197
198 p = msg->front.iov_base;
199 end = p + msg->front.iov_len;
200
201 dout("__send_subscribe to 'mdsmap' %u+\n",
202 (unsigned)monc->have_mdsmap);
203 if (monc->want_next_osdmap) {
204 dout("__send_subscribe to 'osdmap' %u\n",
205 (unsigned)monc->have_osdmap);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800206 ceph_encode_32(&p, 3);
Sage Weilba75bb92009-10-06 11:31:11 -0700207 ceph_encode_string(&p, end, "osdmap", 6);
208 i = p;
209 i->have = cpu_to_le64(monc->have_osdmap);
210 i->onetime = 1;
211 p += sizeof(*i);
212 monc->want_next_osdmap = 2; /* requested */
213 } else {
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800214 ceph_encode_32(&p, 2);
Sage Weilba75bb92009-10-06 11:31:11 -0700215 }
216 ceph_encode_string(&p, end, "mdsmap", 6);
217 i = p;
218 i->have = cpu_to_le64(monc->have_mdsmap);
219 i->onetime = 0;
220 p += sizeof(*i);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800221 ceph_encode_string(&p, end, "monmap", 6);
222 i = p;
223 i->have = 0;
224 i->onetime = 0;
225 p += sizeof(*i);
Sage Weilba75bb92009-10-06 11:31:11 -0700226
227 msg->front.iov_len = p - msg->front.iov_base;
228 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
229 ceph_con_send(monc->con, msg);
230
231 monc->sub_sent = jiffies | 1; /* never 0 */
232 }
233}
234
235static void handle_subscribe_ack(struct ceph_mon_client *monc,
236 struct ceph_msg *msg)
237{
238 unsigned seconds;
Sage Weil07bd10f2009-10-14 17:26:40 -0700239 struct ceph_mon_subscribe_ack *h = msg->front.iov_base;
Sage Weilba75bb92009-10-06 11:31:11 -0700240
Sage Weil07bd10f2009-10-14 17:26:40 -0700241 if (msg->front.iov_len < sizeof(*h))
242 goto bad;
243 seconds = le32_to_cpu(h->duration);
244
Sage Weilba75bb92009-10-06 11:31:11 -0700245 mutex_lock(&monc->mutex);
246 if (monc->hunting) {
247 pr_info("mon%d %s session established\n",
248 monc->cur_mon, pr_addr(&monc->con->peer_addr.in_addr));
249 monc->hunting = false;
250 }
251 dout("handle_subscribe_ack after %d seconds\n", seconds);
Sage Weil0656d112009-10-08 10:25:46 -0700252 monc->sub_renew_after = monc->sub_sent + (seconds >> 1)*HZ - 1;
Sage Weilba75bb92009-10-06 11:31:11 -0700253 monc->sub_sent = 0;
254 mutex_unlock(&monc->mutex);
255 return;
256bad:
257 pr_err("got corrupt subscribe-ack msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -0800258 ceph_msg_dump(msg);
Sage Weilba75bb92009-10-06 11:31:11 -0700259}
260
261/*
262 * Keep track of which maps we have
263 */
264int ceph_monc_got_mdsmap(struct ceph_mon_client *monc, u32 got)
265{
266 mutex_lock(&monc->mutex);
267 monc->have_mdsmap = got;
268 mutex_unlock(&monc->mutex);
269 return 0;
270}
271
272int ceph_monc_got_osdmap(struct ceph_mon_client *monc, u32 got)
273{
274 mutex_lock(&monc->mutex);
275 monc->have_osdmap = got;
276 monc->want_next_osdmap = 0;
277 mutex_unlock(&monc->mutex);
278 return 0;
279}
280
281/*
282 * Register interest in the next osdmap
283 */
284void ceph_monc_request_next_osdmap(struct ceph_mon_client *monc)
285{
286 dout("request_next_osdmap have %u\n", monc->have_osdmap);
287 mutex_lock(&monc->mutex);
288 if (!monc->want_next_osdmap)
289 monc->want_next_osdmap = 1;
290 if (monc->want_next_osdmap < 2)
291 __send_subscribe(monc);
292 mutex_unlock(&monc->mutex);
293}
294
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800295/*
Sage Weil50b885b2009-12-01 14:12:07 -0800296 *
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800297 */
298int ceph_monc_open_session(struct ceph_mon_client *monc)
Sage Weilba75bb92009-10-06 11:31:11 -0700299{
300 if (!monc->con) {
301 monc->con = kmalloc(sizeof(*monc->con), GFP_KERNEL);
302 if (!monc->con)
303 return -ENOMEM;
304 ceph_con_init(monc->client->msgr, monc->con);
305 monc->con->private = monc;
306 monc->con->ops = &mon_con_ops;
307 }
308
309 mutex_lock(&monc->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800310 __open_session(monc);
Sage Weilba75bb92009-10-06 11:31:11 -0700311 __schedule_delayed(monc);
312 mutex_unlock(&monc->mutex);
313 return 0;
314}
315
316/*
317 * The monitor responds with mount ack indicate mount success. The
318 * included client ticket allows the client to talk to MDSs and OSDs.
319 */
Sage Weil07433042009-11-18 16:50:41 -0800320static void ceph_monc_handle_map(struct ceph_mon_client *monc,
321 struct ceph_msg *msg)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800322{
323 struct ceph_client *client = monc->client;
324 struct ceph_monmap *monmap = NULL, *old = monc->monmap;
325 void *p, *end;
326
327 mutex_lock(&monc->mutex);
328
329 dout("handle_monmap\n");
330 p = msg->front.iov_base;
331 end = p + msg->front.iov_len;
332
333 monmap = ceph_monmap_decode(p, end);
334 if (IS_ERR(monmap)) {
335 pr_err("problem decoding monmap, %d\n",
336 (int)PTR_ERR(monmap));
Sage Weild4a780c2009-12-11 08:55:23 -0800337 goto out;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800338 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800339
Sage Weil07433042009-11-18 16:50:41 -0800340 if (ceph_check_fsid(monc->client, &monmap->fsid) < 0) {
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800341 kfree(monmap);
Sage Weild4a780c2009-12-11 08:55:23 -0800342 goto out;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800343 }
344
345 client->monc.monmap = monmap;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800346 kfree(old);
347
Sage Weild4a780c2009-12-11 08:55:23 -0800348out:
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800349 mutex_unlock(&monc->mutex);
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800350 wake_up(&client->auth_wq);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800351}
Sage Weilba75bb92009-10-06 11:31:11 -0700352
Sage Weilba75bb92009-10-06 11:31:11 -0700353/*
354 * statfs
355 */
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700356static struct ceph_mon_generic_request *__lookup_generic_req(
Sage Weil85ff03f2010-02-15 14:47:28 -0800357 struct ceph_mon_client *monc, u64 tid)
358{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700359 struct ceph_mon_generic_request *req;
360 struct rb_node *n = monc->generic_request_tree.rb_node;
Sage Weil85ff03f2010-02-15 14:47:28 -0800361
362 while (n) {
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700363 req = rb_entry(n, struct ceph_mon_generic_request, node);
Sage Weil85ff03f2010-02-15 14:47:28 -0800364 if (tid < req->tid)
365 n = n->rb_left;
366 else if (tid > req->tid)
367 n = n->rb_right;
368 else
369 return req;
370 }
371 return NULL;
372}
373
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700374static void __insert_generic_request(struct ceph_mon_client *monc,
375 struct ceph_mon_generic_request *new)
Sage Weil85ff03f2010-02-15 14:47:28 -0800376{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700377 struct rb_node **p = &monc->generic_request_tree.rb_node;
Sage Weil85ff03f2010-02-15 14:47:28 -0800378 struct rb_node *parent = NULL;
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700379 struct ceph_mon_generic_request *req = NULL;
Sage Weil85ff03f2010-02-15 14:47:28 -0800380
381 while (*p) {
382 parent = *p;
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700383 req = rb_entry(parent, struct ceph_mon_generic_request, node);
Sage Weil85ff03f2010-02-15 14:47:28 -0800384 if (new->tid < req->tid)
385 p = &(*p)->rb_left;
386 else if (new->tid > req->tid)
387 p = &(*p)->rb_right;
388 else
389 BUG();
390 }
391
392 rb_link_node(&new->node, parent, p);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700393 rb_insert_color(&new->node, &monc->generic_request_tree);
Sage Weil85ff03f2010-02-15 14:47:28 -0800394}
395
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700396static void release_generic_request(struct kref *kref)
Sage Weil3143edd2010-03-24 21:43:33 -0700397{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700398 struct ceph_mon_generic_request *req =
399 container_of(kref, struct ceph_mon_generic_request, kref);
Sage Weil3143edd2010-03-24 21:43:33 -0700400
401 if (req->reply)
402 ceph_msg_put(req->reply);
403 if (req->request)
404 ceph_msg_put(req->request);
405}
406
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700407static void put_generic_request(struct ceph_mon_generic_request *req)
Sage Weil3143edd2010-03-24 21:43:33 -0700408{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700409 kref_put(&req->kref, release_generic_request);
Sage Weil3143edd2010-03-24 21:43:33 -0700410}
411
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700412static void get_generic_request(struct ceph_mon_generic_request *req)
Sage Weil3143edd2010-03-24 21:43:33 -0700413{
414 kref_get(&req->kref);
415}
416
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700417static struct ceph_msg *get_generic_reply(struct ceph_connection *con,
Sage Weil3143edd2010-03-24 21:43:33 -0700418 struct ceph_msg_header *hdr,
419 int *skip)
420{
421 struct ceph_mon_client *monc = con->private;
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700422 struct ceph_mon_generic_request *req;
Sage Weil3143edd2010-03-24 21:43:33 -0700423 u64 tid = le64_to_cpu(hdr->tid);
424 struct ceph_msg *m;
425
426 mutex_lock(&monc->mutex);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700427 req = __lookup_generic_req(monc, tid);
Sage Weil3143edd2010-03-24 21:43:33 -0700428 if (!req) {
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700429 dout("get_generic_reply %lld dne\n", tid);
Sage Weil3143edd2010-03-24 21:43:33 -0700430 *skip = 1;
431 m = NULL;
432 } else {
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700433 dout("get_generic_reply %lld got %p\n", tid, req->reply);
Sage Weil3143edd2010-03-24 21:43:33 -0700434 m = ceph_msg_get(req->reply);
435 /*
436 * we don't need to track the connection reading into
437 * this reply because we only have one open connection
438 * at a time, ever.
439 */
440 }
441 mutex_unlock(&monc->mutex);
442 return m;
443}
444
Sage Weilba75bb92009-10-06 11:31:11 -0700445static void handle_statfs_reply(struct ceph_mon_client *monc,
446 struct ceph_msg *msg)
447{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700448 struct ceph_mon_generic_request *req;
Sage Weilba75bb92009-10-06 11:31:11 -0700449 struct ceph_mon_statfs_reply *reply = msg->front.iov_base;
Sage Weil3143edd2010-03-24 21:43:33 -0700450 u64 tid = le64_to_cpu(msg->hdr.tid);
Sage Weilba75bb92009-10-06 11:31:11 -0700451
452 if (msg->front.iov_len != sizeof(*reply))
453 goto bad;
Sage Weilba75bb92009-10-06 11:31:11 -0700454 dout("handle_statfs_reply %p tid %llu\n", msg, tid);
455
456 mutex_lock(&monc->mutex);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700457 req = __lookup_generic_req(monc, tid);
Sage Weilba75bb92009-10-06 11:31:11 -0700458 if (req) {
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700459 *(struct ceph_statfs *)req->buf = reply->st;
Sage Weilba75bb92009-10-06 11:31:11 -0700460 req->result = 0;
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700461 get_generic_request(req);
Sage Weilba75bb92009-10-06 11:31:11 -0700462 }
463 mutex_unlock(&monc->mutex);
Sage Weil3143edd2010-03-24 21:43:33 -0700464 if (req) {
Sage Weilba75bb92009-10-06 11:31:11 -0700465 complete(&req->completion);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700466 put_generic_request(req);
Sage Weil3143edd2010-03-24 21:43:33 -0700467 }
Sage Weilba75bb92009-10-06 11:31:11 -0700468 return;
469
470bad:
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700471 pr_err("corrupt generic reply, no tid\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -0800472 ceph_msg_dump(msg);
Sage Weilba75bb92009-10-06 11:31:11 -0700473}
474
475/*
Sage Weilba75bb92009-10-06 11:31:11 -0700476 * Do a synchronous statfs().
477 */
478int ceph_monc_do_statfs(struct ceph_mon_client *monc, struct ceph_statfs *buf)
479{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700480 struct ceph_mon_generic_request *req;
Sage Weil3143edd2010-03-24 21:43:33 -0700481 struct ceph_mon_statfs *h;
Sage Weilba75bb92009-10-06 11:31:11 -0700482 int err;
483
Julia Lawallcffe7b62010-05-13 22:07:29 +0200484 req = kzalloc(sizeof(*req), GFP_NOFS);
Sage Weil3143edd2010-03-24 21:43:33 -0700485 if (!req)
486 return -ENOMEM;
Sage Weilba75bb92009-10-06 11:31:11 -0700487
Sage Weil3143edd2010-03-24 21:43:33 -0700488 kref_init(&req->kref);
489 req->buf = buf;
490 init_completion(&req->completion);
491
Sage Weila79832f2010-04-01 16:06:19 -0700492 err = -ENOMEM;
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700493 req->request = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -0700494 if (!req->request)
Sage Weil3143edd2010-03-24 21:43:33 -0700495 goto out;
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700496 req->reply = ceph_msg_new(CEPH_MSG_STATFS_REPLY, 1024, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -0700497 if (!req->reply)
Sage Weil3143edd2010-03-24 21:43:33 -0700498 goto out;
Sage Weil3143edd2010-03-24 21:43:33 -0700499
500 /* fill out request */
501 h = req->request->front.iov_base;
502 h->monhdr.have_version = 0;
503 h->monhdr.session_mon = cpu_to_le16(-1);
504 h->monhdr.session_mon_tid = 0;
505 h->fsid = monc->monmap->fsid;
Sage Weilba75bb92009-10-06 11:31:11 -0700506
507 /* register request */
508 mutex_lock(&monc->mutex);
Sage Weil3143edd2010-03-24 21:43:33 -0700509 req->tid = ++monc->last_tid;
510 req->request->hdr.tid = cpu_to_le64(req->tid);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700511 __insert_generic_request(monc, req);
512 monc->num_generic_requests++;
Sage Weilba75bb92009-10-06 11:31:11 -0700513 mutex_unlock(&monc->mutex);
514
515 /* send request and wait */
Sage Weil3143edd2010-03-24 21:43:33 -0700516 ceph_con_send(monc->con, ceph_msg_get(req->request));
517 err = wait_for_completion_interruptible(&req->completion);
Sage Weilba75bb92009-10-06 11:31:11 -0700518
519 mutex_lock(&monc->mutex);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700520 rb_erase(&req->node, &monc->generic_request_tree);
521 monc->num_generic_requests--;
Sage Weilba75bb92009-10-06 11:31:11 -0700522 mutex_unlock(&monc->mutex);
523
524 if (!err)
Sage Weil3143edd2010-03-24 21:43:33 -0700525 err = req->result;
526
527out:
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700528 kref_put(&req->kref, release_generic_request);
Sage Weilba75bb92009-10-06 11:31:11 -0700529 return err;
530}
531
532/*
533 * Resend pending statfs requests.
534 */
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700535static void __resend_generic_request(struct ceph_mon_client *monc)
Sage Weilba75bb92009-10-06 11:31:11 -0700536{
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700537 struct ceph_mon_generic_request *req;
Sage Weil85ff03f2010-02-15 14:47:28 -0800538 struct rb_node *p;
Sage Weilba75bb92009-10-06 11:31:11 -0700539
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700540 for (p = rb_first(&monc->generic_request_tree); p; p = rb_next(p)) {
541 req = rb_entry(p, struct ceph_mon_generic_request, node);
Sage Weil3143edd2010-03-24 21:43:33 -0700542 ceph_con_send(monc->con, ceph_msg_get(req->request));
Sage Weilba75bb92009-10-06 11:31:11 -0700543 }
544}
545
546/*
547 * Delayed work. If we haven't mounted yet, retry. Otherwise,
548 * renew/retry subscription as needed (in case it is timing out, or we
549 * got an ENOMEM). And keep the monitor connection alive.
550 */
551static void delayed_work(struct work_struct *work)
552{
553 struct ceph_mon_client *monc =
554 container_of(work, struct ceph_mon_client, delayed_work.work);
555
556 dout("monc delayed_work\n");
557 mutex_lock(&monc->mutex);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800558 if (monc->hunting) {
559 __close_session(monc);
560 __open_session(monc); /* continue hunting */
Sage Weilba75bb92009-10-06 11:31:11 -0700561 } else {
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800562 ceph_con_keepalive(monc->con);
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800563
564 __validate_auth(monc);
565
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800566 if (monc->auth->ops->is_authenticated(monc->auth))
567 __send_subscribe(monc);
Sage Weilba75bb92009-10-06 11:31:11 -0700568 }
Sage Weilba75bb92009-10-06 11:31:11 -0700569 __schedule_delayed(monc);
570 mutex_unlock(&monc->mutex);
571}
572
Sage Weil6b805182009-10-27 11:50:50 -0700573/*
574 * On startup, we build a temporary monmap populated with the IPs
575 * provided by mount(2).
576 */
577static int build_initial_monmap(struct ceph_mon_client *monc)
578{
579 struct ceph_mount_args *args = monc->client->mount_args;
580 struct ceph_entity_addr *mon_addr = args->mon_addr;
581 int num_mon = args->num_mon;
582 int i;
583
584 /* build initial monmap */
585 monc->monmap = kzalloc(sizeof(*monc->monmap) +
586 num_mon*sizeof(monc->monmap->mon_inst[0]),
587 GFP_KERNEL);
588 if (!monc->monmap)
589 return -ENOMEM;
590 for (i = 0; i < num_mon; i++) {
591 monc->monmap->mon_inst[i].addr = mon_addr[i];
Sage Weil6b805182009-10-27 11:50:50 -0700592 monc->monmap->mon_inst[i].addr.nonce = 0;
593 monc->monmap->mon_inst[i].name.type =
594 CEPH_ENTITY_TYPE_MON;
595 monc->monmap->mon_inst[i].name.num = cpu_to_le64(i);
596 }
597 monc->monmap->num_mon = num_mon;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800598 monc->have_fsid = false;
Sage Weil6b805182009-10-27 11:50:50 -0700599
600 /* release addr memory */
601 kfree(args->mon_addr);
602 args->mon_addr = NULL;
603 args->num_mon = 0;
604 return 0;
605}
606
Sage Weilba75bb92009-10-06 11:31:11 -0700607int ceph_monc_init(struct ceph_mon_client *monc, struct ceph_client *cl)
608{
609 int err = 0;
610
611 dout("init\n");
612 memset(monc, 0, sizeof(*monc));
613 monc->client = cl;
614 monc->monmap = NULL;
615 mutex_init(&monc->mutex);
616
Sage Weil6b805182009-10-27 11:50:50 -0700617 err = build_initial_monmap(monc);
618 if (err)
619 goto out;
620
Sage Weilba75bb92009-10-06 11:31:11 -0700621 monc->con = NULL;
622
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800623 /* authentication */
624 monc->auth = ceph_auth_init(cl->mount_args->name,
625 cl->mount_args->secret);
626 if (IS_ERR(monc->auth))
627 return PTR_ERR(monc->auth);
628 monc->auth->want_keys =
629 CEPH_ENTITY_TYPE_AUTH | CEPH_ENTITY_TYPE_MON |
630 CEPH_ENTITY_TYPE_OSD | CEPH_ENTITY_TYPE_MDS;
631
Sage Weilba75bb92009-10-06 11:31:11 -0700632 /* msg pools */
Sage Weila79832f2010-04-01 16:06:19 -0700633 err = -ENOMEM;
Sage Weil7c315c52010-03-24 21:52:30 -0700634 monc->m_subscribe_ack = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE_ACK,
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700635 sizeof(struct ceph_mon_subscribe_ack),
636 GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -0700637 if (!monc->m_subscribe_ack)
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800638 goto out_monmap;
Sage Weil6694d6b2010-03-24 21:48:05 -0700639
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700640 monc->m_auth_reply = ceph_msg_new(CEPH_MSG_AUTH_REPLY, 4096, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -0700641 if (!monc->m_auth_reply)
Sage Weil7c315c52010-03-24 21:52:30 -0700642 goto out_subscribe_ack;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800643
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700644 monc->m_auth = ceph_msg_new(CEPH_MSG_AUTH, 4096, GFP_NOFS);
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800645 monc->pending_auth = 0;
Sage Weila79832f2010-04-01 16:06:19 -0700646 if (!monc->m_auth)
Sage Weil6694d6b2010-03-24 21:48:05 -0700647 goto out_auth_reply;
Sage Weilba75bb92009-10-06 11:31:11 -0700648
649 monc->cur_mon = -1;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800650 monc->hunting = true;
Sage Weilba75bb92009-10-06 11:31:11 -0700651 monc->sub_renew_after = jiffies;
652 monc->sub_sent = 0;
653
654 INIT_DELAYED_WORK(&monc->delayed_work, delayed_work);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700655 monc->generic_request_tree = RB_ROOT;
656 monc->num_generic_requests = 0;
Sage Weilba75bb92009-10-06 11:31:11 -0700657 monc->last_tid = 0;
658
659 monc->have_mdsmap = 0;
660 monc->have_osdmap = 0;
661 monc->want_next_osdmap = 1;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800662 return 0;
663
Sage Weil6694d6b2010-03-24 21:48:05 -0700664out_auth_reply:
665 ceph_msg_put(monc->m_auth_reply);
Sage Weil7c315c52010-03-24 21:52:30 -0700666out_subscribe_ack:
667 ceph_msg_put(monc->m_subscribe_ack);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800668out_monmap:
669 kfree(monc->monmap);
Sage Weilba75bb92009-10-06 11:31:11 -0700670out:
671 return err;
672}
673
674void ceph_monc_stop(struct ceph_mon_client *monc)
675{
676 dout("stop\n");
677 cancel_delayed_work_sync(&monc->delayed_work);
678
679 mutex_lock(&monc->mutex);
680 __close_session(monc);
681 if (monc->con) {
682 monc->con->private = NULL;
683 monc->con->ops->put(monc->con);
684 monc->con = NULL;
685 }
686 mutex_unlock(&monc->mutex);
687
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800688 ceph_auth_destroy(monc->auth);
689
690 ceph_msg_put(monc->m_auth);
Sage Weil6694d6b2010-03-24 21:48:05 -0700691 ceph_msg_put(monc->m_auth_reply);
Sage Weil7c315c52010-03-24 21:52:30 -0700692 ceph_msg_put(monc->m_subscribe_ack);
Sage Weilba75bb92009-10-06 11:31:11 -0700693
694 kfree(monc->monmap);
695}
696
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800697static void handle_auth_reply(struct ceph_mon_client *monc,
698 struct ceph_msg *msg)
699{
700 int ret;
701
702 mutex_lock(&monc->mutex);
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800703 monc->pending_auth = 0;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800704 ret = ceph_handle_auth_reply(monc->auth, msg->front.iov_base,
705 msg->front.iov_len,
706 monc->m_auth->front.iov_base,
707 monc->m_auth->front_max);
708 if (ret < 0) {
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800709 monc->client->auth_err = ret;
710 wake_up(&monc->client->auth_wq);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800711 } else if (ret > 0) {
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800712 __send_prepared_auth_request(monc, ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800713 } else if (monc->auth->ops->is_authenticated(monc->auth)) {
714 dout("authenticated, starting session\n");
Sage Weil07433042009-11-18 16:50:41 -0800715
716 monc->client->msgr->inst.name.type = CEPH_ENTITY_TYPE_CLIENT;
717 monc->client->msgr->inst.name.num = monc->auth->global_id;
718
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800719 __send_subscribe(monc);
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700720 __resend_generic_request(monc);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800721 }
722 mutex_unlock(&monc->mutex);
723}
724
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800725static int __validate_auth(struct ceph_mon_client *monc)
726{
727 int ret;
728
729 if (monc->pending_auth)
730 return 0;
731
732 ret = ceph_build_auth(monc->auth, monc->m_auth->front.iov_base,
733 monc->m_auth->front_max);
734 if (ret <= 0)
735 return ret; /* either an error, or no need to authenticate */
736 __send_prepared_auth_request(monc, ret);
737 return 0;
738}
739
740int ceph_monc_validate_auth(struct ceph_mon_client *monc)
741{
742 int ret;
743
744 mutex_lock(&monc->mutex);
745 ret = __validate_auth(monc);
746 mutex_unlock(&monc->mutex);
747 return ret;
748}
749
Sage Weilba75bb92009-10-06 11:31:11 -0700750/*
751 * handle incoming message
752 */
753static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
754{
755 struct ceph_mon_client *monc = con->private;
756 int type = le16_to_cpu(msg->hdr.type);
757
758 if (!monc)
759 return;
760
761 switch (type) {
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800762 case CEPH_MSG_AUTH_REPLY:
763 handle_auth_reply(monc, msg);
Sage Weilba75bb92009-10-06 11:31:11 -0700764 break;
765
766 case CEPH_MSG_MON_SUBSCRIBE_ACK:
767 handle_subscribe_ack(monc, msg);
768 break;
769
770 case CEPH_MSG_STATFS_REPLY:
771 handle_statfs_reply(monc, msg);
772 break;
773
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800774 case CEPH_MSG_MON_MAP:
775 ceph_monc_handle_map(monc, msg);
776 break;
777
Sage Weilba75bb92009-10-06 11:31:11 -0700778 case CEPH_MSG_MDS_MAP:
779 ceph_mdsc_handle_map(&monc->client->mdsc, msg);
780 break;
781
782 case CEPH_MSG_OSD_MAP:
783 ceph_osdc_handle_map(&monc->client->osdc, msg);
784 break;
785
786 default:
787 pr_err("received unknown message type %d %s\n", type,
788 ceph_msg_type_name(type));
789 }
790 ceph_msg_put(msg);
791}
792
793/*
794 * Allocate memory for incoming message
795 */
796static struct ceph_msg *mon_alloc_msg(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -0800797 struct ceph_msg_header *hdr,
798 int *skip)
Sage Weilba75bb92009-10-06 11:31:11 -0700799{
800 struct ceph_mon_client *monc = con->private;
801 int type = le16_to_cpu(hdr->type);
Yehuda Sadeh24504182010-01-08 13:58:34 -0800802 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -0800803 struct ceph_msg *m = NULL;
Sage Weilba75bb92009-10-06 11:31:11 -0700804
Yehuda Sadeh24504182010-01-08 13:58:34 -0800805 *skip = 0;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -0800806
Sage Weilba75bb92009-10-06 11:31:11 -0700807 switch (type) {
Sage Weilba75bb92009-10-06 11:31:11 -0700808 case CEPH_MSG_MON_SUBSCRIBE_ACK:
Sage Weil7c315c52010-03-24 21:52:30 -0700809 m = ceph_msg_get(monc->m_subscribe_ack);
Yehuda Sadeh24504182010-01-08 13:58:34 -0800810 break;
Sage Weilba75bb92009-10-06 11:31:11 -0700811 case CEPH_MSG_STATFS_REPLY:
Yehuda Sadehf8c76f62010-04-22 15:40:37 -0700812 return get_generic_reply(con, hdr, skip);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800813 case CEPH_MSG_AUTH_REPLY:
Sage Weil6694d6b2010-03-24 21:48:05 -0700814 m = ceph_msg_get(monc->m_auth_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -0800815 break;
Sage Weil5b3a4db2010-02-19 21:43:23 -0800816 case CEPH_MSG_MON_MAP:
817 case CEPH_MSG_MDS_MAP:
818 case CEPH_MSG_OSD_MAP:
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700819 m = ceph_msg_new(type, front_len, GFP_NOFS);
Sage Weil5b3a4db2010-02-19 21:43:23 -0800820 break;
Sage Weilba75bb92009-10-06 11:31:11 -0700821 }
Yehuda Sadeh24504182010-01-08 13:58:34 -0800822
Sage Weil5b3a4db2010-02-19 21:43:23 -0800823 if (!m) {
824 pr_info("alloc_msg unknown type %d\n", type);
Yehuda Sadeh24504182010-01-08 13:58:34 -0800825 *skip = 1;
Sage Weil5b3a4db2010-02-19 21:43:23 -0800826 }
Yehuda Sadeh24504182010-01-08 13:58:34 -0800827 return m;
Sage Weilba75bb92009-10-06 11:31:11 -0700828}
829
830/*
831 * If the monitor connection resets, pick a new monitor and resubmit
832 * any pending requests.
833 */
834static void mon_fault(struct ceph_connection *con)
835{
836 struct ceph_mon_client *monc = con->private;
837
838 if (!monc)
839 return;
840
841 dout("mon_fault\n");
842 mutex_lock(&monc->mutex);
843 if (!con->private)
844 goto out;
845
846 if (monc->con && !monc->hunting)
847 pr_info("mon%d %s session lost, "
848 "hunting for new mon\n", monc->cur_mon,
849 pr_addr(&monc->con->peer_addr.in_addr));
850
851 __close_session(monc);
852 if (!monc->hunting) {
853 /* start hunting */
854 monc->hunting = true;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800855 __open_session(monc);
Sage Weilba75bb92009-10-06 11:31:11 -0700856 } else {
857 /* already hunting, let's wait a bit */
858 __schedule_delayed(monc);
859 }
860out:
861 mutex_unlock(&monc->mutex);
862}
863
864const static struct ceph_connection_operations mon_con_ops = {
865 .get = ceph_con_get,
866 .put = ceph_con_put,
867 .dispatch = dispatch,
868 .fault = mon_fault,
869 .alloc_msg = mon_alloc_msg,
Sage Weilba75bb92009-10-06 11:31:11 -0700870};