lowcomms.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  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. * lowcomms will choose to use wither TCP or SCTP as its transport layer
  39. * depending on the configuration variable 'protocol'. This should be set
  40. * to 0 (default) for TCP or 1 for SCTP. It shouldbe configured using a
  41. * cluster-wide mechanism as it must be the same on all nodes of the cluster
  42. * for the DLM to function.
  43. *
  44. */
  45. #include <asm/ioctls.h>
  46. #include <net/sock.h>
  47. #include <net/tcp.h>
  48. #include <linux/pagemap.h>
  49. #include <linux/idr.h>
  50. #include <linux/file.h>
  51. #include <linux/sctp.h>
  52. #include <net/sctp/user.h>
  53. #include "dlm_internal.h"
  54. #include "lowcomms.h"
  55. #include "midcomms.h"
  56. #include "config.h"
  57. #define NEEDED_RMEM (4*1024*1024)
  58. struct cbuf {
  59. unsigned int base;
  60. unsigned int len;
  61. unsigned int mask;
  62. };
  63. static void cbuf_add(struct cbuf *cb, int n)
  64. {
  65. cb->len += n;
  66. }
  67. static int cbuf_data(struct cbuf *cb)
  68. {
  69. return ((cb->base + cb->len) & cb->mask);
  70. }
  71. static void cbuf_init(struct cbuf *cb, int size)
  72. {
  73. cb->base = cb->len = 0;
  74. cb->mask = size-1;
  75. }
  76. static void cbuf_eat(struct cbuf *cb, int n)
  77. {
  78. cb->len -= n;
  79. cb->base += n;
  80. cb->base &= cb->mask;
  81. }
  82. static bool cbuf_empty(struct cbuf *cb)
  83. {
  84. return cb->len == 0;
  85. }
  86. struct connection {
  87. struct socket *sock; /* NULL if not connected */
  88. uint32_t nodeid; /* So we know who we are in the list */
  89. struct mutex sock_mutex;
  90. unsigned long flags;
  91. #define CF_READ_PENDING 1
  92. #define CF_WRITE_PENDING 2
  93. #define CF_CONNECT_PENDING 3
  94. #define CF_INIT_PENDING 4
  95. #define CF_IS_OTHERCON 5
  96. struct list_head writequeue; /* List of outgoing writequeue_entries */
  97. spinlock_t writequeue_lock;
  98. int (*rx_action) (struct connection *); /* What to do when active */
  99. void (*connect_action) (struct connection *); /* What to do to connect */
  100. struct page *rx_page;
  101. struct cbuf cb;
  102. int retries;
  103. #define MAX_CONNECT_RETRIES 3
  104. int sctp_assoc;
  105. struct connection *othercon;
  106. struct work_struct rwork; /* Receive workqueue */
  107. struct work_struct swork; /* Send workqueue */
  108. };
  109. #define sock2con(x) ((struct connection *)(x)->sk_user_data)
  110. /* An entry waiting to be sent */
  111. struct writequeue_entry {
  112. struct list_head list;
  113. struct page *page;
  114. int offset;
  115. int len;
  116. int end;
  117. int users;
  118. struct connection *con;
  119. };
  120. static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT];
  121. static int dlm_local_count;
  122. /* Work queues */
  123. static struct workqueue_struct *recv_workqueue;
  124. static struct workqueue_struct *send_workqueue;
  125. static DEFINE_IDR(connections_idr);
  126. static DECLARE_MUTEX(connections_lock);
  127. static int max_nodeid;
  128. static struct kmem_cache *con_cache;
  129. static void process_recv_sockets(struct work_struct *work);
  130. static void process_send_sockets(struct work_struct *work);
  131. /*
  132. * If 'allocation' is zero then we don't attempt to create a new
  133. * connection structure for this node.
  134. */
  135. static struct connection *__nodeid2con(int nodeid, gfp_t alloc)
  136. {
  137. struct connection *con = NULL;
  138. int r;
  139. int n;
  140. con = idr_find(&connections_idr, nodeid);
  141. if (con || !alloc)
  142. return con;
  143. r = idr_pre_get(&connections_idr, alloc);
  144. if (!r)
  145. return NULL;
  146. con = kmem_cache_zalloc(con_cache, alloc);
  147. if (!con)
  148. return NULL;
  149. r = idr_get_new_above(&connections_idr, con, nodeid, &n);
  150. if (r) {
  151. kmem_cache_free(con_cache, con);
  152. return NULL;
  153. }
  154. if (n != nodeid) {
  155. idr_remove(&connections_idr, n);
  156. kmem_cache_free(con_cache, con);
  157. return NULL;
  158. }
  159. con->nodeid = nodeid;
  160. mutex_init(&con->sock_mutex);
  161. INIT_LIST_HEAD(&con->writequeue);
  162. spin_lock_init(&con->writequeue_lock);
  163. INIT_WORK(&con->swork, process_send_sockets);
  164. INIT_WORK(&con->rwork, process_recv_sockets);
  165. /* Setup action pointers for child sockets */
  166. if (con->nodeid) {
  167. struct connection *zerocon = idr_find(&connections_idr, 0);
  168. con->connect_action = zerocon->connect_action;
  169. if (!con->rx_action)
  170. con->rx_action = zerocon->rx_action;
  171. }
  172. if (nodeid > max_nodeid)
  173. max_nodeid = nodeid;
  174. return con;
  175. }
  176. static struct connection *nodeid2con(int nodeid, gfp_t allocation)
  177. {
  178. struct connection *con;
  179. down(&connections_lock);
  180. con = __nodeid2con(nodeid, allocation);
  181. up(&connections_lock);
  182. return con;
  183. }
  184. /* This is a bit drastic, but only called when things go wrong */
  185. static struct connection *assoc2con(int assoc_id)
  186. {
  187. int i;
  188. struct connection *con;
  189. down(&connections_lock);
  190. for (i=0; i<=max_nodeid; i++) {
  191. con = __nodeid2con(i, 0);
  192. if (con && con->sctp_assoc == assoc_id) {
  193. up(&connections_lock);
  194. return con;
  195. }
  196. }
  197. up(&connections_lock);
  198. return NULL;
  199. }
  200. static int nodeid_to_addr(int nodeid, struct sockaddr *retaddr)
  201. {
  202. struct sockaddr_storage addr;
  203. int error;
  204. if (!dlm_local_count)
  205. return -1;
  206. error = dlm_nodeid_to_addr(nodeid, &addr);
  207. if (error)
  208. return error;
  209. if (dlm_local_addr[0]->ss_family == AF_INET) {
  210. struct sockaddr_in *in4 = (struct sockaddr_in *) &addr;
  211. struct sockaddr_in *ret4 = (struct sockaddr_in *) retaddr;
  212. ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
  213. } else {
  214. struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &addr;
  215. struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) retaddr;
  216. memcpy(&ret6->sin6_addr, &in6->sin6_addr,
  217. sizeof(in6->sin6_addr));
  218. }
  219. return 0;
  220. }
  221. /* Data available on socket or listen socket received a connect */
  222. static void lowcomms_data_ready(struct sock *sk, int count_unused)
  223. {
  224. struct connection *con = sock2con(sk);
  225. if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags))
  226. queue_work(recv_workqueue, &con->rwork);
  227. }
  228. static void lowcomms_write_space(struct sock *sk)
  229. {
  230. struct connection *con = sock2con(sk);
  231. if (con && !test_and_set_bit(CF_WRITE_PENDING, &con->flags))
  232. queue_work(send_workqueue, &con->swork);
  233. }
  234. static inline void lowcomms_connect_sock(struct connection *con)
  235. {
  236. if (!test_and_set_bit(CF_CONNECT_PENDING, &con->flags))
  237. queue_work(send_workqueue, &con->swork);
  238. }
  239. static void lowcomms_state_change(struct sock *sk)
  240. {
  241. if (sk->sk_state == TCP_ESTABLISHED)
  242. lowcomms_write_space(sk);
  243. }
  244. /* Make a socket active */
  245. static int add_sock(struct socket *sock, struct connection *con)
  246. {
  247. con->sock = sock;
  248. /* Install a data_ready callback */
  249. con->sock->sk->sk_data_ready = lowcomms_data_ready;
  250. con->sock->sk->sk_write_space = lowcomms_write_space;
  251. con->sock->sk->sk_state_change = lowcomms_state_change;
  252. con->sock->sk->sk_user_data = con;
  253. return 0;
  254. }
  255. /* Add the port number to an IPv6 or 4 sockaddr and return the address
  256. length */
  257. static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
  258. int *addr_len)
  259. {
  260. saddr->ss_family = dlm_local_addr[0]->ss_family;
  261. if (saddr->ss_family == AF_INET) {
  262. struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
  263. in4_addr->sin_port = cpu_to_be16(port);
  264. *addr_len = sizeof(struct sockaddr_in);
  265. memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
  266. } else {
  267. struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
  268. in6_addr->sin6_port = cpu_to_be16(port);
  269. *addr_len = sizeof(struct sockaddr_in6);
  270. }
  271. memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
  272. }
  273. /* Close a remote connection and tidy up */
  274. static void close_connection(struct connection *con, bool and_other)
  275. {
  276. mutex_lock(&con->sock_mutex);
  277. if (con->sock) {
  278. sock_release(con->sock);
  279. con->sock = NULL;
  280. }
  281. if (con->othercon && and_other) {
  282. /* Will only re-enter once. */
  283. close_connection(con->othercon, false);
  284. }
  285. if (con->rx_page) {
  286. __free_page(con->rx_page);
  287. con->rx_page = NULL;
  288. }
  289. con->retries = 0;
  290. mutex_unlock(&con->sock_mutex);
  291. }
  292. /* We only send shutdown messages to nodes that are not part of the cluster */
  293. static void sctp_send_shutdown(sctp_assoc_t associd)
  294. {
  295. static char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
  296. struct msghdr outmessage;
  297. struct cmsghdr *cmsg;
  298. struct sctp_sndrcvinfo *sinfo;
  299. int ret;
  300. struct connection *con;
  301. con = nodeid2con(0,0);
  302. BUG_ON(con == NULL);
  303. outmessage.msg_name = NULL;
  304. outmessage.msg_namelen = 0;
  305. outmessage.msg_control = outcmsg;
  306. outmessage.msg_controllen = sizeof(outcmsg);
  307. outmessage.msg_flags = MSG_EOR;
  308. cmsg = CMSG_FIRSTHDR(&outmessage);
  309. cmsg->cmsg_level = IPPROTO_SCTP;
  310. cmsg->cmsg_type = SCTP_SNDRCV;
  311. cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
  312. outmessage.msg_controllen = cmsg->cmsg_len;
  313. sinfo = CMSG_DATA(cmsg);
  314. memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
  315. sinfo->sinfo_flags |= MSG_EOF;
  316. sinfo->sinfo_assoc_id = associd;
  317. ret = kernel_sendmsg(con->sock, &outmessage, NULL, 0, 0);
  318. if (ret != 0)
  319. log_print("send EOF to node failed: %d", ret);
  320. }
  321. /* INIT failed but we don't know which node...
  322. restart INIT on all pending nodes */
  323. static void sctp_init_failed(void)
  324. {
  325. int i;
  326. struct connection *con;
  327. down(&connections_lock);
  328. for (i=1; i<=max_nodeid; i++) {
  329. con = __nodeid2con(i, 0);
  330. if (!con)
  331. continue;
  332. con->sctp_assoc = 0;
  333. if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
  334. if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
  335. queue_work(send_workqueue, &con->swork);
  336. }
  337. }
  338. }
  339. up(&connections_lock);
  340. }
  341. /* Something happened to an association */
  342. static void process_sctp_notification(struct connection *con,
  343. struct msghdr *msg, char *buf)
  344. {
  345. union sctp_notification *sn = (union sctp_notification *)buf;
  346. if (sn->sn_header.sn_type == SCTP_ASSOC_CHANGE) {
  347. switch (sn->sn_assoc_change.sac_state) {
  348. case SCTP_COMM_UP:
  349. case SCTP_RESTART:
  350. {
  351. /* Check that the new node is in the lockspace */
  352. struct sctp_prim prim;
  353. int nodeid;
  354. int prim_len, ret;
  355. int addr_len;
  356. struct connection *new_con;
  357. struct file *file;
  358. sctp_peeloff_arg_t parg;
  359. int parglen = sizeof(parg);
  360. /*
  361. * We get this before any data for an association.
  362. * We verify that the node is in the cluster and
  363. * then peel off a socket for it.
  364. */
  365. if ((int)sn->sn_assoc_change.sac_assoc_id <= 0) {
  366. log_print("COMM_UP for invalid assoc ID %d",
  367. (int)sn->sn_assoc_change.sac_assoc_id);
  368. sctp_init_failed();
  369. return;
  370. }
  371. memset(&prim, 0, sizeof(struct sctp_prim));
  372. prim_len = sizeof(struct sctp_prim);
  373. prim.ssp_assoc_id = sn->sn_assoc_change.sac_assoc_id;
  374. ret = kernel_getsockopt(con->sock,
  375. IPPROTO_SCTP,
  376. SCTP_PRIMARY_ADDR,
  377. (char*)&prim,
  378. &prim_len);
  379. if (ret < 0) {
  380. log_print("getsockopt/sctp_primary_addr on "
  381. "new assoc %d failed : %d",
  382. (int)sn->sn_assoc_change.sac_assoc_id,
  383. ret);
  384. /* Retry INIT later */
  385. new_con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
  386. if (new_con)
  387. clear_bit(CF_CONNECT_PENDING, &con->flags);
  388. return;
  389. }
  390. make_sockaddr(&prim.ssp_addr, 0, &addr_len);
  391. if (dlm_addr_to_nodeid(&prim.ssp_addr, &nodeid)) {
  392. int i;
  393. unsigned char *b=(unsigned char *)&prim.ssp_addr;
  394. log_print("reject connect from unknown addr");
  395. for (i=0; i<sizeof(struct sockaddr_storage);i++)
  396. printk("%02x ", b[i]);
  397. printk("\n");
  398. sctp_send_shutdown(prim.ssp_assoc_id);
  399. return;
  400. }
  401. new_con = nodeid2con(nodeid, GFP_KERNEL);
  402. if (!new_con)
  403. return;
  404. /* Peel off a new sock */
  405. parg.associd = sn->sn_assoc_change.sac_assoc_id;
  406. ret = kernel_getsockopt(con->sock, IPPROTO_SCTP,
  407. SCTP_SOCKOPT_PEELOFF,
  408. (void *)&parg, &parglen);
  409. if (ret) {
  410. log_print("Can't peel off a socket for "
  411. "connection %d to node %d: err=%d\n",
  412. parg.associd, nodeid, ret);
  413. }
  414. file = fget(parg.sd);
  415. new_con->sock = SOCKET_I(file->f_dentry->d_inode);
  416. add_sock(new_con->sock, new_con);
  417. fput(file);
  418. put_unused_fd(parg.sd);
  419. log_print("got new/restarted association %d nodeid %d",
  420. (int)sn->sn_assoc_change.sac_assoc_id, nodeid);
  421. /* Send any pending writes */
  422. clear_bit(CF_CONNECT_PENDING, &new_con->flags);
  423. clear_bit(CF_INIT_PENDING, &con->flags);
  424. if (!test_and_set_bit(CF_WRITE_PENDING, &new_con->flags)) {
  425. queue_work(send_workqueue, &new_con->swork);
  426. }
  427. if (!test_and_set_bit(CF_READ_PENDING, &new_con->flags))
  428. queue_work(recv_workqueue, &new_con->rwork);
  429. }
  430. break;
  431. case SCTP_COMM_LOST:
  432. case SCTP_SHUTDOWN_COMP:
  433. {
  434. con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
  435. if (con) {
  436. con->sctp_assoc = 0;
  437. }
  438. }
  439. break;
  440. /* We don't know which INIT failed, so clear the PENDING flags
  441. * on them all. if assoc_id is zero then it will then try
  442. * again */
  443. case SCTP_CANT_STR_ASSOC:
  444. {
  445. log_print("Can't start SCTP association - retrying");
  446. sctp_init_failed();
  447. }
  448. break;
  449. default:
  450. log_print("unexpected SCTP assoc change id=%d state=%d",
  451. (int)sn->sn_assoc_change.sac_assoc_id,
  452. sn->sn_assoc_change.sac_state);
  453. }
  454. }
  455. }
  456. /* Data received from remote end */
  457. static int receive_from_sock(struct connection *con)
  458. {
  459. int ret = 0;
  460. struct msghdr msg = {};
  461. struct kvec iov[2];
  462. unsigned len;
  463. int r;
  464. int call_again_soon = 0;
  465. int nvec;
  466. char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
  467. mutex_lock(&con->sock_mutex);
  468. if (con->sock == NULL) {
  469. ret = -EAGAIN;
  470. goto out_close;
  471. }
  472. if (con->rx_page == NULL) {
  473. /*
  474. * This doesn't need to be atomic, but I think it should
  475. * improve performance if it is.
  476. */
  477. con->rx_page = alloc_page(GFP_ATOMIC);
  478. if (con->rx_page == NULL)
  479. goto out_resched;
  480. cbuf_init(&con->cb, PAGE_CACHE_SIZE);
  481. }
  482. /* Only SCTP needs these really */
  483. memset(&incmsg, 0, sizeof(incmsg));
  484. msg.msg_control = incmsg;
  485. msg.msg_controllen = sizeof(incmsg);
  486. /*
  487. * iov[0] is the bit of the circular buffer between the current end
  488. * point (cb.base + cb.len) and the end of the buffer.
  489. */
  490. iov[0].iov_len = con->cb.base - cbuf_data(&con->cb);
  491. iov[0].iov_base = page_address(con->rx_page) + cbuf_data(&con->cb);
  492. iov[1].iov_len = 0;
  493. nvec = 1;
  494. /*
  495. * iov[1] is the bit of the circular buffer between the start of the
  496. * buffer and the start of the currently used section (cb.base)
  497. */
  498. if (cbuf_data(&con->cb) >= con->cb.base) {
  499. iov[0].iov_len = PAGE_CACHE_SIZE - cbuf_data(&con->cb);
  500. iov[1].iov_len = con->cb.base;
  501. iov[1].iov_base = page_address(con->rx_page);
  502. nvec = 2;
  503. }
  504. len = iov[0].iov_len + iov[1].iov_len;
  505. r = ret = kernel_recvmsg(con->sock, &msg, iov, nvec, len,
  506. MSG_DONTWAIT | MSG_NOSIGNAL);
  507. if (ret <= 0)
  508. goto out_close;
  509. /* Process SCTP notifications */
  510. if (msg.msg_flags & MSG_NOTIFICATION) {
  511. msg.msg_control = incmsg;
  512. msg.msg_controllen = sizeof(incmsg);
  513. process_sctp_notification(con, &msg,
  514. page_address(con->rx_page) + con->cb.base);
  515. mutex_unlock(&con->sock_mutex);
  516. return 0;
  517. }
  518. BUG_ON(con->nodeid == 0);
  519. if (ret == len)
  520. call_again_soon = 1;
  521. cbuf_add(&con->cb, ret);
  522. ret = dlm_process_incoming_buffer(con->nodeid,
  523. page_address(con->rx_page),
  524. con->cb.base, con->cb.len,
  525. PAGE_CACHE_SIZE);
  526. if (ret == -EBADMSG) {
  527. log_print("lowcomms: addr=%p, base=%u, len=%u, "
  528. "iov_len=%u, iov_base[0]=%p, read=%d",
  529. page_address(con->rx_page), con->cb.base, con->cb.len,
  530. len, iov[0].iov_base, r);
  531. }
  532. if (ret < 0)
  533. goto out_close;
  534. cbuf_eat(&con->cb, ret);
  535. if (cbuf_empty(&con->cb) && !call_again_soon) {
  536. __free_page(con->rx_page);
  537. con->rx_page = NULL;
  538. }
  539. if (call_again_soon)
  540. goto out_resched;
  541. mutex_unlock(&con->sock_mutex);
  542. return 0;
  543. out_resched:
  544. if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
  545. queue_work(recv_workqueue, &con->rwork);
  546. mutex_unlock(&con->sock_mutex);
  547. return -EAGAIN;
  548. out_close:
  549. mutex_unlock(&con->sock_mutex);
  550. if (ret != -EAGAIN) {
  551. close_connection(con, false);
  552. /* Reconnect when there is something to send */
  553. }
  554. /* Don't return success if we really got EOF */
  555. if (ret == 0)
  556. ret = -EAGAIN;
  557. return ret;
  558. }
  559. /* Listening socket is busy, accept a connection */
  560. static int tcp_accept_from_sock(struct connection *con)
  561. {
  562. int result;
  563. struct sockaddr_storage peeraddr;
  564. struct socket *newsock;
  565. int len;
  566. int nodeid;
  567. struct connection *newcon;
  568. struct connection *addcon;
  569. memset(&peeraddr, 0, sizeof(peeraddr));
  570. result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
  571. IPPROTO_TCP, &newsock);
  572. if (result < 0)
  573. return -ENOMEM;
  574. mutex_lock_nested(&con->sock_mutex, 0);
  575. result = -ENOTCONN;
  576. if (con->sock == NULL)
  577. goto accept_err;
  578. newsock->type = con->sock->type;
  579. newsock->ops = con->sock->ops;
  580. result = con->sock->ops->accept(con->sock, newsock, O_NONBLOCK);
  581. if (result < 0)
  582. goto accept_err;
  583. /* Get the connected socket's peer */
  584. memset(&peeraddr, 0, sizeof(peeraddr));
  585. if (newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr,
  586. &len, 2)) {
  587. result = -ECONNABORTED;
  588. goto accept_err;
  589. }
  590. /* Get the new node's NODEID */
  591. make_sockaddr(&peeraddr, 0, &len);
  592. if (dlm_addr_to_nodeid(&peeraddr, &nodeid)) {
  593. log_print("connect from non cluster node");
  594. sock_release(newsock);
  595. mutex_unlock(&con->sock_mutex);
  596. return -1;
  597. }
  598. log_print("got connection from %d", nodeid);
  599. /* Check to see if we already have a connection to this node. This
  600. * could happen if the two nodes initiate a connection at roughly
  601. * the same time and the connections cross on the wire.
  602. * In this case we store the incoming one in "othercon"
  603. */
  604. newcon = nodeid2con(nodeid, GFP_KERNEL);
  605. if (!newcon) {
  606. result = -ENOMEM;
  607. goto accept_err;
  608. }
  609. mutex_lock_nested(&newcon->sock_mutex, 1);
  610. if (newcon->sock) {
  611. struct connection *othercon = newcon->othercon;
  612. if (!othercon) {
  613. othercon = kmem_cache_zalloc(con_cache, GFP_KERNEL);
  614. if (!othercon) {
  615. log_print("failed to allocate incoming socket");
  616. mutex_unlock(&newcon->sock_mutex);
  617. result = -ENOMEM;
  618. goto accept_err;
  619. }
  620. othercon->nodeid = nodeid;
  621. othercon->rx_action = receive_from_sock;
  622. mutex_init(&othercon->sock_mutex);
  623. INIT_WORK(&othercon->swork, process_send_sockets);
  624. INIT_WORK(&othercon->rwork, process_recv_sockets);
  625. set_bit(CF_IS_OTHERCON, &othercon->flags);
  626. }
  627. if (!othercon->sock) {
  628. newcon->othercon = othercon;
  629. othercon->sock = newsock;
  630. newsock->sk->sk_user_data = othercon;
  631. add_sock(newsock, othercon);
  632. addcon = othercon;
  633. }
  634. else {
  635. printk("Extra connection from node %d attempted\n", nodeid);
  636. result = -EAGAIN;
  637. mutex_unlock(&newcon->sock_mutex);
  638. goto accept_err;
  639. }
  640. }
  641. else {
  642. newsock->sk->sk_user_data = newcon;
  643. newcon->rx_action = receive_from_sock;
  644. add_sock(newsock, newcon);
  645. addcon = newcon;
  646. }
  647. mutex_unlock(&newcon->sock_mutex);
  648. /*
  649. * Add it to the active queue in case we got data
  650. * beween processing the accept adding the socket
  651. * to the read_sockets list
  652. */
  653. if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
  654. queue_work(recv_workqueue, &addcon->rwork);
  655. mutex_unlock(&con->sock_mutex);
  656. return 0;
  657. accept_err:
  658. mutex_unlock(&con->sock_mutex);
  659. sock_release(newsock);
  660. if (result != -EAGAIN)
  661. log_print("error accepting connection from node: %d", result);
  662. return result;
  663. }
  664. static void free_entry(struct writequeue_entry *e)
  665. {
  666. __free_page(e->page);
  667. kfree(e);
  668. }
  669. /* Initiate an SCTP association.
  670. This is a special case of send_to_sock() in that we don't yet have a
  671. peeled-off socket for this association, so we use the listening socket
  672. and add the primary IP address of the remote node.
  673. */
  674. static void sctp_init_assoc(struct connection *con)
  675. {
  676. struct sockaddr_storage rem_addr;
  677. char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
  678. struct msghdr outmessage;
  679. struct cmsghdr *cmsg;
  680. struct sctp_sndrcvinfo *sinfo;
  681. struct connection *base_con;
  682. struct writequeue_entry *e;
  683. int len, offset;
  684. int ret;
  685. int addrlen;
  686. struct kvec iov[1];
  687. if (test_and_set_bit(CF_INIT_PENDING, &con->flags))
  688. return;
  689. if (con->retries++ > MAX_CONNECT_RETRIES)
  690. return;
  691. log_print("Initiating association with node %d", con->nodeid);
  692. if (nodeid_to_addr(con->nodeid, (struct sockaddr *)&rem_addr)) {
  693. log_print("no address for nodeid %d", con->nodeid);
  694. return;
  695. }
  696. base_con = nodeid2con(0, 0);
  697. BUG_ON(base_con == NULL);
  698. make_sockaddr(&rem_addr, dlm_config.ci_tcp_port, &addrlen);
  699. outmessage.msg_name = &rem_addr;
  700. outmessage.msg_namelen = addrlen;
  701. outmessage.msg_control = outcmsg;
  702. outmessage.msg_controllen = sizeof(outcmsg);
  703. outmessage.msg_flags = MSG_EOR;
  704. spin_lock(&con->writequeue_lock);
  705. e = list_entry(con->writequeue.next, struct writequeue_entry,
  706. list);
  707. BUG_ON((struct list_head *) e == &con->writequeue);
  708. len = e->len;
  709. offset = e->offset;
  710. spin_unlock(&con->writequeue_lock);
  711. kmap(e->page);
  712. /* Send the first block off the write queue */
  713. iov[0].iov_base = page_address(e->page)+offset;
  714. iov[0].iov_len = len;
  715. cmsg = CMSG_FIRSTHDR(&outmessage);
  716. cmsg->cmsg_level = IPPROTO_SCTP;
  717. cmsg->cmsg_type = SCTP_SNDRCV;
  718. cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
  719. sinfo = CMSG_DATA(cmsg);
  720. memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
  721. sinfo->sinfo_ppid = cpu_to_le32(dlm_our_nodeid());
  722. outmessage.msg_controllen = cmsg->cmsg_len;
  723. ret = kernel_sendmsg(base_con->sock, &outmessage, iov, 1, len);
  724. if (ret < 0) {
  725. log_print("Send first packet to node %d failed: %d",
  726. con->nodeid, ret);
  727. /* Try again later */
  728. clear_bit(CF_CONNECT_PENDING, &con->flags);
  729. clear_bit(CF_INIT_PENDING, &con->flags);
  730. }
  731. else {
  732. spin_lock(&con->writequeue_lock);
  733. e->offset += ret;
  734. e->len -= ret;
  735. if (e->len == 0 && e->users == 0) {
  736. list_del(&e->list);
  737. kunmap(e->page);
  738. free_entry(e);
  739. }
  740. spin_unlock(&con->writequeue_lock);
  741. }
  742. }
  743. /* Connect a new socket to its peer */
  744. static void tcp_connect_to_sock(struct connection *con)
  745. {
  746. int result = -EHOSTUNREACH;
  747. struct sockaddr_storage saddr;
  748. int addr_len;
  749. struct socket *sock;
  750. if (con->nodeid == 0) {
  751. log_print("attempt to connect sock 0 foiled");
  752. return;
  753. }
  754. mutex_lock(&con->sock_mutex);
  755. if (con->retries++ > MAX_CONNECT_RETRIES)
  756. goto out;
  757. /* Some odd races can cause double-connects, ignore them */
  758. if (con->sock) {
  759. result = 0;
  760. goto out;
  761. }
  762. /* Create a socket to communicate with */
  763. result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
  764. IPPROTO_TCP, &sock);
  765. if (result < 0)
  766. goto out_err;
  767. memset(&saddr, 0, sizeof(saddr));
  768. if (dlm_nodeid_to_addr(con->nodeid, &saddr))
  769. goto out_err;
  770. sock->sk->sk_user_data = con;
  771. con->rx_action = receive_from_sock;
  772. con->connect_action = tcp_connect_to_sock;
  773. add_sock(sock, con);
  774. make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
  775. log_print("connecting to %d", con->nodeid);
  776. result =
  777. sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
  778. O_NONBLOCK);
  779. if (result == -EINPROGRESS)
  780. result = 0;
  781. if (result == 0)
  782. goto out;
  783. out_err:
  784. if (con->sock) {
  785. sock_release(con->sock);
  786. con->sock = NULL;
  787. }
  788. /*
  789. * Some errors are fatal and this list might need adjusting. For other
  790. * errors we try again until the max number of retries is reached.
  791. */
  792. if (result != -EHOSTUNREACH && result != -ENETUNREACH &&
  793. result != -ENETDOWN && result != EINVAL
  794. && result != -EPROTONOSUPPORT) {
  795. lowcomms_connect_sock(con);
  796. result = 0;
  797. }
  798. out:
  799. mutex_unlock(&con->sock_mutex);
  800. return;
  801. }
  802. static struct socket *tcp_create_listen_sock(struct connection *con,
  803. struct sockaddr_storage *saddr)
  804. {
  805. struct socket *sock = NULL;
  806. int result = 0;
  807. int one = 1;
  808. int addr_len;
  809. if (dlm_local_addr[0]->ss_family == AF_INET)
  810. addr_len = sizeof(struct sockaddr_in);
  811. else
  812. addr_len = sizeof(struct sockaddr_in6);
  813. /* Create a socket to communicate with */
  814. result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
  815. IPPROTO_TCP, &sock);
  816. if (result < 0) {
  817. log_print("Can't create listening comms socket");
  818. goto create_out;
  819. }
  820. result = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
  821. (char *)&one, sizeof(one));
  822. if (result < 0) {
  823. log_print("Failed to set SO_REUSEADDR on socket: %d", result);
  824. }
  825. sock->sk->sk_user_data = con;
  826. con->rx_action = tcp_accept_from_sock;
  827. con->connect_action = tcp_connect_to_sock;
  828. con->sock = sock;
  829. /* Bind to our port */
  830. make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
  831. result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
  832. if (result < 0) {
  833. log_print("Can't bind to port %d", dlm_config.ci_tcp_port);
  834. sock_release(sock);
  835. sock = NULL;
  836. con->sock = NULL;
  837. goto create_out;
  838. }
  839. result = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
  840. (char *)&one, sizeof(one));
  841. if (result < 0) {
  842. log_print("Set keepalive failed: %d", result);
  843. }
  844. result = sock->ops->listen(sock, 5);
  845. if (result < 0) {
  846. log_print("Can't listen on port %d", dlm_config.ci_tcp_port);
  847. sock_release(sock);
  848. sock = NULL;
  849. goto create_out;
  850. }
  851. create_out:
  852. return sock;
  853. }
  854. /* Get local addresses */
  855. static void init_local(void)
  856. {
  857. struct sockaddr_storage sas, *addr;
  858. int i;
  859. dlm_local_count = 0;
  860. for (i = 0; i < DLM_MAX_ADDR_COUNT - 1; i++) {
  861. if (dlm_our_addr(&sas, i))
  862. break;
  863. addr = kmalloc(sizeof(*addr), GFP_KERNEL);
  864. if (!addr)
  865. break;
  866. memcpy(addr, &sas, sizeof(*addr));
  867. dlm_local_addr[dlm_local_count++] = addr;
  868. }
  869. }
  870. /* Bind to an IP address. SCTP allows multiple address so it can do
  871. multi-homing */
  872. static int add_sctp_bind_addr(struct connection *sctp_con,
  873. struct sockaddr_storage *addr,
  874. int addr_len, int num)
  875. {
  876. int result = 0;
  877. if (num == 1)
  878. result = kernel_bind(sctp_con->sock,
  879. (struct sockaddr *) addr,
  880. addr_len);
  881. else
  882. result = kernel_setsockopt(sctp_con->sock, SOL_SCTP,
  883. SCTP_SOCKOPT_BINDX_ADD,
  884. (char *)addr, addr_len);
  885. if (result < 0)
  886. log_print("Can't bind to port %d addr number %d",
  887. dlm_config.ci_tcp_port, num);
  888. return result;
  889. }
  890. /* Initialise SCTP socket and bind to all interfaces */
  891. static int sctp_listen_for_all(void)
  892. {
  893. struct socket *sock = NULL;
  894. struct sockaddr_storage localaddr;
  895. struct sctp_event_subscribe subscribe;
  896. int result = -EINVAL, num = 1, i, addr_len;
  897. struct connection *con = nodeid2con(0, GFP_KERNEL);
  898. int bufsize = NEEDED_RMEM;
  899. if (!con)
  900. return -ENOMEM;
  901. log_print("Using SCTP for communications");
  902. result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_SEQPACKET,
  903. IPPROTO_SCTP, &sock);
  904. if (result < 0) {
  905. log_print("Can't create comms socket, check SCTP is loaded");
  906. goto out;
  907. }
  908. /* Listen for events */
  909. memset(&subscribe, 0, sizeof(subscribe));
  910. subscribe.sctp_data_io_event = 1;
  911. subscribe.sctp_association_event = 1;
  912. subscribe.sctp_send_failure_event = 1;
  913. subscribe.sctp_shutdown_event = 1;
  914. subscribe.sctp_partial_delivery_event = 1;
  915. result = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
  916. (char *)&bufsize, sizeof(bufsize));
  917. if (result)
  918. log_print("Error increasing buffer space on socket %d", result);
  919. result = kernel_setsockopt(sock, SOL_SCTP, SCTP_EVENTS,
  920. (char *)&subscribe, sizeof(subscribe));
  921. if (result < 0) {
  922. log_print("Failed to set SCTP_EVENTS on socket: result=%d",
  923. result);
  924. goto create_delsock;
  925. }
  926. /* Init con struct */
  927. sock->sk->sk_user_data = con;
  928. con->sock = sock;
  929. con->sock->sk->sk_data_ready = lowcomms_data_ready;
  930. con->rx_action = receive_from_sock;
  931. con->connect_action = sctp_init_assoc;
  932. /* Bind to all interfaces. */
  933. for (i = 0; i < dlm_local_count; i++) {
  934. memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
  935. make_sockaddr(&localaddr, dlm_config.ci_tcp_port, &addr_len);
  936. result = add_sctp_bind_addr(con, &localaddr, addr_len, num);
  937. if (result)
  938. goto create_delsock;
  939. ++num;
  940. }
  941. result = sock->ops->listen(sock, 5);
  942. if (result < 0) {
  943. log_print("Can't set socket listening");
  944. goto create_delsock;
  945. }
  946. return 0;
  947. create_delsock:
  948. sock_release(sock);
  949. con->sock = NULL;
  950. out:
  951. return result;
  952. }
  953. static int tcp_listen_for_all(void)
  954. {
  955. struct socket *sock = NULL;
  956. struct connection *con = nodeid2con(0, GFP_KERNEL);
  957. int result = -EINVAL;
  958. if (!con)
  959. return -ENOMEM;
  960. /* We don't support multi-homed hosts */
  961. if (dlm_local_addr[1] != NULL) {
  962. log_print("TCP protocol can't handle multi-homed hosts, "
  963. "try SCTP");
  964. return -EINVAL;
  965. }
  966. log_print("Using TCP for communications");
  967. sock = tcp_create_listen_sock(con, dlm_local_addr[0]);
  968. if (sock) {
  969. add_sock(sock, con);
  970. result = 0;
  971. }
  972. else {
  973. result = -EADDRINUSE;
  974. }
  975. return result;
  976. }
  977. static struct writequeue_entry *new_writequeue_entry(struct connection *con,
  978. gfp_t allocation)
  979. {
  980. struct writequeue_entry *entry;
  981. entry = kmalloc(sizeof(struct writequeue_entry), allocation);
  982. if (!entry)
  983. return NULL;
  984. entry->page = alloc_page(allocation);
  985. if (!entry->page) {
  986. kfree(entry);
  987. return NULL;
  988. }
  989. entry->offset = 0;
  990. entry->len = 0;
  991. entry->end = 0;
  992. entry->users = 0;
  993. entry->con = con;
  994. return entry;
  995. }
  996. void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc)
  997. {
  998. struct connection *con;
  999. struct writequeue_entry *e;
  1000. int offset = 0;
  1001. int users = 0;
  1002. con = nodeid2con(nodeid, allocation);
  1003. if (!con)
  1004. return NULL;
  1005. spin_lock(&con->writequeue_lock);
  1006. e = list_entry(con->writequeue.prev, struct writequeue_entry, list);
  1007. if ((&e->list == &con->writequeue) ||
  1008. (PAGE_CACHE_SIZE - e->end < len)) {
  1009. e = NULL;
  1010. } else {
  1011. offset = e->end;
  1012. e->end += len;
  1013. users = e->users++;
  1014. }
  1015. spin_unlock(&con->writequeue_lock);
  1016. if (e) {
  1017. got_one:
  1018. if (users == 0)
  1019. kmap(e->page);
  1020. *ppc = page_address(e->page) + offset;
  1021. return e;
  1022. }
  1023. e = new_writequeue_entry(con, allocation);
  1024. if (e) {
  1025. spin_lock(&con->writequeue_lock);
  1026. offset = e->end;
  1027. e->end += len;
  1028. users = e->users++;
  1029. list_add_tail(&e->list, &con->writequeue);
  1030. spin_unlock(&con->writequeue_lock);
  1031. goto got_one;
  1032. }
  1033. return NULL;
  1034. }
  1035. void dlm_lowcomms_commit_buffer(void *mh)
  1036. {
  1037. struct writequeue_entry *e = (struct writequeue_entry *)mh;
  1038. struct connection *con = e->con;
  1039. int users;
  1040. spin_lock(&con->writequeue_lock);
  1041. users = --e->users;
  1042. if (users)
  1043. goto out;
  1044. e->len = e->end - e->offset;
  1045. kunmap(e->page);
  1046. spin_unlock(&con->writequeue_lock);
  1047. if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
  1048. queue_work(send_workqueue, &con->swork);
  1049. }
  1050. return;
  1051. out:
  1052. spin_unlock(&con->writequeue_lock);
  1053. return;
  1054. }
  1055. /* Send a message */
  1056. static void send_to_sock(struct connection *con)
  1057. {
  1058. int ret = 0;
  1059. ssize_t(*sendpage) (struct socket *, struct page *, int, size_t, int);
  1060. const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
  1061. struct writequeue_entry *e;
  1062. int len, offset;
  1063. mutex_lock(&con->sock_mutex);
  1064. if (con->sock == NULL)
  1065. goto out_connect;
  1066. sendpage = con->sock->ops->sendpage;
  1067. spin_lock(&con->writequeue_lock);
  1068. for (;;) {
  1069. e = list_entry(con->writequeue.next, struct writequeue_entry,
  1070. list);
  1071. if ((struct list_head *) e == &con->writequeue)
  1072. break;
  1073. len = e->len;
  1074. offset = e->offset;
  1075. BUG_ON(len == 0 && e->users == 0);
  1076. spin_unlock(&con->writequeue_lock);
  1077. kmap(e->page);
  1078. ret = 0;
  1079. if (len) {
  1080. ret = sendpage(con->sock, e->page, offset, len,
  1081. msg_flags);
  1082. if (ret == -EAGAIN || ret == 0) {
  1083. cond_resched();
  1084. goto out;
  1085. }
  1086. if (ret <= 0)
  1087. goto send_error;
  1088. }
  1089. /* Don't starve people filling buffers */
  1090. cond_resched();
  1091. spin_lock(&con->writequeue_lock);
  1092. e->offset += ret;
  1093. e->len -= ret;
  1094. if (e->len == 0 && e->users == 0) {
  1095. list_del(&e->list);
  1096. kunmap(e->page);
  1097. free_entry(e);
  1098. continue;
  1099. }
  1100. }
  1101. spin_unlock(&con->writequeue_lock);
  1102. out:
  1103. mutex_unlock(&con->sock_mutex);
  1104. return;
  1105. send_error:
  1106. mutex_unlock(&con->sock_mutex);
  1107. close_connection(con, false);
  1108. lowcomms_connect_sock(con);
  1109. return;
  1110. out_connect:
  1111. mutex_unlock(&con->sock_mutex);
  1112. if (!test_bit(CF_INIT_PENDING, &con->flags))
  1113. lowcomms_connect_sock(con);
  1114. return;
  1115. }
  1116. static void clean_one_writequeue(struct connection *con)
  1117. {
  1118. struct list_head *list;
  1119. struct list_head *temp;
  1120. spin_lock(&con->writequeue_lock);
  1121. list_for_each_safe(list, temp, &con->writequeue) {
  1122. struct writequeue_entry *e =
  1123. list_entry(list, struct writequeue_entry, list);
  1124. list_del(&e->list);
  1125. free_entry(e);
  1126. }
  1127. spin_unlock(&con->writequeue_lock);
  1128. }
  1129. /* Called from recovery when it knows that a node has
  1130. left the cluster */
  1131. int dlm_lowcomms_close(int nodeid)
  1132. {
  1133. struct connection *con;
  1134. log_print("closing connection to node %d", nodeid);
  1135. con = nodeid2con(nodeid, 0);
  1136. if (con) {
  1137. clean_one_writequeue(con);
  1138. close_connection(con, true);
  1139. }
  1140. return 0;
  1141. }
  1142. /* Receive workqueue function */
  1143. static void process_recv_sockets(struct work_struct *work)
  1144. {
  1145. struct connection *con = container_of(work, struct connection, rwork);
  1146. int err;
  1147. clear_bit(CF_READ_PENDING, &con->flags);
  1148. do {
  1149. err = con->rx_action(con);
  1150. } while (!err);
  1151. }
  1152. /* Send workqueue function */
  1153. static void process_send_sockets(struct work_struct *work)
  1154. {
  1155. struct connection *con = container_of(work, struct connection, swork);
  1156. if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
  1157. con->connect_action(con);
  1158. }
  1159. clear_bit(CF_WRITE_PENDING, &con->flags);
  1160. send_to_sock(con);
  1161. }
  1162. /* Discard all entries on the write queues */
  1163. static void clean_writequeues(void)
  1164. {
  1165. int nodeid;
  1166. for (nodeid = 1; nodeid <= max_nodeid; nodeid++) {
  1167. struct connection *con = __nodeid2con(nodeid, 0);
  1168. if (con)
  1169. clean_one_writequeue(con);
  1170. }
  1171. }
  1172. static void work_stop(void)
  1173. {
  1174. destroy_workqueue(recv_workqueue);
  1175. destroy_workqueue(send_workqueue);
  1176. }
  1177. static int work_start(void)
  1178. {
  1179. int error;
  1180. recv_workqueue = create_workqueue("dlm_recv");
  1181. error = IS_ERR(recv_workqueue);
  1182. if (error) {
  1183. log_print("can't start dlm_recv %d", error);
  1184. return error;
  1185. }
  1186. send_workqueue = create_singlethread_workqueue("dlm_send");
  1187. error = IS_ERR(send_workqueue);
  1188. if (error) {
  1189. log_print("can't start dlm_send %d", error);
  1190. destroy_workqueue(recv_workqueue);
  1191. return error;
  1192. }
  1193. return 0;
  1194. }
  1195. void dlm_lowcomms_stop(void)
  1196. {
  1197. int i;
  1198. struct connection *con;
  1199. /* Set all the flags to prevent any
  1200. socket activity.
  1201. */
  1202. down(&connections_lock);
  1203. for (i = 0; i <= max_nodeid; i++) {
  1204. con = __nodeid2con(i, 0);
  1205. if (con) {
  1206. con->flags |= 0x0F;
  1207. if (con->sock)
  1208. con->sock->sk->sk_user_data = NULL;
  1209. }
  1210. }
  1211. up(&connections_lock);
  1212. work_stop();
  1213. down(&connections_lock);
  1214. clean_writequeues();
  1215. for (i = 0; i <= max_nodeid; i++) {
  1216. con = __nodeid2con(i, 0);
  1217. if (con) {
  1218. close_connection(con, true);
  1219. kmem_cache_free(con_cache, con);
  1220. }
  1221. }
  1222. max_nodeid = 0;
  1223. up(&connections_lock);
  1224. kmem_cache_destroy(con_cache);
  1225. idr_init(&connections_idr);
  1226. }
  1227. int dlm_lowcomms_start(void)
  1228. {
  1229. int error = -EINVAL;
  1230. struct connection *con;
  1231. init_local();
  1232. if (!dlm_local_count) {
  1233. error = -ENOTCONN;
  1234. log_print("no local IP address has been set");
  1235. goto out;
  1236. }
  1237. error = -ENOMEM;
  1238. con_cache = kmem_cache_create("dlm_conn", sizeof(struct connection),
  1239. __alignof__(struct connection), 0,
  1240. NULL);
  1241. if (!con_cache)
  1242. goto out;
  1243. /* Set some sysctl minima */
  1244. if (sysctl_rmem_max < NEEDED_RMEM)
  1245. sysctl_rmem_max = NEEDED_RMEM;
  1246. /* Start listening */
  1247. if (dlm_config.ci_protocol == 0)
  1248. error = tcp_listen_for_all();
  1249. else
  1250. error = sctp_listen_for_all();
  1251. if (error)
  1252. goto fail_unlisten;
  1253. error = work_start();
  1254. if (error)
  1255. goto fail_unlisten;
  1256. return 0;
  1257. fail_unlisten:
  1258. con = nodeid2con(0,0);
  1259. if (con) {
  1260. close_connection(con, false);
  1261. kmem_cache_free(con_cache, con);
  1262. }
  1263. kmem_cache_destroy(con_cache);
  1264. out:
  1265. return error;
  1266. }