Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next-2.6
[safe/jmp/linux-2.6] / net / sunrpc / svcsock.c
index 7a2a90f..7e534dd 100644 (file)
@@ -49,6 +49,7 @@
 #include <linux/sunrpc/msg_prot.h>
 #include <linux/sunrpc/svcsock.h>
 #include <linux/sunrpc/stats.h>
+#include <linux/sunrpc/xprt.h>
 
 #define RPCDBG_FACILITY        RPCDBG_SVCXPRT
 
@@ -110,7 +111,7 @@ static void svc_release_skb(struct svc_rqst *rqstp)
                rqstp->rq_xprt_ctxt = NULL;
 
                dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
-               skb_free_datagram(svsk->sk_sk, skb);
+               skb_free_datagram_locked(svsk->sk_sk, skb);
        }
 }
 
@@ -149,53 +150,30 @@ static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
                }
                break;
        }
-       return;
 }
 
 /*
- * Generic sendto routine
+ * send routine intended to be shared by the fore- and back-channel
  */
-static int svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
+int svc_send_common(struct socket *sock, struct xdr_buf *xdr,
+                   struct page *headpage, unsigned long headoffset,
+                   struct page *tailpage, unsigned long tailoffset)
 {
-       struct svc_sock *svsk =
-               container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
-       struct socket   *sock = svsk->sk_sock;
-       int             slen;
-       union {
-               struct cmsghdr  hdr;
-               long            all[SVC_PKTINFO_SPACE / sizeof(long)];
-       } buffer;
-       struct cmsghdr *cmh = &buffer.hdr;
-       int             len = 0;
        int             result;
        int             size;
        struct page     **ppage = xdr->pages;
        size_t          base = xdr->page_base;
        unsigned int    pglen = xdr->page_len;
        unsigned int    flags = MSG_MORE;
-       RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
+       int             slen;
+       int             len = 0;
 
        slen = xdr->len;
 
-       if (rqstp->rq_prot == IPPROTO_UDP) {
-               struct msghdr msg = {
-                       .msg_name       = &rqstp->rq_addr,
-                       .msg_namelen    = rqstp->rq_addrlen,
-                       .msg_control    = cmh,
-                       .msg_controllen = sizeof(buffer),
-                       .msg_flags      = MSG_MORE,
-               };
-
-               svc_set_cmsg_data(rqstp, cmh);
-
-               if (sock_sendmsg(sock, &msg, 0) < 0)
-                       goto out;
-       }
-
        /* send head */
        if (slen == xdr->head[0].iov_len)
                flags = 0;
-       len = kernel_sendpage(sock, rqstp->rq_respages[0], 0,
+       len = kernel_sendpage(sock, headpage, headoffset,
                                  xdr->head[0].iov_len, flags);
        if (len != xdr->head[0].iov_len)
                goto out;
@@ -219,16 +197,58 @@ static int svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
                base = 0;
                ppage++;
        }
+
        /* send tail */
        if (xdr->tail[0].iov_len) {
-               result = kernel_sendpage(sock, rqstp->rq_respages[0],
-                                            ((unsigned long)xdr->tail[0].iov_base)
-                                               & (PAGE_SIZE-1),
-                                            xdr->tail[0].iov_len, 0);
-
+               result = kernel_sendpage(sock, tailpage, tailoffset,
+                                  xdr->tail[0].iov_len, 0);
                if (result > 0)
                        len += result;
        }
+
+out:
+       return len;
+}
+
+
+/*
+ * Generic sendto routine
+ */
+static int svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
+{
+       struct svc_sock *svsk =
+               container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
+       struct socket   *sock = svsk->sk_sock;
+       union {
+               struct cmsghdr  hdr;
+               long            all[SVC_PKTINFO_SPACE / sizeof(long)];
+       } buffer;
+       struct cmsghdr *cmh = &buffer.hdr;
+       int             len = 0;
+       unsigned long tailoff;
+       unsigned long headoff;
+       RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
+
+       if (rqstp->rq_prot == IPPROTO_UDP) {
+               struct msghdr msg = {
+                       .msg_name       = &rqstp->rq_addr,
+                       .msg_namelen    = rqstp->rq_addrlen,
+                       .msg_control    = cmh,
+                       .msg_controllen = sizeof(buffer),
+                       .msg_flags      = MSG_MORE,
+               };
+
+               svc_set_cmsg_data(rqstp, cmh);
+
+               if (sock_sendmsg(sock, &msg, 0) < 0)
+                       goto out;
+       }
+
+       tailoff = ((unsigned long)xdr->tail[0].iov_base) & (PAGE_SIZE-1);
+       headoff = 0;
+       len = svc_send_common(sock, xdr, rqstp->rq_respages[0], headoff,
+                              rqstp->rq_respages[0], tailoff);
+
 out:
        dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %s)\n",
                svsk, xdr->head[0].iov_base, xdr->head[0].iov_len,
