lowcomms.c 38 KB

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