lowcomms.c 37 KB

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