@@ -240,42 +260,76 @@ out:
 /*
  * Report socket names for nfsdfs
  */
-static int one_sock_name(char *buf, struct svc_sock *svsk)
+static int svc_one_sock_name(struct svc_sock *svsk, char *buf, int remaining)
 {
+       const struct sock *sk = svsk->sk_sk;
+       const char *proto_name = sk->sk_protocol == IPPROTO_UDP ?
+                                                       "udp" : "tcp";
        int len;
 
-       switch(svsk->sk_sk->sk_family) {
-       case AF_INET:
-               len = sprintf(buf, "ipv4 %s %pI4 %d\n",
-                             svsk->sk_sk->sk_protocol == IPPROTO_UDP ?
-                             "udp" : "tcp",
-                             &inet_sk(svsk->sk_sk)->rcv_saddr,
-                             inet_sk(svsk->sk_sk)->num);
+       switch (sk->sk_family) {
+       case PF_INET:
+               len = snprintf(buf, remaining, "ipv4 %s %pI4 %d\n",
+                               proto_name,
+                               &inet_sk(sk)->inet_rcv_saddr,
+                               inet_sk(sk)->inet_num);
+               break;
+       case PF_INET6:
+               len = snprintf(buf, remaining, "ipv6 %s %pI6 %d\n",
+                               proto_name,
+                               &inet6_sk(sk)->rcv_saddr,
+                               inet_sk(sk)->inet_num);
                break;
        default:
-               len = sprintf(buf, "*unknown-%d*\n",
-                              svsk->sk_sk->sk_family);
+               len = snprintf(buf, remaining, "*unknown-%d*\n",
+                               sk->sk_family);
+       }
+
+       if (len >= remaining) {
+               *buf = '\0';
+               return -ENAMETOOLONG;
        }
        return len;
 }
 
-int
-svc_sock_names(char *buf, struct svc_serv *serv, char *toclose)
+/**
+ * svc_sock_names - construct a list of listener names in a string
+ * @serv: pointer to RPC service
+ * @buf: pointer to a buffer to fill in with socket names
+ * @buflen: size of the buffer to be filled
+ * @toclose: pointer to '\0'-terminated C string containing the name
+ *             of a listener to be closed
+ *
+ * Fills in @buf with a '\n'-separated list of names of listener
+ * sockets.  If @toclose is not NULL, the socket named by @toclose
+ * is closed, and is not included in the output list.
+ *
+ * Returns positive length of the socket name string, or a negative
+ * errno value on error.
+ */
+int svc_sock_names(struct svc_serv *serv, char *buf, const size_t buflen,
+                  const char *toclose)
 {
        struct svc_sock *svsk, *closesk = NULL;
        int len = 0;
 
        if (!serv)
                return 0;
+
        spin_lock_bh(&serv->sv_lock);
        list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list) {
-               int onelen = one_sock_name(buf+len, svsk);
-               if (toclose && strcmp(toclose, buf+len) == 0)
+               int onelen = svc_one_sock_name(svsk, buf + len, buflen - len);
+               if (onelen < 0) {
+                       len = onelen;
+                       break;
+               }
+               if (toclose && strcmp(toclose, buf + len) == 0)
                        closesk = svsk;
                else
                        len += onelen;
        }
        spin_unlock_bh(&serv->sv_lock);
+
        if (closesk)
                /* Should unregister with portmap, but you cannot
                 * unregister just one protocol...
@@ -345,6 +399,8 @@ static void svc_sock_setbufsize(struct socket *sock, unsigned int snd,
        lock_sock(sock->sk);
        sock->sk->sk_sndbuf = snd * 2;
        sock->sk->sk_rcvbuf = rcv * 2;
+       sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK|SOCK_RCVBUF_LOCK;
+       sock->sk->sk_write_space(sock->sk);
        release_sock(sock->sk);
 #endif
 }
@@ -362,8 +418,8 @@ static void svc_udp_data_ready(struct sock *sk, int count)
                set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
                svc_xprt_enqueue(&svsk->sk_xprt);
        }
-       if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
-               wake_up_interruptible(sk->sk_sleep);
+       if (sk_sleep(sk) && waitqueue_active(sk_sleep(sk)))
+               wake_up_interruptible(sk_sleep(sk));
 }
 
 /*
@@ -379,13 +435,48 @@ static void svc_write_space(struct sock *sk)
                svc_xprt_enqueue(&svsk->sk_xprt);
        }
 
-       if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) {
+       if (sk_sleep(sk) && waitqueue_active(sk_sleep(sk))) {
                dprintk("RPC svc_write_space: someone sleeping on %p\n",
                       svsk);
-               wake_up_interruptible(sk->sk_sleep);
+               wake_up_interruptible(sk_sleep(sk));
        }
 }
 
+static void svc_tcp_write_space(struct sock *sk)
+{
+       struct socket *sock = sk->sk_socket;
+
+       if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk) && sock)
+               clear_bit(SOCK_NOSPACE, &sock->flags);
+       svc_write_space(sk);
+}
+
+/*
+ * See net/ipv6/ip_sockglue.c : ip_cmsg_recv_pktinfo
+ */
+static int svc_udp_get_dest_address4(struct svc_rqst *rqstp,
+                                    struct cmsghdr *cmh)
+{
+       struct in_pktinfo *pki = CMSG_DATA(cmh);
+       if (cmh->cmsg_type != IP_PKTINFO)
+               return 0;
+       rqstp->rq_daddr.addr.s_addr = pki->ipi_spec_dst.s_addr;
+       return 1;
+}
+
+/*
+ * See net/ipv6/datagram.c : datagram_recv_ctl
+ */
+static int svc_udp_get_dest_address6(struct svc_rqst *rqstp,
+                                    struct cmsghdr *cmh)
+{
+       struct in6_pktinfo *pki = CMSG_DATA(cmh);
+       if (cmh->cmsg_type != IPV6_PKTINFO)
+               return 0;
+       ipv6_addr_copy(&rqstp->rq_daddr.addr6, &pki->ipi6_addr);
+       return 1;
+}
+
 /*
  * Copy the UDP datagram's destination address to the rqstp structure.
  * The 'destination' address in this case is the address to which the
@@ -393,23 +484,17 @@ static void svc_write_space(struct sock *sk)
  * hosts, this can change from msg to msg. Note that only the IP
  * address changes, the port number should remain the same.
  */
-static void svc_udp_get_dest_address(struct svc_rqst *rqstp,
-                                    struct cmsghdr *cmh)
+static int svc_udp_get_dest_address(struct svc_rqst *rqstp,
+                                   struct cmsghdr *cmh)
 {
-       struct svc_sock *svsk =
-               container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
-       switch (svsk->sk_sk->sk_family) {
-       case AF_INET: {
-               struct in_pktinfo *pki = CMSG_DATA(cmh);
-               rqstp->rq_daddr.addr.s_addr = pki->ipi_spec_dst.s_addr;
-               break;
-               }
-       case AF_INET6: {
-               struct in6_pktinfo *pki = CMSG_DATA(cmh);
-               ipv6_addr_copy(&rqstp->rq_daddr.addr6, &pki->ipi6_addr);
-               break;
-               }
+       switch (cmh->cmsg_level) {
+       case SOL_IP:
+               return svc_udp_get_dest_address4(rqstp, cmh);
+       case SOL_IPV6:
+               return svc_udp_get_dest_address6(rqstp, cmh);
        }
+
+       return 0;
 }
 
 /*
@@ -426,13 +511,14 @@ static int svc_udp_recvfrom(struct svc_rqst *rqstp)
                long            all[SVC_PKTINFO_SPACE / sizeof(long)];
        } buffer;
        struct cmsghdr *cmh = &buffer.hdr;
-       int             err, len;
        struct msghdr msg = {
                .msg_name = svc_addr(rqstp),
                .msg_control = cmh,
                .msg_controllen = sizeof(buffer),
                .msg_flags = MSG_DONTWAIT,
        };
+       size_t len;
+       int err;
 
        if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
            /* udp sockets need large rcvbuf as all pending
@@ -460,12 +546,11 @@ static int svc_udp_recvfrom(struct svc_rqst *rqstp)
                        dprintk("svc: recvfrom returned error %d\n", -err);
                        set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
                }
-               svc_xprt_received(&svsk->sk_xprt);
                return -EAGAIN;
        }
        len = svc_addr_len(svc_addr(rqstp));
-       if (len < 0)
-               return len;
+       if (len == 0)
+               return -EAFNOSUPPORT;
        rqstp->rq_addrlen = len;
        if (skb->tstamp.tv64 == 0) {
                skb->tstamp = ktime_get_real();
@@ -475,26 +560,20 @@ static int svc_udp_recvfrom(struct svc_rqst *rqstp)
        svsk->sk_sk->sk_stamp = skb->tstamp;
        set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
 
-       /*
-        * Maybe more packets - kick another thread ASAP.
-        */
-       svc_xprt_received(&svsk->sk_xprt);
-
        len  = skb->len - sizeof(struct udphdr);
        rqstp->rq_arg.len = len;
 
        rqstp->rq_prot = IPPROTO_UDP;
 
-       if (cmh->cmsg_level != IPPROTO_IP ||
-           cmh->cmsg_type != IP_PKTINFO) {
+       if (!svc_udp_get_dest_address(rqstp, cmh)) {
                if (net_ratelimit())
-                       printk("rpcsvc: received unknown control message:"
-                              "%d/%d\n",
-                              cmh->cmsg_level, cmh->cmsg_type);
-               skb_free_datagram(svsk->sk_sk, skb);
+                       printk(KERN_WARNING
+                               "svc: received unknown control message %d/%d; "
+                               "dropping RPC reply datagram\n",
+                                       cmh->cmsg_level, cmh->cmsg_type);
+               skb_free_datagram_locked(svsk->sk_sk, skb);
                return 0;
        }
-       svc_udp_get_dest_address(rqstp, cmh);
 
        if (skb_is_nonlinear(skb)) {
                /* we have to copy */
@@ -502,18 +581,18 @@ static int svc_udp_recvfrom(struct svc_rqst *rqstp)
                if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
                        local_bh_enable();
                        /* checksum error */
-                       skb_free_datagram(svsk->sk_sk, skb);
+                       skb_free_datagram_locked(svsk->sk_sk, skb);
                        return 0;
                }
                local_bh_enable();
-               skb_free_datagram(svsk->sk_sk, skb);
+               skb_free_datagram_locked(svsk->sk_sk, skb);
        } else {
                /* we can use it in-place */
                rqstp->rq_arg.head[0].iov_base = skb->data +
                        sizeof(struct udphdr);
                rqstp->rq_arg.head[0].iov_len = len;
                if (skb_checksum_complete(skb)) {
-                       skb_free_datagram(svsk->sk_sk, skb);
+                       skb_free_datagram_locked(svsk->sk_sk, skb);
                        return 0;
                }
                rqstp->rq_xprt_ctxt = skb;
@@ -605,8 +684,7 @@ static struct svc_xprt_class svc_udp_class = {
 
 static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
 {
-       int one = 1;
-       mm_segment_t oldfs;
+       int err, level, optname, one = 1;
 
        svc_xprt_init(&svc_udp_class, &svsk->sk_xprt, serv);
        clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
@@ -625,12 +703,22 @@ static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
        set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
        set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
 
-       oldfs = get_fs();
-       set_fs(KERNEL_DS);
        /* make sure we get destination address info */
-       svsk->sk_sock->ops->setsockopt(svsk->sk_sock, IPPROTO_IP, IP_PKTINFO,
-                                      (char __user *)&one, sizeof(one));
-       set_fs(oldfs);
+       switch (svsk->sk_sk->sk_family) {
+       case AF_INET:
+               level = SOL_IP;
+               optname = IP_PKTINFO;
+               break;
+       case AF_INET6:
+               level = SOL_IPV6;
+               optname = IPV6_RECVPKTINFO;
+               break;
+       default:
+               BUG();
+       }
+       err = kernel_setsockopt(svsk->sk_sock, level, optname,
+                                       (char *)&one, sizeof(one));
+       dprintk("svc: kernel_setsockopt returned %d\n", err);
 }
 
 /*
@@ -662,8 +750,8 @@ static void svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
                        printk("svc: socket %p: no user data\n", sk);
        }
 
-       if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
-               wake_up_interruptible_all(sk->sk_sleep);
+       if (sk_sleep(sk) && waitqueue_active(sk_sleep(sk)))
+               wake_up_interruptible_all(sk_sleep(sk));
 }
 
 /*
@@ -682,8 +770,8 @@ static void svc_tcp_state_change(struct sock *sk)
                set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
                svc_xprt_enqueue(&svsk->sk_xprt);
        }
-       if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
-               wake_up_interruptible_all(sk->sk_sleep);
+       if (sk_sleep(sk) && waitqueue_active(sk_sleep(sk)))
+               wake_up_interruptible_all(sk_sleep(sk));
 }
 
 static void svc_tcp_data_ready(struct sock *sk, int count)
@@ -696,8 +784,8 @@ static void svc_tcp_data_ready(struct sock *sk, int count)
                set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
                svc_xprt_enqueue(&svsk->sk_xprt);
        }
-       if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
-               wake_up_interruptible(sk->sk_sleep);
+       if (sk_sleep(sk) && waitqueue_active(sk_sleep(sk)))
+               wake_up_interruptible(sk_sleep(sk));
 }
 
 /*
@@ -780,28 +868,35 @@ failed:
 }
 
 /*
- * Receive data from a TCP socket.
+ * Receive data.
+ * If we haven't gotten the record length yet, get the next four bytes.
+ * Otherwise try to gobble up as much as possible up to the complete
+ * record length.
  */
-static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
+static int svc_tcp_recv_record(struct svc_sock *svsk, struct svc_rqst *rqstp)
 {
-       struct svc_sock *svsk =
-               container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
        struct svc_serv *serv = svsk->sk_xprt.xpt_server;
-       int             len;
-       struct kvec *vec;
-       int pnum, vlen;
+       int len;
 
-       dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
-               svsk, test_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags),
-               test_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags),
-               test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags));
+       if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
+               /* sndbuf needs to have room for one request
+                * per thread, otherwise we can stall even when the
+                * network isn't a bottleneck.
+                *
+                * We count all threads rather than threads in a
+                * particular pool, which provides an upper bound
+                * on the number of threads which will access the socket.
+                *
+                * rcvbuf just needs to be able to hold a few requests.
+                * Normally they will be removed from the queue
+                * as soon a a complete request arrives.
+                */
+               svc_sock_setbufsize(svsk->sk_sock,
+                                   (serv->sv_nrthreads+3) * serv->sv_max_mesg,
+                                   3 * serv->sv_max_mesg);
 
        clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
 
-       /* Receive data. If we haven't got the record length yet, get
-        * the next four bytes. Otherwise try to gobble up as much as
-        * possible up to the complete record length.
-        */
        if (svsk->sk_tcplen < sizeof(rpc_fraghdr)) {
                int             want = sizeof(rpc_fraghdr) - svsk->sk_tcplen;
                struct kvec     iov;
@@ -815,8 +910,7 @@ static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
                if (len < want) {
                        dprintk("svc: short recvfrom while reading record "
                                "length (%d of %d)\n", len, want);
-                       svc_xprt_received(&svsk->sk_xprt);
-                       return -EAGAIN; /* record header not complete */
+                       goto err_again; /* record header not complete */
                }
 
                svsk->sk_reclen = ntohl(svsk->sk_reclen);
@@ -831,6 +925,7 @@ static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
                                        "per record not supported\n");
                        goto err_delete;
                }
