lowcomms.c 36 KB

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