lowcomms-tcp.c 27 KB

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