lowcomms.c 37 KB

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