lowcomms.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  1. /******************************************************************************
  2. *******************************************************************************
  3. **
  4. ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  5. ** Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
  6. **
  7. ** This copyrighted material is made available to anyone wishing to use,
  8. ** modify, copy, or redistribute it subject to the terms and conditions
  9. ** of the GNU General Public License v.2.
  10. **
  11. *******************************************************************************
  12. ******************************************************************************/
  13. /*
  14. * lowcomms.c
  15. *
  16. * This is the "low-level" comms layer.
  17. *
  18. * It is responsible for sending/receiving messages
  19. * from other nodes in the cluster.
  20. *
  21. * Cluster nodes are referred to by their nodeids. nodeids are
  22. * simply 32 bit numbers to the locking module - if they need to
  23. * be expanded for the cluster infrastructure then that is its
  24. * responsibility. It is this layer's
  25. * responsibility to resolve these into IP address or
  26. * whatever it needs for inter-node communication.
  27. *
  28. * The comms level is two kernel threads that deal mainly with
  29. * the receiving of messages from other nodes and passing them
  30. * up to the mid-level comms layer (which understands the
  31. * message format) for execution by the locking core, and
  32. * a send thread which does all the setting up of connections
  33. * to remote nodes and the sending of data. Threads are not allowed
  34. * to send their own data because it may cause them to wait in times
  35. * of high load. Also, this way, the sending thread can collect together
  36. * messages bound for one node and send them in one block.
  37. *
  38. * lowcomms will choose to use either TCP or SCTP as its transport layer
  39. * depending on the configuration variable 'protocol'. This should be set
  40. * to 0 (default) for TCP or 1 for SCTP. It should be configured using a
  41. * cluster-wide mechanism as it must be the same on all nodes of the cluster
  42. * for the DLM to function.
  43. *
  44. */
  45. #include <asm/ioctls.h>
  46. #include <net/sock.h>
  47. #include <net/tcp.h>
  48. #include <linux/pagemap.h>
  49. #include <linux/file.h>
  50. #include <linux/mutex.h>
  51. #include <linux/sctp.h>
  52. #include <linux/slab.h>
  53. #include <net/sctp/sctp.h>
  54. #include <net/sctp/user.h>
  55. #include <net/ipv6.h>
  56. #include "dlm_internal.h"
  57. #include "lowcomms.h"
  58. #include "midcomms.h"
  59. #include "config.h"
  60. #define NEEDED_RMEM (4*1024*1024)
  61. #define CONN_HASH_SIZE 32
  62. /* Number of messages to send before rescheduling */
  63. #define MAX_SEND_MSG_COUNT 25
  64. struct cbuf {
  65. unsigned int base;
  66. unsigned int len;
  67. unsigned int mask;
  68. };
  69. static void cbuf_add(struct cbuf *cb, int n)
  70. {
  71. cb->len += n;
  72. }
  73. static int cbuf_data(struct cbuf *cb)
  74. {
  75. return ((cb->base + cb->len) & cb->mask);
  76. }
  77. static void cbuf_init(struct cbuf *cb, int size)
  78. {
  79. cb->base = cb->len = 0;
  80. cb->mask = size-1;
  81. }
  82. static void cbuf_eat(struct cbuf *cb, int n)
  83. {
  84. cb->len -= n;
  85. cb->base += n;
  86. cb->base &= cb->mask;
  87. }
  88. static bool cbuf_empty(struct cbuf *cb)
  89. {
  90. return cb->len == 0;
  91. }
  92. struct connection {
  93. struct socket *sock; /* NULL if not connected */
  94. uint32_t nodeid; /* So we know who we are in the list */
  95. struct mutex sock_mutex;
  96. unsigned long flags;
  97. #define CF_READ_PENDING 1
  98. #define CF_WRITE_PENDING 2
  99. #define CF_CONNECT_PENDING 3
  100. #define CF_INIT_PENDING 4
  101. #define CF_IS_OTHERCON 5
  102. #define CF_CLOSE 6
  103. #define CF_APP_LIMITED 7
  104. struct list_head writequeue; /* List of outgoing writequeue_entries */
  105. spinlock_t writequeue_lock;
  106. int (*rx_action) (struct connection *); /* What to do when active */
  107. void (*connect_action) (struct connection *); /* What to do to connect */
  108. struct page *rx_page;
  109. struct cbuf cb;
  110. int retries;
  111. #define MAX_CONNECT_RETRIES 3
  112. int sctp_assoc;
  113. struct hlist_node list;
  114. struct connection *othercon;
  115. struct work_struct rwork; /* Receive workqueue */
  116. struct work_struct swork; /* Send workqueue */
  117. };
  118. #define sock2con(x) ((struct connection *)(x)->sk_user_data)
  119. /* An entry waiting to be sent */
  120. struct writequeue_entry {
  121. struct list_head list;
  122. struct page *page;
  123. int offset;
  124. int len;
  125. int end;
  126. int users;
  127. struct connection *con;
  128. };
  129. static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT];
  130. static int dlm_local_count;
  131. /* Work queues */
  132. static struct workqueue_struct *recv_workqueue;
  133. static struct workqueue_struct *send_workqueue;
  134. static struct hlist_head connection_hash[CONN_HASH_SIZE];
  135. static DEFINE_MUTEX(connections_lock);
  136. static struct kmem_cache *con_cache;
  137. static void process_recv_sockets(struct work_struct *work);
  138. static void process_send_sockets(struct work_struct *work);
  139. /* This is deliberately very simple because most clusters have simple
  140. sequential nodeids, so we should be able to go straight to a connection
  141. struct in the array */
  142. static inline int nodeid_hash(int nodeid)
  143. {
  144. return nodeid & (CONN_HASH_SIZE-1);
  145. }
  146. static struct connection *__find_con(int nodeid)
  147. {
  148. int r;
  149. struct hlist_node *h;
  150. struct connection *con;
  151. r = nodeid_hash(nodeid);
  152. hlist_for_each_entry(con, h, &connection_hash[r], list) {
  153. if (con->nodeid == nodeid)
  154. return con;
  155. }
  156. return NULL;
  157. }
  158. /*
  159. * If 'allocation' is zero then we don't attempt to create a new
  160. * connection structure for this node.
  161. */
  162. static struct connection *__nodeid2con(int nodeid, gfp_t alloc)
  163. {
  164. struct connection *con = NULL;
  165. int r;
  166. con = __find_con(nodeid);
  167. if (con || !alloc)
  168. return con;
  169. con = kmem_cache_zalloc(con_cache, alloc);
  170. if (!con)
  171. return NULL;
  172. r = nodeid_hash(nodeid);
  173. hlist_add_head(&con->list, &connection_hash[r]);
  174. con->nodeid = nodeid;
  175. mutex_init(&con->sock_mutex);
  176. INIT_LIST_HEAD(&con->writequeue);
  177. spin_lock_init(&con->writequeue_lock);
  178. INIT_WORK(&con->swork, process_send_sockets);
  179. INIT_WORK(&con->rwork, process_recv_sockets);
  180. /* Setup action pointers for child sockets */
  181. if (con->nodeid) {
  182. struct connection *zerocon = __find_con(0);
  183. con->connect_action = zerocon->connect_action;
  184. if (!con->rx_action)
  185. con->rx_action = zerocon->rx_action;
  186. }
  187. return con;
  188. }
  189. /* Loop round all connections */
  190. static void foreach_conn(void (*conn_func)(struct connection *c))
  191. {
  192. int i;
  193. struct hlist_node *h, *n;
  194. struct connection *con;
  195. for (i = 0; i < CONN_HASH_SIZE; i++) {
  196. hlist_for_each_entry_safe(con, h, n, &connection_hash[i], list){
  197. conn_func(con);
  198. }
  199. }
  200. }
  201. static struct connection *nodeid2con(int nodeid, gfp_t allocation)
  202. {
  203. struct connection *con;
  204. mutex_lock(&connections_lock);
  205. con = __nodeid2con(nodeid, allocation);
  206. mutex_unlock(&connections_lock);
  207. return con;
  208. }
  209. /* This is a bit drastic, but only called when things go wrong */
  210. static struct connection *assoc2con(int assoc_id)
  211. {
  212. int i;
  213. struct hlist_node *h;
  214. struct connection *con;
  215. mutex_lock(&connections_lock);
  216. for (i = 0 ; i < CONN_HASH_SIZE; i++) {
  217. hlist_for_each_entry(con, h, &connection_hash[i], list) {
  218. if (con->sctp_assoc == assoc_id) {
  219. mutex_unlock(&connections_lock);
  220. return con;
  221. }
  222. }
  223. }
  224. mutex_unlock(&connections_lock);
  225. return NULL;
  226. }
  227. static int nodeid_to_addr(int nodeid, struct sockaddr *retaddr)
  228. {
  229. struct sockaddr_storage addr;
  230. int error;
  231. if (!dlm_local_count)
  232. return -1;
  233. error = dlm_nodeid_to_addr(nodeid, &addr);
  234. if (error)
  235. return error;
  236. if (dlm_local_addr[0]->ss_family == AF_INET) {
  237. struct sockaddr_in *in4 = (struct sockaddr_in *) &addr;
  238. struct sockaddr_in *ret4 = (struct sockaddr_in *) retaddr;
  239. ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
  240. } else {
  241. struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &addr;
  242. struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) retaddr;
  243. ret6->sin6_addr = in6->sin6_addr;
  244. }
  245. return 0;
  246. }
  247. /* Data available on socket or listen socket received a connect */
  248. static void lowcomms_data_ready(struct sock *sk, int count_unused)
  249. {
  250. struct connection *con = sock2con(sk);
  251. if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags))
  252. queue_work(recv_workqueue, &con->rwork);
  253. }
  254. static void lowcomms_write_space(struct sock *sk)
  255. {
  256. struct connection *con = sock2con(sk);
  257. if (!con)
  258. return;
  259. clear_bit(SOCK_NOSPACE, &con->sock->flags);
  260. if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) {
  261. con->sock->sk->sk_write_pending--;
  262. clear_bit(SOCK_ASYNC_NOSPACE, &con->sock->flags);
  263. }
  264. if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags))
  265. queue_work(send_workqueue, &con->swork);
  266. }
  267. static inline void lowcomms_connect_sock(struct connection *con)
  268. {
  269. if (test_bit(CF_CLOSE, &con->flags))
  270. return;
  271. if (!test_and_set_bit(CF_CONNECT_PENDING, &con->flags))
  272. queue_work(send_workqueue, &con->swork);
  273. }
  274. static void lowcomms_state_change(struct sock *sk)
  275. {
  276. if (sk->sk_state == TCP_ESTABLISHED)
  277. lowcomms_write_space(sk);
  278. }
  279. int dlm_lowcomms_connect_node(int nodeid)
  280. {
  281. struct connection *con;
  282. /* with sctp there's no connecting without sending */
  283. if (dlm_config.ci_protocol != 0)
  284. return 0;
  285. if (nodeid == dlm_our_nodeid())
  286. return 0;
  287. con = nodeid2con(nodeid, GFP_NOFS);
  288. if (!con)
  289. return -ENOMEM;
  290. lowcomms_connect_sock(con);
  291. return 0;
  292. }
  293. /* Make a socket active */
  294. static int add_sock(struct socket *sock, struct connection *con)
  295. {
  296. con->sock = sock;
  297. /* Install a data_ready callback */
  298. con->sock->sk->sk_data_ready = lowcomms_data_ready;
  299. con->sock->sk->sk_write_space = lowcomms_write_space;
  300. con->sock->sk->sk_state_change = lowcomms_state_change;
  301. con->sock->sk->sk_user_data = con;
  302. con->sock->sk->sk_allocation = GFP_NOFS;
  303. return 0;
  304. }
  305. /* Add the port number to an IPv6 or 4 sockaddr and return the address
  306. length */
  307. static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
  308. int *addr_len)
  309. {
  310. saddr->ss_family = dlm_local_addr[0]->ss_family;
  311. if (saddr->ss_family == AF_INET) {
  312. struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
  313. in4_addr->sin_port = cpu_to_be16(port);
  314. *addr_len = sizeof(struct sockaddr_in);
  315. memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
  316. } else {
  317. struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
  318. in6_addr->sin6_port = cpu_to_be16(port);
  319. *addr_len = sizeof(struct sockaddr_in6);
  320. }
  321. memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
  322. }
  323. /* Close a remote connection and tidy up */
  324. static void close_connection(struct connection *con, bool and_other)
  325. {
  326. mutex_lock(&con->sock_mutex);
  327. if (con->sock) {
  328. sock_release(con->sock);
  329. con->sock = NULL;
  330. }
  331. if (con->othercon && and_other) {
  332. /* Will only re-enter once. */
  333. close_connection(con->othercon, false);
  334. }
  335. if (con->rx_page) {
  336. __free_page(con->rx_page);
  337. con->rx_page = NULL;
  338. }
  339. con->retries = 0;
  340. mutex_unlock(&con->sock_mutex);
  341. }
  342. /* We only send shutdown messages to nodes that are not part of the cluster */
  343. static void sctp_send_shutdown(sctp_assoc_t associd)
  344. {
  345. static char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
  346. struct msghdr outmessage;
  347. struct cmsghdr *cmsg;
  348. struct sctp_sndrcvinfo *sinfo;
  349. int ret;
  350. struct connection *con;
  351. con = nodeid2con(0,0);
  352. BUG_ON(con == NULL);
  353. outmessage.msg_name = NULL;
  354. outmessage.msg_namelen = 0;
  355. outmessage.msg_control = outcmsg;
  356. outmessage.msg_controllen = sizeof(outcmsg);
  357. outmessage.msg_flags = MSG_EOR;
  358. cmsg = CMSG_FIRSTHDR(&outmessage);
  359. cmsg->cmsg_level = IPPROTO_SCTP;
  360. cmsg->cmsg_type = SCTP_SNDRCV;
  361. cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
  362. outmessage.msg_controllen = cmsg->cmsg_len;
  363. sinfo = CMSG_DATA(cmsg);
  364. memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
  365. sinfo->sinfo_flags |= MSG_EOF;
  366. sinfo->sinfo_assoc_id = associd;
  367. ret = kernel_sendmsg(con->sock, &outmessage, NULL, 0, 0);
  368. if (ret != 0)
  369. log_print("send EOF to node failed: %d", ret);
  370. }
  371. static void sctp_init_failed_foreach(struct connection *con)
  372. {
  373. con->sctp_assoc = 0;
  374. if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
  375. if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags))
  376. queue_work(send_workqueue, &con->swork);
  377. }
  378. }
  379. /* INIT failed but we don't know which node...
  380. restart INIT on all pending nodes */
  381. static void sctp_init_failed(void)
  382. {
  383. mutex_lock(&connections_lock);
  384. foreach_conn(sctp_init_failed_foreach);
  385. mutex_unlock(&connections_lock);
  386. }
  387. /* Something happened to an association */
  388. static void process_sctp_notification(struct connection *con,
  389. struct msghdr *msg, char *buf)
  390. {
  391. union sctp_notification *sn = (union sctp_notification *)buf;
  392. if (sn->sn_header.sn_type == SCTP_ASSOC_CHANGE) {
  393. switch (sn->sn_assoc_change.sac_state) {
  394. case SCTP_COMM_UP:
  395. case SCTP_RESTART:
  396. {
  397. /* Check that the new node is in the lockspace */
  398. struct sctp_prim prim;
  399. int nodeid;
  400. int prim_len, ret;
  401. int addr_len;
  402. struct connection *new_con;
  403. /*
  404. * We get this before any data for an association.
  405. * We verify that the node is in the cluster and
  406. * then peel off a socket for it.
  407. */
  408. if ((int)sn->sn_assoc_change.sac_assoc_id <= 0) {
  409. log_print("COMM_UP for invalid assoc ID %d",
  410. (int)sn->sn_assoc_change.sac_assoc_id);
  411. sctp_init_failed();
  412. return;
  413. }
  414. memset(&prim, 0, sizeof(struct sctp_prim));
  415. prim_len = sizeof(struct sctp_prim);
  416. prim.ssp_assoc_id = sn->sn_assoc_change.sac_assoc_id;
  417. ret = kernel_getsockopt(con->sock,
  418. IPPROTO_SCTP,
  419. SCTP_PRIMARY_ADDR,
  420. (char*)&prim,
  421. &prim_len);
  422. if (ret < 0) {
  423. log_print("getsockopt/sctp_primary_addr on "
  424. "new assoc %d failed : %d",
  425. (int)sn->sn_assoc_change.sac_assoc_id,
  426. ret);
  427. /* Retry INIT later */
  428. new_con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
  429. if (new_con)
  430. clear_bit(CF_CONNECT_PENDING, &con->flags);
  431. return;
  432. }
  433. make_sockaddr(&prim.ssp_addr, 0, &addr_len);
  434. if (dlm_addr_to_nodeid(&prim.ssp_addr, &nodeid)) {
  435. unsigned char *b=(unsigned char *)&prim.ssp_addr;
  436. log_print("reject connect from unknown addr");
  437. print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
  438. b, sizeof(struct sockaddr_storage));
  439. sctp_send_shutdown(prim.ssp_assoc_id);
  440. return;
  441. }
  442. new_con = nodeid2con(nodeid, GFP_NOFS);
  443. if (!new_con)
  444. return;
  445. /* Peel off a new sock */
  446. sctp_lock_sock(con->sock->sk);
  447. ret = sctp_do_peeloff(con->sock->sk,
  448. sn->sn_assoc_change.sac_assoc_id,
  449. &new_con->sock);
  450. sctp_release_sock(con->sock->sk);
  451. if (ret < 0) {
  452. log_print("Can't peel off a socket for "
  453. "connection %d to node %d: err=%d",
  454. (int)sn->sn_assoc_change.sac_assoc_id,
  455. nodeid, ret);
  456. return;
  457. }
  458. add_sock(new_con->sock, new_con);
  459. log_print("connecting to %d sctp association %d",
  460. nodeid, (int)sn->sn_assoc_change.sac_assoc_id);
  461. /* Send any pending writes */
  462. clear_bit(CF_CONNECT_PENDING, &new_con->flags);
  463. clear_bit(CF_INIT_PENDING, &con->flags);
  464. if (!test_and_set_bit(CF_WRITE_PENDING, &new_con->flags)) {
  465. queue_work(send_workqueue, &new_con->swork);
  466. }
  467. if (!test_and_set_bit(CF_READ_PENDING, &new_con->flags))
  468. queue_work(recv_workqueue, &new_con->rwork);
  469. }
  470. break;
  471. case SCTP_COMM_LOST:
  472. case SCTP_SHUTDOWN_COMP:
  473. {
  474. con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
  475. if (con) {
  476. con->sctp_assoc = 0;
  477. }
  478. }
  479. break;
  480. /* We don't know which INIT failed, so clear the PENDING flags
  481. * on them all. if assoc_id is zero then it will then try
  482. * again */
  483. case SCTP_CANT_STR_ASSOC:
  484. {
  485. log_print("Can't start SCTP association - retrying");
  486. sctp_init_failed();
  487. }
  488. break;
  489. default:
  490. log_print("unexpected SCTP assoc change id=%d state=%d",
  491. (int)sn->sn_assoc_change.sac_assoc_id,
  492. sn->sn_assoc_change.sac_state);
  493. }
  494. }
  495. }
  496. /* Data received from remote end */
  497. static int receive_from_sock(struct connection *con)
  498. {
  499. int ret = 0;
  500. struct msghdr msg = {};
  501. struct kvec iov[2];
  502. unsigned len;
  503. int r;
  504. int call_again_soon = 0;
  505. int nvec;
  506. char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
  507. mutex_lock(&con->sock_mutex);
  508. if (con->sock == NULL) {
  509. ret = -EAGAIN;
  510. goto out_close;
  511. }
  512. if (con->rx_page == NULL) {
  513. /*
  514. * This doesn't need to be atomic, but I think it should
  515. * improve performance if it is.
  516. */
  517. con->rx_page = alloc_page(GFP_ATOMIC);
  518. if (con->rx_page == NULL)
  519. goto out_resched;
  520. cbuf_init(&con->cb, PAGE_CACHE_SIZE);
  521. }
  522. /* Only SCTP needs these really */
  523. memset(&incmsg, 0, sizeof(incmsg));
  524. msg.msg_control = incmsg;
  525. msg.msg_controllen = sizeof(incmsg);
  526. /*
  527. * iov[0] is the bit of the circular buffer between the current end
  528. * point (cb.base + cb.len) and the end of the buffer.
  529. */
  530. iov[0].iov_len = con->cb.base - cbuf_data(&con->cb);
  531. iov[0].iov_base = page_address(con->rx_page) + cbuf_data(&con->cb);
  532. iov[1].iov_len = 0;
  533. nvec = 1;
  534. /*
  535. * iov[1] is the bit of the circular buffer between the start of the
  536. * buffer and the start of the currently used section (cb.base)
  537. */
  538. if (cbuf_data(&con->cb) >= con->cb.base) {
  539. iov[0].iov_len = PAGE_CACHE_SIZE - cbuf_data(&con->cb);
  540. iov[1].iov_len = con->cb.base;
  541. iov[1].iov_base = page_address(con->rx_page);
  542. nvec = 2;
  543. }
  544. len = iov[0].iov_len + iov[1].iov_len;
  545. r = ret = kernel_recvmsg(con->sock, &msg, iov, nvec, len,
  546. MSG_DONTWAIT | MSG_NOSIGNAL);
  547. if (ret <= 0)
  548. goto out_close;
  549. /* Process SCTP notifications */
  550. if (msg.msg_flags & MSG_NOTIFICATION) {
  551. msg.msg_control = incmsg;
  552. msg.msg_controllen = sizeof(incmsg);
  553. process_sctp_notification(con, &msg,
  554. page_address(con->rx_page) + con->cb.base);
  555. mutex_unlock(&con->sock_mutex);
  556. return 0;
  557. }
  558. BUG_ON(con->nodeid == 0);
  559. if (ret == len)
  560. call_again_soon = 1;
  561. cbuf_add(&con->cb, ret);
  562. ret = dlm_process_incoming_buffer(con->nodeid,
  563. page_address(con->rx_page),
  564. con->cb.base, con->cb.len,
  565. PAGE_CACHE_SIZE);
  566. if (ret == -EBADMSG) {
  567. log_print("lowcomms: addr=%p, base=%u, len=%u, "
  568. "iov_len=%u, iov_base[0]=%p, read=%d",
  569. page_address(con->rx_page), con->cb.base, con->cb.len,
  570. len, iov[0].iov_base, r);
  571. }
  572. if (ret < 0)
  573. goto out_close;
  574. cbuf_eat(&con->cb, ret);
  575. if (cbuf_empty(&con->cb) && !call_again_soon) {
  576. __free_page(con->rx_page);
  577. con->rx_page = NULL;
  578. }
  579. if (call_again_soon)
  580. goto out_resched;
  581. mutex_unlock(&con->sock_mutex);
  582. return 0;
  583. out_resched:
  584. if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
  585. queue_work(recv_workqueue, &con->rwork);
  586. mutex_unlock(&con->sock_mutex);
  587. return -EAGAIN;
  588. out_close:
  589. mutex_unlock(&con->sock_mutex);
  590. if (ret != -EAGAIN) {
  591. close_connection(con, false);
  592. /* Reconnect when there is something to send */
  593. }
  594. /* Don't return success if we really got EOF */
  595. if (ret == 0)
  596. ret = -EAGAIN;
  597. return ret;
  598. }
  599. /* Listening socket is busy, accept a connection */
  600. static int tcp_accept_from_sock(struct connection *con)
  601. {
  602. int result;
  603. struct sockaddr_storage peeraddr;
  604. struct socket *newsock;
  605. int len;
  606. int nodeid;
  607. struct connection *newcon;
  608. struct connection *addcon;
  609. memset(&peeraddr, 0, sizeof(peeraddr));
  610. result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
  611. IPPROTO_TCP, &newsock);
  612. if (result < 0)
  613. return -ENOMEM;
  614. mutex_lock_nested(&con->sock_mutex, 0);
  615. result = -ENOTCONN;
  616. if (con->sock == NULL)
  617. goto accept_err;
  618. newsock->type = con->sock->type;
  619. newsock->ops = con->sock->ops;
  620. result = con->sock->ops->accept(con->sock, newsock, O_NONBLOCK);
  621. if (result < 0)
  622. goto accept_err;
  623. /* Get the connected socket's peer */
  624. memset(&peeraddr, 0, sizeof(peeraddr));
  625. if (newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr,
  626. &len, 2)) {
  627. result = -ECONNABORTED;
  628. goto accept_err;
  629. }
  630. /* Get the new node's NODEID */
  631. make_sockaddr(&peeraddr, 0, &len);
  632. if (dlm_addr_to_nodeid(&peeraddr, &nodeid)) {
  633. unsigned char *b=(unsigned char *)&peeraddr;
  634. log_print("connect from non cluster node");
  635. print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
  636. b, sizeof(struct sockaddr_storage));
  637. sock_release(newsock);
  638. mutex_unlock(&con->sock_mutex);
  639. return -1;
  640. }
  641. log_print("got connection from %d", nodeid);
  642. /* Check to see if we already have a connection to this node. This
  643. * could happen if the two nodes initiate a connection at roughly
  644. * the same time and the connections cross on the wire.
  645. * In this case we store the incoming one in "othercon"
  646. */
  647. newcon = nodeid2con(nodeid, GFP_NOFS);
  648. if (!newcon) {
  649. result = -ENOMEM;
  650. goto accept_err;
  651. }
  652. mutex_lock_nested(&newcon->sock_mutex, 1);
  653. if (newcon->sock) {
  654. struct connection *othercon = newcon->othercon;
  655. if (!othercon) {
  656. othercon = kmem_cache_zalloc(con_cache, GFP_NOFS);
  657. if (!othercon) {
  658. log_print("failed to allocate incoming socket");
  659. mutex_unlock(&newcon->sock_mutex);
  660. result = -ENOMEM;
  661. goto accept_err;
  662. }
  663. othercon->nodeid = nodeid;
  664. othercon->rx_action = receive_from_sock;
  665. mutex_init(&othercon->sock_mutex);
  666. INIT_WORK(&othercon->swork, process_send_sockets);
  667. INIT_WORK(&othercon->rwork, process_recv_sockets);
  668. set_bit(CF_IS_OTHERCON, &othercon->flags);
  669. }
  670. if (!othercon->sock) {
  671. newcon->othercon = othercon;
  672. othercon->sock = newsock;
  673. newsock->sk->sk_user_data = othercon;
  674. add_sock(newsock, othercon);
  675. addcon = othercon;
  676. }
  677. else {
  678. printk("Extra connection from node %d attempted\n", nodeid);
  679. result = -EAGAIN;
  680. mutex_unlock(&newcon->sock_mutex);
  681. goto accept_err;
  682. }
  683. }
  684. else {
  685. newsock->sk->sk_user_data = newcon;
  686. newcon->rx_action = receive_from_sock;
  687. add_sock(newsock, newcon);
  688. addcon = newcon;
  689. }
  690. mutex_unlock(&newcon->sock_mutex);
  691. /*
  692. * Add it to the active queue in case we got data
  693. * between processing the accept adding the socket
  694. * to the read_sockets list
  695. */
  696. if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
  697. queue_work(recv_workqueue, &addcon->rwork);
  698. mutex_unlock(&con->sock_mutex);
  699. return 0;
  700. accept_err:
  701. mutex_unlock(&con->sock_mutex);
  702. sock_release(newsock);
  703. if (result != -EAGAIN)
  704. log_print("error accepting connection from node: %d", result);
  705. return result;
  706. }
  707. static void free_entry(struct writequeue_entry *e)
  708. {
  709. __free_page(e->page);
  710. kfree(e);
  711. }
  712. /* Initiate an SCTP association.
  713. This is a special case of send_to_sock() in that we don't yet have a
  714. peeled-off socket for this association, so we use the listening socket
  715. and add the primary IP address of the remote node.
  716. */
  717. static void sctp_init_assoc(struct connection *con)
  718. {
  719. struct sockaddr_storage rem_addr;
  720. char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
  721. struct msghdr outmessage;
  722. struct cmsghdr *cmsg;
  723. struct sctp_sndrcvinfo *sinfo;
  724. struct connection *base_con;
  725. struct writequeue_entry *e;
  726. int len, offset;
  727. int ret;
  728. int addrlen;
  729. struct kvec iov[1];
  730. if (test_and_set_bit(CF_INIT_PENDING, &con->flags))
  731. return;
  732. if (con->retries++ > MAX_CONNECT_RETRIES)
  733. return;
  734. if (nodeid_to_addr(con->nodeid, (struct sockaddr *)&rem_addr)) {
  735. log_print("no address for nodeid %d", con->nodeid);
  736. return;
  737. }
  738. base_con = nodeid2con(0, 0);
  739. BUG_ON(base_con == NULL);
  740. make_sockaddr(&rem_addr, dlm_config.ci_tcp_port, &addrlen);
  741. outmessage.msg_name = &rem_addr;
  742. outmessage.msg_namelen = addrlen;
  743. outmessage.msg_control = outcmsg;
  744. outmessage.msg_controllen = sizeof(outcmsg);
  745. outmessage.msg_flags = MSG_EOR;
  746. spin_lock(&con->writequeue_lock);
  747. if (list_empty(&con->writequeue)) {
  748. spin_unlock(&con->writequeue_lock);
  749. log_print("writequeue empty for nodeid %d", con->nodeid);
  750. return;
  751. }
  752. e = list_first_entry(&con->writequeue, struct writequeue_entry, list);
  753. len = e->len;
  754. offset = e->offset;
  755. spin_unlock(&con->writequeue_lock);
  756. /* Send the first block off the write queue */
  757. iov[0].iov_base = page_address(e->page)+offset;
  758. iov[0].iov_len = len;
  759. cmsg = CMSG_FIRSTHDR(&outmessage);
  760. cmsg->cmsg_level = IPPROTO_SCTP;
  761. cmsg->cmsg_type = SCTP_SNDRCV;
  762. cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
  763. sinfo = CMSG_DATA(cmsg);
  764. memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
  765. sinfo->sinfo_ppid = cpu_to_le32(dlm_our_nodeid());
  766. outmessage.msg_controllen = cmsg->cmsg_len;
  767. ret = kernel_sendmsg(base_con->sock, &outmessage, iov, 1, len);
  768. if (ret < 0) {
  769. log_print("Send first packet to node %d failed: %d",
  770. con->nodeid, ret);
  771. /* Try again later */
  772. clear_bit(CF_CONNECT_PENDING, &con->flags);
  773. clear_bit(CF_INIT_PENDING, &con->flags);
  774. }
  775. else {
  776. spin_lock(&con->writequeue_lock);
  777. e->offset += ret;
  778. e->len -= ret;
  779. if (e->len == 0 && e->users == 0) {
  780. list_del(&e->list);
  781. free_entry(e);
  782. }
  783. spin_unlock(&con->writequeue_lock);
  784. }
  785. }
  786. /* Connect a new socket to its peer */
  787. static void tcp_connect_to_sock(struct connection *con)
  788. {
  789. int result = -EHOSTUNREACH;
  790. struct sockaddr_storage saddr, src_addr;
  791. int addr_len;
  792. struct socket *sock = NULL;
  793. int one = 1;
  794. if (con->nodeid == 0) {
  795. log_print("attempt to connect sock 0 foiled");
  796. return;
  797. }
  798. mutex_lock(&con->sock_mutex);
  799. if (con->retries++ > MAX_CONNECT_RETRIES)
  800. goto out;
  801. /* Some odd races can cause double-connects, ignore them */
  802. if (con->sock) {
  803. result = 0;
  804. goto out;
  805. }
  806. /* Create a socket to communicate with */
  807. result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
  808. IPPROTO_TCP, &sock);
  809. if (result < 0)
  810. goto out_err;
  811. memset(&saddr, 0, sizeof(saddr));
  812. if (dlm_nodeid_to_addr(con->nodeid, &saddr))
  813. goto out_err;
  814. sock->sk->sk_user_data = con;
  815. con->rx_action = receive_from_sock;
  816. con->connect_action = tcp_connect_to_sock;
  817. add_sock(sock, con);
  818. /* Bind to our cluster-known address connecting to avoid
  819. routing problems */
  820. memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr));
  821. make_sockaddr(&src_addr, 0, &addr_len);
  822. result = sock->ops->bind(sock, (struct sockaddr *) &src_addr,
  823. addr_len);
  824. if (result < 0) {
  825. log_print("could not bind for connect: %d", result);
  826. /* This *may* not indicate a critical error */
  827. }
  828. make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
  829. log_print("connecting to %d", con->nodeid);
  830. /* Turn off Nagle's algorithm */
  831. kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&one,
  832. sizeof(one));
  833. result =
  834. sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
  835. O_NONBLOCK);
  836. if (result == -EINPROGRESS)
  837. result = 0;
  838. if (result == 0)
  839. goto out;
  840. out_err:
  841. if (con->sock) {
  842. sock_release(con->sock);
  843. con->sock = NULL;
  844. } else if (sock) {
  845. sock_release(sock);
  846. }
  847. /*
  848. * Some errors are fatal and this list might need adjusting. For other
  849. * errors we try again until the max number of retries is reached.
  850. */
  851. if (result != -EHOSTUNREACH && result != -ENETUNREACH &&
  852. result != -ENETDOWN && result != -EINVAL
  853. && result != -EPROTONOSUPPORT) {
  854. lowcomms_connect_sock(con);
  855. result = 0;
  856. }
  857. out:
  858. mutex_unlock(&con->sock_mutex);
  859. return;
  860. }
  861. static struct socket *tcp_create_listen_sock(struct connection *con,
  862. struct sockaddr_storage *saddr)
  863. {
  864. struct socket *sock = NULL;
  865. int result = 0;
  866. int one = 1;
  867. int addr_len;
  868. if (dlm_local_addr[0]->ss_family == AF_INET)
  869. addr_len = sizeof(struct sockaddr_in);
  870. else
  871. addr_len = sizeof(struct sockaddr_in6);
  872. /* Create a socket to communicate with */
  873. result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
  874. IPPROTO_TCP, &sock);
  875. if (result < 0) {
  876. log_print("Can't create listening comms socket");
  877. goto create_out;
  878. }
  879. /* Turn off Nagle's algorithm */
  880. kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&one,
  881. sizeof(one));
  882. result = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
  883. (char *)&one, sizeof(one));
  884. if (result < 0) {
  885. log_print("Failed to set SO_REUSEADDR on socket: %d", result);
  886. }
  887. sock->sk->sk_user_data = con;
  888. con->rx_action = tcp_accept_from_sock;
  889. con->connect_action = tcp_connect_to_sock;
  890. con->sock = sock;
  891. /* Bind to our port */
  892. make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
  893. result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
  894. if (result < 0) {
  895. log_print("Can't bind to port %d", dlm_config.ci_tcp_port);
  896. sock_release(sock);
  897. sock = NULL;
  898. con->sock = NULL;
  899. goto create_out;
  900. }
  901. result = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
  902. (char *)&one, sizeof(one));
  903. if (result < 0) {
  904. log_print("Set keepalive failed: %d", result);
  905. }
  906. result = sock->ops->listen(sock, 5);
  907. if (result < 0) {
  908. log_print("Can't listen on port %d", dlm_config.ci_tcp_port);
  909. sock_release(sock);
  910. sock = NULL;
  911. goto create_out;
  912. }
  913. create_out:
  914. return sock;
  915. }
  916. /* Get local addresses */
  917. static void init_local(void)
  918. {
  919. struct sockaddr_storage sas, *addr;
  920. int i;
  921. dlm_local_count = 0;
  922. for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) {
  923. if (dlm_our_addr(&sas, i))
  924. break;
  925. addr = kmalloc(sizeof(*addr), GFP_NOFS);
  926. if (!addr)
  927. break;
  928. memcpy(addr, &sas, sizeof(*addr));
  929. dlm_local_addr[dlm_local_count++] = addr;
  930. }
  931. }
  932. /* Bind to an IP address. SCTP allows multiple address so it can do
  933. multi-homing */
  934. static int add_sctp_bind_addr(struct connection *sctp_con,
  935. struct sockaddr_storage *addr,
  936. int addr_len, int num)
  937. {
  938. int result = 0;
  939. if (num == 1)
  940. result = kernel_bind(sctp_con->sock,
  941. (struct sockaddr *) addr,
  942. addr_len);
  943. else
  944. result = kernel_setsockopt(sctp_con->sock, SOL_SCTP,
  945. SCTP_SOCKOPT_BINDX_ADD,
  946. (char *)addr, addr_len);
  947. if (result < 0)
  948. log_print("Can't bind to port %d addr number %d",
  949. dlm_config.ci_tcp_port, num);
  950. return result;
  951. }
  952. /* Initialise SCTP socket and bind to all interfaces */
  953. static int sctp_listen_for_all(void)
  954. {
  955. struct socket *sock = NULL;
  956. struct sockaddr_storage localaddr;
  957. struct sctp_event_subscribe subscribe;
  958. int result = -EINVAL, num = 1, i, addr_len;
  959. struct connection *con = nodeid2con(0, GFP_NOFS);
  960. int bufsize = NEEDED_RMEM;
  961. if (!con)
  962. return -ENOMEM;
  963. log_print("Using SCTP for communications");
  964. result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_SEQPACKET,
  965. IPPROTO_SCTP, &sock);
  966. if (result < 0) {
  967. log_print("Can't create comms socket, check SCTP is loaded");
  968. goto out;
  969. }
  970. /* Listen for events */
  971. memset(&subscribe, 0, sizeof(subscribe));
  972. subscribe.sctp_data_io_event = 1;
  973. subscribe.sctp_association_event = 1;
  974. subscribe.sctp_send_failure_event = 1;
  975. subscribe.sctp_shutdown_event = 1;
  976. subscribe.sctp_partial_delivery_event = 1;
  977. result = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUFFORCE,
  978. (char *)&bufsize, sizeof(bufsize));
  979. if (result)
  980. log_print("Error increasing buffer space on socket %d", result);
  981. result = kernel_setsockopt(sock, SOL_SCTP, SCTP_EVENTS,
  982. (char *)&subscribe, sizeof(subscribe));
  983. if (result < 0) {
  984. log_print("Failed to set SCTP_EVENTS on socket: result=%d",
  985. result);
  986. goto create_delsock;
  987. }
  988. /* Init con struct */
  989. sock->sk->sk_user_data = con;
  990. con->sock = sock;
  991. con->sock->sk->sk_data_ready = lowcomms_data_ready;
  992. con->rx_action = receive_from_sock;
  993. con->connect_action = sctp_init_assoc;
  994. /* Bind to all interfaces. */
  995. for (i = 0; i < dlm_local_count; i++) {
  996. memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
  997. make_sockaddr(&localaddr, dlm_config.ci_tcp_port, &addr_len);
  998. result = add_sctp_bind_addr(con, &localaddr, addr_len, num);
  999. if (result)
  1000. goto create_delsock;
  1001. ++num;
  1002. }
  1003. result = sock->ops->listen(sock, 5);
  1004. if (result < 0) {
  1005. log_print("Can't set socket listening");
  1006. goto create_delsock;
  1007. }
  1008. return 0;
  1009. create_delsock:
  1010. sock_release(sock);
  1011. con->sock = NULL;
  1012. out:
  1013. return result;
  1014. }
  1015. static int tcp_listen_for_all(void)
  1016. {
  1017. struct socket *sock = NULL;
  1018. struct connection *con = nodeid2con(0, GFP_NOFS);
  1019. int result = -EINVAL;
  1020. if (!con)
  1021. return -ENOMEM;
  1022. /* We don't support multi-homed hosts */
  1023. if (dlm_local_addr[1] != NULL) {
  1024. log_print("TCP protocol can't handle multi-homed hosts, "
  1025. "try SCTP");
  1026. return -EINVAL;
  1027. }
  1028. log_print("Using TCP for communications");
  1029. sock = tcp_create_listen_sock(con, dlm_local_addr[0]);
  1030. if (sock) {
  1031. add_sock(sock, con);
  1032. result = 0;
  1033. }
  1034. else {
  1035. result = -EADDRINUSE;
  1036. }
  1037. return result;
  1038. }
  1039. static struct writequeue_entry *new_writequeue_entry(struct connection *con,
  1040. gfp_t allocation)
  1041. {
  1042. struct writequeue_entry *entry;
  1043. entry = kmalloc(sizeof(struct writequeue_entry), allocation);
  1044. if (!entry)
  1045. return NULL;
  1046. entry->page = alloc_page(allocation);
  1047. if (!entry->page) {
  1048. kfree(entry);
  1049. return NULL;
  1050. }
  1051. entry->offset = 0;
  1052. entry->len = 0;
  1053. entry->end = 0;
  1054. entry->users = 0;
  1055. entry->con = con;
  1056. return entry;
  1057. }
  1058. void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc)
  1059. {
  1060. struct connection *con;
  1061. struct writequeue_entry *e;
  1062. int offset = 0;
  1063. int users = 0;
  1064. con = nodeid2con(nodeid, allocation);
  1065. if (!con)
  1066. return NULL;
  1067. spin_lock(&con->writequeue_lock);
  1068. e = list_entry(con->writequeue.prev, struct writequeue_entry, list);
  1069. if ((&e->list == &con->writequeue) ||
  1070. (PAGE_CACHE_SIZE - e->end < len)) {
  1071. e = NULL;
  1072. } else {
  1073. offset = e->end;
  1074. e->end += len;
  1075. users = e->users++;
  1076. }
  1077. spin_unlock(&con->writequeue_lock);
  1078. if (e) {
  1079. got_one:
  1080. *ppc = page_address(e->page) + offset;
  1081. return e;
  1082. }
  1083. e = new_writequeue_entry(con, allocation);
  1084. if (e) {
  1085. spin_lock(&con->writequeue_lock);
  1086. offset = e->end;
  1087. e->end += len;
  1088. users = e->users++;
  1089. list_add_tail(&e->list, &con->writequeue);
  1090. spin_unlock(&con->writequeue_lock);
  1091. goto got_one;
  1092. }
  1093. return NULL;
  1094. }
  1095. void dlm_lowcomms_commit_buffer(void *mh)
  1096. {
  1097. struct writequeue_entry *e = (struct writequeue_entry *)mh;
  1098. struct connection *con = e->con;
  1099. int users;
  1100. spin_lock(&con->writequeue_lock);
  1101. users = --e->users;
  1102. if (users)
  1103. goto out;
  1104. e->len = e->end - e->offset;
  1105. spin_unlock(&con->writequeue_lock);
  1106. if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
  1107. queue_work(send_workqueue, &con->swork);
  1108. }
  1109. return;
  1110. out:
  1111. spin_unlock(&con->writequeue_lock);
  1112. return;
  1113. }
  1114. /* Send a message */
  1115. static void send_to_sock(struct connection *con)
  1116. {
  1117. int ret = 0;
  1118. const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
  1119. struct writequeue_entry *e;
  1120. int len, offset;
  1121. int count = 0;
  1122. mutex_lock(&con->sock_mutex);
  1123. if (con->sock == NULL)
  1124. goto out_connect;
  1125. spin_lock(&con->writequeue_lock);
  1126. for (;;) {
  1127. e = list_entry(con->writequeue.next, struct writequeue_entry,
  1128. list);
  1129. if ((struct list_head *) e == &con->writequeue)
  1130. break;
  1131. len = e->len;
  1132. offset = e->offset;
  1133. BUG_ON(len == 0 && e->users == 0);
  1134. spin_unlock(&con->writequeue_lock);
  1135. ret = 0;
  1136. if (len) {
  1137. ret = kernel_sendpage(con->sock, e->page, offset, len,
  1138. msg_flags);
  1139. if (ret == -EAGAIN || ret == 0) {
  1140. if (ret == -EAGAIN &&
  1141. test_bit(SOCK_ASYNC_NOSPACE, &con->sock->flags) &&
  1142. !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
  1143. /* Notify TCP that we're limited by the
  1144. * application window size.
  1145. */
  1146. set_bit(SOCK_NOSPACE, &con->sock->flags);
  1147. con->sock->sk->sk_write_pending++;
  1148. }
  1149. cond_resched();
  1150. goto out;
  1151. }
  1152. if (ret <= 0)
  1153. goto send_error;
  1154. }
  1155. /* Don't starve people filling buffers */
  1156. if (++count >= MAX_SEND_MSG_COUNT) {
  1157. cond_resched();
  1158. count = 0;
  1159. }
  1160. spin_lock(&con->writequeue_lock);
  1161. e->offset += ret;
  1162. e->len -= ret;
  1163. if (e->len == 0 && e->users == 0) {
  1164. list_del(&e->list);
  1165. free_entry(e);
  1166. continue;
  1167. }
  1168. }
  1169. spin_unlock(&con->writequeue_lock);
  1170. out:
  1171. mutex_unlock(&con->sock_mutex);
  1172. return;
  1173. send_error:
  1174. mutex_unlock(&con->sock_mutex);
  1175. close_connection(con, false);
  1176. lowcomms_connect_sock(con);
  1177. return;
  1178. out_connect:
  1179. mutex_unlock(&con->sock_mutex);
  1180. if (!test_bit(CF_INIT_PENDING, &con->flags))
  1181. lowcomms_connect_sock(con);
  1182. return;
  1183. }
  1184. static void clean_one_writequeue(struct connection *con)
  1185. {
  1186. struct writequeue_entry *e, *safe;
  1187. spin_lock(&con->writequeue_lock);
  1188. list_for_each_entry_safe(e, safe, &con->writequeue, list) {
  1189. list_del(&e->list);
  1190. free_entry(e);
  1191. }
  1192. spin_unlock(&con->writequeue_lock);
  1193. }
  1194. /* Called from recovery when it knows that a node has
  1195. left the cluster */
  1196. int dlm_lowcomms_close(int nodeid)
  1197. {
  1198. struct connection *con;
  1199. log_print("closing connection to node %d", nodeid);
  1200. con = nodeid2con(nodeid, 0);
  1201. if (con) {
  1202. clear_bit(CF_CONNECT_PENDING, &con->flags);
  1203. clear_bit(CF_WRITE_PENDING, &con->flags);
  1204. set_bit(CF_CLOSE, &con->flags);
  1205. if (cancel_work_sync(&con->swork))
  1206. log_print("canceled swork for node %d", nodeid);
  1207. if (cancel_work_sync(&con->rwork))
  1208. log_print("canceled rwork for node %d", nodeid);
  1209. clean_one_writequeue(con);
  1210. close_connection(con, true);
  1211. }
  1212. return 0;
  1213. }
  1214. /* Receive workqueue function */
  1215. static void process_recv_sockets(struct work_struct *work)
  1216. {
  1217. struct connection *con = container_of(work, struct connection, rwork);
  1218. int err;
  1219. clear_bit(CF_READ_PENDING, &con->flags);
  1220. do {
  1221. err = con->rx_action(con);
  1222. } while (!err);
  1223. }
  1224. /* Send workqueue function */
  1225. static void process_send_sockets(struct work_struct *work)
  1226. {
  1227. struct connection *con = container_of(work, struct connection, swork);
  1228. if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
  1229. con->connect_action(con);
  1230. set_bit(CF_WRITE_PENDING, &con->flags);
  1231. }
  1232. if (test_and_clear_bit(CF_WRITE_PENDING, &con->flags))
  1233. send_to_sock(con);
  1234. }
  1235. /* Discard all entries on the write queues */
  1236. static void clean_writequeues(void)
  1237. {
  1238. foreach_conn(clean_one_writequeue);
  1239. }
  1240. static void work_stop(void)
  1241. {
  1242. destroy_workqueue(recv_workqueue);
  1243. destroy_workqueue(send_workqueue);
  1244. }
  1245. static int work_start(void)
  1246. {
  1247. recv_workqueue = alloc_workqueue("dlm_recv",
  1248. WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
  1249. if (!recv_workqueue) {
  1250. log_print("can't start dlm_recv");
  1251. return -ENOMEM;
  1252. }
  1253. send_workqueue = alloc_workqueue("dlm_send",
  1254. WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
  1255. if (!send_workqueue) {
  1256. log_print("can't start dlm_send");
  1257. destroy_workqueue(recv_workqueue);
  1258. return -ENOMEM;
  1259. }
  1260. return 0;
  1261. }
  1262. static void stop_conn(struct connection *con)
  1263. {
  1264. con->flags |= 0x0F;
  1265. if (con->sock && con->sock->sk)
  1266. con->sock->sk->sk_user_data = NULL;
  1267. }
  1268. static void free_conn(struct connection *con)
  1269. {
  1270. close_connection(con, true);
  1271. if (con->othercon)
  1272. kmem_cache_free(con_cache, con->othercon);
  1273. hlist_del(&con->list);
  1274. kmem_cache_free(con_cache, con);
  1275. }
  1276. void dlm_lowcomms_stop(void)
  1277. {
  1278. /* Set all the flags to prevent any
  1279. socket activity.
  1280. */
  1281. mutex_lock(&connections_lock);
  1282. foreach_conn(stop_conn);
  1283. mutex_unlock(&connections_lock);
  1284. work_stop();
  1285. mutex_lock(&connections_lock);
  1286. clean_writequeues();
  1287. foreach_conn(free_conn);
  1288. mutex_unlock(&connections_lock);
  1289. kmem_cache_destroy(con_cache);
  1290. }
  1291. int dlm_lowcomms_start(void)
  1292. {
  1293. int error = -EINVAL;
  1294. struct connection *con;
  1295. int i;
  1296. for (i = 0; i < CONN_HASH_SIZE; i++)
  1297. INIT_HLIST_HEAD(&connection_hash[i]);
  1298. init_local();
  1299. if (!dlm_local_count) {
  1300. error = -ENOTCONN;
  1301. log_print("no local IP address has been set");
  1302. goto out;
  1303. }
  1304. error = -ENOMEM;
  1305. con_cache = kmem_cache_create("dlm_conn", sizeof(struct connection),
  1306. __alignof__(struct connection), 0,
  1307. NULL);
  1308. if (!con_cache)
  1309. goto out;
  1310. /* Start listening */
  1311. if (dlm_config.ci_protocol == 0)
  1312. error = tcp_listen_for_all();
  1313. else
  1314. error = sctp_listen_for_all();
  1315. if (error)
  1316. goto fail_unlisten;
  1317. error = work_start();
  1318. if (error)
  1319. goto fail_unlisten;
  1320. return 0;
  1321. fail_unlisten:
  1322. con = nodeid2con(0,0);
  1323. if (con) {
  1324. close_connection(con, false);
  1325. kmem_cache_free(con_cache, con);
  1326. }
  1327. kmem_cache_destroy(con_cache);
  1328. out:
  1329. return error;
  1330. }