+
                svsk->sk_reclen &= RPC_FRAGMENT_SIZE_MASK;
                dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
                if (svsk->sk_reclen > serv->sv_max_mesg) {
@@ -850,18 +945,119 @@ static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
        if (len < svsk->sk_reclen) {
                dprintk("svc: incomplete TCP record (%d of %d)\n",
                        len, svsk->sk_reclen);
-               svc_xprt_received(&svsk->sk_xprt);
-               return -EAGAIN; /* record not complete */
+               goto err_again; /* record not complete */
        }
        len = svsk->sk_reclen;
        set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
 
+       return len;
+ error:
+       if (len == -EAGAIN)
+               dprintk("RPC: TCP recv_record got EAGAIN\n");
+       return len;
+ err_delete:
+       set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
+ err_again:
+       return -EAGAIN;
+}
+
+static int svc_process_calldir(struct svc_sock *svsk, struct svc_rqst *rqstp,
+                              struct rpc_rqst **reqpp, struct kvec *vec)
+{
+       struct rpc_rqst *req = NULL;
+       u32 *p;
+       u32 xid;
+       u32 calldir;
+       int len;
+
+       len = svc_recvfrom(rqstp, vec, 1, 8);
+       if (len < 0)
+               goto error;
+
+       p = (u32 *)rqstp->rq_arg.head[0].iov_base;
+       xid = *p++;
+       calldir = *p;
+
+       if (calldir == 0) {
+               /* REQUEST is the most common case */
+               vec[0] = rqstp->rq_arg.head[0];
+       } else {
+               /* REPLY */
+               if (svsk->sk_bc_xprt)
+                       req = xprt_lookup_rqst(svsk->sk_bc_xprt, xid);
+
+               if (!req) {
+                       printk(KERN_NOTICE
+                               "%s: Got unrecognized reply: "
+                               "calldir 0x%x sk_bc_xprt %p xid %08x\n",
+                               __func__, ntohl(calldir),
+                               svsk->sk_bc_xprt, xid);
+                       vec[0] = rqstp->rq_arg.head[0];
+                       goto out;
+               }
+
+               memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
+                      sizeof(struct xdr_buf));
+               /* copy the xid and call direction */
+               memcpy(req->rq_private_buf.head[0].iov_base,
+                      rqstp->rq_arg.head[0].iov_base, 8);
+               vec[0] = req->rq_private_buf.head[0];
+       }
+ out:
+       vec[0].iov_base += 8;
+       vec[0].iov_len -= 8;
+       len = svsk->sk_reclen - 8;
+ error:
+       *reqpp = req;
+       return len;
+}
+
+/*
+ * Receive data from a TCP socket.
+ */
+static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
+{
+       struct svc_sock *svsk =
+               container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
+       struct svc_serv *serv = svsk->sk_xprt.xpt_server;
+       int             len;
+       struct kvec *vec;
+       int pnum, vlen;
+       struct rpc_rqst *req = NULL;
+
+       dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
+               svsk, test_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags),
+               test_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags),
+               test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags));
+
+       len = svc_tcp_recv_record(svsk, rqstp);
+       if (len < 0)
+               goto error;
+
        vec = rqstp->rq_vec;
        vec[0] = rqstp->rq_arg.head[0];
        vlen = PAGE_SIZE;
