lowcomms.c 36 KB

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