lowcomms.c 38 KB

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