lowcomms.c 40 KB

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