lowcomms.c 37 KB

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