Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /* -*- mode: c; c-basic-offset: 8; -*-
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * vim: noexpandtab sw=8 ts=8 sts=0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  * Copyright (C) 2004 Oracle.  All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * ----
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  * Callers for this were originally written against a very simple synchronus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  * API.  This implementation reflects those simple callers.  Some day I'm sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  * we'll need to move to a more robust posting/callback mechanism.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  * Transmit calls pass in kernel virtual addresses and block copying this into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  * the socket's tx buffers via a usual blocking sendmsg.  They'll block waiting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  * for a failed socket to timeout.  TX callers can also pass in a poniter to an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  * 'int' which gets filled with an errno off the wire in response to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  * message they send.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  * Handlers for unsolicited messages are registered.  Each socket has a page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)  * that incoming data is copied into.  First the header, then the data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)  * Handlers are called from only one thread with a reference to this per-socket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23)  * page.  This page is destroyed after the handler call, so it can't be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24)  * referenced beyond the call.  Handlers may block but are discouraged from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25)  * doing so.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  * Any framing errors (bad magic, large payload lengths) close a connection.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29)  * Our sock_container holds the state we associate with a socket.  It's current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30)  * framing state is held there as well as the refcounting we do around when it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31)  * is safe to tear down the socket.  The socket is only finally torn down from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32)  * the container when the container loses all of its references -- so as long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33)  * as you hold a ref on the container you can trust that the socket is valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34)  * for use with kernel socket APIs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36)  * Connections are initiated between a pair of nodes when the node with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37)  * higher node number gets a heartbeat callback which indicates that the lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38)  * numbered node has started heartbeating.  The lower numbered node is passive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39)  * and only accepts the connection if the higher numbered node is heartbeating.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #include <linux/sched/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) #include <linux/kref.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) #include <linux/net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) #include <net/tcp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) #include "heartbeat.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) #include "tcp.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) #include "nodemanager.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) #define MLOG_MASK_PREFIX ML_TCP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) #include "masklog.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) #include "quorum.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) #include "tcp_internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) #define SC_NODEF_FMT "node %s (num %u) at %pI4:%u"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) #define SC_NODEF_ARGS(sc) sc->sc_node->nd_name, sc->sc_node->nd_num,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 			  &sc->sc_node->nd_ipv4_address,		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 			  ntohs(sc->sc_node->nd_ipv4_port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69)  * In the following two log macros, the whitespace after the ',' just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70)  * before ##args is intentional. Otherwise, gcc 2.95 will eat the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71)  * previous token if args expands to nothing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) #define msglog(hdr, fmt, args...) do {					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	typeof(hdr) __hdr = (hdr);					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	mlog(ML_MSG, "[mag %u len %u typ %u stat %d sys_stat %d "	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	     "key %08x num %u] " fmt,					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	     be16_to_cpu(__hdr->magic), be16_to_cpu(__hdr->data_len), 	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	     be16_to_cpu(__hdr->msg_type), be32_to_cpu(__hdr->status),	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	     be32_to_cpu(__hdr->sys_status), be32_to_cpu(__hdr->key),	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	     be32_to_cpu(__hdr->msg_num) ,  ##args);			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) #define sclog(sc, fmt, args...) do {					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	typeof(sc) __sc = (sc);						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	mlog(ML_SOCKET, "[sc %p refs %d sock %p node %u page %p "	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	     "pg_off %zu] " fmt, __sc,					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	     kref_read(&__sc->sc_kref), __sc->sc_sock,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	    __sc->sc_node->nd_num, __sc->sc_page, __sc->sc_page_off ,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	    ##args);							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) static DEFINE_RWLOCK(o2net_handler_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) static struct rb_root o2net_handler_tree = RB_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) static struct o2net_node o2net_nodes[O2NM_MAX_NODES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) /* XXX someday we'll need better accounting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) static struct socket *o2net_listen_sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101)  * listen work is only queued by the listening socket callbacks on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102)  * o2net_wq.  teardown detaches the callbacks before destroying the workqueue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103)  * quorum work is queued as sock containers are shutdown.. stop_listening
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104)  * tears down all the node's sock containers, preventing future shutdowns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105)  * and queued quroum work, before canceling delayed quorum work and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106)  * destroying the work queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) static struct workqueue_struct *o2net_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) static struct work_struct o2net_listen_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) static struct o2hb_callback_func o2net_hb_up, o2net_hb_down;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) #define O2NET_HB_PRI 0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) static struct o2net_handshake *o2net_hand;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) static struct o2net_msg *o2net_keep_req, *o2net_keep_resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) static int o2net_sys_err_translations[O2NET_ERR_MAX] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 		{[O2NET_ERR_NONE]	= 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 		 [O2NET_ERR_NO_HNDLR]	= -ENOPROTOOPT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 		 [O2NET_ERR_OVERFLOW]	= -EOVERFLOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 		 [O2NET_ERR_DIED]	= -EHOSTDOWN,};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) /* can't quite avoid *all* internal declarations :/ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) static void o2net_sc_connect_completed(struct work_struct *work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) static void o2net_rx_until_empty(struct work_struct *work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) static void o2net_shutdown_sc(struct work_struct *work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) static void o2net_listen_data_ready(struct sock *sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) static void o2net_sc_send_keep_req(struct work_struct *work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) static void o2net_idle_timer(struct timer_list *t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) static void o2net_sc_postpone_idle(struct o2net_sock_container *sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) static void o2net_sc_reset_idle_timer(struct o2net_sock_container *sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) #ifdef CONFIG_DEBUG_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) static void o2net_init_nst(struct o2net_send_tracking *nst, u32 msgtype,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 			   u32 msgkey, struct task_struct *task, u8 node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	INIT_LIST_HEAD(&nst->st_net_debug_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	nst->st_task = task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	nst->st_msg_type = msgtype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	nst->st_msg_key = msgkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	nst->st_node = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) static inline void o2net_set_nst_sock_time(struct o2net_send_tracking *nst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	nst->st_sock_time = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) static inline void o2net_set_nst_send_time(struct o2net_send_tracking *nst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	nst->st_send_time = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) static inline void o2net_set_nst_status_time(struct o2net_send_tracking *nst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	nst->st_status_time = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) static inline void o2net_set_nst_sock_container(struct o2net_send_tracking *nst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 						struct o2net_sock_container *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	nst->st_sc = sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) static inline void o2net_set_nst_msg_id(struct o2net_send_tracking *nst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 					u32 msg_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	nst->st_id = msg_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) static inline void o2net_set_sock_timer(struct o2net_sock_container *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	sc->sc_tv_timer = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) static inline void o2net_set_data_ready_time(struct o2net_sock_container *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	sc->sc_tv_data_ready = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) static inline void o2net_set_advance_start_time(struct o2net_sock_container *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	sc->sc_tv_advance_start = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) static inline void o2net_set_advance_stop_time(struct o2net_sock_container *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	sc->sc_tv_advance_stop = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) static inline void o2net_set_func_start_time(struct o2net_sock_container *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	sc->sc_tv_func_start = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) static inline void o2net_set_func_stop_time(struct o2net_sock_container *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	sc->sc_tv_func_stop = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) #else  /* CONFIG_DEBUG_FS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) # define o2net_init_nst(a, b, c, d, e)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) # define o2net_set_nst_sock_time(a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) # define o2net_set_nst_send_time(a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) # define o2net_set_nst_status_time(a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) # define o2net_set_nst_sock_container(a, b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) # define o2net_set_nst_msg_id(a, b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) # define o2net_set_sock_timer(a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) # define o2net_set_data_ready_time(a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) # define o2net_set_advance_start_time(a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) # define o2net_set_advance_stop_time(a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) # define o2net_set_func_start_time(a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) # define o2net_set_func_stop_time(a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) #endif /* CONFIG_DEBUG_FS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) #ifdef CONFIG_OCFS2_FS_STATS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) static ktime_t o2net_get_func_run_time(struct o2net_sock_container *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	return ktime_sub(sc->sc_tv_func_stop, sc->sc_tv_func_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) static void o2net_update_send_stats(struct o2net_send_tracking *nst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 				    struct o2net_sock_container *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	sc->sc_tv_status_total = ktime_add(sc->sc_tv_status_total,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 					   ktime_sub(ktime_get(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 						     nst->st_status_time));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	sc->sc_tv_send_total = ktime_add(sc->sc_tv_send_total,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 					 ktime_sub(nst->st_status_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 						   nst->st_send_time));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	sc->sc_tv_acquiry_total = ktime_add(sc->sc_tv_acquiry_total,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 					    ktime_sub(nst->st_send_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 						      nst->st_sock_time));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	sc->sc_send_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) static void o2net_update_recv_stats(struct o2net_sock_container *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	sc->sc_tv_process_total = ktime_add(sc->sc_tv_process_total,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 					    o2net_get_func_run_time(sc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	sc->sc_recv_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) # define o2net_update_send_stats(a, b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) # define o2net_update_recv_stats(sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) #endif /* CONFIG_OCFS2_FS_STATS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) static inline unsigned int o2net_reconnect_delay(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	return o2nm_single_cluster->cl_reconnect_delay_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) static inline unsigned int o2net_keepalive_delay(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	return o2nm_single_cluster->cl_keepalive_delay_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) static inline unsigned int o2net_idle_timeout(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	return o2nm_single_cluster->cl_idle_timeout_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) static inline int o2net_sys_err_to_errno(enum o2net_system_error err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	int trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	BUG_ON(err >= O2NET_ERR_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	trans = o2net_sys_err_translations[err];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	/* Just in case we mess up the translation table above */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	BUG_ON(err != O2NET_ERR_NONE && trans == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	return trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) static struct o2net_node * o2net_nn_from_num(u8 node_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	BUG_ON(node_num >= ARRAY_SIZE(o2net_nodes));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	return &o2net_nodes[node_num];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) static u8 o2net_num_from_nn(struct o2net_node *nn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	BUG_ON(nn == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	return nn - o2net_nodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) /* ------------------------------------------------------------ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) static int o2net_prep_nsw(struct o2net_node *nn, struct o2net_status_wait *nsw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	spin_lock(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	ret = idr_alloc(&nn->nn_status_idr, nsw, 0, 0, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	if (ret >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 		nsw->ns_id = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 		list_add_tail(&nsw->ns_node_item, &nn->nn_status_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	spin_unlock(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	init_waitqueue_head(&nsw->ns_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	nsw->ns_sys_status = O2NET_ERR_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	nsw->ns_status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) static void o2net_complete_nsw_locked(struct o2net_node *nn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 				      struct o2net_status_wait *nsw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 				      enum o2net_system_error sys_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 				      s32 status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	assert_spin_locked(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	if (!list_empty(&nsw->ns_node_item)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		list_del_init(&nsw->ns_node_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 		nsw->ns_sys_status = sys_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 		nsw->ns_status = status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		idr_remove(&nn->nn_status_idr, nsw->ns_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 		wake_up(&nsw->ns_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) static void o2net_complete_nsw(struct o2net_node *nn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 			       struct o2net_status_wait *nsw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 			       u64 id, enum o2net_system_error sys_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 			       s32 status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	spin_lock(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	if (nsw == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 		if (id > INT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 		nsw = idr_find(&nn->nn_status_idr, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 		if (nsw == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	o2net_complete_nsw_locked(nn, nsw, sys_status, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	spin_unlock(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) static void o2net_complete_nodes_nsw(struct o2net_node *nn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	struct o2net_status_wait *nsw, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	unsigned int num_kills = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	assert_spin_locked(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	list_for_each_entry_safe(nsw, tmp, &nn->nn_status_list, ns_node_item) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 		o2net_complete_nsw_locked(nn, nsw, O2NET_ERR_DIED, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 		num_kills++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	mlog(0, "completed %d messages for node %u\n", num_kills,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	     o2net_num_from_nn(nn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) static int o2net_nsw_completed(struct o2net_node *nn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 			       struct o2net_status_wait *nsw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	int completed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	spin_lock(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	completed = list_empty(&nsw->ns_node_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	spin_unlock(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	return completed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) /* ------------------------------------------------------------ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) static void sc_kref_release(struct kref *kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	struct o2net_sock_container *sc = container_of(kref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 					struct o2net_sock_container, sc_kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	BUG_ON(timer_pending(&sc->sc_idle_timeout));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	sclog(sc, "releasing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	if (sc->sc_sock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 		sock_release(sc->sc_sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 		sc->sc_sock = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	o2nm_undepend_item(&sc->sc_node->nd_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	o2nm_node_put(sc->sc_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	sc->sc_node = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	o2net_debug_del_sc(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	if (sc->sc_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 		__free_page(sc->sc_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	kfree(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) static void sc_put(struct o2net_sock_container *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	sclog(sc, "put\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	kref_put(&sc->sc_kref, sc_kref_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) static void sc_get(struct o2net_sock_container *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	sclog(sc, "get\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	kref_get(&sc->sc_kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) static struct o2net_sock_container *sc_alloc(struct o2nm_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	struct o2net_sock_container *sc, *ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	struct page *page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	int status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	page = alloc_page(GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	sc = kzalloc(sizeof(*sc), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	if (sc == NULL || page == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	kref_init(&sc->sc_kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	o2nm_node_get(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	sc->sc_node = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	/* pin the node item of the remote node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	status = o2nm_depend_item(&node->nd_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	if (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 		mlog_errno(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 		o2nm_node_put(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	INIT_WORK(&sc->sc_connect_work, o2net_sc_connect_completed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	INIT_WORK(&sc->sc_rx_work, o2net_rx_until_empty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	INIT_WORK(&sc->sc_shutdown_work, o2net_shutdown_sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	INIT_DELAYED_WORK(&sc->sc_keepalive_work, o2net_sc_send_keep_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	timer_setup(&sc->sc_idle_timeout, o2net_idle_timer, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	sclog(sc, "alloced\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	ret = sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	sc->sc_page = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	o2net_debug_add_sc(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	sc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	if (page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 		__free_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	kfree(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) /* ------------------------------------------------------------ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) static void o2net_sc_queue_work(struct o2net_sock_container *sc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 				struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	sc_get(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	if (!queue_work(o2net_wq, work))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 		sc_put(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) static void o2net_sc_queue_delayed_work(struct o2net_sock_container *sc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 					struct delayed_work *work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 					int delay)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	sc_get(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	if (!queue_delayed_work(o2net_wq, work, delay))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 		sc_put(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) static void o2net_sc_cancel_delayed_work(struct o2net_sock_container *sc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 					 struct delayed_work *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	if (cancel_delayed_work(work))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 		sc_put(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) static atomic_t o2net_connected_peers = ATOMIC_INIT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) int o2net_num_connected_peers(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	return atomic_read(&o2net_connected_peers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) static void o2net_set_nn_state(struct o2net_node *nn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 			       struct o2net_sock_container *sc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 			       unsigned valid, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	int was_valid = nn->nn_sc_valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	int was_err = nn->nn_persistent_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	struct o2net_sock_container *old_sc = nn->nn_sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	assert_spin_locked(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	if (old_sc && !sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 		atomic_dec(&o2net_connected_peers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	else if (!old_sc && sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		atomic_inc(&o2net_connected_peers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	/* the node num comparison and single connect/accept path should stop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	 * an non-null sc from being overwritten with another */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	BUG_ON(sc && nn->nn_sc && nn->nn_sc != sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	mlog_bug_on_msg(err && valid, "err %d valid %u\n", err, valid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	mlog_bug_on_msg(valid && !sc, "valid %u sc %p\n", valid, sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	if (was_valid && !valid && err == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		err = -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	mlog(ML_CONN, "node %u sc: %p -> %p, valid %u -> %u, err %d -> %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	     o2net_num_from_nn(nn), nn->nn_sc, sc, nn->nn_sc_valid, valid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	     nn->nn_persistent_error, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	nn->nn_sc = sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	nn->nn_sc_valid = valid ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	nn->nn_persistent_error = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	/* mirrors o2net_tx_can_proceed() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	if (nn->nn_persistent_error || nn->nn_sc_valid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 		wake_up(&nn->nn_sc_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	if (was_valid && !was_err && nn->nn_persistent_error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		o2quo_conn_err(o2net_num_from_nn(nn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 		queue_delayed_work(o2net_wq, &nn->nn_still_up,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 				   msecs_to_jiffies(O2NET_QUORUM_DELAY_MS));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	if (was_valid && !valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 		if (old_sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 			printk(KERN_NOTICE "o2net: No longer connected to "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 				SC_NODEF_FMT "\n", SC_NODEF_ARGS(old_sc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		o2net_complete_nodes_nsw(nn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	if (!was_valid && valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 		o2quo_conn_up(o2net_num_from_nn(nn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		cancel_delayed_work(&nn->nn_connect_expired);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		printk(KERN_NOTICE "o2net: %s " SC_NODEF_FMT "\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 		       o2nm_this_node() > sc->sc_node->nd_num ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 		       "Connected to" : "Accepted connection from",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 		       SC_NODEF_ARGS(sc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	/* trigger the connecting worker func as long as we're not valid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	 * it will back off if it shouldn't connect.  This can be called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	 * from node config teardown and so needs to be careful about
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	 * the work queue actually being up. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	if (!valid && o2net_wq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 		unsigned long delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 		/* delay if we're within a RECONNECT_DELAY of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 		 * last attempt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 		delay = (nn->nn_last_connect_attempt +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 			 msecs_to_jiffies(o2net_reconnect_delay()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 			- jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 		if (delay > msecs_to_jiffies(o2net_reconnect_delay()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 			delay = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 		mlog(ML_CONN, "queueing conn attempt in %lu jiffies\n", delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 		queue_delayed_work(o2net_wq, &nn->nn_connect_work, delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 		 * Delay the expired work after idle timeout.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 		 * We might have lots of failed connection attempts that run
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 		 * through here but we only cancel the connect_expired work when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		 * a connection attempt succeeds.  So only the first enqueue of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		 * the connect_expired work will do anything.  The rest will see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 		 * that it's already queued and do nothing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		delay += msecs_to_jiffies(o2net_idle_timeout());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 		queue_delayed_work(o2net_wq, &nn->nn_connect_expired, delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	/* keep track of the nn's sc ref for the caller */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	if ((old_sc == NULL) && sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 		sc_get(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	if (old_sc && (old_sc != sc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		o2net_sc_queue_work(old_sc, &old_sc->sc_shutdown_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 		sc_put(old_sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) /* see o2net_register_callbacks() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) static void o2net_data_ready(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	void (*ready)(struct sock *sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	struct o2net_sock_container *sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	read_lock_bh(&sk->sk_callback_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	sc = sk->sk_user_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	if (sc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 		sclog(sc, "data_ready hit\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 		o2net_set_data_ready_time(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 		o2net_sc_queue_work(sc, &sc->sc_rx_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 		ready = sc->sc_data_ready;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 		ready = sk->sk_data_ready;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	read_unlock_bh(&sk->sk_callback_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	ready(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) /* see o2net_register_callbacks() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) static void o2net_state_change(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	void (*state_change)(struct sock *sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	struct o2net_sock_container *sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	read_lock_bh(&sk->sk_callback_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	sc = sk->sk_user_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	if (sc == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		state_change = sk->sk_state_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	sclog(sc, "state_change to %d\n", sk->sk_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	state_change = sc->sc_state_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	switch(sk->sk_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	/* ignore connecting sockets as they make progress */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	case TCP_SYN_SENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	case TCP_SYN_RECV:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	case TCP_ESTABLISHED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		o2net_sc_queue_work(sc, &sc->sc_connect_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 		printk(KERN_INFO "o2net: Connection to " SC_NODEF_FMT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 			" shutdown, state %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 			SC_NODEF_ARGS(sc), sk->sk_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 		o2net_sc_queue_work(sc, &sc->sc_shutdown_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	read_unlock_bh(&sk->sk_callback_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	state_change(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643)  * we register callbacks so we can queue work on events before calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644)  * the original callbacks.  our callbacks our careful to test user_data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645)  * to discover when they've reaced with o2net_unregister_callbacks().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) static void o2net_register_callbacks(struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 				     struct o2net_sock_container *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	write_lock_bh(&sk->sk_callback_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	/* accepted sockets inherit the old listen socket data ready */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	if (sk->sk_data_ready == o2net_listen_data_ready) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 		sk->sk_data_ready = sk->sk_user_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 		sk->sk_user_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	BUG_ON(sk->sk_user_data != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	sk->sk_user_data = sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	sc_get(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	sc->sc_data_ready = sk->sk_data_ready;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	sc->sc_state_change = sk->sk_state_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	sk->sk_data_ready = o2net_data_ready;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	sk->sk_state_change = o2net_state_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	mutex_init(&sc->sc_send_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	write_unlock_bh(&sk->sk_callback_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) static int o2net_unregister_callbacks(struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 			           struct o2net_sock_container *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	write_lock_bh(&sk->sk_callback_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	if (sk->sk_user_data == sc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 		ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 		sk->sk_user_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 		sk->sk_data_ready = sc->sc_data_ready;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 		sk->sk_state_change = sc->sc_state_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	write_unlock_bh(&sk->sk_callback_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690)  * this is a little helper that is called by callers who have seen a problem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691)  * with an sc and want to detach it from the nn if someone already hasn't beat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692)  * them to it.  if an error is given then the shutdown will be persistent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693)  * and pending transmits will be canceled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) static void o2net_ensure_shutdown(struct o2net_node *nn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 			           struct o2net_sock_container *sc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 				   int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	spin_lock(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	if (nn->nn_sc == sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 		o2net_set_nn_state(nn, NULL, 0, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	spin_unlock(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706)  * This work queue function performs the blocking parts of socket shutdown.  A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707)  * few paths lead here.  set_nn_state will trigger this callback if it sees an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708)  * sc detached from the nn.  state_change will also trigger this callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709)  * directly when it sees errors.  In that case we need to call set_nn_state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710)  * ourselves as state_change couldn't get the nn_lock and call set_nn_state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711)  * itself.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) static void o2net_shutdown_sc(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	struct o2net_sock_container *sc =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 		container_of(work, struct o2net_sock_container,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 			     sc_shutdown_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	sclog(sc, "shutting down\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	/* drop the callbacks ref and call shutdown only once */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	if (o2net_unregister_callbacks(sc->sc_sock->sk, sc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		/* we shouldn't flush as we're in the thread, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		 * races with pending sc work structs are harmless */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		del_timer_sync(&sc->sc_idle_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		o2net_sc_cancel_delayed_work(sc, &sc->sc_keepalive_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 		sc_put(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 		kernel_sock_shutdown(sc->sc_sock, SHUT_RDWR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	/* not fatal so failed connects before the other guy has our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	 * heartbeat can be retried */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	o2net_ensure_shutdown(nn, sc, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	sc_put(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) /* ------------------------------------------------------------ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) static int o2net_handler_cmp(struct o2net_msg_handler *nmh, u32 msg_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 			     u32 key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	int ret = memcmp(&nmh->nh_key, &key, sizeof(key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 		ret = memcmp(&nmh->nh_msg_type, &msg_type, sizeof(msg_type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) static struct o2net_msg_handler *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) o2net_handler_tree_lookup(u32 msg_type, u32 key, struct rb_node ***ret_p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 			  struct rb_node **ret_parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	struct rb_node **p = &o2net_handler_tree.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	struct rb_node *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	struct o2net_msg_handler *nmh, *ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	int cmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	while (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 		parent = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 		nmh = rb_entry(parent, struct o2net_msg_handler, nh_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 		cmp = o2net_handler_cmp(nmh, msg_type, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 		if (cmp < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 			p = &(*p)->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 		else if (cmp > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 			p = &(*p)->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 			ret = nmh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	if (ret_p != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 		*ret_p = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	if (ret_parent != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 		*ret_parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) static void o2net_handler_kref_release(struct kref *kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	struct o2net_msg_handler *nmh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	nmh = container_of(kref, struct o2net_msg_handler, nh_kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	kfree(nmh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) static void o2net_handler_put(struct o2net_msg_handler *nmh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	kref_put(&nmh->nh_kref, o2net_handler_kref_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) /* max_len is protection for the handler func.  incoming messages won't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797)  * be given to the handler if their payload is longer than the max. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) int o2net_register_handler(u32 msg_type, u32 key, u32 max_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 			   o2net_msg_handler_func *func, void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 			   o2net_post_msg_handler_func *post_func,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 			   struct list_head *unreg_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	struct o2net_msg_handler *nmh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	struct rb_node **p, *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	if (max_len > O2NET_MAX_PAYLOAD_BYTES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 		mlog(0, "max_len for message handler out of range: %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 			max_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	if (!msg_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 		mlog(0, "no message type provided: %u, %p\n", msg_type, func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	if (!func) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		mlog(0, "no message handler provided: %u, %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		       msg_type, func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827)        	nmh = kzalloc(sizeof(struct o2net_msg_handler), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	if (nmh == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	nmh->nh_func = func;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	nmh->nh_func_data = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	nmh->nh_post_func = post_func;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	nmh->nh_msg_type = msg_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	nmh->nh_max_len = max_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	nmh->nh_key = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	/* the tree and list get this ref.. they're both removed in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	 * unregister when this ref is dropped */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	kref_init(&nmh->nh_kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	INIT_LIST_HEAD(&nmh->nh_unregister_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	write_lock(&o2net_handler_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	if (o2net_handler_tree_lookup(msg_type, key, &p, &parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 		ret = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	        rb_link_node(&nmh->nh_node, parent, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 		rb_insert_color(&nmh->nh_node, &o2net_handler_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 		list_add_tail(&nmh->nh_unregister_item, unreg_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 		mlog(ML_TCP, "registered handler func %p type %u key %08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 		     func, msg_type, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 		/* we've had some trouble with handlers seemingly vanishing. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 		mlog_bug_on_msg(o2net_handler_tree_lookup(msg_type, key, &p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 							  &parent) == NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 			        "couldn't find handler we *just* registered "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 				"for type %u key %08x\n", msg_type, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	write_unlock(&o2net_handler_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 		kfree(nmh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) EXPORT_SYMBOL_GPL(o2net_register_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) void o2net_unregister_handler_list(struct list_head *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	struct o2net_msg_handler *nmh, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	write_lock(&o2net_handler_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	list_for_each_entry_safe(nmh, n, list, nh_unregister_item) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 		mlog(ML_TCP, "unregistering handler func %p type %u key %08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		     nmh->nh_func, nmh->nh_msg_type, nmh->nh_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		rb_erase(&nmh->nh_node, &o2net_handler_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		list_del_init(&nmh->nh_unregister_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		kref_put(&nmh->nh_kref, o2net_handler_kref_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	write_unlock(&o2net_handler_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) EXPORT_SYMBOL_GPL(o2net_unregister_handler_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) static struct o2net_msg_handler *o2net_handler_get(u32 msg_type, u32 key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	struct o2net_msg_handler *nmh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	read_lock(&o2net_handler_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	nmh = o2net_handler_tree_lookup(msg_type, key, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	if (nmh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		kref_get(&nmh->nh_kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	read_unlock(&o2net_handler_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	return nmh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) /* ------------------------------------------------------------ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) static int o2net_recv_tcp_msg(struct socket *sock, void *data, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	struct kvec vec = { .iov_len = len, .iov_base = data, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	struct msghdr msg = { .msg_flags = MSG_DONTWAIT, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	iov_iter_kvec(&msg.msg_iter, READ, &vec, 1, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	return sock_recvmsg(sock, &msg, MSG_DONTWAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) static int o2net_send_tcp_msg(struct socket *sock, struct kvec *vec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 			      size_t veclen, size_t total)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	struct msghdr msg = {.msg_flags = 0,};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	if (sock == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	ret = kernel_sendmsg(sock, &msg, vec, veclen, total);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	if (likely(ret == total))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	mlog(ML_ERROR, "sendmsg returned %d instead of %zu\n", ret, total);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 		ret = -EPIPE; /* should be smarter, I bet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	mlog(0, "returning error: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) static void o2net_sendpage(struct o2net_sock_container *sc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 			   void *kmalloced_virt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 			   size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 		mutex_lock(&sc->sc_send_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		ret = sc->sc_sock->ops->sendpage(sc->sc_sock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 						 virt_to_page(kmalloced_virt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 						 offset_in_page(kmalloced_virt),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 						 size, MSG_DONTWAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		mutex_unlock(&sc->sc_send_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		if (ret == size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		if (ret == (ssize_t)-EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 			mlog(0, "sendpage of size %zu to " SC_NODEF_FMT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 			     " returned EAGAIN\n", size, SC_NODEF_ARGS(sc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 			cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 		mlog(ML_ERROR, "sendpage of size %zu to " SC_NODEF_FMT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		     " failed with %zd\n", size, SC_NODEF_ARGS(sc), ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 		o2net_ensure_shutdown(nn, sc, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) static void o2net_init_msg(struct o2net_msg *msg, u16 data_len, u16 msg_type, u32 key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	memset(msg, 0, sizeof(struct o2net_msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	msg->magic = cpu_to_be16(O2NET_MSG_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	msg->data_len = cpu_to_be16(data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	msg->msg_type = cpu_to_be16(msg_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	msg->sys_status = cpu_to_be32(O2NET_ERR_NONE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	msg->status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	msg->key = cpu_to_be32(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) static int o2net_tx_can_proceed(struct o2net_node *nn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 			        struct o2net_sock_container **sc_ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 				int *error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	spin_lock(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	if (nn->nn_persistent_error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 		ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 		*sc_ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		*error = nn->nn_persistent_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	} else if (nn->nn_sc_valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		kref_get(&nn->nn_sc->sc_kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 		ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 		*sc_ret = nn->nn_sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 		*error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	spin_unlock(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) /* Get a map of all nodes to which this node is currently connected to */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) void o2net_fill_node_map(unsigned long *map, unsigned bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	struct o2net_sock_container *sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	int node, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	BUG_ON(bytes < (BITS_TO_LONGS(O2NM_MAX_NODES) * sizeof(unsigned long)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	memset(map, 0, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	for (node = 0; node < O2NM_MAX_NODES; ++node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 		if (!o2net_tx_can_proceed(o2net_nn_from_num(node), &sc, &ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 		if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 			set_bit(node, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 			sc_put(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) EXPORT_SYMBOL_GPL(o2net_fill_node_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) int o2net_send_message_vec(u32 msg_type, u32 key, struct kvec *caller_vec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 			   size_t caller_veclen, u8 target_node, int *status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	struct o2net_msg *msg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	size_t veclen, caller_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	struct kvec *vec = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	struct o2net_sock_container *sc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	struct o2net_node *nn = o2net_nn_from_num(target_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	struct o2net_status_wait nsw = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 		.ns_node_item = LIST_HEAD_INIT(nsw.ns_node_item),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	struct o2net_send_tracking nst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	o2net_init_nst(&nst, msg_type, key, current, target_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	if (o2net_wq == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 		mlog(0, "attempt to tx without o2netd running\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 		ret = -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	if (caller_veclen == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 		mlog(0, "bad kvec array length\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	caller_bytes = iov_length((struct iovec *)caller_vec, caller_veclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	if (caller_bytes > O2NET_MAX_PAYLOAD_BYTES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 		mlog(0, "total payload len %zu too large\n", caller_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	if (target_node == o2nm_this_node()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 		ret = -ELOOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	o2net_debug_add_nst(&nst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	o2net_set_nst_sock_time(&nst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	wait_event(nn->nn_sc_wq, o2net_tx_can_proceed(nn, &sc, &ret));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	o2net_set_nst_sock_container(&nst, sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	veclen = caller_veclen + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	vec = kmalloc_array(veclen, sizeof(struct kvec), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	if (vec == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 		mlog(0, "failed to %zu element kvec!\n", veclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	msg = kmalloc(sizeof(struct o2net_msg), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	if (!msg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 		mlog(0, "failed to allocate a o2net_msg!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	o2net_init_msg(msg, caller_bytes, msg_type, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	vec[0].iov_len = sizeof(struct o2net_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	vec[0].iov_base = msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	memcpy(&vec[1], caller_vec, caller_veclen * sizeof(struct kvec));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	ret = o2net_prep_nsw(nn, &nsw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	msg->msg_num = cpu_to_be32(nsw.ns_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	o2net_set_nst_msg_id(&nst, nsw.ns_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	o2net_set_nst_send_time(&nst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	/* finally, convert the message header to network byte-order
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	 * and send */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	mutex_lock(&sc->sc_send_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	ret = o2net_send_tcp_msg(sc->sc_sock, vec, veclen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 				 sizeof(struct o2net_msg) + caller_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	mutex_unlock(&sc->sc_send_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	msglog(msg, "sending returned %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 		mlog(0, "error returned from o2net_send_tcp_msg=%d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	/* wait on other node's handler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	o2net_set_nst_status_time(&nst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	wait_event(nsw.ns_wq, o2net_nsw_completed(nn, &nsw));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	o2net_update_send_stats(&nst, sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	/* Note that we avoid overwriting the callers status return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	 * variable if a system error was reported on the other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	 * side. Callers beware. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	ret = o2net_sys_err_to_errno(nsw.ns_sys_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	if (status && !ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 		*status = nsw.ns_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	mlog(0, "woken, returning system status %d, user status %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	     ret, nsw.ns_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	o2net_debug_del_nst(&nst); /* must be before dropping sc and node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	if (sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 		sc_put(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	kfree(vec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	kfree(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	o2net_complete_nsw(nn, &nsw, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) EXPORT_SYMBOL_GPL(o2net_send_message_vec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) int o2net_send_message(u32 msg_type, u32 key, void *data, u32 len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		       u8 target_node, int *status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	struct kvec vec = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 		.iov_base = data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 		.iov_len = len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	return o2net_send_message_vec(msg_type, key, &vec, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 				      target_node, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) EXPORT_SYMBOL_GPL(o2net_send_message);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) static int o2net_send_status_magic(struct socket *sock, struct o2net_msg *hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 				   enum o2net_system_error syserr, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	struct kvec vec = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 		.iov_base = hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 		.iov_len = sizeof(struct o2net_msg),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	BUG_ON(syserr >= O2NET_ERR_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	/* leave other fields intact from the incoming message, msg_num
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	 * in particular */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	hdr->sys_status = cpu_to_be32(syserr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	hdr->status = cpu_to_be32(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	hdr->magic = cpu_to_be16(O2NET_MSG_STATUS_MAGIC);  // twiddle the magic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	hdr->data_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	msglog(hdr, "about to send status magic %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	/* hdr has been in host byteorder this whole time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	return o2net_send_tcp_msg(sock, &vec, 1, sizeof(struct o2net_msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) /* this returns -errno if the header was unknown or too large, etc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167)  * after this is called the buffer us reused for the next message */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) static int o2net_process_message(struct o2net_sock_container *sc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 				 struct o2net_msg *hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	int ret = 0, handler_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	enum  o2net_system_error syserr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	struct o2net_msg_handler *nmh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	void *ret_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	msglog(hdr, "processing message\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	o2net_sc_postpone_idle(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	switch(be16_to_cpu(hdr->magic)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 		case O2NET_MSG_STATUS_MAGIC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 			/* special type for returning message status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 			o2net_complete_nsw(nn, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 					   be32_to_cpu(hdr->msg_num),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 					   be32_to_cpu(hdr->sys_status),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 					   be32_to_cpu(hdr->status));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 		case O2NET_MSG_KEEP_REQ_MAGIC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 			o2net_sendpage(sc, o2net_keep_resp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 				       sizeof(*o2net_keep_resp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 		case O2NET_MSG_KEEP_RESP_MAGIC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		case O2NET_MSG_MAGIC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 			msglog(hdr, "bad magic\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	/* find a handler for it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	handler_status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	nmh = o2net_handler_get(be16_to_cpu(hdr->msg_type),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 				be32_to_cpu(hdr->key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	if (!nmh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 		mlog(ML_TCP, "couldn't find handler for type %u key %08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 		     be16_to_cpu(hdr->msg_type), be32_to_cpu(hdr->key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 		syserr = O2NET_ERR_NO_HNDLR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 		goto out_respond;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	syserr = O2NET_ERR_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	if (be16_to_cpu(hdr->data_len) > nmh->nh_max_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 		syserr = O2NET_ERR_OVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	if (syserr != O2NET_ERR_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 		goto out_respond;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	o2net_set_func_start_time(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	sc->sc_msg_key = be32_to_cpu(hdr->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	sc->sc_msg_type = be16_to_cpu(hdr->msg_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	handler_status = (nmh->nh_func)(hdr, sizeof(struct o2net_msg) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 					     be16_to_cpu(hdr->data_len),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 					nmh->nh_func_data, &ret_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	o2net_set_func_stop_time(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	o2net_update_recv_stats(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) out_respond:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	/* this destroys the hdr, so don't use it after this */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 	mutex_lock(&sc->sc_send_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	ret = o2net_send_status_magic(sc->sc_sock, hdr, syserr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 				      handler_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	mutex_unlock(&sc->sc_send_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	hdr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	mlog(0, "sending handler status %d, syserr %d returned %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	     handler_status, syserr, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	if (nmh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 		BUG_ON(ret_data != NULL && nmh->nh_post_func == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 		if (nmh->nh_post_func)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 			(nmh->nh_post_func)(handler_status, nmh->nh_func_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 					    ret_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	if (nmh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 		o2net_handler_put(nmh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) static int o2net_check_handshake(struct o2net_sock_container *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	struct o2net_handshake *hand = page_address(sc->sc_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	if (hand->protocol_version != cpu_to_be64(O2NET_PROTOCOL_VERSION)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 		printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " Advertised net "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 		       "protocol version %llu but %llu is required. "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 		       "Disconnecting.\n", SC_NODEF_ARGS(sc),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 		       (unsigned long long)be64_to_cpu(hand->protocol_version),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 		       O2NET_PROTOCOL_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 		/* don't bother reconnecting if its the wrong version. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 		o2net_ensure_shutdown(nn, sc, -ENOTCONN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	 * Ensure timeouts are consistent with other nodes, otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	 * we can end up with one node thinking that the other must be down,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	 * but isn't. This can ultimately cause corruption.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	if (be32_to_cpu(hand->o2net_idle_timeout_ms) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 				o2net_idle_timeout()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 		printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " uses a network "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 		       "idle timeout of %u ms, but we use %u ms locally. "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 		       "Disconnecting.\n", SC_NODEF_ARGS(sc),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 		       be32_to_cpu(hand->o2net_idle_timeout_ms),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 		       o2net_idle_timeout());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 		o2net_ensure_shutdown(nn, sc, -ENOTCONN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 	if (be32_to_cpu(hand->o2net_keepalive_delay_ms) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 			o2net_keepalive_delay()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 		printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " uses a keepalive "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 		       "delay of %u ms, but we use %u ms locally. "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 		       "Disconnecting.\n", SC_NODEF_ARGS(sc),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 		       be32_to_cpu(hand->o2net_keepalive_delay_ms),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 		       o2net_keepalive_delay());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 		o2net_ensure_shutdown(nn, sc, -ENOTCONN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	if (be32_to_cpu(hand->o2hb_heartbeat_timeout_ms) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 			O2HB_MAX_WRITE_TIMEOUT_MS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 		printk(KERN_NOTICE "o2net: " SC_NODEF_FMT " uses a heartbeat "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 		       "timeout of %u ms, but we use %u ms locally. "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 		       "Disconnecting.\n", SC_NODEF_ARGS(sc),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 		       be32_to_cpu(hand->o2hb_heartbeat_timeout_ms),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 		       O2HB_MAX_WRITE_TIMEOUT_MS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 		o2net_ensure_shutdown(nn, sc, -ENOTCONN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	sc->sc_handshake_ok = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	spin_lock(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	/* set valid and queue the idle timers only if it hasn't been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	 * shut down already */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 	if (nn->nn_sc == sc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 		o2net_sc_reset_idle_timer(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 		atomic_set(&nn->nn_timeout, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 		o2net_set_nn_state(nn, sc, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	spin_unlock(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	/* shift everything up as though it wasn't there */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	sc->sc_page_off -= sizeof(struct o2net_handshake);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	if (sc->sc_page_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 		memmove(hand, hand + 1, sc->sc_page_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) /* this demuxes the queued rx bytes into header or payload bits and calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332)  * handlers as each full message is read off the socket.  it returns -error,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333)  * == 0 eof, or > 0 for progress made.*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) static int o2net_advance_rx(struct o2net_sock_container *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	struct o2net_msg *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 	void *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	size_t datalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	sclog(sc, "receiving\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	o2net_set_advance_start_time(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	if (unlikely(sc->sc_handshake_ok == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 		if(sc->sc_page_off < sizeof(struct o2net_handshake)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 			data = page_address(sc->sc_page) + sc->sc_page_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 			datalen = sizeof(struct o2net_handshake) - sc->sc_page_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 			ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 			if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 				sc->sc_page_off += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 		if (sc->sc_page_off == sizeof(struct o2net_handshake)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 			o2net_check_handshake(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 			if (unlikely(sc->sc_handshake_ok == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 				ret = -EPROTO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	/* do we need more header? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	if (sc->sc_page_off < sizeof(struct o2net_msg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 		data = page_address(sc->sc_page) + sc->sc_page_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 		datalen = sizeof(struct o2net_msg) - sc->sc_page_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 		ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 		if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 			sc->sc_page_off += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 			/* only swab incoming here.. we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 			 * only get here once as we cross from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 			 * being under to over */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 			if (sc->sc_page_off == sizeof(struct o2net_msg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 				hdr = page_address(sc->sc_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 				if (be16_to_cpu(hdr->data_len) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 				    O2NET_MAX_PAYLOAD_BYTES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 					ret = -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 		if (ret <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 	if (sc->sc_page_off < sizeof(struct o2net_msg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 		/* oof, still don't have a header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	/* this was swabbed above when we first read it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 	hdr = page_address(sc->sc_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	msglog(hdr, "at page_off %zu\n", sc->sc_page_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 	/* do we need more payload? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	if (sc->sc_page_off - sizeof(struct o2net_msg) < be16_to_cpu(hdr->data_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 		/* need more payload */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 		data = page_address(sc->sc_page) + sc->sc_page_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 		datalen = (sizeof(struct o2net_msg) + be16_to_cpu(hdr->data_len)) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 			  sc->sc_page_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 		ret = o2net_recv_tcp_msg(sc->sc_sock, data, datalen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 		if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 			sc->sc_page_off += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 		if (ret <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	if (sc->sc_page_off - sizeof(struct o2net_msg) == be16_to_cpu(hdr->data_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 		/* we can only get here once, the first time we read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 		 * the payload.. so set ret to progress if the handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 		 * works out. after calling this the message is toast */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 		ret = o2net_process_message(sc, hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 		if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 			ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 		sc->sc_page_off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	sclog(sc, "ret = %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	o2net_set_advance_stop_time(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) /* this work func is triggerd by data ready.  it reads until it can read no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422)  * more.  it interprets 0, eof, as fatal.  if data_ready hits while we're doing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423)  * our work the work struct will be marked and we'll be called again. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) static void o2net_rx_until_empty(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 	struct o2net_sock_container *sc =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 		container_of(work, struct o2net_sock_container, sc_rx_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 		ret = o2net_advance_rx(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 	} while (ret > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	if (ret <= 0 && ret != -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 		struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 		sclog(sc, "saw error %d, closing\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 		/* not permanent so read failed handshake can retry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 		o2net_ensure_shutdown(nn, sc, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 	sc_put(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) static void o2net_initialize_handshake(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	o2net_hand->o2hb_heartbeat_timeout_ms = cpu_to_be32(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 		O2HB_MAX_WRITE_TIMEOUT_MS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	o2net_hand->o2net_idle_timeout_ms = cpu_to_be32(o2net_idle_timeout());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	o2net_hand->o2net_keepalive_delay_ms = cpu_to_be32(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 		o2net_keepalive_delay());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	o2net_hand->o2net_reconnect_delay_ms = cpu_to_be32(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 		o2net_reconnect_delay());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) /* ------------------------------------------------------------ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) /* called when a connect completes and after a sock is accepted.  the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458)  * rx path will see the response and mark the sc valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) static void o2net_sc_connect_completed(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	struct o2net_sock_container *sc =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 		container_of(work, struct o2net_sock_container,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 			     sc_connect_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	mlog(ML_MSG, "sc sending handshake with ver %llu id %llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466)               (unsigned long long)O2NET_PROTOCOL_VERSION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	      (unsigned long long)be64_to_cpu(o2net_hand->connector_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	o2net_initialize_handshake();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 	o2net_sendpage(sc, o2net_hand, sizeof(*o2net_hand));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 	sc_put(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) /* this is called as a work_struct func. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) static void o2net_sc_send_keep_req(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	struct o2net_sock_container *sc =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 		container_of(work, struct o2net_sock_container,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 			     sc_keepalive_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	o2net_sendpage(sc, o2net_keep_req, sizeof(*o2net_keep_req));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	sc_put(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) /* socket shutdown does a del_timer_sync against this as it tears down.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486)  * we can't start this timer until we've got to the point in sc buildup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487)  * where shutdown is going to be involved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) static void o2net_idle_timer(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	struct o2net_sock_container *sc = from_timer(sc, t, sc_idle_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) #ifdef CONFIG_DEBUG_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	unsigned long msecs = ktime_to_ms(ktime_get()) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 		ktime_to_ms(sc->sc_tv_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	unsigned long msecs = o2net_idle_timeout();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	printk(KERN_NOTICE "o2net: Connection to " SC_NODEF_FMT " has been "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 	       "idle for %lu.%lu secs.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	       SC_NODEF_ARGS(sc), msecs / 1000, msecs % 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	/* idle timerout happen, don't shutdown the connection, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 	 * make fence decision. Maybe the connection can recover before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 	 * the decision is made.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 	atomic_set(&nn->nn_timeout, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	o2quo_conn_err(o2net_num_from_nn(nn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 	queue_delayed_work(o2net_wq, &nn->nn_still_up,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 			msecs_to_jiffies(O2NET_QUORUM_DELAY_MS));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	o2net_sc_reset_idle_timer(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) static void o2net_sc_reset_idle_timer(struct o2net_sock_container *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	o2net_sc_cancel_delayed_work(sc, &sc->sc_keepalive_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	o2net_sc_queue_delayed_work(sc, &sc->sc_keepalive_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 		      msecs_to_jiffies(o2net_keepalive_delay()));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	o2net_set_sock_timer(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	mod_timer(&sc->sc_idle_timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 	       jiffies + msecs_to_jiffies(o2net_idle_timeout()));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) static void o2net_sc_postpone_idle(struct o2net_sock_container *sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	struct o2net_node *nn = o2net_nn_from_num(sc->sc_node->nd_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	/* clear fence decision since the connection recover from timeout*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 	if (atomic_read(&nn->nn_timeout)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 		o2quo_conn_up(o2net_num_from_nn(nn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 		cancel_delayed_work(&nn->nn_still_up);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 		atomic_set(&nn->nn_timeout, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	/* Only push out an existing timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	if (timer_pending(&sc->sc_idle_timeout))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 		o2net_sc_reset_idle_timer(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) /* this work func is kicked whenever a path sets the nn state which doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543)  * have valid set.  This includes seeing hb come up, losing a connection,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544)  * having a connect attempt fail, etc. This centralizes the logic which decides
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545)  * if a connect attempt should be made or if we should give up and all future
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546)  * transmit attempts should fail */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) static void o2net_start_connect(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	struct o2net_node *nn =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 		container_of(work, struct o2net_node, nn_connect_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 	struct o2net_sock_container *sc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 	struct o2nm_node *node = NULL, *mynode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 	struct socket *sock = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	struct sockaddr_in myaddr = {0, }, remoteaddr = {0, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 	int ret = 0, stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	unsigned int timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 	unsigned int nofs_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	 * sock_create allocates the sock with GFP_KERNEL. We must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 	 * prevent the filesystem from being reentered by memory reclaim.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 	nofs_flag = memalloc_nofs_save();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 	/* if we're greater we initiate tx, otherwise we accept */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 	if (o2nm_this_node() <= o2net_num_from_nn(nn))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	/* watch for racing with tearing a node down */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 	node = o2nm_get_node_by_num(o2net_num_from_nn(nn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	if (node == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 	mynode = o2nm_get_node_by_num(o2nm_this_node());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	if (mynode == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	spin_lock(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	 * see if we already have one pending or have given up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 	 * For nn_timeout, it is set when we close the connection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 	 * because of the idle time out. So it means that we have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	 * at least connected to that node successfully once,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 	 * now try to connect to it again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	timeout = atomic_read(&nn->nn_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 	stop = (nn->nn_sc ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 		(nn->nn_persistent_error &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 		(nn->nn_persistent_error != -ENOTCONN || timeout == 0)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 	spin_unlock(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 	if (stop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	nn->nn_last_connect_attempt = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 	sc = sc_alloc(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	if (sc == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 		mlog(0, "couldn't allocate sc\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 	ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 		mlog(0, "can't create socket: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	sc->sc_sock = sock; /* freed by sc_kref_release */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 	sock->sk->sk_allocation = GFP_ATOMIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	myaddr.sin_family = AF_INET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 	myaddr.sin_addr.s_addr = mynode->nd_ipv4_address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	myaddr.sin_port = htons(0); /* any port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	ret = sock->ops->bind(sock, (struct sockaddr *)&myaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 			      sizeof(myaddr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 		mlog(ML_ERROR, "bind failed with %d at address %pI4\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 		     ret, &mynode->nd_ipv4_address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 	tcp_sock_set_nodelay(sc->sc_sock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 	tcp_sock_set_user_timeout(sock->sk, O2NET_TCP_USER_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 	o2net_register_callbacks(sc->sc_sock->sk, sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	spin_lock(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 	/* handshake completion will set nn->nn_sc_valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 	o2net_set_nn_state(nn, sc, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 	spin_unlock(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 	remoteaddr.sin_family = AF_INET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 	remoteaddr.sin_addr.s_addr = node->nd_ipv4_address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 	remoteaddr.sin_port = node->nd_ipv4_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 	ret = sc->sc_sock->ops->connect(sc->sc_sock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 					(struct sockaddr *)&remoteaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 					sizeof(remoteaddr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 					O_NONBLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 	if (ret == -EINPROGRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 	if (ret && sc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 		printk(KERN_NOTICE "o2net: Connect attempt to " SC_NODEF_FMT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 		       " failed with errno %d\n", SC_NODEF_ARGS(sc), ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 		/* 0 err so that another will be queued and attempted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 		 * from set_nn_state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 		o2net_ensure_shutdown(nn, sc, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 	if (sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 		sc_put(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 	if (node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 		o2nm_node_put(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 	if (mynode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 		o2nm_node_put(mynode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 	memalloc_nofs_restore(nofs_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) static void o2net_connect_expired(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 	struct o2net_node *nn =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 		container_of(work, struct o2net_node, nn_connect_expired.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 	spin_lock(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	if (!nn->nn_sc_valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 		printk(KERN_NOTICE "o2net: No connection established with "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 		       "node %u after %u.%u seconds, check network and"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 		       " cluster configuration.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 		     o2net_num_from_nn(nn),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 		     o2net_idle_timeout() / 1000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 		     o2net_idle_timeout() % 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 		o2net_set_nn_state(nn, NULL, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 	spin_unlock(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) static void o2net_still_up(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 	struct o2net_node *nn =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 		container_of(work, struct o2net_node, nn_still_up.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	o2quo_hb_still_up(o2net_num_from_nn(nn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) /* ------------------------------------------------------------ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) void o2net_disconnect_node(struct o2nm_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 	struct o2net_node *nn = o2net_nn_from_num(node->nd_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	/* don't reconnect until it's heartbeating again */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 	spin_lock(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 	atomic_set(&nn->nn_timeout, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	o2net_set_nn_state(nn, NULL, 0, -ENOTCONN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 	spin_unlock(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	if (o2net_wq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 		cancel_delayed_work(&nn->nn_connect_expired);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 		cancel_delayed_work(&nn->nn_connect_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 		cancel_delayed_work(&nn->nn_still_up);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 		flush_workqueue(o2net_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) static void o2net_hb_node_down_cb(struct o2nm_node *node, int node_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 				  void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 	o2quo_hb_down(node_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 	if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 	if (node_num != o2nm_this_node())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 		o2net_disconnect_node(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 	BUG_ON(atomic_read(&o2net_connected_peers) < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) static void o2net_hb_node_up_cb(struct o2nm_node *node, int node_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 				void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 	struct o2net_node *nn = o2net_nn_from_num(node_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	o2quo_hb_up(node_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 	BUG_ON(!node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 	/* ensure an immediate connect attempt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 	nn->nn_last_connect_attempt = jiffies -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 		(msecs_to_jiffies(o2net_reconnect_delay()) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 	if (node_num != o2nm_this_node()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 		/* believe it or not, accept and node heartbeating testing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 		 * can succeed for this node before we got here.. so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 		 * only use set_nn_state to clear the persistent error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 		 * if that hasn't already happened */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 		spin_lock(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 		atomic_set(&nn->nn_timeout, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 		if (nn->nn_persistent_error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 			o2net_set_nn_state(nn, NULL, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 		spin_unlock(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) void o2net_unregister_hb_callbacks(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	o2hb_unregister_callback(NULL, &o2net_hb_up);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	o2hb_unregister_callback(NULL, &o2net_hb_down);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) int o2net_register_hb_callbacks(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 	o2hb_setup_callback(&o2net_hb_down, O2HB_NODE_DOWN_CB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 			    o2net_hb_node_down_cb, NULL, O2NET_HB_PRI);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 	o2hb_setup_callback(&o2net_hb_up, O2HB_NODE_UP_CB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 			    o2net_hb_node_up_cb, NULL, O2NET_HB_PRI);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 	ret = o2hb_register_callback(NULL, &o2net_hb_up);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 	if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 		ret = o2hb_register_callback(NULL, &o2net_hb_down);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 		o2net_unregister_hb_callbacks();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) /* ------------------------------------------------------------ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) static int o2net_accept_one(struct socket *sock, int *more)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 	struct sockaddr_in sin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	struct socket *new_sock = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 	struct o2nm_node *node = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 	struct o2nm_node *local_node = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	struct o2net_sock_container *sc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 	struct o2net_node *nn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 	unsigned int nofs_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 	 * sock_create_lite allocates the sock with GFP_KERNEL. We must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 	 * prevent the filesystem from being reentered by memory reclaim.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 	nofs_flag = memalloc_nofs_save();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 	BUG_ON(sock == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 	*more = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 	ret = sock_create_lite(sock->sk->sk_family, sock->sk->sk_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 			       sock->sk->sk_protocol, &new_sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 	new_sock->type = sock->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 	new_sock->ops = sock->ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 	ret = sock->ops->accept(sock, new_sock, O_NONBLOCK, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 	*more = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 	new_sock->sk->sk_allocation = GFP_ATOMIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 	tcp_sock_set_nodelay(new_sock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 	tcp_sock_set_user_timeout(new_sock->sk, O2NET_TCP_USER_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 	ret = new_sock->ops->getname(new_sock, (struct sockaddr *) &sin, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 	node = o2nm_get_node_by_ip(sin.sin_addr.s_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 	if (node == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 		printk(KERN_NOTICE "o2net: Attempt to connect from unknown "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 		       "node at %pI4:%d\n", &sin.sin_addr.s_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 		       ntohs(sin.sin_port));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 	if (o2nm_this_node() >= node->nd_num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 		local_node = o2nm_get_node_by_num(o2nm_this_node());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 		if (local_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 			printk(KERN_NOTICE "o2net: Unexpected connect attempt "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 					"seen at node '%s' (%u, %pI4:%d) from "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 					"node '%s' (%u, %pI4:%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 					local_node->nd_name, local_node->nd_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 					&(local_node->nd_ipv4_address),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 					ntohs(local_node->nd_ipv4_port),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 					node->nd_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 					node->nd_num, &sin.sin_addr.s_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 					ntohs(sin.sin_port));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 	/* this happens all the time when the other node sees our heartbeat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 	 * and tries to connect before we see their heartbeat */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	if (!o2hb_check_node_heartbeating_from_callback(node->nd_num)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 		mlog(ML_CONN, "attempt to connect from node '%s' at "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 		     "%pI4:%d but it isn't heartbeating\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 		     node->nd_name, &sin.sin_addr.s_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 		     ntohs(sin.sin_port));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 	nn = o2net_nn_from_num(node->nd_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 	spin_lock(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 	if (nn->nn_sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 		ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 	spin_unlock(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 		printk(KERN_NOTICE "o2net: Attempt to connect from node '%s' "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 		       "at %pI4:%d but it already has an open connection\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 		       node->nd_name, &sin.sin_addr.s_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 		       ntohs(sin.sin_port));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 	sc = sc_alloc(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 	if (sc == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 	sc->sc_sock = new_sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 	new_sock = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 	spin_lock(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 	atomic_set(&nn->nn_timeout, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 	o2net_set_nn_state(nn, sc, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 	spin_unlock(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 	o2net_register_callbacks(sc->sc_sock->sk, sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 	o2net_sc_queue_work(sc, &sc->sc_rx_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 	o2net_initialize_handshake();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 	o2net_sendpage(sc, o2net_hand, sizeof(*o2net_hand));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 	if (new_sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 		sock_release(new_sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 	if (node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 		o2nm_node_put(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 	if (local_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 		o2nm_node_put(local_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 	if (sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 		sc_put(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 	memalloc_nofs_restore(nofs_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904)  * This function is invoked in response to one or more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905)  * pending accepts at softIRQ level. We must drain the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906)  * entire que before returning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) static void o2net_accept_many(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 	struct socket *sock = o2net_listen_sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 	int	more;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 	 * It is critical to note that due to interrupt moderation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 	 * at the network driver level, we can't assume to get a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 	 * softIRQ for every single conn since tcp SYN packets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 	 * can arrive back-to-back, and therefore many pending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 	 * accepts may result in just 1 softIRQ. If we terminate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 	 * the o2net_accept_one() loop upon seeing an err, what happens
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 	 * to the rest of the conns in the queue? If no new SYN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 	 * arrives for hours, no softIRQ  will be delivered,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 	 * and the connections will just sit in the queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 		o2net_accept_one(sock, &more);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 		if (!more)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) static void o2net_listen_data_ready(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 	void (*ready)(struct sock *sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 	read_lock_bh(&sk->sk_callback_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 	ready = sk->sk_user_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 	if (ready == NULL) { /* check for teardown race */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 		ready = sk->sk_data_ready;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 	/* This callback may called twice when a new connection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 	 * is  being established as a child socket inherits everything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 	 * from a parent LISTEN socket, including the data_ready cb of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 	 * the parent. This leads to a hazard. In o2net_accept_one()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 	 * we are still initializing the child socket but have not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 	 * changed the inherited data_ready callback yet when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 	 * data starts arriving.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 	 * We avoid this hazard by checking the state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 	 * For the listening socket,  the state will be TCP_LISTEN; for the new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 	 * socket, will be  TCP_ESTABLISHED. Also, in this case,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 	 * sk->sk_user_data is not a valid function pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 	if (sk->sk_state == TCP_LISTEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 		queue_work(o2net_wq, &o2net_listen_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 		ready = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 	read_unlock_bh(&sk->sk_callback_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 	if (ready != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 		ready(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) static int o2net_open_listening_sock(__be32 addr, __be16 port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 	struct socket *sock = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 	struct sockaddr_in sin = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 		.sin_family = PF_INET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 		.sin_addr = { .s_addr = addr },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 		.sin_port = port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 	ret = sock_create(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 		printk(KERN_ERR "o2net: Error %d while creating socket\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 	sock->sk->sk_allocation = GFP_ATOMIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 	write_lock_bh(&sock->sk->sk_callback_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 	sock->sk->sk_user_data = sock->sk->sk_data_ready;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 	sock->sk->sk_data_ready = o2net_listen_data_ready;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 	write_unlock_bh(&sock->sk->sk_callback_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 	o2net_listen_sock = sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 	INIT_WORK(&o2net_listen_work, o2net_accept_many);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 	sock->sk->sk_reuse = SK_CAN_REUSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 	ret = sock->ops->bind(sock, (struct sockaddr *)&sin, sizeof(sin));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 		printk(KERN_ERR "o2net: Error %d while binding socket at "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 		       "%pI4:%u\n", ret, &addr, ntohs(port)); 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 	ret = sock->ops->listen(sock, 64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 		printk(KERN_ERR "o2net: Error %d while listening on %pI4:%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 		       ret, &addr, ntohs(port));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 		o2net_listen_sock = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 		if (sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 			sock_release(sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019)  * called from node manager when we should bring up our network listening
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020)  * socket.  node manager handles all the serialization to only call this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021)  * once and to match it with o2net_stop_listening().  note,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022)  * o2nm_this_node() doesn't work yet as we're being called while it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023)  * is being set up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) int o2net_start_listening(struct o2nm_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 	BUG_ON(o2net_wq != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 	BUG_ON(o2net_listen_sock != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 	mlog(ML_KTHREAD, "starting o2net thread...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 	o2net_wq = alloc_ordered_workqueue("o2net", WQ_MEM_RECLAIM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 	if (o2net_wq == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 		mlog(ML_ERROR, "unable to launch o2net thread\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 		return -ENOMEM; /* ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 	ret = o2net_open_listening_sock(node->nd_ipv4_address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 					node->nd_ipv4_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 		destroy_workqueue(o2net_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 		o2net_wq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 		o2quo_conn_up(node->nd_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) /* again, o2nm_this_node() doesn't work here as we're involved in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051)  * tearing it down */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) void o2net_stop_listening(struct o2nm_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 	struct socket *sock = o2net_listen_sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 	size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 	BUG_ON(o2net_wq == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 	BUG_ON(o2net_listen_sock == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 	/* stop the listening socket from generating work */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 	write_lock_bh(&sock->sk->sk_callback_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 	sock->sk->sk_data_ready = sock->sk->sk_user_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 	sock->sk->sk_user_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 	write_unlock_bh(&sock->sk->sk_callback_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 	for (i = 0; i < ARRAY_SIZE(o2net_nodes); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 		struct o2nm_node *node = o2nm_get_node_by_num(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 		if (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 			o2net_disconnect_node(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 			o2nm_node_put(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 	/* finish all work and tear down the work queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 	mlog(ML_KTHREAD, "waiting for o2net thread to exit....\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 	destroy_workqueue(o2net_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 	o2net_wq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 	sock_release(o2net_listen_sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 	o2net_listen_sock = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 	o2quo_conn_err(node->nd_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) /* ------------------------------------------------------------ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) int o2net_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 	unsigned long i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 	o2quo_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 	o2net_debugfs_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 	o2net_hand = kzalloc(sizeof(struct o2net_handshake), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 	o2net_keep_req = kzalloc(sizeof(struct o2net_msg), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 	o2net_keep_resp = kzalloc(sizeof(struct o2net_msg), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 	if (!o2net_hand || !o2net_keep_req || !o2net_keep_resp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 	o2net_hand->protocol_version = cpu_to_be64(O2NET_PROTOCOL_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 	o2net_hand->connector_id = cpu_to_be64(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 	o2net_keep_req->magic = cpu_to_be16(O2NET_MSG_KEEP_REQ_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 	o2net_keep_resp->magic = cpu_to_be16(O2NET_MSG_KEEP_RESP_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 	for (i = 0; i < ARRAY_SIZE(o2net_nodes); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 		struct o2net_node *nn = o2net_nn_from_num(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 		atomic_set(&nn->nn_timeout, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 		spin_lock_init(&nn->nn_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 		INIT_DELAYED_WORK(&nn->nn_connect_work, o2net_start_connect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 		INIT_DELAYED_WORK(&nn->nn_connect_expired,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 				  o2net_connect_expired);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 		INIT_DELAYED_WORK(&nn->nn_still_up, o2net_still_up);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 		/* until we see hb from a node we'll return einval */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 		nn->nn_persistent_error = -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 		init_waitqueue_head(&nn->nn_sc_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 		idr_init(&nn->nn_status_idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 		INIT_LIST_HEAD(&nn->nn_status_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 	kfree(o2net_hand);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 	kfree(o2net_keep_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 	kfree(o2net_keep_resp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 	o2net_debugfs_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 	o2quo_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 	return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) void o2net_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 	o2quo_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 	kfree(o2net_hand);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 	kfree(o2net_keep_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 	kfree(o2net_keep_resp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 	o2net_debugfs_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) }