lowcomms.c 39 KB

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