midcomms.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /******************************************************************************
  3. *******************************************************************************
  4. **
  5. ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  6. ** Copyright (C) 2004-2021 Red Hat, Inc. All rights reserved.
  7. **
  8. **
  9. *******************************************************************************
  10. ******************************************************************************/
  11. /*
  12. * midcomms.c
  13. *
  14. * This is the appallingly named "mid-level" comms layer. It takes care about
  15. * deliver an on application layer "reliable" communication above the used
  16. * lowcomms transport layer.
  17. *
  18. * How it works:
  19. *
  20. * Each nodes keeps track of all send DLM messages in send_queue with a sequence
  21. * number. The receive will send an DLM_ACK message back for every DLM message
  22. * received at the other side. If a reconnect happens in lowcomms we will send
  23. * all unacknowledged dlm messages again. The receiving side might drop any already
  24. * received message by comparing sequence numbers.
  25. *
  26. * How version detection works:
  27. *
  28. * Due the fact that dlm has pre-configured node addresses on every side
  29. * it is in it's nature that every side connects at starts to transmit
  30. * dlm messages which ends in a race. However DLM_RCOM_NAMES, DLM_RCOM_STATUS
  31. * and their replies are the first messages which are exchanges. Due backwards
  32. * compatibility these messages are not covered by the midcomms re-transmission
  33. * layer. These messages have their own re-transmission handling in the dlm
  34. * application layer. The version field of every node will be set on these RCOM
  35. * messages as soon as they arrived and the node isn't yet part of the nodes
  36. * hash. There exists also logic to detect version mismatched if something weird
  37. * going on or the first messages isn't an expected one.
  38. *
  39. * Termination:
  40. *
  41. * The midcomms layer does a 4 way handshake for termination on DLM protocol
  42. * like TCP supports it with half-closed socket support. SCTP doesn't support
  43. * half-closed socket, so we do it on DLM layer. Also socket shutdown() can be
  44. * interrupted by .e.g. tcp reset itself. Additional there exists the othercon
  45. * paradigm in lowcomms which cannot be easily without breaking backwards
  46. * compatibility. A node cannot send anything to another node when a DLM_FIN
  47. * message was send. There exists additional logic to print a warning if
  48. * DLM wants to do it. There exists a state handling like RFC 793 but reduced
  49. * to termination only. The event "member removal event" describes the cluster
  50. * manager removed the node from internal lists, at this point DLM does not
  51. * send any message to the other node. There exists two cases:
  52. *
  53. * 1. The cluster member was removed and we received a FIN
  54. * OR
  55. * 2. We received a FIN but the member was not removed yet
  56. *
  57. * One of these cases will do the CLOSE_WAIT to LAST_ACK change.
  58. *
  59. *
  60. * +---------+
  61. * | CLOSED |
  62. * +---------+
  63. * | add member/receive RCOM version
  64. * | detection msg
  65. * V
  66. * +---------+
  67. * | ESTAB |
  68. * +---------+
  69. * CLOSE | | rcv FIN
  70. * ------- | | -------
  71. * +---------+ snd FIN / \ snd ACK +---------+
  72. * | FIN |<----------------- ------------------>| CLOSE |
  73. * | WAIT-1 |------------------ | WAIT |
  74. * +---------+ rcv FIN \ +---------+
  75. * | rcv ACK of FIN ------- | CLOSE | member
  76. * | -------------- snd ACK | ------- | removal
  77. * V x V snd FIN V event
  78. * +---------+ +---------+ +---------+
  79. * |FINWAIT-2| | CLOSING | | LAST-ACK|
  80. * +---------+ +---------+ +---------+
  81. * | rcv ACK of FIN | rcv ACK of FIN |
  82. * | rcv FIN -------------- | -------------- |
  83. * | ------- x V x V
  84. * \ snd ACK +---------+ +---------+
  85. * ------------------------>| CLOSED | | CLOSED |
  86. * +---------+ +---------+
  87. *
  88. * NOTE: any state can interrupted by midcomms_close() and state will be
  89. * switched to CLOSED in case of fencing. There exists also some timeout
  90. * handling when we receive the version detection RCOM messages which is
  91. * made by observation.
  92. *
  93. * Future improvements:
  94. *
  95. * There exists some known issues/improvements of the dlm handling. Some
  96. * of them should be done in a next major dlm version bump which makes
  97. * it incompatible with previous versions.
  98. *
  99. * Unaligned memory access:
  100. *
  101. * There exists cases when the dlm message buffer length is not aligned
  102. * to 8 byte. However seems nobody detected any problem with it. This
  103. * can be fixed in the next major version bump of dlm.
  104. *
  105. * Version detection:
  106. *
  107. * The version detection and how it's done is related to backwards
  108. * compatibility. There exists better ways to make a better handling.
  109. * However this should be changed in the next major version bump of dlm.
  110. *
  111. * Tail Size checking:
  112. *
  113. * There exists a message tail payload in e.g. DLM_MSG however we don't
  114. * check it against the message length yet regarding to the receive buffer
  115. * length. That need to be validated.
  116. *
  117. * Fencing bad nodes:
  118. *
  119. * At timeout places or weird sequence number behaviours we should send
  120. * a fencing request to the cluster manager.
  121. */
  122. /* Debug switch to enable a 5 seconds sleep waiting of a termination.
  123. * This can be useful to test fencing while termination is running.
  124. * This requires a setup with only gfs2 as dlm user, so that the
  125. * last umount will terminate the connection.
  126. *
  127. * However it became useful to test, while the 5 seconds block in umount
  128. * just press the reset button. In a lot of dropping the termination
  129. * process can could take several seconds.
  130. */
  131. #define DLM_DEBUG_FENCE_TERMINATION 0
  132. #include <trace/events/dlm.h>
  133. #include <net/tcp.h>
  134. #include "dlm_internal.h"
  135. #include "lowcomms.h"
  136. #include "config.h"
  137. #include "memory.h"
  138. #include "lock.h"
  139. #include "util.h"
  140. #include "midcomms.h"
  141. /* init value for sequence numbers for testing purpose only e.g. overflows */
  142. #define DLM_SEQ_INIT 0
  143. /* 5 seconds wait to sync ending of dlm */
  144. #define DLM_SHUTDOWN_TIMEOUT msecs_to_jiffies(5000)
  145. #define DLM_VERSION_NOT_SET 0
  146. #define DLM_SEND_ACK_BACK_MSG_THRESHOLD 32
  147. #define DLM_RECV_ACK_BACK_MSG_THRESHOLD (DLM_SEND_ACK_BACK_MSG_THRESHOLD * 8)
  148. struct midcomms_node {
  149. int nodeid;
  150. uint32_t version;
  151. atomic_t seq_send;
  152. atomic_t seq_next;
  153. /* These queues are unbound because we cannot drop any message in dlm.
  154. * We could send a fence signal for a specific node to the cluster
  155. * manager if queues hits some maximum value, however this handling
  156. * not supported yet.
  157. */
  158. struct list_head send_queue;
  159. spinlock_t send_queue_lock;
  160. atomic_t send_queue_cnt;
  161. #define DLM_NODE_FLAG_CLOSE 1
  162. #define DLM_NODE_FLAG_STOP_TX 2
  163. #define DLM_NODE_FLAG_STOP_RX 3
  164. atomic_t ulp_delivered;
  165. unsigned long flags;
  166. wait_queue_head_t shutdown_wait;
  167. /* dlm tcp termination state */
  168. #define DLM_CLOSED 1
  169. #define DLM_ESTABLISHED 2
  170. #define DLM_FIN_WAIT1 3
  171. #define DLM_FIN_WAIT2 4
  172. #define DLM_CLOSE_WAIT 5
  173. #define DLM_LAST_ACK 6
  174. #define DLM_CLOSING 7
  175. int state;
  176. spinlock_t state_lock;
  177. /* counts how many lockspaces are using this node
  178. * this refcount is necessary to determine if the
  179. * node wants to disconnect.
  180. */
  181. int users;
  182. /* not protected by srcu, node_hash lifetime */
  183. void *debugfs;
  184. struct hlist_node hlist;
  185. struct rcu_head rcu;
  186. };
  187. struct dlm_mhandle {
  188. const union dlm_packet *inner_p;
  189. struct midcomms_node *node;
  190. struct dlm_opts *opts;
  191. struct dlm_msg *msg;
  192. bool committed;
  193. uint32_t seq;
  194. void (*ack_rcv)(struct midcomms_node *node);
  195. /* get_mhandle/commit srcu idx exchange */
  196. int idx;
  197. struct list_head list;
  198. struct rcu_head rcu;
  199. };
  200. static struct hlist_head node_hash[CONN_HASH_SIZE];
  201. static DEFINE_SPINLOCK(nodes_lock);
  202. DEFINE_STATIC_SRCU(nodes_srcu);
  203. /* This mutex prevents that midcomms_close() is running while
  204. * stop() or remove(). As I experienced invalid memory access
  205. * behaviours when DLM_DEBUG_FENCE_TERMINATION is enabled and
  206. * resetting machines. I will end in some double deletion in nodes
  207. * datastructure.
  208. */
  209. static DEFINE_MUTEX(close_lock);
  210. struct kmem_cache *dlm_midcomms_cache_create(void)
  211. {
  212. return KMEM_CACHE(dlm_mhandle, 0);
  213. }
  214. static inline const char *dlm_state_str(int state)
  215. {
  216. switch (state) {
  217. case DLM_CLOSED:
  218. return "CLOSED";
  219. case DLM_ESTABLISHED:
  220. return "ESTABLISHED";
  221. case DLM_FIN_WAIT1:
  222. return "FIN_WAIT1";
  223. case DLM_FIN_WAIT2:
  224. return "FIN_WAIT2";
  225. case DLM_CLOSE_WAIT:
  226. return "CLOSE_WAIT";
  227. case DLM_LAST_ACK:
  228. return "LAST_ACK";
  229. case DLM_CLOSING:
  230. return "CLOSING";
  231. default:
  232. return "UNKNOWN";
  233. }
  234. }
  235. const char *dlm_midcomms_state(struct midcomms_node *node)
  236. {
  237. return dlm_state_str(node->state);
  238. }
  239. unsigned long dlm_midcomms_flags(struct midcomms_node *node)
  240. {
  241. return node->flags;
  242. }
  243. int dlm_midcomms_send_queue_cnt(struct midcomms_node *node)
  244. {
  245. return atomic_read(&node->send_queue_cnt);
  246. }
  247. uint32_t dlm_midcomms_version(struct midcomms_node *node)
  248. {
  249. return node->version;
  250. }
  251. static struct midcomms_node *__find_node(int nodeid, int r)
  252. {
  253. struct midcomms_node *node;
  254. hlist_for_each_entry_rcu(node, &node_hash[r], hlist) {
  255. if (node->nodeid == nodeid)
  256. return node;
  257. }
  258. return NULL;
  259. }
  260. static void dlm_mhandle_release(struct rcu_head *rcu)
  261. {
  262. struct dlm_mhandle *mh = container_of(rcu, struct dlm_mhandle, rcu);
  263. dlm_lowcomms_put_msg(mh->msg);
  264. dlm_free_mhandle(mh);
  265. }
  266. static void dlm_mhandle_delete(struct midcomms_node *node,
  267. struct dlm_mhandle *mh)
  268. {
  269. list_del_rcu(&mh->list);
  270. atomic_dec(&node->send_queue_cnt);
  271. call_rcu(&mh->rcu, dlm_mhandle_release);
  272. }
  273. static void dlm_send_queue_flush(struct midcomms_node *node)
  274. {
  275. struct dlm_mhandle *mh;
  276. pr_debug("flush midcomms send queue of node %d\n", node->nodeid);
  277. rcu_read_lock();
  278. spin_lock_bh(&node->send_queue_lock);
  279. list_for_each_entry_rcu(mh, &node->send_queue, list) {
  280. dlm_mhandle_delete(node, mh);
  281. }
  282. spin_unlock_bh(&node->send_queue_lock);
  283. rcu_read_unlock();
  284. }
  285. static void midcomms_node_reset(struct midcomms_node *node)
  286. {
  287. pr_debug("reset node %d\n", node->nodeid);
  288. atomic_set(&node->seq_next, DLM_SEQ_INIT);
  289. atomic_set(&node->seq_send, DLM_SEQ_INIT);
  290. atomic_set(&node->ulp_delivered, 0);
  291. node->version = DLM_VERSION_NOT_SET;
  292. node->flags = 0;
  293. dlm_send_queue_flush(node);
  294. node->state = DLM_CLOSED;
  295. wake_up(&node->shutdown_wait);
  296. }
  297. static struct midcomms_node *nodeid2node(int nodeid)
  298. {
  299. return __find_node(nodeid, nodeid_hash(nodeid));
  300. }
  301. int dlm_midcomms_addr(int nodeid, struct sockaddr_storage *addr)
  302. {
  303. int ret, idx, r = nodeid_hash(nodeid);
  304. struct midcomms_node *node;
  305. ret = dlm_lowcomms_addr(nodeid, addr);
  306. if (ret)
  307. return ret;
  308. idx = srcu_read_lock(&nodes_srcu);
  309. node = __find_node(nodeid, r);
  310. if (node) {
  311. srcu_read_unlock(&nodes_srcu, idx);
  312. return 0;
  313. }
  314. srcu_read_unlock(&nodes_srcu, idx);
  315. node = kmalloc_obj(*node, GFP_NOFS);
  316. if (!node)
  317. return -ENOMEM;
  318. node->nodeid = nodeid;
  319. spin_lock_init(&node->state_lock);
  320. spin_lock_init(&node->send_queue_lock);
  321. atomic_set(&node->send_queue_cnt, 0);
  322. INIT_LIST_HEAD(&node->send_queue);
  323. init_waitqueue_head(&node->shutdown_wait);
  324. node->users = 0;
  325. midcomms_node_reset(node);
  326. spin_lock_bh(&nodes_lock);
  327. hlist_add_head_rcu(&node->hlist, &node_hash[r]);
  328. spin_unlock_bh(&nodes_lock);
  329. node->debugfs = dlm_create_debug_comms_file(nodeid, node);
  330. return 0;
  331. }
  332. static int dlm_send_ack(int nodeid, uint32_t seq)
  333. {
  334. int mb_len = sizeof(struct dlm_header);
  335. struct dlm_header *m_header;
  336. struct dlm_msg *msg;
  337. char *ppc;
  338. msg = dlm_lowcomms_new_msg(nodeid, mb_len, &ppc, NULL, NULL);
  339. if (!msg)
  340. return -ENOMEM;
  341. m_header = (struct dlm_header *)ppc;
  342. m_header->h_version = cpu_to_le32(DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
  343. m_header->h_nodeid = cpu_to_le32(dlm_our_nodeid());
  344. m_header->h_length = cpu_to_le16(mb_len);
  345. m_header->h_cmd = DLM_ACK;
  346. m_header->u.h_seq = cpu_to_le32(seq);
  347. dlm_lowcomms_commit_msg(msg);
  348. dlm_lowcomms_put_msg(msg);
  349. return 0;
  350. }
  351. static void dlm_send_ack_threshold(struct midcomms_node *node,
  352. uint32_t threshold)
  353. {
  354. uint32_t oval, nval;
  355. bool send_ack;
  356. /* let only send one user trigger threshold to send ack back */
  357. do {
  358. oval = atomic_read(&node->ulp_delivered);
  359. send_ack = (oval > threshold);
  360. /* abort if threshold is not reached */
  361. if (!send_ack)
  362. break;
  363. nval = 0;
  364. /* try to reset ulp_delivered counter */
  365. } while (atomic_cmpxchg(&node->ulp_delivered, oval, nval) != oval);
  366. if (send_ack)
  367. dlm_send_ack(node->nodeid, atomic_read(&node->seq_next));
  368. }
  369. static int dlm_send_fin(struct midcomms_node *node,
  370. void (*ack_rcv)(struct midcomms_node *node))
  371. {
  372. int mb_len = sizeof(struct dlm_header);
  373. struct dlm_header *m_header;
  374. struct dlm_mhandle *mh;
  375. char *ppc;
  376. mh = dlm_midcomms_get_mhandle(node->nodeid, mb_len, &ppc);
  377. if (!mh)
  378. return -ENOMEM;
  379. set_bit(DLM_NODE_FLAG_STOP_TX, &node->flags);
  380. mh->ack_rcv = ack_rcv;
  381. m_header = (struct dlm_header *)ppc;
  382. m_header->h_version = cpu_to_le32(DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
  383. m_header->h_nodeid = cpu_to_le32(dlm_our_nodeid());
  384. m_header->h_length = cpu_to_le16(mb_len);
  385. m_header->h_cmd = DLM_FIN;
  386. pr_debug("sending fin msg to node %d\n", node->nodeid);
  387. dlm_midcomms_commit_mhandle(mh, NULL, 0);
  388. return 0;
  389. }
  390. static void dlm_receive_ack(struct midcomms_node *node, uint32_t seq)
  391. {
  392. struct dlm_mhandle *mh;
  393. rcu_read_lock();
  394. list_for_each_entry_rcu(mh, &node->send_queue, list) {
  395. if (before(mh->seq, seq)) {
  396. if (mh->ack_rcv)
  397. mh->ack_rcv(node);
  398. } else {
  399. /* send queue should be ordered */
  400. break;
  401. }
  402. }
  403. spin_lock_bh(&node->send_queue_lock);
  404. list_for_each_entry_rcu(mh, &node->send_queue, list) {
  405. if (before(mh->seq, seq)) {
  406. dlm_mhandle_delete(node, mh);
  407. } else {
  408. /* send queue should be ordered */
  409. break;
  410. }
  411. }
  412. spin_unlock_bh(&node->send_queue_lock);
  413. rcu_read_unlock();
  414. }
  415. static void dlm_pas_fin_ack_rcv(struct midcomms_node *node)
  416. {
  417. spin_lock_bh(&node->state_lock);
  418. pr_debug("receive passive fin ack from node %d with state %s\n",
  419. node->nodeid, dlm_state_str(node->state));
  420. switch (node->state) {
  421. case DLM_LAST_ACK:
  422. /* DLM_CLOSED */
  423. midcomms_node_reset(node);
  424. break;
  425. case DLM_CLOSED:
  426. /* not valid but somehow we got what we want */
  427. wake_up(&node->shutdown_wait);
  428. break;
  429. default:
  430. spin_unlock_bh(&node->state_lock);
  431. log_print("%s: unexpected state: %d",
  432. __func__, node->state);
  433. WARN_ON_ONCE(1);
  434. return;
  435. }
  436. spin_unlock_bh(&node->state_lock);
  437. }
  438. static void dlm_receive_buffer_3_2_trace(uint32_t seq,
  439. const union dlm_packet *p)
  440. {
  441. switch (p->header.h_cmd) {
  442. case DLM_MSG:
  443. trace_dlm_recv_message(dlm_our_nodeid(), seq, &p->message);
  444. break;
  445. case DLM_RCOM:
  446. trace_dlm_recv_rcom(dlm_our_nodeid(), seq, &p->rcom);
  447. break;
  448. default:
  449. break;
  450. }
  451. }
  452. static void dlm_midcomms_receive_buffer(const union dlm_packet *p,
  453. struct midcomms_node *node,
  454. uint32_t seq)
  455. {
  456. bool is_expected_seq;
  457. uint32_t oval, nval;
  458. do {
  459. oval = atomic_read(&node->seq_next);
  460. is_expected_seq = (oval == seq);
  461. if (!is_expected_seq)
  462. break;
  463. nval = oval + 1;
  464. } while (atomic_cmpxchg(&node->seq_next, oval, nval) != oval);
  465. if (is_expected_seq) {
  466. switch (p->header.h_cmd) {
  467. case DLM_FIN:
  468. spin_lock_bh(&node->state_lock);
  469. pr_debug("receive fin msg from node %d with state %s\n",
  470. node->nodeid, dlm_state_str(node->state));
  471. switch (node->state) {
  472. case DLM_ESTABLISHED:
  473. dlm_send_ack(node->nodeid, nval);
  474. /* passive shutdown DLM_LAST_ACK case 1
  475. * additional we check if the node is used by
  476. * cluster manager events at all.
  477. */
  478. if (node->users == 0) {
  479. node->state = DLM_LAST_ACK;
  480. pr_debug("switch node %d to state %s case 1\n",
  481. node->nodeid, dlm_state_str(node->state));
  482. set_bit(DLM_NODE_FLAG_STOP_RX, &node->flags);
  483. dlm_send_fin(node, dlm_pas_fin_ack_rcv);
  484. } else {
  485. node->state = DLM_CLOSE_WAIT;
  486. pr_debug("switch node %d to state %s\n",
  487. node->nodeid, dlm_state_str(node->state));
  488. }
  489. break;
  490. case DLM_FIN_WAIT1:
  491. dlm_send_ack(node->nodeid, nval);
  492. node->state = DLM_CLOSING;
  493. set_bit(DLM_NODE_FLAG_STOP_RX, &node->flags);
  494. pr_debug("switch node %d to state %s\n",
  495. node->nodeid, dlm_state_str(node->state));
  496. break;
  497. case DLM_FIN_WAIT2:
  498. dlm_send_ack(node->nodeid, nval);
  499. midcomms_node_reset(node);
  500. pr_debug("switch node %d to state %s\n",
  501. node->nodeid, dlm_state_str(node->state));
  502. break;
  503. case DLM_LAST_ACK:
  504. /* probably remove_member caught it, do nothing */
  505. break;
  506. default:
  507. spin_unlock_bh(&node->state_lock);
  508. log_print("%s: unexpected state: %d",
  509. __func__, node->state);
  510. WARN_ON_ONCE(1);
  511. return;
  512. }
  513. spin_unlock_bh(&node->state_lock);
  514. break;
  515. default:
  516. WARN_ON_ONCE(test_bit(DLM_NODE_FLAG_STOP_RX, &node->flags));
  517. dlm_receive_buffer_3_2_trace(seq, p);
  518. dlm_receive_buffer(p, node->nodeid);
  519. atomic_inc(&node->ulp_delivered);
  520. /* unlikely case to send ack back when we don't transmit */
  521. dlm_send_ack_threshold(node, DLM_RECV_ACK_BACK_MSG_THRESHOLD);
  522. break;
  523. }
  524. } else {
  525. /* retry to ack message which we already have by sending back
  526. * current node->seq_next number as ack.
  527. */
  528. if (seq < oval)
  529. dlm_send_ack(node->nodeid, oval);
  530. log_print_ratelimited("ignore dlm msg because seq mismatch, seq: %u, expected: %u, nodeid: %d",
  531. seq, oval, node->nodeid);
  532. }
  533. }
  534. static int dlm_opts_check_msglen(const union dlm_packet *p, uint16_t msglen,
  535. int nodeid)
  536. {
  537. int len = msglen;
  538. /* we only trust outer header msglen because
  539. * it's checked against receive buffer length.
  540. */
  541. if (len < sizeof(struct dlm_opts))
  542. return -1;
  543. len -= sizeof(struct dlm_opts);
  544. if (len < le16_to_cpu(p->opts.o_optlen))
  545. return -1;
  546. len -= le16_to_cpu(p->opts.o_optlen);
  547. switch (p->opts.o_nextcmd) {
  548. case DLM_FIN:
  549. if (len < sizeof(struct dlm_header)) {
  550. log_print("fin too small: %d, will skip this message from node %d",
  551. len, nodeid);
  552. return -1;
  553. }
  554. break;
  555. case DLM_MSG:
  556. if (len < sizeof(struct dlm_message)) {
  557. log_print("msg too small: %d, will skip this message from node %d",
  558. msglen, nodeid);
  559. return -1;
  560. }
  561. break;
  562. case DLM_RCOM:
  563. if (len < sizeof(struct dlm_rcom)) {
  564. log_print("rcom msg too small: %d, will skip this message from node %d",
  565. len, nodeid);
  566. return -1;
  567. }
  568. break;
  569. default:
  570. log_print("unsupported o_nextcmd received: %u, will skip this message from node %d",
  571. p->opts.o_nextcmd, nodeid);
  572. return -1;
  573. }
  574. return 0;
  575. }
  576. static void dlm_midcomms_receive_buffer_3_2(const union dlm_packet *p, int nodeid)
  577. {
  578. uint16_t msglen = le16_to_cpu(p->header.h_length);
  579. struct midcomms_node *node;
  580. uint32_t seq;
  581. int ret, idx;
  582. idx = srcu_read_lock(&nodes_srcu);
  583. node = nodeid2node(nodeid);
  584. if (WARN_ON_ONCE(!node))
  585. goto out;
  586. switch (node->version) {
  587. case DLM_VERSION_NOT_SET:
  588. node->version = DLM_VERSION_3_2;
  589. wake_up(&node->shutdown_wait);
  590. log_print("version 0x%08x for node %d detected", DLM_VERSION_3_2,
  591. node->nodeid);
  592. spin_lock(&node->state_lock);
  593. switch (node->state) {
  594. case DLM_CLOSED:
  595. node->state = DLM_ESTABLISHED;
  596. pr_debug("switch node %d to state %s\n",
  597. node->nodeid, dlm_state_str(node->state));
  598. break;
  599. default:
  600. break;
  601. }
  602. spin_unlock(&node->state_lock);
  603. break;
  604. case DLM_VERSION_3_2:
  605. break;
  606. default:
  607. log_print_ratelimited("version mismatch detected, assumed 0x%08x but node %d has 0x%08x",
  608. DLM_VERSION_3_2, node->nodeid, node->version);
  609. goto out;
  610. }
  611. switch (p->header.h_cmd) {
  612. case DLM_RCOM:
  613. /* these rcom message we use to determine version.
  614. * they have their own retransmission handling and
  615. * are the first messages of dlm.
  616. *
  617. * length already checked.
  618. */
  619. switch (p->rcom.rc_type) {
  620. case cpu_to_le32(DLM_RCOM_NAMES):
  621. fallthrough;
  622. case cpu_to_le32(DLM_RCOM_NAMES_REPLY):
  623. fallthrough;
  624. case cpu_to_le32(DLM_RCOM_STATUS):
  625. fallthrough;
  626. case cpu_to_le32(DLM_RCOM_STATUS_REPLY):
  627. break;
  628. default:
  629. log_print("unsupported rcom type received: %u, will skip this message from node %d",
  630. le32_to_cpu(p->rcom.rc_type), nodeid);
  631. goto out;
  632. }
  633. WARN_ON_ONCE(test_bit(DLM_NODE_FLAG_STOP_RX, &node->flags));
  634. dlm_receive_buffer(p, nodeid);
  635. break;
  636. case DLM_OPTS:
  637. seq = le32_to_cpu(p->header.u.h_seq);
  638. ret = dlm_opts_check_msglen(p, msglen, nodeid);
  639. if (ret < 0) {
  640. log_print("opts msg too small: %u, will skip this message from node %d",
  641. msglen, nodeid);
  642. goto out;
  643. }
  644. p = (union dlm_packet *)((unsigned char *)p->opts.o_opts +
  645. le16_to_cpu(p->opts.o_optlen));
  646. /* recheck inner msglen just if it's not garbage */
  647. msglen = le16_to_cpu(p->header.h_length);
  648. switch (p->header.h_cmd) {
  649. case DLM_RCOM:
  650. if (msglen < sizeof(struct dlm_rcom)) {
  651. log_print("inner rcom msg too small: %u, will skip this message from node %d",
  652. msglen, nodeid);
  653. goto out;
  654. }
  655. break;
  656. case DLM_MSG:
  657. if (msglen < sizeof(struct dlm_message)) {
  658. log_print("inner msg too small: %u, will skip this message from node %d",
  659. msglen, nodeid);
  660. goto out;
  661. }
  662. break;
  663. case DLM_FIN:
  664. if (msglen < sizeof(struct dlm_header)) {
  665. log_print("inner fin too small: %u, will skip this message from node %d",
  666. msglen, nodeid);
  667. goto out;
  668. }
  669. break;
  670. default:
  671. log_print("unsupported inner h_cmd received: %u, will skip this message from node %d",
  672. msglen, nodeid);
  673. goto out;
  674. }
  675. dlm_midcomms_receive_buffer(p, node, seq);
  676. break;
  677. case DLM_ACK:
  678. seq = le32_to_cpu(p->header.u.h_seq);
  679. dlm_receive_ack(node, seq);
  680. break;
  681. default:
  682. log_print("unsupported h_cmd received: %u, will skip this message from node %d",
  683. p->header.h_cmd, nodeid);
  684. break;
  685. }
  686. out:
  687. srcu_read_unlock(&nodes_srcu, idx);
  688. }
  689. static void dlm_midcomms_receive_buffer_3_1(const union dlm_packet *p, int nodeid)
  690. {
  691. uint16_t msglen = le16_to_cpu(p->header.h_length);
  692. struct midcomms_node *node;
  693. int idx;
  694. idx = srcu_read_lock(&nodes_srcu);
  695. node = nodeid2node(nodeid);
  696. if (WARN_ON_ONCE(!node)) {
  697. srcu_read_unlock(&nodes_srcu, idx);
  698. return;
  699. }
  700. switch (node->version) {
  701. case DLM_VERSION_NOT_SET:
  702. node->version = DLM_VERSION_3_1;
  703. wake_up(&node->shutdown_wait);
  704. log_print("version 0x%08x for node %d detected", DLM_VERSION_3_1,
  705. node->nodeid);
  706. break;
  707. case DLM_VERSION_3_1:
  708. break;
  709. default:
  710. log_print_ratelimited("version mismatch detected, assumed 0x%08x but node %d has 0x%08x",
  711. DLM_VERSION_3_1, node->nodeid, node->version);
  712. srcu_read_unlock(&nodes_srcu, idx);
  713. return;
  714. }
  715. srcu_read_unlock(&nodes_srcu, idx);
  716. switch (p->header.h_cmd) {
  717. case DLM_RCOM:
  718. /* length already checked */
  719. break;
  720. case DLM_MSG:
  721. if (msglen < sizeof(struct dlm_message)) {
  722. log_print("msg too small: %u, will skip this message from node %d",
  723. msglen, nodeid);
  724. return;
  725. }
  726. break;
  727. default:
  728. log_print("unsupported h_cmd received: %u, will skip this message from node %d",
  729. p->header.h_cmd, nodeid);
  730. return;
  731. }
  732. dlm_receive_buffer(p, nodeid);
  733. }
  734. int dlm_validate_incoming_buffer(int nodeid, unsigned char *buf, int len)
  735. {
  736. const unsigned char *ptr = buf;
  737. const struct dlm_header *hd;
  738. uint16_t msglen;
  739. int ret = 0;
  740. while (len >= sizeof(struct dlm_header)) {
  741. hd = (struct dlm_header *)ptr;
  742. /* no message should be more than DLM_MAX_SOCKET_BUFSIZE or
  743. * less than dlm_header size.
  744. *
  745. * Some messages does not have a 8 byte length boundary yet
  746. * which can occur in a unaligned memory access of some dlm
  747. * messages. However this problem need to be fixed at the
  748. * sending side, for now it seems nobody run into architecture
  749. * related issues yet but it slows down some processing.
  750. * Fixing this issue should be scheduled in future by doing
  751. * the next major version bump.
  752. */
  753. msglen = le16_to_cpu(hd->h_length);
  754. if (msglen > DLM_MAX_SOCKET_BUFSIZE ||
  755. msglen < sizeof(struct dlm_header)) {
  756. log_print("received invalid length header: %u from node %d, will abort message parsing",
  757. msglen, nodeid);
  758. return -EBADMSG;
  759. }
  760. /* caller will take care that leftover
  761. * will be parsed next call with more data
  762. */
  763. if (msglen > len)
  764. break;
  765. ret += msglen;
  766. len -= msglen;
  767. ptr += msglen;
  768. }
  769. return ret;
  770. }
  771. /*
  772. * Called from the low-level comms layer to process a buffer of
  773. * commands.
  774. */
  775. int dlm_process_incoming_buffer(int nodeid, unsigned char *buf, int len)
  776. {
  777. const unsigned char *ptr = buf;
  778. const struct dlm_header *hd;
  779. uint16_t msglen;
  780. int ret = 0;
  781. while (len >= sizeof(struct dlm_header)) {
  782. hd = (struct dlm_header *)ptr;
  783. msglen = le16_to_cpu(hd->h_length);
  784. if (msglen > len)
  785. break;
  786. switch (hd->h_version) {
  787. case cpu_to_le32(DLM_VERSION_3_1):
  788. dlm_midcomms_receive_buffer_3_1((const union dlm_packet *)ptr, nodeid);
  789. break;
  790. case cpu_to_le32(DLM_VERSION_3_2):
  791. dlm_midcomms_receive_buffer_3_2((const union dlm_packet *)ptr, nodeid);
  792. break;
  793. default:
  794. log_print("received invalid version header: %u from node %d, will skip this message",
  795. le32_to_cpu(hd->h_version), nodeid);
  796. break;
  797. }
  798. ret += msglen;
  799. len -= msglen;
  800. ptr += msglen;
  801. }
  802. return ret;
  803. }
  804. void dlm_midcomms_unack_msg_resend(int nodeid)
  805. {
  806. struct midcomms_node *node;
  807. struct dlm_mhandle *mh;
  808. int idx, ret;
  809. idx = srcu_read_lock(&nodes_srcu);
  810. node = nodeid2node(nodeid);
  811. if (WARN_ON_ONCE(!node)) {
  812. srcu_read_unlock(&nodes_srcu, idx);
  813. return;
  814. }
  815. /* old protocol, we don't support to retransmit on failure */
  816. switch (node->version) {
  817. case DLM_VERSION_3_2:
  818. break;
  819. default:
  820. srcu_read_unlock(&nodes_srcu, idx);
  821. return;
  822. }
  823. rcu_read_lock();
  824. list_for_each_entry_rcu(mh, &node->send_queue, list) {
  825. if (!mh->committed)
  826. continue;
  827. ret = dlm_lowcomms_resend_msg(mh->msg);
  828. if (!ret)
  829. log_print_ratelimited("retransmit dlm msg, seq %u, nodeid %d",
  830. mh->seq, node->nodeid);
  831. }
  832. rcu_read_unlock();
  833. srcu_read_unlock(&nodes_srcu, idx);
  834. }
  835. static void dlm_fill_opts_header(struct dlm_opts *opts, uint16_t inner_len,
  836. uint32_t seq)
  837. {
  838. opts->o_header.h_cmd = DLM_OPTS;
  839. opts->o_header.h_version = cpu_to_le32(DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
  840. opts->o_header.h_nodeid = cpu_to_le32(dlm_our_nodeid());
  841. opts->o_header.h_length = cpu_to_le16(DLM_MIDCOMMS_OPT_LEN + inner_len);
  842. opts->o_header.u.h_seq = cpu_to_le32(seq);
  843. }
  844. static void midcomms_new_msg_cb(void *data)
  845. {
  846. struct dlm_mhandle *mh = data;
  847. atomic_inc(&mh->node->send_queue_cnt);
  848. spin_lock_bh(&mh->node->send_queue_lock);
  849. list_add_tail_rcu(&mh->list, &mh->node->send_queue);
  850. spin_unlock_bh(&mh->node->send_queue_lock);
  851. mh->seq = atomic_fetch_inc(&mh->node->seq_send);
  852. }
  853. static struct dlm_msg *dlm_midcomms_get_msg_3_2(struct dlm_mhandle *mh, int nodeid,
  854. int len, char **ppc)
  855. {
  856. struct dlm_opts *opts;
  857. struct dlm_msg *msg;
  858. msg = dlm_lowcomms_new_msg(nodeid, len + DLM_MIDCOMMS_OPT_LEN,
  859. ppc, midcomms_new_msg_cb, mh);
  860. if (!msg)
  861. return NULL;
  862. opts = (struct dlm_opts *)*ppc;
  863. mh->opts = opts;
  864. /* add possible options here */
  865. dlm_fill_opts_header(opts, len, mh->seq);
  866. *ppc += sizeof(*opts);
  867. mh->inner_p = (const union dlm_packet *)*ppc;
  868. return msg;
  869. }
  870. /* avoid false positive for nodes_srcu, unlock happens in
  871. * dlm_midcomms_commit_mhandle which is a must call if success
  872. */
  873. #ifndef __CHECKER__
  874. struct dlm_mhandle *dlm_midcomms_get_mhandle(int nodeid, int len, char **ppc)
  875. {
  876. struct midcomms_node *node;
  877. struct dlm_mhandle *mh;
  878. struct dlm_msg *msg;
  879. int idx;
  880. idx = srcu_read_lock(&nodes_srcu);
  881. node = nodeid2node(nodeid);
  882. if (WARN_ON_ONCE(!node))
  883. goto err;
  884. /* this is a bug, however we going on and hope it will be resolved */
  885. WARN_ON_ONCE(test_bit(DLM_NODE_FLAG_STOP_TX, &node->flags));
  886. mh = dlm_allocate_mhandle();
  887. if (!mh)
  888. goto err;
  889. mh->committed = false;
  890. mh->ack_rcv = NULL;
  891. mh->idx = idx;
  892. mh->node = node;
  893. switch (node->version) {
  894. case DLM_VERSION_3_1:
  895. msg = dlm_lowcomms_new_msg(nodeid, len, ppc, NULL, NULL);
  896. if (!msg) {
  897. dlm_free_mhandle(mh);
  898. goto err;
  899. }
  900. break;
  901. case DLM_VERSION_3_2:
  902. /* send ack back if necessary */
  903. dlm_send_ack_threshold(node, DLM_SEND_ACK_BACK_MSG_THRESHOLD);
  904. msg = dlm_midcomms_get_msg_3_2(mh, nodeid, len, ppc);
  905. if (!msg) {
  906. dlm_free_mhandle(mh);
  907. goto err;
  908. }
  909. break;
  910. default:
  911. dlm_free_mhandle(mh);
  912. WARN_ON_ONCE(1);
  913. goto err;
  914. }
  915. mh->msg = msg;
  916. /* keep in mind that is a must to call
  917. * dlm_midcomms_commit_msg() which releases
  918. * nodes_srcu using mh->idx which is assumed
  919. * here that the application will call it.
  920. */
  921. return mh;
  922. err:
  923. srcu_read_unlock(&nodes_srcu, idx);
  924. return NULL;
  925. }
  926. #endif
  927. static void dlm_midcomms_commit_msg_3_2_trace(const struct dlm_mhandle *mh,
  928. const void *name, int namelen)
  929. {
  930. switch (mh->inner_p->header.h_cmd) {
  931. case DLM_MSG:
  932. trace_dlm_send_message(mh->node->nodeid, mh->seq,
  933. &mh->inner_p->message,
  934. name, namelen);
  935. break;
  936. case DLM_RCOM:
  937. trace_dlm_send_rcom(mh->node->nodeid, mh->seq,
  938. &mh->inner_p->rcom);
  939. break;
  940. default:
  941. /* nothing to trace */
  942. break;
  943. }
  944. }
  945. static void dlm_midcomms_commit_msg_3_2(struct dlm_mhandle *mh,
  946. const void *name, int namelen)
  947. {
  948. /* nexthdr chain for fast lookup */
  949. mh->opts->o_nextcmd = mh->inner_p->header.h_cmd;
  950. mh->committed = true;
  951. dlm_midcomms_commit_msg_3_2_trace(mh, name, namelen);
  952. dlm_lowcomms_commit_msg(mh->msg);
  953. }
  954. /* avoid false positive for nodes_srcu, lock was happen in
  955. * dlm_midcomms_get_mhandle
  956. */
  957. #ifndef __CHECKER__
  958. void dlm_midcomms_commit_mhandle(struct dlm_mhandle *mh,
  959. const void *name, int namelen)
  960. {
  961. switch (mh->node->version) {
  962. case DLM_VERSION_3_1:
  963. srcu_read_unlock(&nodes_srcu, mh->idx);
  964. dlm_lowcomms_commit_msg(mh->msg);
  965. dlm_lowcomms_put_msg(mh->msg);
  966. /* mh is not part of rcu list in this case */
  967. dlm_free_mhandle(mh);
  968. break;
  969. case DLM_VERSION_3_2:
  970. /* held rcu read lock here, because we sending the
  971. * dlm message out, when we do that we could receive
  972. * an ack back which releases the mhandle and we
  973. * get a use after free.
  974. */
  975. rcu_read_lock();
  976. dlm_midcomms_commit_msg_3_2(mh, name, namelen);
  977. srcu_read_unlock(&nodes_srcu, mh->idx);
  978. rcu_read_unlock();
  979. break;
  980. default:
  981. srcu_read_unlock(&nodes_srcu, mh->idx);
  982. WARN_ON_ONCE(1);
  983. break;
  984. }
  985. }
  986. #endif
  987. int dlm_midcomms_start(void)
  988. {
  989. return dlm_lowcomms_start();
  990. }
  991. void dlm_midcomms_stop(void)
  992. {
  993. dlm_lowcomms_stop();
  994. }
  995. void dlm_midcomms_init(void)
  996. {
  997. int i;
  998. for (i = 0; i < CONN_HASH_SIZE; i++)
  999. INIT_HLIST_HEAD(&node_hash[i]);
  1000. dlm_lowcomms_init();
  1001. }
  1002. static void midcomms_node_release(struct rcu_head *rcu)
  1003. {
  1004. struct midcomms_node *node = container_of(rcu, struct midcomms_node, rcu);
  1005. WARN_ON_ONCE(atomic_read(&node->send_queue_cnt));
  1006. dlm_send_queue_flush(node);
  1007. kfree(node);
  1008. }
  1009. void dlm_midcomms_exit(void)
  1010. {
  1011. struct midcomms_node *node;
  1012. int i, idx;
  1013. idx = srcu_read_lock(&nodes_srcu);
  1014. for (i = 0; i < CONN_HASH_SIZE; i++) {
  1015. hlist_for_each_entry_rcu(node, &node_hash[i], hlist) {
  1016. dlm_delete_debug_comms_file(node->debugfs);
  1017. spin_lock(&nodes_lock);
  1018. hlist_del_rcu(&node->hlist);
  1019. spin_unlock(&nodes_lock);
  1020. call_srcu(&nodes_srcu, &node->rcu, midcomms_node_release);
  1021. }
  1022. }
  1023. srcu_read_unlock(&nodes_srcu, idx);
  1024. dlm_lowcomms_exit();
  1025. }
  1026. static void dlm_act_fin_ack_rcv(struct midcomms_node *node)
  1027. {
  1028. spin_lock_bh(&node->state_lock);
  1029. pr_debug("receive active fin ack from node %d with state %s\n",
  1030. node->nodeid, dlm_state_str(node->state));
  1031. switch (node->state) {
  1032. case DLM_FIN_WAIT1:
  1033. node->state = DLM_FIN_WAIT2;
  1034. pr_debug("switch node %d to state %s\n",
  1035. node->nodeid, dlm_state_str(node->state));
  1036. break;
  1037. case DLM_CLOSING:
  1038. midcomms_node_reset(node);
  1039. pr_debug("switch node %d to state %s\n",
  1040. node->nodeid, dlm_state_str(node->state));
  1041. break;
  1042. case DLM_CLOSED:
  1043. /* not valid but somehow we got what we want */
  1044. wake_up(&node->shutdown_wait);
  1045. break;
  1046. default:
  1047. spin_unlock_bh(&node->state_lock);
  1048. log_print("%s: unexpected state: %d",
  1049. __func__, node->state);
  1050. WARN_ON_ONCE(1);
  1051. return;
  1052. }
  1053. spin_unlock_bh(&node->state_lock);
  1054. }
  1055. void dlm_midcomms_add_member(int nodeid)
  1056. {
  1057. struct midcomms_node *node;
  1058. int idx;
  1059. idx = srcu_read_lock(&nodes_srcu);
  1060. node = nodeid2node(nodeid);
  1061. if (WARN_ON_ONCE(!node)) {
  1062. srcu_read_unlock(&nodes_srcu, idx);
  1063. return;
  1064. }
  1065. spin_lock_bh(&node->state_lock);
  1066. if (!node->users) {
  1067. pr_debug("receive add member from node %d with state %s\n",
  1068. node->nodeid, dlm_state_str(node->state));
  1069. switch (node->state) {
  1070. case DLM_ESTABLISHED:
  1071. break;
  1072. case DLM_CLOSED:
  1073. node->state = DLM_ESTABLISHED;
  1074. pr_debug("switch node %d to state %s\n",
  1075. node->nodeid, dlm_state_str(node->state));
  1076. break;
  1077. default:
  1078. /* some invalid state passive shutdown
  1079. * was failed, we try to reset and
  1080. * hope it will go on.
  1081. */
  1082. log_print("reset node %d because shutdown stuck",
  1083. node->nodeid);
  1084. midcomms_node_reset(node);
  1085. node->state = DLM_ESTABLISHED;
  1086. break;
  1087. }
  1088. }
  1089. node->users++;
  1090. pr_debug("node %d users inc count %d\n", nodeid, node->users);
  1091. spin_unlock_bh(&node->state_lock);
  1092. srcu_read_unlock(&nodes_srcu, idx);
  1093. }
  1094. void dlm_midcomms_remove_member(int nodeid)
  1095. {
  1096. struct midcomms_node *node;
  1097. int idx;
  1098. idx = srcu_read_lock(&nodes_srcu);
  1099. node = nodeid2node(nodeid);
  1100. /* in case of dlm_midcomms_close() removes node */
  1101. if (!node) {
  1102. srcu_read_unlock(&nodes_srcu, idx);
  1103. return;
  1104. }
  1105. spin_lock_bh(&node->state_lock);
  1106. /* case of dlm_midcomms_addr() created node but
  1107. * was not added before because dlm_midcomms_close()
  1108. * removed the node
  1109. */
  1110. if (!node->users) {
  1111. spin_unlock_bh(&node->state_lock);
  1112. srcu_read_unlock(&nodes_srcu, idx);
  1113. return;
  1114. }
  1115. node->users--;
  1116. pr_debug("node %d users dec count %d\n", nodeid, node->users);
  1117. /* hitting users count to zero means the
  1118. * other side is running dlm_midcomms_stop()
  1119. * we meet us to have a clean disconnect.
  1120. */
  1121. if (node->users == 0) {
  1122. pr_debug("receive remove member from node %d with state %s\n",
  1123. node->nodeid, dlm_state_str(node->state));
  1124. switch (node->state) {
  1125. case DLM_ESTABLISHED:
  1126. break;
  1127. case DLM_CLOSE_WAIT:
  1128. /* passive shutdown DLM_LAST_ACK case 2 */
  1129. node->state = DLM_LAST_ACK;
  1130. pr_debug("switch node %d to state %s case 2\n",
  1131. node->nodeid, dlm_state_str(node->state));
  1132. set_bit(DLM_NODE_FLAG_STOP_RX, &node->flags);
  1133. dlm_send_fin(node, dlm_pas_fin_ack_rcv);
  1134. break;
  1135. case DLM_LAST_ACK:
  1136. /* probably receive fin caught it, do nothing */
  1137. break;
  1138. case DLM_CLOSED:
  1139. /* already gone, do nothing */
  1140. break;
  1141. default:
  1142. log_print("%s: unexpected state: %d",
  1143. __func__, node->state);
  1144. break;
  1145. }
  1146. }
  1147. spin_unlock_bh(&node->state_lock);
  1148. srcu_read_unlock(&nodes_srcu, idx);
  1149. }
  1150. void dlm_midcomms_version_wait(void)
  1151. {
  1152. struct midcomms_node *node;
  1153. int i, idx, ret;
  1154. idx = srcu_read_lock(&nodes_srcu);
  1155. for (i = 0; i < CONN_HASH_SIZE; i++) {
  1156. hlist_for_each_entry_rcu(node, &node_hash[i], hlist) {
  1157. ret = wait_event_timeout(node->shutdown_wait,
  1158. node->version != DLM_VERSION_NOT_SET ||
  1159. node->state == DLM_CLOSED ||
  1160. test_bit(DLM_NODE_FLAG_CLOSE, &node->flags),
  1161. DLM_SHUTDOWN_TIMEOUT);
  1162. if (!ret || test_bit(DLM_NODE_FLAG_CLOSE, &node->flags))
  1163. pr_debug("version wait timed out for node %d with state %s\n",
  1164. node->nodeid, dlm_state_str(node->state));
  1165. }
  1166. }
  1167. srcu_read_unlock(&nodes_srcu, idx);
  1168. }
  1169. static void midcomms_shutdown(struct midcomms_node *node)
  1170. {
  1171. int ret;
  1172. /* old protocol, we don't wait for pending operations */
  1173. switch (node->version) {
  1174. case DLM_VERSION_3_2:
  1175. break;
  1176. default:
  1177. return;
  1178. }
  1179. spin_lock_bh(&node->state_lock);
  1180. pr_debug("receive active shutdown for node %d with state %s\n",
  1181. node->nodeid, dlm_state_str(node->state));
  1182. switch (node->state) {
  1183. case DLM_ESTABLISHED:
  1184. node->state = DLM_FIN_WAIT1;
  1185. pr_debug("switch node %d to state %s case 2\n",
  1186. node->nodeid, dlm_state_str(node->state));
  1187. dlm_send_fin(node, dlm_act_fin_ack_rcv);
  1188. break;
  1189. case DLM_CLOSED:
  1190. /* we have what we want */
  1191. break;
  1192. default:
  1193. /* busy to enter DLM_FIN_WAIT1, wait until passive
  1194. * done in shutdown_wait to enter DLM_CLOSED.
  1195. */
  1196. break;
  1197. }
  1198. spin_unlock_bh(&node->state_lock);
  1199. if (DLM_DEBUG_FENCE_TERMINATION)
  1200. msleep(5000);
  1201. /* wait for other side dlm + fin */
  1202. ret = wait_event_timeout(node->shutdown_wait,
  1203. node->state == DLM_CLOSED ||
  1204. test_bit(DLM_NODE_FLAG_CLOSE, &node->flags),
  1205. DLM_SHUTDOWN_TIMEOUT);
  1206. if (!ret)
  1207. pr_debug("active shutdown timed out for node %d with state %s\n",
  1208. node->nodeid, dlm_state_str(node->state));
  1209. else
  1210. pr_debug("active shutdown done for node %d with state %s\n",
  1211. node->nodeid, dlm_state_str(node->state));
  1212. }
  1213. void dlm_midcomms_shutdown(void)
  1214. {
  1215. struct midcomms_node *node;
  1216. int i, idx;
  1217. mutex_lock(&close_lock);
  1218. idx = srcu_read_lock(&nodes_srcu);
  1219. for (i = 0; i < CONN_HASH_SIZE; i++) {
  1220. hlist_for_each_entry_rcu(node, &node_hash[i], hlist) {
  1221. midcomms_shutdown(node);
  1222. }
  1223. }
  1224. dlm_lowcomms_shutdown();
  1225. for (i = 0; i < CONN_HASH_SIZE; i++) {
  1226. hlist_for_each_entry_rcu(node, &node_hash[i], hlist) {
  1227. midcomms_node_reset(node);
  1228. }
  1229. }
  1230. srcu_read_unlock(&nodes_srcu, idx);
  1231. mutex_unlock(&close_lock);
  1232. }
  1233. int dlm_midcomms_close(int nodeid)
  1234. {
  1235. struct midcomms_node *node;
  1236. int idx, ret;
  1237. idx = srcu_read_lock(&nodes_srcu);
  1238. /* Abort pending close/remove operation */
  1239. node = nodeid2node(nodeid);
  1240. if (node) {
  1241. /* let shutdown waiters leave */
  1242. set_bit(DLM_NODE_FLAG_CLOSE, &node->flags);
  1243. wake_up(&node->shutdown_wait);
  1244. }
  1245. srcu_read_unlock(&nodes_srcu, idx);
  1246. synchronize_srcu(&nodes_srcu);
  1247. mutex_lock(&close_lock);
  1248. idx = srcu_read_lock(&nodes_srcu);
  1249. node = nodeid2node(nodeid);
  1250. if (!node) {
  1251. srcu_read_unlock(&nodes_srcu, idx);
  1252. mutex_unlock(&close_lock);
  1253. return dlm_lowcomms_close(nodeid);
  1254. }
  1255. ret = dlm_lowcomms_close(nodeid);
  1256. dlm_delete_debug_comms_file(node->debugfs);
  1257. spin_lock_bh(&nodes_lock);
  1258. hlist_del_rcu(&node->hlist);
  1259. spin_unlock_bh(&nodes_lock);
  1260. srcu_read_unlock(&nodes_srcu, idx);
  1261. /* wait that all readers left until flush send queue */
  1262. synchronize_srcu(&nodes_srcu);
  1263. /* drop all pending dlm messages, this is fine as
  1264. * this function get called when the node is fenced
  1265. */
  1266. dlm_send_queue_flush(node);
  1267. call_srcu(&nodes_srcu, &node->rcu, midcomms_node_release);
  1268. mutex_unlock(&close_lock);
  1269. return ret;
  1270. }
  1271. /* debug functionality to send raw dlm msg from user space */
  1272. struct dlm_rawmsg_data {
  1273. struct midcomms_node *node;
  1274. void *buf;
  1275. };
  1276. static void midcomms_new_rawmsg_cb(void *data)
  1277. {
  1278. struct dlm_rawmsg_data *rd = data;
  1279. struct dlm_header *h = rd->buf;
  1280. switch (h->h_version) {
  1281. case cpu_to_le32(DLM_VERSION_3_1):
  1282. break;
  1283. default:
  1284. switch (h->h_cmd) {
  1285. case DLM_OPTS:
  1286. if (!h->u.h_seq)
  1287. h->u.h_seq = cpu_to_le32(atomic_fetch_inc(&rd->node->seq_send));
  1288. break;
  1289. default:
  1290. break;
  1291. }
  1292. break;
  1293. }
  1294. }
  1295. int dlm_midcomms_rawmsg_send(struct midcomms_node *node, void *buf,
  1296. int buflen)
  1297. {
  1298. struct dlm_rawmsg_data rd;
  1299. struct dlm_msg *msg;
  1300. char *msgbuf;
  1301. rd.node = node;
  1302. rd.buf = buf;
  1303. msg = dlm_lowcomms_new_msg(node->nodeid, buflen, &msgbuf,
  1304. midcomms_new_rawmsg_cb, &rd);
  1305. if (!msg)
  1306. return -ENOMEM;
  1307. memcpy(msgbuf, buf, buflen);
  1308. dlm_lowcomms_commit_msg(msg);
  1309. return 0;
  1310. }