lowcomms-tcp.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. /******************************************************************************
  2. *******************************************************************************
  3. **
  4. ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  5. ** Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
  6. **
  7. ** This copyrighted material is made available to anyone wishing to use,
  8. ** modify, copy, or redistribute it subject to the terms and conditions
  9. ** of the GNU General Public License v.2.
  10. **
  11. *******************************************************************************
  12. ******************************************************************************/
  13. /*
  14. * lowcomms.c
  15. *
  16. * This is the "low-level" comms layer.
  17. *
  18. * It is responsible for sending/receiving messages
  19. * from other nodes in the cluster.
  20. *
  21. * Cluster nodes are referred to by their nodeids. nodeids are
  22. * simply 32 bit numbers to the locking module - if they need to
  23. * be expanded for the cluster infrastructure then that is it's
  24. * responsibility. It is this layer's
  25. * responsibility to resolve these into IP address or
  26. * whatever it needs for inter-node communication.
  27. *
  28. * The comms level is two kernel threads that deal mainly with
  29. * the receiving of messages from other nodes and passing them
  30. * up to the mid-level comms layer (which understands the
  31. * message format) for execution by the locking core, and
  32. * a send thread which does all the setting up of connections
  33. * to remote nodes and the sending of data. Threads are not allowed
  34. * to send their own data because it may cause them to wait in times
  35. * of high load. Also, this way, the sending thread can collect together
  36. * messages bound for one node and send them in one block.
  37. *
  38. * I don't see any problem with the recv thread executing the locking
  39. * code on behalf of remote processes as the locking code is
  40. * short, efficient and never waits.
  41. *
  42. */
  43. #include <asm/ioctls.h>
  44. #include <net/sock.h>
  45. #include <net/tcp.h>
  46. #include <linux/pagemap.h>
  47. #include "dlm_internal.h"
  48. #include "lowcomms.h"
  49. #include "midcomms.h"
  50. #include "config.h"
  51. struct cbuf {
  52. unsigned int base;
  53. unsigned int len;
  54. unsigned int mask;
  55. };
  56. #define NODE_INCREMENT 32
  57. static void cbuf_add(struct cbuf *cb, int n)
  58. {
  59. cb->len += n;
  60. }
  61. static int cbuf_data(struct cbuf *cb)
  62. {
  63. return ((cb->base + cb->len) & cb->mask);
  64. }
  65. static void cbuf_init(struct cbuf *cb, int size)
  66. {
  67. cb->base = cb->len = 0;
  68. cb->mask = size-1;
  69. }
  70. static void cbuf_eat(struct cbuf *cb, int n)
  71. {
  72. cb->len -= n;
  73. cb->base += n;
  74. cb->base &= cb->mask;
  75. }
  76. static bool cbuf_empty(struct cbuf *cb)
  77. {
  78. return cb->len == 0;
  79. }
  80. /* Maximum number of incoming messages to process before
  81. doing a cond_resched()
  82. */
  83. #define MAX_RX_MSG_COUNT 25
  84. struct connection {
  85. struct socket *sock; /* NULL if not connected */
  86. uint32_t nodeid; /* So we know who we are in the list */
  87. struct mutex sock_mutex;
  88. unsigned long flags; /* bit 1,2 = We are on the read/write lists */
  89. #define CF_READ_PENDING 1
  90. #define CF_WRITE_PENDING 2
  91. #define CF_CONNECT_PENDING 3
  92. #define CF_IS_OTHERCON 4
  93. struct list_head writequeue; /* List of outgoing writequeue_entries */
  94. struct list_head listenlist; /* List of allocated listening sockets */
  95. spinlock_t writequeue_lock;
  96. int (*rx_action) (struct connection *); /* What to do when active */
  97. struct page *rx_page;
  98. struct cbuf cb;
  99. int retries;
  100. #define MAX_CONNECT_RETRIES 3
  101. struct connection *othercon;
  102. struct work_struct rwork; /* Receive workqueue */
  103. struct work_struct swork; /* Send workqueue */
  104. };
  105. #define sock2con(x) ((struct connection *)(x)->sk_user_data)
  106. /* An entry waiting to be sent */
  107. struct writequeue_entry {
  108. struct list_head list;
  109. struct page *page;
  110. int offset;
  111. int len;
  112. int end;
  113. int users;
  114. struct connection *con;
  115. };
  116. static struct sockaddr_storage dlm_local_addr;
  117. /* Work queues */
  118. static struct workqueue_struct *recv_workqueue;
  119. static struct workqueue_struct *send_workqueue;
  120. /* An array of pointers to connections, indexed by NODEID */
  121. static struct connection **connections;
  122. static DECLARE_MUTEX(connections_lock);
  123. static struct kmem_cache *con_cache;
  124. static int conn_array_size;
  125. static void process_recv_sockets(struct work_struct *work);
  126. static void process_send_sockets(struct work_struct *work);
  127. static struct connection *nodeid2con(int nodeid, gfp_t allocation)
  128. {
  129. struct connection *con = NULL;
  130. down(&connections_lock);
  131. if (nodeid >= conn_array_size) {
  132. int new_size = nodeid + NODE_INCREMENT;
  133. struct connection **new_conns;
  134. new_conns = kzalloc(sizeof(struct connection *) *
  135. new_size, allocation);
  136. if (!new_conns)
  137. goto finish;
  138. memcpy(new_conns, connections, sizeof(struct connection *) * conn_array_size);
  139. conn_array_size = new_size;
  140. kfree(connections);
  141. connections = new_conns;
  142. }
  143. con = connections[nodeid];
  144. if (con == NULL && allocation) {
  145. con = kmem_cache_zalloc(con_cache, allocation);
  146. if (!con)
  147. goto finish;
  148. con->nodeid = nodeid;
  149. mutex_init(&con->sock_mutex);
  150. INIT_LIST_HEAD(&con->writequeue);
  151. spin_lock_init(&con->writequeue_lock);
  152. INIT_WORK(&con->swork, process_send_sockets);
  153. INIT_WORK(&con->rwork, process_recv_sockets);
  154. connections[nodeid] = con;
  155. }
  156. finish:
  157. up(&connections_lock);
  158. return con;
  159. }
  160. /* Data available on socket or listen socket received a connect */
  161. static void lowcomms_data_ready(struct sock *sk, int count_unused)
  162. {
  163. struct connection *con = sock2con(sk);
  164. if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
  165. queue_work(recv_workqueue, &con->rwork);
  166. }
  167. static void lowcomms_write_space(struct sock *sk)
  168. {
  169. struct connection *con = sock2con(sk);
  170. if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags))
  171. queue_work(send_workqueue, &con->swork);
  172. }
  173. static inline void lowcomms_connect_sock(struct connection *con)
  174. {
  175. if (!test_and_set_bit(CF_CONNECT_PENDING, &con->flags))
  176. queue_work(send_workqueue, &con->swork);
  177. }
  178. static void lowcomms_state_change(struct sock *sk)
  179. {
  180. if (sk->sk_state == TCP_ESTABLISHED)
  181. lowcomms_write_space(sk);
  182. }
  183. /* Make a socket active */
  184. static int add_sock(struct socket *sock, struct connection *con)
  185. {
  186. con->sock = sock;
  187. /* Install a data_ready callback */
  188. con->sock->sk->sk_data_ready = lowcomms_data_ready;
  189. con->sock->sk->sk_write_space = lowcomms_write_space;
  190. con->sock->sk->sk_state_change = lowcomms_state_change;
  191. return 0;
  192. }
  193. /* Add the port number to an IP6 or 4 sockaddr and return the address
  194. length */
  195. static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
  196. int *addr_len)
  197. {
  198. saddr->ss_family = dlm_local_addr.ss_family;
  199. if (saddr->ss_family == AF_INET) {
  200. struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
  201. in4_addr->sin_port = cpu_to_be16(port);
  202. *addr_len = sizeof(struct sockaddr_in);
  203. } else {
  204. struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
  205. in6_addr->sin6_port = cpu_to_be16(port);
  206. *addr_len = sizeof(struct sockaddr_in6);
  207. }
  208. }
  209. /* Close a remote connection and tidy up */
  210. static void close_connection(struct connection *con, bool and_other)
  211. {
  212. mutex_lock(&con->sock_mutex);
  213. if (con->sock) {
  214. sock_release(con->sock);
  215. con->sock = NULL;
  216. }
  217. if (con->othercon && and_other) {
  218. /* Will only re-enter once. */
  219. close_connection(con->othercon, false);
  220. }
  221. if (con->rx_page) {
  222. __free_page(con->rx_page);
  223. con->rx_page = NULL;
  224. }
  225. con->retries = 0;
  226. mutex_unlock(&con->sock_mutex);
  227. }
  228. /* Data received from remote end */
  229. static int receive_from_sock(struct connection *con)
  230. {
  231. int ret = 0;
  232. struct msghdr msg = {};
  233. struct kvec iov[2];
  234. unsigned len;
  235. int r;
  236. int call_again_soon = 0;
  237. int nvec;
  238. mutex_lock(&con->sock_mutex);
  239. if (con->sock == NULL) {
  240. ret = -EAGAIN;
  241. goto out_close;
  242. }
  243. if (con->rx_page == NULL) {
  244. /*
  245. * This doesn't need to be atomic, but I think it should
  246. * improve performance if it is.
  247. */
  248. con->rx_page = alloc_page(GFP_ATOMIC);
  249. if (con->rx_page == NULL)
  250. goto out_resched;
  251. cbuf_init(&con->cb, PAGE_CACHE_SIZE);
  252. }
  253. /*
  254. * iov[0] is the bit of the circular buffer between the current end
  255. * point (cb.base + cb.len) and the end of the buffer.
  256. */
  257. iov[0].iov_len = con->cb.base - cbuf_data(&con->cb);
  258. iov[0].iov_base = page_address(con->rx_page) + cbuf_data(&con->cb);
  259. iov[1].iov_len = 0;
  260. nvec = 1;
  261. /*
  262. * iov[1] is the bit of the circular buffer between the start of the
  263. * buffer and the start of the currently used section (cb.base)
  264. */
  265. if (cbuf_data(&con->cb) >= con->cb.base) {
  266. iov[0].iov_len = PAGE_CACHE_SIZE - cbuf_data(&con->cb);
  267. iov[1].iov_len = con->cb.base;
  268. iov[1].iov_base = page_address(con->rx_page);
  269. nvec = 2;
  270. }
  271. len = iov[0].iov_len + iov[1].iov_len;
  272. r = ret = kernel_recvmsg(con->sock, &msg, iov, nvec, len,
  273. MSG_DONTWAIT | MSG_NOSIGNAL);
  274. if (ret <= 0)
  275. goto out_close;
  276. if (ret == len)
  277. call_again_soon = 1;
  278. cbuf_add(&con->cb, ret);
  279. ret = dlm_process_incoming_buffer(con->nodeid,
  280. page_address(con->rx_page),
  281. con->cb.base, con->cb.len,
  282. PAGE_CACHE_SIZE);
  283. if (ret == -EBADMSG) {
  284. printk(KERN_INFO "dlm: lowcomms: addr=%p, base=%u, len=%u, "
  285. "iov_len=%u, iov_base[0]=%p, read=%d\n",
  286. page_address(con->rx_page), con->cb.base, con->cb.len,
  287. len, iov[0].iov_base, r);
  288. }
  289. if (ret < 0)
  290. goto out_close;
  291. cbuf_eat(&con->cb, ret);
  292. if (cbuf_empty(&con->cb) && !call_again_soon) {
  293. __free_page(con->rx_page);
  294. con->rx_page = NULL;
  295. }
  296. if (call_again_soon)
  297. goto out_resched;
  298. mutex_unlock(&con->sock_mutex);
  299. return 0;
  300. out_resched:
  301. if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
  302. queue_work(recv_workqueue, &con->rwork);
  303. mutex_unlock(&con->sock_mutex);
  304. return -EAGAIN;
  305. out_close:
  306. mutex_unlock(&con->sock_mutex);
  307. if (ret != -EAGAIN && !test_bit(CF_IS_OTHERCON, &con->flags)) {
  308. close_connection(con, false);
  309. /* Reconnect when there is something to send */
  310. }
  311. /* Don't return success if we really got EOF */
  312. if (ret == 0)
  313. ret = -EAGAIN;
  314. return ret;
  315. }
  316. /* Listening socket is busy, accept a connection */
  317. static int accept_from_sock(struct connection *con)
  318. {
  319. int result;
  320. struct sockaddr_storage peeraddr;
  321. struct socket *newsock;
  322. int len;
  323. int nodeid;
  324. struct connection *newcon;
  325. struct connection *addcon;
  326. memset(&peeraddr, 0, sizeof(peeraddr));
  327. result = sock_create_kern(dlm_local_addr.ss_family, SOCK_STREAM,
  328. IPPROTO_TCP, &newsock);
  329. if (result < 0)
  330. return -ENOMEM;
  331. mutex_lock_nested(&con->sock_mutex, 0);
  332. result = -ENOTCONN;
  333. if (con->sock == NULL)
  334. goto accept_err;
  335. newsock->type = con->sock->type;
  336. newsock->ops = con->sock->ops;
  337. result = con->sock->ops->accept(con->sock, newsock, O_NONBLOCK);
  338. if (result < 0)
  339. goto accept_err;
  340. /* Get the connected socket's peer */
  341. memset(&peeraddr, 0, sizeof(peeraddr));
  342. if (newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr,
  343. &len, 2)) {
  344. result = -ECONNABORTED;
  345. goto accept_err;
  346. }
  347. /* Get the new node's NODEID */
  348. make_sockaddr(&peeraddr, 0, &len);
  349. if (dlm_addr_to_nodeid(&peeraddr, &nodeid)) {
  350. printk("dlm: connect from non cluster node\n");
  351. sock_release(newsock);
  352. mutex_unlock(&con->sock_mutex);
  353. return -1;
  354. }
  355. log_print("got connection from %d", nodeid);
  356. /* Check to see if we already have a connection to this node. This
  357. * could happen if the two nodes initiate a connection at roughly
  358. * the same time and the connections cross on the wire.
  359. * TEMPORARY FIX:
  360. * In this case we store the incoming one in "othercon"
  361. */
  362. newcon = nodeid2con(nodeid, GFP_KERNEL);
  363. if (!newcon) {
  364. result = -ENOMEM;
  365. goto accept_err;
  366. }
  367. mutex_lock_nested(&newcon->sock_mutex, 1);
  368. if (newcon->sock) {
  369. struct connection *othercon = newcon->othercon;
  370. if (!othercon) {
  371. othercon = kmem_cache_zalloc(con_cache, GFP_KERNEL);
  372. if (!othercon) {
  373. printk("dlm: failed to allocate incoming socket\n");
  374. mutex_unlock(&newcon->sock_mutex);
  375. result = -ENOMEM;
  376. goto accept_err;
  377. }
  378. othercon->nodeid = nodeid;
  379. othercon->rx_action = receive_from_sock;
  380. mutex_init(&othercon->sock_mutex);
  381. INIT_WORK(&othercon->swork, process_send_sockets);
  382. INIT_WORK(&othercon->rwork, process_recv_sockets);
  383. set_bit(CF_IS_OTHERCON, &othercon->flags);
  384. newcon->othercon = othercon;
  385. }
  386. othercon->sock = newsock;
  387. newsock->sk->sk_user_data = othercon;
  388. add_sock(newsock, othercon);
  389. addcon = othercon;
  390. }
  391. else {
  392. newsock->sk->sk_user_data = newcon;
  393. newcon->rx_action = receive_from_sock;
  394. add_sock(newsock, newcon);
  395. addcon = newcon;
  396. }
  397. mutex_unlock(&newcon->sock_mutex);
  398. /*
  399. * Add it to the active queue in case we got data
  400. * beween processing the accept adding the socket
  401. * to the read_sockets list
  402. */
  403. if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
  404. queue_work(recv_workqueue, &addcon->rwork);
  405. mutex_unlock(&con->sock_mutex);
  406. return 0;
  407. accept_err:
  408. mutex_unlock(&con->sock_mutex);
  409. sock_release(newsock);
  410. if (result != -EAGAIN)
  411. printk("dlm: error accepting connection from node: %d\n", result);
  412. return result;
  413. }
  414. /* Connect a new socket to its peer */
  415. static void connect_to_sock(struct connection *con)
  416. {
  417. int result = -EHOSTUNREACH;
  418. struct sockaddr_storage saddr;
  419. int addr_len;
  420. struct socket *sock;
  421. if (con->nodeid == 0) {
  422. log_print("attempt to connect sock 0 foiled");
  423. return;
  424. }
  425. mutex_lock(&con->sock_mutex);
  426. if (con->retries++ > MAX_CONNECT_RETRIES)
  427. goto out;
  428. /* Some odd races can cause double-connects, ignore them */
  429. if (con->sock) {
  430. result = 0;
  431. goto out;
  432. }
  433. /* Create a socket to communicate with */
  434. result = sock_create_kern(dlm_local_addr.ss_family, SOCK_STREAM,
  435. IPPROTO_TCP, &sock);
  436. if (result < 0)
  437. goto out_err;
  438. memset(&saddr, 0, sizeof(saddr));
  439. if (dlm_nodeid_to_addr(con->nodeid, &saddr))
  440. goto out_err;
  441. sock->sk->sk_user_data = con;
  442. con->rx_action = receive_from_sock;
  443. make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
  444. add_sock(sock, con);
  445. log_print("connecting to %d", con->nodeid);
  446. result =
  447. sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
  448. O_NONBLOCK);
  449. if (result == -EINPROGRESS)
  450. result = 0;
  451. if (result == 0)
  452. goto out;
  453. out_err:
  454. if (con->sock) {
  455. sock_release(con->sock);
  456. con->sock = NULL;
  457. }
  458. /*
  459. * Some errors are fatal and this list might need adjusting. For other
  460. * errors we try again until the max number of retries is reached.
  461. */
  462. if (result != -EHOSTUNREACH && result != -ENETUNREACH &&
  463. result != -ENETDOWN && result != EINVAL
  464. && result != -EPROTONOSUPPORT) {
  465. lowcomms_connect_sock(con);
  466. result = 0;
  467. }
  468. out:
  469. mutex_unlock(&con->sock_mutex);
  470. return;
  471. }
  472. static struct socket *create_listen_sock(struct connection *con,
  473. struct sockaddr_storage *saddr)
  474. {
  475. struct socket *sock = NULL;
  476. mm_segment_t fs;
  477. int result = 0;
  478. int one = 1;
  479. int addr_len;
  480. if (dlm_local_addr.ss_family == AF_INET)
  481. addr_len = sizeof(struct sockaddr_in);
  482. else
  483. addr_len = sizeof(struct sockaddr_in6);
  484. /* Create a socket to communicate with */
  485. result = sock_create_kern(dlm_local_addr.ss_family, SOCK_STREAM, IPPROTO_TCP, &sock);
  486. if (result < 0) {
  487. printk("dlm: Can't create listening comms socket\n");
  488. goto create_out;
  489. }
  490. fs = get_fs();
  491. set_fs(get_ds());
  492. result = sock_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
  493. (char *)&one, sizeof(one));
  494. set_fs(fs);
  495. if (result < 0) {
  496. printk("dlm: Failed to set SO_REUSEADDR on socket: result=%d\n",
  497. result);
  498. }
  499. sock->sk->sk_user_data = con;
  500. con->rx_action = accept_from_sock;
  501. con->sock = sock;
  502. /* Bind to our port */
  503. make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
  504. result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
  505. if (result < 0) {
  506. printk("dlm: Can't bind to port %d\n", dlm_config.ci_tcp_port);
  507. sock_release(sock);
  508. sock = NULL;
  509. con->sock = NULL;
  510. goto create_out;
  511. }
  512. fs = get_fs();
  513. set_fs(get_ds());
  514. result = sock_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
  515. (char *)&one, sizeof(one));
  516. set_fs(fs);
  517. if (result < 0) {
  518. printk("dlm: Set keepalive failed: %d\n", result);
  519. }
  520. result = sock->ops->listen(sock, 5);
  521. if (result < 0) {
  522. printk("dlm: Can't listen on port %d\n", dlm_config.ci_tcp_port);
  523. sock_release(sock);
  524. sock = NULL;
  525. goto create_out;
  526. }
  527. create_out:
  528. return sock;
  529. }
  530. /* Listen on all interfaces */
  531. static int listen_for_all(void)
  532. {
  533. struct socket *sock = NULL;
  534. struct connection *con = nodeid2con(0, GFP_KERNEL);
  535. int result = -EINVAL;
  536. /* We don't support multi-homed hosts */
  537. set_bit(CF_IS_OTHERCON, &con->flags);
  538. sock = create_listen_sock(con, &dlm_local_addr);
  539. if (sock) {
  540. add_sock(sock, con);
  541. result = 0;
  542. }
  543. else {
  544. result = -EADDRINUSE;
  545. }
  546. return result;
  547. }
  548. static struct writequeue_entry *new_writequeue_entry(struct connection *con,
  549. gfp_t allocation)
  550. {
  551. struct writequeue_entry *entry;
  552. entry = kmalloc(sizeof(struct writequeue_entry), allocation);
  553. if (!entry)
  554. return NULL;
  555. entry->page = alloc_page(allocation);
  556. if (!entry->page) {
  557. kfree(entry);
  558. return NULL;
  559. }
  560. entry->offset = 0;
  561. entry->len = 0;
  562. entry->end = 0;
  563. entry->users = 0;
  564. entry->con = con;
  565. return entry;
  566. }
  567. void *dlm_lowcomms_get_buffer(int nodeid, int len,
  568. gfp_t allocation, char **ppc)
  569. {
  570. struct connection *con;
  571. struct writequeue_entry *e;
  572. int offset = 0;
  573. int users = 0;
  574. con = nodeid2con(nodeid, allocation);
  575. if (!con)
  576. return NULL;
  577. spin_lock(&con->writequeue_lock);
  578. e = list_entry(con->writequeue.prev, struct writequeue_entry, list);
  579. if ((&e->list == &con->writequeue) ||
  580. (PAGE_CACHE_SIZE - e->end < len)) {
  581. e = NULL;
  582. } else {
  583. offset = e->end;
  584. e->end += len;
  585. users = e->users++;
  586. }
  587. spin_unlock(&con->writequeue_lock);
  588. if (e) {
  589. got_one:
  590. if (users == 0)
  591. kmap(e->page);
  592. *ppc = page_address(e->page) + offset;
  593. return e;
  594. }
  595. e = new_writequeue_entry(con, allocation);
  596. if (e) {
  597. spin_lock(&con->writequeue_lock);
  598. offset = e->end;
  599. e->end += len;
  600. users = e->users++;
  601. list_add_tail(&e->list, &con->writequeue);
  602. spin_unlock(&con->writequeue_lock);
  603. goto got_one;
  604. }
  605. return NULL;
  606. }
  607. void dlm_lowcomms_commit_buffer(void *mh)
  608. {
  609. struct writequeue_entry *e = (struct writequeue_entry *)mh;
  610. struct connection *con = e->con;
  611. int users;
  612. spin_lock(&con->writequeue_lock);
  613. users = --e->users;
  614. if (users)
  615. goto out;
  616. e->len = e->end - e->offset;
  617. kunmap(e->page);
  618. spin_unlock(&con->writequeue_lock);
  619. if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
  620. queue_work(send_workqueue, &con->swork);
  621. }
  622. return;
  623. out:
  624. spin_unlock(&con->writequeue_lock);
  625. return;
  626. }
  627. static void free_entry(struct writequeue_entry *e)
  628. {
  629. __free_page(e->page);
  630. kfree(e);
  631. }
  632. /* Send a message */
  633. static void send_to_sock(struct connection *con)
  634. {
  635. int ret = 0;
  636. ssize_t(*sendpage) (struct socket *, struct page *, int, size_t, int);
  637. const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
  638. struct writequeue_entry *e;
  639. int len, offset;
  640. mutex_lock(&con->sock_mutex);
  641. if (con->sock == NULL)
  642. goto out_connect;
  643. sendpage = con->sock->ops->sendpage;
  644. spin_lock(&con->writequeue_lock);
  645. for (;;) {
  646. e = list_entry(con->writequeue.next, struct writequeue_entry,
  647. list);
  648. if ((struct list_head *) e == &con->writequeue)
  649. break;
  650. len = e->len;
  651. offset = e->offset;
  652. BUG_ON(len == 0 && e->users == 0);
  653. spin_unlock(&con->writequeue_lock);
  654. kmap(e->page);
  655. ret = 0;
  656. if (len) {
  657. ret = sendpage(con->sock, e->page, offset, len,
  658. msg_flags);
  659. if (ret == -EAGAIN || ret == 0)
  660. goto out;
  661. if (ret <= 0)
  662. goto send_error;
  663. }
  664. else {
  665. /* Don't starve people filling buffers */
  666. cond_resched();
  667. }
  668. spin_lock(&con->writequeue_lock);
  669. e->offset += ret;
  670. e->len -= ret;
  671. if (e->len == 0 && e->users == 0) {
  672. list_del(&e->list);
  673. kunmap(e->page);
  674. free_entry(e);
  675. continue;
  676. }
  677. }
  678. spin_unlock(&con->writequeue_lock);
  679. out:
  680. mutex_unlock(&con->sock_mutex);
  681. return;
  682. send_error:
  683. mutex_unlock(&con->sock_mutex);
  684. close_connection(con, false);
  685. lowcomms_connect_sock(con);
  686. return;
  687. out_connect:
  688. mutex_unlock(&con->sock_mutex);
  689. connect_to_sock(con);
  690. return;
  691. }
  692. static void clean_one_writequeue(struct connection *con)
  693. {
  694. struct list_head *list;
  695. struct list_head *temp;
  696. spin_lock(&con->writequeue_lock);
  697. list_for_each_safe(list, temp, &con->writequeue) {
  698. struct writequeue_entry *e =
  699. list_entry(list, struct writequeue_entry, list);
  700. list_del(&e->list);
  701. free_entry(e);
  702. }
  703. spin_unlock(&con->writequeue_lock);
  704. }
  705. /* Called from recovery when it knows that a node has
  706. left the cluster */
  707. int dlm_lowcomms_close(int nodeid)
  708. {
  709. struct connection *con;
  710. if (!connections)
  711. goto out;
  712. log_print("closing connection to node %d", nodeid);
  713. con = nodeid2con(nodeid, 0);
  714. if (con) {
  715. clean_one_writequeue(con);
  716. close_connection(con, true);
  717. }
  718. return 0;
  719. out:
  720. return -1;
  721. }
  722. /* Look for activity on active sockets */
  723. static void process_recv_sockets(struct work_struct *work)
  724. {
  725. struct connection *con = container_of(work, struct connection, rwork);
  726. int err;
  727. clear_bit(CF_READ_PENDING, &con->flags);
  728. do {
  729. err = con->rx_action(con);
  730. } while (!err);
  731. }
  732. static void process_send_sockets(struct work_struct *work)
  733. {
  734. struct connection *con = container_of(work, struct connection, swork);
  735. if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
  736. connect_to_sock(con);
  737. }
  738. clear_bit(CF_WRITE_PENDING, &con->flags);
  739. send_to_sock(con);
  740. }
  741. /* Discard all entries on the write queues */
  742. static void clean_writequeues(void)
  743. {
  744. int nodeid;
  745. for (nodeid = 1; nodeid < conn_array_size; nodeid++) {
  746. struct connection *con = nodeid2con(nodeid, 0);
  747. if (con)
  748. clean_one_writequeue(con);
  749. }
  750. }
  751. static void work_stop(void)
  752. {
  753. destroy_workqueue(recv_workqueue);
  754. destroy_workqueue(send_workqueue);
  755. }
  756. static int work_start(void)
  757. {
  758. int error;
  759. recv_workqueue = create_workqueue("dlm_recv");
  760. error = IS_ERR(recv_workqueue);
  761. if (error) {
  762. log_print("can't start dlm_recv %d", error);
  763. return error;
  764. }
  765. send_workqueue = create_singlethread_workqueue("dlm_send");
  766. error = IS_ERR(send_workqueue);
  767. if (error) {
  768. log_print("can't start dlm_send %d", error);
  769. destroy_workqueue(recv_workqueue);
  770. return error;
  771. }
  772. return 0;
  773. }
  774. void dlm_lowcomms_stop(void)
  775. {
  776. int i;
  777. /* Set all the flags to prevent any
  778. socket activity.
  779. */
  780. for (i = 0; i < conn_array_size; i++) {
  781. if (connections[i])
  782. connections[i]->flags |= 0xFF;
  783. }
  784. work_stop();
  785. clean_writequeues();
  786. for (i = 0; i < conn_array_size; i++) {
  787. if (connections[i]) {
  788. close_connection(connections[i], true);
  789. if (connections[i]->othercon)
  790. kmem_cache_free(con_cache, connections[i]->othercon);
  791. kmem_cache_free(con_cache, connections[i]);
  792. }
  793. }
  794. kfree(connections);
  795. connections = NULL;
  796. kmem_cache_destroy(con_cache);
  797. }
  798. /* This is quite likely to sleep... */
  799. int dlm_lowcomms_start(void)
  800. {
  801. int error = 0;
  802. error = -ENOMEM;
  803. connections = kzalloc(sizeof(struct connection *) *
  804. NODE_INCREMENT, GFP_KERNEL);
  805. if (!connections)
  806. goto out;
  807. conn_array_size = NODE_INCREMENT;
  808. if (dlm_our_addr(&dlm_local_addr, 0)) {
  809. log_print("no local IP address has been set");
  810. goto fail_free_conn;
  811. }
  812. if (!dlm_our_addr(&dlm_local_addr, 1)) {
  813. log_print("This dlm comms module does not support multi-homed clustering");
  814. goto fail_free_conn;
  815. }
  816. con_cache = kmem_cache_create("dlm_conn", sizeof(struct connection),
  817. __alignof__(struct connection), 0,
  818. NULL, NULL);
  819. if (!con_cache)
  820. goto fail_free_conn;
  821. /* Start listening */
  822. error = listen_for_all();
  823. if (error)
  824. goto fail_unlisten;
  825. error = work_start();
  826. if (error)
  827. goto fail_unlisten;
  828. return 0;
  829. fail_unlisten:
  830. close_connection(connections[0], false);
  831. kmem_cache_free(con_cache, connections[0]);
  832. kmem_cache_destroy(con_cache);
  833. fail_free_conn:
  834. kfree(connections);
  835. out:
  836. return error;
  837. }
  838. /*
  839. * Overrides for Emacs so that we follow Linus's tabbing style.
  840. * Emacs will notice this stuff at the end of the file and automatically
  841. * adjust the settings for this buffer only. This must remain at the end
  842. * of the file.
  843. * ---------------------------------------------------------------------------
  844. * Local variables:
  845. * c-file-style: "linux"
  846. * End:
  847. */