ceph: use connection mutex to protect read and write stages
[sfrench/cifs-2.6.git] / fs / ceph / messenger.h
1 #ifndef __FS_CEPH_MESSENGER_H
2 #define __FS_CEPH_MESSENGER_H
3
4 #include <linux/kref.h>
5 #include <linux/mutex.h>
6 #include <linux/net.h>
7 #include <linux/radix-tree.h>
8 #include <linux/uio.h>
9 #include <linux/version.h>
10 #include <linux/workqueue.h>
11
12 #include "types.h"
13 #include "buffer.h"
14
15 struct ceph_msg;
16 struct ceph_connection;
17
18 extern struct workqueue_struct *ceph_msgr_wq;       /* receive work queue */
19
20 /*
21  * Ceph defines these callbacks for handling connection events.
22  */
23 struct ceph_connection_operations {
24         struct ceph_connection *(*get)(struct ceph_connection *);
25         void (*put)(struct ceph_connection *);
26
27         /* handle an incoming message. */
28         void (*dispatch) (struct ceph_connection *con, struct ceph_msg *m);
29
30         /* authorize an outgoing connection */
31         int (*get_authorizer) (struct ceph_connection *con,
32                                void **buf, int *len, int *proto,
33                                void **reply_buf, int *reply_len, int force_new);
34         int (*verify_authorizer_reply) (struct ceph_connection *con, int len);
35
36         /* protocol version mismatch */
37         void (*bad_proto) (struct ceph_connection *con);
38
39         /* there was some error on the socket (disconnect, whatever) */
40         void (*fault) (struct ceph_connection *con);
41
42         /* a remote host as terminated a message exchange session, and messages
43          * we sent (or they tried to send us) may be lost. */
44         void (*peer_reset) (struct ceph_connection *con);
45
46         struct ceph_msg * (*alloc_msg) (struct ceph_connection *con,
47                                         struct ceph_msg_header *hdr);
48         int (*alloc_middle) (struct ceph_connection *con,
49                              struct ceph_msg *msg);
50         /* an incoming message has a data payload; tell me what pages I
51          * should read the data into. */
52         int (*prepare_pages) (struct ceph_connection *con, struct ceph_msg *m,
53                               int want);
54 };
55
56 extern const char *ceph_name_type_str(int t);
57
58 /* use format string %s%d */
59 #define ENTITY_NAME(n) ceph_name_type_str((n).type), le64_to_cpu((n).num)
60
61 struct ceph_messenger {
62         struct ceph_entity_inst inst;    /* my name+address */
63         struct ceph_entity_addr my_enc_addr;
64         struct page *zero_page;          /* used in certain error cases */
65
66         bool nocrc;
67
68         /*
69          * the global_seq counts connections i (attempt to) initiate
70          * in order to disambiguate certain connect race conditions.
71          */
72         u32 global_seq;
73         spinlock_t global_seq_lock;
74 };
75
76 /*
77  * a single message.  it contains a header (src, dest, message type, etc.),
78  * footer (crc values, mainly), a "front" message body, and possibly a
79  * data payload (stored in some number of pages).
80  */
81 struct ceph_msg {
82         struct ceph_msg_header hdr;     /* header */
83         struct ceph_msg_footer footer;  /* footer */
84         struct kvec front;              /* unaligned blobs of message */
85         struct ceph_buffer *middle;
86         struct page **pages;            /* data payload.  NOT OWNER. */
87         unsigned nr_pages;              /* size of page array */
88         struct list_head list_head;
89         struct kref kref;
90         bool front_is_vmalloc;
91         bool more_to_follow;
92         int front_max;
93
94         struct ceph_msgpool *pool;
95 };
96
97 struct ceph_msg_pos {
98         int page, page_pos;  /* which page; offset in page */
99         int data_pos;        /* offset in data payload */
100         int did_page_crc;    /* true if we've calculated crc for current page */
101 };
102
103 /* ceph connection fault delay defaults, for exponential backoff */
104 #define BASE_DELAY_INTERVAL     (HZ/2)
105 #define MAX_DELAY_INTERVAL      (5 * 60 * HZ)
106
107 /*
108  * ceph_connection state bit flags
109  *
110  * QUEUED and BUSY are used together to ensure that only a single
111  * thread is currently opening, reading or writing data to the socket.
112  */
113 #define LOSSYTX         0  /* we can close channel or drop messages on errors */
114 #define CONNECTING      1
115 #define NEGOTIATING     2
116 #define KEEPALIVE_PENDING      3
117 #define WRITE_PENDING   4  /* we have data ready to send */
118 #define QUEUED          5  /* there is work queued on this connection */
119 #define BUSY            6  /* work is being done */
120 #define STANDBY         8  /* no outgoing messages, socket closed.  we keep
121                             * the ceph_connection around to maintain shared
122                             * state with the peer. */
123 #define CLOSED          10 /* we've closed the connection */
124 #define SOCK_CLOSED     11 /* socket state changed to closed */
125 #define REGISTERED      12 /* connection appears in con_tree */
126 #define OPENING         13 /* open connection w/ (possibly new) peer */
127 #define DEAD            14 /* dead, about to kfree */
128
129 /*
130  * A single connection with another host.
131  *
132  * We maintain a queue of outgoing messages, and some session state to
133  * ensure that we can preserve the lossless, ordered delivery of
134  * messages in the case of a TCP disconnect.
135  */
136 struct ceph_connection {
137         void *private;
138         atomic_t nref;
139
140         const struct ceph_connection_operations *ops;
141
142         struct ceph_messenger *msgr;
143         struct socket *sock;
144         unsigned long state;    /* connection state (see flags above) */
145         const char *error_msg;  /* error message, if any */
146
147         struct ceph_entity_addr peer_addr; /* peer address */
148         struct ceph_entity_name peer_name; /* peer name */
149         struct ceph_entity_addr peer_addr_for_me;
150         u32 connect_seq;      /* identify the most recent connection
151                                  attempt for this connection, client */
152         u32 peer_global_seq;  /* peer's global seq for this connection */
153
154         int auth_retry;       /* true if we need a newer authorizer */
155         void *auth_reply_buf;   /* where to put the authorizer reply */
156         int auth_reply_buf_len;
157
158         struct mutex mutex;
159
160         /* out queue */
161         struct list_head out_queue;
162         struct list_head out_sent;   /* sending or sent but unacked */
163         u64 out_seq;                 /* last message queued for send */
164         u64 out_seq_sent;            /* last message sent */
165         bool out_keepalive_pending;
166
167         u64 in_seq, in_seq_acked;  /* last message received, acked */
168
169         /* connection negotiation temps */
170         char in_banner[CEPH_BANNER_MAX_LEN];
171         union {
172                 struct {  /* outgoing connection */
173                         struct ceph_msg_connect out_connect;
174                         struct ceph_msg_connect_reply in_reply;
175                 };
176                 struct {  /* incoming */
177                         struct ceph_msg_connect in_connect;
178                         struct ceph_msg_connect_reply out_reply;
179                 };
180         };
181         struct ceph_entity_addr actual_peer_addr;
182
183         /* message out temps */
184         struct ceph_msg *out_msg;        /* sending message (== tail of
185                                             out_sent) */
186         bool out_msg_done;
187         struct ceph_msg_pos out_msg_pos;
188
189         struct kvec out_kvec[8],         /* sending header/footer data */
190                 *out_kvec_cur;
191         int out_kvec_left;   /* kvec's left in out_kvec */
192         int out_skip;        /* skip this many bytes */
193         int out_kvec_bytes;  /* total bytes left */
194         bool out_kvec_is_msg; /* kvec refers to out_msg */
195         int out_more;        /* there is more data after the kvecs */
196         __le64 out_temp_ack; /* for writing an ack */
197
198         /* message in temps */
199         struct ceph_msg_header in_hdr;
200         struct ceph_msg *in_msg;
201         struct ceph_msg_pos in_msg_pos;
202         u32 in_front_crc, in_middle_crc, in_data_crc;  /* calculated crc */
203
204         char in_tag;         /* protocol control byte */
205         int in_base_pos;     /* bytes read */
206         __le64 in_temp_ack;  /* for reading an ack */
207
208         struct delayed_work work;           /* send|recv work */
209         unsigned long       delay;          /* current delay interval */
210 };
211
212
213 extern const char *pr_addr(const struct sockaddr_storage *ss);
214 extern int ceph_parse_ips(const char *c, const char *end,
215                           struct ceph_entity_addr *addr,
216                           int max_count, int *count);
217
218
219 extern int ceph_msgr_init(void);
220 extern void ceph_msgr_exit(void);
221
222 extern struct ceph_messenger *ceph_messenger_create(
223         struct ceph_entity_addr *myaddr);
224 extern void ceph_messenger_destroy(struct ceph_messenger *);
225
226 extern void ceph_con_init(struct ceph_messenger *msgr,
227                           struct ceph_connection *con);
228 extern void ceph_con_open(struct ceph_connection *con,
229                           struct ceph_entity_addr *addr);
230 extern void ceph_con_close(struct ceph_connection *con);
231 extern void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg);
232 extern void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg);
233 extern void ceph_con_keepalive(struct ceph_connection *con);
234 extern struct ceph_connection *ceph_con_get(struct ceph_connection *con);
235 extern void ceph_con_put(struct ceph_connection *con);
236
237 extern struct ceph_msg *ceph_msg_new(int type, int front_len,
238                                      int page_len, int page_off,
239                                      struct page **pages);
240 extern void ceph_msg_kfree(struct ceph_msg *m);
241
242 extern struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con,
243                                        struct ceph_msg_header *hdr);
244 extern int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg);
245
246
247 static inline struct ceph_msg *ceph_msg_get(struct ceph_msg *msg)
248 {
249         kref_get(&msg->kref);
250         return msg;
251 }
252 extern void ceph_msg_last_put(struct kref *kref);
253 static inline void ceph_msg_put(struct ceph_msg *msg)
254 {
255         kref_put(&msg->kref, ceph_msg_last_put);
256 }
257
258 extern void ceph_msg_dump(struct ceph_msg *msg);
259
260 #endif