1 #include "ceph_debug.h"
3 #include <linux/types.h>
4 #include <linux/slab.h>
5 #include <linux/random.h>
6 #include <linux/sched.h>
8 #include "mon_client.h"
14 * Interact with Ceph monitor cluster. Handle requests for new map
15 * versions, and periodically resend as needed. Also implement
16 * statfs() and umount().
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.
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.
31 static const struct ceph_connection_operations mon_con_ops
;
33 static int __validate_auth(struct ceph_mon_client
*monc
);
36 * Decode a monmap blob (e.g., during mount).
38 struct ceph_monmap
*ceph_monmap_decode(void *p
, void *end
)
40 struct ceph_monmap
*m
= NULL
;
42 struct ceph_fsid fsid
;
47 ceph_decode_32_safe(&p
, end
, len
, bad
);
48 ceph_decode_need(&p
, end
, len
, bad
);
50 dout("monmap_decode %p %p len %d\n", p
, end
, (int)(end
-p
));
52 ceph_decode_16_safe(&p
, end
, version
, bad
);
54 ceph_decode_need(&p
, end
, sizeof(fsid
) + 2*sizeof(u32
), bad
);
55 ceph_decode_copy(&p
, &fsid
, sizeof(fsid
));
56 epoch
= ceph_decode_32(&p
);
58 num_mon
= ceph_decode_32(&p
);
59 ceph_decode_need(&p
, end
, num_mon
*sizeof(m
->mon_inst
[0]), bad
);
61 if (num_mon
>= CEPH_MAX_MON
)
63 m
= kmalloc(sizeof(*m
) + sizeof(m
->mon_inst
[0])*num_mon
, GFP_NOFS
);
65 return ERR_PTR(-ENOMEM
);
69 ceph_decode_copy(&p
, m
->mon_inst
, num_mon
*sizeof(m
->mon_inst
[0]));
70 for (i
= 0; i
< num_mon
; i
++)
71 ceph_decode_addr(&m
->mon_inst
[i
].addr
);
73 dout("monmap_decode epoch %d, num_mon %d\n", m
->epoch
,
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
));
81 dout("monmap_decode failed with %d\n", err
);
87 * return true if *addr is included in the monmap.
89 int ceph_monmap_contains(struct ceph_monmap
*m
, struct ceph_entity_addr
*addr
)
93 for (i
= 0; i
< m
->num_mon
; i
++)
94 if (memcmp(addr
, &m
->mon_inst
[i
].addr
, sizeof(*addr
)) == 0)
100 * Send an auth request.
102 static void __send_prepared_auth_request(struct ceph_mon_client
*monc
, int len
)
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_con_revoke(monc
->con
, monc
->m_auth
);
108 ceph_msg_get(monc
->m_auth
); /* keep our ref */
109 ceph_con_send(monc
->con
, monc
->m_auth
);
113 * Close monitor session, if any.
115 static void __close_session(struct ceph_mon_client
*monc
)
118 dout("__close_session closing mon%d\n", monc
->cur_mon
);
119 ceph_con_revoke(monc
->con
, monc
->m_auth
);
120 ceph_con_close(monc
->con
);
122 monc
->pending_auth
= 0;
123 ceph_auth_reset(monc
->auth
);
128 * Open a session with a (new) monitor.
130 static int __open_session(struct ceph_mon_client
*monc
)
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
);
141 monc
->sub_renew_after
= jiffies
; /* i.e., expired */
142 monc
->want_next_osdmap
= !!monc
->want_next_osdmap
;
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
);
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
);
154 __send_prepared_auth_request(monc
, ret
);
156 dout("open_session mon%d already open\n", monc
->cur_mon
);
161 static bool __sub_expired(struct ceph_mon_client
*monc
)
163 return time_after_eq(jiffies
, monc
->sub_renew_after
);
167 * Reschedule delayed work timer.
169 static void __schedule_delayed(struct ceph_mon_client
*monc
)
173 if (monc
->cur_mon
< 0 || __sub_expired(monc
))
177 dout("__schedule_delayed after %u\n", delay
);
178 schedule_delayed_work(&monc
->delayed_work
, delay
);
182 * Send subscribe request for mdsmap and/or osdmap.
184 static void __send_subscribe(struct ceph_mon_client
*monc
)
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
= monc
->m_subscribe
;
192 struct ceph_mon_subscribe_item
*i
;
195 p
= msg
->front
.iov_base
;
196 end
= p
+ msg
->front_max
;
198 dout("__send_subscribe to 'mdsmap' %u+\n",
199 (unsigned)monc
->have_mdsmap
);
200 if (monc
->want_next_osdmap
) {
201 dout("__send_subscribe to 'osdmap' %u\n",
202 (unsigned)monc
->have_osdmap
);
203 ceph_encode_32(&p
, 3);
204 ceph_encode_string(&p
, end
, "osdmap", 6);
206 i
->have
= cpu_to_le64(monc
->have_osdmap
);
209 monc
->want_next_osdmap
= 2; /* requested */
211 ceph_encode_32(&p
, 2);
213 ceph_encode_string(&p
, end
, "mdsmap", 6);
215 i
->have
= cpu_to_le64(monc
->have_mdsmap
);
218 ceph_encode_string(&p
, end
, "monmap", 6);
224 msg
->front
.iov_len
= p
- msg
->front
.iov_base
;
225 msg
->hdr
.front_len
= cpu_to_le32(msg
->front
.iov_len
);
226 ceph_con_revoke(monc
->con
, msg
);
227 ceph_con_send(monc
->con
, ceph_msg_get(msg
));
229 monc
->sub_sent
= jiffies
| 1; /* never 0 */
233 static void handle_subscribe_ack(struct ceph_mon_client
*monc
,
234 struct ceph_msg
*msg
)
237 struct ceph_mon_subscribe_ack
*h
= msg
->front
.iov_base
;
239 if (msg
->front
.iov_len
< sizeof(*h
))
241 seconds
= le32_to_cpu(h
->duration
);
243 mutex_lock(&monc
->mutex
);
245 pr_info("mon%d %s session established\n",
246 monc
->cur_mon
, pr_addr(&monc
->con
->peer_addr
.in_addr
));
247 monc
->hunting
= false;
249 dout("handle_subscribe_ack after %d seconds\n", seconds
);
250 monc
->sub_renew_after
= monc
->sub_sent
+ (seconds
>> 1)*HZ
- 1;
252 mutex_unlock(&monc
->mutex
);
255 pr_err("got corrupt subscribe-ack msg\n");
260 * Keep track of which maps we have
262 int ceph_monc_got_mdsmap(struct ceph_mon_client
*monc
, u32 got
)
264 mutex_lock(&monc
->mutex
);
265 monc
->have_mdsmap
= got
;
266 mutex_unlock(&monc
->mutex
);
270 int ceph_monc_got_osdmap(struct ceph_mon_client
*monc
, u32 got
)
272 mutex_lock(&monc
->mutex
);
273 monc
->have_osdmap
= got
;
274 monc
->want_next_osdmap
= 0;
275 mutex_unlock(&monc
->mutex
);
280 * Register interest in the next osdmap
282 void ceph_monc_request_next_osdmap(struct ceph_mon_client
*monc
)
284 dout("request_next_osdmap have %u\n", monc
->have_osdmap
);
285 mutex_lock(&monc
->mutex
);
286 if (!monc
->want_next_osdmap
)
287 monc
->want_next_osdmap
= 1;
288 if (monc
->want_next_osdmap
< 2)
289 __send_subscribe(monc
);
290 mutex_unlock(&monc
->mutex
);
296 int ceph_monc_open_session(struct ceph_mon_client
*monc
)
299 monc
->con
= kmalloc(sizeof(*monc
->con
), GFP_KERNEL
);
302 ceph_con_init(monc
->client
->msgr
, monc
->con
);
303 monc
->con
->private = monc
;
304 monc
->con
->ops
= &mon_con_ops
;
307 mutex_lock(&monc
->mutex
);
308 __open_session(monc
);
309 __schedule_delayed(monc
);
310 mutex_unlock(&monc
->mutex
);
315 * The monitor responds with mount ack indicate mount success. The
316 * included client ticket allows the client to talk to MDSs and OSDs.
318 static void ceph_monc_handle_map(struct ceph_mon_client
*monc
,
319 struct ceph_msg
*msg
)
321 struct ceph_client
*client
= monc
->client
;
322 struct ceph_monmap
*monmap
= NULL
, *old
= monc
->monmap
;
325 mutex_lock(&monc
->mutex
);
327 dout("handle_monmap\n");
328 p
= msg
->front
.iov_base
;
329 end
= p
+ msg
->front
.iov_len
;
331 monmap
= ceph_monmap_decode(p
, end
);
332 if (IS_ERR(monmap
)) {
333 pr_err("problem decoding monmap, %d\n",
334 (int)PTR_ERR(monmap
));
338 if (ceph_check_fsid(monc
->client
, &monmap
->fsid
) < 0) {
343 client
->monc
.monmap
= monmap
;
347 mutex_unlock(&monc
->mutex
);
348 wake_up_all(&client
->auth_wq
);
352 * generic requests (e.g., statfs, poolop)
354 static struct ceph_mon_generic_request
*__lookup_generic_req(
355 struct ceph_mon_client
*monc
, u64 tid
)
357 struct ceph_mon_generic_request
*req
;
358 struct rb_node
*n
= monc
->generic_request_tree
.rb_node
;
361 req
= rb_entry(n
, struct ceph_mon_generic_request
, node
);
364 else if (tid
> req
->tid
)
372 static void __insert_generic_request(struct ceph_mon_client
*monc
,
373 struct ceph_mon_generic_request
*new)
375 struct rb_node
**p
= &monc
->generic_request_tree
.rb_node
;
376 struct rb_node
*parent
= NULL
;
377 struct ceph_mon_generic_request
*req
= NULL
;
381 req
= rb_entry(parent
, struct ceph_mon_generic_request
, node
);
382 if (new->tid
< req
->tid
)
384 else if (new->tid
> req
->tid
)
390 rb_link_node(&new->node
, parent
, p
);
391 rb_insert_color(&new->node
, &monc
->generic_request_tree
);
394 static void release_generic_request(struct kref
*kref
)
396 struct ceph_mon_generic_request
*req
=
397 container_of(kref
, struct ceph_mon_generic_request
, kref
);
400 ceph_msg_put(req
->reply
);
402 ceph_msg_put(req
->request
);
407 static void put_generic_request(struct ceph_mon_generic_request
*req
)
409 kref_put(&req
->kref
, release_generic_request
);
412 static void get_generic_request(struct ceph_mon_generic_request
*req
)
414 kref_get(&req
->kref
);
417 static struct ceph_msg
*get_generic_reply(struct ceph_connection
*con
,
418 struct ceph_msg_header
*hdr
,
421 struct ceph_mon_client
*monc
= con
->private;
422 struct ceph_mon_generic_request
*req
;
423 u64 tid
= le64_to_cpu(hdr
->tid
);
426 mutex_lock(&monc
->mutex
);
427 req
= __lookup_generic_req(monc
, tid
);
429 dout("get_generic_reply %lld dne\n", tid
);
433 dout("get_generic_reply %lld got %p\n", tid
, req
->reply
);
434 m
= ceph_msg_get(req
->reply
);
436 * we don't need to track the connection reading into
437 * this reply because we only have one open connection
441 mutex_unlock(&monc
->mutex
);
445 static int do_generic_request(struct ceph_mon_client
*monc
,
446 struct ceph_mon_generic_request
*req
)
450 /* register request */
451 mutex_lock(&monc
->mutex
);
452 req
->tid
= ++monc
->last_tid
;
453 req
->request
->hdr
.tid
= cpu_to_le64(req
->tid
);
454 __insert_generic_request(monc
, req
);
455 monc
->num_generic_requests
++;
456 ceph_con_send(monc
->con
, ceph_msg_get(req
->request
));
457 mutex_unlock(&monc
->mutex
);
459 err
= wait_for_completion_interruptible(&req
->completion
);
461 mutex_lock(&monc
->mutex
);
462 rb_erase(&req
->node
, &monc
->generic_request_tree
);
463 monc
->num_generic_requests
--;
464 mutex_unlock(&monc
->mutex
);
474 static void handle_statfs_reply(struct ceph_mon_client
*monc
,
475 struct ceph_msg
*msg
)
477 struct ceph_mon_generic_request
*req
;
478 struct ceph_mon_statfs_reply
*reply
= msg
->front
.iov_base
;
479 u64 tid
= le64_to_cpu(msg
->hdr
.tid
);
481 if (msg
->front
.iov_len
!= sizeof(*reply
))
483 dout("handle_statfs_reply %p tid %llu\n", msg
, tid
);
485 mutex_lock(&monc
->mutex
);
486 req
= __lookup_generic_req(monc
, tid
);
488 *(struct ceph_statfs
*)req
->buf
= reply
->st
;
490 get_generic_request(req
);
492 mutex_unlock(&monc
->mutex
);
494 complete_all(&req
->completion
);
495 put_generic_request(req
);
500 pr_err("corrupt generic reply, tid %llu\n", tid
);
505 * Do a synchronous statfs().
507 int ceph_monc_do_statfs(struct ceph_mon_client
*monc
, struct ceph_statfs
*buf
)
509 struct ceph_mon_generic_request
*req
;
510 struct ceph_mon_statfs
*h
;
513 req
= kzalloc(sizeof(*req
), GFP_NOFS
);
517 kref_init(&req
->kref
);
519 req
->buf_len
= sizeof(*buf
);
520 init_completion(&req
->completion
);
523 req
->request
= ceph_msg_new(CEPH_MSG_STATFS
, sizeof(*h
), GFP_NOFS
);
526 req
->reply
= ceph_msg_new(CEPH_MSG_STATFS_REPLY
, 1024, GFP_NOFS
);
530 /* fill out request */
531 h
= req
->request
->front
.iov_base
;
532 h
->monhdr
.have_version
= 0;
533 h
->monhdr
.session_mon
= cpu_to_le16(-1);
534 h
->monhdr
.session_mon_tid
= 0;
535 h
->fsid
= monc
->monmap
->fsid
;
537 err
= do_generic_request(monc
, req
);
540 kref_put(&req
->kref
, release_generic_request
);
547 static int get_poolop_reply_buf(const char *src
, size_t src_len
,
548 char *dst
, size_t dst_len
)
552 if (src_len
!= sizeof(u32
) + dst_len
)
555 buf_len
= le32_to_cpu(*(u32
*)src
);
556 if (buf_len
!= dst_len
)
559 memcpy(dst
, src
+ sizeof(u32
), dst_len
);
563 static void handle_poolop_reply(struct ceph_mon_client
*monc
,
564 struct ceph_msg
*msg
)
566 struct ceph_mon_generic_request
*req
;
567 struct ceph_mon_poolop_reply
*reply
= msg
->front
.iov_base
;
568 u64 tid
= le64_to_cpu(msg
->hdr
.tid
);
570 if (msg
->front
.iov_len
< sizeof(*reply
))
572 dout("handle_poolop_reply %p tid %llu\n", msg
, tid
);
574 mutex_lock(&monc
->mutex
);
575 req
= __lookup_generic_req(monc
, tid
);
578 get_poolop_reply_buf(msg
->front
.iov_base
+ sizeof(*reply
),
579 msg
->front
.iov_len
- sizeof(*reply
),
580 req
->buf
, req
->buf_len
) < 0) {
581 mutex_unlock(&monc
->mutex
);
584 req
->result
= le32_to_cpu(reply
->reply_code
);
585 get_generic_request(req
);
587 mutex_unlock(&monc
->mutex
);
589 complete(&req
->completion
);
590 put_generic_request(req
);
595 pr_err("corrupt generic reply, tid %llu\n", tid
);
600 * Do a synchronous pool op.
602 int ceph_monc_do_poolop(struct ceph_mon_client
*monc
, u32 op
,
603 u32 pool
, u64 snapid
,
606 struct ceph_mon_generic_request
*req
;
607 struct ceph_mon_poolop
*h
;
610 req
= kzalloc(sizeof(*req
), GFP_NOFS
);
614 kref_init(&req
->kref
);
617 init_completion(&req
->completion
);
620 req
->request
= ceph_msg_new(CEPH_MSG_POOLOP
, sizeof(*h
), GFP_NOFS
);
623 req
->reply
= ceph_msg_new(CEPH_MSG_POOLOP_REPLY
, 1024, GFP_NOFS
);
627 /* fill out request */
628 req
->request
->hdr
.version
= cpu_to_le16(2);
629 h
= req
->request
->front
.iov_base
;
630 h
->monhdr
.have_version
= 0;
631 h
->monhdr
.session_mon
= cpu_to_le16(-1);
632 h
->monhdr
.session_mon_tid
= 0;
633 h
->fsid
= monc
->monmap
->fsid
;
634 h
->pool
= cpu_to_le32(pool
);
635 h
->op
= cpu_to_le32(op
);
637 h
->snapid
= cpu_to_le64(snapid
);
640 err
= do_generic_request(monc
, req
);
643 kref_put(&req
->kref
, release_generic_request
);
647 int ceph_monc_create_snapid(struct ceph_mon_client
*monc
,
648 u32 pool
, u64
*snapid
)
650 return ceph_monc_do_poolop(monc
, POOL_OP_CREATE_UNMANAGED_SNAP
,
651 pool
, 0, (char *)snapid
, sizeof(*snapid
));
655 int ceph_monc_delete_snapid(struct ceph_mon_client
*monc
,
656 u32 pool
, u64 snapid
)
658 return ceph_monc_do_poolop(monc
, POOL_OP_CREATE_UNMANAGED_SNAP
,
664 * Resend pending generic requests.
666 static void __resend_generic_request(struct ceph_mon_client
*monc
)
668 struct ceph_mon_generic_request
*req
;
671 for (p
= rb_first(&monc
->generic_request_tree
); p
; p
= rb_next(p
)) {
672 req
= rb_entry(p
, struct ceph_mon_generic_request
, node
);
673 ceph_con_revoke(monc
->con
, req
->request
);
674 ceph_con_send(monc
->con
, ceph_msg_get(req
->request
));
679 * Delayed work. If we haven't mounted yet, retry. Otherwise,
680 * renew/retry subscription as needed (in case it is timing out, or we
681 * got an ENOMEM). And keep the monitor connection alive.
683 static void delayed_work(struct work_struct
*work
)
685 struct ceph_mon_client
*monc
=
686 container_of(work
, struct ceph_mon_client
, delayed_work
.work
);
688 dout("monc delayed_work\n");
689 mutex_lock(&monc
->mutex
);
691 __close_session(monc
);
692 __open_session(monc
); /* continue hunting */
694 ceph_con_keepalive(monc
->con
);
696 __validate_auth(monc
);
698 if (monc
->auth
->ops
->is_authenticated(monc
->auth
))
699 __send_subscribe(monc
);
701 __schedule_delayed(monc
);
702 mutex_unlock(&monc
->mutex
);
706 * On startup, we build a temporary monmap populated with the IPs
707 * provided by mount(2).
709 static int build_initial_monmap(struct ceph_mon_client
*monc
)
711 struct ceph_mount_args
*args
= monc
->client
->mount_args
;
712 struct ceph_entity_addr
*mon_addr
= args
->mon_addr
;
713 int num_mon
= args
->num_mon
;
716 /* build initial monmap */
717 monc
->monmap
= kzalloc(sizeof(*monc
->monmap
) +
718 num_mon
*sizeof(monc
->monmap
->mon_inst
[0]),
722 for (i
= 0; i
< num_mon
; i
++) {
723 monc
->monmap
->mon_inst
[i
].addr
= mon_addr
[i
];
724 monc
->monmap
->mon_inst
[i
].addr
.nonce
= 0;
725 monc
->monmap
->mon_inst
[i
].name
.type
=
726 CEPH_ENTITY_TYPE_MON
;
727 monc
->monmap
->mon_inst
[i
].name
.num
= cpu_to_le64(i
);
729 monc
->monmap
->num_mon
= num_mon
;
730 monc
->have_fsid
= false;
732 /* release addr memory */
733 kfree(args
->mon_addr
);
734 args
->mon_addr
= NULL
;
739 int ceph_monc_init(struct ceph_mon_client
*monc
, struct ceph_client
*cl
)
744 memset(monc
, 0, sizeof(*monc
));
747 mutex_init(&monc
->mutex
);
749 err
= build_initial_monmap(monc
);
756 monc
->auth
= ceph_auth_init(cl
->mount_args
->name
,
757 cl
->mount_args
->secret
);
758 if (IS_ERR(monc
->auth
))
759 return PTR_ERR(monc
->auth
);
760 monc
->auth
->want_keys
=
761 CEPH_ENTITY_TYPE_AUTH
| CEPH_ENTITY_TYPE_MON
|
762 CEPH_ENTITY_TYPE_OSD
| CEPH_ENTITY_TYPE_MDS
;
766 monc
->m_subscribe_ack
= ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE_ACK
,
767 sizeof(struct ceph_mon_subscribe_ack
),
769 if (!monc
->m_subscribe_ack
)
772 monc
->m_subscribe
= ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE
, 96, GFP_NOFS
);
773 if (!monc
->m_subscribe
)
774 goto out_subscribe_ack
;
776 monc
->m_auth_reply
= ceph_msg_new(CEPH_MSG_AUTH_REPLY
, 4096, GFP_NOFS
);
777 if (!monc
->m_auth_reply
)
780 monc
->m_auth
= ceph_msg_new(CEPH_MSG_AUTH
, 4096, GFP_NOFS
);
781 monc
->pending_auth
= 0;
786 monc
->hunting
= true;
787 monc
->sub_renew_after
= jiffies
;
790 INIT_DELAYED_WORK(&monc
->delayed_work
, delayed_work
);
791 monc
->generic_request_tree
= RB_ROOT
;
792 monc
->num_generic_requests
= 0;
795 monc
->have_mdsmap
= 0;
796 monc
->have_osdmap
= 0;
797 monc
->want_next_osdmap
= 1;
801 ceph_msg_put(monc
->m_auth_reply
);
803 ceph_msg_put(monc
->m_subscribe
);
805 ceph_msg_put(monc
->m_subscribe_ack
);
812 void ceph_monc_stop(struct ceph_mon_client
*monc
)
815 cancel_delayed_work_sync(&monc
->delayed_work
);
817 mutex_lock(&monc
->mutex
);
818 __close_session(monc
);
820 monc
->con
->private = NULL
;
821 monc
->con
->ops
->put(monc
->con
);
824 mutex_unlock(&monc
->mutex
);
826 ceph_auth_destroy(monc
->auth
);
828 ceph_msg_put(monc
->m_auth
);
829 ceph_msg_put(monc
->m_auth_reply
);
830 ceph_msg_put(monc
->m_subscribe
);
831 ceph_msg_put(monc
->m_subscribe_ack
);
836 static void handle_auth_reply(struct ceph_mon_client
*monc
,
837 struct ceph_msg
*msg
)
842 mutex_lock(&monc
->mutex
);
844 was_auth
= monc
->auth
->ops
->is_authenticated(monc
->auth
);
845 monc
->pending_auth
= 0;
846 ret
= ceph_handle_auth_reply(monc
->auth
, msg
->front
.iov_base
,
848 monc
->m_auth
->front
.iov_base
,
849 monc
->m_auth
->front_max
);
851 monc
->client
->auth_err
= ret
;
852 wake_up_all(&monc
->client
->auth_wq
);
853 } else if (ret
> 0) {
854 __send_prepared_auth_request(monc
, ret
);
855 } else if (!was_auth
&& monc
->auth
->ops
->is_authenticated(monc
->auth
)) {
856 dout("authenticated, starting session\n");
858 monc
->client
->msgr
->inst
.name
.type
= CEPH_ENTITY_TYPE_CLIENT
;
859 monc
->client
->msgr
->inst
.name
.num
=
860 cpu_to_le64(monc
->auth
->global_id
);
862 __send_subscribe(monc
);
863 __resend_generic_request(monc
);
865 mutex_unlock(&monc
->mutex
);
868 static int __validate_auth(struct ceph_mon_client
*monc
)
872 if (monc
->pending_auth
)
875 ret
= ceph_build_auth(monc
->auth
, monc
->m_auth
->front
.iov_base
,
876 monc
->m_auth
->front_max
);
878 return ret
; /* either an error, or no need to authenticate */
879 __send_prepared_auth_request(monc
, ret
);
883 int ceph_monc_validate_auth(struct ceph_mon_client
*monc
)
887 mutex_lock(&monc
->mutex
);
888 ret
= __validate_auth(monc
);
889 mutex_unlock(&monc
->mutex
);
894 * handle incoming message
896 static void dispatch(struct ceph_connection
*con
, struct ceph_msg
*msg
)
898 struct ceph_mon_client
*monc
= con
->private;
899 int type
= le16_to_cpu(msg
->hdr
.type
);
905 case CEPH_MSG_AUTH_REPLY
:
906 handle_auth_reply(monc
, msg
);
909 case CEPH_MSG_MON_SUBSCRIBE_ACK
:
910 handle_subscribe_ack(monc
, msg
);
913 case CEPH_MSG_STATFS_REPLY
:
914 handle_statfs_reply(monc
, msg
);
917 case CEPH_MSG_POOLOP_REPLY
:
918 handle_poolop_reply(monc
, msg
);
921 case CEPH_MSG_MON_MAP
:
922 ceph_monc_handle_map(monc
, msg
);
925 case CEPH_MSG_MDS_MAP
:
926 ceph_mdsc_handle_map(&monc
->client
->mdsc
, msg
);
929 case CEPH_MSG_OSD_MAP
:
930 ceph_osdc_handle_map(&monc
->client
->osdc
, msg
);
934 pr_err("received unknown message type %d %s\n", type
,
935 ceph_msg_type_name(type
));
941 * Allocate memory for incoming message
943 static struct ceph_msg
*mon_alloc_msg(struct ceph_connection
*con
,
944 struct ceph_msg_header
*hdr
,
947 struct ceph_mon_client
*monc
= con
->private;
948 int type
= le16_to_cpu(hdr
->type
);
949 int front_len
= le32_to_cpu(hdr
->front_len
);
950 struct ceph_msg
*m
= NULL
;
955 case CEPH_MSG_MON_SUBSCRIBE_ACK
:
956 m
= ceph_msg_get(monc
->m_subscribe_ack
);
958 case CEPH_MSG_POOLOP_REPLY
:
959 case CEPH_MSG_STATFS_REPLY
:
960 return get_generic_reply(con
, hdr
, skip
);
961 case CEPH_MSG_AUTH_REPLY
:
962 m
= ceph_msg_get(monc
->m_auth_reply
);
964 case CEPH_MSG_MON_MAP
:
965 case CEPH_MSG_MDS_MAP
:
966 case CEPH_MSG_OSD_MAP
:
967 m
= ceph_msg_new(type
, front_len
, GFP_NOFS
);
972 pr_info("alloc_msg unknown type %d\n", type
);
979 * If the monitor connection resets, pick a new monitor and resubmit
980 * any pending requests.
982 static void mon_fault(struct ceph_connection
*con
)
984 struct ceph_mon_client
*monc
= con
->private;
990 mutex_lock(&monc
->mutex
);
994 if (monc
->con
&& !monc
->hunting
)
995 pr_info("mon%d %s session lost, "
996 "hunting for new mon\n", monc
->cur_mon
,
997 pr_addr(&monc
->con
->peer_addr
.in_addr
));
999 __close_session(monc
);
1000 if (!monc
->hunting
) {
1002 monc
->hunting
= true;
1003 __open_session(monc
);
1005 /* already hunting, let's wait a bit */
1006 __schedule_delayed(monc
);
1009 mutex_unlock(&monc
->mutex
);
1012 static const struct ceph_connection_operations mon_con_ops
= {
1013 .get
= ceph_con_get
,
1014 .put
= ceph_con_put
,
1015 .dispatch
= dispatch
,
1017 .alloc_msg
= mon_alloc_msg
,