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