lowcomms.c 36 KB

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