+
+       /*
+        * We have enough data for the whole tcp record. Let's try and read the
+        * first 8 bytes to get the xid and the call direction. We can use this
+        * to figure out if this is a call or a reply to a callback. If
+        * sk_reclen is < 8 (xid and calldir), then this is a malformed packet.
+        * In that case, don't bother with the calldir and just read the data.
+        * It will be rejected in svc_process.
+        */
+       if (len >= 8) {
+               len = svc_process_calldir(svsk, rqstp, &req, vec);
+               if (len < 0)
+                       goto err_again;
+               vlen -= 8;
+       }
+
        pnum = 1;
        while (vlen < len) {
-               vec[pnum].iov_base = page_address(rqstp->rq_pages[pnum]);
+               vec[pnum].iov_base = (req) ?
+                       page_address(req->rq_private_buf.pages[pnum - 1]) :
+                       page_address(rqstp->rq_pages[pnum]);
                vec[pnum].iov_len = PAGE_SIZE;
                pnum++;
                vlen += PAGE_SIZE;
@@ -871,8 +1067,18 @@ static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
        /* Now receive data */
        len = svc_recvfrom(rqstp, vec, pnum, len);
        if (len < 0)
-               goto error;
+               goto err_again;
 
+       /*
+        * Account for the 8 bytes we read earlier
+        */
+       len += 8;
+
+       if (req) {
+               xprt_complete_rqst(req->rq_task, len);
+               len = 0;
+               goto out;
+       }
        dprintk("svc: TCP complete record (%d bytes)\n", len);
        rqstp->rq_arg.len = len;
        rqstp->rq_arg.page_base = 0;
@@ -886,32 +1092,29 @@ static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
        rqstp->rq_xprt_ctxt   = NULL;
        rqstp->rq_prot        = IPPROTO_TCP;
 
+out:
        /* Reset TCP read info */
        svsk->sk_reclen = 0;
        svsk->sk_tcplen = 0;
 
        svc_xprt_copy_addrs(rqstp, &svsk->sk_xprt);
-       svc_xprt_received(&svsk->sk_xprt);
        if (serv->sv_stats)
                serv->sv_stats->nettcpcnt++;
 
        return len;
 
- err_delete:
-       set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
-       return -EAGAIN;
-
- error:
+err_again:
        if (len == -EAGAIN) {
                dprintk("RPC: TCP recvfrom got EAGAIN\n");
-               svc_xprt_received(&svsk->sk_xprt);
-       } else {
+               return len;
+       }
+error:
+       if (len != -EAGAIN) {
                printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
                       svsk->sk_xprt.xpt_server->sv_name, -len);
-               goto err_delete;
+               set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
        }
