PageRenderTime 65ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/ocfs2/cluster/tcp.c

https://gitlab.com/LiquidSmooth-Devices/android_kernel_htc_msm8974
C | 2000 lines | 1578 code | 366 blank | 56 comment | 247 complexity | 8767e268e6546a2e966de40d0508f0a5 MD5 | raw file
Possible License(s): GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. *
  3. * vim: noexpandtab sw=8 ts=8 sts=0:
  4. *
  5. * Copyright (C) 2004 Oracle. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with this program; if not, write to the
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. * Boston, MA 021110-1307, USA.
  21. *
  22. * ----
  23. *
  24. * Callers for this were originally written against a very simple synchronus
  25. * API. This implementation reflects those simple callers. Some day I'm sure
  26. * we'll need to move to a more robust posting/callback mechanism.
  27. *
  28. * Transmit calls pass in kernel virtual addresses and block copying this into
  29. * the socket's tx buffers via a usual blocking sendmsg. They'll block waiting
  30. * for a failed socket to timeout. TX callers can also pass in a poniter to an
  31. * 'int' which gets filled with an errno off the wire in response to the
  32. * message they send.
  33. *
  34. * Handlers for unsolicited messages are registered. Each socket has a page
  35. * that incoming data is copied into. First the header, then the data.
  36. * Handlers are called from only one thread with a reference to this per-socket
  37. * page. This page is destroyed after the handler call, so it can't be
  38. * referenced beyond the call. Handlers may block but are discouraged from
  39. * doing so.
  40. *
  41. * Any framing errors (bad magic, large payload lengths) close a connection.
  42. *
  43. * Our sock_container holds the state we associate with a socket. It's current
  44. * framing state is held there as well as the refcounting we do around when it
  45. * is safe to tear down the socket. The socket is only finally torn down from
  46. * the container when the container loses all of its references -- so as long
  47. * as you hold a ref on the container you can trust that the socket is valid
  48. * for use with kernel socket APIs.
  49. *
  50. * Connections are initiated between a pair of nodes when the node with the
  51. * higher node number gets a heartbeat callback which indicates that the lower
  52. * numbered node has started heartbeating. The lower numbered node is passive
  53. * and only accepts the connection if the higher numbered node is heartbeating.
  54. */
  55. #include <linux/kernel.h>
  56. #include <linux/jiffies.h>
  57. #include <linux/slab.h>
  58. #include <linux/idr.h>
  59. #include <linux/kref.h>
  60. #include <linux/net.h>
  61. #include <linux/export.h>
  62. #include <net/tcp.h>
  63. #include <asm/uaccess.h>
  64. #include "heartbeat.h"
  65. #include "tcp.h"
  66. #include "nodemanager.h"
  67. #define MLOG_MASK_PREFIX ML_TCP
  68. #include "masklog.h"
  69. #include "quorum.h"
  70. #include "tcp_internal.h"
  71. #define SC_NODEF_FMT "node %s (num %u) at %pI4:%u"
  72. #define SC_NODEF_ARGS(sc) sc->sc_node->nd_name, sc->sc_node->nd_num, \
  73. &sc->sc_node->nd_ipv4_address, \
  74. ntohs(sc->sc_node->nd_ipv4_port)
  75. #define msglog(hdr, fmt, args...) do { \
  76. typeof(hdr) __hdr = (hdr); \
  77. mlog(ML_MSG, "[mag %u len %u typ %u stat %d sys_stat %d " \
  78. "key %08x num %u] " fmt, \
  79. be16_to_cpu(__hdr->magic), be16_to_cpu(__hdr->data_len), \
  80. be16_to_cpu(__hdr->msg_type), be32_to_cpu(__hdr->status), \
  81. be32_to_cpu(__hdr->sys_status), be32_to_cpu(__hdr->key), \
  82. be32_to_cpu(__hdr->msg_num) , ##args); \
  83. } while (0)
  84. #define sclog(sc, fmt, args...) do { \
  85. typeof(sc) __sc = (sc); \
  86. mlog(ML_SOCKET, "[sc %p refs %d sock %p node %u page %p " \
  87. "pg_off %zu] " fmt, __sc, \
  88. atomic_read(&__sc->sc_kref.refcount), __sc->sc_sock, \
  89. __sc->sc_node->nd_num, __sc->sc_page, __sc->sc_page_off , \
  90. ##args); \
  91. } while (0)
  92. static DEFINE_RWLOCK(o2net_handler_lock);
  93. static struct rb_root o2net_handler_tree = RB_ROOT;
  94. static struct o2net_node o2net_nodes[O2NM_MAX_NODES];
  95. static struct socket *o2net_listen_sock = NULL;
  96. static struct workqueue_struct *o2net_wq;
  97. static struct work_struct o2net_listen_work;
  98. static struct o2hb_callback_func o2net_hb_up, o2net_hb_down;
  99. #define O2NET_HB_PRI 0x1
  100. static struct o2net_handshake *o2net_hand;
  101. static struct o2net_msg *o2net_keep_req, *o2net_keep_resp;
  102. static int o2net_sys_err_translations[O2NET_ERR_MAX] =
  103. {[O2NET_ERR_NONE] = 0,
  104. [O2NET_ERR_NO_HNDLR] = -ENOPROTOOPT,
  105. [O2NET_ERR_OVERFLOW] = -EOVERFLOW,
  106. [O2NET_ERR_DIED] = -EHOSTDOWN,};
  107. static void o2net_sc_connect_completed(struct work_struct *work);
  108. static void o2net_rx_until_empty(struct work_struct *work);
  109. static void o2net_shutdown_sc(struct work_struct *work);
  110. static void o2net_listen_data_ready(struct sock *sk, int bytes);
  111. static void o2net_sc_send_keep_req(struct work_struct *work);
  112. static void o2net_idle_timer(unsigned long data);
  113. static void o2net_sc_postpone_idle(struct o2net_sock_container *sc);
  114. static void o2net_sc_reset_idle_timer(struct o2net_sock_container *sc);
  115. #ifdef CONFIG_DEBUG_FS
  116. static void o2net_init_nst(struct o2net_send_tracking *nst, u32 msgtype,
  117. u32 msgkey, struct task_struct *task, u8 node)
  118. {
  119. INIT_LIST_HEAD(&nst->st_net_debug_item);
  120. nst->st_task = task;
  121. nst->st_msg_type = msgtype;
  122. nst->st_msg_key = msgkey;
  123. nst->st_node = node;
  124. }
  125. static inline void o2net_set_nst_sock_time(struct o2net_send_tracking *nst)
  126. {
  127. nst->st_sock_time = ktime_get();
  128. }
  129. static inline void o2net_set_nst_send_time(struct o2net_send_tracking *nst)
  130. {
  131. nst->st_send_time = ktime_get();
  132. }
  133. static inline void o2net_set_nst_status_time(struct o2net_send_tracking *nst)
  134. {
  135. nst->st_status_time = ktime_get();
  136. }
  137. static inline void o2net_set_nst_sock_container(struct o2net_send_tracking *nst,
  138. struct o2net_sock_container *sc)
  139. {
  140. nst->st_sc = sc;
  141. }
  142. static inline void o2net_set_nst_msg_id(struct o2net_send_tracking *nst,
  143. u32 msg_id)
  144. {
  145. nst->st_id = msg_id;
  146. }
  147. static inline void o2net_set_sock_timer(struct o2net_sock_container *sc)
  148. {
  149. sc->sc_tv_timer = ktime_get();
  150. }
  151. static inline void o2net_set_data_ready_time(struct o2net_sock_container *sc)
  152. {
  153. sc->sc_tv_data_ready = ktime_get();
  154. }
  155. static inline void o2net_set_advance_start_time(struct o2net_sock_container *sc)
  156. {
  157. sc->sc_tv_advance_start = ktime_get();
  158. }
  159. static inline void o2net_set_advance_stop_time(struct o2net_sock_container *sc)
  160. {
  161. sc->sc_tv_advance_stop = ktime_get();
  162. }
  163. static inline void o2net_set_func_start_time(struct o2net_sock_container *sc)
  164. {
  165. sc->sc_tv_func_start = ktime_get();
  166. }
  167. static inline void o2net_set_func_stop_time(struct o2net_sock_container *sc)
  168. {
  169. sc->sc_tv_func_stop = ktime_get();
  170. }
  171. #else
  172. # define o2net_init_nst(a, b, c, d, e)
  173. # define o2net_set_nst_sock_time(a)
  174. # define o2net_set_nst_send_time(a)
  175. # define o2net_set_nst_status_time(a)
  176. # define o2net_set_nst_sock_container(a, b)
  177. # define o2net_set_nst_msg_id(a, b)
  178. # define o2net_set_sock_timer(a)
  179. # define o2net_set_data_ready_time(a)
  180. # define o2net_set_advance_start_time(a)
  181. # define o2net_set_advance_stop_time(a)
  182. # define o2net_set_func_start_time(a)
  183. # define o2net_set_func_stop_time(a)
  184. #endif
  185. #ifdef CONFIG_OCFS2_FS_STATS
  186. static ktime_t o2net_get_func_run_time(struct o2net_sock_container *sc)
  187. {
  188. return ktime_sub(sc->sc_tv_func_stop, sc->sc_tv_func_start);
  189. }
  190. static void o2net_update_send_stats(struct o2net_send_tracking *nst,
  191. struct o2net_sock_container *sc)
  192. {
  193. sc->sc_tv_status_total = ktime_add(sc->sc_tv_status_total,
  194. ktime_sub(ktime_get(),
  195. nst->st_status_time));
  196. sc->sc_tv_send_total = ktime_add(sc->sc_tv_send_total,
  197. ktime_sub(nst->st_status_time,
  198. nst->st_send_time));
  199. sc->sc_tv_acquiry_total = ktime_add(sc->sc_tv_acquiry_total,
  200. ktime_sub(nst->st_send_time,
  201. nst->st_sock_time));
  202. sc->sc_send_count++;
  203. }
  204. static void o2net_update_recv_stats(struct o2net_sock_container *sc)
  205. {
  206. sc->sc_tv_process_total = ktime_add(sc->sc_tv_process_total,
  207. o2net_get_func_run_time(sc));
  208. sc->sc_recv_count++;
  209. }
  210. #else
  211. # define o2net_update_send_stats(a, b)
  212. # define o2net_update_recv_stats(sc)
  213. #endif
  214. static inline int o2net_reconnect_delay(void)
  215. {
  216. return o2nm_single_cluster->cl_reconnect_delay_ms;
  217. }
  218. static inline int o2net_keepalive_delay(void)
  219. {
  220. return o2nm_single_cluster->cl_keepalive_delay_ms;
  221. }
  222. static inline int o2net_idle_timeout(void)
  223. {
  224. return o2nm_single_cluster->cl_idle_timeout_ms;
  225. }
  226. static inline int o2net_sys_err_to_errno(enum o2net_system_error err)
  227. {
  228. int trans;
  229. BUG_ON(err >= O2NET_ERR_MAX);
  230. trans = o2net_sys_err_translations[err];
  231. BUG_ON(err != O2NET_ERR_NONE && trans == 0);
  232. return trans;
  233. }
  234. static struct o2net_node * o2net_nn_from_num(u8 node_num)
  235. {
  236. BUG_ON(node_num >= ARRAY_SIZE(o2net_nodes));
  237. return &o2net_nodes[node_num];
  238. }
  239. static u8 o2net_num_from_nn(struct o2net_node *nn)
  240. {
  241. BUG_ON(nn == NULL);
  242. return nn - o2net_nodes;
  243. }
  244. static int o2net_prep_nsw(struct o2net_node *nn, struct o2net_status_wait *nsw)
  245. {
  246. int ret = 0;
  247. do {
  248. if (!idr_pre_get(&nn->nn_status_idr, GFP_ATOMIC)) {
  249. ret = -EAGAIN;
  250. break;
  251. }
  252. spin_lock(&nn->nn_lock);
  253. ret = idr_get_new(&nn->nn_status_idr, nsw, &nsw->ns_id);
  254. if (ret == 0)
  255. list_add_tail(&nsw->ns_node_item,
  256. &nn->nn_status_list);
  257. spin_unlock(&nn->nn_lock);
  258. } while (ret == -EAGAIN);
  259. if (ret == 0) {
  260. init_waitqueue_head(&nsw->ns_wq);
  261. nsw->ns_sys_status = O2NET_ERR_NONE;
  262. nsw->ns_status = 0;
  263. }
  264. return ret;
  265. }
  266. static void o2net_complete_nsw_locked(struct o2net_node *nn,
  267. struct o2net_status_wait *nsw,
  268. enum o2net_system_error sys_status,
  269. s32 status)
  270. {
  271. assert_spin_locked(&nn->nn_lock);
  272. if (!list_empty(&nsw->ns_node_item)) {
  273. list_del_init(&nsw->ns_node_item);
  274. nsw->ns_sys_status = sys_status;
  275. nsw->ns_status = status;
  276. idr_remove(&nn->nn_status_idr, nsw->ns_id);
  277. wake_up(&nsw->ns_wq);
  278. }
  279. }
  280. static void o2net_complete_nsw(struct o2net_node *nn,
  281. struct o2net_status_wait *nsw,
  282. u64 id, enum o2net_system_error sys_status,
  283. s32 status)
  284. {
  285. spin_lock(&nn->nn_lock);
  286. if (nsw == NULL) {
  287. if (id > INT_MAX)
  288. goto out;
  289. nsw = idr_find(&nn->nn_status_idr, id);
  290. if (nsw == NULL)
  291. goto out;
  292. }
  293. o2net_complete_nsw_locked(nn, nsw, sys_status, status);
  294. out:
  295. spin_unlock(&nn->nn_lock);
  296. return;
  297. }
  298. static void o2net_complete_nodes_nsw(struct o2net_node *nn)
  299. {
  300. struct o2net_status_wait *nsw, *tmp;
  301. unsigned int num_kills = 0;
  302. assert_spin_locked(&nn->nn_lock);
  303. list_for_each_entry_safe(nsw, tmp, &nn->nn_status_list, ns_node_item) {
  304. o2net_complete_nsw_locked(nn, nsw, O2NET_ERR_DIED, 0);
  305. num_kills++;
  306. }
  307. mlog(0, "completed %d messages for node %u\n", num_kills,
  308. o2net_num_from_nn(nn));
  309. }
  310. static int o2net_nsw_completed(struct o2net_node *nn,
  311. struct o2net_status_wait *nsw)
  312. {
  313. int completed;
  314. spin_lock(&nn->nn_lock);
  315. completed = list_empty(&nsw->ns_node_item);
  316. spin_unlock(&nn->nn_lock);
  317. return completed;
  318. }
  319. static void sc_kref_release(struct kref *kref)
  320. {
  321. struct o2net_sock_container *sc = container_of(kref,
  322. struct o2net_sock_container, sc_kref);
  323. BUG_ON(timer_pending(&sc->sc_idle_timeout));
  324. sclog(sc, "releasing\n");
  325. if (sc->sc_sock) {
  326. sock_release(sc->sc_sock);
  327. sc->sc_sock = NULL;
  328. }
  329. o2nm_undepend_item(&sc->sc_node->nd_item);
  330. o2nm_node_put(sc->sc_node);
  331. sc->sc_node = NULL;
  332. o2net_debug_del_sc(sc);
  333. kfree(sc);
  334. }
  335. static void sc_put(struct o2net_sock_container *sc)
  336. {
  337. sclog(sc, "put\n");
  338. kref_put(&sc->sc_kref, sc_kref_release);
  339. }
  340. static void sc_get(struct o2net_sock_container *sc)
  341. {
  342. sclog(sc, "get\n");
  343. kref_get(&sc->sc_kref);
  344. }
  345. static struct o2net_sock_container *sc_alloc(struct o2nm_node *node)
  346. {
  347. struct o2net_sock_container *sc, *ret = NULL;
  348. struct page *page = NULL;
  349. int status = 0;
  350. page = alloc_page(GFP_NOFS);
  351. sc = kzalloc(sizeof(*sc), GFP_NOFS);
  352. if (sc == NULL || page == NULL)
  353. goto out;
  354. kref_init(&sc->sc_kref);
  355. o2nm_node_get(node);
  356. sc->sc_node = node;
  357. status = o2nm_depend_item(&node->nd_item);
  358. if (status) {
  359. mlog_errno(status);
  360. o2nm_node_put(node);
  361. goto out;
  362. }
  363. INIT_WORK(&sc->sc_connect_work, o2net_sc_connect_completed);
  364. INIT_WORK(&sc->sc_rx_work, o2net_rx_until_empty);
  365. INIT_WORK(&sc->sc_shutdown_work, o2net_shutdown_sc);
  366. INIT_DELAYED_WORK(&sc->sc_keepalive_work, o2net_sc_send_keep_req);
  367. init_timer(&sc->sc_idle_timeout);
  368. sc->sc_idle_timeout.function = o2net_idle_timer;
  369. sc->sc_idle_timeout.data = (unsigned long)sc;
  370. sclog(sc, "alloced\n");
  371. ret = sc;
  372. sc->sc_page = page;
  373. o2net_debug_add_sc(sc);
  374. sc = NULL;
  375. page = NULL;
  376. out:
  377. if (page)
  378. __free_page(page);
  379. kfree(sc);
  380. return ret;
  381. }
  382. static void o2net_sc_queue_work(struct o2net_sock_container *sc,
  383. struct work_struct *work)
  384. {
  385. sc_get(sc);
  386. if (!queue_work(o2net_wq, work))
  387. sc_put(sc);
  388. }
  389. static void o2net_sc_queue_delayed_work(struct o2net_sock_container *sc,
  390. struct delayed_work *work,
  391. int delay)
  392. {
  393. sc_get(sc);
  394. if (!queue_delayed_work(o2net_wq, work, delay))
  395. sc_put(sc);
  396. }
  397. static void o2net_sc_cancel_delayed_work(struct o2net_sock_container *sc,
  398. struct delayed_work *work)
  399. {
  400. if (cancel_delayed_work(work))
  401. sc_put(sc);
  402. }
  403. static atomic_t o2net_connected_peers = ATOMIC_INIT(0);
  404. int o2net_num_connected_peers(void)
  405. {
  406. return atomic_read(&o2net_connected_peers);
  407. }
  408. static void o2net_set_nn_state(struct o2net_node *nn,
  409. struct o2net_sock_container *sc,
  410. unsigned valid, int err)
  411. {
  412. int was_valid = nn->nn_sc_valid;
  413. int was_err = nn->nn_persistent_error;
  414. struct o2net_sock_container *old_sc = nn->nn_sc;
  415. assert_spin_locked(&nn->nn_lock);
  416. if (old_sc && !sc)
  417. atomic_dec(&o2net_connected_peers);
  418. else if (!old_sc && sc)
  419. atomic_inc(&o2net_connected_peers);
  420. /* the node num comparison and single connect/accept path should stop
  421. * an non-null sc from being overwritten with another */
  422. BUG_ON(sc && nn->nn_sc && nn->nn_sc != sc);
  423. mlog_bug_on_msg(err && valid, "err %d valid %u\n", err, valid);
  424. mlog_bug_on_msg(valid && !sc, "valid %u sc %p\n", valid, sc);
  425. if (was_valid && !valid && err == 0)
  426. err = -ENOTCONN;
  427. mlog(ML_CONN, "node %u sc: %p -> %p, valid %u -> %u, err %d -> %d\n",
  428. o2net_num_from_nn(nn), nn->nn_sc, sc, nn->nn_sc_valid, valid,
  429. nn->nn_persistent_error, err);
  430. nn->nn_sc = sc;
  431. nn->nn_sc_valid = valid ? 1 : 0;
  432. nn->nn_persistent_error = err;
  433. if (nn->nn_persistent_error || nn->nn_sc_valid)
  434. wake_up(&nn->nn_sc_wq);
  435. if (!was_err && nn->nn_persistent_error) {
  436. o2quo_conn_err(o2net_num_from_nn(nn));
  437. queue_delayed_work(o2net_wq, &nn->nn_still_up,
  438. msecs_to_jiffies(O2NET_QUORUM_DELAY_MS));
  439. }
  440. if (was_valid && !valid) {
  441. printk(KERN_NOTICE "o2net: No longer connected to "
  442. SC_NODEF_FMT "\n", SC_NODEF_ARGS(old_sc));
  443. o2net_complete_nodes_nsw(nn);
  444. }
  445. if (!was_valid && valid) {
  446. o2quo_conn_up(o2net_num_from_nn(nn));
  447. cancel_delayed_work(&nn->nn_connect_expired);
  448. printk(KERN_NOTICE "o2net: %s " SC_NODEF_FMT "\n",
  449. o2nm_this_node() > sc->sc_node->nd_num ?
  450. "Connected to" : "Accepted connection from",
  451. SC_NODEF_ARGS(sc));
  452. }
  453. if (!valid && o2net_wq) {
  454. unsigned long delay;
  455. delay = (nn->nn_last_connect_attempt +
  456. msecs_to_jiffies(o2net_reconnect_delay()))
  457. - jiffies;
  458. if (delay > msecs_to_jiffies(o2net_reconnect_delay()))
  459. delay = 0;
  460. mlog(ML_CONN, "queueing conn attempt in %lu jiffies\n", delay);
  461. queue_delayed_work(o2net_wq, &nn->nn_connect_work, delay);
  462. delay += msecs_to_jiffies(o2net_idle_timeout());
  463. queue_delayed_work(o2net_wq, &nn->nn_connect_expired, delay);
  464. }
  465. if ((old_sc == NULL) && sc)
  466. sc_get(sc);
  467. if (old_sc && (old_sc != sc)) {
  468. o2net_sc_queue_work(old_sc, &old_sc->sc_shutdown_work);
  469. sc_put(old_sc);
  470. }
  471. }
  472. static void o2net_data_ready(struct sock *sk, int bytes)
  473. {
  474. void (*ready)(struct sock *sk, int bytes);
  475. read_lock(&sk->sk_callback_lock);
  476. if (sk->sk_user_data) {
  477. struct o2net_sock_container *sc = sk->sk_user_data;
  478. sclog(sc, "data_ready hit\n");
  479. o2net_set_data_ready_time(sc);
  480. o2net_sc_queue_work(sc, &sc->sc_rx_work);
  481. ready = sc->sc_data_ready;
  482. } else {
  483. ready = sk->sk_data_ready;
  484. }
  485. read_unlock(&sk->sk_callback_lock);
  486. ready(sk, bytes);
  487. }
  488. static void o2net_state_change(struct sock *sk)
  489. {
  490. void (*state_change)(struct sock *sk);
  491. struct o2net_sock_container *sc;
  492. read_lock(&sk->sk_callback_lock);
  493. sc = sk->sk_user_data;
  494. if (sc == NULL) {
  495. state_change = sk->sk_state_change;
  496. goto out;
  497. }
  498. sclog(sc, "state_change to %d\n", sk->sk_state);
  499. state_change = sc->sc_state_change;
  500. switch(sk->sk_state) {
  501. case TCP_SYN_SENT:
  502. case TCP_SYN_RECV:
  503. break;
  504. case TCP_ESTABLISHED:
  505. o2net_sc_queue_work(sc, &sc->sc_connect_work);
  506. break;
  507. default:
  508. printk(KERN_INFO "o2net: Connection to " SC_NODEF_FMT
  509. " shutdown, state %d\n",
  510. SC_NODEF_ARGS(sc), sk->sk_state);
  511. o2net_sc_queue_work(sc, &sc->sc_shutdown_work);
  512. break;
  513. }
  514. out:
  515. read_unlock(&sk->sk_callback_lock);
  516. state_change(sk);
  517. }
  518. static void o2net_register_callbacks(struct sock *sk,
  519. struct o2net_sock_container *sc)
  520. {
  521. write_lock_bh(&sk->sk_callback_lock);
  522. if (sk->sk_data_ready == o2net_listen_data_ready) {
  523. sk->sk_data_ready = sk->sk_user_data;
  524. sk->sk_user_data = NULL;
  525. }
  526. BUG_ON(sk->sk_user_data != NULL);
  527. sk->sk_user_data = sc;
  528. sc_get(sc);
  529. sc->sc_data_ready = sk->sk_data_ready;
  530. sc->sc_state_change = sk->sk_state_change;
  531. sk->sk_data_ready = o2net_data_ready;
  532. sk->sk_state_change = o2net_state_change;
  533. mutex_init(&sc->sc_send_lock);
  534. write_unlock_bh(&sk->sk_callback_lock);
  535. }
  536. static int o2net_unregister_callbacks(struct sock *sk,
  537. struct o2net_sock_container *sc)
  538. {
  539. int ret = 0;
  540. write_lock_bh(&sk->sk_callback_lock);
  541. if (sk->sk_user_data == sc) {
  542. ret = 1;
  543. sk->sk_user_data = NULL;
  544. sk->sk_data_ready = sc->sc_data_ready;
  545. sk->sk_state_change = sc->sc_state_change;
  546. }
  547. write_unlock_bh(&sk->sk_callback_lock);
  548. return ret;
  549. }
  550. static void o2net_ensure_shutdown(struct o2net_node *nn,
  551. struct o2net_sock_container *sc,
  552. int err)
  553. {
  554. spin_lock(&nn->nn_lock);
  555. if (nn->nn_sc == sc)
  556. o2net_set_nn_state(nn, NULL, 0, err);
  557. spin_unlock(&nn->nn_lock);
  558. }
  559. static void o2net_shutdown_sc(struct work_struct *work)
  560. {
  561. struct o2net_sock_container *sc =
  562. container_of(work, struct o2net_sock_container,
  563. sc_shutdown_work);
  564. struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
  565. sclog(sc, "shutting down\n");
  566. if (o2net_unregister_callbacks(sc->sc_sock->sk, sc)) {
  567. del_timer_sync(&sc->sc_idle_timeout);
  568. o2net_sc_cancel_delayed_work(sc, &sc->sc_keepalive_work);
  569. sc_put(sc);
  570. kernel_sock_shutdown(sc->sc_sock, SHUT_RDWR);
  571. }
  572. o2net_ensure_shutdown(nn, sc, 0);
  573. sc_put(sc);
  574. }
  575. static int o2net_handler_cmp(struct o2net_msg_handler *nmh, u32 msg_type,
  576. u32 key)
  577. {
  578. int ret = memcmp(&nmh->nh_key, &key, sizeof(key));
  579. if (ret == 0)
  580. ret = memcmp(&nmh->nh_msg_type, &msg_type, sizeof(msg_type));
  581. return ret;
  582. }
  583. static struct o2net_msg_handler *
  584. o2net_handler_tree_lookup(u32 msg_type, u32 key, struct rb_node ***ret_p,
  585. struct rb_node **ret_parent)
  586. {
  587. struct rb_node **p = &o2net_handler_tree.rb_node;
  588. struct rb_node *parent = NULL;
  589. struct o2net_msg_handler *nmh, *ret = NULL;
  590. int cmp;
  591. while (*p) {
  592. parent = *p;
  593. nmh = rb_entry(parent, struct o2net_msg_handler, nh_node);
  594. cmp = o2net_handler_cmp(nmh, msg_type, key);
  595. if (cmp < 0)
  596. p = &(*p)->rb_left;
  597. else if (cmp > 0)
  598. p = &(*p)->rb_right;
  599. else {
  600. ret = nmh;
  601. break;
  602. }
  603. }
  604. if (ret_p != NULL)
  605. *ret_p = p;
  606. if (ret_parent != NULL)
  607. *ret_parent = parent;
  608. return ret;
  609. }
  610. static void o2net_handler_kref_release(struct kref *kref)
  611. {
  612. struct o2net_msg_handler *nmh;
  613. nmh = container_of(kref, struct o2net_msg_handler, nh_kref);
  614. kfree(nmh);
  615. }
  616. static void o2net_handler_put(struct o2net_msg_handler *nmh)
  617. {
  618. kref_put(&nmh->nh_kref, o2net_handler_kref_release);
  619. }
  620. int o2net_register_handler(u32 msg_type, u32 key, u32 max_len,
  621. o2net_msg_handler_func *func, void *data,
  622. o2net_post_msg_handler_func *post_func,
  623. struct list_head *unreg_list)
  624. {
  625. struct o2net_msg_handler *nmh = NULL;
  626. struct rb_node **p, *parent;
  627. int ret = 0;
  628. if (max_len > O2NET_MAX_PAYLOAD_BYTES) {
  629. mlog(0, "max_len for message handler out of range: %u\n",
  630. max_len);
  631. ret = -EINVAL;
  632. goto out;
  633. }
  634. if (!msg_type) {
  635. mlog(0, "no message type provided: %u, %p\n", msg_type, func);
  636. ret = -EINVAL;
  637. goto out;
  638. }
  639. if (!func) {
  640. mlog(0, "no message handler provided: %u, %p\n",
  641. msg_type, func);
  642. ret = -EINVAL;
  643. goto out;
  644. }
  645. nmh = kzalloc(sizeof(struct o2net_msg_handler), GFP_NOFS);
  646. if (nmh == NULL) {
  647. ret = -ENOMEM;
  648. goto out;
  649. }
  650. nmh->nh_func = func;
  651. nmh->nh_func_data = data;
  652. nmh->nh_post_func = post_func;
  653. nmh->nh_msg_type = msg_type;
  654. nmh->nh_max_len = max_len;
  655. nmh->nh_key = key;
  656. kref_init(&nmh->nh_kref);
  657. INIT_LIST_HEAD(&nmh->nh_unregister_item);
  658. write_lock(&o2net_handler_lock);
  659. if (o2net_handler_tree_lookup(msg_type, key, &p, &parent))
  660. ret = -EEXIST;
  661. else {
  662. rb_link_node(&nmh->nh_node, parent, p);
  663. rb_insert_color(&nmh->nh_node, &o2net_handler_tree);
  664. list_add_tail(&nmh->nh_unregister_item, unreg_list);
  665. mlog(ML_TCP, "registered handler func %p type %u key %08x\n",
  666. func, msg_type, key);
  667. mlog_bug_on_msg(o2net_handler_tree_lookup(msg_type, key, &p,
  668. &parent) == NULL,
  669. "couldn't find handler we *just* registerd "
  670. "for type %u key %08x\n", msg_type, key);
  671. }
  672. write_unlock(&o2net_handler_lock);
  673. if (ret)
  674. goto out;
  675. out:
  676. if (ret)
  677. kfree(nmh);
  678. return ret;
  679. }
  680. EXPORT_SYMBOL_GPL(o2net_register_handler);
  681. void o2net_unregister_handler_list(struct list_head *list)
  682. {
  683. struct o2net_msg_handler *nmh, *n;
  684. write_lock(&o2net_handler_lock);
  685. list_for_each_entry_safe(nmh, n, list, nh_unregister_item) {
  686. mlog(ML_TCP, "unregistering handler func %p type %u key %08x\n",
  687. nmh->nh_func, nmh->nh_msg_type, nmh->nh_key);
  688. rb_erase(&nmh->nh_node, &o2net_handler_tree);
  689. list_del_init(&nmh->nh_unregister_item);
  690. kref_put(&nmh->nh_kref, o2net_handler_kref_release);
  691. }
  692. write_unlock(&o2net_handler_lock);
  693. }
  694. EXPORT_SYMBOL_GPL(o2net_unregister_handler_list);
  695. static struct o2net_msg_handler *o2net_handler_get(u32 msg_type, u32 key)
  696. {
  697. struct o2net_msg_handler *nmh;
  698. read_lock(&o2net_handler_lock);
  699. nmh = o2net_handler_tree_lookup(msg_type, key, NULL, NULL);
  700. if (nmh)
  701. kref_get(&nmh->nh_kref);
  702. read_unlock(&o2net_handler_lock);
  703. return nmh;
  704. }
  705. static int o2net_recv_tcp_msg(struct socket *sock, void *data, size_t len)
  706. {
  707. int ret;
  708. mm_segment_t oldfs;
  709. struct kvec vec = {
  710. .iov_len = len,
  711. .iov_base = data,
  712. };
  713. struct msghdr msg = {
  714. .msg_iovlen = 1,
  715. .msg_iov = (struct iovec *)&vec,
  716. .msg_flags = MSG_DONTWAIT,
  717. };
  718. oldfs = get_fs();
  719. set_fs(get_ds());
  720. ret = sock_recvmsg(sock, &msg, len, msg.msg_flags);
  721. set_fs(oldfs);
  722. return ret;
  723. }
  724. static int o2net_send_tcp_msg(struct socket *sock, struct kvec *vec,
  725. size_t veclen, size_t total)
  726. {
  727. int ret;
  728. mm_segment_t oldfs;
  729. struct msghdr msg = {
  730. .msg_iov = (struct iovec *)vec,
  731. .msg_iovlen = veclen,
  732. };
  733. if (sock == NULL) {
  734. ret = -EINVAL;
  735. goto out;
  736. }
  737. oldfs = get_fs();
  738. set_fs(get_ds());
  739. ret = sock_sendmsg(sock, &msg, total);
  740. set_fs(oldfs);
  741. if (ret != total) {
  742. mlog(ML_ERROR, "sendmsg returned %d instead of %zu\n", ret,
  743. total);
  744. if (ret >= 0)
  745. ret = -EPIPE;
  746. goto out;
  747. }
  748. ret = 0;
  749. out:
  750. if (ret < 0)
  751. mlog(0, "returning error: %d\n", ret);
  752. return ret;
  753. }
  754. static void o2net_sendpage(struct o2net_sock_container *sc,
  755. void *kmalloced_virt,
  756. size_t size)
  757. {
  758. struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
  759. ssize_t ret;
  760. while (1) {
  761. mutex_lock(&sc->sc_send_lock);
  762. ret = sc->sc_sock->ops->sendpage(sc->sc_sock,
  763. virt_to_page(kmalloced_virt),
  764. (long)kmalloced_virt & ~PAGE_MASK,
  765. size, MSG_DONTWAIT);
  766. mutex_unlock(&sc->sc_send_lock);
  767. if (ret == size)
  768. break;
  769. if (ret == (ssize_t)-EAGAIN) {
  770. mlog(0, "sendpage of size %zu to " SC_NODEF_FMT
  771. " returned EAGAIN\n", size, SC_NODEF_ARGS(sc));
  772. cond_resched();
  773. continue;
  774. }
  775. mlog(ML_ERROR, "sendpage of size %zu to " SC_NODEF_FMT
  776. " failed with %zd\n", size, SC_NODEF_ARGS(sc), ret);
  777. o2net_ensure_shutdown(nn, sc, 0);
  778. break;
  779. }
  780. }
  781. static void o2net_init_msg(struct o2net_msg *msg, u16 data_len, u16 msg_type, u32 key)
  782. {
  783. memset(msg, 0, sizeof(struct o2net_msg));
  784. msg->magic = cpu_to_be16(O2NET_MSG_MAGIC);
  785. msg->data_len = cpu_to_be16(data_len);
  786. msg->msg_type = cpu_to_be16(msg_type);
  787. msg->sys_status = cpu_to_be32(O2NET_ERR_NONE);
  788. msg->status = 0;
  789. msg->key = cpu_to_be32(key);
  790. }
  791. static int o2net_tx_can_proceed(struct o2net_node *nn,
  792. struct o2net_sock_container **sc_ret,
  793. int *error)
  794. {
  795. int ret = 0;
  796. spin_lock(&nn->nn_lock);
  797. if (nn->nn_persistent_error) {
  798. ret = 1;
  799. *sc_ret = NULL;
  800. *error = nn->nn_persistent_error;
  801. } else if (nn->nn_sc_valid) {
  802. kref_get(&nn->nn_sc->sc_kref);
  803. ret = 1;
  804. *sc_ret = nn->nn_sc;
  805. *error = 0;
  806. }
  807. spin_unlock(&nn->nn_lock);
  808. return ret;
  809. }
  810. void o2net_fill_node_map(unsigned long *map, unsigned bytes)
  811. {
  812. struct o2net_sock_container *sc;
  813. int node, ret;
  814. BUG_ON(bytes < (BITS_TO_LONGS(O2NM_MAX_NODES) * sizeof(unsigned long)));
  815. memset(map, 0, bytes);
  816. for (node = 0; node < O2NM_MAX_NODES; ++node) {
  817. o2net_tx_can_proceed(o2net_nn_from_num(node), &sc, &ret);
  818. if (!ret) {
  819. set_bit(node, map);
  820. sc_put(sc);
  821. }
  822. }
  823. }
  824. EXPORT_SYMBOL_GPL(o2net_fill_node_map);
  825. int o2net_send_message_vec(u32 msg_type, u32 key, struct kvec *caller_vec,
  826. size_t caller_veclen, u8 target_node, int *status)
  827. {
  828. int ret = 0;
  829. struct o2net_msg *msg = NULL;
  830. size_t veclen, caller_bytes = 0;
  831. struct kvec *vec = NULL;
  832. struct o2net_sock_container *sc = NULL;
  833. struct o2net_node *nn = o2net_nn_from_num(target_node);
  834. struct o2net_status_wait nsw = {
  835. .ns_node_item = LIST_HEAD_INIT(nsw.ns_node_item),
  836. };
  837. struct o2net_send_tracking nst;
  838. o2net_init_nst(&nst, msg_type, key, current, target_node);
  839. if (o2net_wq == NULL) {
  840. mlog(0, "attempt to tx without o2netd running\n");
  841. ret = -ESRCH;
  842. goto out;
  843. }
  844. if (caller_veclen == 0) {
  845. mlog(0, "bad kvec array length\n");
  846. ret = -EINVAL;
  847. goto out;
  848. }
  849. caller_bytes = iov_length((struct iovec *)caller_vec, caller_veclen);
  850. if (caller_bytes > O2NET_MAX_PAYLOAD_BYTES) {
  851. mlog(0, "total payload len %zu too large\n", caller_bytes);
  852. ret = -EINVAL;
  853. goto out;
  854. }
  855. if (target_node == o2nm_this_node()) {
  856. ret = -ELOOP;
  857. goto out;
  858. }
  859. o2net_debug_add_nst(&nst);
  860. o2net_set_nst_sock_time(&nst);
  861. wait_event(nn->nn_sc_wq, o2net_tx_can_proceed(nn, &sc, &ret));
  862. if (ret)
  863. goto out;
  864. o2net_set_nst_sock_container(&nst, sc);
  865. veclen = caller_veclen + 1;
  866. vec = kmalloc(sizeof(struct kvec) * veclen, GFP_ATOMIC);
  867. if (vec == NULL) {
  868. mlog(0, "failed to %zu element kvec!\n", veclen);
  869. ret = -ENOMEM;
  870. goto out;
  871. }
  872. msg = kmalloc(sizeof(struct o2net_msg), GFP_ATOMIC);
  873. if (!msg) {
  874. mlog(0, "failed to allocate a o2net_msg!\n");
  875. ret = -ENOMEM;
  876. goto out;
  877. }
  878. o2net_init_msg(msg, caller_bytes, msg_type, key);
  879. vec[0].iov_len = sizeof(struct o2net_msg);
  880. vec[0].iov_base = msg;
  881. memcpy(&vec[1], caller_vec, caller_veclen * sizeof(struct kvec));
  882. ret = o2net_prep_nsw(nn, &nsw);
  883. if (ret)
  884. goto out;
  885. msg->msg_num = cpu_to_be32(nsw.ns_id);
  886. o2net_set_nst_msg_id(&nst, nsw.ns_id);
  887. o2net_set_nst_send_time(&nst);
  888. mutex_lock(&sc->sc_send_lock);
  889. ret = o2net_send_tcp_msg(sc->sc_sock, vec, veclen,
  890. sizeof(struct o2net_msg) + caller_bytes);
  891. mutex_unlock(&sc->sc_send_lock);
  892. msglog(msg, "sending returned %d\n", ret);
  893. if (ret < 0) {
  894. mlog(0, "error returned from o2net_send_tcp_msg=%d\n", ret);
  895. goto out;
  896. }
  897. o2net_set_nst_status_time(&nst);
  898. wait_event(nsw.ns_wq, o2net_nsw_completed(nn, &nsw));
  899. o2net_update_send_stats(&nst, sc);
  900. ret = o2net_sys_err_to_errno(nsw.ns_sys_status);
  901. if (status && !ret)
  902. *status = nsw.ns_status;
  903. mlog(0, "woken, returning system status %d, user status %d\n",
  904. ret, nsw.ns_status);
  905. out:
  906. o2net_debug_del_nst(&nst);
  907. if (sc)
  908. sc_put(sc);
  909. if (vec)
  910. kfree(vec);
  911. if (msg)
  912. kfree(msg);
  913. o2net_complete_nsw(nn, &nsw, 0, 0, 0);
  914. return ret;
  915. }
  916. EXPORT_SYMBOL_GPL(o2net_send_message_vec);
  917. int o2net_send_message(u32 msg_type, u32 key, void *data, u32 len,
  918. u8 target_node, int *status)
  919. {
  920. struct kvec vec = {
  921. .iov_base = data,
  922. .iov_len = len,
  923. };
  924. return o2net_send_message_vec(msg_type, key, &vec, 1,
  925. target_node, status);
  926. }
  927. EXPORT_SYMBOL_GPL(o2net_send_message);
  928. static int o2net_send_status_magic(struct socket *sock, struct o2net_msg *hdr,
  929. enum o2net_system_error syserr, int err)
  930. {
  931. struct kvec vec = {
  932. .iov_base = hdr,
  933. .iov_len = sizeof(struct o2net_msg),
  934. };
  935. BUG_ON(syserr >= O2NET_ERR_MAX);
  936. hdr->sys_status = cpu_to_be32(syserr);
  937. hdr->status = cpu_to_be32(err);
  938. hdr->magic = cpu_to_be16(O2NET_MSG_STATUS_MAGIC);
  939. hdr->data_len = 0;
  940. msglog(hdr, "about to send status magic %d\n", err);
  941. return o2net_send_tcp_msg(sock, &vec, 1, sizeof(struct o2net_msg));
  942. }
  943. static int o2net_process_message(struct o2net_sock_container *sc,
  944. struct o2net_msg *hdr)
  945. {
  946. struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
  947. int ret = 0, handler_status;
  948. enum o2net_system_error syserr;
  949. struct o2net_msg_handler *nmh = NULL;
  950. void *ret_data = NULL;
  951. msglog(hdr, "processing message\n");
  952. o2net_sc_postpone_idle(sc);
  953. switch(be16_to_cpu(hdr->magic)) {
  954. case O2NET_MSG_STATUS_MAGIC:
  955. o2net_complete_nsw(nn, NULL,
  956. be32_to_cpu(hdr->msg_num),
  957. be32_to_cpu(hdr->sys_status),
  958. be32_to_cpu(hdr->status));
  959. goto out;
  960. case O2NET_MSG_KEEP_REQ_MAGIC:
  961. o2net_sendpage(sc, o2net_keep_resp,
  962. sizeof(*o2net_keep_resp));
  963. goto out;
  964. case O2NET_MSG_KEEP_RESP_MAGIC:
  965. goto out;
  966. case O2NET_MSG_MAGIC:
  967. break;
  968. default:
  969. msglog(hdr, "bad magic\n");
  970. ret = -EINVAL;
  971. goto out;
  972. break;
  973. }
  974. handler_status = 0;
  975. nmh = o2net_handler_get(be16_to_cpu(hdr->msg_type),
  976. be32_to_cpu(hdr->key));
  977. if (!nmh) {
  978. mlog(ML_TCP, "couldn't find handler for type %u key %08x\n",
  979. be16_to_cpu(hdr->msg_type), be32_to_cpu(hdr->key));
  980. syserr = O2NET_ERR_NO_HNDLR;
  981. goto out_respond;
  982. }
  983. syserr = O2NET_ERR_NONE;
  984. if (be16_to_cpu(hdr->data_len) > nmh->nh_max_len)
  985. syserr = O2NET_ERR_OVERFLOW;
  986. if (syserr != O2NET_ERR_NONE)
  987. goto out_respond;
  988. o2net_set_func_start_time(sc);
  989. sc->sc_msg_key = be32_to_cpu(hdr->key);
  990. sc->sc_msg_type = be16_to_cpu(hdr->msg_type);
  991. handler_status = (nmh->nh_func)(hdr, sizeof(struct o2net_msg) +
  992. be16_to_cpu(hdr->data_len),
  993. nmh->nh_func_data, &ret_data);
  994. o2net_set_func_stop_time(sc);
  995. o2net_update_recv_stats(sc);
  996. out_respond:
  997. mutex_lock(&sc->sc_send_lock);
  998. ret = o2net_send_status_magic(sc->sc_sock, hdr, syserr,
  999. handler_status);
  1000. mutex_unlock(&sc->sc_send_lock);
  1001. hdr = NULL;
  1002. mlog(0, "sending handler status %d, syserr %d returned %d\n",
  1003. handler_status, syserr, ret);
  1004. if (nmh) {
  1005. BUG_ON(ret_data != NULL && nmh->nh_post_func == NULL);
  1006. if (nmh->nh_post_func)
  1007. (nmh->nh_post_func)(handler_status, nmh->nh_func_data,
  1008. ret_data);
  1009. }
  1010. out:
  1011. if (nmh)
  1012. o2net_handler_put(nmh);
  1013. return ret;
  1014. }
  1015. static int o2net_check_handshake(struct o2net_sock_container *sc)
  1016. {
  1017. struct o2net_handshake *hand = page_address(sc->sc_page);
  1018. struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
  1019. if (hand->protocol_version != cpu_to_be64(O2NET_PROTOCOL_VERSION)) {
  1020. printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " Advertised net "
  1021. "protocol version %llu but %llu is required. "
  1022. "Disconnecting.\n", SC_NODEF_ARGS(sc),
  1023. (unsigned long long)be64_to_cpu(hand->protocol_version),
  1024. O2NET_PROTOCOL_VERSION);
  1025. o2net_ensure_shutdown(nn, sc, -ENOTCONN);
  1026. return -1;
  1027. }
  1028. if (be32_to_cpu(hand->o2net_idle_timeout_ms) !=
  1029. o2net_idle_timeout()) {
  1030. printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " uses a network "
  1031. "idle timeout of %u ms, but we use %u ms locally. "
  1032. "Disconnecting.\n", SC_NODEF_ARGS(sc),
  1033. be32_to_cpu(hand->o2net_idle_timeout_ms),
  1034. o2net_idle_timeout());
  1035. o2net_ensure_shutdown(nn, sc, -ENOTCONN);
  1036. return -1;
  1037. }
  1038. if (be32_to_cpu(hand->o2net_keepalive_delay_ms) !=
  1039. o2net_keepalive_delay()) {
  1040. printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " uses a keepalive "
  1041. "delay of %u ms, but we use %u ms locally. "
  1042. "Disconnecting.\n", SC_NODEF_ARGS(sc),
  1043. be32_to_cpu(hand->o2net_keepalive_delay_ms),
  1044. o2net_keepalive_delay());
  1045. o2net_ensure_shutdown(nn, sc, -ENOTCONN);
  1046. return -1;
  1047. }
  1048. if (be32_to_cpu(hand->o2hb_heartbeat_timeout_ms) !=
  1049. O2HB_MAX_WRITE_TIMEOUT_MS) {
  1050. printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " uses a heartbeat "
  1051. "timeout of %u ms, but we use %u ms locally. "
  1052. "Disconnecting.\n", SC_NODEF_ARGS(sc),
  1053. be32_to_cpu(hand->o2hb_heartbeat_timeout_ms),
  1054. O2HB_MAX_WRITE_TIMEOUT_MS);
  1055. o2net_ensure_shutdown(nn, sc, -ENOTCONN);
  1056. return -1;
  1057. }
  1058. sc->sc_handshake_ok = 1;
  1059. spin_lock(&nn->nn_lock);
  1060. if (nn->nn_sc == sc) {
  1061. o2net_sc_reset_idle_timer(sc);
  1062. atomic_set(&nn->nn_timeout, 0);
  1063. o2net_set_nn_state(nn, sc, 1, 0);
  1064. }
  1065. spin_unlock(&nn->nn_lock);
  1066. sc->sc_page_off -= sizeof(struct o2net_handshake);
  1067. if (sc->sc_page_off)
  1068. memmove(hand, hand + 1, sc->sc_page_off);
  1069. return 0;
  1070. }
  1071. static int o2net_advance_rx(struct o2net_sock_container *sc)
  1072. {
  1073. struct o2net_msg *hdr;
  1074. int ret = 0;
  1075. void *data;
  1076. size_t datalen;
  1077. sclog(sc, "receiving\n");
  1078. o2net_set_advance_start_time(sc);
  1079. if (unlikely(sc->sc_handshake_ok == 0)) {
  1080. if(sc->sc_page_off < sizeof(struct o2net_handshake)) {
  1081. data = page_address(sc->sc_page) + sc->sc_page_off;
  1082. datalen = sizeof(struct o2net_handshake) - sc->sc_page_off;
  1083. ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen);
  1084. if (ret > 0)
  1085. sc->sc_page_off += ret;
  1086. }
  1087. if (sc->sc_page_off == sizeof(struct o2net_handshake)) {
  1088. o2net_check_handshake(sc);
  1089. if (unlikely(sc->sc_handshake_ok == 0))
  1090. ret = -EPROTO;
  1091. }
  1092. goto out;
  1093. }
  1094. if (sc->sc_page_off < sizeof(struct o2net_msg)) {
  1095. data = page_address(sc->sc_page) + sc->sc_page_off;
  1096. datalen = sizeof(struct o2net_msg) - sc->sc_page_off;
  1097. ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen);
  1098. if (ret > 0) {
  1099. sc->sc_page_off += ret;
  1100. if (sc->sc_page_off == sizeof(struct o2net_msg)) {
  1101. hdr = page_address(sc->sc_page);
  1102. if (be16_to_cpu(hdr->data_len) >
  1103. O2NET_MAX_PAYLOAD_BYTES)
  1104. ret = -EOVERFLOW;
  1105. }
  1106. }
  1107. if (ret <= 0)
  1108. goto out;
  1109. }
  1110. if (sc->sc_page_off < sizeof(struct o2net_msg)) {
  1111. goto out;
  1112. }
  1113. hdr = page_address(sc->sc_page);
  1114. msglog(hdr, "at page_off %zu\n", sc->sc_page_off);
  1115. if (sc->sc_page_off - sizeof(struct o2net_msg) < be16_to_cpu(hdr->data_len)) {
  1116. data = page_address(sc->sc_page) + sc->sc_page_off;
  1117. datalen = (sizeof(struct o2net_msg) + be16_to_cpu(hdr->data_len)) -
  1118. sc->sc_page_off;
  1119. ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen);
  1120. if (ret > 0)
  1121. sc->sc_page_off += ret;
  1122. if (ret <= 0)
  1123. goto out;
  1124. }
  1125. if (sc->sc_page_off - sizeof(struct o2net_msg) == be16_to_cpu(hdr->data_len)) {
  1126. ret = o2net_process_message(sc, hdr);
  1127. if (ret == 0)
  1128. ret = 1;
  1129. sc->sc_page_off = 0;
  1130. }
  1131. out:
  1132. sclog(sc, "ret = %d\n", ret);
  1133. o2net_set_advance_stop_time(sc);
  1134. return ret;
  1135. }
  1136. static void o2net_rx_until_empty(struct work_struct *work)
  1137. {
  1138. struct o2net_sock_container *sc =
  1139. container_of(work, struct o2net_sock_container, sc_rx_work);
  1140. int ret;
  1141. do {
  1142. ret = o2net_advance_rx(sc);
  1143. } while (ret > 0);
  1144. if (ret <= 0 && ret != -EAGAIN) {
  1145. struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
  1146. sclog(sc, "saw error %d, closing\n", ret);
  1147. o2net_ensure_shutdown(nn, sc, 0);
  1148. }
  1149. sc_put(sc);
  1150. }
  1151. static int o2net_set_nodelay(struct socket *sock)
  1152. {
  1153. int ret, val = 1;
  1154. mm_segment_t oldfs;
  1155. oldfs = get_fs();
  1156. set_fs(KERNEL_DS);
  1157. ret = sock->ops->setsockopt(sock, SOL_TCP, TCP_NODELAY,
  1158. (char __user *)&val, sizeof(val));
  1159. set_fs(oldfs);
  1160. return ret;
  1161. }
  1162. static void o2net_initialize_handshake(void)
  1163. {
  1164. o2net_hand->o2hb_heartbeat_timeout_ms = cpu_to_be32(
  1165. O2HB_MAX_WRITE_TIMEOUT_MS);
  1166. o2net_hand->o2net_idle_timeout_ms = cpu_to_be32(o2net_idle_timeout());
  1167. o2net_hand->o2net_keepalive_delay_ms = cpu_to_be32(
  1168. o2net_keepalive_delay());
  1169. o2net_hand->o2net_reconnect_delay_ms = cpu_to_be32(
  1170. o2net_reconnect_delay());
  1171. }
  1172. static void o2net_sc_connect_completed(struct work_struct *work)
  1173. {
  1174. struct o2net_sock_container *sc =
  1175. container_of(work, struct o2net_sock_container,
  1176. sc_connect_work);
  1177. mlog(ML_MSG, "sc sending handshake with ver %llu id %llx\n",
  1178. (unsigned long long)O2NET_PROTOCOL_VERSION,
  1179. (unsigned long long)be64_to_cpu(o2net_hand->connector_id));
  1180. o2net_initialize_handshake();
  1181. o2net_sendpage(sc, o2net_hand, sizeof(*o2net_hand));
  1182. sc_put(sc);
  1183. }
  1184. static void o2net_sc_send_keep_req(struct work_struct *work)
  1185. {
  1186. struct o2net_sock_container *sc =
  1187. container_of(work, struct o2net_sock_container,
  1188. sc_keepalive_work.work);
  1189. o2net_sendpage(sc, o2net_keep_req, sizeof(*o2net_keep_req));
  1190. sc_put(sc);
  1191. }
  1192. static void o2net_idle_timer(unsigned long data)
  1193. {
  1194. struct o2net_sock_container *sc = (struct o2net_sock_container *)data;
  1195. struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
  1196. #ifdef CONFIG_DEBUG_FS
  1197. unsigned long msecs = ktime_to_ms(ktime_get()) -
  1198. ktime_to_ms(sc->sc_tv_timer);
  1199. #else
  1200. unsigned long msecs = o2net_idle_timeout();
  1201. #endif
  1202. printk(KERN_NOTICE "o2net: Connection to " SC_NODEF_FMT " has been "
  1203. "idle for %lu.%lu secs, shutting it down.\n", SC_NODEF_ARGS(sc),
  1204. msecs / 1000, msecs % 1000);
  1205. atomic_set(&nn->nn_timeout, 1);
  1206. o2net_sc_queue_work(sc, &sc->sc_shutdown_work);
  1207. }
  1208. static void o2net_sc_reset_idle_timer(struct o2net_sock_container *sc)
  1209. {
  1210. o2net_sc_cancel_delayed_work(sc, &sc->sc_keepalive_work);
  1211. o2net_sc_queue_delayed_work(sc, &sc->sc_keepalive_work,
  1212. msecs_to_jiffies(o2net_keepalive_delay()));
  1213. o2net_set_sock_timer(sc);
  1214. mod_timer(&sc->sc_idle_timeout,
  1215. jiffies + msecs_to_jiffies(o2net_idle_timeout()));
  1216. }
  1217. static void o2net_sc_postpone_idle(struct o2net_sock_container *sc)
  1218. {
  1219. if (timer_pending(&sc->sc_idle_timeout))
  1220. o2net_sc_reset_idle_timer(sc);
  1221. }
  1222. static void o2net_start_connect(struct work_struct *work)
  1223. {
  1224. struct o2net_node *nn =
  1225. container_of(work, struct o2net_node, nn_connect_work.work);
  1226. struct o2net_sock_container *sc = NULL;
  1227. struct o2nm_node *node = NULL, *mynode = NULL;
  1228. struct socket *sock = NULL;
  1229. struct sockaddr_in myaddr = {0, }, remoteaddr = {0, };
  1230. int ret = 0, stop;
  1231. unsigned int timeout;
  1232. if (o2nm_this_node() <= o2net_num_from_nn(nn))
  1233. goto out;
  1234. node = o2nm_get_node_by_num(o2net_num_from_nn(nn));
  1235. if (node == NULL) {
  1236. ret = 0;
  1237. goto out;
  1238. }
  1239. mynode = o2nm_get_node_by_num(o2nm_this_node());
  1240. if (mynode == NULL) {
  1241. ret = 0;
  1242. goto out;
  1243. }
  1244. spin_lock(&nn->nn_lock);
  1245. timeout = atomic_read(&nn->nn_timeout);
  1246. stop = (nn->nn_sc ||
  1247. (nn->nn_persistent_error &&
  1248. (nn->nn_persistent_error != -ENOTCONN || timeout == 0)));
  1249. spin_unlock(&nn->nn_lock);
  1250. if (stop)
  1251. goto out;
  1252. nn->nn_last_connect_attempt = jiffies;
  1253. sc = sc_alloc(node);
  1254. if (sc == NULL) {
  1255. mlog(0, "couldn't allocate sc\n");
  1256. ret = -ENOMEM;
  1257. goto out;
  1258. }
  1259. ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
  1260. if (ret < 0) {
  1261. mlog(0, "can't create socket: %d\n", ret);
  1262. goto out;
  1263. }
  1264. sc->sc_sock = sock;
  1265. sock->sk->sk_allocation = GFP_ATOMIC;
  1266. myaddr.sin_family = AF_INET;
  1267. myaddr.sin_addr.s_addr = mynode->nd_ipv4_address;
  1268. myaddr.sin_port = htons(0);
  1269. ret = sock->ops->bind(sock, (struct sockaddr *)&myaddr,
  1270. sizeof(myaddr));
  1271. if (ret) {
  1272. mlog(ML_ERROR, "bind failed with %d at address %pI4\n",
  1273. ret, &mynode->nd_ipv4_address);
  1274. goto out;
  1275. }
  1276. ret = o2net_set_nodelay(sc->sc_sock);
  1277. if (ret) {
  1278. mlog(ML_ERROR, "setting TCP_NODELAY failed with %d\n", ret);
  1279. goto out;
  1280. }
  1281. o2net_register_callbacks(sc->sc_sock->sk, sc);
  1282. spin_lock(&nn->nn_lock);
  1283. o2net_set_nn_state(nn, sc, 0, 0);
  1284. spin_unlock(&nn->nn_lock);
  1285. remoteaddr.sin_family = AF_INET;
  1286. remoteaddr.sin_addr.s_addr = node->nd_ipv4_address;
  1287. remoteaddr.sin_port = node->nd_ipv4_port;
  1288. ret = sc->sc_sock->ops->connect(sc->sc_sock,
  1289. (struct sockaddr *)&remoteaddr,
  1290. sizeof(remoteaddr),
  1291. O_NONBLOCK);
  1292. if (ret == -EINPROGRESS)
  1293. ret = 0;
  1294. out:
  1295. if (ret) {
  1296. printk(KERN_NOTICE "o2net: Connect attempt to " SC_NODEF_FMT
  1297. " failed with errno %d\n", SC_NODEF_ARGS(sc), ret);
  1298. if (sc)
  1299. o2net_ensure_shutdown(nn, sc, 0);
  1300. }
  1301. if (sc)
  1302. sc_put(sc);
  1303. if (node)
  1304. o2nm_node_put(node);
  1305. if (mynode)
  1306. o2nm_node_put(mynode);
  1307. return;
  1308. }
  1309. static void o2net_connect_expired(struct work_struct *work)
  1310. {
  1311. struct o2net_node *nn =
  1312. container_of(work, struct o2net_node, nn_connect_expired.work);
  1313. spin_lock(&nn->nn_lock);
  1314. if (!nn->nn_sc_valid) {
  1315. printk(KERN_NOTICE "o2net: No connection established with "
  1316. "node %u after %u.%u seconds, giving up.\n",
  1317. o2net_num_from_nn(nn),
  1318. o2net_idle_timeout() / 1000,
  1319. o2net_idle_timeout() % 1000);
  1320. o2net_set_nn_state(nn, NULL, 0, -ENOTCONN);
  1321. }
  1322. spin_unlock(&nn->nn_lock);
  1323. }
  1324. static void o2net_still_up(struct work_struct *work)
  1325. {
  1326. struct o2net_node *nn =
  1327. container_of(work, struct o2net_node, nn_still_up.work);
  1328. o2quo_hb_still_up(o2net_num_from_nn(nn));
  1329. }
  1330. void o2net_disconnect_node(struct o2nm_node *node)
  1331. {
  1332. struct o2net_node *nn = o2net_nn_from_num(node->nd_num);
  1333. spin_lock(&nn->nn_lock);
  1334. atomic_set(&nn->nn_timeout, 0);
  1335. o2net_set_nn_state(nn, NULL, 0, -ENOTCONN);
  1336. spin_unlock(&nn->nn_lock);
  1337. if (o2net_wq) {
  1338. cancel_delayed_work(&nn->nn_connect_expired);
  1339. cancel_delayed_work(&nn->nn_connect_work);
  1340. cancel_delayed_work(&nn->nn_still_up);
  1341. flush_workqueue(o2net_wq);
  1342. }
  1343. }
  1344. static void o2net_hb_node_down_cb(struct o2nm_node *node, int node_num,
  1345. void *data)
  1346. {
  1347. o2quo_hb_down(node_num);
  1348. if (!node)
  1349. return;
  1350. if (node_num != o2nm_this_node())
  1351. o2net_disconnect_node(node);
  1352. BUG_ON(atomic_read(&o2net_connected_peers) < 0);
  1353. }
  1354. static void o2net_hb_node_up_cb(struct o2nm_node *node, int node_num,
  1355. void *data)
  1356. {
  1357. struct o2net_node *nn = o2net_nn_from_num(node_num);
  1358. o2quo_hb_up(node_num);
  1359. BUG_ON(!node);
  1360. nn->nn_last_connect_attempt = jiffies -
  1361. (msecs_to_jiffies(o2net_reconnect_delay()) + 1);
  1362. if (node_num != o2nm_this_node()) {
  1363. spin_lock(&nn->nn_lock);
  1364. atomic_set(&nn->nn_timeout, 0);
  1365. if (nn->nn_persistent_error)
  1366. o2net_set_nn_state(nn, NULL, 0, 0);
  1367. spin_unlock(&nn->nn_lock);
  1368. }
  1369. }
  1370. void o2net_unregister_hb_callbacks(void)
  1371. {
  1372. o2hb_unregister_callback(NULL, &o2net_hb_up);
  1373. o2hb_unregister_callback(NULL, &o2net_hb_down);
  1374. }
  1375. int o2net_register_hb_callbacks(void)
  1376. {
  1377. int ret;
  1378. o2hb_setup_callback(&o2net_hb_down, O2HB_NODE_DOWN_CB,
  1379. o2net_hb_node_down_cb, NULL, O2NET_HB_PRI);
  1380. o2hb_setup_callback(&o2net_hb_up, O2HB_NODE_UP_CB,
  1381. o2net_hb_node_up_cb, NULL, O2NET_HB_PRI);
  1382. ret = o2hb_register_callback(NULL, &o2net_hb_up);
  1383. if (ret == 0)
  1384. ret = o2hb_register_callback(NULL, &o2net_hb_down);
  1385. if (ret)
  1386. o2net_unregister_hb_callbacks();
  1387. return ret;
  1388. }
  1389. static int o2net_accept_one(struct socket *sock)
  1390. {
  1391. int ret, slen;
  1392. struct sockaddr_in sin;
  1393. struct socket *new_sock = NULL;
  1394. struct o2nm_node *node = NULL;
  1395. struct o2nm_node *local_node = NULL;
  1396. struct o2net_sock_container *sc = NULL;
  1397. struct o2net_node *nn;
  1398. BUG_ON(sock == NULL);
  1399. ret = sock_create_lite(sock->sk->sk_family, sock->sk->sk_type,
  1400. sock->sk->sk_protocol, &new_sock);
  1401. if (ret)
  1402. goto out;
  1403. new_sock->type = sock->type;
  1404. new_sock->ops = sock->ops;
  1405. ret = sock->ops->accept(sock, new_sock, O_NONBLOCK);
  1406. if (ret < 0)
  1407. goto out;
  1408. new_sock->sk->sk_allocation = GFP_ATOMIC;
  1409. ret = o2net_set_nodelay(new_sock);
  1410. if (ret) {
  1411. mlog(ML_ERROR, "setting TCP_NODELAY failed with %d\n", ret);
  1412. goto out;
  1413. }
  1414. slen = sizeof(sin);
  1415. ret = new_sock->ops->getname(new_sock, (struct sockaddr *) &sin,
  1416. &slen, 1);
  1417. if (ret < 0)
  1418. goto out;
  1419. node = o2nm_get_node_by_ip(sin.sin_addr.s_addr);
  1420. if (node == NULL) {
  1421. printk(KERN_NOTICE "o2net: Attempt to connect from unknown "
  1422. "node at %pI4:%d\n", &sin.sin_addr.s_addr,
  1423. ntohs(sin.sin_port));
  1424. ret = -EINVAL;
  1425. goto out;
  1426. }
  1427. if (o2nm_this_node() >= node->nd_num) {
  1428. local_node = o2nm_get_node_by_num(o2nm_this_node());
  1429. printk(KERN_NOTICE "o2net: Unexpected connect attempt seen "
  1430. "at node '%s' (%u, %pI4:%d) from node '%s' (%u, "
  1431. "%pI4:%d)\n", local_node->nd_name, local_node->nd_num,
  1432. &(local_node->nd_ipv4_address),
  1433. ntohs(local_node->nd_ipv4_port), node->nd_name,
  1434. node->nd_num, &sin.sin_addr.s_addr, ntohs(sin.sin_port));
  1435. ret = -EINVAL;
  1436. goto out;
  1437. }
  1438. if (!o2hb_check_node_heartbeating_from_callback(node->nd_num)) {
  1439. mlog(ML_CONN, "attempt to connect from node '%s' at "
  1440. "%pI4:%d but it isn't heartbeating\n",
  1441. node->nd_name, &sin.sin_addr.s_addr,
  1442. ntohs(sin.sin_port));
  1443. ret = -EINVAL;
  1444. goto out;
  1445. }
  1446. nn = o2net_nn_from_num(node->nd_num);
  1447. spin_lock(&nn->nn_lock);
  1448. if (nn->nn_sc)
  1449. ret = -EBUSY;
  1450. else
  1451. ret = 0;
  1452. spin_unlock(&nn->nn_lock);
  1453. if (ret) {
  1454. printk(KERN_NOTICE "o2net: Attempt to connect from node '%s' "
  1455. "at %pI4:%d but it already has an open connection\n",
  1456. node->nd_name, &sin.sin_addr.s_addr,
  1457. ntohs(sin.sin_port));
  1458. goto out;
  1459. }
  1460. sc = sc_alloc(node);
  1461. if (sc == NULL) {
  1462. ret = -ENOMEM;
  1463. goto out;
  1464. }
  1465. sc->sc_sock = new_sock;
  1466. new_sock = NULL;
  1467. spin_lock(&nn->nn_lock);
  1468. atomic_set(&nn->nn_timeout, 0);
  1469. o2net_set_nn_state(nn, sc, 0, 0);
  1470. spin_unlock(&nn->nn_lock);
  1471. o2net_register_callbacks(sc->sc_sock->sk, sc);
  1472. o2net_sc_queue_work(sc, &sc->sc_rx_work);
  1473. o2net_initialize_handshake();
  1474. o2net_sendpage(sc, o2net_hand, sizeof(*o2net_hand));
  1475. out:
  1476. if (new_sock)
  1477. sock_release(new_sock);
  1478. if (node)
  1479. o2nm_node_put(node);
  1480. if (local_node)
  1481. o2nm_node_put(local_node);
  1482. if (sc)
  1483. sc_put(sc);
  1484. return ret;
  1485. }
  1486. static void o2net_accept_many(struct work_struct *work)
  1487. {
  1488. struct socket *sock = o2net_listen_sock;
  1489. while (o2net_accept_one(sock) == 0)
  1490. cond_resched();
  1491. }
  1492. static void o2net_listen_data_ready(struct sock *sk, int bytes)
  1493. {
  1494. void (*ready)(struct sock *sk, int bytes);
  1495. read_lock(&sk->sk_callback_lock);
  1496. ready = sk->sk_user_data;
  1497. if (ready == NULL) {
  1498. ready = sk->sk_data_ready;
  1499. goto out;
  1500. }
  1501. if (sk->sk_state == TCP_LISTEN) {
  1502. mlog(ML_TCP, "bytes: %d\n", bytes);
  1503. queue_work(o2net_wq, &o2net_listen_work);
  1504. }
  1505. out:
  1506. read_unlock(&sk->sk_callback_lock);
  1507. ready(sk, bytes);
  1508. }
  1509. static int o2net_open_listening_sock(__be32 addr, __be16 port)
  1510. {
  1511. struct socket *sock = NULL;
  1512. int ret;
  1513. struct sockaddr_in sin = {
  1514. .sin_family = PF_INET,
  1515. .sin_addr = { .s_addr = addr },
  1516. .sin_port = port,
  1517. };
  1518. ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
  1519. if (ret < 0) {
  1520. printk(KERN_ERR "o2net: Error %d while creating socket\n", ret);
  1521. goto out;
  1522. }
  1523. sock->sk->sk_allocation = GFP_ATOMIC;
  1524. write_lock_bh(&sock->sk->sk_callback_lock);
  1525. sock->sk->sk_user_data = sock->sk->sk_data_ready;
  1526. sock->sk->sk_data_ready = o2net_listen_data_ready;
  1527. write_unlock_bh(&sock->sk->sk_callback_lock);
  1528. o2net_listen_sock = sock;
  1529. INIT_WORK(&o2net_listen_work, o2net_accept_many);
  1530. sock->sk->sk_reuse = 1;
  1531. ret = sock->ops->bind(sock, (struct sockaddr *)&sin, sizeof(sin));
  1532. if (ret < 0) {
  1533. printk(KERN_ERR "o2net: Error %d while binding socket at "
  1534. "%pI4:%u\n", ret, &addr, ntohs(port));
  1535. goto out;
  1536. }
  1537. ret = sock->ops->listen(sock, 64);
  1538. if (ret < 0)
  1539. printk(KERN_ERR "o2net: Error %d while listening on %pI4:%u\n",
  1540. ret, &addr, ntohs(port));
  1541. out:
  1542. if (ret) {
  1543. o2net_listen_sock = NULL;
  1544. if (sock)
  1545. sock_release(sock);
  1546. }
  1547. return ret;
  1548. }
  1549. int o2net_start_listening(struct o2nm_node *node)
  1550. {
  1551. int ret = 0;
  1552. BUG_ON(o2net_wq != NULL);
  1553. BUG_ON(o2net_listen_sock != NULL);
  1554. mlog(ML_KTHREAD, "starting o2net thread...\n");
  1555. o2net_wq = create_singlethread_workqueue("o2net");
  1556. if (o2net_wq == NULL) {
  1557. mlog(ML_ERROR, "unable to launch o2net thread\n");
  1558. return -ENOMEM;
  1559. }
  1560. ret = o2net_open_listening_sock(node->nd_ipv4_address,
  1561. node->nd_ipv4_port);
  1562. if (ret) {
  1563. destroy_workqueue(o2net_wq);
  1564. o2net_wq = NULL;
  1565. } else
  1566. o2quo_conn_up(node->nd_num);
  1567. return ret;
  1568. }
  1569. void o2net_stop_listening(struct o2nm_node *node)
  1570. {
  1571. struct socket *sock = o2net_listen_sock;
  1572. size_t i;
  1573. BUG_ON(o2net_wq == NULL);
  1574. BUG_ON(o2net_listen_sock == NULL);
  1575. write_lock_bh(&sock->sk->sk_callback_lock);
  1576. sock->sk->sk_data_ready = sock->sk->sk_user_data;
  1577. sock->sk->sk_user_data = NULL;
  1578. write_unlock_bh(&sock->sk->sk_callback_lock);
  1579. for (i = 0; i < ARRAY_SIZE(o2net_nodes); i++) {
  1580. struct o2nm_node *node = o2nm_get_node_by_num(i);
  1581. if (node) {
  1582. o2net_disconnect_node(node);
  1583. o2nm_node_put(node);
  1584. }
  1585. }
  1586. mlog(ML_KTHREAD, "waiting for o2net thread to exit....\n");
  1587. destroy_workqueue(o2net_wq);
  1588. o2net_wq = NULL;
  1589. sock_release(o2net_listen_sock);
  1590. o2net_listen_sock = NULL;
  1591. o2quo_conn_err(node->nd_num);
  1592. }
  1593. int o2net_init(void)
  1594. {
  1595. unsigned long i;
  1596. o2quo_init();
  1597. if (o2net_debugfs_init())
  1598. return -ENOMEM;
  1599. o2net_hand = kzalloc(sizeof(struct o2net_handshake), GFP_KERNEL);
  1600. o2net_keep_req = kzalloc(sizeof(struct o2net_msg), GFP_KERNEL);
  1601. o2net_keep_resp = kzalloc(sizeof(struct o2net_msg), GFP_KERNEL);
  1602. if (!o2net_hand || !o2net_keep_req || !o2net_keep_resp) {
  1603. kfree(o2net_hand);
  1604. kfree(o2net_keep_req);
  1605. kfree(o2net_keep_resp);
  1606. return -ENOMEM;
  1607. }
  1608. o2net_hand->protocol_version = cpu_to_be64(O2NET_PROTOCOL_VERSION);
  1609. o2net_hand->connector_id = cpu_to_be64(1);
  1610. o2net_keep_req->magic = cpu_to_be16(O2NET_MSG_KEEP_REQ_MAGIC);
  1611. o2net_keep_resp->magic = cpu_to_be16(O2NET_MSG_KEEP_RESP_MAGIC);
  1612. for (i = 0; i < ARRAY_SIZE(o2net_nodes); i++) {

Large files files are truncated, but you can click here to view the full file