blob: ad93342c90d72cad59d4797608940b293f2a71fa [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Sage Weil31b80062009-10-06 11:31:13 -07003
4#include <linux/crc32c.h>
5#include <linux/ctype.h>
6#include <linux/highmem.h>
7#include <linux/inet.h>
8#include <linux/kthread.h>
9#include <linux/net.h>
Ilya Dryomov757856d2015-06-25 17:47:45 +030010#include <linux/nsproxy.h>
Ilya Dryomov633ee4072017-03-21 13:44:28 +010011#include <linux/sched/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Sage Weil31b80062009-10-06 11:31:13 -070013#include <linux/socket.h>
14#include <linux/string.h>
Alex Elder3ebc21f2013-01-31 16:02:01 -060015#ifdef CONFIG_BLOCK
Yehuda Sadeh68b44762010-04-06 15:01:27 -070016#include <linux/bio.h>
Alex Elder3ebc21f2013-01-31 16:02:01 -060017#endif /* CONFIG_BLOCK */
Noah Watkinsee3b56f2011-09-23 11:48:42 -070018#include <linux/dns_resolver.h>
Sage Weil31b80062009-10-06 11:31:13 -070019#include <net/tcp.h>
20
Ilya Dryomov2b3e0c92013-12-24 21:19:24 +020021#include <linux/ceph/ceph_features.h>
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070022#include <linux/ceph/libceph.h>
23#include <linux/ceph/messenger.h>
24#include <linux/ceph/decode.h>
25#include <linux/ceph/pagelist.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040026#include <linux/export.h>
Sage Weil31b80062009-10-06 11:31:13 -070027
28/*
29 * Ceph uses the messenger to exchange ceph_msg messages with other
30 * hosts in the system. The messenger provides ordered and reliable
31 * delivery. We tolerate TCP disconnects by reconnecting (with
32 * exponential backoff) in the case of a fault (disconnection, bad
33 * crc, protocol error). Acks allow sent messages to be discarded by
34 * the sender.
35 */
36
Alex Elderbc18f4b2012-06-20 21:53:53 -050037/*
38 * We track the state of the socket on a given connection using
39 * values defined below. The transition to a new socket state is
40 * handled by a function which verifies we aren't coming from an
41 * unexpected state.
42 *
43 * --------
44 * | NEW* | transient initial state
45 * --------
46 * | con_sock_state_init()
47 * v
48 * ----------
49 * | CLOSED | initialized, but no socket (and no
50 * ---------- TCP connection)
51 * ^ \
52 * | \ con_sock_state_connecting()
53 * | ----------------------
54 * | \
55 * + con_sock_state_closed() \
Sage Weilfbb85a42012-06-27 12:31:02 -070056 * |+--------------------------- \
57 * | \ \ \
58 * | ----------- \ \
59 * | | CLOSING | socket event; \ \
60 * | ----------- await close \ \
61 * | ^ \ |
62 * | | \ |
63 * | + con_sock_state_closing() \ |
64 * | / \ | |
65 * | / --------------- | |
66 * | / \ v v
Alex Elderbc18f4b2012-06-20 21:53:53 -050067 * | / --------------
68 * | / -----------------| CONNECTING | socket created, TCP
69 * | | / -------------- connect initiated
70 * | | | con_sock_state_connected()
71 * | | v
72 * -------------
73 * | CONNECTED | TCP connection established
74 * -------------
75 *
76 * State values for ceph_connection->sock_state; NEW is assumed to be 0.
77 */
Alex Elderce2c8902012-05-22 22:15:49 -050078
79#define CON_SOCK_STATE_NEW 0 /* -> CLOSED */
80#define CON_SOCK_STATE_CLOSED 1 /* -> CONNECTING */
81#define CON_SOCK_STATE_CONNECTING 2 /* -> CONNECTED or -> CLOSING */
82#define CON_SOCK_STATE_CONNECTED 3 /* -> CLOSING or -> CLOSED */
83#define CON_SOCK_STATE_CLOSING 4 /* -> CLOSED */
84
Sage Weil8dacc7d2012-07-20 17:24:40 -070085/*
86 * connection states
87 */
88#define CON_STATE_CLOSED 1 /* -> PREOPEN */
89#define CON_STATE_PREOPEN 2 /* -> CONNECTING, CLOSED */
90#define CON_STATE_CONNECTING 3 /* -> NEGOTIATING, CLOSED */
91#define CON_STATE_NEGOTIATING 4 /* -> OPEN, CLOSED */
92#define CON_STATE_OPEN 5 /* -> STANDBY, CLOSED */
93#define CON_STATE_STANDBY 6 /* -> PREOPEN, CLOSED */
94
Sage Weil4a861692012-07-20 17:29:55 -070095/*
96 * ceph_connection flag bits
97 */
98#define CON_FLAG_LOSSYTX 0 /* we can close channel or drop
99 * messages on errors */
100#define CON_FLAG_KEEPALIVE_PENDING 1 /* we need to send a keepalive */
101#define CON_FLAG_WRITE_PENDING 2 /* we have data ready to send */
102#define CON_FLAG_SOCK_CLOSED 3 /* socket state changed to closed */
103#define CON_FLAG_BACKOFF 4 /* need to retry queuing delayed work */
Sage Weil8dacc7d2012-07-20 17:24:40 -0700104
Alex Elderc9ffc772013-02-20 10:25:12 -0600105static bool con_flag_valid(unsigned long con_flag)
106{
107 switch (con_flag) {
108 case CON_FLAG_LOSSYTX:
109 case CON_FLAG_KEEPALIVE_PENDING:
110 case CON_FLAG_WRITE_PENDING:
111 case CON_FLAG_SOCK_CLOSED:
112 case CON_FLAG_BACKOFF:
113 return true;
114 default:
115 return false;
116 }
117}
118
119static void con_flag_clear(struct ceph_connection *con, unsigned long con_flag)
120{
121 BUG_ON(!con_flag_valid(con_flag));
122
123 clear_bit(con_flag, &con->flags);
124}
125
126static void con_flag_set(struct ceph_connection *con, unsigned long con_flag)
127{
128 BUG_ON(!con_flag_valid(con_flag));
129
130 set_bit(con_flag, &con->flags);
131}
132
133static bool con_flag_test(struct ceph_connection *con, unsigned long con_flag)
134{
135 BUG_ON(!con_flag_valid(con_flag));
136
137 return test_bit(con_flag, &con->flags);
138}
139
140static bool con_flag_test_and_clear(struct ceph_connection *con,
141 unsigned long con_flag)
142{
143 BUG_ON(!con_flag_valid(con_flag));
144
145 return test_and_clear_bit(con_flag, &con->flags);
146}
147
148static bool con_flag_test_and_set(struct ceph_connection *con,
149 unsigned long con_flag)
150{
151 BUG_ON(!con_flag_valid(con_flag));
152
153 return test_and_set_bit(con_flag, &con->flags);
154}
155
Alex Eldere3d5d632013-05-01 12:43:04 -0500156/* Slab caches for frequently-allocated structures */
157
158static struct kmem_cache *ceph_msg_cache;
Alex Elder81b36be2013-05-01 12:43:04 -0500159static struct kmem_cache *ceph_msg_data_cache;
Alex Eldere3d5d632013-05-01 12:43:04 -0500160
Sage Weil31b80062009-10-06 11:31:13 -0700161/* static tag bytes (protocol control messages) */
162static char tag_msg = CEPH_MSGR_TAG_MSG;
163static char tag_ack = CEPH_MSGR_TAG_ACK;
164static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
Yan, Zheng8b9558a2015-09-01 17:19:38 +0800165static char tag_keepalive2 = CEPH_MSGR_TAG_KEEPALIVE2;
Sage Weil31b80062009-10-06 11:31:13 -0700166
Sage Weila6a53492010-04-13 14:07:07 -0700167#ifdef CONFIG_LOCKDEP
168static struct lock_class_key socket_class;
169#endif
170
Alex Elder84495f42012-02-15 07:43:55 -0600171/*
172 * When skipping (ignoring) a block of input we read it into a "skip
173 * buffer," which is this many bytes in size.
174 */
175#define SKIP_BUF_SIZE 1024
Sage Weil31b80062009-10-06 11:31:13 -0700176
177static void queue_con(struct ceph_connection *con);
Ilya Dryomov37ab77a2014-06-24 16:21:45 +0400178static void cancel_con(struct ceph_connection *con);
Ilya Dryomov68931622015-07-03 15:44:41 +0300179static void ceph_con_workfn(struct work_struct *);
Alex Elder93209262013-02-19 12:25:57 -0600180static void con_fault(struct ceph_connection *con);
Sage Weil31b80062009-10-06 11:31:13 -0700181
Sage Weil31b80062009-10-06 11:31:13 -0700182/*
Alex Elderf64a9312012-01-23 15:49:27 -0600183 * Nicely render a sockaddr as a string. An array of formatted
184 * strings is used, to approximate reentrancy.
Sage Weil31b80062009-10-06 11:31:13 -0700185 */
Alex Elderf64a9312012-01-23 15:49:27 -0600186#define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
187#define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
188#define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
189#define MAX_ADDR_STR_LEN 64 /* 54 is enough */
190
191static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
192static atomic_t addr_str_seq = ATOMIC_INIT(0);
Sage Weil31b80062009-10-06 11:31:13 -0700193
Alex Elder57666512012-01-23 15:49:27 -0600194static struct page *zero_page; /* used in certain error cases */
Alex Elder57666512012-01-23 15:49:27 -0600195
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700196const char *ceph_pr_addr(const struct sockaddr_storage *ss)
Sage Weil31b80062009-10-06 11:31:13 -0700197{
198 int i;
199 char *s;
Alex Elder99f0f3b2012-01-23 15:49:27 -0600200 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
201 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Sage Weil31b80062009-10-06 11:31:13 -0700202
Alex Elderf64a9312012-01-23 15:49:27 -0600203 i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
Sage Weil31b80062009-10-06 11:31:13 -0700204 s = addr_str[i];
205
206 switch (ss->ss_family) {
207 case AF_INET:
Alex Elderbd406142012-01-23 15:49:27 -0600208 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr,
209 ntohs(in4->sin_port));
Sage Weil31b80062009-10-06 11:31:13 -0700210 break;
211
212 case AF_INET6:
Alex Elderbd406142012-01-23 15:49:27 -0600213 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr,
214 ntohs(in6->sin6_port));
Sage Weil31b80062009-10-06 11:31:13 -0700215 break;
216
217 default:
Alex Elderd3002b92012-02-14 14:05:33 -0600218 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
219 ss->ss_family);
Sage Weil31b80062009-10-06 11:31:13 -0700220 }
221
222 return s;
223}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700224EXPORT_SYMBOL(ceph_pr_addr);
Sage Weil31b80062009-10-06 11:31:13 -0700225
Sage Weil63f2d212009-11-03 15:17:56 -0800226static void encode_my_addr(struct ceph_messenger *msgr)
227{
228 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
229 ceph_encode_addr(&msgr->my_enc_addr);
230}
231
Sage Weil31b80062009-10-06 11:31:13 -0700232/*
233 * work queue for all reading and writing to/from the socket.
234 */
Alex Eldere0f43c92012-02-14 14:05:33 -0600235static struct workqueue_struct *ceph_msgr_wq;
Sage Weil31b80062009-10-06 11:31:13 -0700236
Alex Eldere3d5d632013-05-01 12:43:04 -0500237static int ceph_msgr_slab_init(void)
238{
239 BUG_ON(ceph_msg_cache);
Geliang Tang5ee61e92016-03-13 15:18:39 +0800240 ceph_msg_cache = KMEM_CACHE(ceph_msg, 0);
Alex Elder81b36be2013-05-01 12:43:04 -0500241 if (!ceph_msg_cache)
242 return -ENOMEM;
243
244 BUG_ON(ceph_msg_data_cache);
Geliang Tang5ee61e92016-03-13 15:18:39 +0800245 ceph_msg_data_cache = KMEM_CACHE(ceph_msg_data, 0);
Alex Elder81b36be2013-05-01 12:43:04 -0500246 if (ceph_msg_data_cache)
247 return 0;
248
249 kmem_cache_destroy(ceph_msg_cache);
250 ceph_msg_cache = NULL;
251
252 return -ENOMEM;
Alex Eldere3d5d632013-05-01 12:43:04 -0500253}
254
255static void ceph_msgr_slab_exit(void)
256{
Alex Elder81b36be2013-05-01 12:43:04 -0500257 BUG_ON(!ceph_msg_data_cache);
258 kmem_cache_destroy(ceph_msg_data_cache);
259 ceph_msg_data_cache = NULL;
260
Alex Eldere3d5d632013-05-01 12:43:04 -0500261 BUG_ON(!ceph_msg_cache);
262 kmem_cache_destroy(ceph_msg_cache);
263 ceph_msg_cache = NULL;
264}
265
Alex Elder15417162013-02-19 12:25:56 -0600266static void _ceph_msgr_exit(void)
Alex Elder6173d1f2012-02-14 14:05:33 -0600267{
Alex Elderd3002b92012-02-14 14:05:33 -0600268 if (ceph_msgr_wq) {
Alex Elder6173d1f2012-02-14 14:05:33 -0600269 destroy_workqueue(ceph_msgr_wq);
Alex Elderd3002b92012-02-14 14:05:33 -0600270 ceph_msgr_wq = NULL;
271 }
Alex Elder6173d1f2012-02-14 14:05:33 -0600272
Alex Elder6173d1f2012-02-14 14:05:33 -0600273 BUG_ON(zero_page == NULL);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300274 put_page(zero_page);
Alex Elder6173d1f2012-02-14 14:05:33 -0600275 zero_page = NULL;
Benoît Canetd920ff62015-06-25 21:02:57 +0200276
277 ceph_msgr_slab_exit();
Alex Elder6173d1f2012-02-14 14:05:33 -0600278}
279
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700280int ceph_msgr_init(void)
Sage Weil31b80062009-10-06 11:31:13 -0700281{
Benoît Canetd920ff62015-06-25 21:02:57 +0200282 if (ceph_msgr_slab_init())
283 return -ENOMEM;
284
Alex Elder57666512012-01-23 15:49:27 -0600285 BUG_ON(zero_page != NULL);
286 zero_page = ZERO_PAGE(0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300287 get_page(zero_page);
Alex Elder57666512012-01-23 15:49:27 -0600288
Ilya Dryomovf9865f02014-10-10 16:39:05 +0400289 /*
290 * The number of active work items is limited by the number of
291 * connections, so leave @max_active at default.
292 */
293 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_MEM_RECLAIM, 0);
Alex Elder6173d1f2012-02-14 14:05:33 -0600294 if (ceph_msgr_wq)
295 return 0;
Alex Elder57666512012-01-23 15:49:27 -0600296
Alex Elder6173d1f2012-02-14 14:05:33 -0600297 pr_err("msgr_init failed to create workqueue\n");
298 _ceph_msgr_exit();
Alex Elder57666512012-01-23 15:49:27 -0600299
Alex Elder6173d1f2012-02-14 14:05:33 -0600300 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -0700301}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700302EXPORT_SYMBOL(ceph_msgr_init);
Sage Weil31b80062009-10-06 11:31:13 -0700303
304void ceph_msgr_exit(void)
305{
Alex Elder57666512012-01-23 15:49:27 -0600306 BUG_ON(ceph_msgr_wq == NULL);
Alex Elder57666512012-01-23 15:49:27 -0600307
Alex Elder6173d1f2012-02-14 14:05:33 -0600308 _ceph_msgr_exit();
Sage Weil31b80062009-10-06 11:31:13 -0700309}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700310EXPORT_SYMBOL(ceph_msgr_exit);
Sage Weil31b80062009-10-06 11:31:13 -0700311
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700312void ceph_msgr_flush(void)
Sage Weila922d382010-05-29 09:41:23 -0700313{
314 flush_workqueue(ceph_msgr_wq);
315}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700316EXPORT_SYMBOL(ceph_msgr_flush);
Sage Weila922d382010-05-29 09:41:23 -0700317
Alex Elderce2c8902012-05-22 22:15:49 -0500318/* Connection socket state transition functions */
319
320static void con_sock_state_init(struct ceph_connection *con)
321{
322 int old_state;
323
324 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
325 if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
326 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700327 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
328 CON_SOCK_STATE_CLOSED);
Alex Elderce2c8902012-05-22 22:15:49 -0500329}
330
331static void con_sock_state_connecting(struct ceph_connection *con)
332{
333 int old_state;
334
335 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
336 if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
337 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700338 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
339 CON_SOCK_STATE_CONNECTING);
Alex Elderce2c8902012-05-22 22:15:49 -0500340}
341
342static void con_sock_state_connected(struct ceph_connection *con)
343{
344 int old_state;
345
346 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
347 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
348 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700349 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
350 CON_SOCK_STATE_CONNECTED);
Alex Elderce2c8902012-05-22 22:15:49 -0500351}
352
353static void con_sock_state_closing(struct ceph_connection *con)
354{
355 int old_state;
356
357 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
358 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
359 old_state != CON_SOCK_STATE_CONNECTED &&
360 old_state != CON_SOCK_STATE_CLOSING))
361 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700362 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
363 CON_SOCK_STATE_CLOSING);
Alex Elderce2c8902012-05-22 22:15:49 -0500364}
365
366static void con_sock_state_closed(struct ceph_connection *con)
367{
368 int old_state;
369
370 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
371 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
Sage Weilfbb85a42012-06-27 12:31:02 -0700372 old_state != CON_SOCK_STATE_CLOSING &&
Sage Weil8007b8d2012-07-30 18:16:16 -0700373 old_state != CON_SOCK_STATE_CONNECTING &&
374 old_state != CON_SOCK_STATE_CLOSED))
Alex Elderce2c8902012-05-22 22:15:49 -0500375 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700376 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
377 CON_SOCK_STATE_CLOSED);
Alex Elderce2c8902012-05-22 22:15:49 -0500378}
Sage Weila922d382010-05-29 09:41:23 -0700379
Sage Weil31b80062009-10-06 11:31:13 -0700380/*
381 * socket callback functions
382 */
383
384/* data available on socket, or listen socket received a connect */
David S. Miller676d2362014-04-11 16:15:36 -0400385static void ceph_sock_data_ready(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700386{
Alex Elderbd406142012-01-23 15:49:27 -0600387 struct ceph_connection *con = sk->sk_user_data;
Guanjun Hea2a32582012-07-08 19:50:33 -0700388 if (atomic_read(&con->msgr->stopping)) {
389 return;
390 }
Alex Elderbd406142012-01-23 15:49:27 -0600391
Sage Weil31b80062009-10-06 11:31:13 -0700392 if (sk->sk_state != TCP_CLOSE_WAIT) {
Alex Elder327800b2012-05-22 11:41:43 -0500393 dout("%s on %p state = %lu, queueing work\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700394 con, con->state);
395 queue_con(con);
396 }
397}
398
399/* socket has buffer space for writing */
Alex Elder327800b2012-05-22 11:41:43 -0500400static void ceph_sock_write_space(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700401{
Alex Elderd3002b92012-02-14 14:05:33 -0600402 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700403
Jim Schutt182fac22012-02-29 08:30:58 -0700404 /* only queue to workqueue if there is data we want to write,
405 * and there is sufficient space in the socket buffer to accept
Alex Elder327800b2012-05-22 11:41:43 -0500406 * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
Jim Schutt182fac22012-02-29 08:30:58 -0700407 * doesn't get called again until try_write() fills the socket
408 * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
409 * and net/core/stream.c:sk_stream_write_space().
410 */
Alex Elderc9ffc772013-02-20 10:25:12 -0600411 if (con_flag_test(con, CON_FLAG_WRITE_PENDING)) {
Eric Dumazet64dc6132013-07-22 20:26:31 -0700412 if (sk_stream_is_writeable(sk)) {
Alex Elder327800b2012-05-22 11:41:43 -0500413 dout("%s %p queueing write work\n", __func__, con);
Jim Schutt182fac22012-02-29 08:30:58 -0700414 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
415 queue_con(con);
416 }
Sage Weil31b80062009-10-06 11:31:13 -0700417 } else {
Alex Elder327800b2012-05-22 11:41:43 -0500418 dout("%s %p nothing to write\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -0700419 }
Sage Weil31b80062009-10-06 11:31:13 -0700420}
421
422/* socket's state has changed */
Alex Elder327800b2012-05-22 11:41:43 -0500423static void ceph_sock_state_change(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700424{
Alex Elderbd406142012-01-23 15:49:27 -0600425 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700426
Alex Elder327800b2012-05-22 11:41:43 -0500427 dout("%s %p state = %lu sk_state = %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700428 con, con->state, sk->sk_state);
429
Sage Weil31b80062009-10-06 11:31:13 -0700430 switch (sk->sk_state) {
431 case TCP_CLOSE:
Alex Elder327800b2012-05-22 11:41:43 -0500432 dout("%s TCP_CLOSE\n", __func__);
Sage Weil31b80062009-10-06 11:31:13 -0700433 case TCP_CLOSE_WAIT:
Alex Elder327800b2012-05-22 11:41:43 -0500434 dout("%s TCP_CLOSE_WAIT\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500435 con_sock_state_closing(con);
Alex Elderc9ffc772013-02-20 10:25:12 -0600436 con_flag_set(con, CON_FLAG_SOCK_CLOSED);
Alex Elderd65c9e02012-06-20 21:53:53 -0500437 queue_con(con);
Sage Weil31b80062009-10-06 11:31:13 -0700438 break;
439 case TCP_ESTABLISHED:
Alex Elder327800b2012-05-22 11:41:43 -0500440 dout("%s TCP_ESTABLISHED\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500441 con_sock_state_connected(con);
Sage Weil31b80062009-10-06 11:31:13 -0700442 queue_con(con);
443 break;
Alex Elderd3002b92012-02-14 14:05:33 -0600444 default: /* Everything else is uninteresting */
445 break;
Sage Weil31b80062009-10-06 11:31:13 -0700446 }
447}
448
449/*
450 * set up socket callbacks
451 */
452static void set_sock_callbacks(struct socket *sock,
453 struct ceph_connection *con)
454{
455 struct sock *sk = sock->sk;
Alex Elderbd406142012-01-23 15:49:27 -0600456 sk->sk_user_data = con;
Alex Elder327800b2012-05-22 11:41:43 -0500457 sk->sk_data_ready = ceph_sock_data_ready;
458 sk->sk_write_space = ceph_sock_write_space;
459 sk->sk_state_change = ceph_sock_state_change;
Sage Weil31b80062009-10-06 11:31:13 -0700460}
461
462
463/*
464 * socket helpers
465 */
466
467/*
468 * initiate connection to a remote socket.
469 */
Alex Elder41617d02012-02-14 14:05:33 -0600470static int ceph_tcp_connect(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700471{
Sage Weilf91d3472010-07-01 15:18:31 -0700472 struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
Sage Weil31b80062009-10-06 11:31:13 -0700473 struct socket *sock;
Ilya Dryomov633ee4072017-03-21 13:44:28 +0100474 unsigned int noio_flag;
Sage Weil31b80062009-10-06 11:31:13 -0700475 int ret;
476
477 BUG_ON(con->sock);
Ilya Dryomov633ee4072017-03-21 13:44:28 +0100478
479 /* sock_create_kern() allocates with GFP_KERNEL */
480 noio_flag = memalloc_noio_save();
Ilya Dryomov757856d2015-06-25 17:47:45 +0300481 ret = sock_create_kern(read_pnet(&con->msgr->net), paddr->ss_family,
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -0500482 SOCK_STREAM, IPPROTO_TCP, &sock);
Ilya Dryomov633ee4072017-03-21 13:44:28 +0100483 memalloc_noio_restore(noio_flag);
Sage Weil31b80062009-10-06 11:31:13 -0700484 if (ret)
Alex Elder41617d02012-02-14 14:05:33 -0600485 return ret;
Ilya Dryomov6d7fdb02015-04-02 14:40:58 +0300486 sock->sk->sk_allocation = GFP_NOFS;
Sage Weil31b80062009-10-06 11:31:13 -0700487
Sage Weila6a53492010-04-13 14:07:07 -0700488#ifdef CONFIG_LOCKDEP
489 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
490#endif
491
Sage Weil31b80062009-10-06 11:31:13 -0700492 set_sock_callbacks(sock, con);
493
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700494 dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700495
Sage Weil89a86be2012-06-09 14:19:21 -0700496 con_sock_state_connecting(con);
Sage Weilf91d3472010-07-01 15:18:31 -0700497 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
498 O_NONBLOCK);
Sage Weil31b80062009-10-06 11:31:13 -0700499 if (ret == -EINPROGRESS) {
500 dout("connect %s EINPROGRESS sk_state = %u\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700501 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -0700502 sock->sk->sk_state);
Alex Eldera5bc3122012-01-23 15:49:27 -0600503 } else if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700504 pr_err("connect %s error %d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700505 ceph_pr_addr(&con->peer_addr.in_addr), ret);
Sage Weil31b80062009-10-06 11:31:13 -0700506 sock_release(sock);
Alex Elder41617d02012-02-14 14:05:33 -0600507 return ret;
Alex Eldera5bc3122012-01-23 15:49:27 -0600508 }
Mike Christie89baaa52014-10-16 01:50:19 -0500509
Ilya Dryomov859bff52015-10-28 23:50:58 +0100510 if (ceph_test_opt(from_msgr(con->msgr), TCP_NODELAY)) {
Chaitanya Huilgolba988f82015-01-23 16:41:25 +0530511 int optval = 1;
512
513 ret = kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY,
514 (char *)&optval, sizeof(optval));
515 if (ret)
516 pr_err("kernel_setsockopt(TCP_NODELAY) failed: %d",
517 ret);
518 }
519
Alex Eldera5bc3122012-01-23 15:49:27 -0600520 con->sock = sock;
Alex Elder41617d02012-02-14 14:05:33 -0600521 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700522}
523
524static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
525{
526 struct kvec iov = {buf, len};
527 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil98bdb0a2011-01-25 08:17:48 -0800528 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700529
Al Viro100803a2015-11-15 00:12:48 -0500530 iov_iter_kvec(&msg.msg_iter, READ | ITER_KVEC, &iov, 1, len);
531 r = sock_recvmsg(sock, &msg, msg.msg_flags);
Sage Weil98bdb0a2011-01-25 08:17:48 -0800532 if (r == -EAGAIN)
533 r = 0;
534 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700535}
536
Alex Elderafb3d902013-03-08 20:58:59 -0600537static int ceph_tcp_recvpage(struct socket *sock, struct page *page,
538 int page_offset, size_t length)
539{
Al Viro100803a2015-11-15 00:12:48 -0500540 struct bio_vec bvec = {
541 .bv_page = page,
542 .bv_offset = page_offset,
543 .bv_len = length
544 };
545 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
546 int r;
Alex Elderafb3d902013-03-08 20:58:59 -0600547
548 BUG_ON(page_offset + length > PAGE_SIZE);
Al Viro100803a2015-11-15 00:12:48 -0500549 iov_iter_bvec(&msg.msg_iter, READ | ITER_BVEC, &bvec, 1, length);
550 r = sock_recvmsg(sock, &msg, msg.msg_flags);
551 if (r == -EAGAIN)
552 r = 0;
553 return r;
Alex Elderafb3d902013-03-08 20:58:59 -0600554}
555
Sage Weil31b80062009-10-06 11:31:13 -0700556/*
557 * write something. @more is true if caller will be sending more data
558 * shortly.
559 */
560static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
561 size_t kvlen, size_t len, int more)
562{
563 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil42961d22011-01-25 08:19:34 -0800564 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700565
566 if (more)
567 msg.msg_flags |= MSG_MORE;
568 else
569 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
570
Sage Weil42961d22011-01-25 08:19:34 -0800571 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
572 if (r == -EAGAIN)
573 r = 0;
574 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700575}
576
Chunwei Chen178eda292014-04-23 12:35:09 +0800577static int __ceph_tcp_sendpage(struct socket *sock, struct page *page,
Alex Eldere1dcb122013-03-06 23:39:38 -0600578 int offset, size_t size, bool more)
Alex Elder31739132012-03-07 11:40:08 -0600579{
580 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
581 int ret;
582
583 ret = kernel_sendpage(sock, page, offset, size, flags);
584 if (ret == -EAGAIN)
585 ret = 0;
586
587 return ret;
588}
589
Chunwei Chen178eda292014-04-23 12:35:09 +0800590static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
591 int offset, size_t size, bool more)
592{
Al Viro61ff6e92016-01-09 20:55:45 -0500593 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
594 struct bio_vec bvec;
Chunwei Chen178eda292014-04-23 12:35:09 +0800595 int ret;
Chunwei Chen178eda292014-04-23 12:35:09 +0800596
597 /* sendpage cannot properly handle pages with page_count == 0,
598 * we need to fallback to sendmsg if that's the case */
599 if (page_count(page) >= 1)
600 return __ceph_tcp_sendpage(sock, page, offset, size, more);
601
Al Viro61ff6e92016-01-09 20:55:45 -0500602 bvec.bv_page = page;
603 bvec.bv_offset = offset;
604 bvec.bv_len = size;
605
606 if (more)
607 msg.msg_flags |= MSG_MORE;
608 else
609 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
610
611 iov_iter_bvec(&msg.msg_iter, WRITE | ITER_BVEC, &bvec, 1, size);
612 ret = sock_sendmsg(sock, &msg);
613 if (ret == -EAGAIN)
614 ret = 0;
Chunwei Chen178eda292014-04-23 12:35:09 +0800615
616 return ret;
617}
Sage Weil31b80062009-10-06 11:31:13 -0700618
619/*
620 * Shutdown/close the socket for the given connection.
621 */
622static int con_close_socket(struct ceph_connection *con)
623{
Sage Weil8007b8d2012-07-30 18:16:16 -0700624 int rc = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700625
626 dout("con_close_socket on %p sock %p\n", con, con->sock);
Sage Weil8007b8d2012-07-30 18:16:16 -0700627 if (con->sock) {
628 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
629 sock_release(con->sock);
630 con->sock = NULL;
631 }
Alex Elder456ea462012-06-20 21:53:53 -0500632
633 /*
Sage Weil4a861692012-07-20 17:29:55 -0700634 * Forcibly clear the SOCK_CLOSED flag. It gets set
Alex Elder456ea462012-06-20 21:53:53 -0500635 * independent of the connection mutex, and we could have
636 * received a socket close event before we had the chance to
637 * shut the socket down.
638 */
Alex Elderc9ffc772013-02-20 10:25:12 -0600639 con_flag_clear(con, CON_FLAG_SOCK_CLOSED);
Sage Weil8007b8d2012-07-30 18:16:16 -0700640
Alex Elderce2c8902012-05-22 22:15:49 -0500641 con_sock_state_closed(con);
Sage Weil31b80062009-10-06 11:31:13 -0700642 return rc;
643}
644
645/*
646 * Reset a connection. Discard all incoming and outgoing messages
647 * and clear *_seq state.
648 */
649static void ceph_msg_remove(struct ceph_msg *msg)
650{
651 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -0500652
Sage Weil31b80062009-10-06 11:31:13 -0700653 ceph_msg_put(msg);
654}
655static void ceph_msg_remove_list(struct list_head *head)
656{
657 while (!list_empty(head)) {
658 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
659 list_head);
660 ceph_msg_remove(msg);
661 }
662}
663
664static void reset_connection(struct ceph_connection *con)
665{
666 /* reset connection, out_queue, msg_ and connect_seq */
667 /* discard existing out_queue and msg_seq */
Sage Weil0fa6ebc2012-12-27 20:27:04 -0600668 dout("reset_connection %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -0700669 ceph_msg_remove_list(&con->out_queue);
670 ceph_msg_remove_list(&con->out_sent);
671
Sage Weilcf3e5c42009-12-11 09:48:05 -0800672 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -0500673 BUG_ON(con->in_msg->con != con);
Sage Weilcf3e5c42009-12-11 09:48:05 -0800674 ceph_msg_put(con->in_msg);
675 con->in_msg = NULL;
676 }
677
Sage Weil31b80062009-10-06 11:31:13 -0700678 con->connect_seq = 0;
679 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800680 if (con->out_msg) {
Ilya Dryomov583d0fe2015-11-02 17:13:58 +0100681 BUG_ON(con->out_msg->con != con);
Sage Weilc86a2932009-12-14 14:04:30 -0800682 ceph_msg_put(con->out_msg);
683 con->out_msg = NULL;
684 }
Sage Weil31b80062009-10-06 11:31:13 -0700685 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700686 con->in_seq_acked = 0;
Ilya Dryomov67645d72015-12-28 13:18:34 +0300687
688 con->out_skip = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700689}
690
691/*
692 * mark a peer down. drop any open connections.
693 */
694void ceph_con_close(struct ceph_connection *con)
695{
Sage Weil8c50c812012-07-30 16:24:37 -0700696 mutex_lock(&con->mutex);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700697 dout("con_close %p peer %s\n", con,
698 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil8dacc7d2012-07-20 17:24:40 -0700699 con->state = CON_STATE_CLOSED;
Alex Eldera5988c42012-05-29 11:04:58 -0500700
Alex Elderc9ffc772013-02-20 10:25:12 -0600701 con_flag_clear(con, CON_FLAG_LOSSYTX); /* so we retry next connect */
702 con_flag_clear(con, CON_FLAG_KEEPALIVE_PENDING);
703 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
704 con_flag_clear(con, CON_FLAG_BACKOFF);
Alex Eldera5988c42012-05-29 11:04:58 -0500705
Sage Weil31b80062009-10-06 11:31:13 -0700706 reset_connection(con);
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700707 con->peer_global_seq = 0;
Ilya Dryomov37ab77a2014-06-24 16:21:45 +0400708 cancel_con(con);
Sage Weilee76e072012-07-20 16:45:49 -0700709 con_close_socket(con);
Sage Weilec302642009-12-22 10:43:42 -0800710 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700711}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700712EXPORT_SYMBOL(ceph_con_close);
Sage Weil31b80062009-10-06 11:31:13 -0700713
714/*
Sage Weil31b80062009-10-06 11:31:13 -0700715 * Reopen a closed connection, with a new peer address.
716 */
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700717void ceph_con_open(struct ceph_connection *con,
718 __u8 entity_type, __u64 entity_num,
719 struct ceph_entity_addr *addr)
Sage Weil31b80062009-10-06 11:31:13 -0700720{
Sage Weil54691552012-07-30 16:21:40 -0700721 mutex_lock(&con->mutex);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700722 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
Sage Weil8dacc7d2012-07-20 17:24:40 -0700723
Alex Elder122070a2012-12-26 10:43:57 -0600724 WARN_ON(con->state != CON_STATE_CLOSED);
Sage Weil8dacc7d2012-07-20 17:24:40 -0700725 con->state = CON_STATE_PREOPEN;
Alex Eldera5988c42012-05-29 11:04:58 -0500726
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700727 con->peer_name.type = (__u8) entity_type;
728 con->peer_name.num = cpu_to_le64(entity_num);
729
Sage Weil31b80062009-10-06 11:31:13 -0700730 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800731 con->delay = 0; /* reset backoff memory */
Sage Weil54691552012-07-30 16:21:40 -0700732 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700733 queue_con(con);
734}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700735EXPORT_SYMBOL(ceph_con_open);
Sage Weil31b80062009-10-06 11:31:13 -0700736
737/*
Sage Weil87b315a2010-03-22 14:51:18 -0700738 * return true if this connection ever successfully opened
739 */
740bool ceph_con_opened(struct ceph_connection *con)
741{
742 return con->connect_seq > 0;
743}
744
745/*
Sage Weil31b80062009-10-06 11:31:13 -0700746 * initialize a new connection.
747 */
Alex Elder1bfd89f2012-05-26 23:26:43 -0500748void ceph_con_init(struct ceph_connection *con, void *private,
749 const struct ceph_connection_operations *ops,
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700750 struct ceph_messenger *msgr)
Sage Weil31b80062009-10-06 11:31:13 -0700751{
752 dout("con_init %p\n", con);
753 memset(con, 0, sizeof(*con));
Alex Elder1bfd89f2012-05-26 23:26:43 -0500754 con->private = private;
755 con->ops = ops;
Sage Weil31b80062009-10-06 11:31:13 -0700756 con->msgr = msgr;
Alex Elderce2c8902012-05-22 22:15:49 -0500757
758 con_sock_state_init(con);
759
Sage Weilec302642009-12-22 10:43:42 -0800760 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700761 INIT_LIST_HEAD(&con->out_queue);
762 INIT_LIST_HEAD(&con->out_sent);
Ilya Dryomov68931622015-07-03 15:44:41 +0300763 INIT_DELAYED_WORK(&con->work, ceph_con_workfn);
Alex Eldera5988c42012-05-29 11:04:58 -0500764
Sage Weil8dacc7d2012-07-20 17:24:40 -0700765 con->state = CON_STATE_CLOSED;
Sage Weil31b80062009-10-06 11:31:13 -0700766}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700767EXPORT_SYMBOL(ceph_con_init);
Sage Weil31b80062009-10-06 11:31:13 -0700768
769
770/*
771 * We maintain a global counter to order connection attempts. Get
772 * a unique seq greater than @gt.
773 */
774static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
775{
776 u32 ret;
777
778 spin_lock(&msgr->global_seq_lock);
779 if (msgr->global_seq < gt)
780 msgr->global_seq = gt;
781 ret = ++msgr->global_seq;
782 spin_unlock(&msgr->global_seq_lock);
783 return ret;
784}
785
Alex Eldere2200422012-05-23 14:35:23 -0500786static void con_out_kvec_reset(struct ceph_connection *con)
Alex Elder859eb792012-02-14 14:05:33 -0600787{
Ilya Dryomov67645d72015-12-28 13:18:34 +0300788 BUG_ON(con->out_skip);
789
Alex Elder859eb792012-02-14 14:05:33 -0600790 con->out_kvec_left = 0;
791 con->out_kvec_bytes = 0;
792 con->out_kvec_cur = &con->out_kvec[0];
793}
794
Alex Eldere2200422012-05-23 14:35:23 -0500795static void con_out_kvec_add(struct ceph_connection *con,
Alex Elder859eb792012-02-14 14:05:33 -0600796 size_t size, void *data)
797{
Ilya Dryomov67645d72015-12-28 13:18:34 +0300798 int index = con->out_kvec_left;
Alex Elder859eb792012-02-14 14:05:33 -0600799
Ilya Dryomov67645d72015-12-28 13:18:34 +0300800 BUG_ON(con->out_skip);
Alex Elder859eb792012-02-14 14:05:33 -0600801 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
802
803 con->out_kvec[index].iov_len = size;
804 con->out_kvec[index].iov_base = data;
805 con->out_kvec_left++;
806 con->out_kvec_bytes += size;
807}
Sage Weil31b80062009-10-06 11:31:13 -0700808
Ilya Dryomov67645d72015-12-28 13:18:34 +0300809/*
810 * Chop off a kvec from the end. Return residual number of bytes for
811 * that kvec, i.e. how many bytes would have been written if the kvec
812 * hadn't been nuked.
813 */
814static int con_out_kvec_skip(struct ceph_connection *con)
815{
816 int off = con->out_kvec_cur - con->out_kvec;
817 int skip = 0;
818
819 if (con->out_kvec_bytes > 0) {
820 skip = con->out_kvec[off + con->out_kvec_left - 1].iov_len;
821 BUG_ON(con->out_kvec_bytes < skip);
822 BUG_ON(!con->out_kvec_left);
823 con->out_kvec_bytes -= skip;
824 con->out_kvec_left--;
825 }
826
827 return skip;
828}
829
Alex Elderdf6ad1f2012-06-11 14:57:13 -0500830#ifdef CONFIG_BLOCK
Alex Elder6aaa4512013-03-06 23:39:39 -0600831
832/*
833 * For a bio data item, a piece is whatever remains of the next
834 * entry in the current bio iovec, or the first entry in the next
835 * bio in the list.
836 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500837static void ceph_msg_data_bio_cursor_init(struct ceph_msg_data_cursor *cursor,
Alex Elder25aff7c2013-03-11 23:34:22 -0500838 size_t length)
Alex Elder6aaa4512013-03-06 23:39:39 -0600839{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500840 struct ceph_msg_data *data = cursor->data;
Alex Elder6aaa4512013-03-06 23:39:39 -0600841 struct bio *bio;
842
843 BUG_ON(data->type != CEPH_MSG_DATA_BIO);
844
845 bio = data->bio;
846 BUG_ON(!bio);
Alex Elder6aaa4512013-03-06 23:39:39 -0600847
Alex Elderca8b3a62013-04-05 14:46:01 -0500848 cursor->resid = min(length, data->bio_length);
Alex Elder6aaa4512013-03-06 23:39:39 -0600849 cursor->bio = bio;
Kent Overstreetf38a5182013-08-07 14:30:24 -0700850 cursor->bvec_iter = bio->bi_iter;
851 cursor->last_piece =
852 cursor->resid <= bio_iter_len(bio, cursor->bvec_iter);
Alex Elder6aaa4512013-03-06 23:39:39 -0600853}
854
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500855static struct page *ceph_msg_data_bio_next(struct ceph_msg_data_cursor *cursor,
Alex Elder6aaa4512013-03-06 23:39:39 -0600856 size_t *page_offset,
857 size_t *length)
858{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500859 struct ceph_msg_data *data = cursor->data;
Alex Elder6aaa4512013-03-06 23:39:39 -0600860 struct bio *bio;
Kent Overstreetf38a5182013-08-07 14:30:24 -0700861 struct bio_vec bio_vec;
Alex Elder6aaa4512013-03-06 23:39:39 -0600862
863 BUG_ON(data->type != CEPH_MSG_DATA_BIO);
864
865 bio = cursor->bio;
866 BUG_ON(!bio);
867
Kent Overstreetf38a5182013-08-07 14:30:24 -0700868 bio_vec = bio_iter_iovec(bio, cursor->bvec_iter);
Alex Elder6aaa4512013-03-06 23:39:39 -0600869
Kent Overstreetf38a5182013-08-07 14:30:24 -0700870 *page_offset = (size_t) bio_vec.bv_offset;
Alex Elder6aaa4512013-03-06 23:39:39 -0600871 BUG_ON(*page_offset >= PAGE_SIZE);
Alex Elder25aff7c2013-03-11 23:34:22 -0500872 if (cursor->last_piece) /* pagelist offset is always 0 */
873 *length = cursor->resid;
874 else
Kent Overstreetf38a5182013-08-07 14:30:24 -0700875 *length = (size_t) bio_vec.bv_len;
Alex Elder25aff7c2013-03-11 23:34:22 -0500876 BUG_ON(*length > cursor->resid);
Alex Elder5df521b2013-03-30 15:09:59 -0500877 BUG_ON(*page_offset + *length > PAGE_SIZE);
Alex Elder6aaa4512013-03-06 23:39:39 -0600878
Kent Overstreetf38a5182013-08-07 14:30:24 -0700879 return bio_vec.bv_page;
Alex Elder6aaa4512013-03-06 23:39:39 -0600880}
881
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500882static bool ceph_msg_data_bio_advance(struct ceph_msg_data_cursor *cursor,
883 size_t bytes)
Alex Elder6aaa4512013-03-06 23:39:39 -0600884{
Alex Elder6aaa4512013-03-06 23:39:39 -0600885 struct bio *bio;
Kent Overstreetf38a5182013-08-07 14:30:24 -0700886 struct bio_vec bio_vec;
Alex Elder6aaa4512013-03-06 23:39:39 -0600887
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500888 BUG_ON(cursor->data->type != CEPH_MSG_DATA_BIO);
Alex Elder6aaa4512013-03-06 23:39:39 -0600889
890 bio = cursor->bio;
891 BUG_ON(!bio);
892
Kent Overstreetf38a5182013-08-07 14:30:24 -0700893 bio_vec = bio_iter_iovec(bio, cursor->bvec_iter);
Alex Elder6aaa4512013-03-06 23:39:39 -0600894
895 /* Advance the cursor offset */
896
Alex Elder25aff7c2013-03-11 23:34:22 -0500897 BUG_ON(cursor->resid < bytes);
898 cursor->resid -= bytes;
Kent Overstreetf38a5182013-08-07 14:30:24 -0700899
900 bio_advance_iter(bio, &cursor->bvec_iter, bytes);
901
902 if (bytes < bio_vec.bv_len)
Alex Elder6aaa4512013-03-06 23:39:39 -0600903 return false; /* more bytes to process in this segment */
904
905 /* Move on to the next segment, and possibly the next bio */
906
Kent Overstreetf38a5182013-08-07 14:30:24 -0700907 if (!cursor->bvec_iter.bi_size) {
Alex Elder6aaa4512013-03-06 23:39:39 -0600908 bio = bio->bi_next;
Ilya Dryomov0ec1d152014-02-05 15:19:55 +0200909 cursor->bio = bio;
910 if (bio)
911 cursor->bvec_iter = bio->bi_iter;
912 else
913 memset(&cursor->bvec_iter, 0,
914 sizeof(cursor->bvec_iter));
Alex Elder6aaa4512013-03-06 23:39:39 -0600915 }
Alex Elder6aaa4512013-03-06 23:39:39 -0600916
Alex Elder25aff7c2013-03-11 23:34:22 -0500917 if (!cursor->last_piece) {
918 BUG_ON(!cursor->resid);
919 BUG_ON(!bio);
920 /* A short read is OK, so use <= rather than == */
Kent Overstreetf38a5182013-08-07 14:30:24 -0700921 if (cursor->resid <= bio_iter_len(bio, cursor->bvec_iter))
Alex Elder6aaa4512013-03-06 23:39:39 -0600922 cursor->last_piece = true;
Alex Elder25aff7c2013-03-11 23:34:22 -0500923 }
Alex Elder6aaa4512013-03-06 23:39:39 -0600924
925 return true;
926}
Alex Elderea965712013-04-05 14:46:01 -0500927#endif /* CONFIG_BLOCK */
Alex Elderdf6ad1f2012-06-11 14:57:13 -0500928
Alex Elderfe38a2b2013-03-06 23:39:39 -0600929/*
Alex Eldere766d7b2013-03-07 15:38:28 -0600930 * For a page array, a piece comes from the first page in the array
931 * that has not already been fully consumed.
932 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500933static void ceph_msg_data_pages_cursor_init(struct ceph_msg_data_cursor *cursor,
Alex Elder25aff7c2013-03-11 23:34:22 -0500934 size_t length)
Alex Eldere766d7b2013-03-07 15:38:28 -0600935{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500936 struct ceph_msg_data *data = cursor->data;
Alex Eldere766d7b2013-03-07 15:38:28 -0600937 int page_count;
938
939 BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
940
941 BUG_ON(!data->pages);
942 BUG_ON(!data->length);
943
Alex Elderca8b3a62013-04-05 14:46:01 -0500944 cursor->resid = min(length, data->length);
Alex Eldere766d7b2013-03-07 15:38:28 -0600945 page_count = calc_pages_for(data->alignment, (u64)data->length);
Alex Eldere766d7b2013-03-07 15:38:28 -0600946 cursor->page_offset = data->alignment & ~PAGE_MASK;
947 cursor->page_index = 0;
Alex Elder56fc5652013-03-30 23:46:55 -0500948 BUG_ON(page_count > (int)USHRT_MAX);
949 cursor->page_count = (unsigned short)page_count;
950 BUG_ON(length > SIZE_MAX - cursor->page_offset);
Ilya Dryomov5f740d72014-08-08 12:43:39 +0400951 cursor->last_piece = cursor->page_offset + cursor->resid <= PAGE_SIZE;
Alex Eldere766d7b2013-03-07 15:38:28 -0600952}
953
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500954static struct page *
955ceph_msg_data_pages_next(struct ceph_msg_data_cursor *cursor,
956 size_t *page_offset, size_t *length)
Alex Eldere766d7b2013-03-07 15:38:28 -0600957{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500958 struct ceph_msg_data *data = cursor->data;
Alex Eldere766d7b2013-03-07 15:38:28 -0600959
960 BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
961
962 BUG_ON(cursor->page_index >= cursor->page_count);
963 BUG_ON(cursor->page_offset >= PAGE_SIZE);
Alex Eldere766d7b2013-03-07 15:38:28 -0600964
965 *page_offset = cursor->page_offset;
Alex Elder25aff7c2013-03-11 23:34:22 -0500966 if (cursor->last_piece)
Alex Eldere766d7b2013-03-07 15:38:28 -0600967 *length = cursor->resid;
Alex Elder25aff7c2013-03-11 23:34:22 -0500968 else
Alex Eldere766d7b2013-03-07 15:38:28 -0600969 *length = PAGE_SIZE - *page_offset;
Alex Eldere766d7b2013-03-07 15:38:28 -0600970
971 return data->pages[cursor->page_index];
972}
973
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500974static bool ceph_msg_data_pages_advance(struct ceph_msg_data_cursor *cursor,
Alex Eldere766d7b2013-03-07 15:38:28 -0600975 size_t bytes)
976{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500977 BUG_ON(cursor->data->type != CEPH_MSG_DATA_PAGES);
Alex Eldere766d7b2013-03-07 15:38:28 -0600978
979 BUG_ON(cursor->page_offset + bytes > PAGE_SIZE);
Alex Eldere766d7b2013-03-07 15:38:28 -0600980
981 /* Advance the cursor page offset */
982
983 cursor->resid -= bytes;
Alex Elder5df521b2013-03-30 15:09:59 -0500984 cursor->page_offset = (cursor->page_offset + bytes) & ~PAGE_MASK;
985 if (!bytes || cursor->page_offset)
Alex Eldere766d7b2013-03-07 15:38:28 -0600986 return false; /* more bytes to process in the current page */
987
Yan, Zhengd90deda2014-03-23 06:50:39 +0800988 if (!cursor->resid)
989 return false; /* no more data */
990
Alex Elder5df521b2013-03-30 15:09:59 -0500991 /* Move on to the next page; offset is already at 0 */
Alex Eldere766d7b2013-03-07 15:38:28 -0600992
993 BUG_ON(cursor->page_index >= cursor->page_count);
Alex Eldere766d7b2013-03-07 15:38:28 -0600994 cursor->page_index++;
Alex Elder25aff7c2013-03-11 23:34:22 -0500995 cursor->last_piece = cursor->resid <= PAGE_SIZE;
Alex Eldere766d7b2013-03-07 15:38:28 -0600996
997 return true;
998}
999
1000/*
Alex Elderdd236fc2013-03-06 23:39:39 -06001001 * For a pagelist, a piece is whatever remains to be consumed in the
1002 * first page in the list, or the front of the next page.
Alex Elderfe38a2b2013-03-06 23:39:39 -06001003 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001004static void
1005ceph_msg_data_pagelist_cursor_init(struct ceph_msg_data_cursor *cursor,
Alex Elder25aff7c2013-03-11 23:34:22 -05001006 size_t length)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001007{
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001008 struct ceph_msg_data *data = cursor->data;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001009 struct ceph_pagelist *pagelist;
1010 struct page *page;
1011
Alex Elderdd236fc2013-03-06 23:39:39 -06001012 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001013
1014 pagelist = data->pagelist;
1015 BUG_ON(!pagelist);
Alex Elder25aff7c2013-03-11 23:34:22 -05001016
1017 if (!length)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001018 return; /* pagelist can be assigned but empty */
1019
1020 BUG_ON(list_empty(&pagelist->head));
1021 page = list_first_entry(&pagelist->head, struct page, lru);
1022
Alex Elderca8b3a62013-04-05 14:46:01 -05001023 cursor->resid = min(length, pagelist->length);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001024 cursor->page = page;
1025 cursor->offset = 0;
Alex Eldera51b272e2013-04-19 15:34:49 -05001026 cursor->last_piece = cursor->resid <= PAGE_SIZE;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001027}
1028
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001029static struct page *
1030ceph_msg_data_pagelist_next(struct ceph_msg_data_cursor *cursor,
1031 size_t *page_offset, size_t *length)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001032{
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001033 struct ceph_msg_data *data = cursor->data;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001034 struct ceph_pagelist *pagelist;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001035
1036 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1037
1038 pagelist = data->pagelist;
1039 BUG_ON(!pagelist);
1040
1041 BUG_ON(!cursor->page);
Alex Elder25aff7c2013-03-11 23:34:22 -05001042 BUG_ON(cursor->offset + cursor->resid != pagelist->length);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001043
Alex Elder5df521b2013-03-30 15:09:59 -05001044 /* offset of first page in pagelist is always 0 */
Alex Elderfe38a2b2013-03-06 23:39:39 -06001045 *page_offset = cursor->offset & ~PAGE_MASK;
Alex Elder5df521b2013-03-30 15:09:59 -05001046 if (cursor->last_piece)
Alex Elder25aff7c2013-03-11 23:34:22 -05001047 *length = cursor->resid;
1048 else
1049 *length = PAGE_SIZE - *page_offset;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001050
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001051 return cursor->page;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001052}
1053
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001054static bool ceph_msg_data_pagelist_advance(struct ceph_msg_data_cursor *cursor,
Alex Elderdd236fc2013-03-06 23:39:39 -06001055 size_t bytes)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001056{
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001057 struct ceph_msg_data *data = cursor->data;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001058 struct ceph_pagelist *pagelist;
1059
1060 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1061
1062 pagelist = data->pagelist;
1063 BUG_ON(!pagelist);
Alex Elder25aff7c2013-03-11 23:34:22 -05001064
1065 BUG_ON(cursor->offset + cursor->resid != pagelist->length);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001066 BUG_ON((cursor->offset & ~PAGE_MASK) + bytes > PAGE_SIZE);
1067
1068 /* Advance the cursor offset */
1069
Alex Elder25aff7c2013-03-11 23:34:22 -05001070 cursor->resid -= bytes;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001071 cursor->offset += bytes;
Alex Elder5df521b2013-03-30 15:09:59 -05001072 /* offset of first page in pagelist is always 0 */
Alex Elderfe38a2b2013-03-06 23:39:39 -06001073 if (!bytes || cursor->offset & ~PAGE_MASK)
1074 return false; /* more bytes to process in the current page */
1075
Yan, Zhengd90deda2014-03-23 06:50:39 +08001076 if (!cursor->resid)
1077 return false; /* no more data */
1078
Alex Elderfe38a2b2013-03-06 23:39:39 -06001079 /* Move on to the next page */
1080
1081 BUG_ON(list_is_last(&cursor->page->lru, &pagelist->head));
Geliang Tang17ddc492015-11-16 21:46:32 +08001082 cursor->page = list_next_entry(cursor->page, lru);
Alex Elder25aff7c2013-03-11 23:34:22 -05001083 cursor->last_piece = cursor->resid <= PAGE_SIZE;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001084
1085 return true;
1086}
1087
Alex Elderdd236fc2013-03-06 23:39:39 -06001088/*
1089 * Message data is handled (sent or received) in pieces, where each
1090 * piece resides on a single page. The network layer might not
1091 * consume an entire piece at once. A data item's cursor keeps
1092 * track of which piece is next to process and how much remains to
1093 * be processed in that piece. It also tracks whether the current
1094 * piece is the last one in the data item.
1095 */
Alex Elderca8b3a62013-04-05 14:46:01 -05001096static void __ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor)
Alex Elderdd236fc2013-03-06 23:39:39 -06001097{
Alex Elderca8b3a62013-04-05 14:46:01 -05001098 size_t length = cursor->total_resid;
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001099
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001100 switch (cursor->data->type) {
Alex Elderdd236fc2013-03-06 23:39:39 -06001101 case CEPH_MSG_DATA_PAGELIST:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001102 ceph_msg_data_pagelist_cursor_init(cursor, length);
Alex Elderdd236fc2013-03-06 23:39:39 -06001103 break;
Alex Eldere766d7b2013-03-07 15:38:28 -06001104 case CEPH_MSG_DATA_PAGES:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001105 ceph_msg_data_pages_cursor_init(cursor, length);
Alex Eldere766d7b2013-03-07 15:38:28 -06001106 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001107#ifdef CONFIG_BLOCK
1108 case CEPH_MSG_DATA_BIO:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001109 ceph_msg_data_bio_cursor_init(cursor, length);
Alex Elder6aaa4512013-03-06 23:39:39 -06001110 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001111#endif /* CONFIG_BLOCK */
Alex Elder6aaa4512013-03-06 23:39:39 -06001112 case CEPH_MSG_DATA_NONE:
Alex Elderdd236fc2013-03-06 23:39:39 -06001113 default:
1114 /* BUG(); */
1115 break;
1116 }
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001117 cursor->need_crc = true;
Alex Elderdd236fc2013-03-06 23:39:39 -06001118}
1119
Alex Elderca8b3a62013-04-05 14:46:01 -05001120static void ceph_msg_data_cursor_init(struct ceph_msg *msg, size_t length)
1121{
1122 struct ceph_msg_data_cursor *cursor = &msg->cursor;
1123 struct ceph_msg_data *data;
1124
1125 BUG_ON(!length);
1126 BUG_ON(length > msg->data_length);
1127 BUG_ON(list_empty(&msg->data));
1128
Alex Elderca8b3a62013-04-05 14:46:01 -05001129 cursor->data_head = &msg->data;
1130 cursor->total_resid = length;
1131 data = list_first_entry(&msg->data, struct ceph_msg_data, links);
1132 cursor->data = data;
1133
1134 __ceph_msg_data_cursor_init(cursor);
1135}
1136
Alex Elderdd236fc2013-03-06 23:39:39 -06001137/*
1138 * Return the page containing the next piece to process for a given
1139 * data item, and supply the page offset and length of that piece.
1140 * Indicate whether this is the last piece in this data item.
1141 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001142static struct page *ceph_msg_data_next(struct ceph_msg_data_cursor *cursor,
1143 size_t *page_offset, size_t *length,
Alex Elderdd236fc2013-03-06 23:39:39 -06001144 bool *last_piece)
1145{
1146 struct page *page;
1147
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001148 switch (cursor->data->type) {
Alex Elderdd236fc2013-03-06 23:39:39 -06001149 case CEPH_MSG_DATA_PAGELIST:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001150 page = ceph_msg_data_pagelist_next(cursor, page_offset, length);
Alex Elderdd236fc2013-03-06 23:39:39 -06001151 break;
Alex Eldere766d7b2013-03-07 15:38:28 -06001152 case CEPH_MSG_DATA_PAGES:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001153 page = ceph_msg_data_pages_next(cursor, page_offset, length);
Alex Eldere766d7b2013-03-07 15:38:28 -06001154 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001155#ifdef CONFIG_BLOCK
1156 case CEPH_MSG_DATA_BIO:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001157 page = ceph_msg_data_bio_next(cursor, page_offset, length);
Alex Elder6aaa4512013-03-06 23:39:39 -06001158 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001159#endif /* CONFIG_BLOCK */
Alex Elder6aaa4512013-03-06 23:39:39 -06001160 case CEPH_MSG_DATA_NONE:
Alex Elderdd236fc2013-03-06 23:39:39 -06001161 default:
1162 page = NULL;
1163 break;
1164 }
1165 BUG_ON(!page);
1166 BUG_ON(*page_offset + *length > PAGE_SIZE);
1167 BUG_ON(!*length);
1168 if (last_piece)
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001169 *last_piece = cursor->last_piece;
Alex Elderdd236fc2013-03-06 23:39:39 -06001170
1171 return page;
1172}
1173
1174/*
1175 * Returns true if the result moves the cursor on to the next piece
1176 * of the data item.
1177 */
Ilya Dryomov1759f7b2017-05-19 11:38:17 +02001178static void ceph_msg_data_advance(struct ceph_msg_data_cursor *cursor,
1179 size_t bytes)
Alex Elderdd236fc2013-03-06 23:39:39 -06001180{
1181 bool new_piece;
1182
Alex Elder25aff7c2013-03-11 23:34:22 -05001183 BUG_ON(bytes > cursor->resid);
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001184 switch (cursor->data->type) {
Alex Elderdd236fc2013-03-06 23:39:39 -06001185 case CEPH_MSG_DATA_PAGELIST:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001186 new_piece = ceph_msg_data_pagelist_advance(cursor, bytes);
Alex Elderdd236fc2013-03-06 23:39:39 -06001187 break;
Alex Eldere766d7b2013-03-07 15:38:28 -06001188 case CEPH_MSG_DATA_PAGES:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001189 new_piece = ceph_msg_data_pages_advance(cursor, bytes);
Alex Eldere766d7b2013-03-07 15:38:28 -06001190 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001191#ifdef CONFIG_BLOCK
1192 case CEPH_MSG_DATA_BIO:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001193 new_piece = ceph_msg_data_bio_advance(cursor, bytes);
Alex Elder6aaa4512013-03-06 23:39:39 -06001194 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001195#endif /* CONFIG_BLOCK */
Alex Elder6aaa4512013-03-06 23:39:39 -06001196 case CEPH_MSG_DATA_NONE:
Alex Elderdd236fc2013-03-06 23:39:39 -06001197 default:
1198 BUG();
1199 break;
1200 }
Alex Elderca8b3a62013-04-05 14:46:01 -05001201 cursor->total_resid -= bytes;
Alex Elderdd236fc2013-03-06 23:39:39 -06001202
Alex Elderca8b3a62013-04-05 14:46:01 -05001203 if (!cursor->resid && cursor->total_resid) {
1204 WARN_ON(!cursor->last_piece);
1205 BUG_ON(list_is_last(&cursor->data->links, cursor->data_head));
Geliang Tang17ddc492015-11-16 21:46:32 +08001206 cursor->data = list_next_entry(cursor->data, links);
Alex Elderca8b3a62013-04-05 14:46:01 -05001207 __ceph_msg_data_cursor_init(cursor);
Alex Eldera51b272e2013-04-19 15:34:49 -05001208 new_piece = true;
Alex Elderca8b3a62013-04-05 14:46:01 -05001209 }
Alex Eldera51b272e2013-04-19 15:34:49 -05001210 cursor->need_crc = new_piece;
Alex Elderdd236fc2013-03-06 23:39:39 -06001211}
1212
Ilya Dryomovdbc0d3c2016-02-19 11:38:57 +01001213static size_t sizeof_footer(struct ceph_connection *con)
1214{
1215 return (con->peer_features & CEPH_FEATURE_MSG_AUTH) ?
1216 sizeof(struct ceph_msg_footer) :
1217 sizeof(struct ceph_msg_footer_old);
1218}
1219
Alex Elder98fa5dd2013-04-02 12:09:50 -05001220static void prepare_message_data(struct ceph_msg *msg, u32 data_len)
Alex Elder739c9052012-06-11 14:57:13 -05001221{
Alex Elder739c9052012-06-11 14:57:13 -05001222 BUG_ON(!msg);
Alex Elder25aff7c2013-03-11 23:34:22 -05001223 BUG_ON(!data_len);
Alex Elder739c9052012-06-11 14:57:13 -05001224
Alex Elder4c59b4a2013-03-11 23:34:23 -05001225 /* Initialize data cursor */
Alex Elderfe38a2b2013-03-06 23:39:39 -06001226
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001227 ceph_msg_data_cursor_init(msg, (size_t)data_len);
Alex Elder739c9052012-06-11 14:57:13 -05001228}
1229
Sage Weil31b80062009-10-06 11:31:13 -07001230/*
1231 * Prepare footer for currently outgoing message, and finish things
1232 * off. Assumes out_kvec* are already valid.. we just add on to the end.
1233 */
Alex Elder859eb792012-02-14 14:05:33 -06001234static void prepare_write_message_footer(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001235{
1236 struct ceph_msg *m = con->out_msg;
1237
Alex Elderfd154f32012-06-11 14:57:13 -05001238 m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
1239
Sage Weil31b80062009-10-06 11:31:13 -07001240 dout("prepare_write_message_footer %p\n", con);
Ilya Dryomov89f08172016-02-20 15:56:07 +01001241 con_out_kvec_add(con, sizeof_footer(con), &m->footer);
Yan, Zheng33d07332014-11-04 16:33:37 +08001242 if (con->peer_features & CEPH_FEATURE_MSG_AUTH) {
1243 if (con->ops->sign_message)
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01001244 con->ops->sign_message(m);
Yan, Zheng33d07332014-11-04 16:33:37 +08001245 else
1246 m->footer.sig = 0;
Yan, Zheng33d07332014-11-04 16:33:37 +08001247 } else {
1248 m->old_footer.flags = m->footer.flags;
Yan, Zheng33d07332014-11-04 16:33:37 +08001249 }
Sage Weil31b80062009-10-06 11:31:13 -07001250 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -08001251 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -07001252}
1253
1254/*
1255 * Prepare headers for the next outgoing message.
1256 */
1257static void prepare_write_message(struct ceph_connection *con)
1258{
1259 struct ceph_msg *m;
Alex Eldera9a0c512012-02-15 07:43:54 -06001260 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001261
Alex Eldere2200422012-05-23 14:35:23 -05001262 con_out_kvec_reset(con);
Sage Weilc86a2932009-12-14 14:04:30 -08001263 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -07001264
1265 /* Sneak an ack in there first? If we can get it into the same
1266 * TCP packet that's a good thing. */
1267 if (con->in_seq > con->in_seq_acked) {
1268 con->in_seq_acked = con->in_seq;
Alex Eldere2200422012-05-23 14:35:23 -05001269 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001270 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -05001271 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -06001272 &con->out_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001273 }
1274
Alex Elder38941f82012-06-01 14:56:43 -05001275 BUG_ON(list_empty(&con->out_queue));
Alex Elder859eb792012-02-14 14:05:33 -06001276 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -08001277 con->out_msg = m;
Alex Elder38941f82012-06-01 14:56:43 -05001278 BUG_ON(m->con != con);
Sage Weil4cf9d542011-07-26 11:27:24 -07001279
1280 /* put message on sent list */
1281 ceph_msg_get(m);
1282 list_move_tail(&m->list_head, &con->out_sent);
Sage Weil31b80062009-10-06 11:31:13 -07001283
Sage Weile84346b2010-05-11 21:20:38 -07001284 /*
1285 * only assign outgoing seq # if we haven't sent this message
1286 * yet. if it is requeued, resend with it's original seq.
1287 */
1288 if (m->needs_out_seq) {
1289 m->hdr.seq = cpu_to_le64(++con->out_seq);
1290 m->needs_out_seq = false;
Ilya Dryomov98ad5eb2017-06-15 16:30:54 +02001291
Ilya Dryomov4690faf2017-07-26 09:59:15 +02001292 if (con->ops->reencode_message)
1293 con->ops->reencode_message(m);
1294 }
Sage Weil31b80062009-10-06 11:31:13 -07001295
Alex Elder98fa5dd2013-04-02 12:09:50 -05001296 dout("prepare_write_message %p seq %lld type %d len %d+%d+%zd\n",
Sage Weil31b80062009-10-06 11:31:13 -07001297 m, con->out_seq, le16_to_cpu(m->hdr.type),
1298 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
Alex Elder98fa5dd2013-04-02 12:09:50 -05001299 m->data_length);
Ilya Dryomov98ad5eb2017-06-15 16:30:54 +02001300 WARN_ON(m->front.iov_len != le32_to_cpu(m->hdr.front_len));
1301 WARN_ON(m->data_length != le32_to_cpu(m->hdr.data_len));
Sage Weil31b80062009-10-06 11:31:13 -07001302
1303 /* tag + hdr + front + middle */
Alex Eldere2200422012-05-23 14:35:23 -05001304 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
Ilya Dryomov67645d72015-12-28 13:18:34 +03001305 con_out_kvec_add(con, sizeof(con->out_hdr), &con->out_hdr);
Alex Eldere2200422012-05-23 14:35:23 -05001306 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
Alex Elder859eb792012-02-14 14:05:33 -06001307
Sage Weil31b80062009-10-06 11:31:13 -07001308 if (m->middle)
Alex Eldere2200422012-05-23 14:35:23 -05001309 con_out_kvec_add(con, m->middle->vec.iov_len,
Alex Elder859eb792012-02-14 14:05:33 -06001310 m->middle->vec.iov_base);
Sage Weil31b80062009-10-06 11:31:13 -07001311
Ilya Dryomov67645d72015-12-28 13:18:34 +03001312 /* fill in hdr crc and finalize hdr */
Alex Eldera9a0c512012-02-15 07:43:54 -06001313 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
1314 con->out_msg->hdr.crc = cpu_to_le32(crc);
Ilya Dryomov67645d72015-12-28 13:18:34 +03001315 memcpy(&con->out_hdr, &con->out_msg->hdr, sizeof(con->out_hdr));
Alex Eldera9a0c512012-02-15 07:43:54 -06001316
Ilya Dryomov67645d72015-12-28 13:18:34 +03001317 /* fill in front and middle crc, footer */
Alex Eldera9a0c512012-02-15 07:43:54 -06001318 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
1319 con->out_msg->footer.front_crc = cpu_to_le32(crc);
1320 if (m->middle) {
1321 crc = crc32c(0, m->middle->vec.iov_base,
1322 m->middle->vec.iov_len);
1323 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
1324 } else
Sage Weil31b80062009-10-06 11:31:13 -07001325 con->out_msg->footer.middle_crc = 0;
Alex Elder739c9052012-06-11 14:57:13 -05001326 dout("%s front_crc %u middle_crc %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -07001327 le32_to_cpu(con->out_msg->footer.front_crc),
1328 le32_to_cpu(con->out_msg->footer.middle_crc));
Ilya Dryomov67645d72015-12-28 13:18:34 +03001329 con->out_msg->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001330
1331 /* is there a data payload? */
Alex Elder739c9052012-06-11 14:57:13 -05001332 con->out_msg->footer.data_crc = 0;
Alex Elder98fa5dd2013-04-02 12:09:50 -05001333 if (m->data_length) {
1334 prepare_message_data(con->out_msg, m->data_length);
Alex Elder78625052013-03-06 23:39:39 -06001335 con->out_more = 1; /* data + footer will follow */
1336 } else {
Sage Weil31b80062009-10-06 11:31:13 -07001337 /* no, queue up footer too and be done */
Alex Elder859eb792012-02-14 14:05:33 -06001338 prepare_write_message_footer(con);
Alex Elder78625052013-03-06 23:39:39 -06001339 }
Sage Weil31b80062009-10-06 11:31:13 -07001340
Alex Elderc9ffc772013-02-20 10:25:12 -06001341 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07001342}
1343
1344/*
1345 * Prepare an ack.
1346 */
1347static void prepare_write_ack(struct ceph_connection *con)
1348{
1349 dout("prepare_write_ack %p %llu -> %llu\n", con,
1350 con->in_seq_acked, con->in_seq);
1351 con->in_seq_acked = con->in_seq;
1352
Alex Eldere2200422012-05-23 14:35:23 -05001353 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001354
Alex Eldere2200422012-05-23 14:35:23 -05001355 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Alex Elder859eb792012-02-14 14:05:33 -06001356
Sage Weil31b80062009-10-06 11:31:13 -07001357 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -05001358 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -06001359 &con->out_temp_ack);
1360
Sage Weil31b80062009-10-06 11:31:13 -07001361 con->out_more = 1; /* more will follow.. eventually.. */
Alex Elderc9ffc772013-02-20 10:25:12 -06001362 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07001363}
1364
1365/*
Sage Weil3a230832013-03-25 08:47:40 -07001366 * Prepare to share the seq during handshake
1367 */
1368static void prepare_write_seq(struct ceph_connection *con)
1369{
1370 dout("prepare_write_seq %p %llu -> %llu\n", con,
1371 con->in_seq_acked, con->in_seq);
1372 con->in_seq_acked = con->in_seq;
1373
1374 con_out_kvec_reset(con);
1375
1376 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
1377 con_out_kvec_add(con, sizeof (con->out_temp_ack),
1378 &con->out_temp_ack);
1379
1380 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1381}
1382
1383/*
Sage Weil31b80062009-10-06 11:31:13 -07001384 * Prepare to write keepalive byte.
1385 */
1386static void prepare_write_keepalive(struct ceph_connection *con)
1387{
1388 dout("prepare_write_keepalive %p\n", con);
Alex Eldere2200422012-05-23 14:35:23 -05001389 con_out_kvec_reset(con);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001390 if (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2) {
Deepa Dinamani1134e092017-05-08 15:59:19 -07001391 struct timespec now;
Ilya Dryomov7f61f542015-09-14 16:01:05 +03001392
Deepa Dinamani1134e092017-05-08 15:59:19 -07001393 ktime_get_real_ts(&now);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001394 con_out_kvec_add(con, sizeof(tag_keepalive2), &tag_keepalive2);
Ilya Dryomov7f61f542015-09-14 16:01:05 +03001395 ceph_encode_timespec(&con->out_temp_keepalive2, &now);
1396 con_out_kvec_add(con, sizeof(con->out_temp_keepalive2),
1397 &con->out_temp_keepalive2);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001398 } else {
1399 con_out_kvec_add(con, sizeof(tag_keepalive), &tag_keepalive);
1400 }
Alex Elderc9ffc772013-02-20 10:25:12 -06001401 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07001402}
1403
1404/*
1405 * Connection negotiation.
1406 */
1407
Alex Elderdac1e712012-05-16 15:16:39 -05001408static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
1409 int *auth_proto)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001410{
Alex Eldera3530df2012-05-16 15:16:39 -05001411 struct ceph_auth_handshake *auth;
Alex Elderb1c6b982012-05-16 15:16:38 -05001412
1413 if (!con->ops->get_authorizer) {
1414 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
1415 con->out_connect.authorizer_len = 0;
Alex Elder729796b2012-05-16 15:16:39 -05001416 return NULL;
Alex Elderb1c6b982012-05-16 15:16:38 -05001417 }
1418
Alex Elderdac1e712012-05-16 15:16:39 -05001419 auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
Alex Eldera3530df2012-05-16 15:16:39 -05001420 if (IS_ERR(auth))
Alex Elder729796b2012-05-16 15:16:39 -05001421 return auth;
Sage Weil0da5d702011-05-19 11:21:05 -07001422
Alex Elder8f43fb52012-05-16 15:16:39 -05001423 con->auth_reply_buf = auth->authorizer_reply_buf;
1424 con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
Alex Elder729796b2012-05-16 15:16:39 -05001425 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001426}
1427
Sage Weil31b80062009-10-06 11:31:13 -07001428/*
1429 * We connected to a peer and are saying hello.
1430 */
Alex Eldere825a662012-05-16 15:16:38 -05001431static void prepare_write_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001432{
Alex Eldere2200422012-05-23 14:35:23 -05001433 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
1434 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
Alex Eldere825a662012-05-16 15:16:38 -05001435 &con->msgr->my_enc_addr);
Sage Weileed0ef22009-11-10 14:34:36 -08001436
Sage Weileed0ef22009-11-10 14:34:36 -08001437 con->out_more = 0;
Alex Elderc9ffc772013-02-20 10:25:12 -06001438 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weileed0ef22009-11-10 14:34:36 -08001439}
1440
Alex Eldere825a662012-05-16 15:16:38 -05001441static int prepare_write_connect(struct ceph_connection *con)
Sage Weileed0ef22009-11-10 14:34:36 -08001442{
Eric Dumazet95c96172012-04-15 05:58:06 +00001443 unsigned int global_seq = get_global_seq(con->msgr, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001444 int proto;
Alex Elderdac1e712012-05-16 15:16:39 -05001445 int auth_proto;
Alex Elder729796b2012-05-16 15:16:39 -05001446 struct ceph_auth_handshake *auth;
Sage Weil31b80062009-10-06 11:31:13 -07001447
1448 switch (con->peer_name.type) {
1449 case CEPH_ENTITY_TYPE_MON:
1450 proto = CEPH_MONC_PROTOCOL;
1451 break;
1452 case CEPH_ENTITY_TYPE_OSD:
1453 proto = CEPH_OSDC_PROTOCOL;
1454 break;
1455 case CEPH_ENTITY_TYPE_MDS:
1456 proto = CEPH_MDSC_PROTOCOL;
1457 break;
1458 default:
1459 BUG();
1460 }
1461
1462 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
1463 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001464
Ilya Dryomov859bff52015-10-28 23:50:58 +01001465 con->out_connect.features =
1466 cpu_to_le64(from_msgr(con->msgr)->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -07001467 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
1468 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
1469 con->out_connect.global_seq = cpu_to_le32(global_seq);
1470 con->out_connect.protocol_version = cpu_to_le32(proto);
1471 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001472
Alex Elderdac1e712012-05-16 15:16:39 -05001473 auth_proto = CEPH_AUTH_UNKNOWN;
1474 auth = get_connect_authorizer(con, &auth_proto);
Alex Elder729796b2012-05-16 15:16:39 -05001475 if (IS_ERR(auth))
1476 return PTR_ERR(auth);
Alex Elder3da54772012-05-16 15:16:39 -05001477
Alex Elderdac1e712012-05-16 15:16:39 -05001478 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
Alex Elder3da54772012-05-16 15:16:39 -05001479 con->out_connect.authorizer_len = auth ?
1480 cpu_to_le32(auth->authorizer_buf_len) : 0;
1481
Alex Eldere2200422012-05-23 14:35:23 -05001482 con_out_kvec_add(con, sizeof (con->out_connect),
Alex Elder3da54772012-05-16 15:16:39 -05001483 &con->out_connect);
1484 if (auth && auth->authorizer_buf_len)
Alex Eldere2200422012-05-23 14:35:23 -05001485 con_out_kvec_add(con, auth->authorizer_buf_len,
Alex Elder3da54772012-05-16 15:16:39 -05001486 auth->authorizer_buf);
Alex Elder859eb792012-02-14 14:05:33 -06001487
Sage Weil31b80062009-10-06 11:31:13 -07001488 con->out_more = 0;
Alex Elderc9ffc772013-02-20 10:25:12 -06001489 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001490
Alex Eldere10c7582012-05-16 15:16:38 -05001491 return 0;
Sage Weil31b80062009-10-06 11:31:13 -07001492}
1493
Sage Weil31b80062009-10-06 11:31:13 -07001494/*
1495 * write as much of pending kvecs to the socket as we can.
1496 * 1 -> done
1497 * 0 -> socket full, but more to do
1498 * <0 -> error
1499 */
1500static int write_partial_kvec(struct ceph_connection *con)
1501{
1502 int ret;
1503
1504 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
1505 while (con->out_kvec_bytes > 0) {
1506 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
1507 con->out_kvec_left, con->out_kvec_bytes,
1508 con->out_more);
1509 if (ret <= 0)
1510 goto out;
1511 con->out_kvec_bytes -= ret;
1512 if (con->out_kvec_bytes == 0)
1513 break; /* done */
Alex Elderf42299e2012-02-15 07:43:54 -06001514
1515 /* account for full iov entries consumed */
1516 while (ret >= con->out_kvec_cur->iov_len) {
1517 BUG_ON(!con->out_kvec_left);
1518 ret -= con->out_kvec_cur->iov_len;
1519 con->out_kvec_cur++;
1520 con->out_kvec_left--;
1521 }
1522 /* and for a partially-consumed entry */
1523 if (ret) {
1524 con->out_kvec_cur->iov_len -= ret;
1525 con->out_kvec_cur->iov_base += ret;
Sage Weil31b80062009-10-06 11:31:13 -07001526 }
1527 }
1528 con->out_kvec_left = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001529 ret = 1;
1530out:
1531 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
1532 con->out_kvec_bytes, con->out_kvec_left, ret);
1533 return ret; /* done! */
1534}
1535
Alex Elder35b62802013-03-08 20:59:00 -06001536static u32 ceph_crc32c_page(u32 crc, struct page *page,
1537 unsigned int page_offset,
1538 unsigned int length)
1539{
1540 char *kaddr;
1541
1542 kaddr = kmap(page);
1543 BUG_ON(kaddr == NULL);
1544 crc = crc32c(crc, kaddr + page_offset, length);
1545 kunmap(page);
1546
1547 return crc;
1548}
Sage Weil31b80062009-10-06 11:31:13 -07001549/*
1550 * Write as much message data payload as we can. If we finish, queue
1551 * up the footer.
1552 * 1 -> done, footer is now queued in out_kvec[].
1553 * 0 -> socket full, but more to do
1554 * <0 -> error
1555 */
Alex Elder34d2d202013-03-08 20:58:59 -06001556static int write_partial_message_data(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001557{
1558 struct ceph_msg *msg = con->out_msg;
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001559 struct ceph_msg_data_cursor *cursor = &msg->cursor;
Ilya Dryomov859bff52015-10-28 23:50:58 +01001560 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
Alex Elderf5db90b2013-03-11 23:34:23 -05001561 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001562
Alex Elder859a35d2013-03-11 23:34:23 -05001563 dout("%s %p msg %p\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07001564
Alex Elder5240d9f2013-03-14 14:09:06 -05001565 if (list_empty(&msg->data))
Alex Elder4c59b4a2013-03-11 23:34:23 -05001566 return -EINVAL;
1567
Alex Elder5821bd82012-06-11 14:57:13 -05001568 /*
1569 * Iterate through each page that contains data to be
1570 * written, and send as much as possible for each.
1571 *
1572 * If we are calculating the data crc (the default), we will
1573 * need to map the page. If we have no pages, they have
1574 * been revoked, so use the zero page.
1575 */
Alex Elderf5db90b2013-03-11 23:34:23 -05001576 crc = do_datacrc ? le32_to_cpu(msg->footer.data_crc) : 0;
Alex Elder643c68a2013-03-11 23:34:23 -05001577 while (cursor->resid) {
Alex Elder8a166d02013-03-08 13:35:36 -06001578 struct page *page;
Alex Eldere387d522013-03-06 23:39:38 -06001579 size_t page_offset;
1580 size_t length;
Alex Elder8a166d02013-03-08 13:35:36 -06001581 bool last_piece;
Alex Elderf5db90b2013-03-11 23:34:23 -05001582 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001583
Shraddha Barke343128c2015-10-19 21:59:00 +05301584 page = ceph_msg_data_next(cursor, &page_offset, &length,
1585 &last_piece);
Alex Eldere387d522013-03-06 23:39:38 -06001586 ret = ceph_tcp_sendpage(con->sock, page, page_offset,
Benoît Canetc2cfa192015-06-25 20:32:34 +03001587 length, !last_piece);
Alex Elderf5db90b2013-03-11 23:34:23 -05001588 if (ret <= 0) {
1589 if (do_datacrc)
1590 msg->footer.data_crc = cpu_to_le32(crc);
Sage Weil31b80062009-10-06 11:31:13 -07001591
Alex Elderf5db90b2013-03-11 23:34:23 -05001592 return ret;
1593 }
Alex Elder143334f2013-03-29 11:44:10 -05001594 if (do_datacrc && cursor->need_crc)
1595 crc = ceph_crc32c_page(crc, page, page_offset, length);
Ilya Dryomov1759f7b2017-05-19 11:38:17 +02001596 ceph_msg_data_advance(cursor, (size_t)ret);
Sage Weil31b80062009-10-06 11:31:13 -07001597 }
1598
Alex Elder34d2d202013-03-08 20:58:59 -06001599 dout("%s %p msg %p done\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07001600
1601 /* prepare and queue up footer, too */
Alex Elderf5db90b2013-03-11 23:34:23 -05001602 if (do_datacrc)
1603 msg->footer.data_crc = cpu_to_le32(crc);
1604 else
Alex Elder84ca8fc2012-06-11 14:57:13 -05001605 msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
Alex Eldere2200422012-05-23 14:35:23 -05001606 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001607 prepare_write_message_footer(con);
Alex Elderf5db90b2013-03-11 23:34:23 -05001608
1609 return 1; /* must return > 0 to indicate success */
Sage Weil31b80062009-10-06 11:31:13 -07001610}
1611
1612/*
1613 * write some zeros
1614 */
1615static int write_partial_skip(struct ceph_connection *con)
1616{
1617 int ret;
1618
Ilya Dryomov67645d72015-12-28 13:18:34 +03001619 dout("%s %p %d left\n", __func__, con, con->out_skip);
Sage Weil31b80062009-10-06 11:31:13 -07001620 while (con->out_skip > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001621 size_t size = min(con->out_skip, (int) PAGE_SIZE);
Sage Weil31b80062009-10-06 11:31:13 -07001622
Alex Eldere1dcb122013-03-06 23:39:38 -06001623 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, true);
Sage Weil31b80062009-10-06 11:31:13 -07001624 if (ret <= 0)
1625 goto out;
1626 con->out_skip -= ret;
1627 }
1628 ret = 1;
1629out:
1630 return ret;
1631}
1632
1633/*
1634 * Prepare to read connection handshake, or an ack.
1635 */
Sage Weileed0ef22009-11-10 14:34:36 -08001636static void prepare_read_banner(struct ceph_connection *con)
1637{
1638 dout("prepare_read_banner %p\n", con);
1639 con->in_base_pos = 0;
1640}
1641
Sage Weil31b80062009-10-06 11:31:13 -07001642static void prepare_read_connect(struct ceph_connection *con)
1643{
1644 dout("prepare_read_connect %p\n", con);
1645 con->in_base_pos = 0;
1646}
1647
1648static void prepare_read_ack(struct ceph_connection *con)
1649{
1650 dout("prepare_read_ack %p\n", con);
1651 con->in_base_pos = 0;
1652}
1653
Sage Weil3a230832013-03-25 08:47:40 -07001654static void prepare_read_seq(struct ceph_connection *con)
1655{
1656 dout("prepare_read_seq %p\n", con);
1657 con->in_base_pos = 0;
1658 con->in_tag = CEPH_MSGR_TAG_SEQ;
1659}
1660
Sage Weil31b80062009-10-06 11:31:13 -07001661static void prepare_read_tag(struct ceph_connection *con)
1662{
1663 dout("prepare_read_tag %p\n", con);
1664 con->in_base_pos = 0;
1665 con->in_tag = CEPH_MSGR_TAG_READY;
1666}
1667
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001668static void prepare_read_keepalive_ack(struct ceph_connection *con)
1669{
1670 dout("prepare_read_keepalive_ack %p\n", con);
1671 con->in_base_pos = 0;
1672}
1673
Sage Weil31b80062009-10-06 11:31:13 -07001674/*
1675 * Prepare to read a message.
1676 */
1677static int prepare_read_message(struct ceph_connection *con)
1678{
1679 dout("prepare_read_message %p\n", con);
1680 BUG_ON(con->in_msg != NULL);
1681 con->in_base_pos = 0;
1682 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1683 return 0;
1684}
1685
1686
1687static int read_partial(struct ceph_connection *con,
Alex Elderfd516532012-05-10 10:29:50 -05001688 int end, int size, void *object)
Sage Weil31b80062009-10-06 11:31:13 -07001689{
Alex Eldere6cee712012-05-10 10:29:50 -05001690 while (con->in_base_pos < end) {
1691 int left = end - con->in_base_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001692 int have = size - left;
1693 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1694 if (ret <= 0)
1695 return ret;
1696 con->in_base_pos += ret;
1697 }
1698 return 1;
1699}
1700
1701
1702/*
1703 * Read all or part of the connect-side handshake on a new connection
1704 */
Sage Weileed0ef22009-11-10 14:34:36 -08001705static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001706{
Alex Elderfd516532012-05-10 10:29:50 -05001707 int size;
1708 int end;
1709 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001710
Sage Weileed0ef22009-11-10 14:34:36 -08001711 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07001712
1713 /* peer's banner */
Alex Elderfd516532012-05-10 10:29:50 -05001714 size = strlen(CEPH_BANNER);
1715 end = size;
1716 ret = read_partial(con, end, size, con->in_banner);
Sage Weil31b80062009-10-06 11:31:13 -07001717 if (ret <= 0)
1718 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001719
1720 size = sizeof (con->actual_peer_addr);
1721 end += size;
1722 ret = read_partial(con, end, size, &con->actual_peer_addr);
Sage Weil31b80062009-10-06 11:31:13 -07001723 if (ret <= 0)
1724 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001725
1726 size = sizeof (con->peer_addr_for_me);
1727 end += size;
1728 ret = read_partial(con, end, size, &con->peer_addr_for_me);
Sage Weil31b80062009-10-06 11:31:13 -07001729 if (ret <= 0)
1730 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001731
Sage Weileed0ef22009-11-10 14:34:36 -08001732out:
1733 return ret;
1734}
1735
1736static int read_partial_connect(struct ceph_connection *con)
1737{
Alex Elderfd516532012-05-10 10:29:50 -05001738 int size;
1739 int end;
1740 int ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001741
1742 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1743
Alex Elderfd516532012-05-10 10:29:50 -05001744 size = sizeof (con->in_reply);
1745 end = size;
1746 ret = read_partial(con, end, size, &con->in_reply);
Sage Weil31b80062009-10-06 11:31:13 -07001747 if (ret <= 0)
1748 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001749
1750 size = le32_to_cpu(con->in_reply.authorizer_len);
1751 end += size;
1752 ret = read_partial(con, end, size, con->auth_reply_buf);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001753 if (ret <= 0)
1754 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001755
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001756 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1757 con, (int)con->in_reply.tag,
1758 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001759 le32_to_cpu(con->in_reply.global_seq));
1760out:
1761 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001762
Sage Weil31b80062009-10-06 11:31:13 -07001763}
1764
1765/*
1766 * Verify the hello banner looks okay.
1767 */
1768static int verify_hello(struct ceph_connection *con)
1769{
1770 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001771 pr_err("connect to %s got bad banner\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001772 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001773 con->error_msg = "protocol error, bad banner";
1774 return -1;
1775 }
1776 return 0;
1777}
1778
1779static bool addr_is_blank(struct sockaddr_storage *ss)
1780{
Ilya Dryomovc44bd692015-07-09 13:57:52 +03001781 struct in_addr *addr = &((struct sockaddr_in *)ss)->sin_addr;
1782 struct in6_addr *addr6 = &((struct sockaddr_in6 *)ss)->sin6_addr;
1783
Sage Weil31b80062009-10-06 11:31:13 -07001784 switch (ss->ss_family) {
1785 case AF_INET:
Ilya Dryomovc44bd692015-07-09 13:57:52 +03001786 return addr->s_addr == htonl(INADDR_ANY);
Sage Weil31b80062009-10-06 11:31:13 -07001787 case AF_INET6:
Ilya Dryomovc44bd692015-07-09 13:57:52 +03001788 return ipv6_addr_any(addr6);
1789 default:
1790 return true;
Sage Weil31b80062009-10-06 11:31:13 -07001791 }
Sage Weil31b80062009-10-06 11:31:13 -07001792}
1793
1794static int addr_port(struct sockaddr_storage *ss)
1795{
1796 switch (ss->ss_family) {
1797 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001798 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -07001799 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001800 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -07001801 }
1802 return 0;
1803}
1804
1805static void addr_set_port(struct sockaddr_storage *ss, int p)
1806{
1807 switch (ss->ss_family) {
1808 case AF_INET:
1809 ((struct sockaddr_in *)ss)->sin_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001810 break;
Sage Weil31b80062009-10-06 11:31:13 -07001811 case AF_INET6:
1812 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001813 break;
Sage Weil31b80062009-10-06 11:31:13 -07001814 }
1815}
1816
1817/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001818 * Unlike other *_pton function semantics, zero indicates success.
1819 */
1820static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1821 char delim, const char **ipend)
1822{
Alex Elder99f0f3b2012-01-23 15:49:27 -06001823 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1824 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001825
1826 memset(ss, 0, sizeof(*ss));
1827
1828 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1829 ss->ss_family = AF_INET;
1830 return 0;
1831 }
1832
1833 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1834 ss->ss_family = AF_INET6;
1835 return 0;
1836 }
1837
1838 return -EINVAL;
1839}
1840
1841/*
1842 * Extract hostname string and resolve using kernel DNS facility.
1843 */
1844#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1845static int ceph_dns_resolve_name(const char *name, size_t namelen,
1846 struct sockaddr_storage *ss, char delim, const char **ipend)
1847{
1848 const char *end, *delim_p;
1849 char *colon_p, *ip_addr = NULL;
1850 int ip_len, ret;
1851
1852 /*
1853 * The end of the hostname occurs immediately preceding the delimiter or
1854 * the port marker (':') where the delimiter takes precedence.
1855 */
1856 delim_p = memchr(name, delim, namelen);
1857 colon_p = memchr(name, ':', namelen);
1858
1859 if (delim_p && colon_p)
1860 end = delim_p < colon_p ? delim_p : colon_p;
1861 else if (!delim_p && colon_p)
1862 end = colon_p;
1863 else {
1864 end = delim_p;
1865 if (!end) /* case: hostname:/ */
1866 end = name + namelen;
1867 }
1868
1869 if (end <= name)
1870 return -EINVAL;
1871
1872 /* do dns_resolve upcall */
1873 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1874 if (ip_len > 0)
1875 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1876 else
1877 ret = -ESRCH;
1878
1879 kfree(ip_addr);
1880
1881 *ipend = end;
1882
1883 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1884 ret, ret ? "failed" : ceph_pr_addr(ss));
1885
1886 return ret;
1887}
1888#else
1889static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1890 struct sockaddr_storage *ss, char delim, const char **ipend)
1891{
1892 return -EINVAL;
1893}
1894#endif
1895
1896/*
1897 * Parse a server name (IP or hostname). If a valid IP address is not found
1898 * then try to extract a hostname to resolve using userspace DNS upcall.
1899 */
1900static int ceph_parse_server_name(const char *name, size_t namelen,
1901 struct sockaddr_storage *ss, char delim, const char **ipend)
1902{
1903 int ret;
1904
1905 ret = ceph_pton(name, namelen, ss, delim, ipend);
1906 if (ret)
1907 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1908
1909 return ret;
1910}
1911
1912/*
Sage Weil31b80062009-10-06 11:31:13 -07001913 * Parse an ip[:port] list into an addr array. Use the default
1914 * monitor port if a port isn't specified.
1915 */
1916int ceph_parse_ips(const char *c, const char *end,
1917 struct ceph_entity_addr *addr,
1918 int max_count, int *count)
1919{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001920 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001921 const char *p = c;
1922
1923 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1924 for (i = 0; i < max_count; i++) {
1925 const char *ipend;
1926 struct sockaddr_storage *ss = &addr[i].in_addr;
Sage Weil31b80062009-10-06 11:31:13 -07001927 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001928 char delim = ',';
1929
1930 if (*p == '[') {
1931 delim = ']';
1932 p++;
1933 }
Sage Weil31b80062009-10-06 11:31:13 -07001934
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001935 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1936 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001937 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001938 ret = -EINVAL;
1939
Sage Weil31b80062009-10-06 11:31:13 -07001940 p = ipend;
1941
Sage Weil39139f62010-07-08 09:54:52 -07001942 if (delim == ']') {
1943 if (*p != ']') {
1944 dout("missing matching ']'\n");
1945 goto bad;
1946 }
1947 p++;
1948 }
1949
Sage Weil31b80062009-10-06 11:31:13 -07001950 /* port? */
1951 if (p < end && *p == ':') {
1952 port = 0;
1953 p++;
1954 while (p < end && *p >= '0' && *p <= '9') {
1955 port = (port * 10) + (*p - '0');
1956 p++;
1957 }
Ilya Dryomovf48db1e2013-12-30 19:21:29 +02001958 if (port == 0)
1959 port = CEPH_MON_PORT;
1960 else if (port > 65535)
Sage Weil31b80062009-10-06 11:31:13 -07001961 goto bad;
1962 } else {
1963 port = CEPH_MON_PORT;
1964 }
1965
1966 addr_set_port(ss, port);
1967
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001968 dout("parse_ips got %s\n", ceph_pr_addr(ss));
Sage Weil31b80062009-10-06 11:31:13 -07001969
1970 if (p == end)
1971 break;
1972 if (*p != ',')
1973 goto bad;
1974 p++;
1975 }
1976
1977 if (p != end)
1978 goto bad;
1979
1980 if (count)
1981 *count = i + 1;
1982 return 0;
1983
1984bad:
Sage Weil39139f62010-07-08 09:54:52 -07001985 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001986 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07001987}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001988EXPORT_SYMBOL(ceph_parse_ips);
Sage Weil31b80062009-10-06 11:31:13 -07001989
Sage Weileed0ef22009-11-10 14:34:36 -08001990static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001991{
Sage Weileed0ef22009-11-10 14:34:36 -08001992 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07001993
1994 if (verify_hello(con) < 0)
1995 return -1;
1996
Sage Weil63f2d212009-11-03 15:17:56 -08001997 ceph_decode_addr(&con->actual_peer_addr);
1998 ceph_decode_addr(&con->peer_addr_for_me);
1999
Sage Weil31b80062009-10-06 11:31:13 -07002000 /*
2001 * Make sure the other end is who we wanted. note that the other
2002 * end may not yet know their ip address, so if it's 0.0.0.0, give
2003 * them the benefit of the doubt.
2004 */
Sage Weil103e2d32010-01-07 16:12:36 -08002005 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
2006 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07002007 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
2008 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Joe Perchesb9a67892014-09-09 21:17:29 -07002009 pr_warn("wrong peer, want %s/%d, got %s/%d\n",
2010 ceph_pr_addr(&con->peer_addr.in_addr),
2011 (int)le32_to_cpu(con->peer_addr.nonce),
2012 ceph_pr_addr(&con->actual_peer_addr.in_addr),
2013 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08002014 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07002015 return -1;
2016 }
2017
2018 /*
2019 * did we learn our address?
2020 */
2021 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
2022 int port = addr_port(&con->msgr->inst.addr.in_addr);
2023
2024 memcpy(&con->msgr->inst.addr.in_addr,
2025 &con->peer_addr_for_me.in_addr,
2026 sizeof(con->peer_addr_for_me.in_addr));
2027 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08002028 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08002029 dout("process_banner learned my addr is %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002030 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002031 }
2032
Sage Weileed0ef22009-11-10 14:34:36 -08002033 return 0;
2034}
2035
2036static int process_connect(struct ceph_connection *con)
2037{
Ilya Dryomov859bff52015-10-28 23:50:58 +01002038 u64 sup_feat = from_msgr(con->msgr)->supported_features;
2039 u64 req_feat = from_msgr(con->msgr)->required_features;
Ilya Dryomovdcbbd972017-06-05 14:44:59 +02002040 u64 server_feat = le64_to_cpu(con->in_reply.features);
Sage Weil0da5d702011-05-19 11:21:05 -07002041 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08002042
Sage Weileed0ef22009-11-10 14:34:36 -08002043 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
2044
Ilya Dryomov5c056fd2016-12-02 16:35:09 +01002045 if (con->auth_reply_buf) {
2046 /*
2047 * Any connection that defines ->get_authorizer()
2048 * should also define ->verify_authorizer_reply().
2049 * See get_connect_authorizer().
2050 */
Ilya Dryomov0dde5842016-12-02 16:35:09 +01002051 ret = con->ops->verify_authorizer_reply(con);
Ilya Dryomov5c056fd2016-12-02 16:35:09 +01002052 if (ret < 0) {
2053 con->error_msg = "bad authorize reply";
2054 return ret;
2055 }
2056 }
2057
Sage Weil31b80062009-10-06 11:31:13 -07002058 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08002059 case CEPH_MSGR_TAG_FEATURES:
2060 pr_err("%s%lld %s feature set mismatch,"
2061 " my %llx < server's %llx, missing %llx\n",
2062 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002063 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08002064 sup_feat, server_feat, server_feat & ~sup_feat);
2065 con->error_msg = "missing required protocol features";
Sage Weil0fa6ebc2012-12-27 20:27:04 -06002066 reset_connection(con);
Sage Weil04a419f2009-12-23 09:30:21 -08002067 return -1;
2068
Sage Weil31b80062009-10-06 11:31:13 -07002069 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07002070 pr_err("%s%lld %s protocol version mismatch,"
2071 " my %d != server's %d\n",
2072 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002073 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -07002074 le32_to_cpu(con->out_connect.protocol_version),
2075 le32_to_cpu(con->in_reply.protocol_version));
2076 con->error_msg = "protocol version mismatch";
Sage Weil0fa6ebc2012-12-27 20:27:04 -06002077 reset_connection(con);
Sage Weil31b80062009-10-06 11:31:13 -07002078 return -1;
2079
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002080 case CEPH_MSGR_TAG_BADAUTHORIZER:
2081 con->auth_retry++;
2082 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
2083 con->auth_retry);
2084 if (con->auth_retry == 2) {
2085 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002086 return -1;
2087 }
Jim Schutt6d4221b2012-08-10 10:37:38 -07002088 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05002089 ret = prepare_write_connect(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002090 if (ret < 0)
2091 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07002092 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002093 break;
Sage Weil31b80062009-10-06 11:31:13 -07002094
2095 case CEPH_MSGR_TAG_RESETSESSION:
2096 /*
2097 * If we connected with a large connect_seq but the peer
2098 * has no record of a session with us (no connection, or
2099 * connect_seq == 0), they will send RESETSESION to indicate
2100 * that they must have reset their session, and may have
2101 * dropped messages.
2102 */
2103 dout("process_connect got RESET peer seq %u\n",
Sage Weil5bdca4e2012-07-10 11:53:34 -07002104 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil31b80062009-10-06 11:31:13 -07002105 pr_err("%s%lld %s connection reset\n",
2106 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002107 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002108 reset_connection(con);
Jim Schutt6d4221b2012-08-10 10:37:38 -07002109 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05002110 ret = prepare_write_connect(con);
2111 if (ret < 0)
2112 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002113 prepare_read_connect(con);
2114
2115 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08002116 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002117 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
2118 if (con->ops->peer_reset)
2119 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08002120 mutex_lock(&con->mutex);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002121 if (con->state != CON_STATE_NEGOTIATING)
Sage Weil0da5d702011-05-19 11:21:05 -07002122 return -EAGAIN;
Sage Weil31b80062009-10-06 11:31:13 -07002123 break;
2124
2125 case CEPH_MSGR_TAG_RETRY_SESSION:
2126 /*
2127 * If we sent a smaller connect_seq than the peer has, try
2128 * again with a larger value.
2129 */
Sage Weil5bdca4e2012-07-10 11:53:34 -07002130 dout("process_connect got RETRY_SESSION my seq %u, peer %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07002131 le32_to_cpu(con->out_connect.connect_seq),
Sage Weil5bdca4e2012-07-10 11:53:34 -07002132 le32_to_cpu(con->in_reply.connect_seq));
2133 con->connect_seq = le32_to_cpu(con->in_reply.connect_seq);
Jim Schutt6d4221b2012-08-10 10:37:38 -07002134 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05002135 ret = prepare_write_connect(con);
2136 if (ret < 0)
2137 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002138 prepare_read_connect(con);
2139 break;
2140
2141 case CEPH_MSGR_TAG_RETRY_GLOBAL:
2142 /*
2143 * If we sent a smaller global_seq than the peer has, try
2144 * again with a larger value.
2145 */
Sage Weileed0ef22009-11-10 14:34:36 -08002146 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07002147 con->peer_global_seq,
Sage Weil5bdca4e2012-07-10 11:53:34 -07002148 le32_to_cpu(con->in_reply.global_seq));
Sage Weil31b80062009-10-06 11:31:13 -07002149 get_global_seq(con->msgr,
Sage Weil5bdca4e2012-07-10 11:53:34 -07002150 le32_to_cpu(con->in_reply.global_seq));
Jim Schutt6d4221b2012-08-10 10:37:38 -07002151 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05002152 ret = prepare_write_connect(con);
2153 if (ret < 0)
2154 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002155 prepare_read_connect(con);
2156 break;
2157
Sage Weil3a230832013-03-25 08:47:40 -07002158 case CEPH_MSGR_TAG_SEQ:
Sage Weil31b80062009-10-06 11:31:13 -07002159 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08002160 if (req_feat & ~server_feat) {
2161 pr_err("%s%lld %s protocol feature mismatch,"
2162 " my required %llx > server's %llx, need %llx\n",
2163 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002164 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08002165 req_feat, server_feat, req_feat & ~server_feat);
2166 con->error_msg = "missing required protocol features";
Sage Weil0fa6ebc2012-12-27 20:27:04 -06002167 reset_connection(con);
Sage Weil04a419f2009-12-23 09:30:21 -08002168 return -1;
2169 }
Sage Weil8dacc7d2012-07-20 17:24:40 -07002170
Alex Elder122070a2012-12-26 10:43:57 -06002171 WARN_ON(con->state != CON_STATE_NEGOTIATING);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002172 con->state = CON_STATE_OPEN;
Sage Weil20e55c42013-03-25 09:30:13 -07002173 con->auth_retry = 0; /* we authenticated; clear flag */
Sage Weil31b80062009-10-06 11:31:13 -07002174 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
2175 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07002176 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07002177 dout("process_connect got READY gseq %d cseq %d (%d)\n",
2178 con->peer_global_seq,
2179 le32_to_cpu(con->in_reply.connect_seq),
2180 con->connect_seq);
2181 WARN_ON(con->connect_seq !=
2182 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08002183
2184 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
Alex Elderc9ffc772013-02-20 10:25:12 -06002185 con_flag_set(con, CON_FLAG_LOSSYTX);
Sage Weil92ac41d2009-12-14 14:56:56 -08002186
Sage Weil85effe12012-07-30 16:22:05 -07002187 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -07002188
Sage Weil3a230832013-03-25 08:47:40 -07002189 if (con->in_reply.tag == CEPH_MSGR_TAG_SEQ) {
2190 prepare_write_seq(con);
2191 prepare_read_seq(con);
2192 } else {
2193 prepare_read_tag(con);
2194 }
Sage Weil31b80062009-10-06 11:31:13 -07002195 break;
2196
2197 case CEPH_MSGR_TAG_WAIT:
2198 /*
2199 * If there is a connection race (we are opening
2200 * connections to each other), one of us may just have
2201 * to WAIT. This shouldn't happen if we are the
2202 * client.
2203 */
Sage Weil04177882011-05-12 15:33:17 -07002204 con->error_msg = "protocol error, got WAIT as client";
2205 return -1;
Sage Weil31b80062009-10-06 11:31:13 -07002206
2207 default:
Sage Weil31b80062009-10-06 11:31:13 -07002208 con->error_msg = "protocol error, garbage tag during connect";
2209 return -1;
2210 }
2211 return 0;
2212}
2213
2214
2215/*
2216 * read (part of) an ack
2217 */
2218static int read_partial_ack(struct ceph_connection *con)
2219{
Alex Elderfd516532012-05-10 10:29:50 -05002220 int size = sizeof (con->in_temp_ack);
2221 int end = size;
Sage Weil31b80062009-10-06 11:31:13 -07002222
Alex Elderfd516532012-05-10 10:29:50 -05002223 return read_partial(con, end, size, &con->in_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07002224}
2225
Sage Weil31b80062009-10-06 11:31:13 -07002226/*
2227 * We can finally discard anything that's been acked.
2228 */
2229static void process_ack(struct ceph_connection *con)
2230{
2231 struct ceph_msg *m;
2232 u64 ack = le64_to_cpu(con->in_temp_ack);
2233 u64 seq;
Yan, Zheng0a2ad542017-05-05 18:47:37 +08002234 bool reconnect = (con->in_tag == CEPH_MSGR_TAG_SEQ);
2235 struct list_head *list = reconnect ? &con->out_queue : &con->out_sent;
Sage Weil31b80062009-10-06 11:31:13 -07002236
Yan, Zheng0a2ad542017-05-05 18:47:37 +08002237 /*
2238 * In the reconnect case, con_fault() has requeued messages
2239 * in out_sent. We should cleanup old messages according to
2240 * the reconnect seq.
2241 */
2242 while (!list_empty(list)) {
2243 m = list_first_entry(list, struct ceph_msg, list_head);
2244 if (reconnect && m->needs_out_seq)
2245 break;
Sage Weil31b80062009-10-06 11:31:13 -07002246 seq = le64_to_cpu(m->hdr.seq);
2247 if (seq > ack)
2248 break;
2249 dout("got ack for seq %llu type %d at %p\n", seq,
2250 le16_to_cpu(m->hdr.type), m);
Sage Weil4cf9d542011-07-26 11:27:24 -07002251 m->ack_stamp = jiffies;
Sage Weil31b80062009-10-06 11:31:13 -07002252 ceph_msg_remove(m);
2253 }
Yan, Zheng0a2ad542017-05-05 18:47:37 +08002254
Sage Weil31b80062009-10-06 11:31:13 -07002255 prepare_read_tag(con);
2256}
2257
2258
Yehuda Sadeh24504182010-01-08 13:58:34 -08002259static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07002260 struct kvec *section,
2261 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002262{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002263 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07002264
Yehuda Sadeh24504182010-01-08 13:58:34 -08002265 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07002266
Yehuda Sadeh24504182010-01-08 13:58:34 -08002267 while (section->iov_len < sec_len) {
2268 BUG_ON(section->iov_base == NULL);
2269 left = sec_len - section->iov_len;
2270 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
2271 section->iov_len, left);
2272 if (ret <= 0)
2273 return ret;
2274 section->iov_len += ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002275 }
Alex Elderfe3ad592012-02-15 07:43:54 -06002276 if (section->iov_len == sec_len)
2277 *crc = crc32c(0, section->iov_base, section->iov_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002278
2279 return 1;
2280}
2281
Alex Elder34d2d202013-03-08 20:58:59 -06002282static int read_partial_msg_data(struct ceph_connection *con)
2283{
2284 struct ceph_msg *msg = con->in_msg;
Alex Elder8ae4f4f2013-03-14 14:09:06 -05002285 struct ceph_msg_data_cursor *cursor = &msg->cursor;
Ilya Dryomov859bff52015-10-28 23:50:58 +01002286 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
Alex Elder686be202013-03-11 23:34:23 -05002287 struct page *page;
2288 size_t page_offset;
2289 size_t length;
Alex Elderf5db90b2013-03-11 23:34:23 -05002290 u32 crc = 0;
Alex Elder34d2d202013-03-08 20:58:59 -06002291 int ret;
2292
2293 BUG_ON(!msg);
Alex Elder5240d9f2013-03-14 14:09:06 -05002294 if (list_empty(&msg->data))
Alex Elder4c59b4a2013-03-11 23:34:23 -05002295 return -EIO;
Alex Elder34d2d202013-03-08 20:58:59 -06002296
Alex Elderf5db90b2013-03-11 23:34:23 -05002297 if (do_datacrc)
2298 crc = con->in_data_crc;
Alex Elder643c68a2013-03-11 23:34:23 -05002299 while (cursor->resid) {
Shraddha Barke343128c2015-10-19 21:59:00 +05302300 page = ceph_msg_data_next(cursor, &page_offset, &length, NULL);
Alex Elder686be202013-03-11 23:34:23 -05002301 ret = ceph_tcp_recvpage(con->sock, page, page_offset, length);
Alex Elderf5db90b2013-03-11 23:34:23 -05002302 if (ret <= 0) {
2303 if (do_datacrc)
2304 con->in_data_crc = crc;
2305
Alex Elder686be202013-03-11 23:34:23 -05002306 return ret;
Alex Elderf5db90b2013-03-11 23:34:23 -05002307 }
Alex Elder686be202013-03-11 23:34:23 -05002308
2309 if (do_datacrc)
Alex Elderf5db90b2013-03-11 23:34:23 -05002310 crc = ceph_crc32c_page(crc, page, page_offset, ret);
Ilya Dryomov1759f7b2017-05-19 11:38:17 +02002311 ceph_msg_data_advance(cursor, (size_t)ret);
Alex Elder34d2d202013-03-08 20:58:59 -06002312 }
Alex Elderf5db90b2013-03-11 23:34:23 -05002313 if (do_datacrc)
2314 con->in_data_crc = crc;
Alex Elder34d2d202013-03-08 20:58:59 -06002315
2316 return 1; /* must return > 0 to indicate success */
2317}
2318
Sage Weil31b80062009-10-06 11:31:13 -07002319/*
2320 * read (part of) a message.
2321 */
Alex Elder686be202013-03-11 23:34:23 -05002322static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip);
2323
Sage Weil31b80062009-10-06 11:31:13 -07002324static int read_partial_message(struct ceph_connection *con)
2325{
2326 struct ceph_msg *m = con->in_msg;
Alex Elderfd516532012-05-10 10:29:50 -05002327 int size;
2328 int end;
Sage Weil31b80062009-10-06 11:31:13 -07002329 int ret;
Eric Dumazet95c96172012-04-15 05:58:06 +00002330 unsigned int front_len, middle_len, data_len;
Ilya Dryomov859bff52015-10-28 23:50:58 +01002331 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
Yan, Zheng33d07332014-11-04 16:33:37 +08002332 bool need_sign = (con->peer_features & CEPH_FEATURE_MSG_AUTH);
Sage Weilae187562010-04-22 07:47:01 -07002333 u64 seq;
Alex Elderfe3ad592012-02-15 07:43:54 -06002334 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07002335
2336 dout("read_partial_message con %p msg %p\n", con, m);
2337
2338 /* header */
Alex Elderfd516532012-05-10 10:29:50 -05002339 size = sizeof (con->in_hdr);
2340 end = size;
2341 ret = read_partial(con, end, size, &con->in_hdr);
Alex Elder57dac9d2012-05-10 10:29:50 -05002342 if (ret <= 0)
2343 return ret;
Alex Elderfe3ad592012-02-15 07:43:54 -06002344
2345 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
2346 if (cpu_to_le32(crc) != con->in_hdr.crc) {
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002347 pr_err("read_partial_message bad hdr crc %u != expected %u\n",
Alex Elderfe3ad592012-02-15 07:43:54 -06002348 crc, con->in_hdr.crc);
2349 return -EBADMSG;
2350 }
2351
Sage Weil31b80062009-10-06 11:31:13 -07002352 front_len = le32_to_cpu(con->in_hdr.front_len);
2353 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
2354 return -EIO;
2355 middle_len = le32_to_cpu(con->in_hdr.middle_len);
Alex Elder7b11ba32013-03-08 18:51:03 -06002356 if (middle_len > CEPH_MSG_MAX_MIDDLE_LEN)
Sage Weil31b80062009-10-06 11:31:13 -07002357 return -EIO;
2358 data_len = le32_to_cpu(con->in_hdr.data_len);
2359 if (data_len > CEPH_MSG_MAX_DATA_LEN)
2360 return -EIO;
2361
Sage Weilae187562010-04-22 07:47:01 -07002362 /* verify seq# */
2363 seq = le64_to_cpu(con->in_hdr.seq);
2364 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07002365 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07002366 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002367 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weilae187562010-04-22 07:47:01 -07002368 seq, con->in_seq + 1);
2369 con->in_base_pos = -front_len - middle_len - data_len -
Ilya Dryomovdbc0d3c2016-02-19 11:38:57 +01002370 sizeof_footer(con);
Sage Weilae187562010-04-22 07:47:01 -07002371 con->in_tag = CEPH_MSGR_TAG_READY;
Ilya Dryomove7a88e82016-02-17 20:04:08 +01002372 return 1;
Sage Weilae187562010-04-22 07:47:01 -07002373 } else if ((s64)seq - (s64)con->in_seq > 1) {
2374 pr_err("read_partial_message bad seq %lld expected %lld\n",
2375 seq, con->in_seq + 1);
2376 con->error_msg = "bad message sequence # for incoming message";
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002377 return -EBADE;
Sage Weilae187562010-04-22 07:47:01 -07002378 }
2379
Sage Weil31b80062009-10-06 11:31:13 -07002380 /* allocate message? */
2381 if (!con->in_msg) {
Sage Weil4740a622012-07-30 18:19:30 -07002382 int skip = 0;
2383
Sage Weil31b80062009-10-06 11:31:13 -07002384 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
Alex Elder6ebc8b32013-03-08 18:51:03 -06002385 front_len, data_len);
Sage Weil4740a622012-07-30 18:19:30 -07002386 ret = ceph_con_in_msg_alloc(con, &skip);
2387 if (ret < 0)
2388 return ret;
Alex Elderf759ebb2013-04-05 14:46:01 -05002389
2390 BUG_ON(!con->in_msg ^ skip);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002391 if (skip) {
Sage Weil31b80062009-10-06 11:31:13 -07002392 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07002393 dout("alloc_msg said skip message\n");
Sage Weil31b80062009-10-06 11:31:13 -07002394 con->in_base_pos = -front_len - middle_len - data_len -
Ilya Dryomovdbc0d3c2016-02-19 11:38:57 +01002395 sizeof_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07002396 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002397 con->in_seq++;
Ilya Dryomove7a88e82016-02-17 20:04:08 +01002398 return 1;
Sage Weil31b80062009-10-06 11:31:13 -07002399 }
Alex Elder38941f82012-06-01 14:56:43 -05002400
Sage Weil4740a622012-07-30 18:19:30 -07002401 BUG_ON(!con->in_msg);
Alex Elder38941f82012-06-01 14:56:43 -05002402 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07002403 m = con->in_msg;
2404 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002405 if (m->middle)
2406 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002407
Alex Elder78625052013-03-06 23:39:39 -06002408 /* prepare for data payload, if any */
Sage Weila4107022012-07-30 16:20:25 -07002409
Alex Elder78625052013-03-06 23:39:39 -06002410 if (data_len)
Alex Elder98fa5dd2013-04-02 12:09:50 -05002411 prepare_message_data(con->in_msg, data_len);
Sage Weil31b80062009-10-06 11:31:13 -07002412 }
2413
2414 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002415 ret = read_partial_message_section(con, &m->front, front_len,
2416 &con->in_front_crc);
2417 if (ret <= 0)
2418 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002419
2420 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002421 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07002422 ret = read_partial_message_section(con, &m->middle->vec,
2423 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002424 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07002425 if (ret <= 0)
2426 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002427 }
2428
2429 /* (page) data */
Alex Elder34d2d202013-03-08 20:58:59 -06002430 if (data_len) {
2431 ret = read_partial_msg_data(con);
2432 if (ret <= 0)
2433 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002434 }
2435
Sage Weil31b80062009-10-06 11:31:13 -07002436 /* footer */
Ilya Dryomov89f08172016-02-20 15:56:07 +01002437 size = sizeof_footer(con);
Alex Elderfd516532012-05-10 10:29:50 -05002438 end += size;
2439 ret = read_partial(con, end, size, &m->footer);
Alex Elder57dac9d2012-05-10 10:29:50 -05002440 if (ret <= 0)
2441 return ret;
2442
Yan, Zheng33d07332014-11-04 16:33:37 +08002443 if (!need_sign) {
2444 m->footer.flags = m->old_footer.flags;
2445 m->footer.sig = 0;
2446 }
2447
Sage Weil31b80062009-10-06 11:31:13 -07002448 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
2449 m, front_len, m->footer.front_crc, middle_len,
2450 m->footer.middle_crc, data_len, m->footer.data_crc);
2451
2452 /* crc ok? */
2453 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
2454 pr_err("read_partial_message %p front crc %u != exp. %u\n",
2455 m, con->in_front_crc, m->footer.front_crc);
2456 return -EBADMSG;
2457 }
2458 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
2459 pr_err("read_partial_message %p middle crc %u != exp %u\n",
2460 m, con->in_middle_crc, m->footer.middle_crc);
2461 return -EBADMSG;
2462 }
Alex Elderbca064d2012-02-15 07:43:54 -06002463 if (do_datacrc &&
Sage Weil31b80062009-10-06 11:31:13 -07002464 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
2465 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
2466 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
2467 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
2468 return -EBADMSG;
2469 }
2470
Yan, Zheng33d07332014-11-04 16:33:37 +08002471 if (need_sign && con->ops->check_message_signature &&
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01002472 con->ops->check_message_signature(m)) {
Yan, Zheng33d07332014-11-04 16:33:37 +08002473 pr_err("read_partial_message %p signature check failed\n", m);
2474 return -EBADMSG;
2475 }
2476
Sage Weil31b80062009-10-06 11:31:13 -07002477 return 1; /* done! */
2478}
2479
2480/*
2481 * Process message. This happens in the worker thread. The callback should
2482 * be careful not to do anything that waits on other incoming messages or it
2483 * may deadlock.
2484 */
2485static void process_message(struct ceph_connection *con)
2486{
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01002487 struct ceph_msg *msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07002488
Alex Elder38941f82012-06-01 14:56:43 -05002489 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07002490 con->in_msg = NULL;
2491
2492 /* if first message, set peer_name */
2493 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07002494 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07002495
Sage Weil31b80062009-10-06 11:31:13 -07002496 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08002497 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002498
2499 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
2500 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07002501 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07002502 le16_to_cpu(msg->hdr.type),
2503 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2504 le32_to_cpu(msg->hdr.front_len),
2505 le32_to_cpu(msg->hdr.data_len),
2506 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
2507 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08002508
2509 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002510}
2511
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002512static int read_keepalive_ack(struct ceph_connection *con)
2513{
2514 struct ceph_timespec ceph_ts;
2515 size_t size = sizeof(ceph_ts);
2516 int ret = read_partial(con, size, size, &ceph_ts);
2517 if (ret <= 0)
2518 return ret;
2519 ceph_decode_timespec(&con->last_keepalive_ack, &ceph_ts);
2520 prepare_read_tag(con);
2521 return 1;
2522}
Sage Weil31b80062009-10-06 11:31:13 -07002523
2524/*
2525 * Write something to the socket. Called in a worker thread when the
2526 * socket appears to be writeable and we have something ready to send.
2527 */
2528static int try_write(struct ceph_connection *con)
2529{
Sage Weil31b80062009-10-06 11:31:13 -07002530 int ret = 1;
2531
Sage Weild59315c2012-06-21 12:49:23 -07002532 dout("try_write start %p state %lu\n", con, con->state);
Sage Weil31b80062009-10-06 11:31:13 -07002533
Sage Weil31b80062009-10-06 11:31:13 -07002534more:
2535 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
2536
2537 /* open the socket first? */
Sage Weil8dacc7d2012-07-20 17:24:40 -07002538 if (con->state == CON_STATE_PREOPEN) {
2539 BUG_ON(con->sock);
2540 con->state = CON_STATE_CONNECTING;
Alex Eldera5988c42012-05-29 11:04:58 -05002541
Alex Eldere2200422012-05-23 14:35:23 -05002542 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05002543 prepare_write_banner(con);
Sage Weileed0ef22009-11-10 14:34:36 -08002544 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07002545
Sage Weilcf3e5c42009-12-11 09:48:05 -08002546 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002547 con->in_tag = CEPH_MSGR_TAG_READY;
2548 dout("try_write initiating connect on %p new state %lu\n",
2549 con, con->state);
Alex Elder41617d02012-02-14 14:05:33 -06002550 ret = ceph_tcp_connect(con);
2551 if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -07002552 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -07002553 goto out;
2554 }
2555 }
2556
2557more_kvec:
2558 /* kvec data queued? */
Ilya Dryomov67645d72015-12-28 13:18:34 +03002559 if (con->out_kvec_left) {
2560 ret = write_partial_kvec(con);
Sage Weil31b80062009-10-06 11:31:13 -07002561 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08002562 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002563 }
Ilya Dryomov67645d72015-12-28 13:18:34 +03002564 if (con->out_skip) {
2565 ret = write_partial_skip(con);
Sage Weil31b80062009-10-06 11:31:13 -07002566 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08002567 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002568 }
2569
2570 /* msg pages? */
2571 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08002572 if (con->out_msg_done) {
2573 ceph_msg_put(con->out_msg);
2574 con->out_msg = NULL; /* we're done with this one */
2575 goto do_next;
2576 }
2577
Alex Elder34d2d202013-03-08 20:58:59 -06002578 ret = write_partial_message_data(con);
Sage Weil31b80062009-10-06 11:31:13 -07002579 if (ret == 1)
2580 goto more_kvec; /* we need to send the footer, too! */
2581 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08002582 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002583 if (ret < 0) {
Alex Elder34d2d202013-03-08 20:58:59 -06002584 dout("try_write write_partial_message_data err %d\n",
Sage Weil31b80062009-10-06 11:31:13 -07002585 ret);
Sage Weil42961d22011-01-25 08:19:34 -08002586 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002587 }
2588 }
2589
Sage Weilc86a2932009-12-14 14:04:30 -08002590do_next:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002591 if (con->state == CON_STATE_OPEN) {
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002592 if (con_flag_test_and_clear(con, CON_FLAG_KEEPALIVE_PENDING)) {
2593 prepare_write_keepalive(con);
2594 goto more;
2595 }
Sage Weil31b80062009-10-06 11:31:13 -07002596 /* is anything else pending? */
2597 if (!list_empty(&con->out_queue)) {
2598 prepare_write_message(con);
2599 goto more;
2600 }
2601 if (con->in_seq > con->in_seq_acked) {
2602 prepare_write_ack(con);
2603 goto more;
2604 }
Sage Weil31b80062009-10-06 11:31:13 -07002605 }
2606
2607 /* Nothing to do! */
Alex Elderc9ffc772013-02-20 10:25:12 -06002608 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07002609 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07002610 ret = 0;
2611out:
Sage Weil42961d22011-01-25 08:19:34 -08002612 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002613 return ret;
2614}
2615
2616
2617
2618/*
2619 * Read what we can from the socket.
2620 */
2621static int try_read(struct ceph_connection *con)
2622{
Sage Weil31b80062009-10-06 11:31:13 -07002623 int ret = -1;
2624
Sage Weil31b80062009-10-06 11:31:13 -07002625more:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002626 dout("try_read start on %p state %lu\n", con, con->state);
2627 if (con->state != CON_STATE_CONNECTING &&
2628 con->state != CON_STATE_NEGOTIATING &&
2629 con->state != CON_STATE_OPEN)
2630 return 0;
2631
2632 BUG_ON(!con->sock);
2633
Sage Weil31b80062009-10-06 11:31:13 -07002634 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2635 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07002636
Sage Weil8dacc7d2012-07-20 17:24:40 -07002637 if (con->state == CON_STATE_CONNECTING) {
Alex Elder7593af92012-05-24 11:55:03 -05002638 dout("try_read connecting\n");
2639 ret = read_partial_banner(con);
2640 if (ret <= 0)
Alex Elderab166d52012-05-31 11:37:29 -05002641 goto out;
Alex Elder7593af92012-05-24 11:55:03 -05002642 ret = process_banner(con);
2643 if (ret < 0)
2644 goto out;
2645
Sage Weil8dacc7d2012-07-20 17:24:40 -07002646 con->state = CON_STATE_NEGOTIATING;
Alex Elder7593af92012-05-24 11:55:03 -05002647
Jim Schutt6d4221b2012-08-10 10:37:38 -07002648 /*
2649 * Received banner is good, exchange connection info.
2650 * Do not reset out_kvec, as sending our banner raced
2651 * with receiving peer banner after connect completed.
2652 */
Alex Elder7593af92012-05-24 11:55:03 -05002653 ret = prepare_write_connect(con);
2654 if (ret < 0)
2655 goto out;
2656 prepare_read_connect(con);
2657
2658 /* Send connection info before awaiting response */
Sage Weil0da5d702011-05-19 11:21:05 -07002659 goto out;
2660 }
2661
Sage Weil8dacc7d2012-07-20 17:24:40 -07002662 if (con->state == CON_STATE_NEGOTIATING) {
Alex Elder7593af92012-05-24 11:55:03 -05002663 dout("try_read negotiating\n");
Sage Weil31b80062009-10-06 11:31:13 -07002664 ret = read_partial_connect(con);
2665 if (ret <= 0)
Sage Weil31b80062009-10-06 11:31:13 -07002666 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002667 ret = process_connect(con);
2668 if (ret < 0)
2669 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002670 goto more;
2671 }
2672
Alex Elder122070a2012-12-26 10:43:57 -06002673 WARN_ON(con->state != CON_STATE_OPEN);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002674
Sage Weil31b80062009-10-06 11:31:13 -07002675 if (con->in_base_pos < 0) {
2676 /*
2677 * skipping + discarding content.
2678 *
2679 * FIXME: there must be a better way to do this!
2680 */
Alex Elder84495f42012-02-15 07:43:55 -06002681 static char buf[SKIP_BUF_SIZE];
2682 int skip = min((int) sizeof (buf), -con->in_base_pos);
2683
Sage Weil31b80062009-10-06 11:31:13 -07002684 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2685 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2686 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002687 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002688 con->in_base_pos += ret;
2689 if (con->in_base_pos)
2690 goto more;
2691 }
2692 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2693 /*
2694 * what's next?
2695 */
2696 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2697 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002698 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002699 dout("try_read got tag %d\n", (int)con->in_tag);
2700 switch (con->in_tag) {
2701 case CEPH_MSGR_TAG_MSG:
2702 prepare_read_message(con);
2703 break;
2704 case CEPH_MSGR_TAG_ACK:
2705 prepare_read_ack(con);
2706 break;
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002707 case CEPH_MSGR_TAG_KEEPALIVE2_ACK:
2708 prepare_read_keepalive_ack(con);
2709 break;
Sage Weil31b80062009-10-06 11:31:13 -07002710 case CEPH_MSGR_TAG_CLOSE:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002711 con_close_socket(con);
2712 con->state = CON_STATE_CLOSED;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002713 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002714 default:
2715 goto bad_tag;
2716 }
2717 }
2718 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2719 ret = read_partial_message(con);
2720 if (ret <= 0) {
2721 switch (ret) {
2722 case -EBADMSG:
Ilya Dryomova51983e2015-10-28 23:52:06 +01002723 con->error_msg = "bad crc/signature";
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002724 /* fall through */
2725 case -EBADE:
Sage Weil31b80062009-10-06 11:31:13 -07002726 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002727 break;
Sage Weil31b80062009-10-06 11:31:13 -07002728 case -EIO:
2729 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08002730 break;
Sage Weil31b80062009-10-06 11:31:13 -07002731 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002732 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002733 }
2734 if (con->in_tag == CEPH_MSGR_TAG_READY)
2735 goto more;
2736 process_message(con);
Sage Weil7b862e02012-07-30 18:16:56 -07002737 if (con->state == CON_STATE_OPEN)
2738 prepare_read_tag(con);
Sage Weil31b80062009-10-06 11:31:13 -07002739 goto more;
2740 }
Sage Weil3a230832013-03-25 08:47:40 -07002741 if (con->in_tag == CEPH_MSGR_TAG_ACK ||
2742 con->in_tag == CEPH_MSGR_TAG_SEQ) {
2743 /*
2744 * the final handshake seq exchange is semantically
2745 * equivalent to an ACK
2746 */
Sage Weil31b80062009-10-06 11:31:13 -07002747 ret = read_partial_ack(con);
2748 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002749 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002750 process_ack(con);
2751 goto more;
2752 }
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002753 if (con->in_tag == CEPH_MSGR_TAG_KEEPALIVE2_ACK) {
2754 ret = read_keepalive_ack(con);
2755 if (ret <= 0)
2756 goto out;
2757 goto more;
2758 }
Sage Weil31b80062009-10-06 11:31:13 -07002759
Sage Weil31b80062009-10-06 11:31:13 -07002760out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002761 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002762 return ret;
2763
2764bad_tag:
2765 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2766 con->error_msg = "protocol error, garbage tag";
2767 ret = -1;
2768 goto out;
2769}
2770
2771
2772/*
Alex Elder802c6d92012-10-08 20:37:30 -07002773 * Atomically queue work on a connection after the specified delay.
2774 * Bump @con reference to avoid races with connection teardown.
2775 * Returns 0 if work was queued, or an error code otherwise.
Sage Weil31b80062009-10-06 11:31:13 -07002776 */
Alex Elder802c6d92012-10-08 20:37:30 -07002777static int queue_con_delay(struct ceph_connection *con, unsigned long delay)
Sage Weil31b80062009-10-06 11:31:13 -07002778{
Sage Weil31b80062009-10-06 11:31:13 -07002779 if (!con->ops->get(con)) {
Alex Elder802c6d92012-10-08 20:37:30 -07002780 dout("%s %p ref count 0\n", __func__, con);
Alex Elder802c6d92012-10-08 20:37:30 -07002781 return -ENOENT;
Sage Weil31b80062009-10-06 11:31:13 -07002782 }
2783
Alex Elder802c6d92012-10-08 20:37:30 -07002784 if (!queue_delayed_work(ceph_msgr_wq, &con->work, delay)) {
2785 dout("%s %p - already queued\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -07002786 con->ops->put(con);
Alex Elder802c6d92012-10-08 20:37:30 -07002787 return -EBUSY;
Sage Weil31b80062009-10-06 11:31:13 -07002788 }
Alex Elder802c6d92012-10-08 20:37:30 -07002789
2790 dout("%s %p %lu\n", __func__, con, delay);
Alex Elder802c6d92012-10-08 20:37:30 -07002791 return 0;
2792}
2793
2794static void queue_con(struct ceph_connection *con)
2795{
2796 (void) queue_con_delay(con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07002797}
2798
Ilya Dryomov37ab77a2014-06-24 16:21:45 +04002799static void cancel_con(struct ceph_connection *con)
2800{
2801 if (cancel_delayed_work(&con->work)) {
2802 dout("%s %p\n", __func__, con);
2803 con->ops->put(con);
2804 }
2805}
2806
Alex Elder7bb21d682012-12-07 19:50:07 -06002807static bool con_sock_closed(struct ceph_connection *con)
2808{
Alex Elderc9ffc772013-02-20 10:25:12 -06002809 if (!con_flag_test_and_clear(con, CON_FLAG_SOCK_CLOSED))
Alex Elder7bb21d682012-12-07 19:50:07 -06002810 return false;
2811
2812#define CASE(x) \
2813 case CON_STATE_ ## x: \
2814 con->error_msg = "socket closed (con state " #x ")"; \
2815 break;
2816
2817 switch (con->state) {
2818 CASE(CLOSED);
2819 CASE(PREOPEN);
2820 CASE(CONNECTING);
2821 CASE(NEGOTIATING);
2822 CASE(OPEN);
2823 CASE(STANDBY);
2824 default:
Joe Perchesb9a67892014-09-09 21:17:29 -07002825 pr_warn("%s con %p unrecognized state %lu\n",
Alex Elder7bb21d682012-12-07 19:50:07 -06002826 __func__, con, con->state);
2827 con->error_msg = "unrecognized con state";
2828 BUG();
2829 break;
2830 }
2831#undef CASE
2832
2833 return true;
2834}
2835
Alex Elderf20a39f2013-02-19 12:25:57 -06002836static bool con_backoff(struct ceph_connection *con)
2837{
2838 int ret;
2839
2840 if (!con_flag_test_and_clear(con, CON_FLAG_BACKOFF))
2841 return false;
2842
2843 ret = queue_con_delay(con, round_jiffies_relative(con->delay));
2844 if (ret) {
2845 dout("%s: con %p FAILED to back off %lu\n", __func__,
2846 con, con->delay);
2847 BUG_ON(ret == -ENOENT);
2848 con_flag_set(con, CON_FLAG_BACKOFF);
2849 }
2850
2851 return true;
2852}
2853
Alex Elder93209262013-02-19 12:25:57 -06002854/* Finish fault handling; con->mutex must *not* be held here */
2855
2856static void con_fault_finish(struct ceph_connection *con)
2857{
Ilya Dryomovf6330cc2016-01-13 14:32:57 +01002858 dout("%s %p\n", __func__, con);
2859
Alex Elder93209262013-02-19 12:25:57 -06002860 /*
2861 * in case we faulted due to authentication, invalidate our
2862 * current tickets so that we can get new ones.
2863 */
Ilya Dryomovf6330cc2016-01-13 14:32:57 +01002864 if (con->auth_retry) {
2865 dout("auth_retry %d, invalidating\n", con->auth_retry);
2866 if (con->ops->invalidate_authorizer)
2867 con->ops->invalidate_authorizer(con);
2868 con->auth_retry = 0;
Alex Elder93209262013-02-19 12:25:57 -06002869 }
2870
2871 if (con->ops->fault)
2872 con->ops->fault(con);
2873}
2874
Sage Weil31b80062009-10-06 11:31:13 -07002875/*
2876 * Do some work on a connection. Drop a connection ref when we're done.
2877 */
Ilya Dryomov68931622015-07-03 15:44:41 +03002878static void ceph_con_workfn(struct work_struct *work)
Sage Weil31b80062009-10-06 11:31:13 -07002879{
2880 struct ceph_connection *con = container_of(work, struct ceph_connection,
2881 work.work);
Alex Elder49659412013-02-19 12:25:57 -06002882 bool fault;
Sage Weil31b80062009-10-06 11:31:13 -07002883
Sage Weil9dd46582010-04-28 13:51:50 -07002884 mutex_lock(&con->mutex);
Alex Elder49659412013-02-19 12:25:57 -06002885 while (true) {
2886 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002887
Alex Elder49659412013-02-19 12:25:57 -06002888 if ((fault = con_sock_closed(con))) {
2889 dout("%s: con %p SOCK_CLOSED\n", __func__, con);
2890 break;
2891 }
2892 if (con_backoff(con)) {
2893 dout("%s: con %p BACKOFF\n", __func__, con);
2894 break;
2895 }
2896 if (con->state == CON_STATE_STANDBY) {
2897 dout("%s: con %p STANDBY\n", __func__, con);
2898 break;
2899 }
2900 if (con->state == CON_STATE_CLOSED) {
2901 dout("%s: con %p CLOSED\n", __func__, con);
2902 BUG_ON(con->sock);
2903 break;
2904 }
2905 if (con->state == CON_STATE_PREOPEN) {
2906 dout("%s: con %p PREOPEN\n", __func__, con);
2907 BUG_ON(con->sock);
2908 }
Sage Weil0da5d702011-05-19 11:21:05 -07002909
Alex Elder49659412013-02-19 12:25:57 -06002910 ret = try_read(con);
2911 if (ret < 0) {
2912 if (ret == -EAGAIN)
2913 continue;
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002914 if (!con->error_msg)
2915 con->error_msg = "socket error on read";
Alex Elder49659412013-02-19 12:25:57 -06002916 fault = true;
2917 break;
2918 }
2919
2920 ret = try_write(con);
2921 if (ret < 0) {
2922 if (ret == -EAGAIN)
2923 continue;
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002924 if (!con->error_msg)
2925 con->error_msg = "socket error on write";
Alex Elder49659412013-02-19 12:25:57 -06002926 fault = true;
2927 }
2928
2929 break; /* If we make it to here, we're done */
Sage Weil3a140a02012-07-30 16:24:21 -07002930 }
Alex Elderb6e7b6a2013-02-19 12:25:57 -06002931 if (fault)
2932 con_fault(con);
Sage Weil9dd46582010-04-28 13:51:50 -07002933 mutex_unlock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002934
Alex Elderb6e7b6a2013-02-19 12:25:57 -06002935 if (fault)
2936 con_fault_finish(con);
2937
2938 con->ops->put(con);
Sage Weil31b80062009-10-06 11:31:13 -07002939}
2940
Sage Weil31b80062009-10-06 11:31:13 -07002941/*
2942 * Generic error/fault handler. A retry mechanism is used with
2943 * exponential backoff
2944 */
Alex Elder93209262013-02-19 12:25:57 -06002945static void con_fault(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07002946{
Sage Weil31b80062009-10-06 11:31:13 -07002947 dout("fault %p state %lu to peer %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002948 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002949
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002950 pr_warn("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
2951 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
2952 con->error_msg = NULL;
2953
Alex Elder122070a2012-12-26 10:43:57 -06002954 WARN_ON(con->state != CON_STATE_CONNECTING &&
Sage Weil8dacc7d2012-07-20 17:24:40 -07002955 con->state != CON_STATE_NEGOTIATING &&
2956 con->state != CON_STATE_OPEN);
Sage Weilec302642009-12-22 10:43:42 -08002957
Sage Weil31b80062009-10-06 11:31:13 -07002958 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002959
Alex Elderc9ffc772013-02-20 10:25:12 -06002960 if (con_flag_test(con, CON_FLAG_LOSSYTX)) {
Sage Weil8dacc7d2012-07-20 17:24:40 -07002961 dout("fault on LOSSYTX channel, marking CLOSED\n");
2962 con->state = CON_STATE_CLOSED;
Alex Elder93209262013-02-19 12:25:57 -06002963 return;
Sage Weil3b5ede02012-07-20 15:22:53 -07002964 }
2965
Sage Weil5e095e82009-12-14 14:30:34 -08002966 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002967 BUG_ON(con->in_msg->con != con);
Sage Weil5e095e82009-12-14 14:30:34 -08002968 ceph_msg_put(con->in_msg);
2969 con->in_msg = NULL;
2970 }
Sage Weil31b80062009-10-06 11:31:13 -07002971
Sage Weile80a52d2010-02-25 12:40:45 -08002972 /* Requeue anything that hasn't been acked */
2973 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002974
Sage Weile76661d2011-03-03 10:10:15 -08002975 /* If there are no messages queued or keepalive pending, place
2976 * the connection in a STANDBY state */
2977 if (list_empty(&con->out_queue) &&
Alex Elderc9ffc772013-02-20 10:25:12 -06002978 !con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING)) {
Sage Weile00de342011-03-04 12:25:05 -08002979 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
Alex Elderc9ffc772013-02-20 10:25:12 -06002980 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002981 con->state = CON_STATE_STANDBY;
Sage Weile80a52d2010-02-25 12:40:45 -08002982 } else {
2983 /* retry after a delay. */
Sage Weil8dacc7d2012-07-20 17:24:40 -07002984 con->state = CON_STATE_PREOPEN;
Sage Weile80a52d2010-02-25 12:40:45 -08002985 if (con->delay == 0)
2986 con->delay = BASE_DELAY_INTERVAL;
2987 else if (con->delay < MAX_DELAY_INTERVAL)
2988 con->delay *= 2;
Alex Elderc9ffc772013-02-20 10:25:12 -06002989 con_flag_set(con, CON_FLAG_BACKOFF);
Alex Elder8618e302012-10-08 20:37:30 -07002990 queue_con(con);
Sage Weil31b80062009-10-06 11:31:13 -07002991 }
Sage Weil31b80062009-10-06 11:31:13 -07002992}
2993
2994
2995
2996/*
Alex Elder15d98822012-05-26 23:26:43 -05002997 * initialize a new messenger instance
Sage Weil31b80062009-10-06 11:31:13 -07002998 */
Alex Elder15d98822012-05-26 23:26:43 -05002999void ceph_messenger_init(struct ceph_messenger *msgr,
Ilya Dryomov859bff52015-10-28 23:50:58 +01003000 struct ceph_entity_addr *myaddr)
Sage Weil31b80062009-10-06 11:31:13 -07003001{
Sage Weil31b80062009-10-06 11:31:13 -07003002 spin_lock_init(&msgr->global_seq_lock);
3003
Sage Weil31b80062009-10-06 11:31:13 -07003004 if (myaddr)
3005 msgr->inst.addr = *myaddr;
3006
3007 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08003008 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08003009 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08003010 encode_my_addr(msgr);
Sage Weil31b80062009-10-06 11:31:13 -07003011
Guanjun Hea2a32582012-07-08 19:50:33 -07003012 atomic_set(&msgr->stopping, 0);
Ilya Dryomov757856d2015-06-25 17:47:45 +03003013 write_pnet(&msgr->net, get_net(current->nsproxy->net_ns));
Sage Weil31b80062009-10-06 11:31:13 -07003014
Alex Elder15d98822012-05-26 23:26:43 -05003015 dout("%s %p\n", __func__, msgr);
Sage Weil31b80062009-10-06 11:31:13 -07003016}
Alex Elder15d98822012-05-26 23:26:43 -05003017EXPORT_SYMBOL(ceph_messenger_init);
Sage Weil31b80062009-10-06 11:31:13 -07003018
Ilya Dryomov757856d2015-06-25 17:47:45 +03003019void ceph_messenger_fini(struct ceph_messenger *msgr)
3020{
3021 put_net(read_pnet(&msgr->net));
3022}
3023EXPORT_SYMBOL(ceph_messenger_fini);
3024
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003025static void msg_con_set(struct ceph_msg *msg, struct ceph_connection *con)
3026{
3027 if (msg->con)
3028 msg->con->ops->put(msg->con);
3029
3030 msg->con = con ? con->ops->get(con) : NULL;
3031 BUG_ON(msg->con != con);
3032}
3033
Sage Weile00de342011-03-04 12:25:05 -08003034static void clear_standby(struct ceph_connection *con)
3035{
3036 /* come back from STANDBY? */
Sage Weil8dacc7d2012-07-20 17:24:40 -07003037 if (con->state == CON_STATE_STANDBY) {
Sage Weile00de342011-03-04 12:25:05 -08003038 dout("clear_standby %p and ++connect_seq\n", con);
Sage Weil8dacc7d2012-07-20 17:24:40 -07003039 con->state = CON_STATE_PREOPEN;
Sage Weile00de342011-03-04 12:25:05 -08003040 con->connect_seq++;
Alex Elderc9ffc772013-02-20 10:25:12 -06003041 WARN_ON(con_flag_test(con, CON_FLAG_WRITE_PENDING));
3042 WARN_ON(con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING));
Sage Weile00de342011-03-04 12:25:05 -08003043 }
3044}
3045
Sage Weil31b80062009-10-06 11:31:13 -07003046/*
3047 * Queue up an outgoing message on the given connection.
3048 */
3049void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
3050{
Sage Weila59b55a2012-07-20 15:34:04 -07003051 /* set src+dst */
3052 msg->hdr.src = con->msgr->inst.name;
3053 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
3054 msg->needs_out_seq = true;
3055
3056 mutex_lock(&con->mutex);
3057
Sage Weil8dacc7d2012-07-20 17:24:40 -07003058 if (con->state == CON_STATE_CLOSED) {
Sage Weil31b80062009-10-06 11:31:13 -07003059 dout("con_send %p closed, dropping %p\n", con, msg);
3060 ceph_msg_put(msg);
Sage Weila59b55a2012-07-20 15:34:04 -07003061 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003062 return;
3063 }
3064
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003065 msg_con_set(msg, con);
Sage Weil31b80062009-10-06 11:31:13 -07003066
Sage Weil31b80062009-10-06 11:31:13 -07003067 BUG_ON(!list_empty(&msg->list_head));
3068 list_add_tail(&msg->list_head, &con->out_queue);
3069 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
3070 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
3071 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
3072 le32_to_cpu(msg->hdr.front_len),
3073 le32_to_cpu(msg->hdr.middle_len),
3074 le32_to_cpu(msg->hdr.data_len));
Sage Weil00650932012-07-20 15:33:04 -07003075
3076 clear_standby(con);
Sage Weilec302642009-12-22 10:43:42 -08003077 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003078
3079 /* if there wasn't anything waiting to send before, queue
3080 * new work */
Alex Elderc9ffc772013-02-20 10:25:12 -06003081 if (con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07003082 queue_con(con);
3083}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003084EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07003085
3086/*
3087 * Revoke a message that was previously queued for send
3088 */
Alex Elder6740a842012-06-01 14:56:43 -05003089void ceph_msg_revoke(struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07003090{
Alex Elder6740a842012-06-01 14:56:43 -05003091 struct ceph_connection *con = msg->con;
3092
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003093 if (!con) {
3094 dout("%s msg %p null con\n", __func__, msg);
Alex Elder6740a842012-06-01 14:56:43 -05003095 return; /* Message not in our possession */
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003096 }
Alex Elder6740a842012-06-01 14:56:43 -05003097
Sage Weilec302642009-12-22 10:43:42 -08003098 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003099 if (!list_empty(&msg->list_head)) {
Alex Elder38941f82012-06-01 14:56:43 -05003100 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07003101 list_del_init(&msg->list_head);
Sage Weil31b80062009-10-06 11:31:13 -07003102 msg->hdr.seq = 0;
Alex Elder38941f82012-06-01 14:56:43 -05003103
Sage Weil31b80062009-10-06 11:31:13 -07003104 ceph_msg_put(msg);
Sage Weiled98ada2010-07-05 12:15:14 -07003105 }
3106 if (con->out_msg == msg) {
Ilya Dryomov67645d72015-12-28 13:18:34 +03003107 BUG_ON(con->out_skip);
3108 /* footer */
3109 if (con->out_msg_done) {
3110 con->out_skip += con_out_kvec_skip(con);
3111 } else {
3112 BUG_ON(!msg->data_length);
Ilya Dryomov89f08172016-02-20 15:56:07 +01003113 con->out_skip += sizeof_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07003114 }
Ilya Dryomov67645d72015-12-28 13:18:34 +03003115 /* data, middle, front */
3116 if (msg->data_length)
3117 con->out_skip += msg->cursor.total_resid;
3118 if (msg->middle)
3119 con->out_skip += con_out_kvec_skip(con);
3120 con->out_skip += con_out_kvec_skip(con);
Alex Elder92ce0342012-06-04 14:43:33 -05003121
Ilya Dryomov67645d72015-12-28 13:18:34 +03003122 dout("%s %p msg %p - was sending, will write %d skip %d\n",
3123 __func__, con, msg, con->out_kvec_bytes, con->out_skip);
3124 msg->hdr.seq = 0;
3125 con->out_msg = NULL;
Alex Elder92ce0342012-06-04 14:43:33 -05003126 ceph_msg_put(msg);
Sage Weil31b80062009-10-06 11:31:13 -07003127 }
Ilya Dryomov67645d72015-12-28 13:18:34 +03003128
Sage Weilec302642009-12-22 10:43:42 -08003129 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003130}
3131
3132/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08003133 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08003134 */
Alex Elder8921d112012-06-01 14:56:43 -05003135void ceph_msg_revoke_incoming(struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08003136{
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003137 struct ceph_connection *con = msg->con;
Alex Elder8921d112012-06-01 14:56:43 -05003138
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003139 if (!con) {
Alex Elder8921d112012-06-01 14:56:43 -05003140 dout("%s msg %p null con\n", __func__, msg);
Alex Elder8921d112012-06-01 14:56:43 -05003141 return; /* Message not in our possession */
3142 }
3143
Sage Weil350b1c32009-12-22 10:45:45 -08003144 mutex_lock(&con->mutex);
Alex Elder8921d112012-06-01 14:56:43 -05003145 if (con->in_msg == msg) {
Eric Dumazet95c96172012-04-15 05:58:06 +00003146 unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
3147 unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
3148 unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
Sage Weil350b1c32009-12-22 10:45:45 -08003149
3150 /* skip rest of message */
Alex Elder8921d112012-06-01 14:56:43 -05003151 dout("%s %p msg %p revoked\n", __func__, con, msg);
3152 con->in_base_pos = con->in_base_pos -
Sage Weil350b1c32009-12-22 10:45:45 -08003153 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08003154 front_len -
3155 middle_len -
3156 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08003157 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08003158 ceph_msg_put(con->in_msg);
3159 con->in_msg = NULL;
3160 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07003161 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08003162 } else {
Alex Elder8921d112012-06-01 14:56:43 -05003163 dout("%s %p in_msg %p msg %p no-op\n",
3164 __func__, con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08003165 }
3166 mutex_unlock(&con->mutex);
3167}
3168
3169/*
Sage Weil31b80062009-10-06 11:31:13 -07003170 * Queue a keepalive byte to ensure the tcp connection is alive.
3171 */
3172void ceph_con_keepalive(struct ceph_connection *con)
3173{
Sage Weile00de342011-03-04 12:25:05 -08003174 dout("con_keepalive %p\n", con);
Sage Weil00650932012-07-20 15:33:04 -07003175 mutex_lock(&con->mutex);
Sage Weile00de342011-03-04 12:25:05 -08003176 clear_standby(con);
Sage Weil00650932012-07-20 15:33:04 -07003177 mutex_unlock(&con->mutex);
Alex Elderc9ffc772013-02-20 10:25:12 -06003178 if (con_flag_test_and_set(con, CON_FLAG_KEEPALIVE_PENDING) == 0 &&
3179 con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07003180 queue_con(con);
3181}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003182EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07003183
Yan, Zheng8b9558a2015-09-01 17:19:38 +08003184bool ceph_con_keepalive_expired(struct ceph_connection *con,
3185 unsigned long interval)
3186{
3187 if (interval > 0 &&
3188 (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2)) {
Deepa Dinamani1134e092017-05-08 15:59:19 -07003189 struct timespec now;
Yan, Zheng8b9558a2015-09-01 17:19:38 +08003190 struct timespec ts;
Deepa Dinamani1134e092017-05-08 15:59:19 -07003191 ktime_get_real_ts(&now);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08003192 jiffies_to_timespec(interval, &ts);
3193 ts = timespec_add(con->last_keepalive_ack, ts);
3194 return timespec_compare(&now, &ts) >= 0;
3195 }
3196 return false;
3197}
3198
Alex Elder6644ed72013-03-11 23:34:24 -05003199static struct ceph_msg_data *ceph_msg_data_create(enum ceph_msg_data_type type)
Alex Elder43794502013-03-01 18:00:16 -06003200{
Alex Elder6644ed72013-03-11 23:34:24 -05003201 struct ceph_msg_data *data;
3202
3203 if (WARN_ON(!ceph_msg_data_type_valid(type)))
3204 return NULL;
3205
Alex Elder81b36be2013-05-01 12:43:04 -05003206 data = kmem_cache_zalloc(ceph_msg_data_cache, GFP_NOFS);
Dan Carpenter7c40b222017-07-17 11:13:35 +03003207 if (!data)
3208 return NULL;
3209
3210 data->type = type;
Alex Elder5240d9f2013-03-14 14:09:06 -05003211 INIT_LIST_HEAD(&data->links);
Alex Elder6644ed72013-03-11 23:34:24 -05003212
3213 return data;
3214}
3215
3216static void ceph_msg_data_destroy(struct ceph_msg_data *data)
3217{
3218 if (!data)
3219 return;
3220
Alex Elder5240d9f2013-03-14 14:09:06 -05003221 WARN_ON(!list_empty(&data->links));
Yan, Zhenge4339d282014-09-16 17:50:45 +08003222 if (data->type == CEPH_MSG_DATA_PAGELIST)
Alex Elder6644ed72013-03-11 23:34:24 -05003223 ceph_pagelist_release(data->pagelist);
Alex Elder81b36be2013-05-01 12:43:04 -05003224 kmem_cache_free(ceph_msg_data_cache, data);
Alex Elder43794502013-03-01 18:00:16 -06003225}
3226
Alex Elder90af3602013-04-05 14:46:01 -05003227void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
Alex Elderf1baeb22013-03-07 15:38:26 -06003228 size_t length, size_t alignment)
Alex Elder02afca62013-02-14 12:16:43 -06003229{
Alex Elder6644ed72013-03-11 23:34:24 -05003230 struct ceph_msg_data *data;
3231
Alex Elder07aa1552013-03-04 18:29:06 -06003232 BUG_ON(!pages);
3233 BUG_ON(!length);
Alex Elder02afca62013-02-14 12:16:43 -06003234
Alex Elder6644ed72013-03-11 23:34:24 -05003235 data = ceph_msg_data_create(CEPH_MSG_DATA_PAGES);
3236 BUG_ON(!data);
3237 data->pages = pages;
3238 data->length = length;
3239 data->alignment = alignment & ~PAGE_MASK;
3240
Alex Elder5240d9f2013-03-14 14:09:06 -05003241 list_add_tail(&data->links, &msg->data);
3242 msg->data_length += length;
Alex Elder02afca62013-02-14 12:16:43 -06003243}
Alex Elder90af3602013-04-05 14:46:01 -05003244EXPORT_SYMBOL(ceph_msg_data_add_pages);
Sage Weil31b80062009-10-06 11:31:13 -07003245
Alex Elder90af3602013-04-05 14:46:01 -05003246void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
Alex Elder27fa8382013-02-14 12:16:43 -06003247 struct ceph_pagelist *pagelist)
3248{
Alex Elder6644ed72013-03-11 23:34:24 -05003249 struct ceph_msg_data *data;
3250
Alex Elder07aa1552013-03-04 18:29:06 -06003251 BUG_ON(!pagelist);
3252 BUG_ON(!pagelist->length);
Alex Elder27fa8382013-02-14 12:16:43 -06003253
Alex Elder6644ed72013-03-11 23:34:24 -05003254 data = ceph_msg_data_create(CEPH_MSG_DATA_PAGELIST);
3255 BUG_ON(!data);
3256 data->pagelist = pagelist;
3257
Alex Elder5240d9f2013-03-14 14:09:06 -05003258 list_add_tail(&data->links, &msg->data);
3259 msg->data_length += pagelist->length;
Alex Elder27fa8382013-02-14 12:16:43 -06003260}
Alex Elder90af3602013-04-05 14:46:01 -05003261EXPORT_SYMBOL(ceph_msg_data_add_pagelist);
Alex Elder27fa8382013-02-14 12:16:43 -06003262
Alex Elderea965712013-04-05 14:46:01 -05003263#ifdef CONFIG_BLOCK
Alex Elder90af3602013-04-05 14:46:01 -05003264void ceph_msg_data_add_bio(struct ceph_msg *msg, struct bio *bio,
Alex Eldera1930802013-03-14 14:09:06 -05003265 size_t length)
Alex Elder27fa8382013-02-14 12:16:43 -06003266{
Alex Elder6644ed72013-03-11 23:34:24 -05003267 struct ceph_msg_data *data;
Alex Elder27fa8382013-02-14 12:16:43 -06003268
Alex Elder6644ed72013-03-11 23:34:24 -05003269 BUG_ON(!bio);
Alex Elder6644ed72013-03-11 23:34:24 -05003270
3271 data = ceph_msg_data_create(CEPH_MSG_DATA_BIO);
3272 BUG_ON(!data);
3273 data->bio = bio;
Alex Elderc851c492013-04-05 14:46:01 -05003274 data->bio_length = length;
Alex Elder6644ed72013-03-11 23:34:24 -05003275
Alex Elder5240d9f2013-03-14 14:09:06 -05003276 list_add_tail(&data->links, &msg->data);
3277 msg->data_length += length;
Alex Elder27fa8382013-02-14 12:16:43 -06003278}
Alex Elder90af3602013-04-05 14:46:01 -05003279EXPORT_SYMBOL(ceph_msg_data_add_bio);
Alex Elderea965712013-04-05 14:46:01 -05003280#endif /* CONFIG_BLOCK */
Alex Elder27fa8382013-02-14 12:16:43 -06003281
Sage Weil31b80062009-10-06 11:31:13 -07003282/*
3283 * construct a new message with given type, size
3284 * the new msg has a ref count of 1.
3285 */
Sage Weilb61c2762011-08-09 15:03:46 -07003286struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
3287 bool can_fail)
Sage Weil31b80062009-10-06 11:31:13 -07003288{
3289 struct ceph_msg *m;
3290
Alex Eldere3d5d632013-05-01 12:43:04 -05003291 m = kmem_cache_zalloc(ceph_msg_cache, flags);
Sage Weil31b80062009-10-06 11:31:13 -07003292 if (m == NULL)
3293 goto out;
Alex Elder38941f82012-06-01 14:56:43 -05003294
Sage Weil31b80062009-10-06 11:31:13 -07003295 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07003296 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
Sage Weil31b80062009-10-06 11:31:13 -07003297 m->hdr.front_len = cpu_to_le32(front_len);
Sage Weil31b80062009-10-06 11:31:13 -07003298
Alex Elder9516e452013-03-01 18:00:16 -06003299 INIT_LIST_HEAD(&m->list_head);
3300 kref_init(&m->kref);
Alex Elder5240d9f2013-03-14 14:09:06 -05003301 INIT_LIST_HEAD(&m->data);
Henry C Changca208922011-05-03 02:29:56 +00003302
Sage Weil31b80062009-10-06 11:31:13 -07003303 /* front */
3304 if (front_len) {
Ilya Dryomoveeb0bed2014-01-09 20:08:21 +02003305 m->front.iov_base = ceph_kvmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07003306 if (m->front.iov_base == NULL) {
Sage Weilb61c2762011-08-09 15:03:46 -07003307 dout("ceph_msg_new can't allocate %d bytes\n",
Sage Weil31b80062009-10-06 11:31:13 -07003308 front_len);
3309 goto out2;
3310 }
3311 } else {
3312 m->front.iov_base = NULL;
3313 }
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02003314 m->front_alloc_len = m->front.iov_len = front_len;
Sage Weil31b80062009-10-06 11:31:13 -07003315
Sage Weilbb257662010-04-01 16:07:23 -07003316 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07003317 return m;
3318
3319out2:
3320 ceph_msg_put(m);
3321out:
Sage Weilb61c2762011-08-09 15:03:46 -07003322 if (!can_fail) {
3323 pr_err("msg_new can't create type %d front %d\n", type,
3324 front_len);
Sage Weilf0ed1b72011-08-09 15:05:07 -07003325 WARN_ON(1);
Sage Weilb61c2762011-08-09 15:03:46 -07003326 } else {
3327 dout("msg_new can't create type %d front %d\n", type,
3328 front_len);
3329 }
Sage Weila79832f2010-04-01 16:06:19 -07003330 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07003331}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003332EXPORT_SYMBOL(ceph_msg_new);
Sage Weil31b80062009-10-06 11:31:13 -07003333
3334/*
Sage Weil31b80062009-10-06 11:31:13 -07003335 * Allocate "middle" portion of a message, if it is needed and wasn't
3336 * allocated by alloc_msg. This allows us to read a small fixed-size
3337 * per-type header in the front and then gracefully fail (i.e.,
3338 * propagate the error to the caller based on info in the front) when
3339 * the middle is too large.
3340 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08003341static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07003342{
3343 int type = le16_to_cpu(msg->hdr.type);
3344 int middle_len = le32_to_cpu(msg->hdr.middle_len);
3345
3346 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
3347 ceph_msg_type_name(type), middle_len);
3348 BUG_ON(!middle_len);
3349 BUG_ON(msg->middle);
3350
Sage Weilb6c1d5b2009-12-07 12:17:17 -08003351 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07003352 if (!msg->middle)
3353 return -ENOMEM;
3354 return 0;
3355}
3356
Yehuda Sadeh24504182010-01-08 13:58:34 -08003357/*
Alex Elder1c20f2d2012-06-04 14:43:32 -05003358 * Allocate a message for receiving an incoming message on a
3359 * connection, and save the result in con->in_msg. Uses the
3360 * connection's private alloc_msg op if available.
3361 *
Sage Weil4740a622012-07-30 18:19:30 -07003362 * Returns 0 on success, or a negative error code.
3363 *
3364 * On success, if we set *skip = 1:
3365 * - the next message should be skipped and ignored.
3366 * - con->in_msg == NULL
3367 * or if we set *skip = 0:
3368 * - con->in_msg is non-null.
3369 * On error (ENOMEM, EAGAIN, ...),
3370 * - con->in_msg == NULL
Yehuda Sadeh24504182010-01-08 13:58:34 -08003371 */
Sage Weil4740a622012-07-30 18:19:30 -07003372static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip)
Yehuda Sadeh24504182010-01-08 13:58:34 -08003373{
Sage Weil4740a622012-07-30 18:19:30 -07003374 struct ceph_msg_header *hdr = &con->in_hdr;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003375 int middle_len = le32_to_cpu(hdr->middle_len);
Alex Elder1d866d12013-03-01 18:00:14 -06003376 struct ceph_msg *msg;
Sage Weil4740a622012-07-30 18:19:30 -07003377 int ret = 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003378
Alex Elder1c20f2d2012-06-04 14:43:32 -05003379 BUG_ON(con->in_msg != NULL);
Alex Elder53ded492013-03-01 18:00:14 -06003380 BUG_ON(!con->ops->alloc_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08003381
Alex Elder53ded492013-03-01 18:00:14 -06003382 mutex_unlock(&con->mutex);
3383 msg = con->ops->alloc_msg(con, hdr, skip);
3384 mutex_lock(&con->mutex);
3385 if (con->state != CON_STATE_OPEN) {
3386 if (msg)
Alex Elder1d866d12013-03-01 18:00:14 -06003387 ceph_msg_put(msg);
Alex Elder53ded492013-03-01 18:00:14 -06003388 return -EAGAIN;
3389 }
Alex Elder41375772013-03-05 09:25:10 -06003390 if (msg) {
3391 BUG_ON(*skip);
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003392 msg_con_set(msg, con);
Alex Elder41375772013-03-05 09:25:10 -06003393 con->in_msg = msg;
Alex Elder41375772013-03-05 09:25:10 -06003394 } else {
3395 /*
3396 * Null message pointer means either we should skip
3397 * this message or we couldn't allocate memory. The
3398 * former is not an error.
3399 */
3400 if (*skip)
3401 return 0;
Alex Elder41375772013-03-05 09:25:10 -06003402
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03003403 con->error_msg = "error allocating memory for incoming message";
Alex Elder53ded492013-03-01 18:00:14 -06003404 return -ENOMEM;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003405 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05003406 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08003407
Alex Elder1c20f2d2012-06-04 14:43:32 -05003408 if (middle_len && !con->in_msg->middle) {
3409 ret = ceph_alloc_middle(con, con->in_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08003410 if (ret < 0) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05003411 ceph_msg_put(con->in_msg);
3412 con->in_msg = NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003413 }
3414 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08003415
Sage Weil4740a622012-07-30 18:19:30 -07003416 return ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003417}
3418
Sage Weil31b80062009-10-06 11:31:13 -07003419
3420/*
3421 * Free a generically kmalloc'd message.
3422 */
Ilya Dryomov0215e442014-06-20 14:14:41 +04003423static void ceph_msg_free(struct ceph_msg *m)
Sage Weil31b80062009-10-06 11:31:13 -07003424{
Ilya Dryomov0215e442014-06-20 14:14:41 +04003425 dout("%s %p\n", __func__, m);
Ilya Dryomov4965fc32014-10-23 16:32:57 +04003426 kvfree(m->front.iov_base);
Alex Eldere3d5d632013-05-01 12:43:04 -05003427 kmem_cache_free(ceph_msg_cache, m);
Sage Weil31b80062009-10-06 11:31:13 -07003428}
3429
Ilya Dryomov0215e442014-06-20 14:14:41 +04003430static void ceph_msg_release(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07003431{
Sage Weilc2e552e2009-12-07 15:55:05 -08003432 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Geliang Tang10bcee12015-12-18 23:33:30 +08003433 struct ceph_msg_data *data, *next;
Sage Weil31b80062009-10-06 11:31:13 -07003434
Ilya Dryomov0215e442014-06-20 14:14:41 +04003435 dout("%s %p\n", __func__, m);
Sage Weilc2e552e2009-12-07 15:55:05 -08003436 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07003437
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003438 msg_con_set(m, NULL);
3439
Sage Weilc2e552e2009-12-07 15:55:05 -08003440 /* drop middle, data, if any */
3441 if (m->middle) {
3442 ceph_buffer_put(m->middle);
3443 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07003444 }
Alex Elder5240d9f2013-03-14 14:09:06 -05003445
Geliang Tang10bcee12015-12-18 23:33:30 +08003446 list_for_each_entry_safe(data, next, &m->data, links) {
3447 list_del_init(&data->links);
Alex Elder5240d9f2013-03-14 14:09:06 -05003448 ceph_msg_data_destroy(data);
3449 }
Alex Eldera1930802013-03-14 14:09:06 -05003450 m->data_length = 0;
Sage Weil58bb3b32009-12-23 12:12:31 -08003451
Sage Weilc2e552e2009-12-07 15:55:05 -08003452 if (m->pool)
3453 ceph_msgpool_put(m->pool, m);
3454 else
Ilya Dryomov0215e442014-06-20 14:14:41 +04003455 ceph_msg_free(m);
Sage Weil31b80062009-10-06 11:31:13 -07003456}
Ilya Dryomov0215e442014-06-20 14:14:41 +04003457
3458struct ceph_msg *ceph_msg_get(struct ceph_msg *msg)
3459{
3460 dout("%s %p (was %d)\n", __func__, msg,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003461 kref_read(&msg->kref));
Ilya Dryomov0215e442014-06-20 14:14:41 +04003462 kref_get(&msg->kref);
3463 return msg;
3464}
3465EXPORT_SYMBOL(ceph_msg_get);
3466
3467void ceph_msg_put(struct ceph_msg *msg)
3468{
3469 dout("%s %p (was %d)\n", __func__, msg,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003470 kref_read(&msg->kref));
Ilya Dryomov0215e442014-06-20 14:14:41 +04003471 kref_put(&msg->kref, ceph_msg_release);
3472}
3473EXPORT_SYMBOL(ceph_msg_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003474
3475void ceph_msg_dump(struct ceph_msg *msg)
3476{
Ilya Dryomov3cea4c32014-01-09 20:08:21 +02003477 pr_debug("msg_dump %p (front_alloc_len %d length %zd)\n", msg,
3478 msg->front_alloc_len, msg->data_length);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003479 print_hex_dump(KERN_DEBUG, "header: ",
3480 DUMP_PREFIX_OFFSET, 16, 1,
3481 &msg->hdr, sizeof(msg->hdr), true);
3482 print_hex_dump(KERN_DEBUG, " front: ",
3483 DUMP_PREFIX_OFFSET, 16, 1,
3484 msg->front.iov_base, msg->front.iov_len, true);
3485 if (msg->middle)
3486 print_hex_dump(KERN_DEBUG, "middle: ",
3487 DUMP_PREFIX_OFFSET, 16, 1,
3488 msg->middle->vec.iov_base,
3489 msg->middle->vec.iov_len, true);
3490 print_hex_dump(KERN_DEBUG, "footer: ",
3491 DUMP_PREFIX_OFFSET, 16, 1,
3492 &msg->footer, sizeof(msg->footer), true);
3493}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003494EXPORT_SYMBOL(ceph_msg_dump);