-
-       return len;
+       return -EAGAIN;
 }
 
 /*
@@ -962,25 +1165,16 @@ static void svc_tcp_prep_reply_hdr(struct svc_rqst *rqstp)
 static int svc_tcp_has_wspace(struct svc_xprt *xprt)
 {
        struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
-       struct svc_serv *serv = svsk->sk_xprt.xpt_server;
+       struct svc_serv *serv = svsk->sk_xprt.xpt_server;
        int required;
-       int wspace;
 
-       /*
-        * Set the SOCK_NOSPACE flag before checking the available
-        * sock space.
-        */
+       if (test_bit(XPT_LISTENER, &xprt->xpt_flags))
+               return 1;
+       required = atomic_read(&xprt->xpt_reserved) + serv->sv_max_mesg;
+       if (sk_stream_wspace(svsk->sk_sk) >= required)
+               return 1;
        set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
-       required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
-       wspace = sk_stream_wspace(svsk->sk_sk);
-
-       if (wspace < sk_stream_min_wspace(svsk->sk_sk))
-               return 0;
-       if (required * 2 > wspace)
-               return 0;
-
-       clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
-       return 1;
+       return 0;
 }
 
 static struct svc_xprt *svc_tcp_create(struct svc_serv *serv,
@@ -1036,13 +1230,22 @@ static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
                dprintk("setting up TCP socket for reading\n");
                sk->sk_state_change = svc_tcp_state_change;
                sk->sk_data_ready = svc_tcp_data_ready;
-               sk->sk_write_space = svc_write_space;
+               sk->sk_write_space = svc_tcp_write_space;
 
                svsk->sk_reclen = 0;
                svsk->sk_tcplen = 0;
 
                tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
 
+               /* initialise setting must have enough space to
+                * receive and respond to one request.
+                * svc_tcp_recvfrom will re-adjust if necessary
+                */
+               svc_sock_setbufsize(svsk->sk_sock,
+                                   3 * svsk->sk_xprt.xpt_server->sv_max_mesg,
+                                   3 * svsk->sk_xprt.xpt_server->sv_max_mesg);
+
+               set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
                set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
                if (sk->sk_state != TCP_ESTABLISHED)
                        set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
@@ -1083,7 +1286,6 @@ static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
        struct svc_sock *svsk;
        struct sock     *inet;
        int             pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
-       int             val;
 
        dprintk("svc: svc_setup_socket %p\n", sock);
        if (!(svsk = kzalloc(sizeof(*svsk), GFP_KERNEL))) {
@@ -1095,8 +1297,8 @@ static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
 
        /* Register socket with portmapper */
        if (*errp >= 0 && pmap_register)
-               *errp = svc_register(serv, inet->sk_protocol,
-                                    ntohs(inet_sk(inet)->sport));
+               *errp = svc_register(serv, inet->sk_family, inet->sk_protocol,
+                                    ntohs(inet_sk(inet)->inet_sport));
 
        if (*errp < 0) {
                kfree(svsk);
@@ -1113,26 +1315,8 @@ static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
        /* Initialize the socket */
        if (sock->type == SOCK_DGRAM)
                svc_udp_init(svsk, serv);
-       else {
-               /* initialise setting must have enough space to
-                * receive and respond to one request.
-                */
-               svc_sock_setbufsize(svsk->sk_sock, 4 * serv->sv_max_mesg,
-                                       4 * serv->sv_max_mesg);
+       else
                svc_tcp_init(svsk, serv);
-       }
-
-       /*
-        * We start one listener per sv_serv.  We want AF_INET
-        * requests to be automatically shunted to our AF_INET6
-        * listener using a mapped IPv4 address.  Make sure
-        * no-one starts an equivalent IPv4 listener, which
-        * would steal our incoming connections.
-        */
-       val = 0;
-       if (serv->sv_family == AF_INET6)
-               kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
-                                       (char *)&val, sizeof(val));
 
        dprintk("svc: svc_setup_socket created %p (inet %p)\n",
                                svsk, svsk->sk_sk);
@@ -1140,9 +1324,19 @@ static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
        return svsk;
 }
 
-int svc_addsock(struct svc_serv *serv,
-               int fd,
-               char *name_return)
+/**
+ * svc_addsock - add a listener socket to an RPC service
+ * @serv: pointer to RPC service to which to add a new listener
+ * @fd: file descriptor of the new listener
+ * @name_return: pointer to buffer to fill in with name of listener
+ * @len: size of the buffer
+ *
+ * Fills in socket name and returns positive length of name if successful.
+ * Name is terminated with '\n'.  On error, returns a negative errno
+ * value.
+ */
+int svc_addsock(struct svc_serv *serv, const int fd, char *name_return,
+               const size_t len)
 {
        int err = 0;
        struct socket *so = sockfd_lookup(fd, &err);
@@ -1150,7 +1344,7 @@ int svc_addsock(struct svc_serv *serv,
 
        if (!so)
                return err;
-       if (so->sk->sk_family != AF_INET)
+       if ((so->sk->sk_family != PF_INET) && (so->sk->sk_family != PF_INET6))
                err =  -EAFNOSUPPORT;
        else if (so->sk->sk_protocol != IPPROTO_TCP &&
            so->sk->sk_protocol != IPPROTO_UDP)
@@ -1182,7 +1376,7 @@ int svc_addsock(struct svc_serv *serv,
                sockfd_put(so);
                return err;
        }
-       return one_sock_name(name_return, svsk);
+       return svc_one_sock_name(svsk, name_return, len);
 }
 EXPORT_SYMBOL_GPL(svc_addsock);
 
@@ -1201,6 +1395,8 @@ static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
        struct sockaddr_storage addr;
        struct sockaddr *newsin = (struct sockaddr *)&addr;
        int             newlen;
+       int             family;
+       int             val;
        RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
 
        dprintk("svc: svc_create_socket(%s, %d, %s)\n",
@@ -1212,14 +1408,35 @@ static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
                                "sockets supported\n");
                return ERR_PTR(-EINVAL);
        }
+
        type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
+       switch (sin->sa_family) {
+       case AF_INET6:
+               family = PF_INET6;
+               break;
+       case AF_INET:
+               family = PF_INET;
+               break;
+       default:
+               return ERR_PTR(-EINVAL);
+       }
 
-       error = sock_create_kern(sin->sa_family, type, protocol, &sock);
+       error = sock_create_kern(family, type, protocol, &sock);
        if (error < 0)
                return ERR_PTR(error);
 
        svc_reclassify_socket(sock);
 
+       /*
+        * If this is an PF_INET6 listener, we want to avoid
+        * getting requests from IPv4 remotes.  Those should
+        * be shunted to a PF_INET listener via rpcbind.
+        */
+       val = 1;
+       if (family == PF_INET6)
+               kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
+                                       (char *)&val, sizeof(val));
+
        if (type == SOCK_STREAM)
                sock->sk->sk_reuse = 1;         /* allow address reuse */
        error = kernel_bind(sock, sin, len);
