dlm: initialize file_lock struct in GETLK before copying conflicting lock
[safe/jmp/linux-2.6] / fs / dlm / lowcomms.c
index 9e9d2e8..103a5eb 100644 (file)
@@ -50,6 +50,7 @@
 #include <linux/pagemap.h>
 #include <linux/idr.h>
 #include <linux/file.h>
+#include <linux/mutex.h>
 #include <linux/sctp.h>
 #include <net/sctp/user.h>
 
@@ -138,7 +139,7 @@ static struct workqueue_struct *recv_workqueue;
 static struct workqueue_struct *send_workqueue;
 
 static DEFINE_IDR(connections_idr);
-static DECLARE_MUTEX(connections_lock);
+static DEFINE_MUTEX(connections_lock);
 static int max_nodeid;
 static struct kmem_cache *con_cache;
 
@@ -205,9 +206,9 @@ static struct connection *nodeid2con(int nodeid, gfp_t allocation)
 {
        struct connection *con;
 
-       down(&connections_lock);
+       mutex_lock(&connections_lock);
        con = __nodeid2con(nodeid, allocation);
-       up(&connections_lock);
+       mutex_unlock(&connections_lock);
 
        return con;
 }
@@ -218,15 +219,15 @@ static struct connection *assoc2con(int assoc_id)
        int i;
        struct connection *con;
 
-       down(&connections_lock);
+       mutex_lock(&connections_lock);
        for (i=0; i<=max_nodeid; i++) {
                con = __nodeid2con(i, 0);
                if (con && con->sctp_assoc == assoc_id) {
-                       up(&connections_lock);
+                       mutex_unlock(&connections_lock);
                        return con;
                }
        }
-       up(&connections_lock);
+       mutex_unlock(&connections_lock);
        return NULL;
 }
 
@@ -294,6 +295,7 @@ static int add_sock(struct socket *sock, struct connection *con)
        con->sock->sk->sk_write_space = lowcomms_write_space;
        con->sock->sk->sk_state_change = lowcomms_state_change;
        con->sock->sk->sk_user_data = con;
+       con->sock->sk->sk_allocation = GFP_NOFS;
        return 0;
 }
 
@@ -334,18 +336,8 @@ static void close_connection(struct connection *con, bool and_other)
                con->rx_page = NULL;
        }
 
-       /* If we are an 'othercon' then NULL the pointer to us
-          from the parent and tidy ourself up */
-       if (test_bit(CF_IS_OTHERCON, &con->flags)) {
-               struct connection *parent = __nodeid2con(con->nodeid, 0);
-               parent->othercon = NULL;
-               kmem_cache_free(con_cache, con);
-       }
-       else {
-               /* Parent connections get reused */
-               con->retries = 0;
-               mutex_unlock(&con->sock_mutex);
-       }
+       con->retries = 0;
+       mutex_unlock(&con->sock_mutex);
 }
 
 /* We only send shutdown messages to nodes that are not part of the cluster */
@@ -391,7 +383,7 @@ static void sctp_init_failed(void)
        int i;
        struct connection *con;
 
-       down(&connections_lock);
+       mutex_lock(&connections_lock);
        for (i=1; i<=max_nodeid; i++) {
                con = __nodeid2con(i, 0);
                if (!con)
@@ -403,7 +395,7 @@ static void sctp_init_failed(void)
                        }
                }
        }
-       up(&connections_lock);
+       mutex_unlock(&connections_lock);
 }
 
 /* Something happened to an association */
@@ -731,6 +723,8 @@ static int tcp_accept_from_sock(struct connection *con)
                        INIT_WORK(&othercon->swork, process_send_sockets);
                        INIT_WORK(&othercon->rwork, process_recv_sockets);
                        set_bit(CF_IS_OTHERCON, &othercon->flags);
+               }
+               if (!othercon->sock) {
                        newcon->othercon = othercon;
                        othercon->sock = newsock;
                        newsock->sk->sk_user_data = othercon;
@@ -830,7 +824,6 @@ static void sctp_init_assoc(struct connection *con)
        len = e->len;
        offset = e->offset;
        spin_unlock(&con->writequeue_lock);
-       kmap(e->page);
 
        /* Send the first block off the write queue */
        iov[0].iov_base = page_address(e->page)+offset;
@@ -861,7 +854,6 @@ static void sctp_init_assoc(struct connection *con)
 
                if (e->len == 0 && e->users == 0) {
                        list_del(&e->list);
-                       kunmap(e->page);
                        free_entry(e);
                }
                spin_unlock(&con->writequeue_lock);
@@ -872,7 +864,7 @@ static void sctp_init_assoc(struct connection *con)
 static void tcp_connect_to_sock(struct connection *con)
 {
        int result = -EHOSTUNREACH;
-       struct sockaddr_storage saddr;
+       struct sockaddr_storage saddr, src_addr;
        int addr_len;
        struct socket *sock;
 
@@ -898,14 +890,27 @@ static void tcp_connect_to_sock(struct connection *con)
                goto out_err;
 
        memset(&saddr, 0, sizeof(saddr));
-       if (dlm_nodeid_to_addr(con->nodeid, &saddr))
+       if (dlm_nodeid_to_addr(con->nodeid, &saddr)) {
+               sock_release(sock);
                goto out_err;
+       }
 
        sock->sk->sk_user_data = con;
        con->rx_action = receive_from_sock;
        con->connect_action = tcp_connect_to_sock;
        add_sock(sock, con);
 
+       /* Bind to our cluster-known address connecting to avoid
+          routing problems */
+       memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr));
+       make_sockaddr(&src_addr, 0, &addr_len);
+       result = sock->ops->bind(sock, (struct sockaddr *) &src_addr,
+                                addr_len);
+       if (result < 0) {
+               log_print("could not bind for connect: %d", result);
+               /* This *may* not indicate a critical error */
+       }
+
        make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
 
        log_print("connecting to %d", con->nodeid);
