lowcomms.c 37 KB

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