lowcomms.c 36 KB

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