lowcomms-tcp.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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 iovec iov[2];
  234. mm_segment_t fs;
  235. unsigned len;
  236. int r;
  237. int call_again_soon = 0;
  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. msg.msg_control = NULL;
  254. msg.msg_controllen = 0;
  255. msg.msg_iovlen = 1;
  256. msg.msg_iov = iov;
  257. msg.msg_name = NULL;
  258. msg.msg_namelen = 0;
  259. msg.msg_flags = 0;
  260. /*
  261. * iov[0] is the bit of the circular buffer between the current end
  262. * point (cb.base + cb.len) and the end of the buffer.
  263. */
  264. iov[0].iov_len = con->cb.base - cbuf_data(&con->cb);
  265. iov[0].iov_base = page_address(con->rx_page) + cbuf_data(&con->cb);
  266. iov[1].iov_len = 0;
  267. /*
  268. * iov[1] is the bit of the circular buffer between the start of the
  269. * buffer and the start of the currently used section (cb.base)
  270. */
  271. if (cbuf_data(&con->cb) >= con->cb.base) {
  272. iov[0].iov_len = PAGE_CACHE_SIZE - cbuf_data(&con->cb);
  273. iov[1].iov_len = con->cb.base;
  274. iov[1].iov_base = page_address(con->rx_page);
  275. msg.msg_iovlen = 2;
  276. }
  277. len = iov[0].iov_len + iov[1].iov_len;
  278. fs = get_fs();
  279. set_fs(get_ds());
  280. r = ret = sock_recvmsg(con->sock, &msg, len,
  281. MSG_DONTWAIT | MSG_NOSIGNAL);
  282. set_fs(fs);
  283. if (ret <= 0)
  284. goto out_close;
  285. if (ret == -EAGAIN)
  286. goto out_resched;
  287. if (ret == len)
  288. call_again_soon = 1;
  289. cbuf_add(&con->cb, ret);
  290. ret = dlm_process_incoming_buffer(con->nodeid,
  291. page_address(con->rx_page),
  292. con->cb.base, con->cb.len,
  293. PAGE_CACHE_SIZE);
  294. if (ret == -EBADMSG) {
  295. printk(KERN_INFO "dlm: lowcomms: addr=%p, base=%u, len=%u, "
  296. "iov_len=%u, iov_base[0]=%p, read=%d\n",
  297. page_address(con->rx_page), con->cb.base, con->cb.len,
  298. len, iov[0].iov_base, r);
  299. }
  300. if (ret < 0)
  301. goto out_close;
  302. cbuf_eat(&con->cb, ret);
  303. if (cbuf_empty(&con->cb) && !call_again_soon) {
  304. __free_page(con->rx_page);
  305. con->rx_page = NULL;
  306. }
  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. /* Don't return success if we really got EOF */
  323. if (ret == 0)
  324. ret = -EAGAIN;
  325. return ret;
  326. }
  327. /* Listening socket is busy, accept a connection */
  328. static int accept_from_sock(struct connection *con)
  329. {
  330. int result;
  331. struct sockaddr_storage peeraddr;
  332. struct socket *newsock;
  333. int len;
  334. int nodeid;
  335. struct connection *newcon;
  336. struct connection *addcon;
  337. memset(&peeraddr, 0, sizeof(peeraddr));
  338. result = sock_create_kern(dlm_local_addr.ss_family, SOCK_STREAM,
  339. IPPROTO_TCP, &newsock);
  340. if (result < 0)
  341. return -ENOMEM;
  342. mutex_lock_nested(&con->sock_mutex, 0);
  343. result = -ENOTCONN;
  344. if (con->sock == NULL)
  345. goto accept_err;
  346. newsock->type = con->sock->type;
  347. newsock->ops = con->sock->ops;
  348. result = con->sock->ops->accept(con->sock, newsock, O_NONBLOCK);
  349. if (result < 0)
  350. goto accept_err;
  351. /* Get the connected socket's peer */
  352. memset(&peeraddr, 0, sizeof(peeraddr));
  353. if (newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr,
  354. &len, 2)) {
  355. result = -ECONNABORTED;
  356. goto accept_err;
  357. }
  358. /* Get the new node's NODEID */
  359. make_sockaddr(&peeraddr, 0, &len);
  360. if (dlm_addr_to_nodeid(&peeraddr, &nodeid)) {
  361. printk("dlm: connect from non cluster node\n");
  362. sock_release(newsock);
  363. mutex_unlock(&con->sock_mutex);
  364. return -1;
  365. }
  366. log_print("got connection from %d", nodeid);
  367. /* Check to see if we already have a connection to this node. This
  368. * could happen if the two nodes initiate a connection at roughly
  369. * the same time and the connections cross on the wire.
  370. * TEMPORARY FIX:
  371. * In this case we store the incoming one in "othercon"
  372. */
  373. newcon = nodeid2con(nodeid, GFP_KERNEL);
  374. if (!newcon) {
  375. result = -ENOMEM;
  376. goto accept_err;
  377. }
  378. mutex_lock_nested(&newcon->sock_mutex, 1);
  379. if (newcon->sock) {
  380. struct connection *othercon = newcon->othercon;
  381. if (!othercon) {
  382. othercon = kmem_cache_zalloc(con_cache, GFP_KERNEL);
  383. if (!othercon) {
  384. printk("dlm: failed to allocate incoming socket\n");
  385. mutex_unlock(&newcon->sock_mutex);
  386. result = -ENOMEM;
  387. goto accept_err;
  388. }
  389. othercon->nodeid = nodeid;
  390. othercon->rx_action = receive_from_sock;
  391. mutex_init(&othercon->sock_mutex);
  392. INIT_WORK(&othercon->swork, process_send_sockets);
  393. INIT_WORK(&othercon->rwork, process_recv_sockets);
  394. set_bit(CF_IS_OTHERCON, &othercon->flags);
  395. newcon->othercon = othercon;
  396. }
  397. othercon->sock = newsock;
  398. newsock->sk->sk_user_data = othercon;
  399. add_sock(newsock, othercon);
  400. addcon = othercon;
  401. }
  402. else {
  403. newsock->sk->sk_user_data = newcon;
  404. newcon->rx_action = receive_from_sock;
  405. add_sock(newsock, newcon);
  406. addcon = newcon;
  407. }
  408. mutex_unlock(&newcon->sock_mutex);
  409. /*
  410. * Add it to the active queue in case we got data
  411. * beween processing the accept adding the socket
  412. * to the read_sockets list
  413. */
  414. if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
  415. queue_work(recv_workqueue, &addcon->rwork);
  416. mutex_unlock(&con->sock_mutex);
  417. return 0;
  418. accept_err:
  419. mutex_unlock(&con->sock_mutex);
  420. sock_release(newsock);
  421. if (result != -EAGAIN)
  422. printk("dlm: error accepting connection from node: %d\n", result);
  423. return result;
  424. }
  425. /* Connect a new socket to its peer */
  426. static void connect_to_sock(struct connection *con)
  427. {
  428. int result = -EHOSTUNREACH;
  429. struct sockaddr_storage saddr;
  430. int addr_len;
  431. struct socket *sock;
  432. if (con->nodeid == 0) {
  433. log_print("attempt to connect sock 0 foiled");
  434. return;
  435. }
  436. mutex_lock(&con->sock_mutex);
  437. if (con->retries++ > MAX_CONNECT_RETRIES)
  438. goto out;
  439. /* Some odd races can cause double-connects, ignore them */
  440. if (con->sock) {
  441. result = 0;
  442. goto out;
  443. }
  444. /* Create a socket to communicate with */
  445. result = sock_create_kern(dlm_local_addr.ss_family, SOCK_STREAM,
  446. IPPROTO_TCP, &sock);
  447. if (result < 0)
  448. goto out_err;
  449. memset(&saddr, 0, sizeof(saddr));
  450. if (dlm_nodeid_to_addr(con->nodeid, &saddr))
  451. goto out_err;
  452. sock->sk->sk_user_data = con;
  453. con->rx_action = receive_from_sock;
  454. make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
  455. add_sock(sock, con);
  456. log_print("connecting to %d", con->nodeid);
  457. result =
  458. sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
  459. O_NONBLOCK);
  460. if (result == -EINPROGRESS)
  461. result = 0;
  462. if (result == 0)
  463. goto out;
  464. out_err:
  465. if (con->sock) {
  466. sock_release(con->sock);
  467. con->sock = NULL;
  468. }
  469. /*
  470. * Some errors are fatal and this list might need adjusting. For other
  471. * errors we try again until the max number of retries is reached.
  472. */
  473. if (result != -EHOSTUNREACH && result != -ENETUNREACH &&
  474. result != -ENETDOWN && result != EINVAL
  475. && result != -EPROTONOSUPPORT) {
  476. lowcomms_connect_sock(con);
  477. result = 0;
  478. }
  479. out:
  480. mutex_unlock(&con->sock_mutex);
  481. return;
  482. }
  483. static struct socket *create_listen_sock(struct connection *con,
  484. struct sockaddr_storage *saddr)
  485. {
  486. struct socket *sock = NULL;
  487. mm_segment_t fs;
  488. int result = 0;
  489. int one = 1;
  490. int addr_len;
  491. if (dlm_local_addr.ss_family == AF_INET)
  492. addr_len = sizeof(struct sockaddr_in);
  493. else
  494. addr_len = sizeof(struct sockaddr_in6);
  495. /* Create a socket to communicate with */
  496. result = sock_create_kern(dlm_local_addr.ss_family, SOCK_STREAM, IPPROTO_TCP, &sock);
  497. if (result < 0) {
  498. printk("dlm: Can't create listening comms socket\n");
  499. goto create_out;
  500. }
  501. fs = get_fs();
  502. set_fs(get_ds());
  503. result = sock_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
  504. (char *)&one, sizeof(one));
  505. set_fs(fs);
  506. if (result < 0) {
  507. printk("dlm: Failed to set SO_REUSEADDR on socket: result=%d\n",
  508. result);
  509. }
  510. sock->sk->sk_user_data = con;
  511. con->rx_action = accept_from_sock;
  512. con->sock = sock;
  513. /* Bind to our port */
  514. make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
  515. result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
  516. if (result < 0) {
  517. printk("dlm: Can't bind to port %d\n", dlm_config.ci_tcp_port);
  518. sock_release(sock);
  519. sock = NULL;
  520. con->sock = NULL;
  521. goto create_out;
  522. }
  523. fs = get_fs();
  524. set_fs(get_ds());
  525. result = sock_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
  526. (char *)&one, sizeof(one));
  527. set_fs(fs);
  528. if (result < 0) {
  529. printk("dlm: Set keepalive failed: %d\n", result);
  530. }
  531. result = sock->ops->listen(sock, 5);
  532. if (result < 0) {
  533. printk("dlm: Can't listen on port %d\n", dlm_config.ci_tcp_port);
  534. sock_release(sock);
  535. sock = NULL;
  536. goto create_out;
  537. }
  538. create_out:
  539. return sock;
  540. }
  541. /* Listen on all interfaces */
  542. static int listen_for_all(void)
  543. {
  544. struct socket *sock = NULL;
  545. struct connection *con = nodeid2con(0, GFP_KERNEL);
  546. int result = -EINVAL;
  547. /* We don't support multi-homed hosts */
  548. set_bit(CF_IS_OTHERCON, &con->flags);
  549. sock = create_listen_sock(con, &dlm_local_addr);
  550. if (sock) {
  551. add_sock(sock, con);
  552. result = 0;
  553. }
  554. else {
  555. result = -EADDRINUSE;
  556. }
  557. return result;
  558. }
  559. static struct writequeue_entry *new_writequeue_entry(struct connection *con,
  560. gfp_t allocation)
  561. {
  562. struct writequeue_entry *entry;
  563. entry = kmalloc(sizeof(struct writequeue_entry), allocation);
  564. if (!entry)
  565. return NULL;
  566. entry->page = alloc_page(allocation);
  567. if (!entry->page) {
  568. kfree(entry);
  569. return NULL;
  570. }
  571. entry->offset = 0;
  572. entry->len = 0;
  573. entry->end = 0;
  574. entry->users = 0;
  575. entry->con = con;
  576. return entry;
  577. }
  578. void *dlm_lowcomms_get_buffer(int nodeid, int len,
  579. gfp_t allocation, char **ppc)
  580. {
  581. struct connection *con;
  582. struct writequeue_entry *e;
  583. int offset = 0;
  584. int users = 0;
  585. con = nodeid2con(nodeid, allocation);
  586. if (!con)
  587. return NULL;
  588. spin_lock(&con->writequeue_lock);
  589. e = list_entry(con->writequeue.prev, struct writequeue_entry, list);
  590. if ((&e->list == &con->writequeue) ||
  591. (PAGE_CACHE_SIZE - e->end < len)) {
  592. e = NULL;
  593. } else {
  594. offset = e->end;
  595. e->end += len;
  596. users = e->users++;
  597. }
  598. spin_unlock(&con->writequeue_lock);
  599. if (e) {
  600. got_one:
  601. if (users == 0)
  602. kmap(e->page);
  603. *ppc = page_address(e->page) + offset;
  604. return e;
  605. }
  606. e = new_writequeue_entry(con, allocation);
  607. if (e) {
  608. spin_lock(&con->writequeue_lock);
  609. offset = e->end;
  610. e->end += len;
  611. users = e->users++;
  612. list_add_tail(&e->list, &con->writequeue);
  613. spin_unlock(&con->writequeue_lock);
  614. goto got_one;
  615. }
  616. return NULL;
  617. }
  618. void dlm_lowcomms_commit_buffer(void *mh)
  619. {
  620. struct writequeue_entry *e = (struct writequeue_entry *)mh;
  621. struct connection *con = e->con;
  622. int users;
  623. spin_lock(&con->writequeue_lock);
  624. users = --e->users;
  625. if (users)
  626. goto out;
  627. e->len = e->end - e->offset;
  628. kunmap(e->page);
  629. spin_unlock(&con->writequeue_lock);
  630. if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
  631. queue_work(send_workqueue, &con->swork);
  632. }
  633. return;
  634. out:
  635. spin_unlock(&con->writequeue_lock);
  636. return;
  637. }
  638. static void free_entry(struct writequeue_entry *e)
  639. {
  640. __free_page(e->page);
  641. kfree(e);
  642. }
  643. /* Send a message */
  644. static void send_to_sock(struct connection *con)
  645. {
  646. int ret = 0;
  647. ssize_t(*sendpage) (struct socket *, struct page *, int, size_t, int);
  648. const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
  649. struct writequeue_entry *e;
  650. int len, offset;
  651. mutex_lock(&con->sock_mutex);
  652. if (con->sock == NULL)
  653. goto out_connect;
  654. sendpage = con->sock->ops->sendpage;
  655. spin_lock(&con->writequeue_lock);
  656. for (;;) {
  657. e = list_entry(con->writequeue.next, struct writequeue_entry,
  658. list);
  659. if ((struct list_head *) e == &con->writequeue)
  660. break;
  661. len = e->len;
  662. offset = e->offset;
  663. BUG_ON(len == 0 && e->users == 0);
  664. spin_unlock(&con->writequeue_lock);
  665. kmap(e->page);
  666. ret = 0;
  667. if (len) {
  668. ret = sendpage(con->sock, e->page, offset, len,
  669. msg_flags);
  670. if (ret == -EAGAIN || ret == 0)
  671. goto out;
  672. if (ret <= 0)
  673. goto send_error;
  674. }
  675. else {
  676. /* Don't starve people filling buffers */
  677. cond_resched();
  678. }
  679. spin_lock(&con->writequeue_lock);
  680. e->offset += ret;
  681. e->len -= ret;
  682. if (e->len == 0 && e->users == 0) {
  683. list_del(&e->list);
  684. kunmap(e->page);
  685. free_entry(e);
  686. continue;
  687. }
  688. }
  689. spin_unlock(&con->writequeue_lock);
  690. out:
  691. mutex_unlock(&con->sock_mutex);
  692. return;
  693. send_error:
  694. mutex_unlock(&con->sock_mutex);
  695. close_connection(con, false);
  696. lowcomms_connect_sock(con);
  697. return;
  698. out_connect:
  699. mutex_unlock(&con->sock_mutex);
  700. connect_to_sock(con);
  701. return;
  702. }
  703. static void clean_one_writequeue(struct connection *con)
  704. {
  705. struct list_head *list;
  706. struct list_head *temp;
  707. spin_lock(&con->writequeue_lock);
  708. list_for_each_safe(list, temp, &con->writequeue) {
  709. struct writequeue_entry *e =
  710. list_entry(list, struct writequeue_entry, list);
  711. list_del(&e->list);
  712. free_entry(e);
  713. }
  714. spin_unlock(&con->writequeue_lock);
  715. }
  716. /* Called from recovery when it knows that a node has
  717. left the cluster */
  718. int dlm_lowcomms_close(int nodeid)
  719. {
  720. struct connection *con;
  721. if (!connections)
  722. goto out;
  723. log_print("closing connection to node %d", nodeid);
  724. con = nodeid2con(nodeid, 0);
  725. if (con) {
  726. clean_one_writequeue(con);
  727. close_connection(con, true);
  728. }
  729. return 0;
  730. out:
  731. return -1;
  732. }
  733. /* Look for activity on active sockets */
  734. static void process_recv_sockets(struct work_struct *work)
  735. {
  736. struct connection *con = container_of(work, struct connection, rwork);
  737. int err;
  738. clear_bit(CF_READ_PENDING, &con->flags);
  739. do {
  740. err = con->rx_action(con);
  741. } while (!err);
  742. }
  743. static void process_send_sockets(struct work_struct *work)
  744. {
  745. struct connection *con = container_of(work, struct connection, swork);
  746. if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
  747. connect_to_sock(con);
  748. }
  749. clear_bit(CF_WRITE_PENDING, &con->flags);
  750. send_to_sock(con);
  751. }
  752. /* Discard all entries on the write queues */
  753. static void clean_writequeues(void)
  754. {
  755. int nodeid;
  756. for (nodeid = 1; nodeid < conn_array_size; nodeid++) {
  757. struct connection *con = nodeid2con(nodeid, 0);
  758. if (con)
  759. clean_one_writequeue(con);
  760. }
  761. }
  762. static void work_stop(void)
  763. {
  764. destroy_workqueue(recv_workqueue);
  765. destroy_workqueue(send_workqueue);
  766. }
  767. static int work_start(void)
  768. {
  769. int error;
  770. recv_workqueue = create_workqueue("dlm_recv");
  771. error = IS_ERR(recv_workqueue);
  772. if (error) {
  773. log_print("can't start dlm_recv %d", error);
  774. return error;
  775. }
  776. send_workqueue = create_singlethread_workqueue("dlm_send");
  777. error = IS_ERR(send_workqueue);
  778. if (error) {
  779. log_print("can't start dlm_send %d", error);
  780. destroy_workqueue(recv_workqueue);
  781. return error;
  782. }
  783. return 0;
  784. }
  785. void dlm_lowcomms_stop(void)
  786. {
  787. int i;
  788. /* Set all the flags to prevent any
  789. socket activity.
  790. */
  791. for (i = 0; i < conn_array_size; i++) {
  792. if (connections[i])
  793. connections[i]->flags |= 0xFF;
  794. }
  795. work_stop();
  796. clean_writequeues();
  797. for (i = 0; i < conn_array_size; i++) {
  798. if (connections[i]) {
  799. close_connection(connections[i], true);
  800. if (connections[i]->othercon)
  801. kmem_cache_free(con_cache, connections[i]->othercon);
  802. kmem_cache_free(con_cache, connections[i]);
  803. }
  804. }
  805. kfree(connections);
  806. connections = NULL;
  807. kmem_cache_destroy(con_cache);
  808. }
  809. /* This is quite likely to sleep... */
  810. int dlm_lowcomms_start(void)
  811. {
  812. int error = 0;
  813. error = -ENOMEM;
  814. connections = kzalloc(sizeof(struct connection *) *
  815. NODE_INCREMENT, GFP_KERNEL);
  816. if (!connections)
  817. goto out;
  818. conn_array_size = NODE_INCREMENT;
  819. if (dlm_our_addr(&dlm_local_addr, 0)) {
  820. log_print("no local IP address has been set");
  821. goto fail_free_conn;
  822. }
  823. if (!dlm_our_addr(&dlm_local_addr, 1)) {
  824. log_print("This dlm comms module does not support multi-homed clustering");
  825. goto fail_free_conn;
  826. }
  827. con_cache = kmem_cache_create("dlm_conn", sizeof(struct connection),
  828. __alignof__(struct connection), 0,
  829. NULL, NULL);
  830. if (!con_cache)
  831. goto fail_free_conn;
  832. /* Start listening */
  833. error = listen_for_all();
  834. if (error)
  835. goto fail_unlisten;
  836. error = work_start();
  837. if (error)
  838. goto fail_unlisten;
  839. return 0;
  840. fail_unlisten:
  841. close_connection(connections[0], false);
  842. kmem_cache_free(con_cache, connections[0]);
  843. kmem_cache_destroy(con_cache);
  844. fail_free_conn:
  845. kfree(connections);
  846. out:
  847. return error;
  848. }
  849. /*
  850. * Overrides for Emacs so that we follow Linus's tabbing style.
  851. * Emacs will notice this stuff at the end of the file and automatically
  852. * adjust the settings for this buffer only. This must remain at the end
  853. * of the file.
  854. * ---------------------------------------------------------------------------
  855. * Local variables:
  856. * c-file-style: "linux"
  857. * End:
  858. */