lowcomms.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  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 (con && !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 (con && !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. othercon->sock = newsock;
  627. newsock->sk->sk_user_data = othercon;
  628. add_sock(newsock, othercon);
  629. addcon = othercon;
  630. }
  631. else {
  632. printk("Extra connection from node %d attempted\n", nodeid);
  633. result = -EAGAIN;
  634. mutex_unlock(&newcon->sock_mutex);
  635. goto accept_err;
  636. }
  637. }
  638. else {
  639. newsock->sk->sk_user_data = newcon;
  640. newcon->rx_action = receive_from_sock;
  641. add_sock(newsock, newcon);
  642. addcon = newcon;
  643. }
  644. mutex_unlock(&newcon->sock_mutex);
  645. /*
  646. * Add it to the active queue in case we got data
  647. * beween processing the accept adding the socket
  648. * to the read_sockets list
  649. */
  650. if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
  651. queue_work(recv_workqueue, &addcon->rwork);
  652. mutex_unlock(&con->sock_mutex);
  653. return 0;
  654. accept_err:
  655. mutex_unlock(&con->sock_mutex);
  656. sock_release(newsock);
  657. if (result != -EAGAIN)
  658. log_print("error accepting connection from node: %d", result);
  659. return result;
  660. }
  661. static void free_entry(struct writequeue_entry *e)
  662. {
  663. __free_page(e->page);
  664. kfree(e);
  665. }
  666. /* Initiate an SCTP association.
  667. This is a special case of send_to_sock() in that we don't yet have a
  668. peeled-off socket for this association, so we use the listening socket
  669. and add the primary IP address of the remote node.
  670. */
  671. static void sctp_init_assoc(struct connection *con)
  672. {
  673. struct sockaddr_storage rem_addr;
  674. char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
  675. struct msghdr outmessage;
  676. struct cmsghdr *cmsg;
  677. struct sctp_sndrcvinfo *sinfo;
  678. struct connection *base_con;
  679. struct writequeue_entry *e;
  680. int len, offset;
  681. int ret;
  682. int addrlen;
  683. struct kvec iov[1];
  684. if (test_and_set_bit(CF_INIT_PENDING, &con->flags))
  685. return;
  686. if (con->retries++ > MAX_CONNECT_RETRIES)
  687. return;
  688. log_print("Initiating association with node %d", con->nodeid);
  689. if (nodeid_to_addr(con->nodeid, (struct sockaddr *)&rem_addr)) {
  690. log_print("no address for nodeid %d", con->nodeid);
  691. return;
  692. }
  693. base_con = nodeid2con(0, 0);
  694. BUG_ON(base_con == NULL);
  695. make_sockaddr(&rem_addr, dlm_config.ci_tcp_port, &addrlen);
  696. outmessage.msg_name = &rem_addr;
  697. outmessage.msg_namelen = addrlen;
  698. outmessage.msg_control = outcmsg;
  699. outmessage.msg_controllen = sizeof(outcmsg);
  700. outmessage.msg_flags = MSG_EOR;
  701. spin_lock(&con->writequeue_lock);
  702. e = list_entry(con->writequeue.next, struct writequeue_entry,
  703. list);
  704. BUG_ON((struct list_head *) e == &con->writequeue);
  705. len = e->len;
  706. offset = e->offset;
  707. spin_unlock(&con->writequeue_lock);
  708. kmap(e->page);
  709. /* Send the first block off the write queue */
  710. iov[0].iov_base = page_address(e->page)+offset;
  711. iov[0].iov_len = len;
  712. cmsg = CMSG_FIRSTHDR(&outmessage);
  713. cmsg->cmsg_level = IPPROTO_SCTP;
  714. cmsg->cmsg_type = SCTP_SNDRCV;
  715. cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
  716. sinfo = CMSG_DATA(cmsg);
  717. memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
  718. sinfo->sinfo_ppid = cpu_to_le32(dlm_our_nodeid());
  719. outmessage.msg_controllen = cmsg->cmsg_len;
  720. ret = kernel_sendmsg(base_con->sock, &outmessage, iov, 1, len);
  721. if (ret < 0) {
  722. log_print("Send first packet to node %d failed: %d",
  723. con->nodeid, ret);
  724. /* Try again later */
  725. clear_bit(CF_CONNECT_PENDING, &con->flags);
  726. clear_bit(CF_INIT_PENDING, &con->flags);
  727. }
  728. else {
  729. spin_lock(&con->writequeue_lock);
  730. e->offset += ret;
  731. e->len -= ret;
  732. if (e->len == 0 && e->users == 0) {
  733. list_del(&e->list);
  734. kunmap(e->page);
  735. free_entry(e);
  736. }
  737. spin_unlock(&con->writequeue_lock);
  738. }
  739. }
  740. /* Connect a new socket to its peer */
  741. static void tcp_connect_to_sock(struct connection *con)
  742. {
  743. int result = -EHOSTUNREACH;
  744. struct sockaddr_storage saddr;
  745. int addr_len;
  746. struct socket *sock;
  747. if (con->nodeid == 0) {
  748. log_print("attempt to connect sock 0 foiled");
  749. return;
  750. }
  751. mutex_lock(&con->sock_mutex);
  752. if (con->retries++ > MAX_CONNECT_RETRIES)
  753. goto out;
  754. /* Some odd races can cause double-connects, ignore them */
  755. if (con->sock) {
  756. result = 0;
  757. goto out;
  758. }
  759. /* Create a socket to communicate with */
  760. result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
  761. IPPROTO_TCP, &sock);
  762. if (result < 0)
  763. goto out_err;
  764. memset(&saddr, 0, sizeof(saddr));
  765. if (dlm_nodeid_to_addr(con->nodeid, &saddr))
  766. goto out_err;
  767. sock->sk->sk_user_data = con;
  768. con->rx_action = receive_from_sock;
  769. con->connect_action = tcp_connect_to_sock;
  770. add_sock(sock, con);
  771. make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
  772. log_print("connecting to %d", con->nodeid);
  773. result =
  774. sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
  775. O_NONBLOCK);
  776. if (result == -EINPROGRESS)
  777. result = 0;
  778. if (result == 0)
  779. goto out;
  780. out_err:
  781. if (con->sock) {
  782. sock_release(con->sock);
  783. con->sock = NULL;
  784. }
  785. /*
  786. * Some errors are fatal and this list might need adjusting. For other
  787. * errors we try again until the max number of retries is reached.
  788. */
  789. if (result != -EHOSTUNREACH && result != -ENETUNREACH &&
  790. result != -ENETDOWN && result != EINVAL
  791. && result != -EPROTONOSUPPORT) {
  792. lowcomms_connect_sock(con);
  793. result = 0;
  794. }
  795. out:
  796. mutex_unlock(&con->sock_mutex);
  797. return;
  798. }
  799. static struct socket *tcp_create_listen_sock(struct connection *con,
  800. struct sockaddr_storage *saddr)
  801. {
  802. struct socket *sock = NULL;
  803. int result = 0;
  804. int one = 1;
  805. int addr_len;
  806. if (dlm_local_addr[0]->ss_family == AF_INET)
  807. addr_len = sizeof(struct sockaddr_in);
  808. else
  809. addr_len = sizeof(struct sockaddr_in6);
  810. /* Create a socket to communicate with */
  811. result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
  812. IPPROTO_TCP, &sock);
  813. if (result < 0) {
  814. log_print("Can't create listening comms socket");
  815. goto create_out;
  816. }
  817. result = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
  818. (char *)&one, sizeof(one));
  819. if (result < 0) {
  820. log_print("Failed to set SO_REUSEADDR on socket: %d", result);
  821. }
  822. sock->sk->sk_user_data = con;
  823. con->rx_action = tcp_accept_from_sock;
  824. con->connect_action = tcp_connect_to_sock;
  825. con->sock = sock;
  826. /* Bind to our port */
  827. make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
  828. result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
  829. if (result < 0) {
  830. log_print("Can't bind to port %d", dlm_config.ci_tcp_port);
  831. sock_release(sock);
  832. sock = NULL;
  833. con->sock = NULL;
  834. goto create_out;
  835. }
  836. result = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
  837. (char *)&one, sizeof(one));
  838. if (result < 0) {
  839. log_print("Set keepalive failed: %d", result);
  840. }
  841. result = sock->ops->listen(sock, 5);
  842. if (result < 0) {
  843. log_print("Can't listen on port %d", dlm_config.ci_tcp_port);
  844. sock_release(sock);
  845. sock = NULL;
  846. goto create_out;
  847. }
  848. create_out:
  849. return sock;
  850. }
  851. /* Get local addresses */
  852. static void init_local(void)
  853. {
  854. struct sockaddr_storage sas, *addr;
  855. int i;
  856. dlm_local_count = 0;
  857. for (i = 0; i < DLM_MAX_ADDR_COUNT - 1; i++) {
  858. if (dlm_our_addr(&sas, i))
  859. break;
  860. addr = kmalloc(sizeof(*addr), GFP_KERNEL);
  861. if (!addr)
  862. break;
  863. memcpy(addr, &sas, sizeof(*addr));
  864. dlm_local_addr[dlm_local_count++] = addr;
  865. }
  866. }
  867. /* Bind to an IP address. SCTP allows multiple address so it can do
  868. multi-homing */
  869. static int add_sctp_bind_addr(struct connection *sctp_con,
  870. struct sockaddr_storage *addr,
  871. int addr_len, int num)
  872. {
  873. int result = 0;
  874. if (num == 1)
  875. result = kernel_bind(sctp_con->sock,
  876. (struct sockaddr *) addr,
  877. addr_len);
  878. else
  879. result = kernel_setsockopt(sctp_con->sock, SOL_SCTP,
  880. SCTP_SOCKOPT_BINDX_ADD,
  881. (char *)addr, addr_len);
  882. if (result < 0)
  883. log_print("Can't bind to port %d addr number %d",
  884. dlm_config.ci_tcp_port, num);
  885. return result;
  886. }
  887. /* Initialise SCTP socket and bind to all interfaces */
  888. static int sctp_listen_for_all(void)
  889. {
  890. struct socket *sock = NULL;
  891. struct sockaddr_storage localaddr;
  892. struct sctp_event_subscribe subscribe;
  893. int result = -EINVAL, num = 1, i, addr_len;
  894. struct connection *con = nodeid2con(0, GFP_KERNEL);
  895. int bufsize = NEEDED_RMEM;
  896. if (!con)
  897. return -ENOMEM;
  898. log_print("Using SCTP for communications");
  899. result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_SEQPACKET,
  900. IPPROTO_SCTP, &sock);
  901. if (result < 0) {
  902. log_print("Can't create comms socket, check SCTP is loaded");
  903. goto out;
  904. }
  905. /* Listen for events */
  906. memset(&subscribe, 0, sizeof(subscribe));
  907. subscribe.sctp_data_io_event = 1;
  908. subscribe.sctp_association_event = 1;
  909. subscribe.sctp_send_failure_event = 1;
  910. subscribe.sctp_shutdown_event = 1;
  911. subscribe.sctp_partial_delivery_event = 1;
  912. result = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
  913. (char *)&bufsize, sizeof(bufsize));
  914. if (result)
  915. log_print("Error increasing buffer space on socket %d", result);
  916. result = kernel_setsockopt(sock, SOL_SCTP, SCTP_EVENTS,
  917. (char *)&subscribe, sizeof(subscribe));
  918. if (result < 0) {
  919. log_print("Failed to set SCTP_EVENTS on socket: result=%d",
  920. result);
  921. goto create_delsock;
  922. }
  923. /* Init con struct */
  924. sock->sk->sk_user_data = con;
  925. con->sock = sock;
  926. con->sock->sk->sk_data_ready = lowcomms_data_ready;
  927. con->rx_action = receive_from_sock;
  928. con->connect_action = sctp_init_assoc;
  929. /* Bind to all interfaces. */
  930. for (i = 0; i < dlm_local_count; i++) {
  931. memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
  932. make_sockaddr(&localaddr, dlm_config.ci_tcp_port, &addr_len);
  933. result = add_sctp_bind_addr(con, &localaddr, addr_len, num);
  934. if (result)
  935. goto create_delsock;
  936. ++num;
  937. }
  938. result = sock->ops->listen(sock, 5);
  939. if (result < 0) {
  940. log_print("Can't set socket listening");
  941. goto create_delsock;
  942. }
  943. return 0;
  944. create_delsock:
  945. sock_release(sock);
  946. con->sock = NULL;
  947. out:
  948. return result;
  949. }
  950. static int tcp_listen_for_all(void)
  951. {
  952. struct socket *sock = NULL;
  953. struct connection *con = nodeid2con(0, GFP_KERNEL);
  954. int result = -EINVAL;
  955. if (!con)
  956. return -ENOMEM;
  957. /* We don't support multi-homed hosts */
  958. if (dlm_local_addr[1] != NULL) {
  959. log_print("TCP protocol can't handle multi-homed hosts, "
  960. "try SCTP");
  961. return -EINVAL;
  962. }
  963. log_print("Using TCP for communications");
  964. set_bit(CF_IS_OTHERCON, &con->flags);
  965. sock = tcp_create_listen_sock(con, dlm_local_addr[0]);
  966. if (sock) {
  967. add_sock(sock, con);
  968. result = 0;
  969. }
  970. else {
  971. result = -EADDRINUSE;
  972. }
  973. return result;
  974. }
  975. static struct writequeue_entry *new_writequeue_entry(struct connection *con,
  976. gfp_t allocation)
  977. {
  978. struct writequeue_entry *entry;
  979. entry = kmalloc(sizeof(struct writequeue_entry), allocation);
  980. if (!entry)
  981. return NULL;
  982. entry->page = alloc_page(allocation);
  983. if (!entry->page) {
  984. kfree(entry);
  985. return NULL;
  986. }
  987. entry->offset = 0;
  988. entry->len = 0;
  989. entry->end = 0;
  990. entry->users = 0;
  991. entry->con = con;
  992. return entry;
  993. }
  994. void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc)
  995. {
  996. struct connection *con;
  997. struct writequeue_entry *e;
  998. int offset = 0;
  999. int users = 0;
  1000. con = nodeid2con(nodeid, allocation);
  1001. if (!con)
  1002. return NULL;
  1003. spin_lock(&con->writequeue_lock);
  1004. e = list_entry(con->writequeue.prev, struct writequeue_entry, list);
  1005. if ((&e->list == &con->writequeue) ||
  1006. (PAGE_CACHE_SIZE - e->end < len)) {
  1007. e = NULL;
  1008. } else {
  1009. offset = e->end;
  1010. e->end += len;
  1011. users = e->users++;
  1012. }
  1013. spin_unlock(&con->writequeue_lock);
  1014. if (e) {
  1015. got_one:
  1016. if (users == 0)
  1017. kmap(e->page);
  1018. *ppc = page_address(e->page) + offset;
  1019. return e;
  1020. }
  1021. e = new_writequeue_entry(con, allocation);
  1022. if (e) {
  1023. spin_lock(&con->writequeue_lock);
  1024. offset = e->end;
  1025. e->end += len;
  1026. users = e->users++;
  1027. list_add_tail(&e->list, &con->writequeue);
  1028. spin_unlock(&con->writequeue_lock);
  1029. goto got_one;
  1030. }
  1031. return NULL;
  1032. }
  1033. void dlm_lowcomms_commit_buffer(void *mh)
  1034. {
  1035. struct writequeue_entry *e = (struct writequeue_entry *)mh;
  1036. struct connection *con = e->con;
  1037. int users;
  1038. spin_lock(&con->writequeue_lock);
  1039. users = --e->users;
  1040. if (users)
  1041. goto out;
  1042. e->len = e->end - e->offset;
  1043. kunmap(e->page);
  1044. spin_unlock(&con->writequeue_lock);
  1045. if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
  1046. queue_work(send_workqueue, &con->swork);
  1047. }
  1048. return;
  1049. out:
  1050. spin_unlock(&con->writequeue_lock);
  1051. return;
  1052. }
  1053. /* Send a message */
  1054. static void send_to_sock(struct connection *con)
  1055. {
  1056. int ret = 0;
  1057. ssize_t(*sendpage) (struct socket *, struct page *, int, size_t, int);
  1058. const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
  1059. struct writequeue_entry *e;
  1060. int len, offset;
  1061. mutex_lock(&con->sock_mutex);
  1062. if (con->sock == NULL)
  1063. goto out_connect;
  1064. sendpage = con->sock->ops->sendpage;
  1065. spin_lock(&con->writequeue_lock);
  1066. for (;;) {
  1067. e = list_entry(con->writequeue.next, struct writequeue_entry,
  1068. list);
  1069. if ((struct list_head *) e == &con->writequeue)
  1070. break;
  1071. len = e->len;
  1072. offset = e->offset;
  1073. BUG_ON(len == 0 && e->users == 0);
  1074. spin_unlock(&con->writequeue_lock);
  1075. kmap(e->page);
  1076. ret = 0;
  1077. if (len) {
  1078. ret = sendpage(con->sock, e->page, offset, len,
  1079. msg_flags);
  1080. if (ret == -EAGAIN || ret == 0)
  1081. goto out;
  1082. if (ret <= 0)
  1083. goto send_error;
  1084. } else {
  1085. /* Don't starve people filling buffers */
  1086. cond_resched();
  1087. }
  1088. spin_lock(&con->writequeue_lock);
  1089. e->offset += ret;
  1090. e->len -= ret;
  1091. if (e->len == 0 && e->users == 0) {
  1092. list_del(&e->list);
  1093. kunmap(e->page);
  1094. free_entry(e);
  1095. continue;
  1096. }
  1097. }
  1098. spin_unlock(&con->writequeue_lock);
  1099. out:
  1100. mutex_unlock(&con->sock_mutex);
  1101. return;
  1102. send_error:
  1103. mutex_unlock(&con->sock_mutex);
  1104. close_connection(con, false);
  1105. lowcomms_connect_sock(con);
  1106. return;
  1107. out_connect:
  1108. mutex_unlock(&con->sock_mutex);
  1109. if (!test_bit(CF_INIT_PENDING, &con->flags))
  1110. lowcomms_connect_sock(con);
  1111. return;
  1112. }
  1113. static void clean_one_writequeue(struct connection *con)
  1114. {
  1115. struct list_head *list;
  1116. struct list_head *temp;
  1117. spin_lock(&con->writequeue_lock);
  1118. list_for_each_safe(list, temp, &con->writequeue) {
  1119. struct writequeue_entry *e =
  1120. list_entry(list, struct writequeue_entry, list);
  1121. list_del(&e->list);
  1122. free_entry(e);
  1123. }
  1124. spin_unlock(&con->writequeue_lock);
  1125. }
  1126. /* Called from recovery when it knows that a node has
  1127. left the cluster */
  1128. int dlm_lowcomms_close(int nodeid)
  1129. {
  1130. struct connection *con;
  1131. log_print("closing connection to node %d", nodeid);
  1132. con = nodeid2con(nodeid, 0);
  1133. if (con) {
  1134. clean_one_writequeue(con);
  1135. close_connection(con, true);
  1136. }
  1137. return 0;
  1138. }
  1139. /* Receive workqueue function */
  1140. static void process_recv_sockets(struct work_struct *work)
  1141. {
  1142. struct connection *con = container_of(work, struct connection, rwork);
  1143. int err;
  1144. clear_bit(CF_READ_PENDING, &con->flags);
  1145. do {
  1146. err = con->rx_action(con);
  1147. } while (!err);
  1148. }
  1149. /* Send workqueue function */
  1150. static void process_send_sockets(struct work_struct *work)
  1151. {
  1152. struct connection *con = container_of(work, struct connection, swork);
  1153. if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
  1154. con->connect_action(con);
  1155. }
  1156. clear_bit(CF_WRITE_PENDING, &con->flags);
  1157. send_to_sock(con);
  1158. }
  1159. /* Discard all entries on the write queues */
  1160. static void clean_writequeues(void)
  1161. {
  1162. int nodeid;
  1163. for (nodeid = 1; nodeid <= max_nodeid; nodeid++) {
  1164. struct connection *con = __nodeid2con(nodeid, 0);
  1165. if (con)
  1166. clean_one_writequeue(con);
  1167. }
  1168. }
  1169. static void work_stop(void)
  1170. {
  1171. destroy_workqueue(recv_workqueue);
  1172. destroy_workqueue(send_workqueue);
  1173. }
  1174. static int work_start(void)
  1175. {
  1176. int error;
  1177. recv_workqueue = create_workqueue("dlm_recv");
  1178. error = IS_ERR(recv_workqueue);
  1179. if (error) {
  1180. log_print("can't start dlm_recv %d", error);
  1181. return error;
  1182. }
  1183. send_workqueue = create_singlethread_workqueue("dlm_send");
  1184. error = IS_ERR(send_workqueue);
  1185. if (error) {
  1186. log_print("can't start dlm_send %d", error);
  1187. destroy_workqueue(recv_workqueue);
  1188. return error;
  1189. }
  1190. return 0;
  1191. }
  1192. void dlm_lowcomms_stop(void)
  1193. {
  1194. int i;
  1195. struct connection *con;
  1196. /* Set all the flags to prevent any
  1197. socket activity.
  1198. */
  1199. down(&connections_lock);
  1200. for (i = 0; i <= max_nodeid; i++) {
  1201. con = __nodeid2con(i, 0);
  1202. if (con) {
  1203. con->flags |= 0xFF;
  1204. if (con->sock)
  1205. con->sock->sk->sk_user_data = NULL;
  1206. }
  1207. }
  1208. up(&connections_lock);
  1209. work_stop();
  1210. down(&connections_lock);
  1211. clean_writequeues();
  1212. for (i = 0; i <= max_nodeid; i++) {
  1213. con = __nodeid2con(i, 0);
  1214. if (con) {
  1215. close_connection(con, true);
  1216. if (con->othercon)
  1217. kmem_cache_free(con_cache, con->othercon);
  1218. kmem_cache_free(con_cache, con);
  1219. }
  1220. }
  1221. max_nodeid = 0;
  1222. up(&connections_lock);
  1223. kmem_cache_destroy(con_cache);
  1224. idr_init(&connections_idr);
  1225. }
  1226. int dlm_lowcomms_start(void)
  1227. {
  1228. int error = -EINVAL;
  1229. struct connection *con;
  1230. init_local();
  1231. if (!dlm_local_count) {
  1232. error = -ENOTCONN;
  1233. log_print("no local IP address has been set");
  1234. goto out;
  1235. }
  1236. error = -ENOMEM;
  1237. con_cache = kmem_cache_create("dlm_conn", sizeof(struct connection),
  1238. __alignof__(struct connection), 0,
  1239. NULL);
  1240. if (!con_cache)
  1241. goto out;
  1242. /* Set some sysctl minima */
  1243. if (sysctl_rmem_max < NEEDED_RMEM)
  1244. sysctl_rmem_max = NEEDED_RMEM;
  1245. /* Start listening */
  1246. if (dlm_config.ci_protocol == 0)
  1247. error = tcp_listen_for_all();
  1248. else
  1249. error = sctp_listen_for_all();
  1250. if (error)
  1251. goto fail_unlisten;
  1252. error = work_start();
  1253. if (error)
  1254. goto fail_unlisten;
  1255. return 0;
  1256. fail_unlisten:
  1257. con = nodeid2con(0,0);
  1258. if (con) {
  1259. close_connection(con, false);
  1260. kmem_cache_free(con_cache, con);
  1261. }
  1262. kmem_cache_destroy(con_cache);
  1263. out:
  1264. return error;
  1265. }