lowcomms-tcp.c 23 KB

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