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 99a826d..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,
@@ -251,14 +271,14 @@ static int svc_one_sock_name(struct svc_sock *svsk, char *buf, int remaining)
        case PF_INET:
                len = snprintf(buf, remaining, "ipv4 %s %pI4 %d\n",
                                proto_name,
-                               &inet_sk(sk)->rcv_saddr,
-                               inet_sk(sk)->num);
+                               &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)->num);
+                               inet_sk(sk)->inet_num);
                break;
        default:
                len = snprintf(buf, remaining, "*unknown-%d*\n",
@@ -398,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));
 }
 
 /*
@@ -415,10 +435,10 @@ 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));
        }
 }
 
@@ -526,7 +546,6 @@ 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));
@@ -541,11 +560,6 @@ 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;
 
@@ -557,7 +571,7 @@ static int svc_udp_recvfrom(struct svc_rqst *rqstp)
                                "svc: received unknown control message %d/%d; "
                                "dropping RPC reply datagram\n",
                                        cmh->cmsg_level, cmh->cmsg_type);
-               skb_free_datagram(svsk->sk_sk, skb);
+               skb_free_datagram_locked(svsk->sk_sk, skb);
                return 0;
        }
 
@@ -567,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;
@@ -736,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));
 }
 
 /*
@@ -756,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)
@@ -770,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));
 }
 
 /*
@@ -854,21 +868,15 @@ 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;
-
-       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));
+       int len;
 
        if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
                /* sndbuf needs to have room for one request
@@ -889,10 +897,6 @@ static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
 
        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;
@@ -906,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);
@@ -922,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) {
@@ -941,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;
@@ -962,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;
@@ -977,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;
 }
 
 /*
@@ -1186,7 +1298,7 @@ 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_family, inet->sk_protocol,
-                                    ntohs(inet_sk(inet)->sport));
+                                    ntohs(inet_sk(inet)->inet_sport));
 
        if (*errp < 0) {
                kfree(svsk);
@@ -1232,7 +1344,7 @@ int svc_addsock(struct svc_serv *serv, const int fd, char *name_return,
 
        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)
@@ -1368,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));
 }
 
 /*