lowcomms.c 37 KB

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