@@ -1263,8 +1480,8 @@ static void svc_sock_detach(struct svc_xprt *xprt)
        sk->sk_data_ready = svsk->sk_odata;
        sk->sk_write_space = svsk->sk_owspace;
 
-       if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
-               wake_up_interruptible(sk->sk_sleep);
+       if (sk_sleep(sk) && waitqueue_active(sk_sleep(sk)))
+               wake_up_interruptible(sk_sleep(sk));
 }
 
 /*
@@ -1296,3 +1513,42 @@ static void svc_sock_free(struct svc_xprt *xprt)
                sock_release(svsk->sk_sock);
        kfree(svsk);
 }
+
+/*
+ * Create a svc_xprt.
+ *
+ * For internal use only (e.g. nfsv4.1 backchannel).
+ * Callers should typically use the xpo_create() method.
+ */
+struct svc_xprt *svc_sock_create(struct svc_serv *serv, int prot)
+{
+       struct svc_sock *svsk;
+       struct svc_xprt *xprt = NULL;
+
+       dprintk("svc: %s\n", __func__);
+       svsk = kzalloc(sizeof(*svsk), GFP_KERNEL);
+       if (!svsk)
+               goto out;
+
+       xprt = &svsk->sk_xprt;
+       if (prot == IPPROTO_TCP)
+               svc_xprt_init(&svc_tcp_class, xprt, serv);
+       else if (prot == IPPROTO_UDP)
+               svc_xprt_init(&svc_udp_class, xprt, serv);
+       else
+               BUG();
+out:
+       dprintk("svc: %s return %p\n", __func__, xprt);
+       return xprt;
+}
+EXPORT_SYMBOL_GPL(svc_sock_create);
+
+/*
+ * Destroy a svc_sock.
+ */
+void svc_sock_destroy(struct svc_xprt *xprt)
+{
+       if (xprt)
+               kfree(container_of(xprt, struct svc_sock, sk_xprt));
+}
+EXPORT_SYMBOL_GPL(svc_sock_destroy);