lowcomms.c 37 KB

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