blob: 2f126eff275d58417d2397b15e7fcef2351cdde5 [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;
159
Sage Weil31b80062009-10-06 11:31:13 -0700160/* static tag bytes (protocol control messages) */
161static char tag_msg = CEPH_MSGR_TAG_MSG;
162static char tag_ack = CEPH_MSGR_TAG_ACK;
163static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE;
Yan, Zheng8b9558a2015-09-01 17:19:38 +0800164static char tag_keepalive2 = CEPH_MSGR_TAG_KEEPALIVE2;
Sage Weil31b80062009-10-06 11:31:13 -0700165
Sage Weila6a53492010-04-13 14:07:07 -0700166#ifdef CONFIG_LOCKDEP
167static struct lock_class_key socket_class;
168#endif
169
Sage Weil31b80062009-10-06 11:31:13 -0700170static void queue_con(struct ceph_connection *con);
Ilya Dryomov37ab77a2014-06-24 16:21:45 +0400171static void cancel_con(struct ceph_connection *con);
Ilya Dryomov68931622015-07-03 15:44:41 +0300172static void ceph_con_workfn(struct work_struct *);
Alex Elder93209262013-02-19 12:25:57 -0600173static void con_fault(struct ceph_connection *con);
Sage Weil31b80062009-10-06 11:31:13 -0700174
Sage Weil31b80062009-10-06 11:31:13 -0700175/*
Alex Elderf64a9312012-01-23 15:49:27 -0600176 * Nicely render a sockaddr as a string. An array of formatted
177 * strings is used, to approximate reentrancy.
Sage Weil31b80062009-10-06 11:31:13 -0700178 */
Alex Elderf64a9312012-01-23 15:49:27 -0600179#define ADDR_STR_COUNT_LOG 5 /* log2(# address strings in array) */
180#define ADDR_STR_COUNT (1 << ADDR_STR_COUNT_LOG)
181#define ADDR_STR_COUNT_MASK (ADDR_STR_COUNT - 1)
182#define MAX_ADDR_STR_LEN 64 /* 54 is enough */
183
184static char addr_str[ADDR_STR_COUNT][MAX_ADDR_STR_LEN];
185static atomic_t addr_str_seq = ATOMIC_INIT(0);
Sage Weil31b80062009-10-06 11:31:13 -0700186
Alex Elder57666512012-01-23 15:49:27 -0600187static struct page *zero_page; /* used in certain error cases */
Alex Elder57666512012-01-23 15:49:27 -0600188
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700189const char *ceph_pr_addr(const struct sockaddr_storage *ss)
Sage Weil31b80062009-10-06 11:31:13 -0700190{
191 int i;
192 char *s;
Alex Elder99f0f3b2012-01-23 15:49:27 -0600193 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
194 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Sage Weil31b80062009-10-06 11:31:13 -0700195
Alex Elderf64a9312012-01-23 15:49:27 -0600196 i = atomic_inc_return(&addr_str_seq) & ADDR_STR_COUNT_MASK;
Sage Weil31b80062009-10-06 11:31:13 -0700197 s = addr_str[i];
198
199 switch (ss->ss_family) {
200 case AF_INET:
Alex Elderbd406142012-01-23 15:49:27 -0600201 snprintf(s, MAX_ADDR_STR_LEN, "%pI4:%hu", &in4->sin_addr,
202 ntohs(in4->sin_port));
Sage Weil31b80062009-10-06 11:31:13 -0700203 break;
204
205 case AF_INET6:
Alex Elderbd406142012-01-23 15:49:27 -0600206 snprintf(s, MAX_ADDR_STR_LEN, "[%pI6c]:%hu", &in6->sin6_addr,
207 ntohs(in6->sin6_port));
Sage Weil31b80062009-10-06 11:31:13 -0700208 break;
209
210 default:
Alex Elderd3002b92012-02-14 14:05:33 -0600211 snprintf(s, MAX_ADDR_STR_LEN, "(unknown sockaddr family %hu)",
212 ss->ss_family);
Sage Weil31b80062009-10-06 11:31:13 -0700213 }
214
215 return s;
216}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700217EXPORT_SYMBOL(ceph_pr_addr);
Sage Weil31b80062009-10-06 11:31:13 -0700218
Sage Weil63f2d212009-11-03 15:17:56 -0800219static void encode_my_addr(struct ceph_messenger *msgr)
220{
221 memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr));
222 ceph_encode_addr(&msgr->my_enc_addr);
223}
224
Sage Weil31b80062009-10-06 11:31:13 -0700225/*
226 * work queue for all reading and writing to/from the socket.
227 */
Alex Eldere0f43c92012-02-14 14:05:33 -0600228static struct workqueue_struct *ceph_msgr_wq;
Sage Weil31b80062009-10-06 11:31:13 -0700229
Alex Eldere3d5d632013-05-01 12:43:04 -0500230static int ceph_msgr_slab_init(void)
231{
232 BUG_ON(ceph_msg_cache);
Geliang Tang5ee61e92016-03-13 15:18:39 +0800233 ceph_msg_cache = KMEM_CACHE(ceph_msg, 0);
Alex Elder81b36be2013-05-01 12:43:04 -0500234 if (!ceph_msg_cache)
235 return -ENOMEM;
236
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +0200237 return 0;
Alex Eldere3d5d632013-05-01 12:43:04 -0500238}
239
240static void ceph_msgr_slab_exit(void)
241{
242 BUG_ON(!ceph_msg_cache);
243 kmem_cache_destroy(ceph_msg_cache);
244 ceph_msg_cache = NULL;
245}
246
Alex Elder15417162013-02-19 12:25:56 -0600247static void _ceph_msgr_exit(void)
Alex Elder6173d1f2012-02-14 14:05:33 -0600248{
Alex Elderd3002b92012-02-14 14:05:33 -0600249 if (ceph_msgr_wq) {
Alex Elder6173d1f2012-02-14 14:05:33 -0600250 destroy_workqueue(ceph_msgr_wq);
Alex Elderd3002b92012-02-14 14:05:33 -0600251 ceph_msgr_wq = NULL;
252 }
Alex Elder6173d1f2012-02-14 14:05:33 -0600253
Alex Elder6173d1f2012-02-14 14:05:33 -0600254 BUG_ON(zero_page == NULL);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300255 put_page(zero_page);
Alex Elder6173d1f2012-02-14 14:05:33 -0600256 zero_page = NULL;
Benoît Canetd920ff62015-06-25 21:02:57 +0200257
258 ceph_msgr_slab_exit();
Alex Elder6173d1f2012-02-14 14:05:33 -0600259}
260
Chengguang Xu57a35df2018-03-10 20:32:05 +0800261int __init ceph_msgr_init(void)
Sage Weil31b80062009-10-06 11:31:13 -0700262{
Benoît Canetd920ff62015-06-25 21:02:57 +0200263 if (ceph_msgr_slab_init())
264 return -ENOMEM;
265
Alex Elder57666512012-01-23 15:49:27 -0600266 BUG_ON(zero_page != NULL);
267 zero_page = ZERO_PAGE(0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300268 get_page(zero_page);
Alex Elder57666512012-01-23 15:49:27 -0600269
Ilya Dryomovf9865f02014-10-10 16:39:05 +0400270 /*
271 * The number of active work items is limited by the number of
272 * connections, so leave @max_active at default.
273 */
274 ceph_msgr_wq = alloc_workqueue("ceph-msgr", WQ_MEM_RECLAIM, 0);
Alex Elder6173d1f2012-02-14 14:05:33 -0600275 if (ceph_msgr_wq)
276 return 0;
Alex Elder57666512012-01-23 15:49:27 -0600277
Alex Elder6173d1f2012-02-14 14:05:33 -0600278 pr_err("msgr_init failed to create workqueue\n");
279 _ceph_msgr_exit();
Alex Elder57666512012-01-23 15:49:27 -0600280
Alex Elder6173d1f2012-02-14 14:05:33 -0600281 return -ENOMEM;
Sage Weil31b80062009-10-06 11:31:13 -0700282}
283
284void ceph_msgr_exit(void)
285{
Alex Elder57666512012-01-23 15:49:27 -0600286 BUG_ON(ceph_msgr_wq == NULL);
Alex Elder57666512012-01-23 15:49:27 -0600287
Alex Elder6173d1f2012-02-14 14:05:33 -0600288 _ceph_msgr_exit();
Sage Weil31b80062009-10-06 11:31:13 -0700289}
290
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700291void ceph_msgr_flush(void)
Sage Weila922d382010-05-29 09:41:23 -0700292{
293 flush_workqueue(ceph_msgr_wq);
294}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700295EXPORT_SYMBOL(ceph_msgr_flush);
Sage Weila922d382010-05-29 09:41:23 -0700296
Alex Elderce2c8902012-05-22 22:15:49 -0500297/* Connection socket state transition functions */
298
299static void con_sock_state_init(struct ceph_connection *con)
300{
301 int old_state;
302
303 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
304 if (WARN_ON(old_state != CON_SOCK_STATE_NEW))
305 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700306 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
307 CON_SOCK_STATE_CLOSED);
Alex Elderce2c8902012-05-22 22:15:49 -0500308}
309
310static void con_sock_state_connecting(struct ceph_connection *con)
311{
312 int old_state;
313
314 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTING);
315 if (WARN_ON(old_state != CON_SOCK_STATE_CLOSED))
316 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700317 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
318 CON_SOCK_STATE_CONNECTING);
Alex Elderce2c8902012-05-22 22:15:49 -0500319}
320
321static void con_sock_state_connected(struct ceph_connection *con)
322{
323 int old_state;
324
325 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CONNECTED);
326 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING))
327 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700328 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
329 CON_SOCK_STATE_CONNECTED);
Alex Elderce2c8902012-05-22 22:15:49 -0500330}
331
332static void con_sock_state_closing(struct ceph_connection *con)
333{
334 int old_state;
335
336 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSING);
337 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTING &&
338 old_state != CON_SOCK_STATE_CONNECTED &&
339 old_state != CON_SOCK_STATE_CLOSING))
340 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700341 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
342 CON_SOCK_STATE_CLOSING);
Alex Elderce2c8902012-05-22 22:15:49 -0500343}
344
345static void con_sock_state_closed(struct ceph_connection *con)
346{
347 int old_state;
348
349 old_state = atomic_xchg(&con->sock_state, CON_SOCK_STATE_CLOSED);
350 if (WARN_ON(old_state != CON_SOCK_STATE_CONNECTED &&
Sage Weilfbb85a42012-06-27 12:31:02 -0700351 old_state != CON_SOCK_STATE_CLOSING &&
Sage Weil8007b8d2012-07-30 18:16:16 -0700352 old_state != CON_SOCK_STATE_CONNECTING &&
353 old_state != CON_SOCK_STATE_CLOSED))
Alex Elderce2c8902012-05-22 22:15:49 -0500354 printk("%s: unexpected old state %d\n", __func__, old_state);
Sage Weil8007b8d2012-07-30 18:16:16 -0700355 dout("%s con %p sock %d -> %d\n", __func__, con, old_state,
356 CON_SOCK_STATE_CLOSED);
Alex Elderce2c8902012-05-22 22:15:49 -0500357}
Sage Weila922d382010-05-29 09:41:23 -0700358
Sage Weil31b80062009-10-06 11:31:13 -0700359/*
360 * socket callback functions
361 */
362
363/* data available on socket, or listen socket received a connect */
David S. Miller676d2362014-04-11 16:15:36 -0400364static void ceph_sock_data_ready(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700365{
Alex Elderbd406142012-01-23 15:49:27 -0600366 struct ceph_connection *con = sk->sk_user_data;
Guanjun Hea2a32582012-07-08 19:50:33 -0700367 if (atomic_read(&con->msgr->stopping)) {
368 return;
369 }
Alex Elderbd406142012-01-23 15:49:27 -0600370
Sage Weil31b80062009-10-06 11:31:13 -0700371 if (sk->sk_state != TCP_CLOSE_WAIT) {
Alex Elder327800b2012-05-22 11:41:43 -0500372 dout("%s on %p state = %lu, queueing work\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700373 con, con->state);
374 queue_con(con);
375 }
376}
377
378/* socket has buffer space for writing */
Alex Elder327800b2012-05-22 11:41:43 -0500379static void ceph_sock_write_space(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700380{
Alex Elderd3002b92012-02-14 14:05:33 -0600381 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700382
Jim Schutt182fac22012-02-29 08:30:58 -0700383 /* only queue to workqueue if there is data we want to write,
384 * and there is sufficient space in the socket buffer to accept
Alex Elder327800b2012-05-22 11:41:43 -0500385 * more data. clear SOCK_NOSPACE so that ceph_sock_write_space()
Jim Schutt182fac22012-02-29 08:30:58 -0700386 * doesn't get called again until try_write() fills the socket
387 * buffer. See net/ipv4/tcp_input.c:tcp_check_space()
388 * and net/core/stream.c:sk_stream_write_space().
389 */
Alex Elderc9ffc772013-02-20 10:25:12 -0600390 if (con_flag_test(con, CON_FLAG_WRITE_PENDING)) {
Eric Dumazet64dc6132013-07-22 20:26:31 -0700391 if (sk_stream_is_writeable(sk)) {
Alex Elder327800b2012-05-22 11:41:43 -0500392 dout("%s %p queueing write work\n", __func__, con);
Jim Schutt182fac22012-02-29 08:30:58 -0700393 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
394 queue_con(con);
395 }
Sage Weil31b80062009-10-06 11:31:13 -0700396 } else {
Alex Elder327800b2012-05-22 11:41:43 -0500397 dout("%s %p nothing to write\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -0700398 }
Sage Weil31b80062009-10-06 11:31:13 -0700399}
400
401/* socket's state has changed */
Alex Elder327800b2012-05-22 11:41:43 -0500402static void ceph_sock_state_change(struct sock *sk)
Sage Weil31b80062009-10-06 11:31:13 -0700403{
Alex Elderbd406142012-01-23 15:49:27 -0600404 struct ceph_connection *con = sk->sk_user_data;
Sage Weil31b80062009-10-06 11:31:13 -0700405
Alex Elder327800b2012-05-22 11:41:43 -0500406 dout("%s %p state = %lu sk_state = %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -0700407 con, con->state, sk->sk_state);
408
Sage Weil31b80062009-10-06 11:31:13 -0700409 switch (sk->sk_state) {
410 case TCP_CLOSE:
Alex Elder327800b2012-05-22 11:41:43 -0500411 dout("%s TCP_CLOSE\n", __func__);
Gustavo A. R. Silva18370b32017-10-15 12:55:23 -0500412 /* fall through */
Sage Weil31b80062009-10-06 11:31:13 -0700413 case TCP_CLOSE_WAIT:
Alex Elder327800b2012-05-22 11:41:43 -0500414 dout("%s TCP_CLOSE_WAIT\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500415 con_sock_state_closing(con);
Alex Elderc9ffc772013-02-20 10:25:12 -0600416 con_flag_set(con, CON_FLAG_SOCK_CLOSED);
Alex Elderd65c9e02012-06-20 21:53:53 -0500417 queue_con(con);
Sage Weil31b80062009-10-06 11:31:13 -0700418 break;
419 case TCP_ESTABLISHED:
Alex Elder327800b2012-05-22 11:41:43 -0500420 dout("%s TCP_ESTABLISHED\n", __func__);
Alex Elderce2c8902012-05-22 22:15:49 -0500421 con_sock_state_connected(con);
Sage Weil31b80062009-10-06 11:31:13 -0700422 queue_con(con);
423 break;
Alex Elderd3002b92012-02-14 14:05:33 -0600424 default: /* Everything else is uninteresting */
425 break;
Sage Weil31b80062009-10-06 11:31:13 -0700426 }
427}
428
429/*
430 * set up socket callbacks
431 */
432static void set_sock_callbacks(struct socket *sock,
433 struct ceph_connection *con)
434{
435 struct sock *sk = sock->sk;
Alex Elderbd406142012-01-23 15:49:27 -0600436 sk->sk_user_data = con;
Alex Elder327800b2012-05-22 11:41:43 -0500437 sk->sk_data_ready = ceph_sock_data_ready;
438 sk->sk_write_space = ceph_sock_write_space;
439 sk->sk_state_change = ceph_sock_state_change;
Sage Weil31b80062009-10-06 11:31:13 -0700440}
441
442
443/*
444 * socket helpers
445 */
446
447/*
448 * initiate connection to a remote socket.
449 */
Alex Elder41617d02012-02-14 14:05:33 -0600450static int ceph_tcp_connect(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -0700451{
Sage Weilf91d3472010-07-01 15:18:31 -0700452 struct sockaddr_storage *paddr = &con->peer_addr.in_addr;
Sage Weil31b80062009-10-06 11:31:13 -0700453 struct socket *sock;
Ilya Dryomov633ee4072017-03-21 13:44:28 +0100454 unsigned int noio_flag;
Sage Weil31b80062009-10-06 11:31:13 -0700455 int ret;
456
457 BUG_ON(con->sock);
Ilya Dryomov633ee4072017-03-21 13:44:28 +0100458
459 /* sock_create_kern() allocates with GFP_KERNEL */
460 noio_flag = memalloc_noio_save();
Ilya Dryomov757856d2015-06-25 17:47:45 +0300461 ret = sock_create_kern(read_pnet(&con->msgr->net), paddr->ss_family,
Eric W. Biedermaneeb1bd52015-05-08 21:08:05 -0500462 SOCK_STREAM, IPPROTO_TCP, &sock);
Ilya Dryomov633ee4072017-03-21 13:44:28 +0100463 memalloc_noio_restore(noio_flag);
Sage Weil31b80062009-10-06 11:31:13 -0700464 if (ret)
Alex Elder41617d02012-02-14 14:05:33 -0600465 return ret;
Ilya Dryomov6d7fdb02015-04-02 14:40:58 +0300466 sock->sk->sk_allocation = GFP_NOFS;
Sage Weil31b80062009-10-06 11:31:13 -0700467
Sage Weila6a53492010-04-13 14:07:07 -0700468#ifdef CONFIG_LOCKDEP
469 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
470#endif
471
Sage Weil31b80062009-10-06 11:31:13 -0700472 set_sock_callbacks(sock, con);
473
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700474 dout("connect %s\n", ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -0700475
Sage Weil89a86be2012-06-09 14:19:21 -0700476 con_sock_state_connecting(con);
Sage Weilf91d3472010-07-01 15:18:31 -0700477 ret = sock->ops->connect(sock, (struct sockaddr *)paddr, sizeof(*paddr),
478 O_NONBLOCK);
Sage Weil31b80062009-10-06 11:31:13 -0700479 if (ret == -EINPROGRESS) {
480 dout("connect %s EINPROGRESS sk_state = %u\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700481 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -0700482 sock->sk->sk_state);
Alex Eldera5bc3122012-01-23 15:49:27 -0600483 } else if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -0700484 pr_err("connect %s error %d\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700485 ceph_pr_addr(&con->peer_addr.in_addr), ret);
Sage Weil31b80062009-10-06 11:31:13 -0700486 sock_release(sock);
Alex Elder41617d02012-02-14 14:05:33 -0600487 return ret;
Alex Eldera5bc3122012-01-23 15:49:27 -0600488 }
Mike Christie89baaa52014-10-16 01:50:19 -0500489
Ilya Dryomov859bff52015-10-28 23:50:58 +0100490 if (ceph_test_opt(from_msgr(con->msgr), TCP_NODELAY)) {
Chaitanya Huilgolba988f82015-01-23 16:41:25 +0530491 int optval = 1;
492
493 ret = kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY,
494 (char *)&optval, sizeof(optval));
495 if (ret)
496 pr_err("kernel_setsockopt(TCP_NODELAY) failed: %d",
497 ret);
498 }
499
Alex Eldera5bc3122012-01-23 15:49:27 -0600500 con->sock = sock;
Alex Elder41617d02012-02-14 14:05:33 -0600501 return 0;
Sage Weil31b80062009-10-06 11:31:13 -0700502}
503
Ilya Dryomove5c93882018-04-27 18:58:47 +0200504/*
505 * If @buf is NULL, discard up to @len bytes.
506 */
Sage Weil31b80062009-10-06 11:31:13 -0700507static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len)
508{
509 struct kvec iov = {buf, len};
510 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil98bdb0a2011-01-25 08:17:48 -0800511 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700512
Ilya Dryomove5c93882018-04-27 18:58:47 +0200513 if (!buf)
514 msg.msg_flags |= MSG_TRUNC;
515
David Howellsaa563d72018-10-20 00:57:56 +0100516 iov_iter_kvec(&msg.msg_iter, READ, &iov, 1, len);
Al Viro100803a2015-11-15 00:12:48 -0500517 r = sock_recvmsg(sock, &msg, msg.msg_flags);
Sage Weil98bdb0a2011-01-25 08:17:48 -0800518 if (r == -EAGAIN)
519 r = 0;
520 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700521}
522
Alex Elderafb3d902013-03-08 20:58:59 -0600523static int ceph_tcp_recvpage(struct socket *sock, struct page *page,
524 int page_offset, size_t length)
525{
Al Viro100803a2015-11-15 00:12:48 -0500526 struct bio_vec bvec = {
527 .bv_page = page,
528 .bv_offset = page_offset,
529 .bv_len = length
530 };
531 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
532 int r;
Alex Elderafb3d902013-03-08 20:58:59 -0600533
534 BUG_ON(page_offset + length > PAGE_SIZE);
David Howellsaa563d72018-10-20 00:57:56 +0100535 iov_iter_bvec(&msg.msg_iter, READ, &bvec, 1, length);
Al Viro100803a2015-11-15 00:12:48 -0500536 r = sock_recvmsg(sock, &msg, msg.msg_flags);
537 if (r == -EAGAIN)
538 r = 0;
539 return r;
Alex Elderafb3d902013-03-08 20:58:59 -0600540}
541
Sage Weil31b80062009-10-06 11:31:13 -0700542/*
543 * write something. @more is true if caller will be sending more data
544 * shortly.
545 */
546static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov,
547 size_t kvlen, size_t len, int more)
548{
549 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
Sage Weil42961d22011-01-25 08:19:34 -0800550 int r;
Sage Weil31b80062009-10-06 11:31:13 -0700551
552 if (more)
553 msg.msg_flags |= MSG_MORE;
554 else
555 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
556
Sage Weil42961d22011-01-25 08:19:34 -0800557 r = kernel_sendmsg(sock, &msg, iov, kvlen, len);
558 if (r == -EAGAIN)
559 r = 0;
560 return r;
Sage Weil31b80062009-10-06 11:31:13 -0700561}
562
Chunwei Chen178eda292014-04-23 12:35:09 +0800563static int __ceph_tcp_sendpage(struct socket *sock, struct page *page,
Alex Eldere1dcb122013-03-06 23:39:38 -0600564 int offset, size_t size, bool more)
Alex Elder31739132012-03-07 11:40:08 -0600565{
566 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | (more ? MSG_MORE : MSG_EOR);
567 int ret;
568
569 ret = kernel_sendpage(sock, page, offset, size, flags);
570 if (ret == -EAGAIN)
571 ret = 0;
572
573 return ret;
574}
575
Chunwei Chen178eda292014-04-23 12:35:09 +0800576static int ceph_tcp_sendpage(struct socket *sock, struct page *page,
577 int offset, size_t size, bool more)
578{
Al Viro61ff6e92016-01-09 20:55:45 -0500579 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL };
580 struct bio_vec bvec;
Chunwei Chen178eda292014-04-23 12:35:09 +0800581 int ret;
Chunwei Chen178eda292014-04-23 12:35:09 +0800582
Ilya Dryomov7e241f62018-11-08 15:55:37 +0100583 /*
584 * sendpage cannot properly handle pages with page_count == 0,
585 * we need to fall back to sendmsg if that's the case.
586 *
587 * Same goes for slab pages: skb_can_coalesce() allows
588 * coalescing neighboring slab objects into a single frag which
589 * triggers one of hardened usercopy checks.
590 */
591 if (page_count(page) >= 1 && !PageSlab(page))
Chunwei Chen178eda292014-04-23 12:35:09 +0800592 return __ceph_tcp_sendpage(sock, page, offset, size, more);
593
Al Viro61ff6e92016-01-09 20:55:45 -0500594 bvec.bv_page = page;
595 bvec.bv_offset = offset;
596 bvec.bv_len = size;
597
598 if (more)
599 msg.msg_flags |= MSG_MORE;
600 else
601 msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */
602
David Howellsaa563d72018-10-20 00:57:56 +0100603 iov_iter_bvec(&msg.msg_iter, WRITE, &bvec, 1, size);
Al Viro61ff6e92016-01-09 20:55:45 -0500604 ret = sock_sendmsg(sock, &msg);
605 if (ret == -EAGAIN)
606 ret = 0;
Chunwei Chen178eda292014-04-23 12:35:09 +0800607
608 return ret;
609}
Sage Weil31b80062009-10-06 11:31:13 -0700610
611/*
612 * Shutdown/close the socket for the given connection.
613 */
614static int con_close_socket(struct ceph_connection *con)
615{
Sage Weil8007b8d2012-07-30 18:16:16 -0700616 int rc = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700617
618 dout("con_close_socket on %p sock %p\n", con, con->sock);
Sage Weil8007b8d2012-07-30 18:16:16 -0700619 if (con->sock) {
620 rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR);
621 sock_release(con->sock);
622 con->sock = NULL;
623 }
Alex Elder456ea462012-06-20 21:53:53 -0500624
625 /*
Sage Weil4a861692012-07-20 17:29:55 -0700626 * Forcibly clear the SOCK_CLOSED flag. It gets set
Alex Elder456ea462012-06-20 21:53:53 -0500627 * independent of the connection mutex, and we could have
628 * received a socket close event before we had the chance to
629 * shut the socket down.
630 */
Alex Elderc9ffc772013-02-20 10:25:12 -0600631 con_flag_clear(con, CON_FLAG_SOCK_CLOSED);
Sage Weil8007b8d2012-07-30 18:16:16 -0700632
Alex Elderce2c8902012-05-22 22:15:49 -0500633 con_sock_state_closed(con);
Sage Weil31b80062009-10-06 11:31:13 -0700634 return rc;
635}
636
637/*
638 * Reset a connection. Discard all incoming and outgoing messages
639 * and clear *_seq state.
640 */
641static void ceph_msg_remove(struct ceph_msg *msg)
642{
643 list_del_init(&msg->list_head);
Alex Elder38941f82012-06-01 14:56:43 -0500644
Sage Weil31b80062009-10-06 11:31:13 -0700645 ceph_msg_put(msg);
646}
647static void ceph_msg_remove_list(struct list_head *head)
648{
649 while (!list_empty(head)) {
650 struct ceph_msg *msg = list_first_entry(head, struct ceph_msg,
651 list_head);
652 ceph_msg_remove(msg);
653 }
654}
655
656static void reset_connection(struct ceph_connection *con)
657{
658 /* reset connection, out_queue, msg_ and connect_seq */
659 /* discard existing out_queue and msg_seq */
Sage Weil0fa6ebc2012-12-27 20:27:04 -0600660 dout("reset_connection %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -0700661 ceph_msg_remove_list(&con->out_queue);
662 ceph_msg_remove_list(&con->out_sent);
663
Sage Weilcf3e5c42009-12-11 09:48:05 -0800664 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -0500665 BUG_ON(con->in_msg->con != con);
Sage Weilcf3e5c42009-12-11 09:48:05 -0800666 ceph_msg_put(con->in_msg);
667 con->in_msg = NULL;
668 }
669
Sage Weil31b80062009-10-06 11:31:13 -0700670 con->connect_seq = 0;
671 con->out_seq = 0;
Sage Weilc86a2932009-12-14 14:04:30 -0800672 if (con->out_msg) {
Ilya Dryomov583d0fe2015-11-02 17:13:58 +0100673 BUG_ON(con->out_msg->con != con);
Sage Weilc86a2932009-12-14 14:04:30 -0800674 ceph_msg_put(con->out_msg);
675 con->out_msg = NULL;
676 }
Sage Weil31b80062009-10-06 11:31:13 -0700677 con->in_seq = 0;
Sage Weil0e0d5e02010-04-02 16:07:19 -0700678 con->in_seq_acked = 0;
Ilya Dryomov67645d72015-12-28 13:18:34 +0300679
680 con->out_skip = 0;
Sage Weil31b80062009-10-06 11:31:13 -0700681}
682
683/*
684 * mark a peer down. drop any open connections.
685 */
686void ceph_con_close(struct ceph_connection *con)
687{
Sage Weil8c50c812012-07-30 16:24:37 -0700688 mutex_lock(&con->mutex);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700689 dout("con_close %p peer %s\n", con,
690 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil8dacc7d2012-07-20 17:24:40 -0700691 con->state = CON_STATE_CLOSED;
Alex Eldera5988c42012-05-29 11:04:58 -0500692
Alex Elderc9ffc772013-02-20 10:25:12 -0600693 con_flag_clear(con, CON_FLAG_LOSSYTX); /* so we retry next connect */
694 con_flag_clear(con, CON_FLAG_KEEPALIVE_PENDING);
695 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
696 con_flag_clear(con, CON_FLAG_BACKOFF);
Alex Eldera5988c42012-05-29 11:04:58 -0500697
Sage Weil31b80062009-10-06 11:31:13 -0700698 reset_connection(con);
Sage Weil6f2bc3f2010-04-02 16:16:34 -0700699 con->peer_global_seq = 0;
Ilya Dryomov37ab77a2014-06-24 16:21:45 +0400700 cancel_con(con);
Sage Weilee76e072012-07-20 16:45:49 -0700701 con_close_socket(con);
Sage Weilec302642009-12-22 10:43:42 -0800702 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700703}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700704EXPORT_SYMBOL(ceph_con_close);
Sage Weil31b80062009-10-06 11:31:13 -0700705
706/*
Sage Weil31b80062009-10-06 11:31:13 -0700707 * Reopen a closed connection, with a new peer address.
708 */
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700709void ceph_con_open(struct ceph_connection *con,
710 __u8 entity_type, __u64 entity_num,
711 struct ceph_entity_addr *addr)
Sage Weil31b80062009-10-06 11:31:13 -0700712{
Sage Weil54691552012-07-30 16:21:40 -0700713 mutex_lock(&con->mutex);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700714 dout("con_open %p %s\n", con, ceph_pr_addr(&addr->in_addr));
Sage Weil8dacc7d2012-07-20 17:24:40 -0700715
Alex Elder122070a2012-12-26 10:43:57 -0600716 WARN_ON(con->state != CON_STATE_CLOSED);
Sage Weil8dacc7d2012-07-20 17:24:40 -0700717 con->state = CON_STATE_PREOPEN;
Alex Eldera5988c42012-05-29 11:04:58 -0500718
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700719 con->peer_name.type = (__u8) entity_type;
720 con->peer_name.num = cpu_to_le64(entity_num);
721
Sage Weil31b80062009-10-06 11:31:13 -0700722 memcpy(&con->peer_addr, addr, sizeof(*addr));
Sage Weil03c677e2009-11-20 15:14:15 -0800723 con->delay = 0; /* reset backoff memory */
Sage Weil54691552012-07-30 16:21:40 -0700724 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700725 queue_con(con);
726}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700727EXPORT_SYMBOL(ceph_con_open);
Sage Weil31b80062009-10-06 11:31:13 -0700728
729/*
Sage Weil87b315a2010-03-22 14:51:18 -0700730 * return true if this connection ever successfully opened
731 */
732bool ceph_con_opened(struct ceph_connection *con)
733{
734 return con->connect_seq > 0;
735}
736
737/*
Sage Weil31b80062009-10-06 11:31:13 -0700738 * initialize a new connection.
739 */
Alex Elder1bfd89f2012-05-26 23:26:43 -0500740void ceph_con_init(struct ceph_connection *con, void *private,
741 const struct ceph_connection_operations *ops,
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700742 struct ceph_messenger *msgr)
Sage Weil31b80062009-10-06 11:31:13 -0700743{
744 dout("con_init %p\n", con);
745 memset(con, 0, sizeof(*con));
Alex Elder1bfd89f2012-05-26 23:26:43 -0500746 con->private = private;
747 con->ops = ops;
Sage Weil31b80062009-10-06 11:31:13 -0700748 con->msgr = msgr;
Alex Elderce2c8902012-05-22 22:15:49 -0500749
750 con_sock_state_init(con);
751
Sage Weilec302642009-12-22 10:43:42 -0800752 mutex_init(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -0700753 INIT_LIST_HEAD(&con->out_queue);
754 INIT_LIST_HEAD(&con->out_sent);
Ilya Dryomov68931622015-07-03 15:44:41 +0300755 INIT_DELAYED_WORK(&con->work, ceph_con_workfn);
Alex Eldera5988c42012-05-29 11:04:58 -0500756
Sage Weil8dacc7d2012-07-20 17:24:40 -0700757 con->state = CON_STATE_CLOSED;
Sage Weil31b80062009-10-06 11:31:13 -0700758}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700759EXPORT_SYMBOL(ceph_con_init);
Sage Weil31b80062009-10-06 11:31:13 -0700760
761
762/*
763 * We maintain a global counter to order connection attempts. Get
764 * a unique seq greater than @gt.
765 */
766static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt)
767{
768 u32 ret;
769
770 spin_lock(&msgr->global_seq_lock);
771 if (msgr->global_seq < gt)
772 msgr->global_seq = gt;
773 ret = ++msgr->global_seq;
774 spin_unlock(&msgr->global_seq_lock);
775 return ret;
776}
777
Alex Eldere2200422012-05-23 14:35:23 -0500778static void con_out_kvec_reset(struct ceph_connection *con)
Alex Elder859eb792012-02-14 14:05:33 -0600779{
Ilya Dryomov67645d72015-12-28 13:18:34 +0300780 BUG_ON(con->out_skip);
781
Alex Elder859eb792012-02-14 14:05:33 -0600782 con->out_kvec_left = 0;
783 con->out_kvec_bytes = 0;
784 con->out_kvec_cur = &con->out_kvec[0];
785}
786
Alex Eldere2200422012-05-23 14:35:23 -0500787static void con_out_kvec_add(struct ceph_connection *con,
Alex Elder859eb792012-02-14 14:05:33 -0600788 size_t size, void *data)
789{
Ilya Dryomov67645d72015-12-28 13:18:34 +0300790 int index = con->out_kvec_left;
Alex Elder859eb792012-02-14 14:05:33 -0600791
Ilya Dryomov67645d72015-12-28 13:18:34 +0300792 BUG_ON(con->out_skip);
Alex Elder859eb792012-02-14 14:05:33 -0600793 BUG_ON(index >= ARRAY_SIZE(con->out_kvec));
794
795 con->out_kvec[index].iov_len = size;
796 con->out_kvec[index].iov_base = data;
797 con->out_kvec_left++;
798 con->out_kvec_bytes += size;
799}
Sage Weil31b80062009-10-06 11:31:13 -0700800
Ilya Dryomov67645d72015-12-28 13:18:34 +0300801/*
802 * Chop off a kvec from the end. Return residual number of bytes for
803 * that kvec, i.e. how many bytes would have been written if the kvec
804 * hadn't been nuked.
805 */
806static int con_out_kvec_skip(struct ceph_connection *con)
807{
808 int off = con->out_kvec_cur - con->out_kvec;
809 int skip = 0;
810
811 if (con->out_kvec_bytes > 0) {
812 skip = con->out_kvec[off + con->out_kvec_left - 1].iov_len;
813 BUG_ON(con->out_kvec_bytes < skip);
814 BUG_ON(!con->out_kvec_left);
815 con->out_kvec_bytes -= skip;
816 con->out_kvec_left--;
817 }
818
819 return skip;
820}
821
Alex Elderdf6ad1f2012-06-11 14:57:13 -0500822#ifdef CONFIG_BLOCK
Alex Elder6aaa4512013-03-06 23:39:39 -0600823
824/*
825 * For a bio data item, a piece is whatever remains of the next
826 * entry in the current bio iovec, or the first entry in the next
827 * bio in the list.
828 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500829static void ceph_msg_data_bio_cursor_init(struct ceph_msg_data_cursor *cursor,
Alex Elder25aff7c2013-03-11 23:34:22 -0500830 size_t length)
Alex Elder6aaa4512013-03-06 23:39:39 -0600831{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500832 struct ceph_msg_data *data = cursor->data;
Ilya Dryomov5359a172018-01-20 10:30:10 +0100833 struct ceph_bio_iter *it = &cursor->bio_iter;
Alex Elder6aaa4512013-03-06 23:39:39 -0600834
Ilya Dryomov5359a172018-01-20 10:30:10 +0100835 cursor->resid = min_t(size_t, length, data->bio_length);
836 *it = data->bio_pos;
837 if (cursor->resid < it->iter.bi_size)
838 it->iter.bi_size = cursor->resid;
Alex Elder6aaa4512013-03-06 23:39:39 -0600839
Ilya Dryomov5359a172018-01-20 10:30:10 +0100840 BUG_ON(cursor->resid < bio_iter_len(it->bio, it->iter));
841 cursor->last_piece = cursor->resid == bio_iter_len(it->bio, it->iter);
Alex Elder6aaa4512013-03-06 23:39:39 -0600842}
843
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500844static struct page *ceph_msg_data_bio_next(struct ceph_msg_data_cursor *cursor,
Alex Elder6aaa4512013-03-06 23:39:39 -0600845 size_t *page_offset,
846 size_t *length)
847{
Ilya Dryomov5359a172018-01-20 10:30:10 +0100848 struct bio_vec bv = bio_iter_iovec(cursor->bio_iter.bio,
849 cursor->bio_iter.iter);
Alex Elder6aaa4512013-03-06 23:39:39 -0600850
Ilya Dryomov5359a172018-01-20 10:30:10 +0100851 *page_offset = bv.bv_offset;
852 *length = bv.bv_len;
853 return bv.bv_page;
Alex Elder6aaa4512013-03-06 23:39:39 -0600854}
855
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500856static bool ceph_msg_data_bio_advance(struct ceph_msg_data_cursor *cursor,
857 size_t bytes)
Alex Elder6aaa4512013-03-06 23:39:39 -0600858{
Ilya Dryomov5359a172018-01-20 10:30:10 +0100859 struct ceph_bio_iter *it = &cursor->bio_iter;
Alex Elder6aaa4512013-03-06 23:39:39 -0600860
Ilya Dryomov5359a172018-01-20 10:30:10 +0100861 BUG_ON(bytes > cursor->resid);
862 BUG_ON(bytes > bio_iter_len(it->bio, it->iter));
Alex Elder25aff7c2013-03-11 23:34:22 -0500863 cursor->resid -= bytes;
Ilya Dryomov5359a172018-01-20 10:30:10 +0100864 bio_advance_iter(it->bio, &it->iter, bytes);
Kent Overstreetf38a5182013-08-07 14:30:24 -0700865
Ilya Dryomov5359a172018-01-20 10:30:10 +0100866 if (!cursor->resid) {
867 BUG_ON(!cursor->last_piece);
868 return false; /* no more data */
869 }
Kent Overstreetf38a5182013-08-07 14:30:24 -0700870
Ilya Dryomov5359a172018-01-20 10:30:10 +0100871 if (!bytes || (it->iter.bi_size && it->iter.bi_bvec_done))
Alex Elder6aaa4512013-03-06 23:39:39 -0600872 return false; /* more bytes to process in this segment */
873
Ilya Dryomov5359a172018-01-20 10:30:10 +0100874 if (!it->iter.bi_size) {
875 it->bio = it->bio->bi_next;
876 it->iter = it->bio->bi_iter;
877 if (cursor->resid < it->iter.bi_size)
878 it->iter.bi_size = cursor->resid;
Alex Elder6aaa4512013-03-06 23:39:39 -0600879 }
Alex Elder6aaa4512013-03-06 23:39:39 -0600880
Ilya Dryomov5359a172018-01-20 10:30:10 +0100881 BUG_ON(cursor->last_piece);
882 BUG_ON(cursor->resid < bio_iter_len(it->bio, it->iter));
883 cursor->last_piece = cursor->resid == bio_iter_len(it->bio, it->iter);
Alex Elder6aaa4512013-03-06 23:39:39 -0600884 return true;
885}
Alex Elderea965712013-04-05 14:46:01 -0500886#endif /* CONFIG_BLOCK */
Alex Elderdf6ad1f2012-06-11 14:57:13 -0500887
Ilya Dryomovb9e281c2018-01-20 10:30:11 +0100888static void ceph_msg_data_bvecs_cursor_init(struct ceph_msg_data_cursor *cursor,
889 size_t length)
890{
891 struct ceph_msg_data *data = cursor->data;
892 struct bio_vec *bvecs = data->bvec_pos.bvecs;
893
894 cursor->resid = min_t(size_t, length, data->bvec_pos.iter.bi_size);
895 cursor->bvec_iter = data->bvec_pos.iter;
896 cursor->bvec_iter.bi_size = cursor->resid;
897
898 BUG_ON(cursor->resid < bvec_iter_len(bvecs, cursor->bvec_iter));
899 cursor->last_piece =
900 cursor->resid == bvec_iter_len(bvecs, cursor->bvec_iter);
901}
902
903static struct page *ceph_msg_data_bvecs_next(struct ceph_msg_data_cursor *cursor,
904 size_t *page_offset,
905 size_t *length)
906{
907 struct bio_vec bv = bvec_iter_bvec(cursor->data->bvec_pos.bvecs,
908 cursor->bvec_iter);
909
910 *page_offset = bv.bv_offset;
911 *length = bv.bv_len;
912 return bv.bv_page;
913}
914
915static bool ceph_msg_data_bvecs_advance(struct ceph_msg_data_cursor *cursor,
916 size_t bytes)
917{
918 struct bio_vec *bvecs = cursor->data->bvec_pos.bvecs;
919
920 BUG_ON(bytes > cursor->resid);
921 BUG_ON(bytes > bvec_iter_len(bvecs, cursor->bvec_iter));
922 cursor->resid -= bytes;
923 bvec_iter_advance(bvecs, &cursor->bvec_iter, bytes);
924
925 if (!cursor->resid) {
926 BUG_ON(!cursor->last_piece);
927 return false; /* no more data */
928 }
929
930 if (!bytes || cursor->bvec_iter.bi_bvec_done)
931 return false; /* more bytes to process in this segment */
932
933 BUG_ON(cursor->last_piece);
934 BUG_ON(cursor->resid < bvec_iter_len(bvecs, cursor->bvec_iter));
935 cursor->last_piece =
936 cursor->resid == bvec_iter_len(bvecs, cursor->bvec_iter);
937 return true;
938}
939
Alex Elderfe38a2b2013-03-06 23:39:39 -0600940/*
Alex Eldere766d7b2013-03-07 15:38:28 -0600941 * For a page array, a piece comes from the first page in the array
942 * that has not already been fully consumed.
943 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500944static void ceph_msg_data_pages_cursor_init(struct ceph_msg_data_cursor *cursor,
Alex Elder25aff7c2013-03-11 23:34:22 -0500945 size_t length)
Alex Eldere766d7b2013-03-07 15:38:28 -0600946{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500947 struct ceph_msg_data *data = cursor->data;
Alex Eldere766d7b2013-03-07 15:38:28 -0600948 int page_count;
949
950 BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
951
952 BUG_ON(!data->pages);
953 BUG_ON(!data->length);
954
Alex Elderca8b3a62013-04-05 14:46:01 -0500955 cursor->resid = min(length, data->length);
Alex Eldere766d7b2013-03-07 15:38:28 -0600956 page_count = calc_pages_for(data->alignment, (u64)data->length);
Alex Eldere766d7b2013-03-07 15:38:28 -0600957 cursor->page_offset = data->alignment & ~PAGE_MASK;
958 cursor->page_index = 0;
Alex Elder56fc5652013-03-30 23:46:55 -0500959 BUG_ON(page_count > (int)USHRT_MAX);
960 cursor->page_count = (unsigned short)page_count;
961 BUG_ON(length > SIZE_MAX - cursor->page_offset);
Ilya Dryomov5f740d72014-08-08 12:43:39 +0400962 cursor->last_piece = cursor->page_offset + cursor->resid <= PAGE_SIZE;
Alex Eldere766d7b2013-03-07 15:38:28 -0600963}
964
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500965static struct page *
966ceph_msg_data_pages_next(struct ceph_msg_data_cursor *cursor,
967 size_t *page_offset, size_t *length)
Alex Eldere766d7b2013-03-07 15:38:28 -0600968{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500969 struct ceph_msg_data *data = cursor->data;
Alex Eldere766d7b2013-03-07 15:38:28 -0600970
971 BUG_ON(data->type != CEPH_MSG_DATA_PAGES);
972
973 BUG_ON(cursor->page_index >= cursor->page_count);
974 BUG_ON(cursor->page_offset >= PAGE_SIZE);
Alex Eldere766d7b2013-03-07 15:38:28 -0600975
976 *page_offset = cursor->page_offset;
Alex Elder25aff7c2013-03-11 23:34:22 -0500977 if (cursor->last_piece)
Alex Eldere766d7b2013-03-07 15:38:28 -0600978 *length = cursor->resid;
Alex Elder25aff7c2013-03-11 23:34:22 -0500979 else
Alex Eldere766d7b2013-03-07 15:38:28 -0600980 *length = PAGE_SIZE - *page_offset;
Alex Eldere766d7b2013-03-07 15:38:28 -0600981
982 return data->pages[cursor->page_index];
983}
984
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500985static bool ceph_msg_data_pages_advance(struct ceph_msg_data_cursor *cursor,
Alex Eldere766d7b2013-03-07 15:38:28 -0600986 size_t bytes)
987{
Alex Elder8ae4f4f2013-03-14 14:09:06 -0500988 BUG_ON(cursor->data->type != CEPH_MSG_DATA_PAGES);
Alex Eldere766d7b2013-03-07 15:38:28 -0600989
990 BUG_ON(cursor->page_offset + bytes > PAGE_SIZE);
Alex Eldere766d7b2013-03-07 15:38:28 -0600991
992 /* Advance the cursor page offset */
993
994 cursor->resid -= bytes;
Alex Elder5df521b2013-03-30 15:09:59 -0500995 cursor->page_offset = (cursor->page_offset + bytes) & ~PAGE_MASK;
996 if (!bytes || cursor->page_offset)
Alex Eldere766d7b2013-03-07 15:38:28 -0600997 return false; /* more bytes to process in the current page */
998
Yan, Zhengd90deda2014-03-23 06:50:39 +0800999 if (!cursor->resid)
1000 return false; /* no more data */
1001
Alex Elder5df521b2013-03-30 15:09:59 -05001002 /* Move on to the next page; offset is already at 0 */
Alex Eldere766d7b2013-03-07 15:38:28 -06001003
1004 BUG_ON(cursor->page_index >= cursor->page_count);
Alex Eldere766d7b2013-03-07 15:38:28 -06001005 cursor->page_index++;
Alex Elder25aff7c2013-03-11 23:34:22 -05001006 cursor->last_piece = cursor->resid <= PAGE_SIZE;
Alex Eldere766d7b2013-03-07 15:38:28 -06001007
1008 return true;
1009}
1010
1011/*
Alex Elderdd236fc2013-03-06 23:39:39 -06001012 * For a pagelist, a piece is whatever remains to be consumed in the
1013 * first page in the list, or the front of the next page.
Alex Elderfe38a2b2013-03-06 23:39:39 -06001014 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001015static void
1016ceph_msg_data_pagelist_cursor_init(struct ceph_msg_data_cursor *cursor,
Alex Elder25aff7c2013-03-11 23:34:22 -05001017 size_t length)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001018{
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001019 struct ceph_msg_data *data = cursor->data;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001020 struct ceph_pagelist *pagelist;
1021 struct page *page;
1022
Alex Elderdd236fc2013-03-06 23:39:39 -06001023 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001024
1025 pagelist = data->pagelist;
1026 BUG_ON(!pagelist);
Alex Elder25aff7c2013-03-11 23:34:22 -05001027
1028 if (!length)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001029 return; /* pagelist can be assigned but empty */
1030
1031 BUG_ON(list_empty(&pagelist->head));
1032 page = list_first_entry(&pagelist->head, struct page, lru);
1033
Alex Elderca8b3a62013-04-05 14:46:01 -05001034 cursor->resid = min(length, pagelist->length);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001035 cursor->page = page;
1036 cursor->offset = 0;
Alex Eldera51b272e2013-04-19 15:34:49 -05001037 cursor->last_piece = cursor->resid <= PAGE_SIZE;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001038}
1039
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001040static struct page *
1041ceph_msg_data_pagelist_next(struct ceph_msg_data_cursor *cursor,
1042 size_t *page_offset, size_t *length)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001043{
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001044 struct ceph_msg_data *data = cursor->data;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001045 struct ceph_pagelist *pagelist;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001046
1047 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1048
1049 pagelist = data->pagelist;
1050 BUG_ON(!pagelist);
1051
1052 BUG_ON(!cursor->page);
Alex Elder25aff7c2013-03-11 23:34:22 -05001053 BUG_ON(cursor->offset + cursor->resid != pagelist->length);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001054
Alex Elder5df521b2013-03-30 15:09:59 -05001055 /* offset of first page in pagelist is always 0 */
Alex Elderfe38a2b2013-03-06 23:39:39 -06001056 *page_offset = cursor->offset & ~PAGE_MASK;
Alex Elder5df521b2013-03-30 15:09:59 -05001057 if (cursor->last_piece)
Alex Elder25aff7c2013-03-11 23:34:22 -05001058 *length = cursor->resid;
1059 else
1060 *length = PAGE_SIZE - *page_offset;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001061
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001062 return cursor->page;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001063}
1064
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001065static bool ceph_msg_data_pagelist_advance(struct ceph_msg_data_cursor *cursor,
Alex Elderdd236fc2013-03-06 23:39:39 -06001066 size_t bytes)
Alex Elderfe38a2b2013-03-06 23:39:39 -06001067{
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001068 struct ceph_msg_data *data = cursor->data;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001069 struct ceph_pagelist *pagelist;
1070
1071 BUG_ON(data->type != CEPH_MSG_DATA_PAGELIST);
1072
1073 pagelist = data->pagelist;
1074 BUG_ON(!pagelist);
Alex Elder25aff7c2013-03-11 23:34:22 -05001075
1076 BUG_ON(cursor->offset + cursor->resid != pagelist->length);
Alex Elderfe38a2b2013-03-06 23:39:39 -06001077 BUG_ON((cursor->offset & ~PAGE_MASK) + bytes > PAGE_SIZE);
1078
1079 /* Advance the cursor offset */
1080
Alex Elder25aff7c2013-03-11 23:34:22 -05001081 cursor->resid -= bytes;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001082 cursor->offset += bytes;
Alex Elder5df521b2013-03-30 15:09:59 -05001083 /* offset of first page in pagelist is always 0 */
Alex Elderfe38a2b2013-03-06 23:39:39 -06001084 if (!bytes || cursor->offset & ~PAGE_MASK)
1085 return false; /* more bytes to process in the current page */
1086
Yan, Zhengd90deda2014-03-23 06:50:39 +08001087 if (!cursor->resid)
1088 return false; /* no more data */
1089
Alex Elderfe38a2b2013-03-06 23:39:39 -06001090 /* Move on to the next page */
1091
1092 BUG_ON(list_is_last(&cursor->page->lru, &pagelist->head));
Geliang Tang17ddc492015-11-16 21:46:32 +08001093 cursor->page = list_next_entry(cursor->page, lru);
Alex Elder25aff7c2013-03-11 23:34:22 -05001094 cursor->last_piece = cursor->resid <= PAGE_SIZE;
Alex Elderfe38a2b2013-03-06 23:39:39 -06001095
1096 return true;
1097}
1098
Alex Elderdd236fc2013-03-06 23:39:39 -06001099/*
1100 * Message data is handled (sent or received) in pieces, where each
1101 * piece resides on a single page. The network layer might not
1102 * consume an entire piece at once. A data item's cursor keeps
1103 * track of which piece is next to process and how much remains to
1104 * be processed in that piece. It also tracks whether the current
1105 * piece is the last one in the data item.
1106 */
Alex Elderca8b3a62013-04-05 14:46:01 -05001107static void __ceph_msg_data_cursor_init(struct ceph_msg_data_cursor *cursor)
Alex Elderdd236fc2013-03-06 23:39:39 -06001108{
Alex Elderca8b3a62013-04-05 14:46:01 -05001109 size_t length = cursor->total_resid;
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001110
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001111 switch (cursor->data->type) {
Alex Elderdd236fc2013-03-06 23:39:39 -06001112 case CEPH_MSG_DATA_PAGELIST:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001113 ceph_msg_data_pagelist_cursor_init(cursor, length);
Alex Elderdd236fc2013-03-06 23:39:39 -06001114 break;
Alex Eldere766d7b2013-03-07 15:38:28 -06001115 case CEPH_MSG_DATA_PAGES:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001116 ceph_msg_data_pages_cursor_init(cursor, length);
Alex Eldere766d7b2013-03-07 15:38:28 -06001117 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001118#ifdef CONFIG_BLOCK
1119 case CEPH_MSG_DATA_BIO:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001120 ceph_msg_data_bio_cursor_init(cursor, length);
Alex Elder6aaa4512013-03-06 23:39:39 -06001121 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001122#endif /* CONFIG_BLOCK */
Ilya Dryomovb9e281c2018-01-20 10:30:11 +01001123 case CEPH_MSG_DATA_BVECS:
1124 ceph_msg_data_bvecs_cursor_init(cursor, length);
1125 break;
Alex Elder6aaa4512013-03-06 23:39:39 -06001126 case CEPH_MSG_DATA_NONE:
Alex Elderdd236fc2013-03-06 23:39:39 -06001127 default:
1128 /* BUG(); */
1129 break;
1130 }
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001131 cursor->need_crc = true;
Alex Elderdd236fc2013-03-06 23:39:39 -06001132}
1133
Alex Elderca8b3a62013-04-05 14:46:01 -05001134static void ceph_msg_data_cursor_init(struct ceph_msg *msg, size_t length)
1135{
1136 struct ceph_msg_data_cursor *cursor = &msg->cursor;
Alex Elderca8b3a62013-04-05 14:46:01 -05001137
1138 BUG_ON(!length);
1139 BUG_ON(length > msg->data_length);
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02001140 BUG_ON(!msg->num_data_items);
Alex Elderca8b3a62013-04-05 14:46:01 -05001141
Alex Elderca8b3a62013-04-05 14:46:01 -05001142 cursor->total_resid = length;
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02001143 cursor->data = msg->data;
Alex Elderca8b3a62013-04-05 14:46:01 -05001144
1145 __ceph_msg_data_cursor_init(cursor);
1146}
1147
Alex Elderdd236fc2013-03-06 23:39:39 -06001148/*
1149 * Return the page containing the next piece to process for a given
1150 * data item, and supply the page offset and length of that piece.
1151 * Indicate whether this is the last piece in this data item.
1152 */
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001153static struct page *ceph_msg_data_next(struct ceph_msg_data_cursor *cursor,
1154 size_t *page_offset, size_t *length,
Alex Elderdd236fc2013-03-06 23:39:39 -06001155 bool *last_piece)
1156{
1157 struct page *page;
1158
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001159 switch (cursor->data->type) {
Alex Elderdd236fc2013-03-06 23:39:39 -06001160 case CEPH_MSG_DATA_PAGELIST:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001161 page = ceph_msg_data_pagelist_next(cursor, page_offset, length);
Alex Elderdd236fc2013-03-06 23:39:39 -06001162 break;
Alex Eldere766d7b2013-03-07 15:38:28 -06001163 case CEPH_MSG_DATA_PAGES:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001164 page = ceph_msg_data_pages_next(cursor, page_offset, length);
Alex Eldere766d7b2013-03-07 15:38:28 -06001165 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001166#ifdef CONFIG_BLOCK
1167 case CEPH_MSG_DATA_BIO:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001168 page = ceph_msg_data_bio_next(cursor, page_offset, length);
Alex Elder6aaa4512013-03-06 23:39:39 -06001169 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001170#endif /* CONFIG_BLOCK */
Ilya Dryomovb9e281c2018-01-20 10:30:11 +01001171 case CEPH_MSG_DATA_BVECS:
1172 page = ceph_msg_data_bvecs_next(cursor, page_offset, length);
1173 break;
Alex Elder6aaa4512013-03-06 23:39:39 -06001174 case CEPH_MSG_DATA_NONE:
Alex Elderdd236fc2013-03-06 23:39:39 -06001175 default:
1176 page = NULL;
1177 break;
1178 }
Ilya Dryomov5359a172018-01-20 10:30:10 +01001179
Alex Elderdd236fc2013-03-06 23:39:39 -06001180 BUG_ON(!page);
1181 BUG_ON(*page_offset + *length > PAGE_SIZE);
1182 BUG_ON(!*length);
Ilya Dryomov5359a172018-01-20 10:30:10 +01001183 BUG_ON(*length > cursor->resid);
Alex Elderdd236fc2013-03-06 23:39:39 -06001184 if (last_piece)
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001185 *last_piece = cursor->last_piece;
Alex Elderdd236fc2013-03-06 23:39:39 -06001186
1187 return page;
1188}
1189
1190/*
1191 * Returns true if the result moves the cursor on to the next piece
1192 * of the data item.
1193 */
Ilya Dryomov1759f7b2017-05-19 11:38:17 +02001194static void ceph_msg_data_advance(struct ceph_msg_data_cursor *cursor,
1195 size_t bytes)
Alex Elderdd236fc2013-03-06 23:39:39 -06001196{
1197 bool new_piece;
1198
Alex Elder25aff7c2013-03-11 23:34:22 -05001199 BUG_ON(bytes > cursor->resid);
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001200 switch (cursor->data->type) {
Alex Elderdd236fc2013-03-06 23:39:39 -06001201 case CEPH_MSG_DATA_PAGELIST:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001202 new_piece = ceph_msg_data_pagelist_advance(cursor, bytes);
Alex Elderdd236fc2013-03-06 23:39:39 -06001203 break;
Alex Eldere766d7b2013-03-07 15:38:28 -06001204 case CEPH_MSG_DATA_PAGES:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001205 new_piece = ceph_msg_data_pages_advance(cursor, bytes);
Alex Eldere766d7b2013-03-07 15:38:28 -06001206 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001207#ifdef CONFIG_BLOCK
1208 case CEPH_MSG_DATA_BIO:
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001209 new_piece = ceph_msg_data_bio_advance(cursor, bytes);
Alex Elder6aaa4512013-03-06 23:39:39 -06001210 break;
Alex Elderdd236fc2013-03-06 23:39:39 -06001211#endif /* CONFIG_BLOCK */
Ilya Dryomovb9e281c2018-01-20 10:30:11 +01001212 case CEPH_MSG_DATA_BVECS:
1213 new_piece = ceph_msg_data_bvecs_advance(cursor, bytes);
1214 break;
Alex Elder6aaa4512013-03-06 23:39:39 -06001215 case CEPH_MSG_DATA_NONE:
Alex Elderdd236fc2013-03-06 23:39:39 -06001216 default:
1217 BUG();
1218 break;
1219 }
Alex Elderca8b3a62013-04-05 14:46:01 -05001220 cursor->total_resid -= bytes;
Alex Elderdd236fc2013-03-06 23:39:39 -06001221
Alex Elderca8b3a62013-04-05 14:46:01 -05001222 if (!cursor->resid && cursor->total_resid) {
1223 WARN_ON(!cursor->last_piece);
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02001224 cursor->data++;
Alex Elderca8b3a62013-04-05 14:46:01 -05001225 __ceph_msg_data_cursor_init(cursor);
Alex Eldera51b272e2013-04-19 15:34:49 -05001226 new_piece = true;
Alex Elderca8b3a62013-04-05 14:46:01 -05001227 }
Alex Eldera51b272e2013-04-19 15:34:49 -05001228 cursor->need_crc = new_piece;
Alex Elderdd236fc2013-03-06 23:39:39 -06001229}
1230
Ilya Dryomovdbc0d3c2016-02-19 11:38:57 +01001231static size_t sizeof_footer(struct ceph_connection *con)
1232{
1233 return (con->peer_features & CEPH_FEATURE_MSG_AUTH) ?
1234 sizeof(struct ceph_msg_footer) :
1235 sizeof(struct ceph_msg_footer_old);
1236}
1237
Alex Elder98fa5dd2013-04-02 12:09:50 -05001238static void prepare_message_data(struct ceph_msg *msg, u32 data_len)
Alex Elder739c9052012-06-11 14:57:13 -05001239{
Alex Elder4c59b4a2013-03-11 23:34:23 -05001240 /* Initialize data cursor */
Alex Elderfe38a2b2013-03-06 23:39:39 -06001241
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001242 ceph_msg_data_cursor_init(msg, (size_t)data_len);
Alex Elder739c9052012-06-11 14:57:13 -05001243}
1244
Sage Weil31b80062009-10-06 11:31:13 -07001245/*
1246 * Prepare footer for currently outgoing message, and finish things
1247 * off. Assumes out_kvec* are already valid.. we just add on to the end.
1248 */
Alex Elder859eb792012-02-14 14:05:33 -06001249static void prepare_write_message_footer(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001250{
1251 struct ceph_msg *m = con->out_msg;
1252
Alex Elderfd154f32012-06-11 14:57:13 -05001253 m->footer.flags |= CEPH_MSG_FOOTER_COMPLETE;
1254
Sage Weil31b80062009-10-06 11:31:13 -07001255 dout("prepare_write_message_footer %p\n", con);
Ilya Dryomov89f08172016-02-20 15:56:07 +01001256 con_out_kvec_add(con, sizeof_footer(con), &m->footer);
Yan, Zheng33d07332014-11-04 16:33:37 +08001257 if (con->peer_features & CEPH_FEATURE_MSG_AUTH) {
1258 if (con->ops->sign_message)
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01001259 con->ops->sign_message(m);
Yan, Zheng33d07332014-11-04 16:33:37 +08001260 else
1261 m->footer.sig = 0;
Yan, Zheng33d07332014-11-04 16:33:37 +08001262 } else {
1263 m->old_footer.flags = m->footer.flags;
Yan, Zheng33d07332014-11-04 16:33:37 +08001264 }
Sage Weil31b80062009-10-06 11:31:13 -07001265 con->out_more = m->more_to_follow;
Sage Weilc86a2932009-12-14 14:04:30 -08001266 con->out_msg_done = true;
Sage Weil31b80062009-10-06 11:31:13 -07001267}
1268
1269/*
1270 * Prepare headers for the next outgoing message.
1271 */
1272static void prepare_write_message(struct ceph_connection *con)
1273{
1274 struct ceph_msg *m;
Alex Eldera9a0c512012-02-15 07:43:54 -06001275 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001276
Alex Eldere2200422012-05-23 14:35:23 -05001277 con_out_kvec_reset(con);
Sage Weilc86a2932009-12-14 14:04:30 -08001278 con->out_msg_done = false;
Sage Weil31b80062009-10-06 11:31:13 -07001279
1280 /* Sneak an ack in there first? If we can get it into the same
1281 * TCP packet that's a good thing. */
1282 if (con->in_seq > con->in_seq_acked) {
1283 con->in_seq_acked = con->in_seq;
Alex Eldere2200422012-05-23 14:35:23 -05001284 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001285 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -05001286 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -06001287 &con->out_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07001288 }
1289
Alex Elder38941f82012-06-01 14:56:43 -05001290 BUG_ON(list_empty(&con->out_queue));
Alex Elder859eb792012-02-14 14:05:33 -06001291 m = list_first_entry(&con->out_queue, struct ceph_msg, list_head);
Sage Weilc86a2932009-12-14 14:04:30 -08001292 con->out_msg = m;
Alex Elder38941f82012-06-01 14:56:43 -05001293 BUG_ON(m->con != con);
Sage Weil4cf9d542011-07-26 11:27:24 -07001294
1295 /* put message on sent list */
1296 ceph_msg_get(m);
1297 list_move_tail(&m->list_head, &con->out_sent);
Sage Weil31b80062009-10-06 11:31:13 -07001298
Sage Weile84346b2010-05-11 21:20:38 -07001299 /*
1300 * only assign outgoing seq # if we haven't sent this message
1301 * yet. if it is requeued, resend with it's original seq.
1302 */
1303 if (m->needs_out_seq) {
1304 m->hdr.seq = cpu_to_le64(++con->out_seq);
1305 m->needs_out_seq = false;
Ilya Dryomov98ad5eb2017-06-15 16:30:54 +02001306
Ilya Dryomov4690faf2017-07-26 09:59:15 +02001307 if (con->ops->reencode_message)
1308 con->ops->reencode_message(m);
1309 }
Sage Weil31b80062009-10-06 11:31:13 -07001310
Alex Elder98fa5dd2013-04-02 12:09:50 -05001311 dout("prepare_write_message %p seq %lld type %d len %d+%d+%zd\n",
Sage Weil31b80062009-10-06 11:31:13 -07001312 m, con->out_seq, le16_to_cpu(m->hdr.type),
1313 le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len),
Alex Elder98fa5dd2013-04-02 12:09:50 -05001314 m->data_length);
Ilya Dryomov98ad5eb2017-06-15 16:30:54 +02001315 WARN_ON(m->front.iov_len != le32_to_cpu(m->hdr.front_len));
1316 WARN_ON(m->data_length != le32_to_cpu(m->hdr.data_len));
Sage Weil31b80062009-10-06 11:31:13 -07001317
1318 /* tag + hdr + front + middle */
Alex Eldere2200422012-05-23 14:35:23 -05001319 con_out_kvec_add(con, sizeof (tag_msg), &tag_msg);
Ilya Dryomov67645d72015-12-28 13:18:34 +03001320 con_out_kvec_add(con, sizeof(con->out_hdr), &con->out_hdr);
Alex Eldere2200422012-05-23 14:35:23 -05001321 con_out_kvec_add(con, m->front.iov_len, m->front.iov_base);
Alex Elder859eb792012-02-14 14:05:33 -06001322
Sage Weil31b80062009-10-06 11:31:13 -07001323 if (m->middle)
Alex Eldere2200422012-05-23 14:35:23 -05001324 con_out_kvec_add(con, m->middle->vec.iov_len,
Alex Elder859eb792012-02-14 14:05:33 -06001325 m->middle->vec.iov_base);
Sage Weil31b80062009-10-06 11:31:13 -07001326
Ilya Dryomov67645d72015-12-28 13:18:34 +03001327 /* fill in hdr crc and finalize hdr */
Alex Eldera9a0c512012-02-15 07:43:54 -06001328 crc = crc32c(0, &m->hdr, offsetof(struct ceph_msg_header, crc));
1329 con->out_msg->hdr.crc = cpu_to_le32(crc);
Ilya Dryomov67645d72015-12-28 13:18:34 +03001330 memcpy(&con->out_hdr, &con->out_msg->hdr, sizeof(con->out_hdr));
Alex Eldera9a0c512012-02-15 07:43:54 -06001331
Ilya Dryomov67645d72015-12-28 13:18:34 +03001332 /* fill in front and middle crc, footer */
Alex Eldera9a0c512012-02-15 07:43:54 -06001333 crc = crc32c(0, m->front.iov_base, m->front.iov_len);
1334 con->out_msg->footer.front_crc = cpu_to_le32(crc);
1335 if (m->middle) {
1336 crc = crc32c(0, m->middle->vec.iov_base,
1337 m->middle->vec.iov_len);
1338 con->out_msg->footer.middle_crc = cpu_to_le32(crc);
1339 } else
Sage Weil31b80062009-10-06 11:31:13 -07001340 con->out_msg->footer.middle_crc = 0;
Alex Elder739c9052012-06-11 14:57:13 -05001341 dout("%s front_crc %u middle_crc %u\n", __func__,
Sage Weil31b80062009-10-06 11:31:13 -07001342 le32_to_cpu(con->out_msg->footer.front_crc),
1343 le32_to_cpu(con->out_msg->footer.middle_crc));
Ilya Dryomov67645d72015-12-28 13:18:34 +03001344 con->out_msg->footer.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001345
1346 /* is there a data payload? */
Alex Elder739c9052012-06-11 14:57:13 -05001347 con->out_msg->footer.data_crc = 0;
Alex Elder98fa5dd2013-04-02 12:09:50 -05001348 if (m->data_length) {
1349 prepare_message_data(con->out_msg, m->data_length);
Alex Elder78625052013-03-06 23:39:39 -06001350 con->out_more = 1; /* data + footer will follow */
1351 } else {
Sage Weil31b80062009-10-06 11:31:13 -07001352 /* no, queue up footer too and be done */
Alex Elder859eb792012-02-14 14:05:33 -06001353 prepare_write_message_footer(con);
Alex Elder78625052013-03-06 23:39:39 -06001354 }
Sage Weil31b80062009-10-06 11:31:13 -07001355
Alex Elderc9ffc772013-02-20 10:25:12 -06001356 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07001357}
1358
1359/*
1360 * Prepare an ack.
1361 */
1362static void prepare_write_ack(struct ceph_connection *con)
1363{
1364 dout("prepare_write_ack %p %llu -> %llu\n", con,
1365 con->in_seq_acked, con->in_seq);
1366 con->in_seq_acked = con->in_seq;
1367
Alex Eldere2200422012-05-23 14:35:23 -05001368 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001369
Alex Eldere2200422012-05-23 14:35:23 -05001370 con_out_kvec_add(con, sizeof (tag_ack), &tag_ack);
Alex Elder859eb792012-02-14 14:05:33 -06001371
Sage Weil31b80062009-10-06 11:31:13 -07001372 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
Alex Eldere2200422012-05-23 14:35:23 -05001373 con_out_kvec_add(con, sizeof (con->out_temp_ack),
Alex Elder859eb792012-02-14 14:05:33 -06001374 &con->out_temp_ack);
1375
Sage Weil31b80062009-10-06 11:31:13 -07001376 con->out_more = 1; /* more will follow.. eventually.. */
Alex Elderc9ffc772013-02-20 10:25:12 -06001377 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07001378}
1379
1380/*
Sage Weil3a230832013-03-25 08:47:40 -07001381 * Prepare to share the seq during handshake
1382 */
1383static void prepare_write_seq(struct ceph_connection *con)
1384{
1385 dout("prepare_write_seq %p %llu -> %llu\n", con,
1386 con->in_seq_acked, con->in_seq);
1387 con->in_seq_acked = con->in_seq;
1388
1389 con_out_kvec_reset(con);
1390
1391 con->out_temp_ack = cpu_to_le64(con->in_seq_acked);
1392 con_out_kvec_add(con, sizeof (con->out_temp_ack),
1393 &con->out_temp_ack);
1394
1395 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1396}
1397
1398/*
Sage Weil31b80062009-10-06 11:31:13 -07001399 * Prepare to write keepalive byte.
1400 */
1401static void prepare_write_keepalive(struct ceph_connection *con)
1402{
1403 dout("prepare_write_keepalive %p\n", con);
Alex Eldere2200422012-05-23 14:35:23 -05001404 con_out_kvec_reset(con);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001405 if (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2) {
Arnd Bergmann473bd2d2018-07-13 22:18:34 +02001406 struct timespec64 now;
Ilya Dryomov7f61f542015-09-14 16:01:05 +03001407
Arnd Bergmann473bd2d2018-07-13 22:18:34 +02001408 ktime_get_real_ts64(&now);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001409 con_out_kvec_add(con, sizeof(tag_keepalive2), &tag_keepalive2);
Arnd Bergmann473bd2d2018-07-13 22:18:34 +02001410 ceph_encode_timespec64(&con->out_temp_keepalive2, &now);
Ilya Dryomov7f61f542015-09-14 16:01:05 +03001411 con_out_kvec_add(con, sizeof(con->out_temp_keepalive2),
1412 &con->out_temp_keepalive2);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001413 } else {
1414 con_out_kvec_add(con, sizeof(tag_keepalive), &tag_keepalive);
1415 }
Alex Elderc9ffc772013-02-20 10:25:12 -06001416 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07001417}
1418
1419/*
1420 * Connection negotiation.
1421 */
1422
Ilya Dryomov262614c2018-07-26 15:17:46 +02001423static int get_connect_authorizer(struct ceph_connection *con)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001424{
Alex Eldera3530df2012-05-16 15:16:39 -05001425 struct ceph_auth_handshake *auth;
Ilya Dryomov262614c2018-07-26 15:17:46 +02001426 int auth_proto;
Alex Elderb1c6b982012-05-16 15:16:38 -05001427
1428 if (!con->ops->get_authorizer) {
Ilya Dryomov262614c2018-07-26 15:17:46 +02001429 con->auth = NULL;
Alex Elderb1c6b982012-05-16 15:16:38 -05001430 con->out_connect.authorizer_protocol = CEPH_AUTH_UNKNOWN;
1431 con->out_connect.authorizer_len = 0;
Ilya Dryomov262614c2018-07-26 15:17:46 +02001432 return 0;
Alex Elderb1c6b982012-05-16 15:16:38 -05001433 }
1434
Ilya Dryomov262614c2018-07-26 15:17:46 +02001435 auth = con->ops->get_authorizer(con, &auth_proto, con->auth_retry);
Alex Eldera3530df2012-05-16 15:16:39 -05001436 if (IS_ERR(auth))
Ilya Dryomov262614c2018-07-26 15:17:46 +02001437 return PTR_ERR(auth);
Sage Weil0da5d702011-05-19 11:21:05 -07001438
Ilya Dryomov262614c2018-07-26 15:17:46 +02001439 con->auth = auth;
1440 con->out_connect.authorizer_protocol = cpu_to_le32(auth_proto);
1441 con->out_connect.authorizer_len = cpu_to_le32(auth->authorizer_buf_len);
1442 return 0;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001443}
1444
Sage Weil31b80062009-10-06 11:31:13 -07001445/*
1446 * We connected to a peer and are saying hello.
1447 */
Alex Eldere825a662012-05-16 15:16:38 -05001448static void prepare_write_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001449{
Alex Eldere2200422012-05-23 14:35:23 -05001450 con_out_kvec_add(con, strlen(CEPH_BANNER), CEPH_BANNER);
1451 con_out_kvec_add(con, sizeof (con->msgr->my_enc_addr),
Alex Eldere825a662012-05-16 15:16:38 -05001452 &con->msgr->my_enc_addr);
Sage Weileed0ef22009-11-10 14:34:36 -08001453
Sage Weileed0ef22009-11-10 14:34:36 -08001454 con->out_more = 0;
Alex Elderc9ffc772013-02-20 10:25:12 -06001455 con_flag_set(con, CON_FLAG_WRITE_PENDING);
Sage Weileed0ef22009-11-10 14:34:36 -08001456}
1457
Ilya Dryomovc0f56b42018-07-26 17:43:47 +02001458static void __prepare_write_connect(struct ceph_connection *con)
1459{
1460 con_out_kvec_add(con, sizeof(con->out_connect), &con->out_connect);
1461 if (con->auth)
1462 con_out_kvec_add(con, con->auth->authorizer_buf_len,
1463 con->auth->authorizer_buf);
1464
1465 con->out_more = 0;
1466 con_flag_set(con, CON_FLAG_WRITE_PENDING);
1467}
1468
Alex Eldere825a662012-05-16 15:16:38 -05001469static int prepare_write_connect(struct ceph_connection *con)
Sage Weileed0ef22009-11-10 14:34:36 -08001470{
Eric Dumazet95c96172012-04-15 05:58:06 +00001471 unsigned int global_seq = get_global_seq(con->msgr, 0);
Sage Weil31b80062009-10-06 11:31:13 -07001472 int proto;
Ilya Dryomov262614c2018-07-26 15:17:46 +02001473 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001474
1475 switch (con->peer_name.type) {
1476 case CEPH_ENTITY_TYPE_MON:
1477 proto = CEPH_MONC_PROTOCOL;
1478 break;
1479 case CEPH_ENTITY_TYPE_OSD:
1480 proto = CEPH_OSDC_PROTOCOL;
1481 break;
1482 case CEPH_ENTITY_TYPE_MDS:
1483 proto = CEPH_MDSC_PROTOCOL;
1484 break;
1485 default:
1486 BUG();
1487 }
1488
1489 dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con,
1490 con->connect_seq, global_seq, proto);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001491
Ilya Dryomov859bff52015-10-28 23:50:58 +01001492 con->out_connect.features =
1493 cpu_to_le64(from_msgr(con->msgr)->supported_features);
Sage Weil31b80062009-10-06 11:31:13 -07001494 con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT);
1495 con->out_connect.connect_seq = cpu_to_le32(con->connect_seq);
1496 con->out_connect.global_seq = cpu_to_le32(global_seq);
1497 con->out_connect.protocol_version = cpu_to_le32(proto);
1498 con->out_connect.flags = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001499
Ilya Dryomov262614c2018-07-26 15:17:46 +02001500 ret = get_connect_authorizer(con);
1501 if (ret)
1502 return ret;
Alex Elder3da54772012-05-16 15:16:39 -05001503
Ilya Dryomovc0f56b42018-07-26 17:43:47 +02001504 __prepare_write_connect(con);
Alex Eldere10c7582012-05-16 15:16:38 -05001505 return 0;
Sage Weil31b80062009-10-06 11:31:13 -07001506}
1507
Sage Weil31b80062009-10-06 11:31:13 -07001508/*
1509 * write as much of pending kvecs to the socket as we can.
1510 * 1 -> done
1511 * 0 -> socket full, but more to do
1512 * <0 -> error
1513 */
1514static int write_partial_kvec(struct ceph_connection *con)
1515{
1516 int ret;
1517
1518 dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes);
1519 while (con->out_kvec_bytes > 0) {
1520 ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur,
1521 con->out_kvec_left, con->out_kvec_bytes,
1522 con->out_more);
1523 if (ret <= 0)
1524 goto out;
1525 con->out_kvec_bytes -= ret;
1526 if (con->out_kvec_bytes == 0)
1527 break; /* done */
Alex Elderf42299e2012-02-15 07:43:54 -06001528
1529 /* account for full iov entries consumed */
1530 while (ret >= con->out_kvec_cur->iov_len) {
1531 BUG_ON(!con->out_kvec_left);
1532 ret -= con->out_kvec_cur->iov_len;
1533 con->out_kvec_cur++;
1534 con->out_kvec_left--;
1535 }
1536 /* and for a partially-consumed entry */
1537 if (ret) {
1538 con->out_kvec_cur->iov_len -= ret;
1539 con->out_kvec_cur->iov_base += ret;
Sage Weil31b80062009-10-06 11:31:13 -07001540 }
1541 }
1542 con->out_kvec_left = 0;
Sage Weil31b80062009-10-06 11:31:13 -07001543 ret = 1;
1544out:
1545 dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con,
1546 con->out_kvec_bytes, con->out_kvec_left, ret);
1547 return ret; /* done! */
1548}
1549
Alex Elder35b62802013-03-08 20:59:00 -06001550static u32 ceph_crc32c_page(u32 crc, struct page *page,
1551 unsigned int page_offset,
1552 unsigned int length)
1553{
1554 char *kaddr;
1555
1556 kaddr = kmap(page);
1557 BUG_ON(kaddr == NULL);
1558 crc = crc32c(crc, kaddr + page_offset, length);
1559 kunmap(page);
1560
1561 return crc;
1562}
Sage Weil31b80062009-10-06 11:31:13 -07001563/*
1564 * Write as much message data payload as we can. If we finish, queue
1565 * up the footer.
1566 * 1 -> done, footer is now queued in out_kvec[].
1567 * 0 -> socket full, but more to do
1568 * <0 -> error
1569 */
Alex Elder34d2d202013-03-08 20:58:59 -06001570static int write_partial_message_data(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001571{
1572 struct ceph_msg *msg = con->out_msg;
Alex Elder8ae4f4f2013-03-14 14:09:06 -05001573 struct ceph_msg_data_cursor *cursor = &msg->cursor;
Ilya Dryomov859bff52015-10-28 23:50:58 +01001574 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
Alex Elderf5db90b2013-03-11 23:34:23 -05001575 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07001576
Alex Elder859a35d2013-03-11 23:34:23 -05001577 dout("%s %p msg %p\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07001578
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02001579 if (!msg->num_data_items)
Alex Elder4c59b4a2013-03-11 23:34:23 -05001580 return -EINVAL;
1581
Alex Elder5821bd82012-06-11 14:57:13 -05001582 /*
1583 * Iterate through each page that contains data to be
1584 * written, and send as much as possible for each.
1585 *
1586 * If we are calculating the data crc (the default), we will
1587 * need to map the page. If we have no pages, they have
1588 * been revoked, so use the zero page.
1589 */
Alex Elderf5db90b2013-03-11 23:34:23 -05001590 crc = do_datacrc ? le32_to_cpu(msg->footer.data_crc) : 0;
Ilya Dryomov45a267d2018-01-22 15:20:15 +01001591 while (cursor->total_resid) {
Alex Elder8a166d02013-03-08 13:35:36 -06001592 struct page *page;
Alex Eldere387d522013-03-06 23:39:38 -06001593 size_t page_offset;
1594 size_t length;
Alex Elder8a166d02013-03-08 13:35:36 -06001595 bool last_piece;
Alex Elderf5db90b2013-03-11 23:34:23 -05001596 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001597
Ilya Dryomov45a267d2018-01-22 15:20:15 +01001598 if (!cursor->resid) {
1599 ceph_msg_data_advance(cursor, 0);
1600 continue;
1601 }
1602
Shraddha Barke343128c2015-10-19 21:59:00 +05301603 page = ceph_msg_data_next(cursor, &page_offset, &length,
1604 &last_piece);
Alex Eldere387d522013-03-06 23:39:38 -06001605 ret = ceph_tcp_sendpage(con->sock, page, page_offset,
Benoît Canetc2cfa192015-06-25 20:32:34 +03001606 length, !last_piece);
Alex Elderf5db90b2013-03-11 23:34:23 -05001607 if (ret <= 0) {
1608 if (do_datacrc)
1609 msg->footer.data_crc = cpu_to_le32(crc);
Sage Weil31b80062009-10-06 11:31:13 -07001610
Alex Elderf5db90b2013-03-11 23:34:23 -05001611 return ret;
1612 }
Alex Elder143334f2013-03-29 11:44:10 -05001613 if (do_datacrc && cursor->need_crc)
1614 crc = ceph_crc32c_page(crc, page, page_offset, length);
Ilya Dryomov1759f7b2017-05-19 11:38:17 +02001615 ceph_msg_data_advance(cursor, (size_t)ret);
Sage Weil31b80062009-10-06 11:31:13 -07001616 }
1617
Alex Elder34d2d202013-03-08 20:58:59 -06001618 dout("%s %p msg %p done\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07001619
1620 /* prepare and queue up footer, too */
Alex Elderf5db90b2013-03-11 23:34:23 -05001621 if (do_datacrc)
1622 msg->footer.data_crc = cpu_to_le32(crc);
1623 else
Alex Elder84ca8fc2012-06-11 14:57:13 -05001624 msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC;
Alex Eldere2200422012-05-23 14:35:23 -05001625 con_out_kvec_reset(con);
Alex Elder859eb792012-02-14 14:05:33 -06001626 prepare_write_message_footer(con);
Alex Elderf5db90b2013-03-11 23:34:23 -05001627
1628 return 1; /* must return > 0 to indicate success */
Sage Weil31b80062009-10-06 11:31:13 -07001629}
1630
1631/*
1632 * write some zeros
1633 */
1634static int write_partial_skip(struct ceph_connection *con)
1635{
1636 int ret;
1637
Ilya Dryomov67645d72015-12-28 13:18:34 +03001638 dout("%s %p %d left\n", __func__, con, con->out_skip);
Sage Weil31b80062009-10-06 11:31:13 -07001639 while (con->out_skip > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001640 size_t size = min(con->out_skip, (int) PAGE_SIZE);
Sage Weil31b80062009-10-06 11:31:13 -07001641
Alex Eldere1dcb122013-03-06 23:39:38 -06001642 ret = ceph_tcp_sendpage(con->sock, zero_page, 0, size, true);
Sage Weil31b80062009-10-06 11:31:13 -07001643 if (ret <= 0)
1644 goto out;
1645 con->out_skip -= ret;
1646 }
1647 ret = 1;
1648out:
1649 return ret;
1650}
1651
1652/*
1653 * Prepare to read connection handshake, or an ack.
1654 */
Sage Weileed0ef22009-11-10 14:34:36 -08001655static void prepare_read_banner(struct ceph_connection *con)
1656{
1657 dout("prepare_read_banner %p\n", con);
1658 con->in_base_pos = 0;
1659}
1660
Sage Weil31b80062009-10-06 11:31:13 -07001661static void prepare_read_connect(struct ceph_connection *con)
1662{
1663 dout("prepare_read_connect %p\n", con);
1664 con->in_base_pos = 0;
1665}
1666
1667static void prepare_read_ack(struct ceph_connection *con)
1668{
1669 dout("prepare_read_ack %p\n", con);
1670 con->in_base_pos = 0;
1671}
1672
Sage Weil3a230832013-03-25 08:47:40 -07001673static void prepare_read_seq(struct ceph_connection *con)
1674{
1675 dout("prepare_read_seq %p\n", con);
1676 con->in_base_pos = 0;
1677 con->in_tag = CEPH_MSGR_TAG_SEQ;
1678}
1679
Sage Weil31b80062009-10-06 11:31:13 -07001680static void prepare_read_tag(struct ceph_connection *con)
1681{
1682 dout("prepare_read_tag %p\n", con);
1683 con->in_base_pos = 0;
1684 con->in_tag = CEPH_MSGR_TAG_READY;
1685}
1686
Yan, Zheng8b9558a2015-09-01 17:19:38 +08001687static void prepare_read_keepalive_ack(struct ceph_connection *con)
1688{
1689 dout("prepare_read_keepalive_ack %p\n", con);
1690 con->in_base_pos = 0;
1691}
1692
Sage Weil31b80062009-10-06 11:31:13 -07001693/*
1694 * Prepare to read a message.
1695 */
1696static int prepare_read_message(struct ceph_connection *con)
1697{
1698 dout("prepare_read_message %p\n", con);
1699 BUG_ON(con->in_msg != NULL);
1700 con->in_base_pos = 0;
1701 con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0;
1702 return 0;
1703}
1704
1705
1706static int read_partial(struct ceph_connection *con,
Alex Elderfd516532012-05-10 10:29:50 -05001707 int end, int size, void *object)
Sage Weil31b80062009-10-06 11:31:13 -07001708{
Alex Eldere6cee712012-05-10 10:29:50 -05001709 while (con->in_base_pos < end) {
1710 int left = end - con->in_base_pos;
Sage Weil31b80062009-10-06 11:31:13 -07001711 int have = size - left;
1712 int ret = ceph_tcp_recvmsg(con->sock, object + have, left);
1713 if (ret <= 0)
1714 return ret;
1715 con->in_base_pos += ret;
1716 }
1717 return 1;
1718}
1719
1720
1721/*
1722 * Read all or part of the connect-side handshake on a new connection
1723 */
Sage Weileed0ef22009-11-10 14:34:36 -08001724static int read_partial_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07001725{
Alex Elderfd516532012-05-10 10:29:50 -05001726 int size;
1727 int end;
1728 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07001729
Sage Weileed0ef22009-11-10 14:34:36 -08001730 dout("read_partial_banner %p at %d\n", con, con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07001731
1732 /* peer's banner */
Alex Elderfd516532012-05-10 10:29:50 -05001733 size = strlen(CEPH_BANNER);
1734 end = size;
1735 ret = read_partial(con, end, size, con->in_banner);
Sage Weil31b80062009-10-06 11:31:13 -07001736 if (ret <= 0)
1737 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001738
1739 size = sizeof (con->actual_peer_addr);
1740 end += size;
1741 ret = read_partial(con, end, size, &con->actual_peer_addr);
Sage Weil31b80062009-10-06 11:31:13 -07001742 if (ret <= 0)
1743 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001744
1745 size = sizeof (con->peer_addr_for_me);
1746 end += size;
1747 ret = read_partial(con, end, size, &con->peer_addr_for_me);
Sage Weil31b80062009-10-06 11:31:13 -07001748 if (ret <= 0)
1749 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001750
Sage Weileed0ef22009-11-10 14:34:36 -08001751out:
1752 return ret;
1753}
1754
1755static int read_partial_connect(struct ceph_connection *con)
1756{
Alex Elderfd516532012-05-10 10:29:50 -05001757 int size;
1758 int end;
1759 int ret;
Sage Weileed0ef22009-11-10 14:34:36 -08001760
1761 dout("read_partial_connect %p at %d\n", con, con->in_base_pos);
1762
Alex Elderfd516532012-05-10 10:29:50 -05001763 size = sizeof (con->in_reply);
1764 end = size;
1765 ret = read_partial(con, end, size, &con->in_reply);
Sage Weil31b80062009-10-06 11:31:13 -07001766 if (ret <= 0)
1767 goto out;
Alex Elderfd516532012-05-10 10:29:50 -05001768
Ilya Dryomov262614c2018-07-26 15:17:46 +02001769 if (con->auth) {
1770 size = le32_to_cpu(con->in_reply.authorizer_len);
Ilya Dryomov130f52f2018-07-27 19:40:30 +02001771 if (size > con->auth->authorizer_reply_buf_len) {
1772 pr_err("authorizer reply too big: %d > %zu\n", size,
1773 con->auth->authorizer_reply_buf_len);
1774 ret = -EINVAL;
1775 goto out;
1776 }
1777
Ilya Dryomov262614c2018-07-26 15:17:46 +02001778 end += size;
1779 ret = read_partial(con, end, size,
1780 con->auth->authorizer_reply_buf);
1781 if (ret <= 0)
1782 goto out;
1783 }
Sage Weil31b80062009-10-06 11:31:13 -07001784
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001785 dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n",
1786 con, (int)con->in_reply.tag,
1787 le32_to_cpu(con->in_reply.connect_seq),
Sage Weil31b80062009-10-06 11:31:13 -07001788 le32_to_cpu(con->in_reply.global_seq));
1789out:
1790 return ret;
1791}
1792
1793/*
1794 * Verify the hello banner looks okay.
1795 */
1796static int verify_hello(struct ceph_connection *con)
1797{
1798 if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) {
Sage Weil13e38c82009-10-09 16:36:34 -07001799 pr_err("connect to %s got bad banner\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001800 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07001801 con->error_msg = "protocol error, bad banner";
1802 return -1;
1803 }
1804 return 0;
1805}
1806
1807static bool addr_is_blank(struct sockaddr_storage *ss)
1808{
Ilya Dryomovc44bd692015-07-09 13:57:52 +03001809 struct in_addr *addr = &((struct sockaddr_in *)ss)->sin_addr;
1810 struct in6_addr *addr6 = &((struct sockaddr_in6 *)ss)->sin6_addr;
1811
Sage Weil31b80062009-10-06 11:31:13 -07001812 switch (ss->ss_family) {
1813 case AF_INET:
Ilya Dryomovc44bd692015-07-09 13:57:52 +03001814 return addr->s_addr == htonl(INADDR_ANY);
Sage Weil31b80062009-10-06 11:31:13 -07001815 case AF_INET6:
Ilya Dryomovc44bd692015-07-09 13:57:52 +03001816 return ipv6_addr_any(addr6);
1817 default:
1818 return true;
Sage Weil31b80062009-10-06 11:31:13 -07001819 }
Sage Weil31b80062009-10-06 11:31:13 -07001820}
1821
1822static int addr_port(struct sockaddr_storage *ss)
1823{
1824 switch (ss->ss_family) {
1825 case AF_INET:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001826 return ntohs(((struct sockaddr_in *)ss)->sin_port);
Sage Weil31b80062009-10-06 11:31:13 -07001827 case AF_INET6:
Sage Weilf28bcfb2009-11-04 11:46:35 -08001828 return ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
Sage Weil31b80062009-10-06 11:31:13 -07001829 }
1830 return 0;
1831}
1832
1833static void addr_set_port(struct sockaddr_storage *ss, int p)
1834{
1835 switch (ss->ss_family) {
1836 case AF_INET:
1837 ((struct sockaddr_in *)ss)->sin_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001838 break;
Sage Weil31b80062009-10-06 11:31:13 -07001839 case AF_INET6:
1840 ((struct sockaddr_in6 *)ss)->sin6_port = htons(p);
Sage Weila2a79602011-05-12 15:34:24 -07001841 break;
Sage Weil31b80062009-10-06 11:31:13 -07001842 }
1843}
1844
1845/*
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001846 * Unlike other *_pton function semantics, zero indicates success.
1847 */
1848static int ceph_pton(const char *str, size_t len, struct sockaddr_storage *ss,
1849 char delim, const char **ipend)
1850{
Alex Elder99f0f3b2012-01-23 15:49:27 -06001851 struct sockaddr_in *in4 = (struct sockaddr_in *) ss;
1852 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) ss;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001853
1854 memset(ss, 0, sizeof(*ss));
1855
1856 if (in4_pton(str, len, (u8 *)&in4->sin_addr.s_addr, delim, ipend)) {
1857 ss->ss_family = AF_INET;
1858 return 0;
1859 }
1860
1861 if (in6_pton(str, len, (u8 *)&in6->sin6_addr.s6_addr, delim, ipend)) {
1862 ss->ss_family = AF_INET6;
1863 return 0;
1864 }
1865
1866 return -EINVAL;
1867}
1868
1869/*
1870 * Extract hostname string and resolve using kernel DNS facility.
1871 */
1872#ifdef CONFIG_CEPH_LIB_USE_DNS_RESOLVER
1873static int ceph_dns_resolve_name(const char *name, size_t namelen,
1874 struct sockaddr_storage *ss, char delim, const char **ipend)
1875{
1876 const char *end, *delim_p;
1877 char *colon_p, *ip_addr = NULL;
1878 int ip_len, ret;
1879
1880 /*
1881 * The end of the hostname occurs immediately preceding the delimiter or
1882 * the port marker (':') where the delimiter takes precedence.
1883 */
1884 delim_p = memchr(name, delim, namelen);
1885 colon_p = memchr(name, ':', namelen);
1886
1887 if (delim_p && colon_p)
1888 end = delim_p < colon_p ? delim_p : colon_p;
1889 else if (!delim_p && colon_p)
1890 end = colon_p;
1891 else {
1892 end = delim_p;
1893 if (!end) /* case: hostname:/ */
1894 end = name + namelen;
1895 }
1896
1897 if (end <= name)
1898 return -EINVAL;
1899
1900 /* do dns_resolve upcall */
1901 ip_len = dns_query(NULL, name, end - name, NULL, &ip_addr, NULL);
1902 if (ip_len > 0)
1903 ret = ceph_pton(ip_addr, ip_len, ss, -1, NULL);
1904 else
1905 ret = -ESRCH;
1906
1907 kfree(ip_addr);
1908
1909 *ipend = end;
1910
1911 pr_info("resolve '%.*s' (ret=%d): %s\n", (int)(end - name), name,
1912 ret, ret ? "failed" : ceph_pr_addr(ss));
1913
1914 return ret;
1915}
1916#else
1917static inline int ceph_dns_resolve_name(const char *name, size_t namelen,
1918 struct sockaddr_storage *ss, char delim, const char **ipend)
1919{
1920 return -EINVAL;
1921}
1922#endif
1923
1924/*
1925 * Parse a server name (IP or hostname). If a valid IP address is not found
1926 * then try to extract a hostname to resolve using userspace DNS upcall.
1927 */
1928static int ceph_parse_server_name(const char *name, size_t namelen,
1929 struct sockaddr_storage *ss, char delim, const char **ipend)
1930{
1931 int ret;
1932
1933 ret = ceph_pton(name, namelen, ss, delim, ipend);
1934 if (ret)
1935 ret = ceph_dns_resolve_name(name, namelen, ss, delim, ipend);
1936
1937 return ret;
1938}
1939
1940/*
Sage Weil31b80062009-10-06 11:31:13 -07001941 * Parse an ip[:port] list into an addr array. Use the default
1942 * monitor port if a port isn't specified.
1943 */
1944int ceph_parse_ips(const char *c, const char *end,
1945 struct ceph_entity_addr *addr,
1946 int max_count, int *count)
1947{
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001948 int i, ret = -EINVAL;
Sage Weil31b80062009-10-06 11:31:13 -07001949 const char *p = c;
1950
1951 dout("parse_ips on '%.*s'\n", (int)(end-c), c);
1952 for (i = 0; i < max_count; i++) {
1953 const char *ipend;
1954 struct sockaddr_storage *ss = &addr[i].in_addr;
Sage Weil31b80062009-10-06 11:31:13 -07001955 int port;
Sage Weil39139f62010-07-08 09:54:52 -07001956 char delim = ',';
1957
1958 if (*p == '[') {
1959 delim = ']';
1960 p++;
1961 }
Sage Weil31b80062009-10-06 11:31:13 -07001962
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001963 ret = ceph_parse_server_name(p, end - p, ss, delim, &ipend);
1964 if (ret)
Sage Weil31b80062009-10-06 11:31:13 -07001965 goto bad;
Noah Watkinsee3b56f2011-09-23 11:48:42 -07001966 ret = -EINVAL;
1967
Sage Weil31b80062009-10-06 11:31:13 -07001968 p = ipend;
1969
Sage Weil39139f62010-07-08 09:54:52 -07001970 if (delim == ']') {
1971 if (*p != ']') {
1972 dout("missing matching ']'\n");
1973 goto bad;
1974 }
1975 p++;
1976 }
1977
Sage Weil31b80062009-10-06 11:31:13 -07001978 /* port? */
1979 if (p < end && *p == ':') {
1980 port = 0;
1981 p++;
1982 while (p < end && *p >= '0' && *p <= '9') {
1983 port = (port * 10) + (*p - '0');
1984 p++;
1985 }
Ilya Dryomovf48db1e2013-12-30 19:21:29 +02001986 if (port == 0)
1987 port = CEPH_MON_PORT;
1988 else if (port > 65535)
Sage Weil31b80062009-10-06 11:31:13 -07001989 goto bad;
1990 } else {
1991 port = CEPH_MON_PORT;
1992 }
1993
1994 addr_set_port(ss, port);
1995
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001996 dout("parse_ips got %s\n", ceph_pr_addr(ss));
Sage Weil31b80062009-10-06 11:31:13 -07001997
1998 if (p == end)
1999 break;
2000 if (*p != ',')
2001 goto bad;
2002 p++;
2003 }
2004
2005 if (p != end)
2006 goto bad;
2007
2008 if (count)
2009 *count = i + 1;
2010 return 0;
2011
2012bad:
Sage Weil39139f62010-07-08 09:54:52 -07002013 pr_err("parse_ips bad ip '%.*s'\n", (int)(end - c), c);
Noah Watkinsee3b56f2011-09-23 11:48:42 -07002014 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002015}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002016EXPORT_SYMBOL(ceph_parse_ips);
Sage Weil31b80062009-10-06 11:31:13 -07002017
Sage Weileed0ef22009-11-10 14:34:36 -08002018static int process_banner(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07002019{
Sage Weileed0ef22009-11-10 14:34:36 -08002020 dout("process_banner on %p\n", con);
Sage Weil31b80062009-10-06 11:31:13 -07002021
2022 if (verify_hello(con) < 0)
2023 return -1;
2024
Sage Weil63f2d212009-11-03 15:17:56 -08002025 ceph_decode_addr(&con->actual_peer_addr);
2026 ceph_decode_addr(&con->peer_addr_for_me);
2027
Sage Weil31b80062009-10-06 11:31:13 -07002028 /*
2029 * Make sure the other end is who we wanted. note that the other
2030 * end may not yet know their ip address, so if it's 0.0.0.0, give
2031 * them the benefit of the doubt.
2032 */
Sage Weil103e2d32010-01-07 16:12:36 -08002033 if (memcmp(&con->peer_addr, &con->actual_peer_addr,
2034 sizeof(con->peer_addr)) != 0 &&
Sage Weil31b80062009-10-06 11:31:13 -07002035 !(addr_is_blank(&con->actual_peer_addr.in_addr) &&
2036 con->actual_peer_addr.nonce == con->peer_addr.nonce)) {
Joe Perchesb9a67892014-09-09 21:17:29 -07002037 pr_warn("wrong peer, want %s/%d, got %s/%d\n",
2038 ceph_pr_addr(&con->peer_addr.in_addr),
2039 (int)le32_to_cpu(con->peer_addr.nonce),
2040 ceph_pr_addr(&con->actual_peer_addr.in_addr),
2041 (int)le32_to_cpu(con->actual_peer_addr.nonce));
Sage Weil58bb3b32009-12-23 12:12:31 -08002042 con->error_msg = "wrong peer at address";
Sage Weil31b80062009-10-06 11:31:13 -07002043 return -1;
2044 }
2045
2046 /*
2047 * did we learn our address?
2048 */
2049 if (addr_is_blank(&con->msgr->inst.addr.in_addr)) {
2050 int port = addr_port(&con->msgr->inst.addr.in_addr);
2051
2052 memcpy(&con->msgr->inst.addr.in_addr,
2053 &con->peer_addr_for_me.in_addr,
2054 sizeof(con->peer_addr_for_me.in_addr));
2055 addr_set_port(&con->msgr->inst.addr.in_addr, port);
Sage Weil63f2d212009-11-03 15:17:56 -08002056 encode_my_addr(con->msgr);
Sage Weileed0ef22009-11-10 14:34:36 -08002057 dout("process_banner learned my addr is %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002058 ceph_pr_addr(&con->msgr->inst.addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002059 }
2060
Sage Weileed0ef22009-11-10 14:34:36 -08002061 return 0;
2062}
2063
2064static int process_connect(struct ceph_connection *con)
2065{
Ilya Dryomov859bff52015-10-28 23:50:58 +01002066 u64 sup_feat = from_msgr(con->msgr)->supported_features;
2067 u64 req_feat = from_msgr(con->msgr)->required_features;
Ilya Dryomovdcbbd972017-06-05 14:44:59 +02002068 u64 server_feat = le64_to_cpu(con->in_reply.features);
Sage Weil0da5d702011-05-19 11:21:05 -07002069 int ret;
Sage Weil04a419f2009-12-23 09:30:21 -08002070
Sage Weileed0ef22009-11-10 14:34:36 -08002071 dout("process_connect on %p tag %d\n", con, (int)con->in_tag);
2072
Ilya Dryomov262614c2018-07-26 15:17:46 +02002073 if (con->auth) {
Ilya Dryomov5c056fd2016-12-02 16:35:09 +01002074 /*
2075 * Any connection that defines ->get_authorizer()
Ilya Dryomov6daca132018-07-27 19:18:34 +02002076 * should also define ->add_authorizer_challenge() and
2077 * ->verify_authorizer_reply().
2078 *
Ilya Dryomov5c056fd2016-12-02 16:35:09 +01002079 * See get_connect_authorizer().
2080 */
Ilya Dryomov6daca132018-07-27 19:18:34 +02002081 if (con->in_reply.tag == CEPH_MSGR_TAG_CHALLENGE_AUTHORIZER) {
2082 ret = con->ops->add_authorizer_challenge(
2083 con, con->auth->authorizer_reply_buf,
2084 le32_to_cpu(con->in_reply.authorizer_len));
2085 if (ret < 0)
2086 return ret;
2087
2088 con_out_kvec_reset(con);
2089 __prepare_write_connect(con);
2090 prepare_read_connect(con);
2091 return 0;
2092 }
2093
Ilya Dryomov0dde5842016-12-02 16:35:09 +01002094 ret = con->ops->verify_authorizer_reply(con);
Ilya Dryomov5c056fd2016-12-02 16:35:09 +01002095 if (ret < 0) {
2096 con->error_msg = "bad authorize reply";
2097 return ret;
2098 }
2099 }
2100
Sage Weil31b80062009-10-06 11:31:13 -07002101 switch (con->in_reply.tag) {
Sage Weil04a419f2009-12-23 09:30:21 -08002102 case CEPH_MSGR_TAG_FEATURES:
2103 pr_err("%s%lld %s feature set mismatch,"
2104 " my %llx < server's %llx, missing %llx\n",
2105 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002106 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08002107 sup_feat, server_feat, server_feat & ~sup_feat);
2108 con->error_msg = "missing required protocol features";
Sage Weil0fa6ebc2012-12-27 20:27:04 -06002109 reset_connection(con);
Sage Weil04a419f2009-12-23 09:30:21 -08002110 return -1;
2111
Sage Weil31b80062009-10-06 11:31:13 -07002112 case CEPH_MSGR_TAG_BADPROTOVER:
Sage Weil31b80062009-10-06 11:31:13 -07002113 pr_err("%s%lld %s protocol version mismatch,"
2114 " my %d != server's %d\n",
2115 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002116 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil31b80062009-10-06 11:31:13 -07002117 le32_to_cpu(con->out_connect.protocol_version),
2118 le32_to_cpu(con->in_reply.protocol_version));
2119 con->error_msg = "protocol version mismatch";
Sage Weil0fa6ebc2012-12-27 20:27:04 -06002120 reset_connection(con);
Sage Weil31b80062009-10-06 11:31:13 -07002121 return -1;
2122
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002123 case CEPH_MSGR_TAG_BADAUTHORIZER:
2124 con->auth_retry++;
2125 dout("process_connect %p got BADAUTHORIZER attempt %d\n", con,
2126 con->auth_retry);
2127 if (con->auth_retry == 2) {
2128 con->error_msg = "connect authorization failure";
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002129 return -1;
2130 }
Jim Schutt6d4221b2012-08-10 10:37:38 -07002131 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05002132 ret = prepare_write_connect(con);
Sage Weil0da5d702011-05-19 11:21:05 -07002133 if (ret < 0)
2134 return ret;
Sage Weil63733a02010-03-15 15:47:22 -07002135 prepare_read_connect(con);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002136 break;
Sage Weil31b80062009-10-06 11:31:13 -07002137
2138 case CEPH_MSGR_TAG_RESETSESSION:
2139 /*
2140 * If we connected with a large connect_seq but the peer
2141 * has no record of a session with us (no connection, or
2142 * connect_seq == 0), they will send RESETSESION to indicate
2143 * that they must have reset their session, and may have
2144 * dropped messages.
2145 */
2146 dout("process_connect got RESET peer seq %u\n",
Sage Weil5bdca4e2012-07-10 11:53:34 -07002147 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil31b80062009-10-06 11:31:13 -07002148 pr_err("%s%lld %s connection reset\n",
2149 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002150 ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002151 reset_connection(con);
Jim Schutt6d4221b2012-08-10 10:37:38 -07002152 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05002153 ret = prepare_write_connect(con);
2154 if (ret < 0)
2155 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002156 prepare_read_connect(con);
2157
2158 /* Tell ceph about it. */
Sage Weilec302642009-12-22 10:43:42 -08002159 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002160 pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name));
2161 if (con->ops->peer_reset)
2162 con->ops->peer_reset(con);
Sage Weilec302642009-12-22 10:43:42 -08002163 mutex_lock(&con->mutex);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002164 if (con->state != CON_STATE_NEGOTIATING)
Sage Weil0da5d702011-05-19 11:21:05 -07002165 return -EAGAIN;
Sage Weil31b80062009-10-06 11:31:13 -07002166 break;
2167
2168 case CEPH_MSGR_TAG_RETRY_SESSION:
2169 /*
2170 * If we sent a smaller connect_seq than the peer has, try
2171 * again with a larger value.
2172 */
Sage Weil5bdca4e2012-07-10 11:53:34 -07002173 dout("process_connect got RETRY_SESSION my seq %u, peer %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07002174 le32_to_cpu(con->out_connect.connect_seq),
Sage Weil5bdca4e2012-07-10 11:53:34 -07002175 le32_to_cpu(con->in_reply.connect_seq));
2176 con->connect_seq = le32_to_cpu(con->in_reply.connect_seq);
Jim Schutt6d4221b2012-08-10 10:37:38 -07002177 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05002178 ret = prepare_write_connect(con);
2179 if (ret < 0)
2180 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002181 prepare_read_connect(con);
2182 break;
2183
2184 case CEPH_MSGR_TAG_RETRY_GLOBAL:
2185 /*
2186 * If we sent a smaller global_seq than the peer has, try
2187 * again with a larger value.
2188 */
Sage Weileed0ef22009-11-10 14:34:36 -08002189 dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n",
Sage Weil31b80062009-10-06 11:31:13 -07002190 con->peer_global_seq,
Sage Weil5bdca4e2012-07-10 11:53:34 -07002191 le32_to_cpu(con->in_reply.global_seq));
Sage Weil31b80062009-10-06 11:31:13 -07002192 get_global_seq(con->msgr,
Sage Weil5bdca4e2012-07-10 11:53:34 -07002193 le32_to_cpu(con->in_reply.global_seq));
Jim Schutt6d4221b2012-08-10 10:37:38 -07002194 con_out_kvec_reset(con);
Alex Elder5a0f8fd2012-05-16 21:51:59 -05002195 ret = prepare_write_connect(con);
2196 if (ret < 0)
2197 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002198 prepare_read_connect(con);
2199 break;
2200
Sage Weil3a230832013-03-25 08:47:40 -07002201 case CEPH_MSGR_TAG_SEQ:
Sage Weil31b80062009-10-06 11:31:13 -07002202 case CEPH_MSGR_TAG_READY:
Sage Weil04a419f2009-12-23 09:30:21 -08002203 if (req_feat & ~server_feat) {
2204 pr_err("%s%lld %s protocol feature mismatch,"
2205 " my required %llx > server's %llx, need %llx\n",
2206 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002207 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weil04a419f2009-12-23 09:30:21 -08002208 req_feat, server_feat, req_feat & ~server_feat);
2209 con->error_msg = "missing required protocol features";
Sage Weil0fa6ebc2012-12-27 20:27:04 -06002210 reset_connection(con);
Sage Weil04a419f2009-12-23 09:30:21 -08002211 return -1;
2212 }
Sage Weil8dacc7d2012-07-20 17:24:40 -07002213
Alex Elder122070a2012-12-26 10:43:57 -06002214 WARN_ON(con->state != CON_STATE_NEGOTIATING);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002215 con->state = CON_STATE_OPEN;
Sage Weil20e55c42013-03-25 09:30:13 -07002216 con->auth_retry = 0; /* we authenticated; clear flag */
Sage Weil31b80062009-10-06 11:31:13 -07002217 con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq);
2218 con->connect_seq++;
Sage Weilaba558e2010-05-12 15:23:30 -07002219 con->peer_features = server_feat;
Sage Weil31b80062009-10-06 11:31:13 -07002220 dout("process_connect got READY gseq %d cseq %d (%d)\n",
2221 con->peer_global_seq,
2222 le32_to_cpu(con->in_reply.connect_seq),
2223 con->connect_seq);
2224 WARN_ON(con->connect_seq !=
2225 le32_to_cpu(con->in_reply.connect_seq));
Sage Weil92ac41d2009-12-14 14:56:56 -08002226
2227 if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY)
Alex Elderc9ffc772013-02-20 10:25:12 -06002228 con_flag_set(con, CON_FLAG_LOSSYTX);
Sage Weil92ac41d2009-12-14 14:56:56 -08002229
Sage Weil85effe12012-07-30 16:22:05 -07002230 con->delay = 0; /* reset backoff memory */
Sage Weil31b80062009-10-06 11:31:13 -07002231
Sage Weil3a230832013-03-25 08:47:40 -07002232 if (con->in_reply.tag == CEPH_MSGR_TAG_SEQ) {
2233 prepare_write_seq(con);
2234 prepare_read_seq(con);
2235 } else {
2236 prepare_read_tag(con);
2237 }
Sage Weil31b80062009-10-06 11:31:13 -07002238 break;
2239
2240 case CEPH_MSGR_TAG_WAIT:
2241 /*
2242 * If there is a connection race (we are opening
2243 * connections to each other), one of us may just have
2244 * to WAIT. This shouldn't happen if we are the
2245 * client.
2246 */
Sage Weil04177882011-05-12 15:33:17 -07002247 con->error_msg = "protocol error, got WAIT as client";
2248 return -1;
Sage Weil31b80062009-10-06 11:31:13 -07002249
2250 default:
Sage Weil31b80062009-10-06 11:31:13 -07002251 con->error_msg = "protocol error, garbage tag during connect";
2252 return -1;
2253 }
2254 return 0;
2255}
2256
2257
2258/*
2259 * read (part of) an ack
2260 */
2261static int read_partial_ack(struct ceph_connection *con)
2262{
Alex Elderfd516532012-05-10 10:29:50 -05002263 int size = sizeof (con->in_temp_ack);
2264 int end = size;
Sage Weil31b80062009-10-06 11:31:13 -07002265
Alex Elderfd516532012-05-10 10:29:50 -05002266 return read_partial(con, end, size, &con->in_temp_ack);
Sage Weil31b80062009-10-06 11:31:13 -07002267}
2268
Sage Weil31b80062009-10-06 11:31:13 -07002269/*
2270 * We can finally discard anything that's been acked.
2271 */
2272static void process_ack(struct ceph_connection *con)
2273{
2274 struct ceph_msg *m;
2275 u64 ack = le64_to_cpu(con->in_temp_ack);
2276 u64 seq;
Yan, Zheng0a2ad542017-05-05 18:47:37 +08002277 bool reconnect = (con->in_tag == CEPH_MSGR_TAG_SEQ);
2278 struct list_head *list = reconnect ? &con->out_queue : &con->out_sent;
Sage Weil31b80062009-10-06 11:31:13 -07002279
Yan, Zheng0a2ad542017-05-05 18:47:37 +08002280 /*
2281 * In the reconnect case, con_fault() has requeued messages
2282 * in out_sent. We should cleanup old messages according to
2283 * the reconnect seq.
2284 */
2285 while (!list_empty(list)) {
2286 m = list_first_entry(list, struct ceph_msg, list_head);
2287 if (reconnect && m->needs_out_seq)
2288 break;
Sage Weil31b80062009-10-06 11:31:13 -07002289 seq = le64_to_cpu(m->hdr.seq);
2290 if (seq > ack)
2291 break;
2292 dout("got ack for seq %llu type %d at %p\n", seq,
2293 le16_to_cpu(m->hdr.type), m);
Sage Weil4cf9d542011-07-26 11:27:24 -07002294 m->ack_stamp = jiffies;
Sage Weil31b80062009-10-06 11:31:13 -07002295 ceph_msg_remove(m);
2296 }
Yan, Zheng0a2ad542017-05-05 18:47:37 +08002297
Sage Weil31b80062009-10-06 11:31:13 -07002298 prepare_read_tag(con);
2299}
2300
2301
Yehuda Sadeh24504182010-01-08 13:58:34 -08002302static int read_partial_message_section(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07002303 struct kvec *section,
2304 unsigned int sec_len, u32 *crc)
Yehuda Sadeh24504182010-01-08 13:58:34 -08002305{
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002306 int ret, left;
Sage Weil31b80062009-10-06 11:31:13 -07002307
Yehuda Sadeh24504182010-01-08 13:58:34 -08002308 BUG_ON(!section);
Sage Weil31b80062009-10-06 11:31:13 -07002309
Yehuda Sadeh24504182010-01-08 13:58:34 -08002310 while (section->iov_len < sec_len) {
2311 BUG_ON(section->iov_base == NULL);
2312 left = sec_len - section->iov_len;
2313 ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base +
2314 section->iov_len, left);
2315 if (ret <= 0)
2316 return ret;
2317 section->iov_len += ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002318 }
Alex Elderfe3ad592012-02-15 07:43:54 -06002319 if (section->iov_len == sec_len)
2320 *crc = crc32c(0, section->iov_base, section->iov_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002321
2322 return 1;
2323}
2324
Alex Elder34d2d202013-03-08 20:58:59 -06002325static int read_partial_msg_data(struct ceph_connection *con)
2326{
2327 struct ceph_msg *msg = con->in_msg;
Alex Elder8ae4f4f2013-03-14 14:09:06 -05002328 struct ceph_msg_data_cursor *cursor = &msg->cursor;
Ilya Dryomov859bff52015-10-28 23:50:58 +01002329 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
Alex Elder686be202013-03-11 23:34:23 -05002330 struct page *page;
2331 size_t page_offset;
2332 size_t length;
Alex Elderf5db90b2013-03-11 23:34:23 -05002333 u32 crc = 0;
Alex Elder34d2d202013-03-08 20:58:59 -06002334 int ret;
2335
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02002336 if (!msg->num_data_items)
Alex Elder4c59b4a2013-03-11 23:34:23 -05002337 return -EIO;
Alex Elder34d2d202013-03-08 20:58:59 -06002338
Alex Elderf5db90b2013-03-11 23:34:23 -05002339 if (do_datacrc)
2340 crc = con->in_data_crc;
Ilya Dryomov45a267d2018-01-22 15:20:15 +01002341 while (cursor->total_resid) {
2342 if (!cursor->resid) {
2343 ceph_msg_data_advance(cursor, 0);
2344 continue;
2345 }
2346
Shraddha Barke343128c2015-10-19 21:59:00 +05302347 page = ceph_msg_data_next(cursor, &page_offset, &length, NULL);
Alex Elder686be202013-03-11 23:34:23 -05002348 ret = ceph_tcp_recvpage(con->sock, page, page_offset, length);
Alex Elderf5db90b2013-03-11 23:34:23 -05002349 if (ret <= 0) {
2350 if (do_datacrc)
2351 con->in_data_crc = crc;
2352
Alex Elder686be202013-03-11 23:34:23 -05002353 return ret;
Alex Elderf5db90b2013-03-11 23:34:23 -05002354 }
Alex Elder686be202013-03-11 23:34:23 -05002355
2356 if (do_datacrc)
Alex Elderf5db90b2013-03-11 23:34:23 -05002357 crc = ceph_crc32c_page(crc, page, page_offset, ret);
Ilya Dryomov1759f7b2017-05-19 11:38:17 +02002358 ceph_msg_data_advance(cursor, (size_t)ret);
Alex Elder34d2d202013-03-08 20:58:59 -06002359 }
Alex Elderf5db90b2013-03-11 23:34:23 -05002360 if (do_datacrc)
2361 con->in_data_crc = crc;
Alex Elder34d2d202013-03-08 20:58:59 -06002362
2363 return 1; /* must return > 0 to indicate success */
2364}
2365
Sage Weil31b80062009-10-06 11:31:13 -07002366/*
2367 * read (part of) a message.
2368 */
Alex Elder686be202013-03-11 23:34:23 -05002369static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip);
2370
Sage Weil31b80062009-10-06 11:31:13 -07002371static int read_partial_message(struct ceph_connection *con)
2372{
2373 struct ceph_msg *m = con->in_msg;
Alex Elderfd516532012-05-10 10:29:50 -05002374 int size;
2375 int end;
Sage Weil31b80062009-10-06 11:31:13 -07002376 int ret;
Eric Dumazet95c96172012-04-15 05:58:06 +00002377 unsigned int front_len, middle_len, data_len;
Ilya Dryomov859bff52015-10-28 23:50:58 +01002378 bool do_datacrc = !ceph_test_opt(from_msgr(con->msgr), NOCRC);
Yan, Zheng33d07332014-11-04 16:33:37 +08002379 bool need_sign = (con->peer_features & CEPH_FEATURE_MSG_AUTH);
Sage Weilae187562010-04-22 07:47:01 -07002380 u64 seq;
Alex Elderfe3ad592012-02-15 07:43:54 -06002381 u32 crc;
Sage Weil31b80062009-10-06 11:31:13 -07002382
2383 dout("read_partial_message con %p msg %p\n", con, m);
2384
2385 /* header */
Alex Elderfd516532012-05-10 10:29:50 -05002386 size = sizeof (con->in_hdr);
2387 end = size;
2388 ret = read_partial(con, end, size, &con->in_hdr);
Alex Elder57dac9d2012-05-10 10:29:50 -05002389 if (ret <= 0)
2390 return ret;
Alex Elderfe3ad592012-02-15 07:43:54 -06002391
2392 crc = crc32c(0, &con->in_hdr, offsetof(struct ceph_msg_header, crc));
2393 if (cpu_to_le32(crc) != con->in_hdr.crc) {
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002394 pr_err("read_partial_message bad hdr crc %u != expected %u\n",
Alex Elderfe3ad592012-02-15 07:43:54 -06002395 crc, con->in_hdr.crc);
2396 return -EBADMSG;
2397 }
2398
Sage Weil31b80062009-10-06 11:31:13 -07002399 front_len = le32_to_cpu(con->in_hdr.front_len);
2400 if (front_len > CEPH_MSG_MAX_FRONT_LEN)
2401 return -EIO;
2402 middle_len = le32_to_cpu(con->in_hdr.middle_len);
Alex Elder7b11ba32013-03-08 18:51:03 -06002403 if (middle_len > CEPH_MSG_MAX_MIDDLE_LEN)
Sage Weil31b80062009-10-06 11:31:13 -07002404 return -EIO;
2405 data_len = le32_to_cpu(con->in_hdr.data_len);
2406 if (data_len > CEPH_MSG_MAX_DATA_LEN)
2407 return -EIO;
2408
Sage Weilae187562010-04-22 07:47:01 -07002409 /* verify seq# */
2410 seq = le64_to_cpu(con->in_hdr.seq);
2411 if ((s64)seq - (s64)con->in_seq < 1) {
Sage Weildf9f86f2010-11-01 15:49:23 -07002412 pr_info("skipping %s%lld %s seq %lld expected %lld\n",
Sage Weilae187562010-04-22 07:47:01 -07002413 ENTITY_NAME(con->peer_name),
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002414 ceph_pr_addr(&con->peer_addr.in_addr),
Sage Weilae187562010-04-22 07:47:01 -07002415 seq, con->in_seq + 1);
2416 con->in_base_pos = -front_len - middle_len - data_len -
Ilya Dryomovdbc0d3c2016-02-19 11:38:57 +01002417 sizeof_footer(con);
Sage Weilae187562010-04-22 07:47:01 -07002418 con->in_tag = CEPH_MSGR_TAG_READY;
Ilya Dryomove7a88e82016-02-17 20:04:08 +01002419 return 1;
Sage Weilae187562010-04-22 07:47:01 -07002420 } else if ((s64)seq - (s64)con->in_seq > 1) {
2421 pr_err("read_partial_message bad seq %lld expected %lld\n",
2422 seq, con->in_seq + 1);
2423 con->error_msg = "bad message sequence # for incoming message";
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002424 return -EBADE;
Sage Weilae187562010-04-22 07:47:01 -07002425 }
2426
Sage Weil31b80062009-10-06 11:31:13 -07002427 /* allocate message? */
2428 if (!con->in_msg) {
Sage Weil4740a622012-07-30 18:19:30 -07002429 int skip = 0;
2430
Sage Weil31b80062009-10-06 11:31:13 -07002431 dout("got hdr type %d front %d data %d\n", con->in_hdr.type,
Alex Elder6ebc8b32013-03-08 18:51:03 -06002432 front_len, data_len);
Sage Weil4740a622012-07-30 18:19:30 -07002433 ret = ceph_con_in_msg_alloc(con, &skip);
2434 if (ret < 0)
2435 return ret;
Alex Elderf759ebb2013-04-05 14:46:01 -05002436
2437 BUG_ON(!con->in_msg ^ skip);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002438 if (skip) {
Sage Weil31b80062009-10-06 11:31:13 -07002439 /* skip this message */
Sage Weila79832f2010-04-01 16:06:19 -07002440 dout("alloc_msg said skip message\n");
Sage Weil31b80062009-10-06 11:31:13 -07002441 con->in_base_pos = -front_len - middle_len - data_len -
Ilya Dryomovdbc0d3c2016-02-19 11:38:57 +01002442 sizeof_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07002443 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07002444 con->in_seq++;
Ilya Dryomove7a88e82016-02-17 20:04:08 +01002445 return 1;
Sage Weil31b80062009-10-06 11:31:13 -07002446 }
Alex Elder38941f82012-06-01 14:56:43 -05002447
Sage Weil4740a622012-07-30 18:19:30 -07002448 BUG_ON(!con->in_msg);
Alex Elder38941f82012-06-01 14:56:43 -05002449 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07002450 m = con->in_msg;
2451 m->front.iov_len = 0; /* haven't read it yet */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002452 if (m->middle)
2453 m->middle->vec.iov_len = 0;
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08002454
Alex Elder78625052013-03-06 23:39:39 -06002455 /* prepare for data payload, if any */
Sage Weila4107022012-07-30 16:20:25 -07002456
Alex Elder78625052013-03-06 23:39:39 -06002457 if (data_len)
Alex Elder98fa5dd2013-04-02 12:09:50 -05002458 prepare_message_data(con->in_msg, data_len);
Sage Weil31b80062009-10-06 11:31:13 -07002459 }
2460
2461 /* front */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002462 ret = read_partial_message_section(con, &m->front, front_len,
2463 &con->in_front_crc);
2464 if (ret <= 0)
2465 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002466
2467 /* middle */
Yehuda Sadeh24504182010-01-08 13:58:34 -08002468 if (m->middle) {
Sage Weil213c99e2010-08-03 10:25:11 -07002469 ret = read_partial_message_section(con, &m->middle->vec,
2470 middle_len,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002471 &con->in_middle_crc);
Sage Weil31b80062009-10-06 11:31:13 -07002472 if (ret <= 0)
2473 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002474 }
2475
2476 /* (page) data */
Alex Elder34d2d202013-03-08 20:58:59 -06002477 if (data_len) {
2478 ret = read_partial_msg_data(con);
2479 if (ret <= 0)
2480 return ret;
Sage Weil31b80062009-10-06 11:31:13 -07002481 }
2482
Sage Weil31b80062009-10-06 11:31:13 -07002483 /* footer */
Ilya Dryomov89f08172016-02-20 15:56:07 +01002484 size = sizeof_footer(con);
Alex Elderfd516532012-05-10 10:29:50 -05002485 end += size;
2486 ret = read_partial(con, end, size, &m->footer);
Alex Elder57dac9d2012-05-10 10:29:50 -05002487 if (ret <= 0)
2488 return ret;
2489
Yan, Zheng33d07332014-11-04 16:33:37 +08002490 if (!need_sign) {
2491 m->footer.flags = m->old_footer.flags;
2492 m->footer.sig = 0;
2493 }
2494
Sage Weil31b80062009-10-06 11:31:13 -07002495 dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n",
2496 m, front_len, m->footer.front_crc, middle_len,
2497 m->footer.middle_crc, data_len, m->footer.data_crc);
2498
2499 /* crc ok? */
2500 if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) {
2501 pr_err("read_partial_message %p front crc %u != exp. %u\n",
2502 m, con->in_front_crc, m->footer.front_crc);
2503 return -EBADMSG;
2504 }
2505 if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) {
2506 pr_err("read_partial_message %p middle crc %u != exp %u\n",
2507 m, con->in_middle_crc, m->footer.middle_crc);
2508 return -EBADMSG;
2509 }
Alex Elderbca064d2012-02-15 07:43:54 -06002510 if (do_datacrc &&
Sage Weil31b80062009-10-06 11:31:13 -07002511 (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 &&
2512 con->in_data_crc != le32_to_cpu(m->footer.data_crc)) {
2513 pr_err("read_partial_message %p data crc %u != exp. %u\n", m,
2514 con->in_data_crc, le32_to_cpu(m->footer.data_crc));
2515 return -EBADMSG;
2516 }
2517
Yan, Zheng33d07332014-11-04 16:33:37 +08002518 if (need_sign && con->ops->check_message_signature &&
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01002519 con->ops->check_message_signature(m)) {
Yan, Zheng33d07332014-11-04 16:33:37 +08002520 pr_err("read_partial_message %p signature check failed\n", m);
2521 return -EBADMSG;
2522 }
2523
Sage Weil31b80062009-10-06 11:31:13 -07002524 return 1; /* done! */
2525}
2526
2527/*
2528 * Process message. This happens in the worker thread. The callback should
2529 * be careful not to do anything that waits on other incoming messages or it
2530 * may deadlock.
2531 */
2532static void process_message(struct ceph_connection *con)
2533{
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01002534 struct ceph_msg *msg = con->in_msg;
Sage Weil31b80062009-10-06 11:31:13 -07002535
Alex Elder38941f82012-06-01 14:56:43 -05002536 BUG_ON(con->in_msg->con != con);
Sage Weil31b80062009-10-06 11:31:13 -07002537 con->in_msg = NULL;
2538
2539 /* if first message, set peer_name */
2540 if (con->peer_name.type == 0)
Sage Weildbad1852010-03-25 15:45:38 -07002541 con->peer_name = msg->hdr.src;
Sage Weil31b80062009-10-06 11:31:13 -07002542
Sage Weil31b80062009-10-06 11:31:13 -07002543 con->in_seq++;
Sage Weilec302642009-12-22 10:43:42 -08002544 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002545
2546 dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n",
2547 msg, le64_to_cpu(msg->hdr.seq),
Sage Weildbad1852010-03-25 15:45:38 -07002548 ENTITY_NAME(msg->hdr.src),
Sage Weil31b80062009-10-06 11:31:13 -07002549 le16_to_cpu(msg->hdr.type),
2550 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
2551 le32_to_cpu(msg->hdr.front_len),
2552 le32_to_cpu(msg->hdr.data_len),
2553 con->in_front_crc, con->in_middle_crc, con->in_data_crc);
2554 con->ops->dispatch(con, msg);
Sage Weilec302642009-12-22 10:43:42 -08002555
2556 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07002557}
2558
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002559static int read_keepalive_ack(struct ceph_connection *con)
2560{
2561 struct ceph_timespec ceph_ts;
2562 size_t size = sizeof(ceph_ts);
2563 int ret = read_partial(con, size, size, &ceph_ts);
2564 if (ret <= 0)
2565 return ret;
Arnd Bergmann473bd2d2018-07-13 22:18:34 +02002566 ceph_decode_timespec64(&con->last_keepalive_ack, &ceph_ts);
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002567 prepare_read_tag(con);
2568 return 1;
2569}
Sage Weil31b80062009-10-06 11:31:13 -07002570
2571/*
2572 * Write something to the socket. Called in a worker thread when the
2573 * socket appears to be writeable and we have something ready to send.
2574 */
2575static int try_write(struct ceph_connection *con)
2576{
Sage Weil31b80062009-10-06 11:31:13 -07002577 int ret = 1;
2578
Sage Weild59315c2012-06-21 12:49:23 -07002579 dout("try_write start %p state %lu\n", con, con->state);
Ilya Dryomov9c55ad12018-04-24 19:10:55 +02002580 if (con->state != CON_STATE_PREOPEN &&
2581 con->state != CON_STATE_CONNECTING &&
2582 con->state != CON_STATE_NEGOTIATING &&
2583 con->state != CON_STATE_OPEN)
2584 return 0;
Sage Weil31b80062009-10-06 11:31:13 -07002585
Sage Weil31b80062009-10-06 11:31:13 -07002586 /* open the socket first? */
Sage Weil8dacc7d2012-07-20 17:24:40 -07002587 if (con->state == CON_STATE_PREOPEN) {
2588 BUG_ON(con->sock);
2589 con->state = CON_STATE_CONNECTING;
Alex Eldera5988c42012-05-29 11:04:58 -05002590
Alex Eldere2200422012-05-23 14:35:23 -05002591 con_out_kvec_reset(con);
Alex Eldere825a662012-05-16 15:16:38 -05002592 prepare_write_banner(con);
Sage Weileed0ef22009-11-10 14:34:36 -08002593 prepare_read_banner(con);
Sage Weil31b80062009-10-06 11:31:13 -07002594
Sage Weilcf3e5c42009-12-11 09:48:05 -08002595 BUG_ON(con->in_msg);
Sage Weil31b80062009-10-06 11:31:13 -07002596 con->in_tag = CEPH_MSGR_TAG_READY;
2597 dout("try_write initiating connect on %p new state %lu\n",
2598 con, con->state);
Alex Elder41617d02012-02-14 14:05:33 -06002599 ret = ceph_tcp_connect(con);
2600 if (ret < 0) {
Sage Weil31b80062009-10-06 11:31:13 -07002601 con->error_msg = "connect error";
Sage Weil31b80062009-10-06 11:31:13 -07002602 goto out;
2603 }
2604 }
2605
Ilya Dryomovd2935d62018-04-25 12:17:13 +02002606more:
2607 dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes);
Ilya Dryomov9c55ad12018-04-24 19:10:55 +02002608 BUG_ON(!con->sock);
2609
Sage Weil31b80062009-10-06 11:31:13 -07002610 /* kvec data queued? */
Ilya Dryomov67645d72015-12-28 13:18:34 +03002611 if (con->out_kvec_left) {
2612 ret = write_partial_kvec(con);
Sage Weil31b80062009-10-06 11:31:13 -07002613 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08002614 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002615 }
Ilya Dryomov67645d72015-12-28 13:18:34 +03002616 if (con->out_skip) {
2617 ret = write_partial_skip(con);
Sage Weil31b80062009-10-06 11:31:13 -07002618 if (ret <= 0)
Sage Weil42961d22011-01-25 08:19:34 -08002619 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002620 }
2621
2622 /* msg pages? */
2623 if (con->out_msg) {
Sage Weilc86a2932009-12-14 14:04:30 -08002624 if (con->out_msg_done) {
2625 ceph_msg_put(con->out_msg);
2626 con->out_msg = NULL; /* we're done with this one */
2627 goto do_next;
2628 }
2629
Alex Elder34d2d202013-03-08 20:58:59 -06002630 ret = write_partial_message_data(con);
Sage Weil31b80062009-10-06 11:31:13 -07002631 if (ret == 1)
Ilya Dryomovd2935d62018-04-25 12:17:13 +02002632 goto more; /* we need to send the footer, too! */
Sage Weil31b80062009-10-06 11:31:13 -07002633 if (ret == 0)
Sage Weil42961d22011-01-25 08:19:34 -08002634 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002635 if (ret < 0) {
Alex Elder34d2d202013-03-08 20:58:59 -06002636 dout("try_write write_partial_message_data err %d\n",
Sage Weil31b80062009-10-06 11:31:13 -07002637 ret);
Sage Weil42961d22011-01-25 08:19:34 -08002638 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002639 }
2640 }
2641
Sage Weilc86a2932009-12-14 14:04:30 -08002642do_next:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002643 if (con->state == CON_STATE_OPEN) {
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002644 if (con_flag_test_and_clear(con, CON_FLAG_KEEPALIVE_PENDING)) {
2645 prepare_write_keepalive(con);
2646 goto more;
2647 }
Sage Weil31b80062009-10-06 11:31:13 -07002648 /* is anything else pending? */
2649 if (!list_empty(&con->out_queue)) {
2650 prepare_write_message(con);
2651 goto more;
2652 }
2653 if (con->in_seq > con->in_seq_acked) {
2654 prepare_write_ack(con);
2655 goto more;
2656 }
Sage Weil31b80062009-10-06 11:31:13 -07002657 }
2658
2659 /* Nothing to do! */
Alex Elderc9ffc772013-02-20 10:25:12 -06002660 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
Sage Weil31b80062009-10-06 11:31:13 -07002661 dout("try_write nothing else to write.\n");
Sage Weil31b80062009-10-06 11:31:13 -07002662 ret = 0;
2663out:
Sage Weil42961d22011-01-25 08:19:34 -08002664 dout("try_write done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002665 return ret;
2666}
2667
Sage Weil31b80062009-10-06 11:31:13 -07002668/*
2669 * Read what we can from the socket.
2670 */
2671static int try_read(struct ceph_connection *con)
2672{
Sage Weil31b80062009-10-06 11:31:13 -07002673 int ret = -1;
2674
Sage Weil31b80062009-10-06 11:31:13 -07002675more:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002676 dout("try_read start on %p state %lu\n", con, con->state);
2677 if (con->state != CON_STATE_CONNECTING &&
2678 con->state != CON_STATE_NEGOTIATING &&
2679 con->state != CON_STATE_OPEN)
2680 return 0;
2681
2682 BUG_ON(!con->sock);
2683
Sage Weil31b80062009-10-06 11:31:13 -07002684 dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag,
2685 con->in_base_pos);
Sage Weil0da5d702011-05-19 11:21:05 -07002686
Sage Weil8dacc7d2012-07-20 17:24:40 -07002687 if (con->state == CON_STATE_CONNECTING) {
Alex Elder7593af92012-05-24 11:55:03 -05002688 dout("try_read connecting\n");
2689 ret = read_partial_banner(con);
2690 if (ret <= 0)
Alex Elderab166d52012-05-31 11:37:29 -05002691 goto out;
Alex Elder7593af92012-05-24 11:55:03 -05002692 ret = process_banner(con);
2693 if (ret < 0)
2694 goto out;
2695
Sage Weil8dacc7d2012-07-20 17:24:40 -07002696 con->state = CON_STATE_NEGOTIATING;
Alex Elder7593af92012-05-24 11:55:03 -05002697
Jim Schutt6d4221b2012-08-10 10:37:38 -07002698 /*
2699 * Received banner is good, exchange connection info.
2700 * Do not reset out_kvec, as sending our banner raced
2701 * with receiving peer banner after connect completed.
2702 */
Alex Elder7593af92012-05-24 11:55:03 -05002703 ret = prepare_write_connect(con);
2704 if (ret < 0)
2705 goto out;
2706 prepare_read_connect(con);
2707
2708 /* Send connection info before awaiting response */
Sage Weil0da5d702011-05-19 11:21:05 -07002709 goto out;
2710 }
2711
Sage Weil8dacc7d2012-07-20 17:24:40 -07002712 if (con->state == CON_STATE_NEGOTIATING) {
Alex Elder7593af92012-05-24 11:55:03 -05002713 dout("try_read negotiating\n");
Sage Weil31b80062009-10-06 11:31:13 -07002714 ret = read_partial_connect(con);
2715 if (ret <= 0)
Sage Weil31b80062009-10-06 11:31:13 -07002716 goto out;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002717 ret = process_connect(con);
2718 if (ret < 0)
2719 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002720 goto more;
2721 }
2722
Alex Elder122070a2012-12-26 10:43:57 -06002723 WARN_ON(con->state != CON_STATE_OPEN);
Sage Weil8dacc7d2012-07-20 17:24:40 -07002724
Sage Weil31b80062009-10-06 11:31:13 -07002725 if (con->in_base_pos < 0) {
2726 /*
2727 * skipping + discarding content.
Sage Weil31b80062009-10-06 11:31:13 -07002728 */
Ilya Dryomove5c93882018-04-27 18:58:47 +02002729 ret = ceph_tcp_recvmsg(con->sock, NULL, -con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07002730 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002731 goto out;
Ilya Dryomove5c93882018-04-27 18:58:47 +02002732 dout("skipped %d / %d bytes\n", ret, -con->in_base_pos);
Sage Weil31b80062009-10-06 11:31:13 -07002733 con->in_base_pos += ret;
2734 if (con->in_base_pos)
2735 goto more;
2736 }
2737 if (con->in_tag == CEPH_MSGR_TAG_READY) {
2738 /*
2739 * what's next?
2740 */
2741 ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1);
2742 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002743 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002744 dout("try_read got tag %d\n", (int)con->in_tag);
2745 switch (con->in_tag) {
2746 case CEPH_MSGR_TAG_MSG:
2747 prepare_read_message(con);
2748 break;
2749 case CEPH_MSGR_TAG_ACK:
2750 prepare_read_ack(con);
2751 break;
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002752 case CEPH_MSGR_TAG_KEEPALIVE2_ACK:
2753 prepare_read_keepalive_ack(con);
2754 break;
Sage Weil31b80062009-10-06 11:31:13 -07002755 case CEPH_MSGR_TAG_CLOSE:
Sage Weil8dacc7d2012-07-20 17:24:40 -07002756 con_close_socket(con);
2757 con->state = CON_STATE_CLOSED;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002758 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002759 default:
2760 goto bad_tag;
2761 }
2762 }
2763 if (con->in_tag == CEPH_MSGR_TAG_MSG) {
2764 ret = read_partial_message(con);
2765 if (ret <= 0) {
2766 switch (ret) {
2767 case -EBADMSG:
Ilya Dryomova51983e2015-10-28 23:52:06 +01002768 con->error_msg = "bad crc/signature";
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002769 /* fall through */
2770 case -EBADE:
Sage Weil31b80062009-10-06 11:31:13 -07002771 ret = -EIO;
Sage Weil98bdb0a2011-01-25 08:17:48 -08002772 break;
Sage Weil31b80062009-10-06 11:31:13 -07002773 case -EIO:
2774 con->error_msg = "io error";
Sage Weil98bdb0a2011-01-25 08:17:48 -08002775 break;
Sage Weil31b80062009-10-06 11:31:13 -07002776 }
Sage Weil98bdb0a2011-01-25 08:17:48 -08002777 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002778 }
2779 if (con->in_tag == CEPH_MSGR_TAG_READY)
2780 goto more;
2781 process_message(con);
Sage Weil7b862e02012-07-30 18:16:56 -07002782 if (con->state == CON_STATE_OPEN)
2783 prepare_read_tag(con);
Sage Weil31b80062009-10-06 11:31:13 -07002784 goto more;
2785 }
Sage Weil3a230832013-03-25 08:47:40 -07002786 if (con->in_tag == CEPH_MSGR_TAG_ACK ||
2787 con->in_tag == CEPH_MSGR_TAG_SEQ) {
2788 /*
2789 * the final handshake seq exchange is semantically
2790 * equivalent to an ACK
2791 */
Sage Weil31b80062009-10-06 11:31:13 -07002792 ret = read_partial_ack(con);
2793 if (ret <= 0)
Sage Weil98bdb0a2011-01-25 08:17:48 -08002794 goto out;
Sage Weil31b80062009-10-06 11:31:13 -07002795 process_ack(con);
2796 goto more;
2797 }
Yan, Zheng8b9558a2015-09-01 17:19:38 +08002798 if (con->in_tag == CEPH_MSGR_TAG_KEEPALIVE2_ACK) {
2799 ret = read_keepalive_ack(con);
2800 if (ret <= 0)
2801 goto out;
2802 goto more;
2803 }
Sage Weil31b80062009-10-06 11:31:13 -07002804
Sage Weil31b80062009-10-06 11:31:13 -07002805out:
Sage Weil98bdb0a2011-01-25 08:17:48 -08002806 dout("try_read done on %p ret %d\n", con, ret);
Sage Weil31b80062009-10-06 11:31:13 -07002807 return ret;
2808
2809bad_tag:
2810 pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag);
2811 con->error_msg = "protocol error, garbage tag";
2812 ret = -1;
2813 goto out;
2814}
2815
2816
2817/*
Alex Elder802c6d92012-10-08 20:37:30 -07002818 * Atomically queue work on a connection after the specified delay.
2819 * Bump @con reference to avoid races with connection teardown.
2820 * Returns 0 if work was queued, or an error code otherwise.
Sage Weil31b80062009-10-06 11:31:13 -07002821 */
Alex Elder802c6d92012-10-08 20:37:30 -07002822static int queue_con_delay(struct ceph_connection *con, unsigned long delay)
Sage Weil31b80062009-10-06 11:31:13 -07002823{
Sage Weil31b80062009-10-06 11:31:13 -07002824 if (!con->ops->get(con)) {
Alex Elder802c6d92012-10-08 20:37:30 -07002825 dout("%s %p ref count 0\n", __func__, con);
Alex Elder802c6d92012-10-08 20:37:30 -07002826 return -ENOENT;
Sage Weil31b80062009-10-06 11:31:13 -07002827 }
2828
Alex Elder802c6d92012-10-08 20:37:30 -07002829 if (!queue_delayed_work(ceph_msgr_wq, &con->work, delay)) {
2830 dout("%s %p - already queued\n", __func__, con);
Sage Weil31b80062009-10-06 11:31:13 -07002831 con->ops->put(con);
Alex Elder802c6d92012-10-08 20:37:30 -07002832 return -EBUSY;
Sage Weil31b80062009-10-06 11:31:13 -07002833 }
Alex Elder802c6d92012-10-08 20:37:30 -07002834
2835 dout("%s %p %lu\n", __func__, con, delay);
Alex Elder802c6d92012-10-08 20:37:30 -07002836 return 0;
2837}
2838
2839static void queue_con(struct ceph_connection *con)
2840{
2841 (void) queue_con_delay(con, 0);
Sage Weil31b80062009-10-06 11:31:13 -07002842}
2843
Ilya Dryomov37ab77a2014-06-24 16:21:45 +04002844static void cancel_con(struct ceph_connection *con)
2845{
2846 if (cancel_delayed_work(&con->work)) {
2847 dout("%s %p\n", __func__, con);
2848 con->ops->put(con);
2849 }
2850}
2851
Alex Elder7bb21d682012-12-07 19:50:07 -06002852static bool con_sock_closed(struct ceph_connection *con)
2853{
Alex Elderc9ffc772013-02-20 10:25:12 -06002854 if (!con_flag_test_and_clear(con, CON_FLAG_SOCK_CLOSED))
Alex Elder7bb21d682012-12-07 19:50:07 -06002855 return false;
2856
2857#define CASE(x) \
2858 case CON_STATE_ ## x: \
2859 con->error_msg = "socket closed (con state " #x ")"; \
2860 break;
2861
2862 switch (con->state) {
2863 CASE(CLOSED);
2864 CASE(PREOPEN);
2865 CASE(CONNECTING);
2866 CASE(NEGOTIATING);
2867 CASE(OPEN);
2868 CASE(STANDBY);
2869 default:
Joe Perchesb9a67892014-09-09 21:17:29 -07002870 pr_warn("%s con %p unrecognized state %lu\n",
Alex Elder7bb21d682012-12-07 19:50:07 -06002871 __func__, con, con->state);
2872 con->error_msg = "unrecognized con state";
2873 BUG();
2874 break;
2875 }
2876#undef CASE
2877
2878 return true;
2879}
2880
Alex Elderf20a39f2013-02-19 12:25:57 -06002881static bool con_backoff(struct ceph_connection *con)
2882{
2883 int ret;
2884
2885 if (!con_flag_test_and_clear(con, CON_FLAG_BACKOFF))
2886 return false;
2887
2888 ret = queue_con_delay(con, round_jiffies_relative(con->delay));
2889 if (ret) {
2890 dout("%s: con %p FAILED to back off %lu\n", __func__,
2891 con, con->delay);
2892 BUG_ON(ret == -ENOENT);
2893 con_flag_set(con, CON_FLAG_BACKOFF);
2894 }
2895
2896 return true;
2897}
2898
Alex Elder93209262013-02-19 12:25:57 -06002899/* Finish fault handling; con->mutex must *not* be held here */
2900
2901static void con_fault_finish(struct ceph_connection *con)
2902{
Ilya Dryomovf6330cc2016-01-13 14:32:57 +01002903 dout("%s %p\n", __func__, con);
2904
Alex Elder93209262013-02-19 12:25:57 -06002905 /*
2906 * in case we faulted due to authentication, invalidate our
2907 * current tickets so that we can get new ones.
2908 */
Ilya Dryomovf6330cc2016-01-13 14:32:57 +01002909 if (con->auth_retry) {
2910 dout("auth_retry %d, invalidating\n", con->auth_retry);
2911 if (con->ops->invalidate_authorizer)
2912 con->ops->invalidate_authorizer(con);
2913 con->auth_retry = 0;
Alex Elder93209262013-02-19 12:25:57 -06002914 }
2915
2916 if (con->ops->fault)
2917 con->ops->fault(con);
2918}
2919
Sage Weil31b80062009-10-06 11:31:13 -07002920/*
2921 * Do some work on a connection. Drop a connection ref when we're done.
2922 */
Ilya Dryomov68931622015-07-03 15:44:41 +03002923static void ceph_con_workfn(struct work_struct *work)
Sage Weil31b80062009-10-06 11:31:13 -07002924{
2925 struct ceph_connection *con = container_of(work, struct ceph_connection,
2926 work.work);
Alex Elder49659412013-02-19 12:25:57 -06002927 bool fault;
Sage Weil31b80062009-10-06 11:31:13 -07002928
Sage Weil9dd46582010-04-28 13:51:50 -07002929 mutex_lock(&con->mutex);
Alex Elder49659412013-02-19 12:25:57 -06002930 while (true) {
2931 int ret;
Sage Weil31b80062009-10-06 11:31:13 -07002932
Alex Elder49659412013-02-19 12:25:57 -06002933 if ((fault = con_sock_closed(con))) {
2934 dout("%s: con %p SOCK_CLOSED\n", __func__, con);
2935 break;
2936 }
2937 if (con_backoff(con)) {
2938 dout("%s: con %p BACKOFF\n", __func__, con);
2939 break;
2940 }
2941 if (con->state == CON_STATE_STANDBY) {
2942 dout("%s: con %p STANDBY\n", __func__, con);
2943 break;
2944 }
2945 if (con->state == CON_STATE_CLOSED) {
2946 dout("%s: con %p CLOSED\n", __func__, con);
2947 BUG_ON(con->sock);
2948 break;
2949 }
2950 if (con->state == CON_STATE_PREOPEN) {
2951 dout("%s: con %p PREOPEN\n", __func__, con);
2952 BUG_ON(con->sock);
2953 }
Sage Weil0da5d702011-05-19 11:21:05 -07002954
Alex Elder49659412013-02-19 12:25:57 -06002955 ret = try_read(con);
2956 if (ret < 0) {
2957 if (ret == -EAGAIN)
2958 continue;
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002959 if (!con->error_msg)
2960 con->error_msg = "socket error on read";
Alex Elder49659412013-02-19 12:25:57 -06002961 fault = true;
2962 break;
2963 }
2964
2965 ret = try_write(con);
2966 if (ret < 0) {
2967 if (ret == -EAGAIN)
2968 continue;
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002969 if (!con->error_msg)
2970 con->error_msg = "socket error on write";
Alex Elder49659412013-02-19 12:25:57 -06002971 fault = true;
2972 }
2973
2974 break; /* If we make it to here, we're done */
Sage Weil3a140a02012-07-30 16:24:21 -07002975 }
Alex Elderb6e7b6a2013-02-19 12:25:57 -06002976 if (fault)
2977 con_fault(con);
Sage Weil9dd46582010-04-28 13:51:50 -07002978 mutex_unlock(&con->mutex);
Sage Weil0da5d702011-05-19 11:21:05 -07002979
Alex Elderb6e7b6a2013-02-19 12:25:57 -06002980 if (fault)
2981 con_fault_finish(con);
2982
2983 con->ops->put(con);
Sage Weil31b80062009-10-06 11:31:13 -07002984}
2985
Sage Weil31b80062009-10-06 11:31:13 -07002986/*
2987 * Generic error/fault handler. A retry mechanism is used with
2988 * exponential backoff
2989 */
Alex Elder93209262013-02-19 12:25:57 -06002990static void con_fault(struct ceph_connection *con)
Sage Weil31b80062009-10-06 11:31:13 -07002991{
Sage Weil31b80062009-10-06 11:31:13 -07002992 dout("fault %p state %lu to peer %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002993 con, con->state, ceph_pr_addr(&con->peer_addr.in_addr));
Sage Weil31b80062009-10-06 11:31:13 -07002994
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03002995 pr_warn("%s%lld %s %s\n", ENTITY_NAME(con->peer_name),
2996 ceph_pr_addr(&con->peer_addr.in_addr), con->error_msg);
2997 con->error_msg = NULL;
2998
Alex Elder122070a2012-12-26 10:43:57 -06002999 WARN_ON(con->state != CON_STATE_CONNECTING &&
Sage Weil8dacc7d2012-07-20 17:24:40 -07003000 con->state != CON_STATE_NEGOTIATING &&
3001 con->state != CON_STATE_OPEN);
Sage Weilec302642009-12-22 10:43:42 -08003002
Sage Weil31b80062009-10-06 11:31:13 -07003003 con_close_socket(con);
Sage Weil5e095e82009-12-14 14:30:34 -08003004
Alex Elderc9ffc772013-02-20 10:25:12 -06003005 if (con_flag_test(con, CON_FLAG_LOSSYTX)) {
Sage Weil8dacc7d2012-07-20 17:24:40 -07003006 dout("fault on LOSSYTX channel, marking CLOSED\n");
3007 con->state = CON_STATE_CLOSED;
Alex Elder93209262013-02-19 12:25:57 -06003008 return;
Sage Weil3b5ede02012-07-20 15:22:53 -07003009 }
3010
Sage Weil5e095e82009-12-14 14:30:34 -08003011 if (con->in_msg) {
Alex Elder38941f82012-06-01 14:56:43 -05003012 BUG_ON(con->in_msg->con != con);
Sage Weil5e095e82009-12-14 14:30:34 -08003013 ceph_msg_put(con->in_msg);
3014 con->in_msg = NULL;
3015 }
Sage Weil31b80062009-10-06 11:31:13 -07003016
Sage Weile80a52d2010-02-25 12:40:45 -08003017 /* Requeue anything that hasn't been acked */
3018 list_splice_init(&con->out_sent, &con->out_queue);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003019
Sage Weile76661d2011-03-03 10:10:15 -08003020 /* If there are no messages queued or keepalive pending, place
3021 * the connection in a STANDBY state */
3022 if (list_empty(&con->out_queue) &&
Alex Elderc9ffc772013-02-20 10:25:12 -06003023 !con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING)) {
Sage Weile00de342011-03-04 12:25:05 -08003024 dout("fault %p setting STANDBY clearing WRITE_PENDING\n", con);
Alex Elderc9ffc772013-02-20 10:25:12 -06003025 con_flag_clear(con, CON_FLAG_WRITE_PENDING);
Sage Weil8dacc7d2012-07-20 17:24:40 -07003026 con->state = CON_STATE_STANDBY;
Sage Weile80a52d2010-02-25 12:40:45 -08003027 } else {
3028 /* retry after a delay. */
Sage Weil8dacc7d2012-07-20 17:24:40 -07003029 con->state = CON_STATE_PREOPEN;
Sage Weile80a52d2010-02-25 12:40:45 -08003030 if (con->delay == 0)
3031 con->delay = BASE_DELAY_INTERVAL;
3032 else if (con->delay < MAX_DELAY_INTERVAL)
3033 con->delay *= 2;
Alex Elderc9ffc772013-02-20 10:25:12 -06003034 con_flag_set(con, CON_FLAG_BACKOFF);
Alex Elder8618e302012-10-08 20:37:30 -07003035 queue_con(con);
Sage Weil31b80062009-10-06 11:31:13 -07003036 }
Sage Weil31b80062009-10-06 11:31:13 -07003037}
3038
3039
3040
3041/*
Alex Elder15d98822012-05-26 23:26:43 -05003042 * initialize a new messenger instance
Sage Weil31b80062009-10-06 11:31:13 -07003043 */
Alex Elder15d98822012-05-26 23:26:43 -05003044void ceph_messenger_init(struct ceph_messenger *msgr,
Ilya Dryomov859bff52015-10-28 23:50:58 +01003045 struct ceph_entity_addr *myaddr)
Sage Weil31b80062009-10-06 11:31:13 -07003046{
Sage Weil31b80062009-10-06 11:31:13 -07003047 spin_lock_init(&msgr->global_seq_lock);
3048
Sage Weil31b80062009-10-06 11:31:13 -07003049 if (myaddr)
3050 msgr->inst.addr = *myaddr;
3051
3052 /* select a random nonce */
Sage Weilac8839d2010-01-27 14:28:10 -08003053 msgr->inst.addr.type = 0;
Sage Weil103e2d32010-01-07 16:12:36 -08003054 get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce));
Sage Weil63f2d212009-11-03 15:17:56 -08003055 encode_my_addr(msgr);
Sage Weil31b80062009-10-06 11:31:13 -07003056
Guanjun Hea2a32582012-07-08 19:50:33 -07003057 atomic_set(&msgr->stopping, 0);
Ilya Dryomov757856d2015-06-25 17:47:45 +03003058 write_pnet(&msgr->net, get_net(current->nsproxy->net_ns));
Sage Weil31b80062009-10-06 11:31:13 -07003059
Alex Elder15d98822012-05-26 23:26:43 -05003060 dout("%s %p\n", __func__, msgr);
Sage Weil31b80062009-10-06 11:31:13 -07003061}
Alex Elder15d98822012-05-26 23:26:43 -05003062EXPORT_SYMBOL(ceph_messenger_init);
Sage Weil31b80062009-10-06 11:31:13 -07003063
Ilya Dryomov757856d2015-06-25 17:47:45 +03003064void ceph_messenger_fini(struct ceph_messenger *msgr)
3065{
3066 put_net(read_pnet(&msgr->net));
3067}
3068EXPORT_SYMBOL(ceph_messenger_fini);
3069
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003070static void msg_con_set(struct ceph_msg *msg, struct ceph_connection *con)
3071{
3072 if (msg->con)
3073 msg->con->ops->put(msg->con);
3074
3075 msg->con = con ? con->ops->get(con) : NULL;
3076 BUG_ON(msg->con != con);
3077}
3078
Sage Weile00de342011-03-04 12:25:05 -08003079static void clear_standby(struct ceph_connection *con)
3080{
3081 /* come back from STANDBY? */
Sage Weil8dacc7d2012-07-20 17:24:40 -07003082 if (con->state == CON_STATE_STANDBY) {
Sage Weile00de342011-03-04 12:25:05 -08003083 dout("clear_standby %p and ++connect_seq\n", con);
Sage Weil8dacc7d2012-07-20 17:24:40 -07003084 con->state = CON_STATE_PREOPEN;
Sage Weile00de342011-03-04 12:25:05 -08003085 con->connect_seq++;
Alex Elderc9ffc772013-02-20 10:25:12 -06003086 WARN_ON(con_flag_test(con, CON_FLAG_WRITE_PENDING));
3087 WARN_ON(con_flag_test(con, CON_FLAG_KEEPALIVE_PENDING));
Sage Weile00de342011-03-04 12:25:05 -08003088 }
3089}
3090
Sage Weil31b80062009-10-06 11:31:13 -07003091/*
3092 * Queue up an outgoing message on the given connection.
3093 */
3094void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg)
3095{
Sage Weila59b55a2012-07-20 15:34:04 -07003096 /* set src+dst */
3097 msg->hdr.src = con->msgr->inst.name;
3098 BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len));
3099 msg->needs_out_seq = true;
3100
3101 mutex_lock(&con->mutex);
3102
Sage Weil8dacc7d2012-07-20 17:24:40 -07003103 if (con->state == CON_STATE_CLOSED) {
Sage Weil31b80062009-10-06 11:31:13 -07003104 dout("con_send %p closed, dropping %p\n", con, msg);
3105 ceph_msg_put(msg);
Sage Weila59b55a2012-07-20 15:34:04 -07003106 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003107 return;
3108 }
3109
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003110 msg_con_set(msg, con);
Sage Weil31b80062009-10-06 11:31:13 -07003111
Sage Weil31b80062009-10-06 11:31:13 -07003112 BUG_ON(!list_empty(&msg->list_head));
3113 list_add_tail(&msg->list_head, &con->out_queue);
3114 dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg,
3115 ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type),
3116 ceph_msg_type_name(le16_to_cpu(msg->hdr.type)),
3117 le32_to_cpu(msg->hdr.front_len),
3118 le32_to_cpu(msg->hdr.middle_len),
3119 le32_to_cpu(msg->hdr.data_len));
Sage Weil00650932012-07-20 15:33:04 -07003120
3121 clear_standby(con);
Sage Weilec302642009-12-22 10:43:42 -08003122 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003123
3124 /* if there wasn't anything waiting to send before, queue
3125 * new work */
Alex Elderc9ffc772013-02-20 10:25:12 -06003126 if (con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07003127 queue_con(con);
3128}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003129EXPORT_SYMBOL(ceph_con_send);
Sage Weil31b80062009-10-06 11:31:13 -07003130
3131/*
3132 * Revoke a message that was previously queued for send
3133 */
Alex Elder6740a842012-06-01 14:56:43 -05003134void ceph_msg_revoke(struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07003135{
Alex Elder6740a842012-06-01 14:56:43 -05003136 struct ceph_connection *con = msg->con;
3137
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003138 if (!con) {
3139 dout("%s msg %p null con\n", __func__, msg);
Alex Elder6740a842012-06-01 14:56:43 -05003140 return; /* Message not in our possession */
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003141 }
Alex Elder6740a842012-06-01 14:56:43 -05003142
Sage Weilec302642009-12-22 10:43:42 -08003143 mutex_lock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003144 if (!list_empty(&msg->list_head)) {
Alex Elder38941f82012-06-01 14:56:43 -05003145 dout("%s %p msg %p - was on queue\n", __func__, con, msg);
Sage Weil31b80062009-10-06 11:31:13 -07003146 list_del_init(&msg->list_head);
Sage Weil31b80062009-10-06 11:31:13 -07003147 msg->hdr.seq = 0;
Alex Elder38941f82012-06-01 14:56:43 -05003148
Sage Weil31b80062009-10-06 11:31:13 -07003149 ceph_msg_put(msg);
Sage Weiled98ada2010-07-05 12:15:14 -07003150 }
3151 if (con->out_msg == msg) {
Ilya Dryomov67645d72015-12-28 13:18:34 +03003152 BUG_ON(con->out_skip);
3153 /* footer */
3154 if (con->out_msg_done) {
3155 con->out_skip += con_out_kvec_skip(con);
3156 } else {
3157 BUG_ON(!msg->data_length);
Ilya Dryomov89f08172016-02-20 15:56:07 +01003158 con->out_skip += sizeof_footer(con);
Sage Weil31b80062009-10-06 11:31:13 -07003159 }
Ilya Dryomov67645d72015-12-28 13:18:34 +03003160 /* data, middle, front */
3161 if (msg->data_length)
3162 con->out_skip += msg->cursor.total_resid;
3163 if (msg->middle)
3164 con->out_skip += con_out_kvec_skip(con);
3165 con->out_skip += con_out_kvec_skip(con);
Alex Elder92ce0342012-06-04 14:43:33 -05003166
Ilya Dryomov67645d72015-12-28 13:18:34 +03003167 dout("%s %p msg %p - was sending, will write %d skip %d\n",
3168 __func__, con, msg, con->out_kvec_bytes, con->out_skip);
3169 msg->hdr.seq = 0;
3170 con->out_msg = NULL;
Alex Elder92ce0342012-06-04 14:43:33 -05003171 ceph_msg_put(msg);
Sage Weil31b80062009-10-06 11:31:13 -07003172 }
Ilya Dryomov67645d72015-12-28 13:18:34 +03003173
Sage Weilec302642009-12-22 10:43:42 -08003174 mutex_unlock(&con->mutex);
Sage Weil31b80062009-10-06 11:31:13 -07003175}
3176
3177/*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08003178 * Revoke a message that we may be reading data into
Sage Weil350b1c32009-12-22 10:45:45 -08003179 */
Alex Elder8921d112012-06-01 14:56:43 -05003180void ceph_msg_revoke_incoming(struct ceph_msg *msg)
Sage Weil350b1c32009-12-22 10:45:45 -08003181{
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003182 struct ceph_connection *con = msg->con;
Alex Elder8921d112012-06-01 14:56:43 -05003183
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003184 if (!con) {
Alex Elder8921d112012-06-01 14:56:43 -05003185 dout("%s msg %p null con\n", __func__, msg);
Alex Elder8921d112012-06-01 14:56:43 -05003186 return; /* Message not in our possession */
3187 }
3188
Sage Weil350b1c32009-12-22 10:45:45 -08003189 mutex_lock(&con->mutex);
Alex Elder8921d112012-06-01 14:56:43 -05003190 if (con->in_msg == msg) {
Eric Dumazet95c96172012-04-15 05:58:06 +00003191 unsigned int front_len = le32_to_cpu(con->in_hdr.front_len);
3192 unsigned int middle_len = le32_to_cpu(con->in_hdr.middle_len);
3193 unsigned int data_len = le32_to_cpu(con->in_hdr.data_len);
Sage Weil350b1c32009-12-22 10:45:45 -08003194
3195 /* skip rest of message */
Alex Elder8921d112012-06-01 14:56:43 -05003196 dout("%s %p msg %p revoked\n", __func__, con, msg);
3197 con->in_base_pos = con->in_base_pos -
Sage Weil350b1c32009-12-22 10:45:45 -08003198 sizeof(struct ceph_msg_header) -
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08003199 front_len -
3200 middle_len -
3201 data_len -
Sage Weil350b1c32009-12-22 10:45:45 -08003202 sizeof(struct ceph_msg_footer);
Sage Weil350b1c32009-12-22 10:45:45 -08003203 ceph_msg_put(con->in_msg);
3204 con->in_msg = NULL;
3205 con->in_tag = CEPH_MSGR_TAG_READY;
Sage Weil684be252010-04-21 20:45:59 -07003206 con->in_seq++;
Sage Weil350b1c32009-12-22 10:45:45 -08003207 } else {
Alex Elder8921d112012-06-01 14:56:43 -05003208 dout("%s %p in_msg %p msg %p no-op\n",
3209 __func__, con, con->in_msg, msg);
Sage Weil350b1c32009-12-22 10:45:45 -08003210 }
3211 mutex_unlock(&con->mutex);
3212}
3213
3214/*
Sage Weil31b80062009-10-06 11:31:13 -07003215 * Queue a keepalive byte to ensure the tcp connection is alive.
3216 */
3217void ceph_con_keepalive(struct ceph_connection *con)
3218{
Sage Weile00de342011-03-04 12:25:05 -08003219 dout("con_keepalive %p\n", con);
Sage Weil00650932012-07-20 15:33:04 -07003220 mutex_lock(&con->mutex);
Sage Weile00de342011-03-04 12:25:05 -08003221 clear_standby(con);
Sage Weil00650932012-07-20 15:33:04 -07003222 mutex_unlock(&con->mutex);
Alex Elderc9ffc772013-02-20 10:25:12 -06003223 if (con_flag_test_and_set(con, CON_FLAG_KEEPALIVE_PENDING) == 0 &&
3224 con_flag_test_and_set(con, CON_FLAG_WRITE_PENDING) == 0)
Sage Weil31b80062009-10-06 11:31:13 -07003225 queue_con(con);
3226}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003227EXPORT_SYMBOL(ceph_con_keepalive);
Sage Weil31b80062009-10-06 11:31:13 -07003228
Yan, Zheng8b9558a2015-09-01 17:19:38 +08003229bool ceph_con_keepalive_expired(struct ceph_connection *con,
3230 unsigned long interval)
3231{
3232 if (interval > 0 &&
3233 (con->peer_features & CEPH_FEATURE_MSGR_KEEPALIVE2)) {
Arnd Bergmann473bd2d2018-07-13 22:18:34 +02003234 struct timespec64 now;
3235 struct timespec64 ts;
3236 ktime_get_real_ts64(&now);
3237 jiffies_to_timespec64(interval, &ts);
3238 ts = timespec64_add(con->last_keepalive_ack, ts);
3239 return timespec64_compare(&now, &ts) >= 0;
Yan, Zheng8b9558a2015-09-01 17:19:38 +08003240 }
3241 return false;
3242}
3243
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003244static struct ceph_msg_data *ceph_msg_data_add(struct ceph_msg *msg)
Alex Elder43794502013-03-01 18:00:16 -06003245{
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003246 BUG_ON(msg->num_data_items >= msg->max_data_items);
3247 return &msg->data[msg->num_data_items++];
Alex Elder6644ed72013-03-11 23:34:24 -05003248}
3249
3250static void ceph_msg_data_destroy(struct ceph_msg_data *data)
3251{
Yan, Zhenge4339d282014-09-16 17:50:45 +08003252 if (data->type == CEPH_MSG_DATA_PAGELIST)
Alex Elder6644ed72013-03-11 23:34:24 -05003253 ceph_pagelist_release(data->pagelist);
Alex Elder43794502013-03-01 18:00:16 -06003254}
3255
Alex Elder90af3602013-04-05 14:46:01 -05003256void ceph_msg_data_add_pages(struct ceph_msg *msg, struct page **pages,
Alex Elderf1baeb22013-03-07 15:38:26 -06003257 size_t length, size_t alignment)
Alex Elder02afca62013-02-14 12:16:43 -06003258{
Alex Elder6644ed72013-03-11 23:34:24 -05003259 struct ceph_msg_data *data;
3260
Alex Elder07aa1552013-03-04 18:29:06 -06003261 BUG_ON(!pages);
3262 BUG_ON(!length);
Alex Elder02afca62013-02-14 12:16:43 -06003263
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003264 data = ceph_msg_data_add(msg);
3265 data->type = CEPH_MSG_DATA_PAGES;
Alex Elder6644ed72013-03-11 23:34:24 -05003266 data->pages = pages;
3267 data->length = length;
3268 data->alignment = alignment & ~PAGE_MASK;
3269
Alex Elder5240d9f2013-03-14 14:09:06 -05003270 msg->data_length += length;
Alex Elder02afca62013-02-14 12:16:43 -06003271}
Alex Elder90af3602013-04-05 14:46:01 -05003272EXPORT_SYMBOL(ceph_msg_data_add_pages);
Sage Weil31b80062009-10-06 11:31:13 -07003273
Alex Elder90af3602013-04-05 14:46:01 -05003274void ceph_msg_data_add_pagelist(struct ceph_msg *msg,
Alex Elder27fa8382013-02-14 12:16:43 -06003275 struct ceph_pagelist *pagelist)
3276{
Alex Elder6644ed72013-03-11 23:34:24 -05003277 struct ceph_msg_data *data;
3278
Alex Elder07aa1552013-03-04 18:29:06 -06003279 BUG_ON(!pagelist);
3280 BUG_ON(!pagelist->length);
Alex Elder27fa8382013-02-14 12:16:43 -06003281
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003282 data = ceph_msg_data_add(msg);
3283 data->type = CEPH_MSG_DATA_PAGELIST;
Ilya Dryomov894868332018-09-28 16:02:53 +02003284 refcount_inc(&pagelist->refcnt);
Alex Elder6644ed72013-03-11 23:34:24 -05003285 data->pagelist = pagelist;
3286
Alex Elder5240d9f2013-03-14 14:09:06 -05003287 msg->data_length += pagelist->length;
Alex Elder27fa8382013-02-14 12:16:43 -06003288}
Alex Elder90af3602013-04-05 14:46:01 -05003289EXPORT_SYMBOL(ceph_msg_data_add_pagelist);
Alex Elder27fa8382013-02-14 12:16:43 -06003290
Alex Elderea965712013-04-05 14:46:01 -05003291#ifdef CONFIG_BLOCK
Ilya Dryomov5359a172018-01-20 10:30:10 +01003292void ceph_msg_data_add_bio(struct ceph_msg *msg, struct ceph_bio_iter *bio_pos,
3293 u32 length)
Alex Elder27fa8382013-02-14 12:16:43 -06003294{
Alex Elder6644ed72013-03-11 23:34:24 -05003295 struct ceph_msg_data *data;
Alex Elder27fa8382013-02-14 12:16:43 -06003296
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003297 data = ceph_msg_data_add(msg);
3298 data->type = CEPH_MSG_DATA_BIO;
Ilya Dryomov5359a172018-01-20 10:30:10 +01003299 data->bio_pos = *bio_pos;
Alex Elderc851c492013-04-05 14:46:01 -05003300 data->bio_length = length;
Alex Elder6644ed72013-03-11 23:34:24 -05003301
Alex Elder5240d9f2013-03-14 14:09:06 -05003302 msg->data_length += length;
Alex Elder27fa8382013-02-14 12:16:43 -06003303}
Alex Elder90af3602013-04-05 14:46:01 -05003304EXPORT_SYMBOL(ceph_msg_data_add_bio);
Alex Elderea965712013-04-05 14:46:01 -05003305#endif /* CONFIG_BLOCK */
Alex Elder27fa8382013-02-14 12:16:43 -06003306
Ilya Dryomovb9e281c2018-01-20 10:30:11 +01003307void ceph_msg_data_add_bvecs(struct ceph_msg *msg,
3308 struct ceph_bvec_iter *bvec_pos)
3309{
3310 struct ceph_msg_data *data;
3311
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003312 data = ceph_msg_data_add(msg);
3313 data->type = CEPH_MSG_DATA_BVECS;
Ilya Dryomovb9e281c2018-01-20 10:30:11 +01003314 data->bvec_pos = *bvec_pos;
3315
Ilya Dryomovb9e281c2018-01-20 10:30:11 +01003316 msg->data_length += bvec_pos->iter.bi_size;
3317}
3318EXPORT_SYMBOL(ceph_msg_data_add_bvecs);
3319
Sage Weil31b80062009-10-06 11:31:13 -07003320/*
3321 * construct a new message with given type, size
3322 * the new msg has a ref count of 1.
3323 */
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003324struct ceph_msg *ceph_msg_new2(int type, int front_len, int max_data_items,
3325 gfp_t flags, bool can_fail)
Sage Weil31b80062009-10-06 11:31:13 -07003326{
3327 struct ceph_msg *m;
3328
Alex Eldere3d5d632013-05-01 12:43:04 -05003329 m = kmem_cache_zalloc(ceph_msg_cache, flags);
Sage Weil31b80062009-10-06 11:31:13 -07003330 if (m == NULL)
3331 goto out;
Alex Elder38941f82012-06-01 14:56:43 -05003332
Sage Weil31b80062009-10-06 11:31:13 -07003333 m->hdr.type = cpu_to_le16(type);
Sage Weil45c6ceb2010-05-11 15:01:51 -07003334 m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT);
Sage Weil31b80062009-10-06 11:31:13 -07003335 m->hdr.front_len = cpu_to_le32(front_len);
Sage Weil31b80062009-10-06 11:31:13 -07003336
Alex Elder9516e452013-03-01 18:00:16 -06003337 INIT_LIST_HEAD(&m->list_head);
3338 kref_init(&m->kref);
Henry C Changca208922011-05-03 02:29:56 +00003339
Sage Weil31b80062009-10-06 11:31:13 -07003340 /* front */
3341 if (front_len) {
Ilya Dryomoveeb0bed2014-01-09 20:08:21 +02003342 m->front.iov_base = ceph_kvmalloc(front_len, flags);
Sage Weil31b80062009-10-06 11:31:13 -07003343 if (m->front.iov_base == NULL) {
Sage Weilb61c2762011-08-09 15:03:46 -07003344 dout("ceph_msg_new can't allocate %d bytes\n",
Sage Weil31b80062009-10-06 11:31:13 -07003345 front_len);
3346 goto out2;
3347 }
3348 } else {
3349 m->front.iov_base = NULL;
3350 }
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02003351 m->front_alloc_len = m->front.iov_len = front_len;
Sage Weil31b80062009-10-06 11:31:13 -07003352
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003353 if (max_data_items) {
3354 m->data = kmalloc_array(max_data_items, sizeof(*m->data),
3355 flags);
3356 if (!m->data)
3357 goto out2;
3358
3359 m->max_data_items = max_data_items;
3360 }
3361
Sage Weilbb257662010-04-01 16:07:23 -07003362 dout("ceph_msg_new %p front %d\n", m, front_len);
Sage Weil31b80062009-10-06 11:31:13 -07003363 return m;
3364
3365out2:
3366 ceph_msg_put(m);
3367out:
Sage Weilb61c2762011-08-09 15:03:46 -07003368 if (!can_fail) {
3369 pr_err("msg_new can't create type %d front %d\n", type,
3370 front_len);
Sage Weilf0ed1b72011-08-09 15:05:07 -07003371 WARN_ON(1);
Sage Weilb61c2762011-08-09 15:03:46 -07003372 } else {
3373 dout("msg_new can't create type %d front %d\n", type,
3374 front_len);
3375 }
Sage Weila79832f2010-04-01 16:06:19 -07003376 return NULL;
Sage Weil31b80062009-10-06 11:31:13 -07003377}
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003378EXPORT_SYMBOL(ceph_msg_new2);
3379
3380struct ceph_msg *ceph_msg_new(int type, int front_len, gfp_t flags,
3381 bool can_fail)
3382{
3383 return ceph_msg_new2(type, front_len, 0, flags, can_fail);
3384}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003385EXPORT_SYMBOL(ceph_msg_new);
Sage Weil31b80062009-10-06 11:31:13 -07003386
3387/*
Sage Weil31b80062009-10-06 11:31:13 -07003388 * Allocate "middle" portion of a message, if it is needed and wasn't
3389 * allocated by alloc_msg. This allows us to read a small fixed-size
3390 * per-type header in the front and then gracefully fail (i.e.,
3391 * propagate the error to the caller based on info in the front) when
3392 * the middle is too large.
3393 */
Yehuda Sadeh24504182010-01-08 13:58:34 -08003394static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg)
Sage Weil31b80062009-10-06 11:31:13 -07003395{
3396 int type = le16_to_cpu(msg->hdr.type);
3397 int middle_len = le32_to_cpu(msg->hdr.middle_len);
3398
3399 dout("alloc_middle %p type %d %s middle_len %d\n", msg, type,
3400 ceph_msg_type_name(type), middle_len);
3401 BUG_ON(!middle_len);
3402 BUG_ON(msg->middle);
3403
Sage Weilb6c1d5b2009-12-07 12:17:17 -08003404 msg->middle = ceph_buffer_new(middle_len, GFP_NOFS);
Sage Weil31b80062009-10-06 11:31:13 -07003405 if (!msg->middle)
3406 return -ENOMEM;
3407 return 0;
3408}
3409
Yehuda Sadeh24504182010-01-08 13:58:34 -08003410/*
Alex Elder1c20f2d2012-06-04 14:43:32 -05003411 * Allocate a message for receiving an incoming message on a
3412 * connection, and save the result in con->in_msg. Uses the
3413 * connection's private alloc_msg op if available.
3414 *
Sage Weil4740a622012-07-30 18:19:30 -07003415 * Returns 0 on success, or a negative error code.
3416 *
3417 * On success, if we set *skip = 1:
3418 * - the next message should be skipped and ignored.
3419 * - con->in_msg == NULL
3420 * or if we set *skip = 0:
3421 * - con->in_msg is non-null.
3422 * On error (ENOMEM, EAGAIN, ...),
3423 * - con->in_msg == NULL
Yehuda Sadeh24504182010-01-08 13:58:34 -08003424 */
Sage Weil4740a622012-07-30 18:19:30 -07003425static int ceph_con_in_msg_alloc(struct ceph_connection *con, int *skip)
Yehuda Sadeh24504182010-01-08 13:58:34 -08003426{
Sage Weil4740a622012-07-30 18:19:30 -07003427 struct ceph_msg_header *hdr = &con->in_hdr;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003428 int middle_len = le32_to_cpu(hdr->middle_len);
Alex Elder1d866d12013-03-01 18:00:14 -06003429 struct ceph_msg *msg;
Sage Weil4740a622012-07-30 18:19:30 -07003430 int ret = 0;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003431
Alex Elder1c20f2d2012-06-04 14:43:32 -05003432 BUG_ON(con->in_msg != NULL);
Alex Elder53ded492013-03-01 18:00:14 -06003433 BUG_ON(!con->ops->alloc_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08003434
Alex Elder53ded492013-03-01 18:00:14 -06003435 mutex_unlock(&con->mutex);
3436 msg = con->ops->alloc_msg(con, hdr, skip);
3437 mutex_lock(&con->mutex);
3438 if (con->state != CON_STATE_OPEN) {
3439 if (msg)
Alex Elder1d866d12013-03-01 18:00:14 -06003440 ceph_msg_put(msg);
Alex Elder53ded492013-03-01 18:00:14 -06003441 return -EAGAIN;
3442 }
Alex Elder41375772013-03-05 09:25:10 -06003443 if (msg) {
3444 BUG_ON(*skip);
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003445 msg_con_set(msg, con);
Alex Elder41375772013-03-05 09:25:10 -06003446 con->in_msg = msg;
Alex Elder41375772013-03-05 09:25:10 -06003447 } else {
3448 /*
3449 * Null message pointer means either we should skip
3450 * this message or we couldn't allocate memory. The
3451 * former is not an error.
3452 */
3453 if (*skip)
3454 return 0;
Alex Elder41375772013-03-05 09:25:10 -06003455
Ilya Dryomov67c64eb2015-03-23 14:52:40 +03003456 con->error_msg = "error allocating memory for incoming message";
Alex Elder53ded492013-03-01 18:00:14 -06003457 return -ENOMEM;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003458 }
Alex Elder1c20f2d2012-06-04 14:43:32 -05003459 memcpy(&con->in_msg->hdr, &con->in_hdr, sizeof(con->in_hdr));
Yehuda Sadeh24504182010-01-08 13:58:34 -08003460
Alex Elder1c20f2d2012-06-04 14:43:32 -05003461 if (middle_len && !con->in_msg->middle) {
3462 ret = ceph_alloc_middle(con, con->in_msg);
Yehuda Sadeh24504182010-01-08 13:58:34 -08003463 if (ret < 0) {
Alex Elder1c20f2d2012-06-04 14:43:32 -05003464 ceph_msg_put(con->in_msg);
3465 con->in_msg = NULL;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003466 }
3467 }
Yehuda Sadeh9d7f0f12010-01-11 10:32:02 -08003468
Sage Weil4740a622012-07-30 18:19:30 -07003469 return ret;
Yehuda Sadeh24504182010-01-08 13:58:34 -08003470}
3471
Sage Weil31b80062009-10-06 11:31:13 -07003472
3473/*
3474 * Free a generically kmalloc'd message.
3475 */
Ilya Dryomov0215e442014-06-20 14:14:41 +04003476static void ceph_msg_free(struct ceph_msg *m)
Sage Weil31b80062009-10-06 11:31:13 -07003477{
Ilya Dryomov0215e442014-06-20 14:14:41 +04003478 dout("%s %p\n", __func__, m);
Ilya Dryomov4965fc32014-10-23 16:32:57 +04003479 kvfree(m->front.iov_base);
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003480 kfree(m->data);
Alex Eldere3d5d632013-05-01 12:43:04 -05003481 kmem_cache_free(ceph_msg_cache, m);
Sage Weil31b80062009-10-06 11:31:13 -07003482}
3483
Ilya Dryomov0215e442014-06-20 14:14:41 +04003484static void ceph_msg_release(struct kref *kref)
Sage Weil31b80062009-10-06 11:31:13 -07003485{
Sage Weilc2e552e2009-12-07 15:55:05 -08003486 struct ceph_msg *m = container_of(kref, struct ceph_msg, kref);
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003487 int i;
Sage Weil31b80062009-10-06 11:31:13 -07003488
Ilya Dryomov0215e442014-06-20 14:14:41 +04003489 dout("%s %p\n", __func__, m);
Sage Weilc2e552e2009-12-07 15:55:05 -08003490 WARN_ON(!list_empty(&m->list_head));
Sage Weil31b80062009-10-06 11:31:13 -07003491
Ilya Dryomov583d0fe2015-11-02 17:13:58 +01003492 msg_con_set(m, NULL);
3493
Sage Weilc2e552e2009-12-07 15:55:05 -08003494 /* drop middle, data, if any */
3495 if (m->middle) {
3496 ceph_buffer_put(m->middle);
3497 m->middle = NULL;
Sage Weil31b80062009-10-06 11:31:13 -07003498 }
Alex Elder5240d9f2013-03-14 14:09:06 -05003499
Ilya Dryomov0d9c1ab2018-10-15 17:38:23 +02003500 for (i = 0; i < m->num_data_items; i++)
3501 ceph_msg_data_destroy(&m->data[i]);
Sage Weil58bb3b32009-12-23 12:12:31 -08003502
Sage Weilc2e552e2009-12-07 15:55:05 -08003503 if (m->pool)
3504 ceph_msgpool_put(m->pool, m);
3505 else
Ilya Dryomov0215e442014-06-20 14:14:41 +04003506 ceph_msg_free(m);
Sage Weil31b80062009-10-06 11:31:13 -07003507}
Ilya Dryomov0215e442014-06-20 14:14:41 +04003508
3509struct ceph_msg *ceph_msg_get(struct ceph_msg *msg)
3510{
3511 dout("%s %p (was %d)\n", __func__, msg,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003512 kref_read(&msg->kref));
Ilya Dryomov0215e442014-06-20 14:14:41 +04003513 kref_get(&msg->kref);
3514 return msg;
3515}
3516EXPORT_SYMBOL(ceph_msg_get);
3517
3518void ceph_msg_put(struct ceph_msg *msg)
3519{
3520 dout("%s %p (was %d)\n", __func__, msg,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003521 kref_read(&msg->kref));
Ilya Dryomov0215e442014-06-20 14:14:41 +04003522 kref_put(&msg->kref, ceph_msg_release);
3523}
3524EXPORT_SYMBOL(ceph_msg_put);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003525
3526void ceph_msg_dump(struct ceph_msg *msg)
3527{
Ilya Dryomov3cea4c32014-01-09 20:08:21 +02003528 pr_debug("msg_dump %p (front_alloc_len %d length %zd)\n", msg,
3529 msg->front_alloc_len, msg->data_length);
Sage Weil9ec7cab2009-12-14 15:13:47 -08003530 print_hex_dump(KERN_DEBUG, "header: ",
3531 DUMP_PREFIX_OFFSET, 16, 1,
3532 &msg->hdr, sizeof(msg->hdr), true);
3533 print_hex_dump(KERN_DEBUG, " front: ",
3534 DUMP_PREFIX_OFFSET, 16, 1,
3535 msg->front.iov_base, msg->front.iov_len, true);
3536 if (msg->middle)
3537 print_hex_dump(KERN_DEBUG, "middle: ",
3538 DUMP_PREFIX_OFFSET, 16, 1,
3539 msg->middle->vec.iov_base,
3540 msg->middle->vec.iov_len, true);
3541 print_hex_dump(KERN_DEBUG, "footer: ",
3542 DUMP_PREFIX_OFFSET, 16, 1,
3543 &msg->footer, sizeof(msg->footer), true);
3544}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003545EXPORT_SYMBOL(ceph_msg_dump);