Commit | Line | Data |
---|---|---|
ba75bb98 SW |
1 | #include "ceph_debug.h" |
2 | ||
3 | #include <linux/types.h> | |
4 | #include <linux/random.h> | |
5 | #include <linux/sched.h> | |
6 | ||
7 | #include "mon_client.h" | |
8 | #include "super.h" | |
4e7a5dcd | 9 | #include "auth.h" |
ba75bb98 SW |
10 | #include "decode.h" |
11 | ||
12 | /* | |
13 | * Interact with Ceph monitor cluster. Handle requests for new map | |
14 | * versions, and periodically resend as needed. Also implement | |
15 | * statfs() and umount(). | |
16 | * | |
17 | * A small cluster of Ceph "monitors" are responsible for managing critical | |
18 | * cluster configuration and state information. An odd number (e.g., 3, 5) | |
19 | * of cmon daemons use a modified version of the Paxos part-time parliament | |
20 | * algorithm to manage the MDS map (mds cluster membership), OSD map, and | |
21 | * list of clients who have mounted the file system. | |
22 | * | |
23 | * We maintain an open, active session with a monitor at all times in order to | |
24 | * receive timely MDSMap updates. We periodically send a keepalive byte on the | |
25 | * TCP socket to ensure we detect a failure. If the connection does break, we | |
26 | * randomly hunt for a new monitor. Once the connection is reestablished, we | |
27 | * resend any outstanding requests. | |
28 | */ | |
29 | ||
30 | const static struct ceph_connection_operations mon_con_ops; | |
31 | ||
32 | /* | |
33 | * Decode a monmap blob (e.g., during mount). | |
34 | */ | |
35 | struct ceph_monmap *ceph_monmap_decode(void *p, void *end) | |
36 | { | |
37 | struct ceph_monmap *m = NULL; | |
38 | int i, err = -EINVAL; | |
39 | struct ceph_fsid fsid; | |
40 | u32 epoch, num_mon; | |
41 | u16 version; | |
4e7a5dcd SW |
42 | u32 len; |
43 | ||
44 | ceph_decode_32_safe(&p, end, len, bad); | |
45 | ceph_decode_need(&p, end, len, bad); | |
ba75bb98 SW |
46 | |
47 | dout("monmap_decode %p %p len %d\n", p, end, (int)(end-p)); | |
48 | ||
49 | ceph_decode_16_safe(&p, end, version, bad); | |
50 | ||
51 | ceph_decode_need(&p, end, sizeof(fsid) + 2*sizeof(u32), bad); | |
52 | ceph_decode_copy(&p, &fsid, sizeof(fsid)); | |
c89136ea | 53 | epoch = ceph_decode_32(&p); |
ba75bb98 | 54 | |
c89136ea | 55 | num_mon = ceph_decode_32(&p); |
ba75bb98 SW |
56 | ceph_decode_need(&p, end, num_mon*sizeof(m->mon_inst[0]), bad); |
57 | ||
58 | if (num_mon >= CEPH_MAX_MON) | |
59 | goto bad; | |
60 | m = kmalloc(sizeof(*m) + sizeof(m->mon_inst[0])*num_mon, GFP_NOFS); | |
61 | if (m == NULL) | |
62 | return ERR_PTR(-ENOMEM); | |
63 | m->fsid = fsid; | |
64 | m->epoch = epoch; | |
65 | m->num_mon = num_mon; | |
66 | ceph_decode_copy(&p, m->mon_inst, num_mon*sizeof(m->mon_inst[0])); | |
63f2d211 SW |
67 | for (i = 0; i < num_mon; i++) |
68 | ceph_decode_addr(&m->mon_inst[i].addr); | |
ba75bb98 | 69 | |
ba75bb98 SW |
70 | dout("monmap_decode epoch %d, num_mon %d\n", m->epoch, |
71 | m->num_mon); | |
72 | for (i = 0; i < m->num_mon; i++) | |
73 | dout("monmap_decode mon%d is %s\n", i, | |
74 | pr_addr(&m->mon_inst[i].addr.in_addr)); | |
75 | return m; | |
76 | ||
77 | bad: | |
78 | dout("monmap_decode failed with %d\n", err); | |
79 | kfree(m); | |
80 | return ERR_PTR(err); | |
81 | } | |
82 | ||
83 | /* | |
84 | * return true if *addr is included in the monmap. | |
85 | */ | |
86 | int ceph_monmap_contains(struct ceph_monmap *m, struct ceph_entity_addr *addr) | |
87 | { | |
88 | int i; | |
89 | ||
90 | for (i = 0; i < m->num_mon; i++) | |
91 | if (ceph_entity_addr_equal(addr, &m->mon_inst[i].addr)) | |
92 | return 1; | |
93 | return 0; | |
94 | } | |
95 | ||
96 | /* | |
97 | * Close monitor session, if any. | |
98 | */ | |
99 | static void __close_session(struct ceph_mon_client *monc) | |
100 | { | |
101 | if (monc->con) { | |
102 | dout("__close_session closing mon%d\n", monc->cur_mon); | |
4e7a5dcd | 103 | ceph_con_revoke(monc->con, monc->m_auth); |
ba75bb98 SW |
104 | ceph_con_close(monc->con); |
105 | monc->cur_mon = -1; | |
4e7a5dcd | 106 | ceph_auth_reset(monc->auth); |
ba75bb98 SW |
107 | } |
108 | } | |
109 | ||
110 | /* | |
111 | * Open a session with a (new) monitor. | |
112 | */ | |
113 | static int __open_session(struct ceph_mon_client *monc) | |
114 | { | |
115 | char r; | |
4e7a5dcd | 116 | int ret; |
ba75bb98 SW |
117 | |
118 | if (monc->cur_mon < 0) { | |
119 | get_random_bytes(&r, 1); | |
120 | monc->cur_mon = r % monc->monmap->num_mon; | |
121 | dout("open_session num=%d r=%d -> mon%d\n", | |
122 | monc->monmap->num_mon, r, monc->cur_mon); | |
123 | monc->sub_sent = 0; | |
124 | monc->sub_renew_after = jiffies; /* i.e., expired */ | |
125 | monc->want_next_osdmap = !!monc->want_next_osdmap; | |
126 | ||
127 | dout("open_session mon%d opening\n", monc->cur_mon); | |
128 | monc->con->peer_name.type = CEPH_ENTITY_TYPE_MON; | |
129 | monc->con->peer_name.num = cpu_to_le64(monc->cur_mon); | |
130 | ceph_con_open(monc->con, | |
131 | &monc->monmap->mon_inst[monc->cur_mon].addr); | |
4e7a5dcd SW |
132 | |
133 | /* initiatiate authentication handshake */ | |
134 | ret = ceph_auth_build_hello(monc->auth, | |
135 | monc->m_auth->front.iov_base, | |
136 | monc->m_auth->front_max); | |
137 | monc->m_auth->front.iov_len = ret; | |
138 | monc->m_auth->hdr.front_len = cpu_to_le32(ret); | |
139 | ceph_msg_get(monc->m_auth); /* keep our ref */ | |
140 | ceph_con_send(monc->con, monc->m_auth); | |
ba75bb98 SW |
141 | } else { |
142 | dout("open_session mon%d already open\n", monc->cur_mon); | |
143 | } | |
144 | return 0; | |
145 | } | |
146 | ||
147 | static bool __sub_expired(struct ceph_mon_client *monc) | |
148 | { | |
149 | return time_after_eq(jiffies, monc->sub_renew_after); | |
150 | } | |
151 | ||
152 | /* | |
153 | * Reschedule delayed work timer. | |
154 | */ | |
155 | static void __schedule_delayed(struct ceph_mon_client *monc) | |
156 | { | |
157 | unsigned delay; | |
158 | ||
4e7a5dcd | 159 | if (monc->cur_mon < 0 || __sub_expired(monc)) |
ba75bb98 SW |
160 | delay = 10 * HZ; |
161 | else | |
162 | delay = 20 * HZ; | |
163 | dout("__schedule_delayed after %u\n", delay); | |
164 | schedule_delayed_work(&monc->delayed_work, delay); | |
165 | } | |
166 | ||
167 | /* | |
168 | * Send subscribe request for mdsmap and/or osdmap. | |
169 | */ | |
170 | static void __send_subscribe(struct ceph_mon_client *monc) | |
171 | { | |
172 | dout("__send_subscribe sub_sent=%u exp=%u want_osd=%d\n", | |
173 | (unsigned)monc->sub_sent, __sub_expired(monc), | |
174 | monc->want_next_osdmap); | |
175 | if ((__sub_expired(monc) && !monc->sub_sent) || | |
176 | monc->want_next_osdmap == 1) { | |
177 | struct ceph_msg *msg; | |
178 | struct ceph_mon_subscribe_item *i; | |
179 | void *p, *end; | |
180 | ||
4e7a5dcd | 181 | msg = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 96, 0, 0, NULL); |
ba75bb98 SW |
182 | if (!msg) |
183 | return; | |
184 | ||
185 | p = msg->front.iov_base; | |
186 | end = p + msg->front.iov_len; | |
187 | ||
188 | dout("__send_subscribe to 'mdsmap' %u+\n", | |
189 | (unsigned)monc->have_mdsmap); | |
190 | if (monc->want_next_osdmap) { | |
191 | dout("__send_subscribe to 'osdmap' %u\n", | |
192 | (unsigned)monc->have_osdmap); | |
4e7a5dcd | 193 | ceph_encode_32(&p, 3); |
ba75bb98 SW |
194 | ceph_encode_string(&p, end, "osdmap", 6); |
195 | i = p; | |
196 | i->have = cpu_to_le64(monc->have_osdmap); | |
197 | i->onetime = 1; | |
198 | p += sizeof(*i); | |
199 | monc->want_next_osdmap = 2; /* requested */ | |
200 | } else { | |
4e7a5dcd | 201 | ceph_encode_32(&p, 2); |
ba75bb98 SW |
202 | } |
203 | ceph_encode_string(&p, end, "mdsmap", 6); | |
204 | i = p; | |
205 | i->have = cpu_to_le64(monc->have_mdsmap); | |
206 | i->onetime = 0; | |
207 | p += sizeof(*i); | |
4e7a5dcd SW |
208 | ceph_encode_string(&p, end, "monmap", 6); |
209 | i = p; | |
210 | i->have = 0; | |
211 | i->onetime = 0; | |
212 | p += sizeof(*i); | |
ba75bb98 SW |
213 | |
214 | msg->front.iov_len = p - msg->front.iov_base; | |
215 | msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); | |
216 | ceph_con_send(monc->con, msg); | |
217 | ||
218 | monc->sub_sent = jiffies | 1; /* never 0 */ | |
219 | } | |
220 | } | |
221 | ||
222 | static void handle_subscribe_ack(struct ceph_mon_client *monc, | |
223 | struct ceph_msg *msg) | |
224 | { | |
225 | unsigned seconds; | |
07bd10fb SW |
226 | struct ceph_mon_subscribe_ack *h = msg->front.iov_base; |
227 | ||
228 | if (msg->front.iov_len < sizeof(*h)) | |
229 | goto bad; | |
230 | seconds = le32_to_cpu(h->duration); | |
ba75bb98 | 231 | |
ba75bb98 SW |
232 | mutex_lock(&monc->mutex); |
233 | if (monc->hunting) { | |
234 | pr_info("mon%d %s session established\n", | |
235 | monc->cur_mon, pr_addr(&monc->con->peer_addr.in_addr)); | |
236 | monc->hunting = false; | |
237 | } | |
238 | dout("handle_subscribe_ack after %d seconds\n", seconds); | |
0656d11b | 239 | monc->sub_renew_after = monc->sub_sent + (seconds >> 1)*HZ - 1; |
ba75bb98 SW |
240 | monc->sub_sent = 0; |
241 | mutex_unlock(&monc->mutex); | |
242 | return; | |
243 | bad: | |
244 | pr_err("got corrupt subscribe-ack msg\n"); | |
245 | } | |
246 | ||
247 | /* | |
248 | * Keep track of which maps we have | |
249 | */ | |
250 | int ceph_monc_got_mdsmap(struct ceph_mon_client *monc, u32 got) | |
251 | { | |
252 | mutex_lock(&monc->mutex); | |
253 | monc->have_mdsmap = got; | |
254 | mutex_unlock(&monc->mutex); | |
255 | return 0; | |
256 | } | |
257 | ||
258 | int ceph_monc_got_osdmap(struct ceph_mon_client *monc, u32 got) | |
259 | { | |
260 | mutex_lock(&monc->mutex); | |
261 | monc->have_osdmap = got; | |
262 | monc->want_next_osdmap = 0; | |
263 | mutex_unlock(&monc->mutex); | |
264 | return 0; | |
265 | } | |
266 | ||
267 | /* | |
268 | * Register interest in the next osdmap | |
269 | */ | |
270 | void ceph_monc_request_next_osdmap(struct ceph_mon_client *monc) | |
271 | { | |
272 | dout("request_next_osdmap have %u\n", monc->have_osdmap); | |
273 | mutex_lock(&monc->mutex); | |
274 | if (!monc->want_next_osdmap) | |
275 | monc->want_next_osdmap = 1; | |
276 | if (monc->want_next_osdmap < 2) | |
277 | __send_subscribe(monc); | |
278 | mutex_unlock(&monc->mutex); | |
279 | } | |
280 | ||
4e7a5dcd | 281 | /* |
50b885b9 | 282 | * |
4e7a5dcd SW |
283 | */ |
284 | int ceph_monc_open_session(struct ceph_mon_client *monc) | |
ba75bb98 SW |
285 | { |
286 | if (!monc->con) { | |
287 | monc->con = kmalloc(sizeof(*monc->con), GFP_KERNEL); | |
288 | if (!monc->con) | |
289 | return -ENOMEM; | |
290 | ceph_con_init(monc->client->msgr, monc->con); | |
291 | monc->con->private = monc; | |
292 | monc->con->ops = &mon_con_ops; | |
293 | } | |
294 | ||
295 | mutex_lock(&monc->mutex); | |
4e7a5dcd | 296 | __open_session(monc); |
ba75bb98 SW |
297 | __schedule_delayed(monc); |
298 | mutex_unlock(&monc->mutex); | |
299 | return 0; | |
300 | } | |
301 | ||
4e7a5dcd SW |
302 | /* |
303 | * The monitor responds with mount ack indicate mount success. The | |
304 | * included client ticket allows the client to talk to MDSs and OSDs. | |
305 | */ | |
0743304d SW |
306 | static void ceph_monc_handle_map(struct ceph_mon_client *monc, |
307 | struct ceph_msg *msg) | |
4e7a5dcd SW |
308 | { |
309 | struct ceph_client *client = monc->client; | |
310 | struct ceph_monmap *monmap = NULL, *old = monc->monmap; | |
311 | void *p, *end; | |
312 | ||
313 | mutex_lock(&monc->mutex); | |
314 | ||
315 | dout("handle_monmap\n"); | |
316 | p = msg->front.iov_base; | |
317 | end = p + msg->front.iov_len; | |
318 | ||
319 | monmap = ceph_monmap_decode(p, end); | |
320 | if (IS_ERR(monmap)) { | |
321 | pr_err("problem decoding monmap, %d\n", | |
322 | (int)PTR_ERR(monmap)); | |
323 | return; | |
324 | } | |
0743304d SW |
325 | |
326 | if (ceph_check_fsid(monc->client, &monmap->fsid) < 0) { | |
4e7a5dcd SW |
327 | kfree(monmap); |
328 | return; | |
329 | } | |
330 | ||
331 | client->monc.monmap = monmap; | |
4e7a5dcd SW |
332 | kfree(old); |
333 | ||
334 | mutex_unlock(&monc->mutex); | |
335 | wake_up(&client->mount_wq); | |
336 | } | |
337 | ||
ba75bb98 SW |
338 | /* |
339 | * statfs | |
340 | */ | |
341 | static void handle_statfs_reply(struct ceph_mon_client *monc, | |
342 | struct ceph_msg *msg) | |
343 | { | |
344 | struct ceph_mon_statfs_request *req; | |
345 | struct ceph_mon_statfs_reply *reply = msg->front.iov_base; | |
346 | u64 tid; | |
347 | ||
348 | if (msg->front.iov_len != sizeof(*reply)) | |
349 | goto bad; | |
350 | tid = le64_to_cpu(reply->tid); | |
351 | dout("handle_statfs_reply %p tid %llu\n", msg, tid); | |
352 | ||
353 | mutex_lock(&monc->mutex); | |
354 | req = radix_tree_lookup(&monc->statfs_request_tree, tid); | |
355 | if (req) { | |
356 | *req->buf = reply->st; | |
357 | req->result = 0; | |
358 | } | |
359 | mutex_unlock(&monc->mutex); | |
360 | if (req) | |
361 | complete(&req->completion); | |
362 | return; | |
363 | ||
364 | bad: | |
365 | pr_err("corrupt statfs reply, no tid\n"); | |
366 | } | |
367 | ||
368 | /* | |
369 | * (re)send a statfs request | |
370 | */ | |
371 | static int send_statfs(struct ceph_mon_client *monc, | |
372 | struct ceph_mon_statfs_request *req) | |
373 | { | |
374 | struct ceph_msg *msg; | |
375 | struct ceph_mon_statfs *h; | |
ba75bb98 SW |
376 | |
377 | dout("send_statfs tid %llu\n", req->tid); | |
ba75bb98 SW |
378 | msg = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), 0, 0, NULL); |
379 | if (IS_ERR(msg)) | |
380 | return PTR_ERR(msg); | |
381 | req->request = msg; | |
382 | h = msg->front.iov_base; | |
13e38c8a SW |
383 | h->monhdr.have_version = 0; |
384 | h->monhdr.session_mon = cpu_to_le16(-1); | |
385 | h->monhdr.session_mon_tid = 0; | |
ba75bb98 SW |
386 | h->fsid = monc->monmap->fsid; |
387 | h->tid = cpu_to_le64(req->tid); | |
388 | ceph_con_send(monc->con, msg); | |
389 | return 0; | |
390 | } | |
391 | ||
392 | /* | |
393 | * Do a synchronous statfs(). | |
394 | */ | |
395 | int ceph_monc_do_statfs(struct ceph_mon_client *monc, struct ceph_statfs *buf) | |
396 | { | |
397 | struct ceph_mon_statfs_request req; | |
398 | int err; | |
399 | ||
400 | req.buf = buf; | |
401 | init_completion(&req.completion); | |
402 | ||
403 | /* allocate memory for reply */ | |
404 | err = ceph_msgpool_resv(&monc->msgpool_statfs_reply, 1); | |
405 | if (err) | |
406 | return err; | |
407 | ||
408 | /* register request */ | |
409 | mutex_lock(&monc->mutex); | |
410 | req.tid = ++monc->last_tid; | |
411 | req.last_attempt = jiffies; | |
412 | req.delay = BASE_DELAY_INTERVAL; | |
413 | if (radix_tree_insert(&monc->statfs_request_tree, req.tid, &req) < 0) { | |
414 | mutex_unlock(&monc->mutex); | |
415 | pr_err("ENOMEM in do_statfs\n"); | |
416 | return -ENOMEM; | |
417 | } | |
418 | monc->num_statfs_requests++; | |
419 | mutex_unlock(&monc->mutex); | |
420 | ||
421 | /* send request and wait */ | |
422 | err = send_statfs(monc, &req); | |
423 | if (!err) | |
424 | err = wait_for_completion_interruptible(&req.completion); | |
425 | ||
426 | mutex_lock(&monc->mutex); | |
427 | radix_tree_delete(&monc->statfs_request_tree, req.tid); | |
428 | monc->num_statfs_requests--; | |
429 | ceph_msgpool_resv(&monc->msgpool_statfs_reply, -1); | |
430 | mutex_unlock(&monc->mutex); | |
431 | ||
432 | if (!err) | |
433 | err = req.result; | |
434 | return err; | |
435 | } | |
436 | ||
437 | /* | |
438 | * Resend pending statfs requests. | |
439 | */ | |
440 | static void __resend_statfs(struct ceph_mon_client *monc) | |
441 | { | |
442 | u64 next_tid = 0; | |
443 | int got; | |
444 | int did = 0; | |
445 | struct ceph_mon_statfs_request *req; | |
446 | ||
447 | while (1) { | |
448 | got = radix_tree_gang_lookup(&monc->statfs_request_tree, | |
449 | (void **)&req, | |
450 | next_tid, 1); | |
451 | if (got == 0) | |
452 | break; | |
453 | did++; | |
454 | next_tid = req->tid + 1; | |
455 | ||
456 | send_statfs(monc, req); | |
457 | } | |
458 | } | |
459 | ||
460 | /* | |
461 | * Delayed work. If we haven't mounted yet, retry. Otherwise, | |
462 | * renew/retry subscription as needed (in case it is timing out, or we | |
463 | * got an ENOMEM). And keep the monitor connection alive. | |
464 | */ | |
465 | static void delayed_work(struct work_struct *work) | |
466 | { | |
467 | struct ceph_mon_client *monc = | |
468 | container_of(work, struct ceph_mon_client, delayed_work.work); | |
469 | ||
470 | dout("monc delayed_work\n"); | |
471 | mutex_lock(&monc->mutex); | |
4e7a5dcd SW |
472 | if (monc->hunting) { |
473 | __close_session(monc); | |
474 | __open_session(monc); /* continue hunting */ | |
ba75bb98 | 475 | } else { |
4e7a5dcd SW |
476 | ceph_con_keepalive(monc->con); |
477 | if (monc->auth->ops->is_authenticated(monc->auth)) | |
478 | __send_subscribe(monc); | |
ba75bb98 | 479 | } |
ba75bb98 SW |
480 | __schedule_delayed(monc); |
481 | mutex_unlock(&monc->mutex); | |
482 | } | |
483 | ||
6b805185 SW |
484 | /* |
485 | * On startup, we build a temporary monmap populated with the IPs | |
486 | * provided by mount(2). | |
487 | */ | |
488 | static int build_initial_monmap(struct ceph_mon_client *monc) | |
489 | { | |
490 | struct ceph_mount_args *args = monc->client->mount_args; | |
491 | struct ceph_entity_addr *mon_addr = args->mon_addr; | |
492 | int num_mon = args->num_mon; | |
493 | int i; | |
494 | ||
495 | /* build initial monmap */ | |
496 | monc->monmap = kzalloc(sizeof(*monc->monmap) + | |
497 | num_mon*sizeof(monc->monmap->mon_inst[0]), | |
498 | GFP_KERNEL); | |
499 | if (!monc->monmap) | |
500 | return -ENOMEM; | |
501 | for (i = 0; i < num_mon; i++) { | |
502 | monc->monmap->mon_inst[i].addr = mon_addr[i]; | |
503 | monc->monmap->mon_inst[i].addr.erank = 0; | |
504 | monc->monmap->mon_inst[i].addr.nonce = 0; | |
505 | monc->monmap->mon_inst[i].name.type = | |
506 | CEPH_ENTITY_TYPE_MON; | |
507 | monc->monmap->mon_inst[i].name.num = cpu_to_le64(i); | |
508 | } | |
509 | monc->monmap->num_mon = num_mon; | |
4e7a5dcd | 510 | monc->have_fsid = false; |
6b805185 SW |
511 | |
512 | /* release addr memory */ | |
513 | kfree(args->mon_addr); | |
514 | args->mon_addr = NULL; | |
515 | args->num_mon = 0; | |
516 | return 0; | |
517 | } | |
518 | ||
ba75bb98 SW |
519 | int ceph_monc_init(struct ceph_mon_client *monc, struct ceph_client *cl) |
520 | { | |
521 | int err = 0; | |
522 | ||
523 | dout("init\n"); | |
524 | memset(monc, 0, sizeof(*monc)); | |
525 | monc->client = cl; | |
526 | monc->monmap = NULL; | |
527 | mutex_init(&monc->mutex); | |
528 | ||
6b805185 SW |
529 | err = build_initial_monmap(monc); |
530 | if (err) | |
531 | goto out; | |
532 | ||
ba75bb98 SW |
533 | monc->con = NULL; |
534 | ||
4e7a5dcd SW |
535 | /* authentication */ |
536 | monc->auth = ceph_auth_init(cl->mount_args->name, | |
537 | cl->mount_args->secret); | |
538 | if (IS_ERR(monc->auth)) | |
539 | return PTR_ERR(monc->auth); | |
540 | monc->auth->want_keys = | |
541 | CEPH_ENTITY_TYPE_AUTH | CEPH_ENTITY_TYPE_MON | | |
542 | CEPH_ENTITY_TYPE_OSD | CEPH_ENTITY_TYPE_MDS; | |
543 | ||
ba75bb98 | 544 | /* msg pools */ |
07bd10fb SW |
545 | err = ceph_msgpool_init(&monc->msgpool_subscribe_ack, |
546 | sizeof(struct ceph_mon_subscribe_ack), 1, false); | |
ba75bb98 | 547 | if (err < 0) |
4e7a5dcd | 548 | goto out_monmap; |
ba75bb98 SW |
549 | err = ceph_msgpool_init(&monc->msgpool_statfs_reply, |
550 | sizeof(struct ceph_mon_statfs_reply), 0, false); | |
551 | if (err < 0) | |
4e7a5dcd SW |
552 | goto out_pool1; |
553 | err = ceph_msgpool_init(&monc->msgpool_auth_reply, 4096, 1, false); | |
554 | if (err < 0) | |
555 | goto out_pool2; | |
556 | ||
557 | monc->m_auth = ceph_msg_new(CEPH_MSG_AUTH, 4096, 0, 0, NULL); | |
558 | if (IS_ERR(monc->m_auth)) { | |
559 | err = PTR_ERR(monc->m_auth); | |
560 | monc->m_auth = NULL; | |
561 | goto out_pool3; | |
562 | } | |
ba75bb98 SW |
563 | |
564 | monc->cur_mon = -1; | |
4e7a5dcd | 565 | monc->hunting = true; |
ba75bb98 SW |
566 | monc->sub_renew_after = jiffies; |
567 | monc->sub_sent = 0; | |
568 | ||
569 | INIT_DELAYED_WORK(&monc->delayed_work, delayed_work); | |
570 | INIT_RADIX_TREE(&monc->statfs_request_tree, GFP_NOFS); | |
571 | monc->num_statfs_requests = 0; | |
572 | monc->last_tid = 0; | |
573 | ||
574 | monc->have_mdsmap = 0; | |
575 | monc->have_osdmap = 0; | |
576 | monc->want_next_osdmap = 1; | |
4e7a5dcd SW |
577 | return 0; |
578 | ||
579 | out_pool3: | |
580 | ceph_msgpool_destroy(&monc->msgpool_auth_reply); | |
581 | out_pool2: | |
582 | ceph_msgpool_destroy(&monc->msgpool_subscribe_ack); | |
583 | out_pool1: | |
584 | ceph_msgpool_destroy(&monc->msgpool_statfs_reply); | |
585 | out_monmap: | |
586 | kfree(monc->monmap); | |
ba75bb98 SW |
587 | out: |
588 | return err; | |
589 | } | |
590 | ||
591 | void ceph_monc_stop(struct ceph_mon_client *monc) | |
592 | { | |
593 | dout("stop\n"); | |
594 | cancel_delayed_work_sync(&monc->delayed_work); | |
595 | ||
596 | mutex_lock(&monc->mutex); | |
597 | __close_session(monc); | |
598 | if (monc->con) { | |
599 | monc->con->private = NULL; | |
600 | monc->con->ops->put(monc->con); | |
601 | monc->con = NULL; | |
602 | } | |
603 | mutex_unlock(&monc->mutex); | |
604 | ||
4e7a5dcd SW |
605 | ceph_auth_destroy(monc->auth); |
606 | ||
607 | ceph_msg_put(monc->m_auth); | |
ba75bb98 SW |
608 | ceph_msgpool_destroy(&monc->msgpool_subscribe_ack); |
609 | ceph_msgpool_destroy(&monc->msgpool_statfs_reply); | |
4e7a5dcd | 610 | ceph_msgpool_destroy(&monc->msgpool_auth_reply); |
ba75bb98 SW |
611 | |
612 | kfree(monc->monmap); | |
613 | } | |
614 | ||
615 | ||
4e7a5dcd SW |
616 | static void handle_auth_reply(struct ceph_mon_client *monc, |
617 | struct ceph_msg *msg) | |
618 | { | |
619 | int ret; | |
620 | ||
621 | mutex_lock(&monc->mutex); | |
622 | ret = ceph_handle_auth_reply(monc->auth, msg->front.iov_base, | |
623 | msg->front.iov_len, | |
624 | monc->m_auth->front.iov_base, | |
625 | monc->m_auth->front_max); | |
626 | if (ret < 0) { | |
627 | monc->client->mount_err = ret; | |
628 | wake_up(&monc->client->mount_wq); | |
629 | } else if (ret > 0) { | |
630 | monc->m_auth->front.iov_len = ret; | |
631 | monc->m_auth->hdr.front_len = cpu_to_le32(ret); | |
632 | ceph_msg_get(monc->m_auth); /* keep our ref */ | |
633 | ceph_con_send(monc->con, monc->m_auth); | |
634 | } else if (monc->auth->ops->is_authenticated(monc->auth)) { | |
635 | dout("authenticated, starting session\n"); | |
0743304d SW |
636 | |
637 | monc->client->msgr->inst.name.type = CEPH_ENTITY_TYPE_CLIENT; | |
638 | monc->client->msgr->inst.name.num = monc->auth->global_id; | |
639 | ||
4e7a5dcd SW |
640 | __send_subscribe(monc); |
641 | __resend_statfs(monc); | |
642 | } | |
643 | mutex_unlock(&monc->mutex); | |
644 | } | |
645 | ||
ba75bb98 SW |
646 | /* |
647 | * handle incoming message | |
648 | */ | |
649 | static void dispatch(struct ceph_connection *con, struct ceph_msg *msg) | |
650 | { | |
651 | struct ceph_mon_client *monc = con->private; | |
652 | int type = le16_to_cpu(msg->hdr.type); | |
653 | ||
654 | if (!monc) | |
655 | return; | |
656 | ||
657 | switch (type) { | |
4e7a5dcd SW |
658 | case CEPH_MSG_AUTH_REPLY: |
659 | handle_auth_reply(monc, msg); | |
ba75bb98 SW |
660 | break; |
661 | ||
662 | case CEPH_MSG_MON_SUBSCRIBE_ACK: | |
663 | handle_subscribe_ack(monc, msg); | |
664 | break; | |
665 | ||
666 | case CEPH_MSG_STATFS_REPLY: | |
667 | handle_statfs_reply(monc, msg); | |
668 | break; | |
669 | ||
4e7a5dcd SW |
670 | case CEPH_MSG_MON_MAP: |
671 | ceph_monc_handle_map(monc, msg); | |
672 | break; | |
673 | ||
ba75bb98 SW |
674 | case CEPH_MSG_MDS_MAP: |
675 | ceph_mdsc_handle_map(&monc->client->mdsc, msg); | |
676 | break; | |
677 | ||
678 | case CEPH_MSG_OSD_MAP: | |
679 | ceph_osdc_handle_map(&monc->client->osdc, msg); | |
680 | break; | |
681 | ||
682 | default: | |
683 | pr_err("received unknown message type %d %s\n", type, | |
684 | ceph_msg_type_name(type)); | |
685 | } | |
686 | ceph_msg_put(msg); | |
687 | } | |
688 | ||
689 | /* | |
690 | * Allocate memory for incoming message | |
691 | */ | |
692 | static struct ceph_msg *mon_alloc_msg(struct ceph_connection *con, | |
693 | struct ceph_msg_header *hdr) | |
694 | { | |
695 | struct ceph_mon_client *monc = con->private; | |
696 | int type = le16_to_cpu(hdr->type); | |
8f3bc053 | 697 | int front = le32_to_cpu(hdr->front_len); |
ba75bb98 SW |
698 | |
699 | switch (type) { | |
ba75bb98 | 700 | case CEPH_MSG_MON_SUBSCRIBE_ACK: |
8f3bc053 | 701 | return ceph_msgpool_get(&monc->msgpool_subscribe_ack, front); |
ba75bb98 | 702 | case CEPH_MSG_STATFS_REPLY: |
8f3bc053 | 703 | return ceph_msgpool_get(&monc->msgpool_statfs_reply, front); |
4e7a5dcd SW |
704 | case CEPH_MSG_AUTH_REPLY: |
705 | return ceph_msgpool_get(&monc->msgpool_auth_reply, front); | |
ba75bb98 SW |
706 | } |
707 | return ceph_alloc_msg(con, hdr); | |
708 | } | |
709 | ||
710 | /* | |
711 | * If the monitor connection resets, pick a new monitor and resubmit | |
712 | * any pending requests. | |
713 | */ | |
714 | static void mon_fault(struct ceph_connection *con) | |
715 | { | |
716 | struct ceph_mon_client *monc = con->private; | |
717 | ||
718 | if (!monc) | |
719 | return; | |
720 | ||
721 | dout("mon_fault\n"); | |
722 | mutex_lock(&monc->mutex); | |
723 | if (!con->private) | |
724 | goto out; | |
725 | ||
726 | if (monc->con && !monc->hunting) | |
727 | pr_info("mon%d %s session lost, " | |
728 | "hunting for new mon\n", monc->cur_mon, | |
729 | pr_addr(&monc->con->peer_addr.in_addr)); | |
730 | ||
731 | __close_session(monc); | |
732 | if (!monc->hunting) { | |
733 | /* start hunting */ | |
734 | monc->hunting = true; | |
4e7a5dcd | 735 | __open_session(monc); |
ba75bb98 SW |
736 | } else { |
737 | /* already hunting, let's wait a bit */ | |
738 | __schedule_delayed(monc); | |
739 | } | |
740 | out: | |
741 | mutex_unlock(&monc->mutex); | |
742 | } | |
743 | ||
744 | const static struct ceph_connection_operations mon_con_ops = { | |
745 | .get = ceph_con_get, | |
746 | .put = ceph_con_put, | |
747 | .dispatch = dispatch, | |
748 | .fault = mon_fault, | |
749 | .alloc_msg = mon_alloc_msg, | |
750 | .alloc_middle = ceph_alloc_middle, | |
751 | }; |