blob: 91a57857cf113b90d55259416e5df37c69ee8a6b [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__);
Gustavo A. R. Silva18370b32017-10-15 12:55:23 -0500433 /* fall through */
Sage Weil31b80062009-10-06 11:31:13 -0700434 case TCP_CLOSE_WAIT:
Alex Elder327800b2012-05-22 11:41:43 -0500435 dout("%s TCP_CLOSE_WAIT\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500436 con_sock_state_closing(con);
Alex Elderc9ffc772013-02-20 10:25:12 -0600437 con_flag_set(con, CON_FLAG_SOCK_CLOSED);
Alex Elderd65c9e02012-06-20 21:53:53 -0500438 queue_con(con);
Sage Weil31b80062009-10-06 11:31:13 -0700439 break;
440 case TCP_ESTABLISHED:
Alex Elder327800b2012-05-22 11:41:43 -0500441 dout("%s TCP_ESTABLISHED\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500442 con_sock_state_connected(con);
Sage Weil31b80062009-10-06 11:31:13 -0700443 queue_con(con);
444 break;
Alex Elderd3002b92012-02-14 14:05:33 -0600445 default: /* Everything else is uninteresting */
446 break;
Sage Weil31b80062009-10-06 11:31:13 -0700447 }
448}
449
450/*
451 * set up socket callbacks
452 */
453static void set_sock_callbacks(struct socket *sock,
454 struct ceph_connection *con)
455{
456 struct sock *sk = sock->sk;
Alex Elderbd406142012-01-23 15:49:27 -0600457 sk->sk_user_data = con;
Alex Elder327800b2012-05-22 11:41:43 -0500458 sk->sk_data_ready = ceph_sock_data_ready;
459 sk->sk_write_space = ceph_sock_write_space;
460 sk->sk_state_change = ceph_sock_state_change;
Sage Weil31b80062009-10-06 11:31:13 -0700461}
462
463
464/*
465 * socket helpers
466 */
467
468/*
469 * initiate connection to a remote socket.
470 */
Alex Elder41617d02012-02-14 14:05:33 -0600471static int ceph_tcp_connect(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700472{
Sage Weilf91d3472010-07-01 15:18:31 -0700473 struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
Sage Weil31b80062009-10-06 11:31:13 -0700474 struct socket *sock;
Ilya Dryomov633ee4072017-03-21 13:44:28 +0100475 unsigned int noio_flag;
Sage Weil31b80062009-10-06 11:31:13 -0700476 int ret;
477
478 BUG_ON(con->sock);
Ilya Dryomov633ee4072017-03-21 13:44:28 +0100479
480 /* sock_create_kern() allocates with GFP_KERNEL */
481 noio_flag = memalloc_noio_save();
Ilya Dryomov757856d2015-06-25 17:47:45 +0300482 ret = sock_create_kern(read_pnet(&con->msgr->net), paddr->ss_family,
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -0500483 SOCK_STREAM, IPPROTO_TCP, &sock);
Ilya Dryomov633ee4072017-03-21 13:44:28 +0100484 memalloc_noio_restore(noio_flag);
Sage Weil31b80062009-10-06 11:31:13 -0700485 if (ret)
Alex Elder41617d02012-02-14 14:05:33 -0600486 return ret;
Ilya Dryomov6d7fdb02015-04-02 14:40:58 +0300487 sock->sk->sk_allocation = GFP_NOFS;
Sage Weil31b80062009-10-06 11:31:13 -0700488
Sage Weila6a53492010-04-13 14:07:07 -0700489#ifdef CONFIG_LOCKDEP
490 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
491#endif
492
Sage Weil31b80062009-10-06 11:31:13 -0700493 set_sock_callbacks(sock, con);
494
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700495 dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700496
Sage Weil89a86be2012-06-09 14:19:21 -0700497 con_sock_state_connecting(con);
Sage Weilf91d3472010-07-01 15:18:31 -0700498 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
499 O_NONBLOCK);
Sage Weil31b80062009-10-06 11:31:13 -0700500 if (ret == -EINPROGRESS) {
501 dout("connect %s EINPROGRESS sk_state = %u\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700502 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -0700503 sock->sk->sk_state);
Alex Eldera5bc3122012-01-23 15:49:27 -0600504 } else if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700505 pr_err("connect %s error %d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700506 ceph_pr_addr(&con->peer_addr.in_addr), ret);
Sage Weil31b80062009-10-06 11:31:13 -0700507 sock_release(sock);
Alex Elder41617d02012-02-14 14:05:33 -0600508 return ret;
Alex Eldera5bc3122012-01-23 15:49:27 -0600509 }
Mike Christie89baaa52014-10-16 01:50:19 -0500510
Ilya Dryomov859bff52015-10-28 23:50:58 +0100511 if (ceph_test_opt(from_msgr(con->msgr), TCP_NODELAY)) {
Chaitanya Huilgolba988f82015-01-23 16:41:25 +0530512 int optval = 1;
513
514 ret = kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY,
515 (char *)&optval, sizeof(optval));
516 if (ret)
517 pr_err("kernel_setsockopt(TCP_NODELAY) failed: %d",
518 ret);
519 }
520
Alex Eldera5bc3122012-01-23 15:49:27 -0600521 con->sock = sock;
Alex Elder41617d02012-02-14 14:05:33 -0600522 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700523}
524
525static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
526{
527 struct kvec iov = {buf, len};
528 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil98bdb0a2011-01-25 08:17:48 -0800529 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700530
Al Viro100803a2015-11-15 00:12:48 -0500531 iov_iter_kvec(&msg.msg_iter, READ | ITER_KVEC, &iov, 1, len);
532 r = sock_recvmsg(sock, &msg, msg.msg_flags);
Sage Weil98bdb0a2011-01-25 08:17:48 -0800533 if (r == -EAGAIN)
534 r = 0;
535 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700536}
537
Alex Elderafb3d902013-03-08 20:58:59 -0600538static int ceph_tcp_recvpage(struct socket *sock, struct page *page,
539 int page_offset, size_t length)
540{
Al Viro100803a2015-11-15 00:12:48 -0500541 struct bio_vec bvec = {
542 .bv_page = page,
543 .bv_offset = page_offset,
544 .bv_len = length
545 };
546 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
547 int r;
Alex Elderafb3d902013-03-08 20:58:59 -0600548
549 BUG_ON(page_offset + length > PAGE_SIZE);
Al Viro100803a2015-11-15 00:12:48 -0500550 iov_iter_bvec(&msg.msg_iter, READ | ITER_BVEC, &bvec, 1, length);
551 r = sock_recvmsg(sock, &msg, msg.msg_flags);
552 if (r == -EAGAIN)
553 r = 0;
554 return r;
Alex Elderafb3d902013-03-08 20:58:59 -0600555}
556
Sage Weil31b80062009-10-06 11:31:13 -0700557/*
558 * write something. @more is true if caller will be sending more data
559 * shortly.
560 */
561static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
562 size_t kvlen, size_t len, int more)
563{
564 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil42961d22011-01-25 08:19:34 -0800565 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700566
567 if (more)
568 msg.msg_flags |= MSG_MORE;
569 else
570 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
571
Sage Weil42961d22011-01-25 08:19:34 -0800572 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
573 if (r == -EAGAIN)
574 r = 0;
575 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700576}
577
Chunwei Chen178eda292014-04-23 12:35:09 +0800578static int __ceph_tcp_sendpage(struct socket *sock, struct page *page,
Alex Eldere1dcb122013-03-06 23:39:38 -0600579 int offset, size_t size, bool more)
Alex Elder31739132012-03-07 11:40:08 -0600580{
581 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
582 int ret;
583
584 ret = kernel_sendpage(sock, page, offset, size, flags);
585 if (ret == -EAGAIN)
586 ret = 0;
587
588 return ret;
589}
590
Chunwei Chen178eda292014-04-23 12:35:09 +0800591static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
592 int offset, size_t size, bool more)
593{
Al Viro61ff6e92016-01-09 20:55:45 -0500594 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
595 struct bio_vec bvec;
Chunwei Chen178eda292014-04-23 12:35:09 +0800596 int ret;
Chunwei Chen178eda292014-04-23 12:35:09 +0800597
598 /* sendpage cannot properly handle pages with page_count == 0,
599 * we need to fallback to sendmsg if that's the case */
600 if (page_count(page) >= 1)
601 return __ceph_tcp_sendpage(sock, page, offset, size, more);
602
Al Viro61ff6e92016-01-09 20:55:45 -0500603 bvec.bv_page = page;
604 bvec.bv_offset = offset;
605 bvec.bv_len = size;
606
607 if (more)
608 msg.msg_flags |= MSG_MORE;
609 else
610 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
611
612 iov_iter_bvec(&msg.msg_iter, WRITE | ITER_BVEC, &bvec, 1, size);
613 ret = sock_sendmsg(sock, &msg);
614 if (ret == -EAGAIN)
615 ret = 0;
Chunwei Chen178eda292014-04-23 12:35:09 +0800616
617 return ret;
618}
Sage Weil31b80062009-10-06 11:31:13 -0700619
620/*
621 * Shutdown/close the socket for the given connection.
622 */
623static int con_close_socket(struct ceph_connection *con)
624{
Sage Weil8007b8d2012-07-30 18:16:16 -0700625 int rc = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700626
627 dout("con_close_socket on %p sock %p\n", con, con->sock);
Sage Weil8007b8d2012-07-30 18:16:16 -0700628 if (con->sock) {
629 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
630 sock_release(con->sock);
631 con->sock = NULL;
632 }
Alex Elder456ea462012-06-20 21:53:53 -0500633
634 /*
Sage Weil4a861692012-07-20 17:29:55 -0700635 * Forcibly clear the SOCK_CLOSED flag. It gets set
Alex Elder456ea462012-06-20 21:53:53 -0500636 * independent of the connection mutex, and we could have
637 * received a socket close event before we had the chance to
638 * shut the socket down.
639 */
Alex Elderc9ffc772013-02-20 10:25:12 -0600640 con_flag_clear(con, CON_FLAG_SOCK_CLOSED);
Sage Weil8007b8d2012-07-30 18:16:16 -0700641
Alex Elderce2c8902012-05-22 22:15:49 -0500642 con_sock_state_closed(con);
Sage Weil31b80062009-10-06 11:31:13 -0700643 return rc;
644}
645
646/*
647 * Reset a connection. Discard all incoming and outgoing messages
648 * and clear *_seq state.
649 */
650static void ceph_msg_remove(struct ceph_msg *msg)
651{
652 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -0500653
Sage Weil31b80062009-10-06 11:31:13 -0700654 ceph_msg_put(msg);
655}
656static void ceph_msg_remove_list(struct list_head *head)
657{
658 while (!list_empty(head)) {
659 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
660 list_head);
661 ceph_msg_remove(msg);
662 }
663}
664
665static void reset_connection(struct ceph_connection *con)
666{
667 /* reset connection, out_queue, msg_ and connect_seq */
668 /* discard existing out_queue and msg_seq */
Sage Weil0fa6ebc2012-12-27 20:27:04 -0600669 dout("reset_connection %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -0700670 ceph_msg_remove_list(&con->out_queue);
671 ceph_msg_remove_list(&con->out_sent);
672
Sage Weilcf3e5c42009-12-11 09:48:05 -0800673 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -0500674 BUG_ON(con->in_msg->con != con);
Sage Weilcf3e5c42009-12-11 09:48:05 -0800675 ceph_msg_put(con->in_msg);
676 con->in_msg = NULL;
677 }
678
Sage Weil31b80062009-10-06 11:31:13 -0700679 con->connect_seq = 0;
680 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800681 if (con->out_msg) {
Ilya Dryomov583d0fe2015-11-02 17:13:58 +0100682 BUG_ON(con->out_msg->con != con);
Sage Weilc86a2932009-12-14 14:04:30 -0800683 ceph_msg_put(con->out_msg);
684 con->out_msg = NULL;
685 }
Sage Weil31b80062009-10-06 11:31:13 -0700686 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700687 con->in_seq_acked = 0;
Ilya Dryomov67645d72015-12-28 13:18:34 +0300688
689 con->out_skip = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700690}
691
692/*
693 * mark a peer down. drop any open connections.
694 */
695void ceph_con_close(struct ceph_connection *con)
696{
Sage Weil8c50c812012-07-30 16:24:37 -0700697 mutex_lock(&con->mutex);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700698 dout("con_close %p peer %s\n", con,
699 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil8dacc7d2012-07-20 17:24:40 -0700700 con->state = CON_STATE_CLOSED;
Alex Eldera5988c42012-05-29 11:04:58 -0500701
Alex Elderc9ffc772013-02-20 10:25:12 -0600702 con_flag_clear(con, CON_FLAG_LOSSYTX); /* so we retry next connect */
703 con_flag_clear(con, CON_FLAG_KEEPALIVE_PENDING);
704 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
705 con_flag_clear(con, CON_FLAG_BACKOFF);
Alex Eldera5988c42012-05-29 11:04:58 -0500706
Sage Weil31b80062009-10-06 11:31:13 -0700707 reset_connection(con);
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700708 con->peer_global_seq = 0;
Ilya Dryomov37ab77a2014-06-24 16:21:45 +0400709 cancel_con(con);
Sage Weilee76e072012-07-20 16:45:49 -0700710 con_close_socket(con);
Sage Weilec302642009-12-22 10:43:42 -0800711 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700712}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700713EXPORT_SYMBOL(ceph_con_close);
Sage Weil31b80062009-10-06 11:31:13 -0700714
715/*
Sage Weil31b80062009-10-06 11:31:13 -0700716 * Reopen a closed connection, with a new peer address.
717 */
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700718void ceph_con_open(struct ceph_connection *con,
719 __u8 entity_type, __u64 entity_num,
720 struct ceph_entity_addr *addr)
Sage Weil31b80062009-10-06 11:31:13 -0700721{
Sage Weil54691552012-07-30 16:21:40 -0700722 mutex_lock(&con->mutex);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700723 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
Sage Weil8dacc7d2012-07-20 17:24:40 -0700724
Alex Elder122070a2012-12-26 10:43:57 -0600725 WARN_ON(con->state != CON_STATE_CLOSED);
Sage Weil8dacc7d2012-07-20 17:24:40 -0700726 con->state = CON_STATE_PREOPEN;
Alex Eldera5988c42012-05-29 11:04:58 -0500727
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700728 con->peer_name.type = (__u8) entity_type;
729 con->peer_name.num = cpu_to_le64(entity_num);
730
Sage Weil31b80062009-10-06 11:31:13 -0700731 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800732 con->delay = 0; /* reset backoff memory */
Sage Weil54691552012-07-30 16:21:40 -0700733 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700734 queue_con(con);
735}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700736EXPORT_SYMBOL(ceph_con_open);
Sage Weil31b80062009-10-06 11:31:13 -0700737
738/*
Sage Weil87b315a2010-03-22 14:51:18 -0700739 * return true if this connection ever successfully opened
740 */
741bool ceph_con_opened(struct ceph_connection *con)
742{
743 return con->connect_seq > 0;
744}
745
746/*
Sage Weil31b80062009-10-06 11:31:13 -0700747 * initialize a new connection.
748 */
Alex Elder1bfd89f2012-05-26 23:26:43 -0500749void ceph_con_init(struct ceph_connection *con, void *private,
750 const struct ceph_connection_operations *ops,
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700751 struct ceph_messenger *msgr)
Sage Weil31b80062009-10-06 11:31:13 -0700752{
753 dout("con_init %p\n", con);
754 memset(con, 0, sizeof(*con));
Alex Elder1bfd89f2012-05-26 23:26:43 -0500755 con->private = private;
756 con->ops = ops;
Sage Weil31b80062009-10-06 11:31:13 -0700757 con->msgr = msgr;
Alex Elderce2c8902012-05-22 22:15:49 -0500758
759 con_sock_state_init(con);
760
Sage Weilec302642009-12-22 10:43:42 -0800761 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700762 INIT_LIST_HEAD(&con->out_queue);
763 INIT_LIST_HEAD(&con->out_sent);
Ilya Dryomov68931622015-07-03 15:44:41 +0300764 INIT_DELAYED_WORK(&con->work, ceph_con_workfn);
Alex Eldera5988c42012-05-29 11:04:58 -0500765
Sage Weil8dacc7d2012-07-20 17:24:40 -0700766 con->state = CON_STATE_CLOSED;
Sage Weil31b80062009-10-06 11:31:13 -0700767}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700768EXPORT_SYMBOL(ceph_con_init);
Sage Weil31b80062009-10-06 11:31:13 -0700769
770
771/*
772 * We maintain a global counter to order connection attempts. Get
773 * a unique seq greater than @gt.
774 */
775static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
776{
777 u32 ret;
778
779 spin_lock(&msgr->global_seq_lock);
780 if (msgr->global_seq < gt)
781 msgr->global_seq = gt;
782 ret = ++msgr->global_seq;
783 spin_unlock(&msgr->global_seq_lock);
784 return ret;
785}
786
Alex Eldere2200422012-05-23 14:35:23 -0500787static void con_out_kvec_reset(struct ceph_connection *con)
Alex Elder859eb792012-02-14 14:05:33 -0600788{
Ilya Dryomov67645d72015-12-28 13:18:34 +0300789 BUG_ON(con->out_skip);
790
Alex Elder859eb792012-02-14 14:05:33 -0600791 con->out_kvec_left = 0;
792 con->out_kvec_bytes = 0;
793 con->out_kvec_cur = &con->out_kvec[0];
794}
795
Alex Eldere2200422012-05-23 14:35:23 -0500796static void con_out_kvec_add(struct ceph_connection *con,
Alex Elder859eb792012-02-14 14:05:33 -0600797 size_t size, void *data)
798{
Ilya Dryomov67645d72015-12-28 13:18:34 +0300799 int index = con->out_kvec_left;
Alex Elder859eb792012-02-14 14:05:33 -0600800
Ilya Dryomov67645d72015-12-28 13:18:34 +0300801 BUG_ON(con->out_skip);
Alex Elder859eb792012-02-14 14:05:33 -0600802 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
803
804 con->out_kvec[index].iov_len = size;
805 con->out_kvec[index].iov_base = data;
806 con->out_kvec_left++;
807 con->out_kvec_bytes += size;
808}
Sage Weil31b80062009-10-06 11:31:13 -0700809
Ilya Dryomov67645d72015-12-28 13:18:34 +0300810/*
811 * Chop off a kvec from the end. Return residual number of bytes for
812 * that kvec, i.e. how many bytes would have been written if the kvec
813 * hadn't been nuked.
814 */
815static int con_out_kvec_skip(struct ceph_connection *con)
816{
817 int off = con->out_kvec_cur - con->out_kvec;
818 int skip = 0;
819
820 if (con->out_kvec_bytes > 0) {
821 skip = con->out_kvec[off + con->out_kvec_left - 1].iov_len;
822 BUG_ON(con->out_kvec_bytes < skip);
823 BUG_ON(!con->out_kvec_left);
824 con->out_kvec_bytes -= skip;
825 con->out_kvec_left--;
826 }
827
828 return skip;
829}
830
Alex Elderdf6ad1f2012-06-11 14:57:13 -0500831#ifdef CONFIG_BLOCK
Alex Elder6aaa4512013-03-06 23:39:39 -0600832
833/*
834 * For a bio data item, a piece is whatever remains of the next
835 * entry in the current bio iovec, or the first entry in the next
836 * bio in the list.
837 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500838static void ceph_msg_data_bio_cursor_init(struct ceph_msg_data_cursor *cursor,
Alex Elder25aff7c2013-03-11 23:34:22 -0500839 size_t length)
Alex Elder6aaa4512013-03-06 23:39:39 -0600840{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500841 struct ceph_msg_data *data = cursor->data;
Ilya Dryomov5359a172018-01-20 10:30:10 +0100842 struct ceph_bio_iter *it = &cursor->bio_iter;
Alex Elder6aaa4512013-03-06 23:39:39 -0600843
Ilya Dryomov5359a172018-01-20 10:30:10 +0100844 cursor->resid = min_t(size_t, length, data->bio_length);
845 *it = data->bio_pos;
846 if (cursor->resid < it->iter.bi_size)
847 it->iter.bi_size = cursor->resid;
Alex Elder6aaa4512013-03-06 23:39:39 -0600848
Ilya Dryomov5359a172018-01-20 10:30:10 +0100849 BUG_ON(cursor->resid < bio_iter_len(it->bio, it->iter));
850 cursor->last_piece = cursor->resid == bio_iter_len(it->bio, it->iter);
Alex Elder6aaa4512013-03-06 23:39:39 -0600851}
852
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500853static struct page *ceph_msg_data_bio_next(struct ceph_msg_data_cursor *cursor,
Alex Elder6aaa4512013-03-06 23:39:39 -0600854 size_t *page_offset,
855 size_t *length)
856{
Ilya Dryomov5359a172018-01-20 10:30:10 +0100857 struct bio_vec bv = bio_iter_iovec(cursor->bio_iter.bio,
858 cursor->bio_iter.iter);
Alex Elder6aaa4512013-03-06 23:39:39 -0600859
Ilya Dryomov5359a172018-01-20 10:30:10 +0100860 *page_offset = bv.bv_offset;
861 *length = bv.bv_len;
862 return bv.bv_page;
Alex Elder6aaa4512013-03-06 23:39:39 -0600863}
864
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500865static bool ceph_msg_data_bio_advance(struct ceph_msg_data_cursor *cursor,
866 size_t bytes)
Alex Elder6aaa4512013-03-06 23:39:39 -0600867{
Ilya Dryomov5359a172018-01-20 10:30:10 +0100868 struct ceph_bio_iter *it = &cursor->bio_iter;
Alex Elder6aaa4512013-03-06 23:39:39 -0600869
Ilya Dryomov5359a172018-01-20 10:30:10 +0100870 BUG_ON(bytes > cursor->resid);
871 BUG_ON(bytes > bio_iter_len(it->bio, it->iter));
Alex Elder25aff7c2013-03-11 23:34:22 -0500872 cursor->resid -= bytes;
Ilya Dryomov5359a172018-01-20 10:30:10 +0100873 bio_advance_iter(it->bio, &it->iter, bytes);
Kent Overstreetf38a5182013-08-07 14:30:24 -0700874
Ilya Dryomov5359a172018-01-20 10:30:10 +0100875 if (!cursor->resid) {
876 BUG_ON(!cursor->last_piece);
877 return false; /* no more data */
878 }
Kent Overstreetf38a5182013-08-07 14:30:24 -0700879
Ilya Dryomov5359a172018-01-20 10:30:10 +0100880 if (!bytes || (it->iter.bi_size && it->iter.bi_bvec_done))
Alex Elder6aaa4512013-03-06 23:39:39 -0600881 return false; /* more bytes to process in this segment */
882
Ilya Dryomov5359a172018-01-20 10:30:10 +0100883 if (!it->iter.bi_size) {
884 it->bio = it->bio->bi_next;
885 it->iter = it->bio->bi_iter;
886 if (cursor->resid < it->iter.bi_size)
887 it->iter.bi_size = cursor->resid;
Alex Elder6aaa4512013-03-06 23:39:39 -0600888 }
Alex Elder6aaa4512013-03-06 23:39:39 -0600889
Ilya Dryomov5359a172018-01-20 10:30:10 +0100890 BUG_ON(cursor->last_piece);
891 BUG_ON(cursor->resid < bio_iter_len(it->bio, it->iter));
892 cursor->last_piece = cursor->resid == bio_iter_len(it->bio, it->iter);
Alex Elder6aaa4512013-03-06 23:39:39 -0600893 return true;
894}
Alex Elderea965712013-04-05 14:46:01 -0500895#endif /* CONFIG_BLOCK */
Alex Elderdf6ad1f2012-06-11 14:57:13 -0500896
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100897static void ceph_msg_data_bvecs_cursor_init(struct ceph_msg_data_cursor *cursor,
898 size_t length)
899{
900 struct ceph_msg_data *data = cursor->data;
901 struct bio_vec *bvecs = data->bvec_pos.bvecs;
902
903 cursor->resid = min_t(size_t, length, data->bvec_pos.iter.bi_size);
904 cursor->bvec_iter = data->bvec_pos.iter;
905 cursor->bvec_iter.bi_size = cursor->resid;
906
907 BUG_ON(cursor->resid < bvec_iter_len(bvecs, cursor->bvec_iter));
908 cursor->last_piece =
909 cursor->resid == bvec_iter_len(bvecs, cursor->bvec_iter);
910}
911
912static struct page *ceph_msg_data_bvecs_next(struct ceph_msg_data_cursor *cursor,
913 size_t *page_offset,
914 size_t *length)
915{
916 struct bio_vec bv = bvec_iter_bvec(cursor->data->bvec_pos.bvecs,
917 cursor->bvec_iter);
918
919 *page_offset = bv.bv_offset;
920 *length = bv.bv_len;
921 return bv.bv_page;
922}
923
924static bool ceph_msg_data_bvecs_advance(struct ceph_msg_data_cursor *cursor,
925 size_t bytes)
926{
927 struct bio_vec *bvecs = cursor->data->bvec_pos.bvecs;
928
929 BUG_ON(bytes > cursor->resid);
930 BUG_ON(bytes > bvec_iter_len(bvecs, cursor->bvec_iter));
931 cursor->resid -= bytes;
932 bvec_iter_advance(bvecs, &cursor->bvec_iter, bytes);
933
934 if (!cursor->resid) {
935 BUG_ON(!cursor->last_piece);
936 return false; /* no more data */
937 }
938
939 if (!bytes || cursor->bvec_iter.bi_bvec_done)
940 return false; /* more bytes to process in this segment */
941
942 BUG_ON(cursor->last_piece);
943 BUG_ON(cursor->resid < bvec_iter_len(bvecs, cursor->bvec_iter));
944 cursor->last_piece =
945 cursor->resid == bvec_iter_len(bvecs, cursor->bvec_iter);
946 return true;
947}
948
Alex Elderfe38a2b2013-03-06 23:39:39 -0600949/*
Alex Eldere766d7b2013-03-07 15:38:28 -0600950 * For a page array, a piece comes from the first page in the array
951 * that has not already been fully consumed.
952 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500953static void ceph_msg_data_pages_cursor_init(struct ceph_msg_data_cursor *cursor,
Alex Elder25aff7c2013-03-11 23:34:22 -0500954 size_t length)
Alex Eldere766d7b2013-03-07 15:38:28 -0600955{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500956 struct ceph_msg_data *data = cursor->data;
Alex Eldere766d7b2013-03-07 15:38:28 -0600957 int page_count;
958
959 BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
960
961 BUG_ON(!data->pages);
962 BUG_ON(!data->length);
963
Alex Elderca8b3a62013-04-05 14:46:01 -0500964 cursor->resid = min(length, data->length);
Alex Eldere766d7b2013-03-07 15:38:28 -0600965 page_count = calc_pages_for(data->alignment, (u64)data->length);
Alex Eldere766d7b2013-03-07 15:38:28 -0600966 cursor->page_offset = data->alignment & ~PAGE_MASK;
967 cursor->page_index = 0;
Alex Elder56fc5652013-03-30 23:46:55 -0500968 BUG_ON(page_count > (int)USHRT_MAX);
969 cursor->page_count = (unsigned short)page_count;
970 BUG_ON(length > SIZE_MAX - cursor->page_offset);
Ilya Dryomov5f740d72014-08-08 12:43:39 +0400971 cursor->last_piece = cursor->page_offset + cursor->resid <= PAGE_SIZE;
Alex Eldere766d7b2013-03-07 15:38:28 -0600972}
973
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500974static struct page *
975ceph_msg_data_pages_next(struct ceph_msg_data_cursor *cursor,
976 size_t *page_offset, size_t *length)
Alex Eldere766d7b2013-03-07 15:38:28 -0600977{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500978 struct ceph_msg_data *data = cursor->data;
Alex Eldere766d7b2013-03-07 15:38:28 -0600979
980 BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
981
982 BUG_ON(cursor->page_index >= cursor->page_count);
983 BUG_ON(cursor->page_offset >= PAGE_SIZE);
Alex Eldere766d7b2013-03-07 15:38:28 -0600984
985 *page_offset = cursor->page_offset;
Alex Elder25aff7c2013-03-11 23:34:22 -0500986 if (cursor->last_piece)
Alex Eldere766d7b2013-03-07 15:38:28 -0600987 *length = cursor->resid;
Alex Elder25aff7c2013-03-11 23:34:22 -0500988 else
Alex Eldere766d7b2013-03-07 15:38:28 -0600989 *length = PAGE_SIZE - *page_offset;
Alex Eldere766d7b2013-03-07 15:38:28 -0600990
991 return data->pages[cursor->page_index];
992}
993
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500994static bool ceph_msg_data_pages_advance(struct ceph_msg_data_cursor *cursor,
Alex Eldere766d7b2013-03-07 15:38:28 -0600995 size_t bytes)
996{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500997 BUG_ON(cursor->data->type != CEPH_MSG_DATA_PAGES);
Alex Eldere766d7b2013-03-07 15:38:28 -0600998
999 BUG_ON(cursor->page_offset + bytes > PAGE_SIZE);
Alex Eldere766d7b2013-03-07 15:38:28 -06001000
1001 /* Advance the cursor page offset */
1002
1003 cursor->resid -= bytes;
Alex Elder5df521b2013-03-30 15:09:59 -05001004 cursor->page_offset = (cursor->page_offset + bytes) & ~PAGE_MASK;
1005 if (!bytes || cursor->page_offset)
Alex Eldere766d7b2013-03-07 15:38:28 -06001006 return false; /* more bytes to process in the current page */
1007
Yan, Zhengd90deda2014-03-23 06:50:39 +08001008 if (!cursor->resid)
1009 return false; /* no more data */
1010
Alex Elder5df521b2013-03-30 15:09:59 -05001011 /* Move on to the next page; offset is already at 0 */
Alex Eldere766d7b2013-03-07 15:38:28 -06001012
1013 BUG_ON(cursor->page_index >= cursor->page_count);
Alex Eldere766d7b2013-03-07 15:38:28 -06001014 cursor->page_index++;
Alex Elder25aff7c2013-03-11 23:34:22 -05001015 cursor->last_piece = cursor->resid <= PAGE_SIZE;
Alex Eldere766d7b2013-03-07 15:38:28 -06001016
1017 return true;
1018}
1019
1020/*
Alex Elderdd236fc2013-03-06 23:39:39 -06001021 * For a pagelist, a piece is whatever remains to be consumed in the
1022 * first page in the list, or the front of the next page.
Alex Elderfe38a2b2013-03-06 23:39:39 -06001023 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001024static void
1025ceph_msg_data_pagelist_cursor_init(struct ceph_msg_data_cursor *cursor,
Alex Elder25aff7c2013-03-11 23:34:22 -05001026 size_t length)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001027{
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001028 struct ceph_msg_data *data = cursor->data;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001029 struct ceph_pagelist *pagelist;
1030 struct page *page;
1031
Alex Elderdd236fc2013-03-06 23:39:39 -06001032 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001033
1034 pagelist = data->pagelist;
1035 BUG_ON(!pagelist);
Alex Elder25aff7c2013-03-11 23:34:22 -05001036
1037 if (!length)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001038 return; /* pagelist can be assigned but empty */
1039
1040 BUG_ON(list_empty(&pagelist->head));
1041 page = list_first_entry(&pagelist->head, struct page, lru);
1042
Alex Elderca8b3a62013-04-05 14:46:01 -05001043 cursor->resid = min(length, pagelist->length);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001044 cursor->page = page;
1045 cursor->offset = 0;
Alex Eldera51b272e2013-04-19 15:34:49 -05001046 cursor->last_piece = cursor->resid <= PAGE_SIZE;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001047}
1048
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001049static struct page *
1050ceph_msg_data_pagelist_next(struct ceph_msg_data_cursor *cursor,
1051 size_t *page_offset, size_t *length)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001052{
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001053 struct ceph_msg_data *data = cursor->data;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001054 struct ceph_pagelist *pagelist;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001055
1056 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1057
1058 pagelist = data->pagelist;
1059 BUG_ON(!pagelist);
1060
1061 BUG_ON(!cursor->page);
Alex Elder25aff7c2013-03-11 23:34:22 -05001062 BUG_ON(cursor->offset + cursor->resid != pagelist->length);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001063
Alex Elder5df521b2013-03-30 15:09:59 -05001064 /* offset of first page in pagelist is always 0 */
Alex Elderfe38a2b2013-03-06 23:39:39 -06001065 *page_offset = cursor->offset & ~PAGE_MASK;
Alex Elder5df521b2013-03-30 15:09:59 -05001066 if (cursor->last_piece)
Alex Elder25aff7c2013-03-11 23:34:22 -05001067 *length = cursor->resid;
1068 else
1069 *length = PAGE_SIZE - *page_offset;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001070
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001071 return cursor->page;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001072}
1073
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001074static bool ceph_msg_data_pagelist_advance(struct ceph_msg_data_cursor *cursor,
Alex Elderdd236fc2013-03-06 23:39:39 -06001075 size_t bytes)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001076{
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001077 struct ceph_msg_data *data = cursor->data;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001078 struct ceph_pagelist *pagelist;
1079
1080 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1081
1082 pagelist = data->pagelist;
1083 BUG_ON(!pagelist);
Alex Elder25aff7c2013-03-11 23:34:22 -05001084
1085 BUG_ON(cursor->offset + cursor->resid != pagelist->length);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001086 BUG_ON((cursor->offset & ~PAGE_MASK) + bytes > PAGE_SIZE);
1087
1088 /* Advance the cursor offset */
1089
Alex Elder25aff7c2013-03-11 23:34:22 -05001090 cursor->resid -= bytes;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001091 cursor->offset += bytes;
Alex Elder5df521b2013-03-30 15:09:59 -05001092 /* offset of first page in pagelist is always 0 */
Alex Elderfe38a2b2013-03-06 23:39:39 -06001093 if (!bytes || cursor->offset & ~PAGE_MASK)
1094 return false; /* more bytes to process in the current page */
1095
Yan, Zhengd90deda2014-03-23 06:50:39 +08001096 if (!cursor->resid)
1097 return false; /* no more data */
1098
Alex Elderfe38a2b2013-03-06 23:39:39 -06001099 /* Move on to the next page */
1100
1101 BUG_ON(list_is_last(&cursor->page->lru, &pagelist->head));
Geliang Tang17ddc492015-11-16 21:46:32 +08001102 cursor->page = list_next_entry(cursor->page, lru);
Alex Elder25aff7c2013-03-11 23:34:22 -05001103 cursor->last_piece = cursor->resid <= PAGE_SIZE;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001104
1105 return true;
1106}
1107
Alex Elderdd236fc2013-03-06 23:39:39 -06001108/*
1109 * Message data is handled (sent or received) in pieces, where each
1110 * piece resides on a single page. The network layer might not
1111 * consume an entire piece at once. A data item's cursor keeps
1112 * track of which piece is next to process and how much remains to
1113 * be processed in that piece. It also tracks whether the current
1114 * piece is the last one in the data item.
1115 */
Alex Elderca8b3a62013-04-05 14:46:01 -05001116static void __ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor)
Alex Elderdd236fc2013-03-06 23:39:39 -06001117{
Alex Elderca8b3a62013-04-05 14:46:01 -05001118 size_t length = cursor->total_resid;
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001119
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001120 switch (cursor->data->type) {
Alex Elderdd236fc2013-03-06 23:39:39 -06001121 case CEPH_MSG_DATA_PAGELIST:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001122 ceph_msg_data_pagelist_cursor_init(cursor, length);
Alex Elderdd236fc2013-03-06 23:39:39 -06001123 break;
Alex Eldere766d7b2013-03-07 15:38:28 -06001124 case CEPH_MSG_DATA_PAGES:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001125 ceph_msg_data_pages_cursor_init(cursor, length);
Alex Eldere766d7b2013-03-07 15:38:28 -06001126 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001127#ifdef CONFIG_BLOCK
1128 case CEPH_MSG_DATA_BIO:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001129 ceph_msg_data_bio_cursor_init(cursor, length);
Alex Elder6aaa4512013-03-06 23:39:39 -06001130 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001131#endif /* CONFIG_BLOCK */
Ilya Dryomovb9e281c2018-01-20 10:30:11 +01001132 case CEPH_MSG_DATA_BVECS:
1133 ceph_msg_data_bvecs_cursor_init(cursor, length);
1134 break;
Alex Elder6aaa4512013-03-06 23:39:39 -06001135 case CEPH_MSG_DATA_NONE:
Alex Elderdd236fc2013-03-06 23:39:39 -06001136 default:
1137 /* BUG(); */
1138 break;
1139 }
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001140 cursor->need_crc = true;
Alex Elderdd236fc2013-03-06 23:39:39 -06001141}
1142
Alex Elderca8b3a62013-04-05 14:46:01 -05001143static void ceph_msg_data_cursor_init(struct ceph_msg *msg, size_t length)
1144{
1145 struct ceph_msg_data_cursor *cursor = &msg->cursor;
1146 struct ceph_msg_data *data;
1147
1148 BUG_ON(!length);
1149 BUG_ON(length > msg->data_length);
1150 BUG_ON(list_empty(&msg->data));
1151
Alex Elderca8b3a62013-04-05 14:46:01 -05001152 cursor->data_head = &msg->data;
1153 cursor->total_resid = length;
1154 data = list_first_entry(&msg->data, struct ceph_msg_data, links);
1155 cursor->data = data;
1156
1157 __ceph_msg_data_cursor_init(cursor);
1158}
1159
Alex Elderdd236fc2013-03-06 23:39:39 -06001160/*
1161 * Return the page containing the next piece to process for a given
1162 * data item, and supply the page offset and length of that piece.
1163 * Indicate whether this is the last piece in this data item.
1164 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001165static struct page *ceph_msg_data_next(struct ceph_msg_data_cursor *cursor,
1166 size_t *page_offset, size_t *length,
Alex Elderdd236fc2013-03-06 23:39:39 -06001167 bool *last_piece)
1168{
1169 struct page *page;
1170
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001171 switch (cursor->data->type) {
Alex Elderdd236fc2013-03-06 23:39:39 -06001172 case CEPH_MSG_DATA_PAGELIST:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001173 page = ceph_msg_data_pagelist_next(cursor, page_offset, length);
Alex Elderdd236fc2013-03-06 23:39:39 -06001174 break;
Alex Eldere766d7b2013-03-07 15:38:28 -06001175 case CEPH_MSG_DATA_PAGES:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001176 page = ceph_msg_data_pages_next(cursor, page_offset, length);
Alex Eldere766d7b2013-03-07 15:38:28 -06001177 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001178#ifdef CONFIG_BLOCK
1179 case CEPH_MSG_DATA_BIO:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001180 page = ceph_msg_data_bio_next(cursor, page_offset, length);
Alex Elder6aaa4512013-03-06 23:39:39 -06001181 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001182#endif /* CONFIG_BLOCK */
Ilya Dryomovb9e281c2018-01-20 10:30:11 +01001183 case CEPH_MSG_DATA_BVECS:
1184 page = ceph_msg_data_bvecs_next(cursor, page_offset, length);
1185 break;
Alex Elder6aaa4512013-03-06 23:39:39 -06001186 case CEPH_MSG_DATA_NONE:
Alex Elderdd236fc2013-03-06 23:39:39 -06001187 default:
1188 page = NULL;
1189 break;
1190 }
Ilya Dryomov5359a172018-01-20 10:30:10 +01001191
Alex Elderdd236fc2013-03-06 23:39:39 -06001192 BUG_ON(!page);
1193 BUG_ON(*page_offset + *length > PAGE_SIZE);
1194 BUG_ON(!*length);
Ilya Dryomov5359a172018-01-20 10:30:10 +01001195 BUG_ON(*length > cursor->resid);
Alex Elderdd236fc2013-03-06 23:39:39 -06001196 if (last_piece)
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001197 *last_piece = cursor->last_piece;
Alex Elderdd236fc2013-03-06 23:39:39 -06001198
1199 return page;
1200}
1201
1202/*
1203 * Returns true if the result moves the cursor on to the next piece
1204 * of the data item.
1205 */
Ilya Dryomov1759f7b2017-05-19 11:38:17 +02001206static void ceph_msg_data_advance(struct ceph_msg_data_cursor *cursor,
1207 size_t bytes)
Alex Elderdd236fc2013-03-06 23:39:39 -06001208{
1209 bool new_piece;
1210
Alex Elder25aff7c2013-03-11 23:34:22 -05001211 BUG_ON(bytes > cursor->resid);
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001212 switch (cursor->data->type) {
Alex Elderdd236fc2013-03-06 23:39:39 -06001213 case CEPH_MSG_DATA_PAGELIST:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001214 new_piece = ceph_msg_data_pagelist_advance(cursor, bytes);
Alex Elderdd236fc2013-03-06 23:39:39 -06001215 break;
Alex Eldere766d7b2013-03-07 15:38:28 -06001216 case CEPH_MSG_DATA_PAGES:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001217 new_piece = ceph_msg_data_pages_advance(cursor, bytes);
Alex Eldere766d7b2013-03-07 15:38:28 -06001218 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001219#ifdef CONFIG_BLOCK
1220 case CEPH_MSG_DATA_BIO:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001221 new_piece = ceph_msg_data_bio_advance(cursor, bytes);
Alex Elder6aaa4512013-03-06 23:39:39 -06001222 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001223#endif /* CONFIG_BLOCK */
Ilya Dryomovb9e281c2018-01-20 10:30:11 +01001224 case CEPH_MSG_DATA_BVECS:
1225 new_piece = ceph_msg_data_bvecs_advance(cursor, bytes);
1226 break;
Alex Elder6aaa4512013-03-06 23:39:39 -06001227 case CEPH_MSG_DATA_NONE:
Alex Elderdd236fc2013-03-06 23:39:39 -06001228 default:
1229 BUG();
1230 break;
1231 }
Alex Elderca8b3a62013-04-05 14:46:01 -05001232 cursor->total_resid -= bytes;
Alex Elderdd236fc2013-03-06 23:39:39 -06001233
Alex Elderca8b3a62013-04-05 14:46:01 -05001234 if (!cursor->resid && cursor->total_resid) {
1235 WARN_ON(!cursor->last_piece);
1236 BUG_ON(list_is_last(&cursor->data->links, cursor->data_head));
Geliang Tang17ddc492015-11-16 21:46:32 +08001237 cursor->data = list_next_entry(cursor->data, links);
Alex Elderca8b3a62013-04-05 14:46:01 -05001238 __ceph_msg_data_cursor_init(cursor);
Alex Eldera51b272e2013-04-19 15:34:49 -05001239 new_piece = true;
Alex Elderca8b3a62013-04-05 14:46:01 -05001240 }
Alex Eldera51b272e2013-04-19 15:34:49 -05001241 cursor->need_crc = new_piece;
Alex Elderdd236fc2013-03-06 23:39:39 -06001242}
1243
Ilya Dryomovdbc0d3c2016-02-19 11:38:57 +01001244static size_t sizeof_footer(struct ceph_connection *con)
1245{
1246 return (con->peer_features & CEPH_FEATURE_MSG_AUTH) ?
1247 sizeof(struct ceph_msg_footer) :
1248 sizeof(struct ceph_msg_footer_old);
1249}
1250
Alex Elder98fa5dd2013-04-02 12:09:50 -05001251static void prepare_message_data(struct ceph_msg *msg, u32 data_len)
Alex Elder739c9052012-06-11 14:57:13 -05001252{
Alex Elder739c9052012-06-11 14:57:13 -05001253 BUG_ON(!msg);
Alex Elder25aff7c2013-03-11 23:34:22 -05001254 BUG_ON(!data_len);
Alex Elder739c9052012-06-11 14:57:13 -05001255
Alex Elder4c59b4a2013-03-11 23:34:23 -05001256 /* Initialize data cursor */
Alex Elderfe38a2b2013-03-06 23:39:39 -06001257
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001258 ceph_msg_data_cursor_init(msg, (size_t)data_len);
Alex Elder739c9052012-06-11 14:57:13 -05001259}
1260
Sage Weil31b80062009-10-06 11:31:13 -07001261/*
1262 * Prepare footer for currently outgoing message, and finish things
1263 * off. Assumes out_kvec* are already valid.. we just add on to the end.
1264 */
Alex Elder859eb792012-02-14 14:05:33 -06001265static void prepare_write_message_footer(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001266{
1267 struct ceph_msg *m = con->out_msg;
1268
Alex Elderfd154f32012-06-11 14:57:13 -05001269 m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
1270
Sage Weil31b80062009-10-06 11:31:13 -07001271 dout("prepare_write_message_footer %p\n", con);
Ilya Dryomov89f08172016-02-20 15:56:07 +01001272 con_out_kvec_add(con, sizeof_footer(con), &m->footer);
Yan, Zheng33d07332014-11-04 16:33:37 +08001273 if (con->peer_features & CEPH_FEATURE_MSG_AUTH) {
1274 if (con->ops->sign_message)
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01001275 con->ops->sign_message(m);
Yan, Zheng33d07332014-11-04 16:33:37 +08001276 else
1277 m->footer.sig = 0;
Yan, Zheng33d07332014-11-04 16:33:37 +08001278 } else {
1279 m->old_footer.flags = m->footer.flags;
Yan, Zheng33d07332014-11-04 16:33:37 +08001280 }
Sage Weil31b80062009-10-06 11:31:13 -07001281 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -08001282 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -07001283}
1284
1285/*
1286 * Prepare headers for the next outgoing message.
1287 */
1288static void prepare_write_message(struct ceph_connection *con)
1289{
1290 struct ceph_msg *m;
Alex Eldera9a0c512012-02-15 07:43:54 -06001291 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001292
Alex Eldere2200422012-05-23 14:35:23 -05001293 con_out_kvec_reset(con);
Sage Weilc86a2932009-12-14 14:04:30 -08001294 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -07001295
1296 /* Sneak an ack in there first? If we can get it into the same
1297 * TCP packet that's a good thing. */
1298 if (con->in_seq > con->in_seq_acked) {
1299 con->in_seq_acked = con->in_seq;
Alex Eldere2200422012-05-23 14:35:23 -05001300 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001301 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -05001302 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -06001303 &con->out_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001304 }
1305
Alex Elder38941f82012-06-01 14:56:43 -05001306 BUG_ON(list_empty(&con->out_queue));
Alex Elder859eb792012-02-14 14:05:33 -06001307 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -08001308 con->out_msg = m;
Alex Elder38941f82012-06-01 14:56:43 -05001309 BUG_ON(m->con != con);
Sage Weil4cf9d542011-07-26 11:27:24 -07001310
1311 /* put message on sent list */
1312 ceph_msg_get(m);
1313 list_move_tail(&m->list_head, &con->out_sent);
Sage Weil31b80062009-10-06 11:31:13 -07001314
Sage Weile84346b2010-05-11 21:20:38 -07001315 /*
1316 * only assign outgoing seq # if we haven't sent this message
1317 * yet. if it is requeued, resend with it's original seq.
1318 */
1319 if (m->needs_out_seq) {
1320 m->hdr.seq = cpu_to_le64(++con->out_seq);
1321 m->needs_out_seq = false;
Ilya Dryomov98ad5eb2017-06-15 16:30:54 +02001322
Ilya Dryomov4690faf2017-07-26 09:59:15 +02001323 if (con->ops->reencode_message)
1324 con->ops->reencode_message(m);
1325 }
Sage Weil31b80062009-10-06 11:31:13 -07001326
Alex Elder98fa5dd2013-04-02 12:09:50 -05001327 dout("prepare_write_message %p seq %lld type %d len %d+%d+%zd\n",
Sage Weil31b80062009-10-06 11:31:13 -07001328 m, con->out_seq, le16_to_cpu(m->hdr.type),
1329 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
Alex Elder98fa5dd2013-04-02 12:09:50 -05001330 m->data_length);
Ilya Dryomov98ad5eb2017-06-15 16:30:54 +02001331 WARN_ON(m->front.iov_len != le32_to_cpu(m->hdr.front_len));
1332 WARN_ON(m->data_length != le32_to_cpu(m->hdr.data_len));
Sage Weil31b80062009-10-06 11:31:13 -07001333
1334 /* tag + hdr + front + middle */
Alex Eldere2200422012-05-23 14:35:23 -05001335 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
Ilya Dryomov67645d72015-12-28 13:18:34 +03001336 con_out_kvec_add(con, sizeof(con->out_hdr), &con->out_hdr);
Alex Eldere2200422012-05-23 14:35:23 -05001337 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
Alex Elder859eb792012-02-14 14:05:33 -06001338
Sage Weil31b80062009-10-06 11:31:13 -07001339 if (m->middle)
Alex Eldere2200422012-05-23 14:35:23 -05001340 con_out_kvec_add(con, m->middle->vec.iov_len,
Alex Elder859eb792012-02-14 14:05:33 -06001341 m->middle->vec.iov_base);
Sage Weil31b80062009-10-06 11:31:13 -07001342
Ilya Dryomov67645d72015-12-28 13:18:34 +03001343 /* fill in hdr crc and finalize hdr */
Alex Eldera9a0c512012-02-15 07:43:54 -06001344 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
1345 con->out_msg->hdr.crc = cpu_to_le32(crc);
Ilya Dryomov67645d72015-12-28 13:18:34 +03001346 memcpy(&con->out_hdr, &con->out_msg->hdr, sizeof(con->out_hdr));
Alex Eldera9a0c512012-02-15 07:43:54 -06001347
Ilya Dryomov67645d72015-12-28 13:18:34 +03001348 /* fill in front and middle crc, footer */
Alex Eldera9a0c512012-02-15 07:43:54 -06001349 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
1350 con->out_msg->footer.front_crc = cpu_to_le32(crc);
1351 if (m->middle) {
1352 crc = crc32c(0, m->middle->vec.iov_base,
1353 m->middle->vec.iov_len);
1354 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
1355 } else
Sage Weil31b80062009-10-06 11:31:13 -07001356 con->out_msg->footer.middle_crc = 0;
Alex Elder739c9052012-06-11 14:57:13 -05001357 dout("%s front_crc %u middle_crc %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -07001358 le32_to_cpu(con->out_msg->footer.front_crc),
1359 le32_to_cpu(con->out_msg->footer.middle_crc));
Ilya Dryomov67645d72015-12-28 13:18:34 +03001360 con->out_msg->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001361
1362 /* is there a data payload? */
Alex Elder739c9052012-06-11 14:57:13 -05001363 con->out_msg->footer.data_crc = 0;
Alex Elder98fa5dd2013-04-02 12:09:50 -05001364 if (m->data_length) {
1365 prepare_message_data(con->out_msg, m->data_length);
Alex Elder78625052013-03-06 23:39:39 -06001366 con->out_more = 1; /* data + footer will follow */
1367 } else {
Sage Weil31b80062009-10-06 11:31:13 -07001368 /* no, queue up footer too and be done */
Alex Elder859eb792012-02-14 14:05:33 -06001369 prepare_write_message_footer(con);
Alex Elder78625052013-03-06 23:39:39 -06001370 }
Sage Weil31b80062009-10-06 11:31:13 -07001371
Alex Elderc9ffc772013-02-20 10:25:12 -06001372 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07001373}
1374
1375/*
1376 * Prepare an ack.
1377 */
1378static void prepare_write_ack(struct ceph_connection *con)
1379{
1380 dout("prepare_write_ack %p %llu -> %llu\n", con,
1381 con->in_seq_acked, con->in_seq);
1382 con->in_seq_acked = con->in_seq;
1383
Alex Eldere2200422012-05-23 14:35:23 -05001384 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001385
Alex Eldere2200422012-05-23 14:35:23 -05001386 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Alex Elder859eb792012-02-14 14:05:33 -06001387
Sage Weil31b80062009-10-06 11:31:13 -07001388 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -05001389 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -06001390 &con->out_temp_ack);
1391
Sage Weil31b80062009-10-06 11:31:13 -07001392 con->out_more = 1; /* more will follow.. eventually.. */
Alex Elderc9ffc772013-02-20 10:25:12 -06001393 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07001394}
1395
1396/*
Sage Weil3a230832013-03-25 08:47:40 -07001397 * Prepare to share the seq during handshake
1398 */
1399static void prepare_write_seq(struct ceph_connection *con)
1400{
1401 dout("prepare_write_seq %p %llu -> %llu\n", con,
1402 con->in_seq_acked, con->in_seq);
1403 con->in_seq_acked = con->in_seq;
1404
1405 con_out_kvec_reset(con);
1406
1407 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
1408 con_out_kvec_add(con, sizeof (con->out_temp_ack),
1409 &con->out_temp_ack);
1410
1411 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1412}
1413
1414/*
Sage Weil31b80062009-10-06 11:31:13 -07001415 * Prepare to write keepalive byte.
1416 */
1417static void prepare_write_keepalive(struct ceph_connection *con)
1418{
1419 dout("prepare_write_keepalive %p\n", con);
Alex Eldere2200422012-05-23 14:35:23 -05001420 con_out_kvec_reset(con);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001421 if (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2) {
Deepa Dinamani1134e092017-05-08 15:59:19 -07001422 struct timespec now;
Ilya Dryomov7f61f542015-09-14 16:01:05 +03001423
Deepa Dinamani1134e092017-05-08 15:59:19 -07001424 ktime_get_real_ts(&now);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001425 con_out_kvec_add(con, sizeof(tag_keepalive2), &tag_keepalive2);
Ilya Dryomov7f61f542015-09-14 16:01:05 +03001426 ceph_encode_timespec(&con->out_temp_keepalive2, &now);
1427 con_out_kvec_add(con, sizeof(con->out_temp_keepalive2),
1428 &con->out_temp_keepalive2);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001429 } else {
1430 con_out_kvec_add(con, sizeof(tag_keepalive), &tag_keepalive);
1431 }
Alex Elderc9ffc772013-02-20 10:25:12 -06001432 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07001433}
1434
1435/*
1436 * Connection negotiation.
1437 */
1438
Alex Elderdac1e712012-05-16 15:16:39 -05001439static struct ceph_auth_handshake *get_connect_authorizer(struct ceph_connection *con,
1440 int *auth_proto)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001441{
Alex Eldera3530df2012-05-16 15:16:39 -05001442 struct ceph_auth_handshake *auth;
Alex Elderb1c6b982012-05-16 15:16:38 -05001443
1444 if (!con->ops->get_authorizer) {
1445 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
1446 con->out_connect.authorizer_len = 0;
Alex Elder729796b2012-05-16 15:16:39 -05001447 return NULL;
Alex Elderb1c6b982012-05-16 15:16:38 -05001448 }
1449
Alex Elderdac1e712012-05-16 15:16:39 -05001450 auth = con->ops->get_authorizer(con, auth_proto, con->auth_retry);
Alex Eldera3530df2012-05-16 15:16:39 -05001451 if (IS_ERR(auth))
Alex Elder729796b2012-05-16 15:16:39 -05001452 return auth;
Sage Weil0da5d702011-05-19 11:21:05 -07001453
Alex Elder8f43fb52012-05-16 15:16:39 -05001454 con->auth_reply_buf = auth->authorizer_reply_buf;
1455 con->auth_reply_buf_len = auth->authorizer_reply_buf_len;
Alex Elder729796b2012-05-16 15:16:39 -05001456 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001457}
1458
Sage Weil31b80062009-10-06 11:31:13 -07001459/*
1460 * We connected to a peer and are saying hello.
1461 */
Alex Eldere825a662012-05-16 15:16:38 -05001462static void prepare_write_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001463{
Alex Eldere2200422012-05-23 14:35:23 -05001464 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
1465 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
Alex Eldere825a662012-05-16 15:16:38 -05001466 &con->msgr->my_enc_addr);
Sage Weileed0ef22009-11-10 14:34:36 -08001467
Sage Weileed0ef22009-11-10 14:34:36 -08001468 con->out_more = 0;
Alex Elderc9ffc772013-02-20 10:25:12 -06001469 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weileed0ef22009-11-10 14:34:36 -08001470}
1471
Alex Eldere825a662012-05-16 15:16:38 -05001472static int prepare_write_connect(struct ceph_connection *con)
Sage Weileed0ef22009-11-10 14:34:36 -08001473{
Eric Dumazet95c96172012-04-15 05:58:06 +00001474 unsigned int global_seq = get_global_seq(con->msgr, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001475 int proto;
Alex Elderdac1e712012-05-16 15:16:39 -05001476 int auth_proto;
Alex Elder729796b2012-05-16 15:16:39 -05001477 struct ceph_auth_handshake *auth;
Sage Weil31b80062009-10-06 11:31:13 -07001478
1479 switch (con->peer_name.type) {
1480 case CEPH_ENTITY_TYPE_MON:
1481 proto = CEPH_MONC_PROTOCOL;
1482 break;
1483 case CEPH_ENTITY_TYPE_OSD:
1484 proto = CEPH_OSDC_PROTOCOL;
1485 break;
1486 case CEPH_ENTITY_TYPE_MDS:
1487 proto = CEPH_MDSC_PROTOCOL;
1488 break;
1489 default:
1490 BUG();
1491 }
1492
1493 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
1494 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001495
Ilya Dryomov859bff52015-10-28 23:50:58 +01001496 con->out_connect.features =
1497 cpu_to_le64(from_msgr(con->msgr)->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -07001498 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
1499 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
1500 con->out_connect.global_seq = cpu_to_le32(global_seq);
1501 con->out_connect.protocol_version = cpu_to_le32(proto);
1502 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001503
Alex Elderdac1e712012-05-16 15:16:39 -05001504 auth_proto = CEPH_AUTH_UNKNOWN;
1505 auth = get_connect_authorizer(con, &auth_proto);
Alex Elder729796b2012-05-16 15:16:39 -05001506 if (IS_ERR(auth))
1507 return PTR_ERR(auth);
Alex Elder3da54772012-05-16 15:16:39 -05001508
Alex Elderdac1e712012-05-16 15:16:39 -05001509 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
Alex Elder3da54772012-05-16 15:16:39 -05001510 con->out_connect.authorizer_len = auth ?
1511 cpu_to_le32(auth->authorizer_buf_len) : 0;
1512
Alex Eldere2200422012-05-23 14:35:23 -05001513 con_out_kvec_add(con, sizeof (con->out_connect),
Alex Elder3da54772012-05-16 15:16:39 -05001514 &con->out_connect);
1515 if (auth && auth->authorizer_buf_len)
Alex Eldere2200422012-05-23 14:35:23 -05001516 con_out_kvec_add(con, auth->authorizer_buf_len,
Alex Elder3da54772012-05-16 15:16:39 -05001517 auth->authorizer_buf);
Alex Elder859eb792012-02-14 14:05:33 -06001518
Sage Weil31b80062009-10-06 11:31:13 -07001519 con->out_more = 0;
Alex Elderc9ffc772013-02-20 10:25:12 -06001520 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001521
Alex Eldere10c7582012-05-16 15:16:38 -05001522 return 0;
Sage Weil31b80062009-10-06 11:31:13 -07001523}
1524
Sage Weil31b80062009-10-06 11:31:13 -07001525/*
1526 * write as much of pending kvecs to the socket as we can.
1527 * 1 -> done
1528 * 0 -> socket full, but more to do
1529 * <0 -> error
1530 */
1531static int write_partial_kvec(struct ceph_connection *con)
1532{
1533 int ret;
1534
1535 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
1536 while (con->out_kvec_bytes > 0) {
1537 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
1538 con->out_kvec_left, con->out_kvec_bytes,
1539 con->out_more);
1540 if (ret <= 0)
1541 goto out;
1542 con->out_kvec_bytes -= ret;
1543 if (con->out_kvec_bytes == 0)
1544 break; /* done */
Alex Elderf42299e2012-02-15 07:43:54 -06001545
1546 /* account for full iov entries consumed */
1547 while (ret >= con->out_kvec_cur->iov_len) {
1548 BUG_ON(!con->out_kvec_left);
1549 ret -= con->out_kvec_cur->iov_len;
1550 con->out_kvec_cur++;
1551 con->out_kvec_left--;
1552 }
1553 /* and for a partially-consumed entry */
1554 if (ret) {
1555 con->out_kvec_cur->iov_len -= ret;
1556 con->out_kvec_cur->iov_base += ret;
Sage Weil31b80062009-10-06 11:31:13 -07001557 }
1558 }
1559 con->out_kvec_left = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001560 ret = 1;
1561out:
1562 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
1563 con->out_kvec_bytes, con->out_kvec_left, ret);
1564 return ret; /* done! */
1565}
1566
Alex Elder35b62802013-03-08 20:59:00 -06001567static u32 ceph_crc32c_page(u32 crc, struct page *page,
1568 unsigned int page_offset,
1569 unsigned int length)
1570{
1571 char *kaddr;
1572
1573 kaddr = kmap(page);
1574 BUG_ON(kaddr == NULL);
1575 crc = crc32c(crc, kaddr + page_offset, length);
1576 kunmap(page);
1577
1578 return crc;
1579}
Sage Weil31b80062009-10-06 11:31:13 -07001580/*
1581 * Write as much message data payload as we can. If we finish, queue
1582 * up the footer.
1583 * 1 -> done, footer is now queued in out_kvec[].
1584 * 0 -> socket full, but more to do
1585 * <0 -> error
1586 */
Alex Elder34d2d202013-03-08 20:58:59 -06001587static int write_partial_message_data(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001588{
1589 struct ceph_msg *msg = con->out_msg;
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001590 struct ceph_msg_data_cursor *cursor = &msg->cursor;
Ilya Dryomov859bff52015-10-28 23:50:58 +01001591 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
Alex Elderf5db90b2013-03-11 23:34:23 -05001592 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001593
Alex Elder859a35d2013-03-11 23:34:23 -05001594 dout("%s %p msg %p\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07001595
Alex Elder5240d9f2013-03-14 14:09:06 -05001596 if (list_empty(&msg->data))
Alex Elder4c59b4a2013-03-11 23:34:23 -05001597 return -EINVAL;
1598
Alex Elder5821bd82012-06-11 14:57:13 -05001599 /*
1600 * Iterate through each page that contains data to be
1601 * written, and send as much as possible for each.
1602 *
1603 * If we are calculating the data crc (the default), we will
1604 * need to map the page. If we have no pages, they have
1605 * been revoked, so use the zero page.
1606 */
Alex Elderf5db90b2013-03-11 23:34:23 -05001607 crc = do_datacrc ? le32_to_cpu(msg->footer.data_crc) : 0;
Alex Elder643c68a2013-03-11 23:34:23 -05001608 while (cursor->resid) {
Alex Elder8a166d02013-03-08 13:35:36 -06001609 struct page *page;
Alex Eldere387d522013-03-06 23:39:38 -06001610 size_t page_offset;
1611 size_t length;
Alex Elder8a166d02013-03-08 13:35:36 -06001612 bool last_piece;
Alex Elderf5db90b2013-03-11 23:34:23 -05001613 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001614
Shraddha Barke343128c2015-10-19 21:59:00 +05301615 page = ceph_msg_data_next(cursor, &page_offset, &length,
1616 &last_piece);
Alex Eldere387d522013-03-06 23:39:38 -06001617 ret = ceph_tcp_sendpage(con->sock, page, page_offset,
Benoît Canetc2cfa192015-06-25 20:32:34 +03001618 length, !last_piece);
Alex Elderf5db90b2013-03-11 23:34:23 -05001619 if (ret <= 0) {
1620 if (do_datacrc)
1621 msg->footer.data_crc = cpu_to_le32(crc);
Sage Weil31b80062009-10-06 11:31:13 -07001622
Alex Elderf5db90b2013-03-11 23:34:23 -05001623 return ret;
1624 }
Alex Elder143334f2013-03-29 11:44:10 -05001625 if (do_datacrc && cursor->need_crc)
1626 crc = ceph_crc32c_page(crc, page, page_offset, length);
Ilya Dryomov1759f7b2017-05-19 11:38:17 +02001627 ceph_msg_data_advance(cursor, (size_t)ret);
Sage Weil31b80062009-10-06 11:31:13 -07001628 }
1629
Alex Elder34d2d202013-03-08 20:58:59 -06001630 dout("%s %p msg %p done\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07001631
1632 /* prepare and queue up footer, too */
Alex Elderf5db90b2013-03-11 23:34:23 -05001633 if (do_datacrc)
1634 msg->footer.data_crc = cpu_to_le32(crc);
1635 else
Alex Elder84ca8fc2012-06-11 14:57:13 -05001636 msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
Alex Eldere2200422012-05-23 14:35:23 -05001637 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001638 prepare_write_message_footer(con);
Alex Elderf5db90b2013-03-11 23:34:23 -05001639
1640 return 1; /* must return > 0 to indicate success */
Sage Weil31b80062009-10-06 11:31:13 -07001641}
1642
1643/*
1644 * write some zeros
1645 */
1646static int write_partial_skip(struct ceph_connection *con)
1647{
1648 int ret;
1649
Ilya Dryomov67645d72015-12-28 13:18:34 +03001650 dout("%s %p %d left\n", __func__, con, con->out_skip);
Sage Weil31b80062009-10-06 11:31:13 -07001651 while (con->out_skip > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001652 size_t size = min(con->out_skip, (int) PAGE_SIZE);
Sage Weil31b80062009-10-06 11:31:13 -07001653
Alex Eldere1dcb122013-03-06 23:39:38 -06001654 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, true);
Sage Weil31b80062009-10-06 11:31:13 -07001655 if (ret <= 0)
1656 goto out;
1657 con->out_skip -= ret;
1658 }
1659 ret = 1;
1660out:
1661 return ret;
1662}
1663
1664/*
1665 * Prepare to read connection handshake, or an ack.
1666 */
Sage Weileed0ef22009-11-10 14:34:36 -08001667static void prepare_read_banner(struct ceph_connection *con)
1668{
1669 dout("prepare_read_banner %p\n", con);
1670 con->in_base_pos = 0;
1671}
1672
Sage Weil31b80062009-10-06 11:31:13 -07001673static void prepare_read_connect(struct ceph_connection *con)
1674{
1675 dout("prepare_read_connect %p\n", con);
1676 con->in_base_pos = 0;
1677}
1678
1679static void prepare_read_ack(struct ceph_connection *con)
1680{
1681 dout("prepare_read_ack %p\n", con);
1682 con->in_base_pos = 0;
1683}
1684
Sage Weil3a230832013-03-25 08:47:40 -07001685static void prepare_read_seq(struct ceph_connection *con)
1686{
1687 dout("prepare_read_seq %p\n", con);
1688 con->in_base_pos = 0;
1689 con->in_tag = CEPH_MSGR_TAG_SEQ;
1690}
1691
Sage Weil31b80062009-10-06 11:31:13 -07001692static void prepare_read_tag(struct ceph_connection *con)
1693{
1694 dout("prepare_read_tag %p\n", con);
1695 con->in_base_pos = 0;
1696 con->in_tag = CEPH_MSGR_TAG_READY;
1697}
1698
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001699static void prepare_read_keepalive_ack(struct ceph_connection *con)
1700{
1701 dout("prepare_read_keepalive_ack %p\n", con);
1702 con->in_base_pos = 0;
1703}
1704
Sage Weil31b80062009-10-06 11:31:13 -07001705/*
1706 * Prepare to read a message.
1707 */
1708static int prepare_read_message(struct ceph_connection *con)
1709{
1710 dout("prepare_read_message %p\n", con);
1711 BUG_ON(con->in_msg != NULL);
1712 con->in_base_pos = 0;
1713 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1714 return 0;
1715}
1716
1717
1718static int read_partial(struct ceph_connection *con,
Alex Elderfd516532012-05-10 10:29:50 -05001719 int end, int size, void *object)
Sage Weil31b80062009-10-06 11:31:13 -07001720{
Alex Eldere6cee712012-05-10 10:29:50 -05001721 while (con->in_base_pos < end) {
1722 int left = end - con->in_base_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001723 int have = size - left;
1724 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1725 if (ret <= 0)
1726 return ret;
1727 con->in_base_pos += ret;
1728 }
1729 return 1;
1730}
1731
1732
1733/*
1734 * Read all or part of the connect-side handshake on a new connection
1735 */
Sage Weileed0ef22009-11-10 14:34:36 -08001736static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001737{
Alex Elderfd516532012-05-10 10:29:50 -05001738 int size;
1739 int end;
1740 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001741
Sage Weileed0ef22009-11-10 14:34:36 -08001742 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07001743
1744 /* peer's banner */
Alex Elderfd516532012-05-10 10:29:50 -05001745 size = strlen(CEPH_BANNER);
1746 end = size;
1747 ret = read_partial(con, end, size, con->in_banner);
Sage Weil31b80062009-10-06 11:31:13 -07001748 if (ret <= 0)
1749 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001750
1751 size = sizeof (con->actual_peer_addr);
1752 end += size;
1753 ret = read_partial(con, end, size, &con->actual_peer_addr);
Sage Weil31b80062009-10-06 11:31:13 -07001754 if (ret <= 0)
1755 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001756
1757 size = sizeof (con->peer_addr_for_me);
1758 end += size;
1759 ret = read_partial(con, end, size, &con->peer_addr_for_me);
Sage Weil31b80062009-10-06 11:31:13 -07001760 if (ret <= 0)
1761 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001762
Sage Weileed0ef22009-11-10 14:34:36 -08001763out:
1764 return ret;
1765}
1766
1767static int read_partial_connect(struct ceph_connection *con)
1768{
Alex Elderfd516532012-05-10 10:29:50 -05001769 int size;
1770 int end;
1771 int ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001772
1773 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1774
Alex Elderfd516532012-05-10 10:29:50 -05001775 size = sizeof (con->in_reply);
1776 end = size;
1777 ret = read_partial(con, end, size, &con->in_reply);
Sage Weil31b80062009-10-06 11:31:13 -07001778 if (ret <= 0)
1779 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001780
1781 size = le32_to_cpu(con->in_reply.authorizer_len);
1782 end += size;
1783 ret = read_partial(con, end, size, con->auth_reply_buf);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001784 if (ret <= 0)
1785 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07001786
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001787 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1788 con, (int)con->in_reply.tag,
1789 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001790 le32_to_cpu(con->in_reply.global_seq));
1791out:
1792 return ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001793
Sage Weil31b80062009-10-06 11:31:13 -07001794}
1795
1796/*
1797 * Verify the hello banner looks okay.
1798 */
1799static int verify_hello(struct ceph_connection *con)
1800{
1801 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001802 pr_err("connect to %s got bad banner\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001803 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001804 con->error_msg = "protocol error, bad banner";
1805 return -1;
1806 }
1807 return 0;
1808}
1809
1810static bool addr_is_blank(struct sockaddr_storage *ss)
1811{
Ilya Dryomovc44bd692015-07-09 13:57:52 +03001812 struct in_addr *addr = &((struct sockaddr_in *)ss)->sin_addr;
1813 struct in6_addr *addr6 = &((struct sockaddr_in6 *)ss)->sin6_addr;
1814
Sage Weil31b80062009-10-06 11:31:13 -07001815 switch (ss->ss_family) {
1816 case AF_INET:
Ilya Dryomovc44bd692015-07-09 13:57:52 +03001817 return addr->s_addr == htonl(INADDR_ANY);
Sage Weil31b80062009-10-06 11:31:13 -07001818 case AF_INET6:
Ilya Dryomovc44bd692015-07-09 13:57:52 +03001819 return ipv6_addr_any(addr6);
1820 default:
1821 return true;
Sage Weil31b80062009-10-06 11:31:13 -07001822 }
Sage Weil31b80062009-10-06 11:31:13 -07001823}
1824
1825static int addr_port(struct sockaddr_storage *ss)
1826{
1827 switch (ss->ss_family) {
1828 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001829 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -07001830 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001831 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -07001832 }
1833 return 0;
1834}
1835
1836static void addr_set_port(struct sockaddr_storage *ss, int p)
1837{
1838 switch (ss->ss_family) {
1839 case AF_INET:
1840 ((struct sockaddr_in *)ss)->sin_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001841 break;
Sage Weil31b80062009-10-06 11:31:13 -07001842 case AF_INET6:
1843 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001844 break;
Sage Weil31b80062009-10-06 11:31:13 -07001845 }
1846}
1847
1848/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001849 * Unlike other *_pton function semantics, zero indicates success.
1850 */
1851static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1852 char delim, const char **ipend)
1853{
Alex Elder99f0f3b2012-01-23 15:49:27 -06001854 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1855 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001856
1857 memset(ss, 0, sizeof(*ss));
1858
1859 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1860 ss->ss_family = AF_INET;
1861 return 0;
1862 }
1863
1864 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1865 ss->ss_family = AF_INET6;
1866 return 0;
1867 }
1868
1869 return -EINVAL;
1870}
1871
1872/*
1873 * Extract hostname string and resolve using kernel DNS facility.
1874 */
1875#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1876static int ceph_dns_resolve_name(const char *name, size_t namelen,
1877 struct sockaddr_storage *ss, char delim, const char **ipend)
1878{
1879 const char *end, *delim_p;
1880 char *colon_p, *ip_addr = NULL;
1881 int ip_len, ret;
1882
1883 /*
1884 * The end of the hostname occurs immediately preceding the delimiter or
1885 * the port marker (':') where the delimiter takes precedence.
1886 */
1887 delim_p = memchr(name, delim, namelen);
1888 colon_p = memchr(name, ':', namelen);
1889
1890 if (delim_p && colon_p)
1891 end = delim_p < colon_p ? delim_p : colon_p;
1892 else if (!delim_p && colon_p)
1893 end = colon_p;
1894 else {
1895 end = delim_p;
1896 if (!end) /* case: hostname:/ */
1897 end = name + namelen;
1898 }
1899
1900 if (end <= name)
1901 return -EINVAL;
1902
1903 /* do dns_resolve upcall */
1904 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1905 if (ip_len > 0)
1906 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1907 else
1908 ret = -ESRCH;
1909
1910 kfree(ip_addr);
1911
1912 *ipend = end;
1913
1914 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1915 ret, ret ? "failed" : ceph_pr_addr(ss));
1916
1917 return ret;
1918}
1919#else
1920static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1921 struct sockaddr_storage *ss, char delim, const char **ipend)
1922{
1923 return -EINVAL;
1924}
1925#endif
1926
1927/*
1928 * Parse a server name (IP or hostname). If a valid IP address is not found
1929 * then try to extract a hostname to resolve using userspace DNS upcall.
1930 */
1931static int ceph_parse_server_name(const char *name, size_t namelen,
1932 struct sockaddr_storage *ss, char delim, const char **ipend)
1933{
1934 int ret;
1935
1936 ret = ceph_pton(name, namelen, ss, delim, ipend);
1937 if (ret)
1938 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1939
1940 return ret;
1941}
1942
1943/*
Sage Weil31b80062009-10-06 11:31:13 -07001944 * Parse an ip[:port] list into an addr array. Use the default
1945 * monitor port if a port isn't specified.
1946 */
1947int ceph_parse_ips(const char *c, const char *end,
1948 struct ceph_entity_addr *addr,
1949 int max_count, int *count)
1950{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001951 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001952 const char *p = c;
1953
1954 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1955 for (i = 0; i < max_count; i++) {
1956 const char *ipend;
1957 struct sockaddr_storage *ss = &addr[i].in_addr;
Sage Weil31b80062009-10-06 11:31:13 -07001958 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001959 char delim = ',';
1960
1961 if (*p == '[') {
1962 delim = ']';
1963 p++;
1964 }
Sage Weil31b80062009-10-06 11:31:13 -07001965
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001966 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1967 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001968 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001969 ret = -EINVAL;
1970
Sage Weil31b80062009-10-06 11:31:13 -07001971 p = ipend;
1972
Sage Weil39139f62010-07-08 09:54:52 -07001973 if (delim == ']') {
1974 if (*p != ']') {
1975 dout("missing matching ']'\n");
1976 goto bad;
1977 }
1978 p++;
1979 }
1980
Sage Weil31b80062009-10-06 11:31:13 -07001981 /* port? */
1982 if (p < end && *p == ':') {
1983 port = 0;
1984 p++;
1985 while (p < end && *p >= '0' && *p <= '9') {
1986 port = (port * 10) + (*p - '0');
1987 p++;
1988 }
Ilya Dryomovf48db1e2013-12-30 19:21:29 +02001989 if (port == 0)
1990 port = CEPH_MON_PORT;
1991 else if (port > 65535)
Sage Weil31b80062009-10-06 11:31:13 -07001992 goto bad;
1993 } else {
1994 port = CEPH_MON_PORT;
1995 }
1996
1997 addr_set_port(ss, port);
1998
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001999 dout("parse_ips got %s\n", ceph_pr_addr(ss));
Sage Weil31b80062009-10-06 11:31:13 -07002000
2001 if (p == end)
2002 break;
2003 if (*p != ',')
2004 goto bad;
2005 p++;
2006 }
2007
2008 if (p != end)
2009 goto bad;
2010
2011 if (count)
2012 *count = i + 1;
2013 return 0;
2014
2015bad:
Sage Weil39139f62010-07-08 09:54:52 -07002016 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07002017 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002018}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002019EXPORT_SYMBOL(ceph_parse_ips);
Sage Weil31b80062009-10-06 11:31:13 -07002020
Sage Weileed0ef22009-11-10 14:34:36 -08002021static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07002022{
Sage Weileed0ef22009-11-10 14:34:36 -08002023 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07002024
2025 if (verify_hello(con) < 0)
2026 return -1;
2027
Sage Weil63f2d212009-11-03 15:17:56 -08002028 ceph_decode_addr(&con->actual_peer_addr);
2029 ceph_decode_addr(&con->peer_addr_for_me);
2030
Sage Weil31b80062009-10-06 11:31:13 -07002031 /*
2032 * Make sure the other end is who we wanted. note that the other
2033 * end may not yet know their ip address, so if it's 0.0.0.0, give
2034 * them the benefit of the doubt.
2035 */
Sage Weil103e2d32010-01-07 16:12:36 -08002036 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
2037 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07002038 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
2039 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Joe Perchesb9a67892014-09-09 21:17:29 -07002040 pr_warn("wrong peer, want %s/%d, got %s/%d\n",
2041 ceph_pr_addr(&con->peer_addr.in_addr),
2042 (int)le32_to_cpu(con->peer_addr.nonce),
2043 ceph_pr_addr(&con->actual_peer_addr.in_addr),
2044 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08002045 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07002046 return -1;
2047 }
2048
2049 /*
2050 * did we learn our address?
2051 */
2052 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
2053 int port = addr_port(&con->msgr->inst.addr.in_addr);
2054
2055 memcpy(&con->msgr->inst.addr.in_addr,
2056 &con->peer_addr_for_me.in_addr,
2057 sizeof(con->peer_addr_for_me.in_addr));
2058 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08002059 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08002060 dout("process_banner learned my addr is %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002061 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002062 }
2063
Sage Weileed0ef22009-11-10 14:34:36 -08002064 return 0;
2065}
2066
2067static int process_connect(struct ceph_connection *con)
2068{
Ilya Dryomov859bff52015-10-28 23:50:58 +01002069 u64 sup_feat = from_msgr(con->msgr)->supported_features;
2070 u64 req_feat = from_msgr(con->msgr)->required_features;
Ilya Dryomovdcbbd972017-06-05 14:44:59 +02002071 u64 server_feat = le64_to_cpu(con->in_reply.features);
Sage Weil0da5d702011-05-19 11:21:05 -07002072 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08002073
Sage Weileed0ef22009-11-10 14:34:36 -08002074 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
2075
Ilya Dryomov5c056fd2016-12-02 16:35:09 +01002076 if (con->auth_reply_buf) {
2077 /*
2078 * Any connection that defines ->get_authorizer()
2079 * should also define ->verify_authorizer_reply().
2080 * See get_connect_authorizer().
2081 */
Ilya Dryomov0dde5842016-12-02 16:35:09 +01002082 ret = con->ops->verify_authorizer_reply(con);
Ilya Dryomov5c056fd2016-12-02 16:35:09 +01002083 if (ret < 0) {
2084 con->error_msg = "bad authorize reply";
2085 return ret;
2086 }
2087 }
2088
Sage Weil31b80062009-10-06 11:31:13 -07002089 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08002090 case CEPH_MSGR_TAG_FEATURES:
2091 pr_err("%s%lld %s feature set mismatch,"
2092 " my %llx < server's %llx, missing %llx\n",
2093 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002094 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08002095 sup_feat, server_feat, server_feat & ~sup_feat);
2096 con->error_msg = "missing required protocol features";
Sage Weil0fa6ebc2012-12-27 20:27:04 -06002097 reset_connection(con);
Sage Weil04a419f2009-12-23 09:30:21 -08002098 return -1;
2099
Sage Weil31b80062009-10-06 11:31:13 -07002100 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07002101 pr_err("%s%lld %s protocol version mismatch,"
2102 " my %d != server's %d\n",
2103 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002104 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -07002105 le32_to_cpu(con->out_connect.protocol_version),
2106 le32_to_cpu(con->in_reply.protocol_version));
2107 con->error_msg = "protocol version mismatch";
Sage Weil0fa6ebc2012-12-27 20:27:04 -06002108 reset_connection(con);
Sage Weil31b80062009-10-06 11:31:13 -07002109 return -1;
2110
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002111 case CEPH_MSGR_TAG_BADAUTHORIZER:
2112 con->auth_retry++;
2113 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
2114 con->auth_retry);
2115 if (con->auth_retry == 2) {
2116 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002117 return -1;
2118 }
Jim Schutt6d4221b2012-08-10 10:37:38 -07002119 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05002120 ret = prepare_write_connect(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002121 if (ret < 0)
2122 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07002123 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002124 break;
Sage Weil31b80062009-10-06 11:31:13 -07002125
2126 case CEPH_MSGR_TAG_RESETSESSION:
2127 /*
2128 * If we connected with a large connect_seq but the peer
2129 * has no record of a session with us (no connection, or
2130 * connect_seq == 0), they will send RESETSESION to indicate
2131 * that they must have reset their session, and may have
2132 * dropped messages.
2133 */
2134 dout("process_connect got RESET peer seq %u\n",
Sage Weil5bdca4e2012-07-10 11:53:34 -07002135 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil31b80062009-10-06 11:31:13 -07002136 pr_err("%s%lld %s connection reset\n",
2137 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002138 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002139 reset_connection(con);
Jim Schutt6d4221b2012-08-10 10:37:38 -07002140 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05002141 ret = prepare_write_connect(con);
2142 if (ret < 0)
2143 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002144 prepare_read_connect(con);
2145
2146 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08002147 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002148 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
2149 if (con->ops->peer_reset)
2150 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08002151 mutex_lock(&con->mutex);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002152 if (con->state != CON_STATE_NEGOTIATING)
Sage Weil0da5d702011-05-19 11:21:05 -07002153 return -EAGAIN;
Sage Weil31b80062009-10-06 11:31:13 -07002154 break;
2155
2156 case CEPH_MSGR_TAG_RETRY_SESSION:
2157 /*
2158 * If we sent a smaller connect_seq than the peer has, try
2159 * again with a larger value.
2160 */
Sage Weil5bdca4e2012-07-10 11:53:34 -07002161 dout("process_connect got RETRY_SESSION my seq %u, peer %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07002162 le32_to_cpu(con->out_connect.connect_seq),
Sage Weil5bdca4e2012-07-10 11:53:34 -07002163 le32_to_cpu(con->in_reply.connect_seq));
2164 con->connect_seq = le32_to_cpu(con->in_reply.connect_seq);
Jim Schutt6d4221b2012-08-10 10:37:38 -07002165 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05002166 ret = prepare_write_connect(con);
2167 if (ret < 0)
2168 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002169 prepare_read_connect(con);
2170 break;
2171
2172 case CEPH_MSGR_TAG_RETRY_GLOBAL:
2173 /*
2174 * If we sent a smaller global_seq than the peer has, try
2175 * again with a larger value.
2176 */
Sage Weileed0ef22009-11-10 14:34:36 -08002177 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07002178 con->peer_global_seq,
Sage Weil5bdca4e2012-07-10 11:53:34 -07002179 le32_to_cpu(con->in_reply.global_seq));
Sage Weil31b80062009-10-06 11:31:13 -07002180 get_global_seq(con->msgr,
Sage Weil5bdca4e2012-07-10 11:53:34 -07002181 le32_to_cpu(con->in_reply.global_seq));
Jim Schutt6d4221b2012-08-10 10:37:38 -07002182 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05002183 ret = prepare_write_connect(con);
2184 if (ret < 0)
2185 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002186 prepare_read_connect(con);
2187 break;
2188
Sage Weil3a230832013-03-25 08:47:40 -07002189 case CEPH_MSGR_TAG_SEQ:
Sage Weil31b80062009-10-06 11:31:13 -07002190 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08002191 if (req_feat & ~server_feat) {
2192 pr_err("%s%lld %s protocol feature mismatch,"
2193 " my required %llx > server's %llx, need %llx\n",
2194 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002195 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08002196 req_feat, server_feat, req_feat & ~server_feat);
2197 con->error_msg = "missing required protocol features";
Sage Weil0fa6ebc2012-12-27 20:27:04 -06002198 reset_connection(con);
Sage Weil04a419f2009-12-23 09:30:21 -08002199 return -1;
2200 }
Sage Weil8dacc7d2012-07-20 17:24:40 -07002201
Alex Elder122070a2012-12-26 10:43:57 -06002202 WARN_ON(con->state != CON_STATE_NEGOTIATING);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002203 con->state = CON_STATE_OPEN;
Sage Weil20e55c42013-03-25 09:30:13 -07002204 con->auth_retry = 0; /* we authenticated; clear flag */
Sage Weil31b80062009-10-06 11:31:13 -07002205 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
2206 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07002207 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07002208 dout("process_connect got READY gseq %d cseq %d (%d)\n",
2209 con->peer_global_seq,
2210 le32_to_cpu(con->in_reply.connect_seq),
2211 con->connect_seq);
2212 WARN_ON(con->connect_seq !=
2213 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08002214
2215 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
Alex Elderc9ffc772013-02-20 10:25:12 -06002216 con_flag_set(con, CON_FLAG_LOSSYTX);
Sage Weil92ac41d2009-12-14 14:56:56 -08002217
Sage Weil85effe12012-07-30 16:22:05 -07002218 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -07002219
Sage Weil3a230832013-03-25 08:47:40 -07002220 if (con->in_reply.tag == CEPH_MSGR_TAG_SEQ) {
2221 prepare_write_seq(con);
2222 prepare_read_seq(con);
2223 } else {
2224 prepare_read_tag(con);
2225 }
Sage Weil31b80062009-10-06 11:31:13 -07002226 break;
2227
2228 case CEPH_MSGR_TAG_WAIT:
2229 /*
2230 * If there is a connection race (we are opening
2231 * connections to each other), one of us may just have
2232 * to WAIT. This shouldn't happen if we are the
2233 * client.
2234 */
Sage Weil04177882011-05-12 15:33:17 -07002235 con->error_msg = "protocol error, got WAIT as client";
2236 return -1;
Sage Weil31b80062009-10-06 11:31:13 -07002237
2238 default:
Sage Weil31b80062009-10-06 11:31:13 -07002239 con->error_msg = "protocol error, garbage tag during connect";
2240 return -1;
2241 }
2242 return 0;
2243}
2244
2245
2246/*
2247 * read (part of) an ack
2248 */
2249static int read_partial_ack(struct ceph_connection *con)
2250{
Alex Elderfd516532012-05-10 10:29:50 -05002251 int size = sizeof (con->in_temp_ack);
2252 int end = size;
Sage Weil31b80062009-10-06 11:31:13 -07002253
Alex Elderfd516532012-05-10 10:29:50 -05002254 return read_partial(con, end, size, &con->in_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07002255}
2256
Sage Weil31b80062009-10-06 11:31:13 -07002257/*
2258 * We can finally discard anything that's been acked.
2259 */
2260static void process_ack(struct ceph_connection *con)
2261{
2262 struct ceph_msg *m;
2263 u64 ack = le64_to_cpu(con->in_temp_ack);
2264 u64 seq;
Yan, Zheng0a2ad542017-05-05 18:47:37 +08002265 bool reconnect = (con->in_tag == CEPH_MSGR_TAG_SEQ);
2266 struct list_head *list = reconnect ? &con->out_queue : &con->out_sent;
Sage Weil31b80062009-10-06 11:31:13 -07002267
Yan, Zheng0a2ad542017-05-05 18:47:37 +08002268 /*
2269 * In the reconnect case, con_fault() has requeued messages
2270 * in out_sent. We should cleanup old messages according to
2271 * the reconnect seq.
2272 */
2273 while (!list_empty(list)) {
2274 m = list_first_entry(list, struct ceph_msg, list_head);
2275 if (reconnect && m->needs_out_seq)
2276 break;
Sage Weil31b80062009-10-06 11:31:13 -07002277 seq = le64_to_cpu(m->hdr.seq);
2278 if (seq > ack)
2279 break;
2280 dout("got ack for seq %llu type %d at %p\n", seq,
2281 le16_to_cpu(m->hdr.type), m);
Sage Weil4cf9d542011-07-26 11:27:24 -07002282 m->ack_stamp = jiffies;
Sage Weil31b80062009-10-06 11:31:13 -07002283 ceph_msg_remove(m);
2284 }
Yan, Zheng0a2ad542017-05-05 18:47:37 +08002285
Sage Weil31b80062009-10-06 11:31:13 -07002286 prepare_read_tag(con);
2287}
2288
2289
Yehuda Sadeh24504182010-01-08 13:58:34 -08002290static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07002291 struct kvec *section,
2292 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002293{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002294 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07002295
Yehuda Sadeh24504182010-01-08 13:58:34 -08002296 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07002297
Yehuda Sadeh24504182010-01-08 13:58:34 -08002298 while (section->iov_len < sec_len) {
2299 BUG_ON(section->iov_base == NULL);
2300 left = sec_len - section->iov_len;
2301 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
2302 section->iov_len, left);
2303 if (ret <= 0)
2304 return ret;
2305 section->iov_len += ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002306 }
Alex Elderfe3ad592012-02-15 07:43:54 -06002307 if (section->iov_len == sec_len)
2308 *crc = crc32c(0, section->iov_base, section->iov_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002309
2310 return 1;
2311}
2312
Alex Elder34d2d202013-03-08 20:58:59 -06002313static int read_partial_msg_data(struct ceph_connection *con)
2314{
2315 struct ceph_msg *msg = con->in_msg;
Alex Elder8ae4f4f2013-03-14 14:09:06 -05002316 struct ceph_msg_data_cursor *cursor = &msg->cursor;
Ilya Dryomov859bff52015-10-28 23:50:58 +01002317 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
Alex Elder686be202013-03-11 23:34:23 -05002318 struct page *page;
2319 size_t page_offset;
2320 size_t length;
Alex Elderf5db90b2013-03-11 23:34:23 -05002321 u32 crc = 0;
Alex Elder34d2d202013-03-08 20:58:59 -06002322 int ret;
2323
2324 BUG_ON(!msg);
Alex Elder5240d9f2013-03-14 14:09:06 -05002325 if (list_empty(&msg->data))
Alex Elder4c59b4a2013-03-11 23:34:23 -05002326 return -EIO;
Alex Elder34d2d202013-03-08 20:58:59 -06002327
Alex Elderf5db90b2013-03-11 23:34:23 -05002328 if (do_datacrc)
2329 crc = con->in_data_crc;
Alex Elder643c68a2013-03-11 23:34:23 -05002330 while (cursor->resid) {
Shraddha Barke343128c2015-10-19 21:59:00 +05302331 page = ceph_msg_data_next(cursor, &page_offset, &length, NULL);
Alex Elder686be202013-03-11 23:34:23 -05002332 ret = ceph_tcp_recvpage(con->sock, page, page_offset, length);
Alex Elderf5db90b2013-03-11 23:34:23 -05002333 if (ret <= 0) {
2334 if (do_datacrc)
2335 con->in_data_crc = crc;
2336
Alex Elder686be202013-03-11 23:34:23 -05002337 return ret;
Alex Elderf5db90b2013-03-11 23:34:23 -05002338 }
Alex Elder686be202013-03-11 23:34:23 -05002339
2340 if (do_datacrc)
Alex Elderf5db90b2013-03-11 23:34:23 -05002341 crc = ceph_crc32c_page(crc, page, page_offset, ret);
Ilya Dryomov1759f7b2017-05-19 11:38:17 +02002342 ceph_msg_data_advance(cursor, (size_t)ret);
Alex Elder34d2d202013-03-08 20:58:59 -06002343 }
Alex Elderf5db90b2013-03-11 23:34:23 -05002344 if (do_datacrc)
2345 con->in_data_crc = crc;
Alex Elder34d2d202013-03-08 20:58:59 -06002346
2347 return 1; /* must return > 0 to indicate success */
2348}
2349
Sage Weil31b80062009-10-06 11:31:13 -07002350/*
2351 * read (part of) a message.
2352 */
Alex Elder686be202013-03-11 23:34:23 -05002353static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip);
2354
Sage Weil31b80062009-10-06 11:31:13 -07002355static int read_partial_message(struct ceph_connection *con)
2356{
2357 struct ceph_msg *m = con->in_msg;
Alex Elderfd516532012-05-10 10:29:50 -05002358 int size;
2359 int end;
Sage Weil31b80062009-10-06 11:31:13 -07002360 int ret;
Eric Dumazet95c96172012-04-15 05:58:06 +00002361 unsigned int front_len, middle_len, data_len;
Ilya Dryomov859bff52015-10-28 23:50:58 +01002362 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
Yan, Zheng33d07332014-11-04 16:33:37 +08002363 bool need_sign = (con->peer_features & CEPH_FEATURE_MSG_AUTH);
Sage Weilae187562010-04-22 07:47:01 -07002364 u64 seq;
Alex Elderfe3ad592012-02-15 07:43:54 -06002365 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07002366
2367 dout("read_partial_message con %p msg %p\n", con, m);
2368
2369 /* header */
Alex Elderfd516532012-05-10 10:29:50 -05002370 size = sizeof (con->in_hdr);
2371 end = size;
2372 ret = read_partial(con, end, size, &con->in_hdr);
Alex Elder57dac9d2012-05-10 10:29:50 -05002373 if (ret <= 0)
2374 return ret;
Alex Elderfe3ad592012-02-15 07:43:54 -06002375
2376 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
2377 if (cpu_to_le32(crc) != con->in_hdr.crc) {
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002378 pr_err("read_partial_message bad hdr crc %u != expected %u\n",
Alex Elderfe3ad592012-02-15 07:43:54 -06002379 crc, con->in_hdr.crc);
2380 return -EBADMSG;
2381 }
2382
Sage Weil31b80062009-10-06 11:31:13 -07002383 front_len = le32_to_cpu(con->in_hdr.front_len);
2384 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
2385 return -EIO;
2386 middle_len = le32_to_cpu(con->in_hdr.middle_len);
Alex Elder7b11ba32013-03-08 18:51:03 -06002387 if (middle_len > CEPH_MSG_MAX_MIDDLE_LEN)
Sage Weil31b80062009-10-06 11:31:13 -07002388 return -EIO;
2389 data_len = le32_to_cpu(con->in_hdr.data_len);
2390 if (data_len > CEPH_MSG_MAX_DATA_LEN)
2391 return -EIO;
2392
Sage Weilae187562010-04-22 07:47:01 -07002393 /* verify seq# */
2394 seq = le64_to_cpu(con->in_hdr.seq);
2395 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07002396 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07002397 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002398 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weilae187562010-04-22 07:47:01 -07002399 seq, con->in_seq + 1);
2400 con->in_base_pos = -front_len - middle_len - data_len -
Ilya Dryomovdbc0d3c2016-02-19 11:38:57 +01002401 sizeof_footer(con);
Sage Weilae187562010-04-22 07:47:01 -07002402 con->in_tag = CEPH_MSGR_TAG_READY;
Ilya Dryomove7a88e82016-02-17 20:04:08 +01002403 return 1;
Sage Weilae187562010-04-22 07:47:01 -07002404 } else if ((s64)seq - (s64)con->in_seq > 1) {
2405 pr_err("read_partial_message bad seq %lld expected %lld\n",
2406 seq, con->in_seq + 1);
2407 con->error_msg = "bad message sequence # for incoming message";
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002408 return -EBADE;
Sage Weilae187562010-04-22 07:47:01 -07002409 }
2410
Sage Weil31b80062009-10-06 11:31:13 -07002411 /* allocate message? */
2412 if (!con->in_msg) {
Sage Weil4740a622012-07-30 18:19:30 -07002413 int skip = 0;
2414
Sage Weil31b80062009-10-06 11:31:13 -07002415 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
Alex Elder6ebc8b32013-03-08 18:51:03 -06002416 front_len, data_len);
Sage Weil4740a622012-07-30 18:19:30 -07002417 ret = ceph_con_in_msg_alloc(con, &skip);
2418 if (ret < 0)
2419 return ret;
Alex Elderf759ebb2013-04-05 14:46:01 -05002420
2421 BUG_ON(!con->in_msg ^ skip);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002422 if (skip) {
Sage Weil31b80062009-10-06 11:31:13 -07002423 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07002424 dout("alloc_msg said skip message\n");
Sage Weil31b80062009-10-06 11:31:13 -07002425 con->in_base_pos = -front_len - middle_len - data_len -
Ilya Dryomovdbc0d3c2016-02-19 11:38:57 +01002426 sizeof_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07002427 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002428 con->in_seq++;
Ilya Dryomove7a88e82016-02-17 20:04:08 +01002429 return 1;
Sage Weil31b80062009-10-06 11:31:13 -07002430 }
Alex Elder38941f82012-06-01 14:56:43 -05002431
Sage Weil4740a622012-07-30 18:19:30 -07002432 BUG_ON(!con->in_msg);
Alex Elder38941f82012-06-01 14:56:43 -05002433 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07002434 m = con->in_msg;
2435 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002436 if (m->middle)
2437 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002438
Alex Elder78625052013-03-06 23:39:39 -06002439 /* prepare for data payload, if any */
Sage Weila4107022012-07-30 16:20:25 -07002440
Alex Elder78625052013-03-06 23:39:39 -06002441 if (data_len)
Alex Elder98fa5dd2013-04-02 12:09:50 -05002442 prepare_message_data(con->in_msg, data_len);
Sage Weil31b80062009-10-06 11:31:13 -07002443 }
2444
2445 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002446 ret = read_partial_message_section(con, &m->front, front_len,
2447 &con->in_front_crc);
2448 if (ret <= 0)
2449 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002450
2451 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002452 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07002453 ret = read_partial_message_section(con, &m->middle->vec,
2454 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002455 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07002456 if (ret <= 0)
2457 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002458 }
2459
2460 /* (page) data */
Alex Elder34d2d202013-03-08 20:58:59 -06002461 if (data_len) {
2462 ret = read_partial_msg_data(con);
2463 if (ret <= 0)
2464 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002465 }
2466
Sage Weil31b80062009-10-06 11:31:13 -07002467 /* footer */
Ilya Dryomov89f08172016-02-20 15:56:07 +01002468 size = sizeof_footer(con);
Alex Elderfd516532012-05-10 10:29:50 -05002469 end += size;
2470 ret = read_partial(con, end, size, &m->footer);
Alex Elder57dac9d2012-05-10 10:29:50 -05002471 if (ret <= 0)
2472 return ret;
2473
Yan, Zheng33d07332014-11-04 16:33:37 +08002474 if (!need_sign) {
2475 m->footer.flags = m->old_footer.flags;
2476 m->footer.sig = 0;
2477 }
2478
Sage Weil31b80062009-10-06 11:31:13 -07002479 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
2480 m, front_len, m->footer.front_crc, middle_len,
2481 m->footer.middle_crc, data_len, m->footer.data_crc);
2482
2483 /* crc ok? */
2484 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
2485 pr_err("read_partial_message %p front crc %u != exp. %u\n",
2486 m, con->in_front_crc, m->footer.front_crc);
2487 return -EBADMSG;
2488 }
2489 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
2490 pr_err("read_partial_message %p middle crc %u != exp %u\n",
2491 m, con->in_middle_crc, m->footer.middle_crc);
2492 return -EBADMSG;
2493 }
Alex Elderbca064d2012-02-15 07:43:54 -06002494 if (do_datacrc &&
Sage Weil31b80062009-10-06 11:31:13 -07002495 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
2496 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
2497 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
2498 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
2499 return -EBADMSG;
2500 }
2501
Yan, Zheng33d07332014-11-04 16:33:37 +08002502 if (need_sign && con->ops->check_message_signature &&
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01002503 con->ops->check_message_signature(m)) {
Yan, Zheng33d07332014-11-04 16:33:37 +08002504 pr_err("read_partial_message %p signature check failed\n", m);
2505 return -EBADMSG;
2506 }
2507
Sage Weil31b80062009-10-06 11:31:13 -07002508 return 1; /* done! */
2509}
2510
2511/*
2512 * Process message. This happens in the worker thread. The callback should
2513 * be careful not to do anything that waits on other incoming messages or it
2514 * may deadlock.
2515 */
2516static void process_message(struct ceph_connection *con)
2517{
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01002518 struct ceph_msg *msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07002519
Alex Elder38941f82012-06-01 14:56:43 -05002520 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07002521 con->in_msg = NULL;
2522
2523 /* if first message, set peer_name */
2524 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07002525 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07002526
Sage Weil31b80062009-10-06 11:31:13 -07002527 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08002528 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002529
2530 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
2531 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07002532 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07002533 le16_to_cpu(msg->hdr.type),
2534 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2535 le32_to_cpu(msg->hdr.front_len),
2536 le32_to_cpu(msg->hdr.data_len),
2537 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
2538 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08002539
2540 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002541}
2542
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002543static int read_keepalive_ack(struct ceph_connection *con)
2544{
2545 struct ceph_timespec ceph_ts;
2546 size_t size = sizeof(ceph_ts);
2547 int ret = read_partial(con, size, size, &ceph_ts);
2548 if (ret <= 0)
2549 return ret;
2550 ceph_decode_timespec(&con->last_keepalive_ack, &ceph_ts);
2551 prepare_read_tag(con);
2552 return 1;
2553}
Sage Weil31b80062009-10-06 11:31:13 -07002554
2555/*
2556 * Write something to the socket. Called in a worker thread when the
2557 * socket appears to be writeable and we have something ready to send.
2558 */
2559static int try_write(struct ceph_connection *con)
2560{
Sage Weil31b80062009-10-06 11:31:13 -07002561 int ret = 1;
2562
Sage Weild59315c2012-06-21 12:49:23 -07002563 dout("try_write start %p state %lu\n", con, con->state);
Sage Weil31b80062009-10-06 11:31:13 -07002564
Sage Weil31b80062009-10-06 11:31:13 -07002565more:
2566 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
2567
2568 /* open the socket first? */
Sage Weil8dacc7d2012-07-20 17:24:40 -07002569 if (con->state == CON_STATE_PREOPEN) {
2570 BUG_ON(con->sock);
2571 con->state = CON_STATE_CONNECTING;
Alex Eldera5988c42012-05-29 11:04:58 -05002572
Alex Eldere2200422012-05-23 14:35:23 -05002573 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05002574 prepare_write_banner(con);
Sage Weileed0ef22009-11-10 14:34:36 -08002575 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07002576
Sage Weilcf3e5c42009-12-11 09:48:05 -08002577 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002578 con->in_tag = CEPH_MSGR_TAG_READY;
2579 dout("try_write initiating connect on %p new state %lu\n",
2580 con, con->state);
Alex Elder41617d02012-02-14 14:05:33 -06002581 ret = ceph_tcp_connect(con);
2582 if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -07002583 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -07002584 goto out;
2585 }
2586 }
2587
2588more_kvec:
2589 /* kvec data queued? */
Ilya Dryomov67645d72015-12-28 13:18:34 +03002590 if (con->out_kvec_left) {
2591 ret = write_partial_kvec(con);
Sage Weil31b80062009-10-06 11:31:13 -07002592 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08002593 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002594 }
Ilya Dryomov67645d72015-12-28 13:18:34 +03002595 if (con->out_skip) {
2596 ret = write_partial_skip(con);
Sage Weil31b80062009-10-06 11:31:13 -07002597 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08002598 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002599 }
2600
2601 /* msg pages? */
2602 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08002603 if (con->out_msg_done) {
2604 ceph_msg_put(con->out_msg);
2605 con->out_msg = NULL; /* we're done with this one */
2606 goto do_next;
2607 }
2608
Alex Elder34d2d202013-03-08 20:58:59 -06002609 ret = write_partial_message_data(con);
Sage Weil31b80062009-10-06 11:31:13 -07002610 if (ret == 1)
2611 goto more_kvec; /* we need to send the footer, too! */
2612 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08002613 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002614 if (ret < 0) {
Alex Elder34d2d202013-03-08 20:58:59 -06002615 dout("try_write write_partial_message_data err %d\n",
Sage Weil31b80062009-10-06 11:31:13 -07002616 ret);
Sage Weil42961d22011-01-25 08:19:34 -08002617 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002618 }
2619 }
2620
Sage Weilc86a2932009-12-14 14:04:30 -08002621do_next:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002622 if (con->state == CON_STATE_OPEN) {
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002623 if (con_flag_test_and_clear(con, CON_FLAG_KEEPALIVE_PENDING)) {
2624 prepare_write_keepalive(con);
2625 goto more;
2626 }
Sage Weil31b80062009-10-06 11:31:13 -07002627 /* is anything else pending? */
2628 if (!list_empty(&con->out_queue)) {
2629 prepare_write_message(con);
2630 goto more;
2631 }
2632 if (con->in_seq > con->in_seq_acked) {
2633 prepare_write_ack(con);
2634 goto more;
2635 }
Sage Weil31b80062009-10-06 11:31:13 -07002636 }
2637
2638 /* Nothing to do! */
Alex Elderc9ffc772013-02-20 10:25:12 -06002639 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07002640 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07002641 ret = 0;
2642out:
Sage Weil42961d22011-01-25 08:19:34 -08002643 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002644 return ret;
2645}
2646
2647
2648
2649/*
2650 * Read what we can from the socket.
2651 */
2652static int try_read(struct ceph_connection *con)
2653{
Sage Weil31b80062009-10-06 11:31:13 -07002654 int ret = -1;
2655
Sage Weil31b80062009-10-06 11:31:13 -07002656more:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002657 dout("try_read start on %p state %lu\n", con, con->state);
2658 if (con->state != CON_STATE_CONNECTING &&
2659 con->state != CON_STATE_NEGOTIATING &&
2660 con->state != CON_STATE_OPEN)
2661 return 0;
2662
2663 BUG_ON(!con->sock);
2664
Sage Weil31b80062009-10-06 11:31:13 -07002665 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2666 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07002667
Sage Weil8dacc7d2012-07-20 17:24:40 -07002668 if (con->state == CON_STATE_CONNECTING) {
Alex Elder7593af92012-05-24 11:55:03 -05002669 dout("try_read connecting\n");
2670 ret = read_partial_banner(con);
2671 if (ret <= 0)
Alex Elderab166d52012-05-31 11:37:29 -05002672 goto out;
Alex Elder7593af92012-05-24 11:55:03 -05002673 ret = process_banner(con);
2674 if (ret < 0)
2675 goto out;
2676
Sage Weil8dacc7d2012-07-20 17:24:40 -07002677 con->state = CON_STATE_NEGOTIATING;
Alex Elder7593af92012-05-24 11:55:03 -05002678
Jim Schutt6d4221b2012-08-10 10:37:38 -07002679 /*
2680 * Received banner is good, exchange connection info.
2681 * Do not reset out_kvec, as sending our banner raced
2682 * with receiving peer banner after connect completed.
2683 */
Alex Elder7593af92012-05-24 11:55:03 -05002684 ret = prepare_write_connect(con);
2685 if (ret < 0)
2686 goto out;
2687 prepare_read_connect(con);
2688
2689 /* Send connection info before awaiting response */
Sage Weil0da5d702011-05-19 11:21:05 -07002690 goto out;
2691 }
2692
Sage Weil8dacc7d2012-07-20 17:24:40 -07002693 if (con->state == CON_STATE_NEGOTIATING) {
Alex Elder7593af92012-05-24 11:55:03 -05002694 dout("try_read negotiating\n");
Sage Weil31b80062009-10-06 11:31:13 -07002695 ret = read_partial_connect(con);
2696 if (ret <= 0)
Sage Weil31b80062009-10-06 11:31:13 -07002697 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002698 ret = process_connect(con);
2699 if (ret < 0)
2700 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002701 goto more;
2702 }
2703
Alex Elder122070a2012-12-26 10:43:57 -06002704 WARN_ON(con->state != CON_STATE_OPEN);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002705
Sage Weil31b80062009-10-06 11:31:13 -07002706 if (con->in_base_pos < 0) {
2707 /*
2708 * skipping + discarding content.
2709 *
2710 * FIXME: there must be a better way to do this!
2711 */
Alex Elder84495f42012-02-15 07:43:55 -06002712 static char buf[SKIP_BUF_SIZE];
2713 int skip = min((int) sizeof (buf), -con->in_base_pos);
2714
Sage Weil31b80062009-10-06 11:31:13 -07002715 dout("skipping %d / %d bytes\n", skip, -con->in_base_pos);
2716 ret = ceph_tcp_recvmsg(con->sock, buf, skip);
2717 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002718 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002719 con->in_base_pos += ret;
2720 if (con->in_base_pos)
2721 goto more;
2722 }
2723 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2724 /*
2725 * what's next?
2726 */
2727 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2728 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002729 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002730 dout("try_read got tag %d\n", (int)con->in_tag);
2731 switch (con->in_tag) {
2732 case CEPH_MSGR_TAG_MSG:
2733 prepare_read_message(con);
2734 break;
2735 case CEPH_MSGR_TAG_ACK:
2736 prepare_read_ack(con);
2737 break;
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002738 case CEPH_MSGR_TAG_KEEPALIVE2_ACK:
2739 prepare_read_keepalive_ack(con);
2740 break;
Sage Weil31b80062009-10-06 11:31:13 -07002741 case CEPH_MSGR_TAG_CLOSE:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002742 con_close_socket(con);
2743 con->state = CON_STATE_CLOSED;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002744 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002745 default:
2746 goto bad_tag;
2747 }
2748 }
2749 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2750 ret = read_partial_message(con);
2751 if (ret <= 0) {
2752 switch (ret) {
2753 case -EBADMSG:
Ilya Dryomova51983e2015-10-28 23:52:06 +01002754 con->error_msg = "bad crc/signature";
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002755 /* fall through */
2756 case -EBADE:
Sage Weil31b80062009-10-06 11:31:13 -07002757 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002758 break;
Sage Weil31b80062009-10-06 11:31:13 -07002759 case -EIO:
2760 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08002761 break;
Sage Weil31b80062009-10-06 11:31:13 -07002762 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002763 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002764 }
2765 if (con->in_tag == CEPH_MSGR_TAG_READY)
2766 goto more;
2767 process_message(con);
Sage Weil7b862e02012-07-30 18:16:56 -07002768 if (con->state == CON_STATE_OPEN)
2769 prepare_read_tag(con);
Sage Weil31b80062009-10-06 11:31:13 -07002770 goto more;
2771 }
Sage Weil3a230832013-03-25 08:47:40 -07002772 if (con->in_tag == CEPH_MSGR_TAG_ACK ||
2773 con->in_tag == CEPH_MSGR_TAG_SEQ) {
2774 /*
2775 * the final handshake seq exchange is semantically
2776 * equivalent to an ACK
2777 */
Sage Weil31b80062009-10-06 11:31:13 -07002778 ret = read_partial_ack(con);
2779 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002780 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002781 process_ack(con);
2782 goto more;
2783 }
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002784 if (con->in_tag == CEPH_MSGR_TAG_KEEPALIVE2_ACK) {
2785 ret = read_keepalive_ack(con);
2786 if (ret <= 0)
2787 goto out;
2788 goto more;
2789 }
Sage Weil31b80062009-10-06 11:31:13 -07002790
Sage Weil31b80062009-10-06 11:31:13 -07002791out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002792 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002793 return ret;
2794
2795bad_tag:
2796 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2797 con->error_msg = "protocol error, garbage tag";
2798 ret = -1;
2799 goto out;
2800}
2801
2802
2803/*
Alex Elder802c6d92012-10-08 20:37:30 -07002804 * Atomically queue work on a connection after the specified delay.
2805 * Bump @con reference to avoid races with connection teardown.
2806 * Returns 0 if work was queued, or an error code otherwise.
Sage Weil31b80062009-10-06 11:31:13 -07002807 */
Alex Elder802c6d92012-10-08 20:37:30 -07002808static int queue_con_delay(struct ceph_connection *con, unsigned long delay)
Sage Weil31b80062009-10-06 11:31:13 -07002809{
Sage Weil31b80062009-10-06 11:31:13 -07002810 if (!con->ops->get(con)) {
Alex Elder802c6d92012-10-08 20:37:30 -07002811 dout("%s %p ref count 0\n", __func__, con);
Alex Elder802c6d92012-10-08 20:37:30 -07002812 return -ENOENT;
Sage Weil31b80062009-10-06 11:31:13 -07002813 }
2814
Alex Elder802c6d92012-10-08 20:37:30 -07002815 if (!queue_delayed_work(ceph_msgr_wq, &con->work, delay)) {
2816 dout("%s %p - already queued\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -07002817 con->ops->put(con);
Alex Elder802c6d92012-10-08 20:37:30 -07002818 return -EBUSY;
Sage Weil31b80062009-10-06 11:31:13 -07002819 }
Alex Elder802c6d92012-10-08 20:37:30 -07002820
2821 dout("%s %p %lu\n", __func__, con, delay);
Alex Elder802c6d92012-10-08 20:37:30 -07002822 return 0;
2823}
2824
2825static void queue_con(struct ceph_connection *con)
2826{
2827 (void) queue_con_delay(con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07002828}
2829
Ilya Dryomov37ab77a2014-06-24 16:21:45 +04002830static void cancel_con(struct ceph_connection *con)
2831{
2832 if (cancel_delayed_work(&con->work)) {
2833 dout("%s %p\n", __func__, con);
2834 con->ops->put(con);
2835 }
2836}
2837
Alex Elder7bb21d682012-12-07 19:50:07 -06002838static bool con_sock_closed(struct ceph_connection *con)
2839{
Alex Elderc9ffc772013-02-20 10:25:12 -06002840 if (!con_flag_test_and_clear(con, CON_FLAG_SOCK_CLOSED))
Alex Elder7bb21d682012-12-07 19:50:07 -06002841 return false;
2842
2843#define CASE(x) \
2844 case CON_STATE_ ## x: \
2845 con->error_msg = "socket closed (con state " #x ")"; \
2846 break;
2847
2848 switch (con->state) {
2849 CASE(CLOSED);
2850 CASE(PREOPEN);
2851 CASE(CONNECTING);
2852 CASE(NEGOTIATING);
2853 CASE(OPEN);
2854 CASE(STANDBY);
2855 default:
Joe Perchesb9a67892014-09-09 21:17:29 -07002856 pr_warn("%s con %p unrecognized state %lu\n",
Alex Elder7bb21d682012-12-07 19:50:07 -06002857 __func__, con, con->state);
2858 con->error_msg = "unrecognized con state";
2859 BUG();
2860 break;
2861 }
2862#undef CASE
2863
2864 return true;
2865}
2866
Alex Elderf20a39f2013-02-19 12:25:57 -06002867static bool con_backoff(struct ceph_connection *con)
2868{
2869 int ret;
2870
2871 if (!con_flag_test_and_clear(con, CON_FLAG_BACKOFF))
2872 return false;
2873
2874 ret = queue_con_delay(con, round_jiffies_relative(con->delay));
2875 if (ret) {
2876 dout("%s: con %p FAILED to back off %lu\n", __func__,
2877 con, con->delay);
2878 BUG_ON(ret == -ENOENT);
2879 con_flag_set(con, CON_FLAG_BACKOFF);
2880 }
2881
2882 return true;
2883}
2884
Alex Elder93209262013-02-19 12:25:57 -06002885/* Finish fault handling; con->mutex must *not* be held here */
2886
2887static void con_fault_finish(struct ceph_connection *con)
2888{
Ilya Dryomovf6330cc2016-01-13 14:32:57 +01002889 dout("%s %p\n", __func__, con);
2890
Alex Elder93209262013-02-19 12:25:57 -06002891 /*
2892 * in case we faulted due to authentication, invalidate our
2893 * current tickets so that we can get new ones.
2894 */
Ilya Dryomovf6330cc2016-01-13 14:32:57 +01002895 if (con->auth_retry) {
2896 dout("auth_retry %d, invalidating\n", con->auth_retry);
2897 if (con->ops->invalidate_authorizer)
2898 con->ops->invalidate_authorizer(con);
2899 con->auth_retry = 0;
Alex Elder93209262013-02-19 12:25:57 -06002900 }
2901
2902 if (con->ops->fault)
2903 con->ops->fault(con);
2904}
2905
Sage Weil31b80062009-10-06 11:31:13 -07002906/*
2907 * Do some work on a connection. Drop a connection ref when we're done.
2908 */
Ilya Dryomov68931622015-07-03 15:44:41 +03002909static void ceph_con_workfn(struct work_struct *work)
Sage Weil31b80062009-10-06 11:31:13 -07002910{
2911 struct ceph_connection *con = container_of(work, struct ceph_connection,
2912 work.work);
Alex Elder49659412013-02-19 12:25:57 -06002913 bool fault;
Sage Weil31b80062009-10-06 11:31:13 -07002914
Sage Weil9dd46582010-04-28 13:51:50 -07002915 mutex_lock(&con->mutex);
Alex Elder49659412013-02-19 12:25:57 -06002916 while (true) {
2917 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002918
Alex Elder49659412013-02-19 12:25:57 -06002919 if ((fault = con_sock_closed(con))) {
2920 dout("%s: con %p SOCK_CLOSED\n", __func__, con);
2921 break;
2922 }
2923 if (con_backoff(con)) {
2924 dout("%s: con %p BACKOFF\n", __func__, con);
2925 break;
2926 }
2927 if (con->state == CON_STATE_STANDBY) {
2928 dout("%s: con %p STANDBY\n", __func__, con);
2929 break;
2930 }
2931 if (con->state == CON_STATE_CLOSED) {
2932 dout("%s: con %p CLOSED\n", __func__, con);
2933 BUG_ON(con->sock);
2934 break;
2935 }
2936 if (con->state == CON_STATE_PREOPEN) {
2937 dout("%s: con %p PREOPEN\n", __func__, con);
2938 BUG_ON(con->sock);
2939 }
Sage Weil0da5d702011-05-19 11:21:05 -07002940
Alex Elder49659412013-02-19 12:25:57 -06002941 ret = try_read(con);
2942 if (ret < 0) {
2943 if (ret == -EAGAIN)
2944 continue;
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002945 if (!con->error_msg)
2946 con->error_msg = "socket error on read";
Alex Elder49659412013-02-19 12:25:57 -06002947 fault = true;
2948 break;
2949 }
2950
2951 ret = try_write(con);
2952 if (ret < 0) {
2953 if (ret == -EAGAIN)
2954 continue;
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002955 if (!con->error_msg)
2956 con->error_msg = "socket error on write";
Alex Elder49659412013-02-19 12:25:57 -06002957 fault = true;
2958 }
2959
2960 break; /* If we make it to here, we're done */
Sage Weil3a140a02012-07-30 16:24:21 -07002961 }
Alex Elderb6e7b6a2013-02-19 12:25:57 -06002962 if (fault)
2963 con_fault(con);
Sage Weil9dd46582010-04-28 13:51:50 -07002964 mutex_unlock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002965
Alex Elderb6e7b6a2013-02-19 12:25:57 -06002966 if (fault)
2967 con_fault_finish(con);
2968
2969 con->ops->put(con);
Sage Weil31b80062009-10-06 11:31:13 -07002970}
2971
Sage Weil31b80062009-10-06 11:31:13 -07002972/*
2973 * Generic error/fault handler. A retry mechanism is used with
2974 * exponential backoff
2975 */
Alex Elder93209262013-02-19 12:25:57 -06002976static void con_fault(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07002977{
Sage Weil31b80062009-10-06 11:31:13 -07002978 dout("fault %p state %lu to peer %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002979 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002980
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002981 pr_warn("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
2982 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
2983 con->error_msg = NULL;
2984
Alex Elder122070a2012-12-26 10:43:57 -06002985 WARN_ON(con->state != CON_STATE_CONNECTING &&
Sage Weil8dacc7d2012-07-20 17:24:40 -07002986 con->state != CON_STATE_NEGOTIATING &&
2987 con->state != CON_STATE_OPEN);
Sage Weilec302642009-12-22 10:43:42 -08002988
Sage Weil31b80062009-10-06 11:31:13 -07002989 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08002990
Alex Elderc9ffc772013-02-20 10:25:12 -06002991 if (con_flag_test(con, CON_FLAG_LOSSYTX)) {
Sage Weil8dacc7d2012-07-20 17:24:40 -07002992 dout("fault on LOSSYTX channel, marking CLOSED\n");
2993 con->state = CON_STATE_CLOSED;
Alex Elder93209262013-02-19 12:25:57 -06002994 return;
Sage Weil3b5ede02012-07-20 15:22:53 -07002995 }
2996
Sage Weil5e095e82009-12-14 14:30:34 -08002997 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -05002998 BUG_ON(con->in_msg->con != con);
Sage Weil5e095e82009-12-14 14:30:34 -08002999 ceph_msg_put(con->in_msg);
3000 con->in_msg = NULL;
3001 }
Sage Weil31b80062009-10-06 11:31:13 -07003002
Sage Weile80a52d2010-02-25 12:40:45 -08003003 /* Requeue anything that hasn't been acked */
3004 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003005
Sage Weile76661d2011-03-03 10:10:15 -08003006 /* If there are no messages queued or keepalive pending, place
3007 * the connection in a STANDBY state */
3008 if (list_empty(&con->out_queue) &&
Alex Elderc9ffc772013-02-20 10:25:12 -06003009 !con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING)) {
Sage Weile00de342011-03-04 12:25:05 -08003010 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
Alex Elderc9ffc772013-02-20 10:25:12 -06003011 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
Sage Weil8dacc7d2012-07-20 17:24:40 -07003012 con->state = CON_STATE_STANDBY;
Sage Weile80a52d2010-02-25 12:40:45 -08003013 } else {
3014 /* retry after a delay. */
Sage Weil8dacc7d2012-07-20 17:24:40 -07003015 con->state = CON_STATE_PREOPEN;
Sage Weile80a52d2010-02-25 12:40:45 -08003016 if (con->delay == 0)
3017 con->delay = BASE_DELAY_INTERVAL;
3018 else if (con->delay < MAX_DELAY_INTERVAL)
3019 con->delay *= 2;
Alex Elderc9ffc772013-02-20 10:25:12 -06003020 con_flag_set(con, CON_FLAG_BACKOFF);
Alex Elder8618e302012-10-08 20:37:30 -07003021 queue_con(con);
Sage Weil31b80062009-10-06 11:31:13 -07003022 }
Sage Weil31b80062009-10-06 11:31:13 -07003023}
3024
3025
3026
3027/*
Alex Elder15d98822012-05-26 23:26:43 -05003028 * initialize a new messenger instance
Sage Weil31b80062009-10-06 11:31:13 -07003029 */
Alex Elder15d98822012-05-26 23:26:43 -05003030void ceph_messenger_init(struct ceph_messenger *msgr,
Ilya Dryomov859bff52015-10-28 23:50:58 +01003031 struct ceph_entity_addr *myaddr)
Sage Weil31b80062009-10-06 11:31:13 -07003032{
Sage Weil31b80062009-10-06 11:31:13 -07003033 spin_lock_init(&msgr->global_seq_lock);
3034
Sage Weil31b80062009-10-06 11:31:13 -07003035 if (myaddr)
3036 msgr->inst.addr = *myaddr;
3037
3038 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08003039 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08003040 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08003041 encode_my_addr(msgr);
Sage Weil31b80062009-10-06 11:31:13 -07003042
Guanjun Hea2a32582012-07-08 19:50:33 -07003043 atomic_set(&msgr->stopping, 0);
Ilya Dryomov757856d2015-06-25 17:47:45 +03003044 write_pnet(&msgr->net, get_net(current->nsproxy->net_ns));
Sage Weil31b80062009-10-06 11:31:13 -07003045
Alex Elder15d98822012-05-26 23:26:43 -05003046 dout("%s %p\n", __func__, msgr);
Sage Weil31b80062009-10-06 11:31:13 -07003047}
Alex Elder15d98822012-05-26 23:26:43 -05003048EXPORT_SYMBOL(ceph_messenger_init);
Sage Weil31b80062009-10-06 11:31:13 -07003049
Ilya Dryomov757856d2015-06-25 17:47:45 +03003050void ceph_messenger_fini(struct ceph_messenger *msgr)
3051{
3052 put_net(read_pnet(&msgr->net));
3053}
3054EXPORT_SYMBOL(ceph_messenger_fini);
3055
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003056static void msg_con_set(struct ceph_msg *msg, struct ceph_connection *con)
3057{
3058 if (msg->con)
3059 msg->con->ops->put(msg->con);
3060
3061 msg->con = con ? con->ops->get(con) : NULL;
3062 BUG_ON(msg->con != con);
3063}
3064
Sage Weile00de342011-03-04 12:25:05 -08003065static void clear_standby(struct ceph_connection *con)
3066{
3067 /* come back from STANDBY? */
Sage Weil8dacc7d2012-07-20 17:24:40 -07003068 if (con->state == CON_STATE_STANDBY) {
Sage Weile00de342011-03-04 12:25:05 -08003069 dout("clear_standby %p and ++connect_seq\n", con);
Sage Weil8dacc7d2012-07-20 17:24:40 -07003070 con->state = CON_STATE_PREOPEN;
Sage Weile00de342011-03-04 12:25:05 -08003071 con->connect_seq++;
Alex Elderc9ffc772013-02-20 10:25:12 -06003072 WARN_ON(con_flag_test(con, CON_FLAG_WRITE_PENDING));
3073 WARN_ON(con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING));
Sage Weile00de342011-03-04 12:25:05 -08003074 }
3075}
3076
Sage Weil31b80062009-10-06 11:31:13 -07003077/*
3078 * Queue up an outgoing message on the given connection.
3079 */
3080void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
3081{
Sage Weila59b55a2012-07-20 15:34:04 -07003082 /* set src+dst */
3083 msg->hdr.src = con->msgr->inst.name;
3084 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
3085 msg->needs_out_seq = true;
3086
3087 mutex_lock(&con->mutex);
3088
Sage Weil8dacc7d2012-07-20 17:24:40 -07003089 if (con->state == CON_STATE_CLOSED) {
Sage Weil31b80062009-10-06 11:31:13 -07003090 dout("con_send %p closed, dropping %p\n", con, msg);
3091 ceph_msg_put(msg);
Sage Weila59b55a2012-07-20 15:34:04 -07003092 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003093 return;
3094 }
3095
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003096 msg_con_set(msg, con);
Sage Weil31b80062009-10-06 11:31:13 -07003097
Sage Weil31b80062009-10-06 11:31:13 -07003098 BUG_ON(!list_empty(&msg->list_head));
3099 list_add_tail(&msg->list_head, &con->out_queue);
3100 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
3101 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
3102 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
3103 le32_to_cpu(msg->hdr.front_len),
3104 le32_to_cpu(msg->hdr.middle_len),
3105 le32_to_cpu(msg->hdr.data_len));
Sage Weil00650932012-07-20 15:33:04 -07003106
3107 clear_standby(con);
Sage Weilec302642009-12-22 10:43:42 -08003108 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003109
3110 /* if there wasn't anything waiting to send before, queue
3111 * new work */
Alex Elderc9ffc772013-02-20 10:25:12 -06003112 if (con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07003113 queue_con(con);
3114}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003115EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07003116
3117/*
3118 * Revoke a message that was previously queued for send
3119 */
Alex Elder6740a842012-06-01 14:56:43 -05003120void ceph_msg_revoke(struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07003121{
Alex Elder6740a842012-06-01 14:56:43 -05003122 struct ceph_connection *con = msg->con;
3123
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003124 if (!con) {
3125 dout("%s msg %p null con\n", __func__, msg);
Alex Elder6740a842012-06-01 14:56:43 -05003126 return; /* Message not in our possession */
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003127 }
Alex Elder6740a842012-06-01 14:56:43 -05003128
Sage Weilec302642009-12-22 10:43:42 -08003129 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003130 if (!list_empty(&msg->list_head)) {
Alex Elder38941f82012-06-01 14:56:43 -05003131 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07003132 list_del_init(&msg->list_head);
Sage Weil31b80062009-10-06 11:31:13 -07003133 msg->hdr.seq = 0;
Alex Elder38941f82012-06-01 14:56:43 -05003134
Sage Weil31b80062009-10-06 11:31:13 -07003135 ceph_msg_put(msg);
Sage Weiled98ada2010-07-05 12:15:14 -07003136 }
3137 if (con->out_msg == msg) {
Ilya Dryomov67645d72015-12-28 13:18:34 +03003138 BUG_ON(con->out_skip);
3139 /* footer */
3140 if (con->out_msg_done) {
3141 con->out_skip += con_out_kvec_skip(con);
3142 } else {
3143 BUG_ON(!msg->data_length);
Ilya Dryomov89f08172016-02-20 15:56:07 +01003144 con->out_skip += sizeof_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07003145 }
Ilya Dryomov67645d72015-12-28 13:18:34 +03003146 /* data, middle, front */
3147 if (msg->data_length)
3148 con->out_skip += msg->cursor.total_resid;
3149 if (msg->middle)
3150 con->out_skip += con_out_kvec_skip(con);
3151 con->out_skip += con_out_kvec_skip(con);
Alex Elder92ce0342012-06-04 14:43:33 -05003152
Ilya Dryomov67645d72015-12-28 13:18:34 +03003153 dout("%s %p msg %p - was sending, will write %d skip %d\n",
3154 __func__, con, msg, con->out_kvec_bytes, con->out_skip);
3155 msg->hdr.seq = 0;
3156 con->out_msg = NULL;
Alex Elder92ce0342012-06-04 14:43:33 -05003157 ceph_msg_put(msg);
Sage Weil31b80062009-10-06 11:31:13 -07003158 }
Ilya Dryomov67645d72015-12-28 13:18:34 +03003159
Sage Weilec302642009-12-22 10:43:42 -08003160 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003161}
3162
3163/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08003164 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08003165 */
Alex Elder8921d112012-06-01 14:56:43 -05003166void ceph_msg_revoke_incoming(struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08003167{
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003168 struct ceph_connection *con = msg->con;
Alex Elder8921d112012-06-01 14:56:43 -05003169
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003170 if (!con) {
Alex Elder8921d112012-06-01 14:56:43 -05003171 dout("%s msg %p null con\n", __func__, msg);
Alex Elder8921d112012-06-01 14:56:43 -05003172 return; /* Message not in our possession */
3173 }
3174
Sage Weil350b1c32009-12-22 10:45:45 -08003175 mutex_lock(&con->mutex);
Alex Elder8921d112012-06-01 14:56:43 -05003176 if (con->in_msg == msg) {
Eric Dumazet95c96172012-04-15 05:58:06 +00003177 unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
3178 unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
3179 unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
Sage Weil350b1c32009-12-22 10:45:45 -08003180
3181 /* skip rest of message */
Alex Elder8921d112012-06-01 14:56:43 -05003182 dout("%s %p msg %p revoked\n", __func__, con, msg);
3183 con->in_base_pos = con->in_base_pos -
Sage Weil350b1c32009-12-22 10:45:45 -08003184 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08003185 front_len -
3186 middle_len -
3187 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08003188 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08003189 ceph_msg_put(con->in_msg);
3190 con->in_msg = NULL;
3191 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07003192 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08003193 } else {
Alex Elder8921d112012-06-01 14:56:43 -05003194 dout("%s %p in_msg %p msg %p no-op\n",
3195 __func__, con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08003196 }
3197 mutex_unlock(&con->mutex);
3198}
3199
3200/*
Sage Weil31b80062009-10-06 11:31:13 -07003201 * Queue a keepalive byte to ensure the tcp connection is alive.
3202 */
3203void ceph_con_keepalive(struct ceph_connection *con)
3204{
Sage Weile00de342011-03-04 12:25:05 -08003205 dout("con_keepalive %p\n", con);
Sage Weil00650932012-07-20 15:33:04 -07003206 mutex_lock(&con->mutex);
Sage Weile00de342011-03-04 12:25:05 -08003207 clear_standby(con);
Sage Weil00650932012-07-20 15:33:04 -07003208 mutex_unlock(&con->mutex);
Alex Elderc9ffc772013-02-20 10:25:12 -06003209 if (con_flag_test_and_set(con, CON_FLAG_KEEPALIVE_PENDING) == 0 &&
3210 con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07003211 queue_con(con);
3212}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003213EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07003214
Yan, Zheng8b9558a2015-09-01 17:19:38 +08003215bool ceph_con_keepalive_expired(struct ceph_connection *con,
3216 unsigned long interval)
3217{
3218 if (interval > 0 &&
3219 (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2)) {
Deepa Dinamani1134e092017-05-08 15:59:19 -07003220 struct timespec now;
Yan, Zheng8b9558a2015-09-01 17:19:38 +08003221 struct timespec ts;
Deepa Dinamani1134e092017-05-08 15:59:19 -07003222 ktime_get_real_ts(&now);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08003223 jiffies_to_timespec(interval, &ts);
3224 ts = timespec_add(con->last_keepalive_ack, ts);
3225 return timespec_compare(&now, &ts) >= 0;
3226 }
3227 return false;
3228}
3229
Alex Elder6644ed72013-03-11 23:34:24 -05003230static struct ceph_msg_data *ceph_msg_data_create(enum ceph_msg_data_type type)
Alex Elder43794502013-03-01 18:00:16 -06003231{
Alex Elder6644ed72013-03-11 23:34:24 -05003232 struct ceph_msg_data *data;
3233
3234 if (WARN_ON(!ceph_msg_data_type_valid(type)))
3235 return NULL;
3236
Alex Elder81b36be2013-05-01 12:43:04 -05003237 data = kmem_cache_zalloc(ceph_msg_data_cache, GFP_NOFS);
Dan Carpenter7c40b222017-07-17 11:13:35 +03003238 if (!data)
3239 return NULL;
3240
3241 data->type = type;
Alex Elder5240d9f2013-03-14 14:09:06 -05003242 INIT_LIST_HEAD(&data->links);
Alex Elder6644ed72013-03-11 23:34:24 -05003243
3244 return data;
3245}
3246
3247static void ceph_msg_data_destroy(struct ceph_msg_data *data)
3248{
3249 if (!data)
3250 return;
3251
Alex Elder5240d9f2013-03-14 14:09:06 -05003252 WARN_ON(!list_empty(&data->links));
Yan, Zhenge4339d282014-09-16 17:50:45 +08003253 if (data->type == CEPH_MSG_DATA_PAGELIST)
Alex Elder6644ed72013-03-11 23:34:24 -05003254 ceph_pagelist_release(data->pagelist);
Alex Elder81b36be2013-05-01 12:43:04 -05003255 kmem_cache_free(ceph_msg_data_cache, data);
Alex Elder43794502013-03-01 18:00:16 -06003256}
3257
Alex Elder90af3602013-04-05 14:46:01 -05003258void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
Alex Elderf1baeb22013-03-07 15:38:26 -06003259 size_t length, size_t alignment)
Alex Elder02afca62013-02-14 12:16:43 -06003260{
Alex Elder6644ed72013-03-11 23:34:24 -05003261 struct ceph_msg_data *data;
3262
Alex Elder07aa1552013-03-04 18:29:06 -06003263 BUG_ON(!pages);
3264 BUG_ON(!length);
Alex Elder02afca62013-02-14 12:16:43 -06003265
Alex Elder6644ed72013-03-11 23:34:24 -05003266 data = ceph_msg_data_create(CEPH_MSG_DATA_PAGES);
3267 BUG_ON(!data);
3268 data->pages = pages;
3269 data->length = length;
3270 data->alignment = alignment & ~PAGE_MASK;
3271
Alex Elder5240d9f2013-03-14 14:09:06 -05003272 list_add_tail(&data->links, &msg->data);
3273 msg->data_length += length;
Alex Elder02afca62013-02-14 12:16:43 -06003274}
Alex Elder90af3602013-04-05 14:46:01 -05003275EXPORT_SYMBOL(ceph_msg_data_add_pages);
Sage Weil31b80062009-10-06 11:31:13 -07003276
Alex Elder90af3602013-04-05 14:46:01 -05003277void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
Alex Elder27fa8382013-02-14 12:16:43 -06003278 struct ceph_pagelist *pagelist)
3279{
Alex Elder6644ed72013-03-11 23:34:24 -05003280 struct ceph_msg_data *data;
3281
Alex Elder07aa1552013-03-04 18:29:06 -06003282 BUG_ON(!pagelist);
3283 BUG_ON(!pagelist->length);
Alex Elder27fa8382013-02-14 12:16:43 -06003284
Alex Elder6644ed72013-03-11 23:34:24 -05003285 data = ceph_msg_data_create(CEPH_MSG_DATA_PAGELIST);
3286 BUG_ON(!data);
3287 data->pagelist = pagelist;
3288
Alex Elder5240d9f2013-03-14 14:09:06 -05003289 list_add_tail(&data->links, &msg->data);
3290 msg->data_length += pagelist->length;
Alex Elder27fa8382013-02-14 12:16:43 -06003291}
Alex Elder90af3602013-04-05 14:46:01 -05003292EXPORT_SYMBOL(ceph_msg_data_add_pagelist);
Alex Elder27fa8382013-02-14 12:16:43 -06003293
Alex Elderea965712013-04-05 14:46:01 -05003294#ifdef CONFIG_BLOCK
Ilya Dryomov5359a172018-01-20 10:30:10 +01003295void ceph_msg_data_add_bio(struct ceph_msg *msg, struct ceph_bio_iter *bio_pos,
3296 u32 length)
Alex Elder27fa8382013-02-14 12:16:43 -06003297{
Alex Elder6644ed72013-03-11 23:34:24 -05003298 struct ceph_msg_data *data;
Alex Elder27fa8382013-02-14 12:16:43 -06003299
Alex Elder6644ed72013-03-11 23:34:24 -05003300 data = ceph_msg_data_create(CEPH_MSG_DATA_BIO);
3301 BUG_ON(!data);
Ilya Dryomov5359a172018-01-20 10:30:10 +01003302 data->bio_pos = *bio_pos;
Alex Elderc851c492013-04-05 14:46:01 -05003303 data->bio_length = length;
Alex Elder6644ed72013-03-11 23:34:24 -05003304
Alex Elder5240d9f2013-03-14 14:09:06 -05003305 list_add_tail(&data->links, &msg->data);
3306 msg->data_length += length;
Alex Elder27fa8382013-02-14 12:16:43 -06003307}
Alex Elder90af3602013-04-05 14:46:01 -05003308EXPORT_SYMBOL(ceph_msg_data_add_bio);
Alex Elderea965712013-04-05 14:46:01 -05003309#endif /* CONFIG_BLOCK */
Alex Elder27fa8382013-02-14 12:16:43 -06003310
Ilya Dryomovb9e281c2018-01-20 10:30:11 +01003311void ceph_msg_data_add_bvecs(struct ceph_msg *msg,
3312 struct ceph_bvec_iter *bvec_pos)
3313{
3314 struct ceph_msg_data *data;
3315
3316 data = ceph_msg_data_create(CEPH_MSG_DATA_BVECS);
3317 BUG_ON(!data);
3318 data->bvec_pos = *bvec_pos;
3319
3320 list_add_tail(&data->links, &msg->data);
3321 msg->data_length += bvec_pos->iter.bi_size;
3322}
3323EXPORT_SYMBOL(ceph_msg_data_add_bvecs);
3324
Sage Weil31b80062009-10-06 11:31:13 -07003325/*
3326 * construct a new message with given type, size
3327 * the new msg has a ref count of 1.
3328 */
Sage Weilb61c2762011-08-09 15:03:46 -07003329struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
3330 bool can_fail)
Sage Weil31b80062009-10-06 11:31:13 -07003331{
3332 struct ceph_msg *m;
3333
Alex Eldere3d5d632013-05-01 12:43:04 -05003334 m = kmem_cache_zalloc(ceph_msg_cache, flags);
Sage Weil31b80062009-10-06 11:31:13 -07003335 if (m == NULL)
3336 goto out;
Alex Elder38941f82012-06-01 14:56:43 -05003337
Sage Weil31b80062009-10-06 11:31:13 -07003338 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07003339 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
Sage Weil31b80062009-10-06 11:31:13 -07003340 m->hdr.front_len = cpu_to_le32(front_len);
Sage Weil31b80062009-10-06 11:31:13 -07003341
Alex Elder9516e452013-03-01 18:00:16 -06003342 INIT_LIST_HEAD(&m->list_head);
3343 kref_init(&m->kref);
Alex Elder5240d9f2013-03-14 14:09:06 -05003344 INIT_LIST_HEAD(&m->data);
Henry C Changca208922011-05-03 02:29:56 +00003345
Sage Weil31b80062009-10-06 11:31:13 -07003346 /* front */
3347 if (front_len) {
Ilya Dryomoveeb0bed2014-01-09 20:08:21 +02003348 m->front.iov_base = ceph_kvmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07003349 if (m->front.iov_base == NULL) {
Sage Weilb61c2762011-08-09 15:03:46 -07003350 dout("ceph_msg_new can't allocate %d bytes\n",
Sage Weil31b80062009-10-06 11:31:13 -07003351 front_len);
3352 goto out2;
3353 }
3354 } else {
3355 m->front.iov_base = NULL;
3356 }
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02003357 m->front_alloc_len = m->front.iov_len = front_len;
Sage Weil31b80062009-10-06 11:31:13 -07003358
Sage Weilbb257662010-04-01 16:07:23 -07003359 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07003360 return m;
3361
3362out2:
3363 ceph_msg_put(m);
3364out:
Sage Weilb61c2762011-08-09 15:03:46 -07003365 if (!can_fail) {
3366 pr_err("msg_new can't create type %d front %d\n", type,
3367 front_len);
Sage Weilf0ed1b72011-08-09 15:05:07 -07003368 WARN_ON(1);
Sage Weilb61c2762011-08-09 15:03:46 -07003369 } else {
3370 dout("msg_new can't create type %d front %d\n", type,
3371 front_len);
3372 }
Sage Weila79832f2010-04-01 16:06:19 -07003373 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07003374}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003375EXPORT_SYMBOL(ceph_msg_new);
Sage Weil31b80062009-10-06 11:31:13 -07003376
3377/*
Sage Weil31b80062009-10-06 11:31:13 -07003378 * Allocate "middle" portion of a message, if it is needed and wasn't
3379 * allocated by alloc_msg. This allows us to read a small fixed-size
3380 * per-type header in the front and then gracefully fail (i.e.,
3381 * propagate the error to the caller based on info in the front) when
3382 * the middle is too large.
3383 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08003384static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07003385{
3386 int type = le16_to_cpu(msg->hdr.type);
3387 int middle_len = le32_to_cpu(msg->hdr.middle_len);
3388
3389 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
3390 ceph_msg_type_name(type), middle_len);
3391 BUG_ON(!middle_len);
3392 BUG_ON(msg->middle);
3393
Sage Weilb6c1d5b2009-12-07 12:17:17 -08003394 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07003395 if (!msg->middle)
3396 return -ENOMEM;
3397 return 0;
3398}
3399
Yehuda Sadeh24504182010-01-08 13:58:34 -08003400/*
Alex Elder1c20f2d2012-06-04 14:43:32 -05003401 * Allocate a message for receiving an incoming message on a
3402 * connection, and save the result in con->in_msg. Uses the
3403 * connection's private alloc_msg op if available.
3404 *
Sage Weil4740a622012-07-30 18:19:30 -07003405 * Returns 0 on success, or a negative error code.
3406 *
3407 * On success, if we set *skip = 1:
3408 * - the next message should be skipped and ignored.
3409 * - con->in_msg == NULL
3410 * or if we set *skip = 0:
3411 * - con->in_msg is non-null.
3412 * On error (ENOMEM, EAGAIN, ...),
3413 * - con->in_msg == NULL
Yehuda Sadeh24504182010-01-08 13:58:34 -08003414 */
Sage Weil4740a622012-07-30 18:19:30 -07003415static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip)
Yehuda Sadeh24504182010-01-08 13:58:34 -08003416{
Sage Weil4740a622012-07-30 18:19:30 -07003417 struct ceph_msg_header *hdr = &con->in_hdr;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003418 int middle_len = le32_to_cpu(hdr->middle_len);
Alex Elder1d866d12013-03-01 18:00:14 -06003419 struct ceph_msg *msg;
Sage Weil4740a622012-07-30 18:19:30 -07003420 int ret = 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003421
Alex Elder1c20f2d2012-06-04 14:43:32 -05003422 BUG_ON(con->in_msg != NULL);
Alex Elder53ded492013-03-01 18:00:14 -06003423 BUG_ON(!con->ops->alloc_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08003424
Alex Elder53ded492013-03-01 18:00:14 -06003425 mutex_unlock(&con->mutex);
3426 msg = con->ops->alloc_msg(con, hdr, skip);
3427 mutex_lock(&con->mutex);
3428 if (con->state != CON_STATE_OPEN) {
3429 if (msg)
Alex Elder1d866d12013-03-01 18:00:14 -06003430 ceph_msg_put(msg);
Alex Elder53ded492013-03-01 18:00:14 -06003431 return -EAGAIN;
3432 }
Alex Elder41375772013-03-05 09:25:10 -06003433 if (msg) {
3434 BUG_ON(*skip);
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003435 msg_con_set(msg, con);
Alex Elder41375772013-03-05 09:25:10 -06003436 con->in_msg = msg;
Alex Elder41375772013-03-05 09:25:10 -06003437 } else {
3438 /*
3439 * Null message pointer means either we should skip
3440 * this message or we couldn't allocate memory. The
3441 * former is not an error.
3442 */
3443 if (*skip)
3444 return 0;
Alex Elder41375772013-03-05 09:25:10 -06003445
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03003446 con->error_msg = "error allocating memory for incoming message";
Alex Elder53ded492013-03-01 18:00:14 -06003447 return -ENOMEM;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003448 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05003449 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08003450
Alex Elder1c20f2d2012-06-04 14:43:32 -05003451 if (middle_len && !con->in_msg->middle) {
3452 ret = ceph_alloc_middle(con, con->in_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08003453 if (ret < 0) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05003454 ceph_msg_put(con->in_msg);
3455 con->in_msg = NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003456 }
3457 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08003458
Sage Weil4740a622012-07-30 18:19:30 -07003459 return ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003460}
3461
Sage Weil31b80062009-10-06 11:31:13 -07003462
3463/*
3464 * Free a generically kmalloc'd message.
3465 */
Ilya Dryomov0215e442014-06-20 14:14:41 +04003466static void ceph_msg_free(struct ceph_msg *m)
Sage Weil31b80062009-10-06 11:31:13 -07003467{
Ilya Dryomov0215e442014-06-20 14:14:41 +04003468 dout("%s %p\n", __func__, m);
Ilya Dryomov4965fc32014-10-23 16:32:57 +04003469 kvfree(m->front.iov_base);
Alex Eldere3d5d632013-05-01 12:43:04 -05003470 kmem_cache_free(ceph_msg_cache, m);
Sage Weil31b80062009-10-06 11:31:13 -07003471}
3472
Ilya Dryomov0215e442014-06-20 14:14:41 +04003473static void ceph_msg_release(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07003474{
Sage Weilc2e552e2009-12-07 15:55:05 -08003475 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Geliang Tang10bcee12015-12-18 23:33:30 +08003476 struct ceph_msg_data *data, *next;
Sage Weil31b80062009-10-06 11:31:13 -07003477
Ilya Dryomov0215e442014-06-20 14:14:41 +04003478 dout("%s %p\n", __func__, m);
Sage Weilc2e552e2009-12-07 15:55:05 -08003479 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07003480
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003481 msg_con_set(m, NULL);
3482
Sage Weilc2e552e2009-12-07 15:55:05 -08003483 /* drop middle, data, if any */
3484 if (m->middle) {
3485 ceph_buffer_put(m->middle);
3486 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07003487 }
Alex Elder5240d9f2013-03-14 14:09:06 -05003488
Geliang Tang10bcee12015-12-18 23:33:30 +08003489 list_for_each_entry_safe(data, next, &m->data, links) {
3490 list_del_init(&data->links);
Alex Elder5240d9f2013-03-14 14:09:06 -05003491 ceph_msg_data_destroy(data);
3492 }
Alex Eldera1930802013-03-14 14:09:06 -05003493 m->data_length = 0;
Sage Weil58bb3b32009-12-23 12:12:31 -08003494
Sage Weilc2e552e2009-12-07 15:55:05 -08003495 if (m->pool)
3496 ceph_msgpool_put(m->pool, m);
3497 else
Ilya Dryomov0215e442014-06-20 14:14:41 +04003498 ceph_msg_free(m);
Sage Weil31b80062009-10-06 11:31:13 -07003499}
Ilya Dryomov0215e442014-06-20 14:14:41 +04003500
3501struct ceph_msg *ceph_msg_get(struct ceph_msg *msg)
3502{
3503 dout("%s %p (was %d)\n", __func__, msg,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003504 kref_read(&msg->kref));
Ilya Dryomov0215e442014-06-20 14:14:41 +04003505 kref_get(&msg->kref);
3506 return msg;
3507}
3508EXPORT_SYMBOL(ceph_msg_get);
3509
3510void ceph_msg_put(struct ceph_msg *msg)
3511{
3512 dout("%s %p (was %d)\n", __func__, msg,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003513 kref_read(&msg->kref));
Ilya Dryomov0215e442014-06-20 14:14:41 +04003514 kref_put(&msg->kref, ceph_msg_release);
3515}
3516EXPORT_SYMBOL(ceph_msg_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003517
3518void ceph_msg_dump(struct ceph_msg *msg)
3519{
Ilya Dryomov3cea4c32014-01-09 20:08:21 +02003520 pr_debug("msg_dump %p (front_alloc_len %d length %zd)\n", msg,
3521 msg->front_alloc_len, msg->data_length);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003522 print_hex_dump(KERN_DEBUG, "header: ",
3523 DUMP_PREFIX_OFFSET, 16, 1,
3524 &msg->hdr, sizeof(msg->hdr), true);
3525 print_hex_dump(KERN_DEBUG, " front: ",
3526 DUMP_PREFIX_OFFSET, 16, 1,
3527 msg->front.iov_base, msg->front.iov_len, true);
3528 if (msg->middle)
3529 print_hex_dump(KERN_DEBUG, "middle: ",
3530 DUMP_PREFIX_OFFSET, 16, 1,
3531 msg->middle->vec.iov_base,
3532 msg->middle->vec.iov_len, true);
3533 print_hex_dump(KERN_DEBUG, "footer: ",
3534 DUMP_PREFIX_OFFSET, 16, 1,
3535 &msg->footer, sizeof(msg->footer), true);
3536}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003537EXPORT_SYMBOL(ceph_msg_dump);