lowcomms-tcp.c 24 KB

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