lowcomms.c 36 KB

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