@@ -927,7 +932,7 @@ out_err:
         * errors we try again until the max number of retries is reached.
         */
        if (result != -EHOSTUNREACH && result != -ENETUNREACH &&
-           result != -ENETDOWN && result != EINVAL
+           result != -ENETDOWN && result != -EINVAL
            && result != -EPROTONOSUPPORT) {
                lowcomms_connect_sock(con);
                result = 0;
@@ -1070,7 +1075,7 @@ static int sctp_listen_for_all(void)
        subscribe.sctp_shutdown_event = 1;
        subscribe.sctp_partial_delivery_event = 1;
 
-       result = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
+       result = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUFFORCE,
                                 (char *)&bufsize, sizeof(bufsize));
        if (result)
                log_print("Error increasing buffer space on socket %d", result);
@@ -1197,8 +1202,6 @@ void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc)
 
        if (e) {
        got_one:
-               if (users == 0)
-                       kmap(e->page);
                *ppc = page_address(e->page) + offset;
                return e;
        }
@@ -1227,7 +1230,6 @@ void dlm_lowcomms_commit_buffer(void *mh)
        if (users)
                goto out;
        e->len = e->end - e->offset;
-       kunmap(e->page);
        spin_unlock(&con->writequeue_lock);
 
        if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
@@ -1266,20 +1268,20 @@ static void send_to_sock(struct connection *con)
                offset = e->offset;
                BUG_ON(len == 0 && e->users == 0);
                spin_unlock(&con->writequeue_lock);
-               kmap(e->page);
 
                ret = 0;
                if (len) {
                        ret = sendpage(con->sock, e->page, offset, len,
                                       msg_flags);
-                       if (ret == -EAGAIN || ret == 0)
+                       if (ret == -EAGAIN || ret == 0) {
+                               cond_resched();
                                goto out;
+                       }
                        if (ret <= 0)
                                goto send_error;
-               } else {
+               }
                        /* Don't starve people filling buffers */
                        cond_resched();
-               }
 
                spin_lock(&con->writequeue_lock);
                e->offset += ret;
@@ -1287,7 +1289,6 @@ static void send_to_sock(struct connection *con)
 
                if (e->len == 0 && e->users == 0) {
                        list_del(&e->list);
-                       kunmap(e->page);
                        free_entry(e);
                        continue;
                }
@@ -1413,7 +1414,7 @@ void dlm_lowcomms_stop(void)
        /* Set all the flags to prevent any
           socket activity.
        */
-       down(&connections_lock);
+       mutex_lock(&connections_lock);
        for (i = 0; i <= max_nodeid; i++) {
                con = __nodeid2con(i, 0);
                if (con) {
@@ -1422,22 +1423,24 @@ void dlm_lowcomms_stop(void)
                                con->sock->sk->sk_user_data = NULL;
                }
        }
-       up(&connections_lock);
+       mutex_unlock(&connections_lock);
 
        work_stop();
 
-       down(&connections_lock);
+       mutex_lock(&connections_lock);
        clean_writequeues();
 
        for (i = 0; i <= max_nodeid; i++) {
                con = __nodeid2con(i, 0);
                if (con) {
                        close_connection(con, true);
+                       if (con->othercon)
+                               kmem_cache_free(con_cache, con->othercon);
                        kmem_cache_free(con_cache, con);
                }
        }
        max_nodeid = 0;
-       up(&connections_lock);
+       mutex_unlock(&connections_lock);
        kmem_cache_destroy(con_cache);
        idr_init(&connections_idr);
 }
@@ -1461,10 +1464,6 @@ int dlm_lowcomms_start(void)
        if (!con_cache)
                goto out;
 
-       /* Set some sysctl minima */
-       if (sysctl_rmem_max < NEEDED_RMEM)
-               sysctl_rmem_max = NEEDED_RMEM;
-
        /* Start listening */
        if (dlm_config.ci_protocol == 0)
                error = tcp_listen_for_all();