Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * linux/net/sunrpc/svcsock.c
0004  *
0005  * These are the RPC server socket internals.
0006  *
0007  * The server scheduling algorithm does not always distribute the load
0008  * evenly when servicing a single client. May need to modify the
0009  * svc_xprt_enqueue procedure...
0010  *
0011  * TCP support is largely untested and may be a little slow. The problem
0012  * is that we currently do two separate recvfrom's, one for the 4-byte
0013  * record length, and the second for the actual record. This could possibly
0014  * be improved by always reading a minimum size of around 100 bytes and
0015  * tucking any superfluous bytes away in a temporary store. Still, that
0016  * leaves write requests out in the rain. An alternative may be to peek at
0017  * the first skb in the queue, and if it matches the next TCP sequence
0018  * number, to extract the record marker. Yuck.
0019  *
0020  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
0021  */
0022 
0023 #include <linux/kernel.h>
0024 #include <linux/sched.h>
0025 #include <linux/module.h>
0026 #include <linux/errno.h>
0027 #include <linux/fcntl.h>
0028 #include <linux/net.h>
0029 #include <linux/in.h>
0030 #include <linux/inet.h>
0031 #include <linux/udp.h>
0032 #include <linux/tcp.h>
0033 #include <linux/unistd.h>
0034 #include <linux/slab.h>
0035 #include <linux/netdevice.h>
0036 #include <linux/skbuff.h>
0037 #include <linux/file.h>
0038 #include <linux/freezer.h>
0039 #include <net/sock.h>
0040 #include <net/checksum.h>
0041 #include <net/ip.h>
0042 #include <net/ipv6.h>
0043 #include <net/udp.h>
0044 #include <net/tcp.h>
0045 #include <net/tcp_states.h>
0046 #include <linux/uaccess.h>
0047 #include <linux/highmem.h>
0048 #include <asm/ioctls.h>
0049 
0050 #include <linux/sunrpc/types.h>
0051 #include <linux/sunrpc/clnt.h>
0052 #include <linux/sunrpc/xdr.h>
0053 #include <linux/sunrpc/msg_prot.h>
0054 #include <linux/sunrpc/svcsock.h>
0055 #include <linux/sunrpc/stats.h>
0056 #include <linux/sunrpc/xprt.h>
0057 
0058 #include <trace/events/sunrpc.h>
0059 
0060 #include "socklib.h"
0061 #include "sunrpc.h"
0062 
0063 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
0064 
0065 
0066 static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
0067                      int flags);
0068 static int      svc_udp_recvfrom(struct svc_rqst *);
0069 static int      svc_udp_sendto(struct svc_rqst *);
0070 static void     svc_sock_detach(struct svc_xprt *);
0071 static void     svc_tcp_sock_detach(struct svc_xprt *);
0072 static void     svc_sock_free(struct svc_xprt *);
0073 
0074 static struct svc_xprt *svc_create_socket(struct svc_serv *, int,
0075                       struct net *, struct sockaddr *,
0076                       int, int);
0077 #ifdef CONFIG_DEBUG_LOCK_ALLOC
0078 static struct lock_class_key svc_key[2];
0079 static struct lock_class_key svc_slock_key[2];
0080 
0081 static void svc_reclassify_socket(struct socket *sock)
0082 {
0083     struct sock *sk = sock->sk;
0084 
0085     if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
0086         return;
0087 
0088     switch (sk->sk_family) {
0089     case AF_INET:
0090         sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
0091                           &svc_slock_key[0],
0092                           "sk_xprt.xpt_lock-AF_INET-NFSD",
0093                           &svc_key[0]);
0094         break;
0095 
0096     case AF_INET6:
0097         sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
0098                           &svc_slock_key[1],
0099                           "sk_xprt.xpt_lock-AF_INET6-NFSD",
0100                           &svc_key[1]);
0101         break;
0102 
0103     default:
0104         BUG();
0105     }
0106 }
0107 #else
0108 static void svc_reclassify_socket(struct socket *sock)
0109 {
0110 }
0111 #endif
0112 
0113 /**
0114  * svc_tcp_release_rqst - Release transport-related resources
0115  * @rqstp: request structure with resources to be released
0116  *
0117  */
0118 static void svc_tcp_release_rqst(struct svc_rqst *rqstp)
0119 {
0120 }
0121 
0122 /**
0123  * svc_udp_release_rqst - Release transport-related resources
0124  * @rqstp: request structure with resources to be released
0125  *
0126  */
0127 static void svc_udp_release_rqst(struct svc_rqst *rqstp)
0128 {
0129     struct sk_buff *skb = rqstp->rq_xprt_ctxt;
0130 
0131     if (skb) {
0132         rqstp->rq_xprt_ctxt = NULL;
0133         consume_skb(skb);
0134     }
0135 }
0136 
0137 union svc_pktinfo_u {
0138     struct in_pktinfo pkti;
0139     struct in6_pktinfo pkti6;
0140 };
0141 #define SVC_PKTINFO_SPACE \
0142     CMSG_SPACE(sizeof(union svc_pktinfo_u))
0143 
0144 static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
0145 {
0146     struct svc_sock *svsk =
0147         container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
0148     switch (svsk->sk_sk->sk_family) {
0149     case AF_INET: {
0150             struct in_pktinfo *pki = CMSG_DATA(cmh);
0151 
0152             cmh->cmsg_level = SOL_IP;
0153             cmh->cmsg_type = IP_PKTINFO;
0154             pki->ipi_ifindex = 0;
0155             pki->ipi_spec_dst.s_addr =
0156                  svc_daddr_in(rqstp)->sin_addr.s_addr;
0157             cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
0158         }
0159         break;
0160 
0161     case AF_INET6: {
0162             struct in6_pktinfo *pki = CMSG_DATA(cmh);
0163             struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
0164 
0165             cmh->cmsg_level = SOL_IPV6;
0166             cmh->cmsg_type = IPV6_PKTINFO;
0167             pki->ipi6_ifindex = daddr->sin6_scope_id;
0168             pki->ipi6_addr = daddr->sin6_addr;
0169             cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
0170         }
0171         break;
0172     }
0173 }
0174 
0175 static int svc_sock_result_payload(struct svc_rqst *rqstp, unsigned int offset,
0176                    unsigned int length)
0177 {
0178     return 0;
0179 }
0180 
0181 /*
0182  * Report socket names for nfsdfs
0183  */
0184 static int svc_one_sock_name(struct svc_sock *svsk, char *buf, int remaining)
0185 {
0186     const struct sock *sk = svsk->sk_sk;
0187     const char *proto_name = sk->sk_protocol == IPPROTO_UDP ?
0188                             "udp" : "tcp";
0189     int len;
0190 
0191     switch (sk->sk_family) {
0192     case PF_INET:
0193         len = snprintf(buf, remaining, "ipv4 %s %pI4 %d\n",
0194                 proto_name,
0195                 &inet_sk(sk)->inet_rcv_saddr,
0196                 inet_sk(sk)->inet_num);
0197         break;
0198 #if IS_ENABLED(CONFIG_IPV6)
0199     case PF_INET6:
0200         len = snprintf(buf, remaining, "ipv6 %s %pI6 %d\n",
0201                 proto_name,
0202                 &sk->sk_v6_rcv_saddr,
0203                 inet_sk(sk)->inet_num);
0204         break;
0205 #endif
0206     default:
0207         len = snprintf(buf, remaining, "*unknown-%d*\n",
0208                 sk->sk_family);
0209     }
0210 
0211     if (len >= remaining) {
0212         *buf = '\0';
0213         return -ENAMETOOLONG;
0214     }
0215     return len;
0216 }
0217 
0218 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
0219 static void svc_flush_bvec(const struct bio_vec *bvec, size_t size, size_t seek)
0220 {
0221     struct bvec_iter bi = {
0222         .bi_size    = size + seek,
0223     };
0224     struct bio_vec bv;
0225 
0226     bvec_iter_advance(bvec, &bi, seek & PAGE_MASK);
0227     for_each_bvec(bv, bvec, bi, bi)
0228         flush_dcache_page(bv.bv_page);
0229 }
0230 #else
0231 static inline void svc_flush_bvec(const struct bio_vec *bvec, size_t size,
0232                   size_t seek)
0233 {
0234 }
0235 #endif
0236 
0237 /*
0238  * Read from @rqstp's transport socket. The incoming message fills whole
0239  * pages in @rqstp's rq_pages array until the last page of the message
0240  * has been received into a partial page.
0241  */
0242 static ssize_t svc_tcp_read_msg(struct svc_rqst *rqstp, size_t buflen,
0243                 size_t seek)
0244 {
0245     struct svc_sock *svsk =
0246         container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
0247     struct bio_vec *bvec = rqstp->rq_bvec;
0248     struct msghdr msg = { NULL };
0249     unsigned int i;
0250     ssize_t len;
0251     size_t t;
0252 
0253     clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
0254 
0255     for (i = 0, t = 0; t < buflen; i++, t += PAGE_SIZE) {
0256         bvec[i].bv_page = rqstp->rq_pages[i];
0257         bvec[i].bv_len = PAGE_SIZE;
0258         bvec[i].bv_offset = 0;
0259     }
0260     rqstp->rq_respages = &rqstp->rq_pages[i];
0261     rqstp->rq_next_page = rqstp->rq_respages + 1;
0262 
0263     iov_iter_bvec(&msg.msg_iter, READ, bvec, i, buflen);
0264     if (seek) {
0265         iov_iter_advance(&msg.msg_iter, seek);
0266         buflen -= seek;
0267     }
0268     len = sock_recvmsg(svsk->sk_sock, &msg, MSG_DONTWAIT);
0269     if (len > 0)
0270         svc_flush_bvec(bvec, len, seek);
0271 
0272     /* If we read a full record, then assume there may be more
0273      * data to read (stream based sockets only!)
0274      */
0275     if (len == buflen)
0276         set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
0277 
0278     return len;
0279 }
0280 
0281 /*
0282  * Set socket snd and rcv buffer lengths
0283  */
0284 static void svc_sock_setbufsize(struct svc_sock *svsk, unsigned int nreqs)
0285 {
0286     unsigned int max_mesg = svsk->sk_xprt.xpt_server->sv_max_mesg;
0287     struct socket *sock = svsk->sk_sock;
0288 
0289     nreqs = min(nreqs, INT_MAX / 2 / max_mesg);
0290 
0291     lock_sock(sock->sk);
0292     sock->sk->sk_sndbuf = nreqs * max_mesg * 2;
0293     sock->sk->sk_rcvbuf = nreqs * max_mesg * 2;
0294     sock->sk->sk_write_space(sock->sk);
0295     release_sock(sock->sk);
0296 }
0297 
0298 static void svc_sock_secure_port(struct svc_rqst *rqstp)
0299 {
0300     if (svc_port_is_privileged(svc_addr(rqstp)))
0301         __set_bit(RQ_SECURE, &rqstp->rq_flags);
0302     else
0303         __clear_bit(RQ_SECURE, &rqstp->rq_flags);
0304 }
0305 
0306 /*
0307  * INET callback when data has been received on the socket.
0308  */
0309 static void svc_data_ready(struct sock *sk)
0310 {
0311     struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
0312 
0313     if (svsk) {
0314         /* Refer to svc_setup_socket() for details. */
0315         rmb();
0316         svsk->sk_odata(sk);
0317         trace_svcsock_data_ready(&svsk->sk_xprt, 0);
0318         if (!test_and_set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags))
0319             svc_xprt_enqueue(&svsk->sk_xprt);
0320     }
0321 }
0322 
0323 /*
0324  * INET callback when space is newly available on the socket.
0325  */
0326 static void svc_write_space(struct sock *sk)
0327 {
0328     struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
0329 
0330     if (svsk) {
0331         /* Refer to svc_setup_socket() for details. */
0332         rmb();
0333         trace_svcsock_write_space(&svsk->sk_xprt, 0);
0334         svsk->sk_owspace(sk);
0335         svc_xprt_enqueue(&svsk->sk_xprt);
0336     }
0337 }
0338 
0339 static int svc_tcp_has_wspace(struct svc_xprt *xprt)
0340 {
0341     struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
0342 
0343     if (test_bit(XPT_LISTENER, &xprt->xpt_flags))
0344         return 1;
0345     return !test_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
0346 }
0347 
0348 static void svc_tcp_kill_temp_xprt(struct svc_xprt *xprt)
0349 {
0350     struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
0351 
0352     sock_no_linger(svsk->sk_sock->sk);
0353 }
0354 
0355 /*
0356  * See net/ipv6/ip_sockglue.c : ip_cmsg_recv_pktinfo
0357  */
0358 static int svc_udp_get_dest_address4(struct svc_rqst *rqstp,
0359                      struct cmsghdr *cmh)
0360 {
0361     struct in_pktinfo *pki = CMSG_DATA(cmh);
0362     struct sockaddr_in *daddr = svc_daddr_in(rqstp);
0363 
0364     if (cmh->cmsg_type != IP_PKTINFO)
0365         return 0;
0366 
0367     daddr->sin_family = AF_INET;
0368     daddr->sin_addr.s_addr = pki->ipi_spec_dst.s_addr;
0369     return 1;
0370 }
0371 
0372 /*
0373  * See net/ipv6/datagram.c : ip6_datagram_recv_ctl
0374  */
0375 static int svc_udp_get_dest_address6(struct svc_rqst *rqstp,
0376                      struct cmsghdr *cmh)
0377 {
0378     struct in6_pktinfo *pki = CMSG_DATA(cmh);
0379     struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
0380 
0381     if (cmh->cmsg_type != IPV6_PKTINFO)
0382         return 0;
0383 
0384     daddr->sin6_family = AF_INET6;
0385     daddr->sin6_addr = pki->ipi6_addr;
0386     daddr->sin6_scope_id = pki->ipi6_ifindex;
0387     return 1;
0388 }
0389 
0390 /*
0391  * Copy the UDP datagram's destination address to the rqstp structure.
0392  * The 'destination' address in this case is the address to which the
0393  * peer sent the datagram, i.e. our local address. For multihomed
0394  * hosts, this can change from msg to msg. Note that only the IP
0395  * address changes, the port number should remain the same.
0396  */
0397 static int svc_udp_get_dest_address(struct svc_rqst *rqstp,
0398                     struct cmsghdr *cmh)
0399 {
0400     switch (cmh->cmsg_level) {
0401     case SOL_IP:
0402         return svc_udp_get_dest_address4(rqstp, cmh);
0403     case SOL_IPV6:
0404         return svc_udp_get_dest_address6(rqstp, cmh);
0405     }
0406 
0407     return 0;
0408 }
0409 
0410 /**
0411  * svc_udp_recvfrom - Receive a datagram from a UDP socket.
0412  * @rqstp: request structure into which to receive an RPC Call
0413  *
0414  * Called in a loop when XPT_DATA has been set.
0415  *
0416  * Returns:
0417  *   On success, the number of bytes in a received RPC Call, or
0418  *   %0 if a complete RPC Call message was not ready to return
0419  */
0420 static int svc_udp_recvfrom(struct svc_rqst *rqstp)
0421 {
0422     struct svc_sock *svsk =
0423         container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
0424     struct svc_serv *serv = svsk->sk_xprt.xpt_server;
0425     struct sk_buff  *skb;
0426     union {
0427         struct cmsghdr  hdr;
0428         long        all[SVC_PKTINFO_SPACE / sizeof(long)];
0429     } buffer;
0430     struct cmsghdr *cmh = &buffer.hdr;
0431     struct msghdr msg = {
0432         .msg_name = svc_addr(rqstp),
0433         .msg_control = cmh,
0434         .msg_controllen = sizeof(buffer),
0435         .msg_flags = MSG_DONTWAIT,
0436     };
0437     size_t len;
0438     int err;
0439 
0440     if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
0441         /* udp sockets need large rcvbuf as all pending
0442          * requests are still in that buffer.  sndbuf must
0443          * also be large enough that there is enough space
0444          * for one reply per thread.  We count all threads
0445          * rather than threads in a particular pool, which
0446          * provides an upper bound on the number of threads
0447          * which will access the socket.
0448          */
0449         svc_sock_setbufsize(svsk, serv->sv_nrthreads + 3);
0450 
0451     clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
0452     err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
0453                  0, 0, MSG_PEEK | MSG_DONTWAIT);
0454     if (err < 0)
0455         goto out_recv_err;
0456     skb = skb_recv_udp(svsk->sk_sk, MSG_DONTWAIT, &err);
0457     if (!skb)
0458         goto out_recv_err;
0459 
0460     len = svc_addr_len(svc_addr(rqstp));
0461     rqstp->rq_addrlen = len;
0462     if (skb->tstamp == 0) {
0463         skb->tstamp = ktime_get_real();
0464         /* Don't enable netstamp, sunrpc doesn't
0465            need that much accuracy */
0466     }
0467     sock_write_timestamp(svsk->sk_sk, skb->tstamp);
0468     set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
0469 
0470     len = skb->len;
0471     rqstp->rq_arg.len = len;
0472     trace_svcsock_udp_recv(&svsk->sk_xprt, len);
0473 
0474     rqstp->rq_prot = IPPROTO_UDP;
0475 
0476     if (!svc_udp_get_dest_address(rqstp, cmh))
0477         goto out_cmsg_err;
0478     rqstp->rq_daddrlen = svc_addr_len(svc_daddr(rqstp));
0479 
0480     if (skb_is_nonlinear(skb)) {
0481         /* we have to copy */
0482         local_bh_disable();
0483         if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb))
0484             goto out_bh_enable;
0485         local_bh_enable();
0486         consume_skb(skb);
0487     } else {
0488         /* we can use it in-place */
0489         rqstp->rq_arg.head[0].iov_base = skb->data;
0490         rqstp->rq_arg.head[0].iov_len = len;
0491         if (skb_checksum_complete(skb))
0492             goto out_free;
0493         rqstp->rq_xprt_ctxt = skb;
0494     }
0495 
0496     rqstp->rq_arg.page_base = 0;
0497     if (len <= rqstp->rq_arg.head[0].iov_len) {
0498         rqstp->rq_arg.head[0].iov_len = len;
0499         rqstp->rq_arg.page_len = 0;
0500         rqstp->rq_respages = rqstp->rq_pages+1;
0501     } else {
0502         rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
0503         rqstp->rq_respages = rqstp->rq_pages + 1 +
0504             DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE);
0505     }
0506     rqstp->rq_next_page = rqstp->rq_respages+1;
0507 
0508     if (serv->sv_stats)
0509         serv->sv_stats->netudpcnt++;
0510 
0511     svc_xprt_received(rqstp->rq_xprt);
0512     return len;
0513 
0514 out_recv_err:
0515     if (err != -EAGAIN) {
0516         /* possibly an icmp error */
0517         set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
0518     }
0519     trace_svcsock_udp_recv_err(&svsk->sk_xprt, err);
0520     goto out_clear_busy;
0521 out_cmsg_err:
0522     net_warn_ratelimited("svc: received unknown control message %d/%d; dropping RPC reply datagram\n",
0523                  cmh->cmsg_level, cmh->cmsg_type);
0524     goto out_free;
0525 out_bh_enable:
0526     local_bh_enable();
0527 out_free:
0528     kfree_skb(skb);
0529 out_clear_busy:
0530     svc_xprt_received(rqstp->rq_xprt);
0531     return 0;
0532 }
0533 
0534 /**
0535  * svc_udp_sendto - Send out a reply on a UDP socket
0536  * @rqstp: completed svc_rqst
0537  *
0538  * xpt_mutex ensures @rqstp's whole message is written to the socket
0539  * without interruption.
0540  *
0541  * Returns the number of bytes sent, or a negative errno.
0542  */
0543 static int svc_udp_sendto(struct svc_rqst *rqstp)
0544 {
0545     struct svc_xprt *xprt = rqstp->rq_xprt;
0546     struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
0547     struct xdr_buf *xdr = &rqstp->rq_res;
0548     union {
0549         struct cmsghdr  hdr;
0550         long        all[SVC_PKTINFO_SPACE / sizeof(long)];
0551     } buffer;
0552     struct cmsghdr *cmh = &buffer.hdr;
0553     struct msghdr msg = {
0554         .msg_name   = &rqstp->rq_addr,
0555         .msg_namelen    = rqstp->rq_addrlen,
0556         .msg_control    = cmh,
0557         .msg_controllen = sizeof(buffer),
0558     };
0559     unsigned int sent;
0560     int err;
0561 
0562     svc_udp_release_rqst(rqstp);
0563 
0564     svc_set_cmsg_data(rqstp, cmh);
0565 
0566     mutex_lock(&xprt->xpt_mutex);
0567 
0568     if (svc_xprt_is_dead(xprt))
0569         goto out_notconn;
0570 
0571     err = xdr_alloc_bvec(xdr, GFP_KERNEL);
0572     if (err < 0)
0573         goto out_unlock;
0574 
0575     err = xprt_sock_sendmsg(svsk->sk_sock, &msg, xdr, 0, 0, &sent);
0576     if (err == -ECONNREFUSED) {
0577         /* ICMP error on earlier request. */
0578         err = xprt_sock_sendmsg(svsk->sk_sock, &msg, xdr, 0, 0, &sent);
0579     }
0580     xdr_free_bvec(xdr);
0581     trace_svcsock_udp_send(xprt, err);
0582 out_unlock:
0583     mutex_unlock(&xprt->xpt_mutex);
0584     if (err < 0)
0585         return err;
0586     return sent;
0587 
0588 out_notconn:
0589     mutex_unlock(&xprt->xpt_mutex);
0590     return -ENOTCONN;
0591 }
0592 
0593 static int svc_udp_has_wspace(struct svc_xprt *xprt)
0594 {
0595     struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
0596     struct svc_serv *serv = xprt->xpt_server;
0597     unsigned long required;
0598 
0599     /*
0600      * Set the SOCK_NOSPACE flag before checking the available
0601      * sock space.
0602      */
0603     set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
0604     required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
0605     if (required*2 > sock_wspace(svsk->sk_sk))
0606         return 0;
0607     clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
0608     return 1;
0609 }
0610 
0611 static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt)
0612 {
0613     BUG();
0614     return NULL;
0615 }
0616 
0617 static void svc_udp_kill_temp_xprt(struct svc_xprt *xprt)
0618 {
0619 }
0620 
0621 static struct svc_xprt *svc_udp_create(struct svc_serv *serv,
0622                        struct net *net,
0623                        struct sockaddr *sa, int salen,
0624                        int flags)
0625 {
0626     return svc_create_socket(serv, IPPROTO_UDP, net, sa, salen, flags);
0627 }
0628 
0629 static const struct svc_xprt_ops svc_udp_ops = {
0630     .xpo_create = svc_udp_create,
0631     .xpo_recvfrom = svc_udp_recvfrom,
0632     .xpo_sendto = svc_udp_sendto,
0633     .xpo_result_payload = svc_sock_result_payload,
0634     .xpo_release_rqst = svc_udp_release_rqst,
0635     .xpo_detach = svc_sock_detach,
0636     .xpo_free = svc_sock_free,
0637     .xpo_has_wspace = svc_udp_has_wspace,
0638     .xpo_accept = svc_udp_accept,
0639     .xpo_secure_port = svc_sock_secure_port,
0640     .xpo_kill_temp_xprt = svc_udp_kill_temp_xprt,
0641 };
0642 
0643 static struct svc_xprt_class svc_udp_class = {
0644     .xcl_name = "udp",
0645     .xcl_owner = THIS_MODULE,
0646     .xcl_ops = &svc_udp_ops,
0647     .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP,
0648     .xcl_ident = XPRT_TRANSPORT_UDP,
0649 };
0650 
0651 static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
0652 {
0653     svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_udp_class,
0654               &svsk->sk_xprt, serv);
0655     clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
0656     svsk->sk_sk->sk_data_ready = svc_data_ready;
0657     svsk->sk_sk->sk_write_space = svc_write_space;
0658 
0659     /* initialise setting must have enough space to
0660      * receive and respond to one request.
0661      * svc_udp_recvfrom will re-adjust if necessary
0662      */
0663     svc_sock_setbufsize(svsk, 3);
0664 
0665     /* data might have come in before data_ready set up */
0666     set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
0667     set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
0668 
0669     /* make sure we get destination address info */
0670     switch (svsk->sk_sk->sk_family) {
0671     case AF_INET:
0672         ip_sock_set_pktinfo(svsk->sk_sock->sk);
0673         break;
0674     case AF_INET6:
0675         ip6_sock_set_recvpktinfo(svsk->sk_sock->sk);
0676         break;
0677     default:
0678         BUG();
0679     }
0680 }
0681 
0682 /*
0683  * A data_ready event on a listening socket means there's a connection
0684  * pending. Do not use state_change as a substitute for it.
0685  */
0686 static void svc_tcp_listen_data_ready(struct sock *sk)
0687 {
0688     struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
0689 
0690     if (svsk) {
0691         /* Refer to svc_setup_socket() for details. */
0692         rmb();
0693         svsk->sk_odata(sk);
0694     }
0695 
0696     /*
0697      * This callback may called twice when a new connection
0698      * is established as a child socket inherits everything
0699      * from a parent LISTEN socket.
0700      * 1) data_ready method of the parent socket will be called
0701      *    when one of child sockets become ESTABLISHED.
0702      * 2) data_ready method of the child socket may be called
0703      *    when it receives data before the socket is accepted.
0704      * In case of 2, we should ignore it silently.
0705      */
0706     if (sk->sk_state == TCP_LISTEN) {
0707         if (svsk) {
0708             set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
0709             svc_xprt_enqueue(&svsk->sk_xprt);
0710         }
0711     }
0712 }
0713 
0714 /*
0715  * A state change on a connected socket means it's dying or dead.
0716  */
0717 static void svc_tcp_state_change(struct sock *sk)
0718 {
0719     struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
0720 
0721     if (svsk) {
0722         /* Refer to svc_setup_socket() for details. */
0723         rmb();
0724         svsk->sk_ostate(sk);
0725         trace_svcsock_tcp_state(&svsk->sk_xprt, svsk->sk_sock);
0726         if (sk->sk_state != TCP_ESTABLISHED)
0727             svc_xprt_deferred_close(&svsk->sk_xprt);
0728     }
0729 }
0730 
0731 /*
0732  * Accept a TCP connection
0733  */
0734 static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt)
0735 {
0736     struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
0737     struct sockaddr_storage addr;
0738     struct sockaddr *sin = (struct sockaddr *) &addr;
0739     struct svc_serv *serv = svsk->sk_xprt.xpt_server;
0740     struct socket   *sock = svsk->sk_sock;
0741     struct socket   *newsock;
0742     struct svc_sock *newsvsk;
0743     int     err, slen;
0744 
0745     if (!sock)
0746         return NULL;
0747 
0748     clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
0749     err = kernel_accept(sock, &newsock, O_NONBLOCK);
0750     if (err < 0) {
0751         if (err == -ENOMEM)
0752             printk(KERN_WARNING "%s: no more sockets!\n",
0753                    serv->sv_name);
0754         else if (err != -EAGAIN)
0755             net_warn_ratelimited("%s: accept failed (err %d)!\n",
0756                          serv->sv_name, -err);
0757         trace_svcsock_accept_err(xprt, serv->sv_name, err);
0758         return NULL;
0759     }
0760     set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
0761 
0762     err = kernel_getpeername(newsock, sin);
0763     if (err < 0) {
0764         trace_svcsock_getpeername_err(xprt, serv->sv_name, err);
0765         goto failed;        /* aborted connection or whatever */
0766     }
0767     slen = err;
0768 
0769     /* Reset the inherited callbacks before calling svc_setup_socket */
0770     newsock->sk->sk_state_change = svsk->sk_ostate;
0771     newsock->sk->sk_data_ready = svsk->sk_odata;
0772     newsock->sk->sk_write_space = svsk->sk_owspace;
0773 
0774     /* make sure that a write doesn't block forever when
0775      * low on memory
0776      */
0777     newsock->sk->sk_sndtimeo = HZ*30;
0778 
0779     newsvsk = svc_setup_socket(serv, newsock,
0780                  (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY));
0781     if (IS_ERR(newsvsk))
0782         goto failed;
0783     svc_xprt_set_remote(&newsvsk->sk_xprt, sin, slen);
0784     err = kernel_getsockname(newsock, sin);
0785     slen = err;
0786     if (unlikely(err < 0))
0787         slen = offsetof(struct sockaddr, sa_data);
0788     svc_xprt_set_local(&newsvsk->sk_xprt, sin, slen);
0789 
0790     if (sock_is_loopback(newsock->sk))
0791         set_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
0792     else
0793         clear_bit(XPT_LOCAL, &newsvsk->sk_xprt.xpt_flags);
0794     if (serv->sv_stats)
0795         serv->sv_stats->nettcpconn++;
0796 
0797     return &newsvsk->sk_xprt;
0798 
0799 failed:
0800     sock_release(newsock);
0801     return NULL;
0802 }
0803 
0804 static size_t svc_tcp_restore_pages(struct svc_sock *svsk,
0805                     struct svc_rqst *rqstp)
0806 {
0807     size_t len = svsk->sk_datalen;
0808     unsigned int i, npages;
0809 
0810     if (!len)
0811         return 0;
0812     npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
0813     for (i = 0; i < npages; i++) {
0814         if (rqstp->rq_pages[i] != NULL)
0815             put_page(rqstp->rq_pages[i]);
0816         BUG_ON(svsk->sk_pages[i] == NULL);
0817         rqstp->rq_pages[i] = svsk->sk_pages[i];
0818         svsk->sk_pages[i] = NULL;
0819     }
0820     rqstp->rq_arg.head[0].iov_base = page_address(rqstp->rq_pages[0]);
0821     return len;
0822 }
0823 
0824 static void svc_tcp_save_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
0825 {
0826     unsigned int i, len, npages;
0827 
0828     if (svsk->sk_datalen == 0)
0829         return;
0830     len = svsk->sk_datalen;
0831     npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
0832     for (i = 0; i < npages; i++) {
0833         svsk->sk_pages[i] = rqstp->rq_pages[i];
0834         rqstp->rq_pages[i] = NULL;
0835     }
0836 }
0837 
0838 static void svc_tcp_clear_pages(struct svc_sock *svsk)
0839 {
0840     unsigned int i, len, npages;
0841 
0842     if (svsk->sk_datalen == 0)
0843         goto out;
0844     len = svsk->sk_datalen;
0845     npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
0846     for (i = 0; i < npages; i++) {
0847         if (svsk->sk_pages[i] == NULL) {
0848             WARN_ON_ONCE(1);
0849             continue;
0850         }
0851         put_page(svsk->sk_pages[i]);
0852         svsk->sk_pages[i] = NULL;
0853     }
0854 out:
0855     svsk->sk_tcplen = 0;
0856     svsk->sk_datalen = 0;
0857 }
0858 
0859 /*
0860  * Receive fragment record header into sk_marker.
0861  */
0862 static ssize_t svc_tcp_read_marker(struct svc_sock *svsk,
0863                    struct svc_rqst *rqstp)
0864 {
0865     ssize_t want, len;
0866 
0867     /* If we haven't gotten the record length yet,
0868      * get the next four bytes.
0869      */
0870     if (svsk->sk_tcplen < sizeof(rpc_fraghdr)) {
0871         struct msghdr   msg = { NULL };
0872         struct kvec iov;
0873 
0874         want = sizeof(rpc_fraghdr) - svsk->sk_tcplen;
0875         iov.iov_base = ((char *)&svsk->sk_marker) + svsk->sk_tcplen;
0876         iov.iov_len  = want;
0877         iov_iter_kvec(&msg.msg_iter, READ, &iov, 1, want);
0878         len = sock_recvmsg(svsk->sk_sock, &msg, MSG_DONTWAIT);
0879         if (len < 0)
0880             return len;
0881         svsk->sk_tcplen += len;
0882         if (len < want) {
0883             /* call again to read the remaining bytes */
0884             goto err_short;
0885         }
0886         trace_svcsock_marker(&svsk->sk_xprt, svsk->sk_marker);
0887         if (svc_sock_reclen(svsk) + svsk->sk_datalen >
0888             svsk->sk_xprt.xpt_server->sv_max_mesg)
0889             goto err_too_large;
0890     }
0891     return svc_sock_reclen(svsk);
0892 
0893 err_too_large:
0894     net_notice_ratelimited("svc: %s %s RPC fragment too large: %d\n",
0895                    __func__, svsk->sk_xprt.xpt_server->sv_name,
0896                    svc_sock_reclen(svsk));
0897     svc_xprt_deferred_close(&svsk->sk_xprt);
0898 err_short:
0899     return -EAGAIN;
0900 }
0901 
0902 static int receive_cb_reply(struct svc_sock *svsk, struct svc_rqst *rqstp)
0903 {
0904     struct rpc_xprt *bc_xprt = svsk->sk_xprt.xpt_bc_xprt;
0905     struct rpc_rqst *req = NULL;
0906     struct kvec *src, *dst;
0907     __be32 *p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
0908     __be32 xid;
0909     __be32 calldir;
0910 
0911     xid = *p++;
0912     calldir = *p;
0913 
0914     if (!bc_xprt)
0915         return -EAGAIN;
0916     spin_lock(&bc_xprt->queue_lock);
0917     req = xprt_lookup_rqst(bc_xprt, xid);
0918     if (!req)
0919         goto unlock_notfound;
0920 
0921     memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
0922     /*
0923      * XXX!: cheating for now!  Only copying HEAD.
0924      * But we know this is good enough for now (in fact, for any
0925      * callback reply in the forseeable future).
0926      */
0927     dst = &req->rq_private_buf.head[0];
0928     src = &rqstp->rq_arg.head[0];
0929     if (dst->iov_len < src->iov_len)
0930         goto unlock_eagain; /* whatever; just giving up. */
0931     memcpy(dst->iov_base, src->iov_base, src->iov_len);
0932     xprt_complete_rqst(req->rq_task, rqstp->rq_arg.len);
0933     rqstp->rq_arg.len = 0;
0934     spin_unlock(&bc_xprt->queue_lock);
0935     return 0;
0936 unlock_notfound:
0937     printk(KERN_NOTICE
0938         "%s: Got unrecognized reply: "
0939         "calldir 0x%x xpt_bc_xprt %p xid %08x\n",
0940         __func__, ntohl(calldir),
0941         bc_xprt, ntohl(xid));
0942 unlock_eagain:
0943     spin_unlock(&bc_xprt->queue_lock);
0944     return -EAGAIN;
0945 }
0946 
0947 static void svc_tcp_fragment_received(struct svc_sock *svsk)
0948 {
0949     /* If we have more data, signal svc_xprt_enqueue() to try again */
0950     svsk->sk_tcplen = 0;
0951     svsk->sk_marker = xdr_zero;
0952 }
0953 
0954 /**
0955  * svc_tcp_recvfrom - Receive data from a TCP socket
0956  * @rqstp: request structure into which to receive an RPC Call
0957  *
0958  * Called in a loop when XPT_DATA has been set.
0959  *
0960  * Read the 4-byte stream record marker, then use the record length
0961  * in that marker to set up exactly the resources needed to receive
0962  * the next RPC message into @rqstp.
0963  *
0964  * Returns:
0965  *   On success, the number of bytes in a received RPC Call, or
0966  *   %0 if a complete RPC Call message was not ready to return
0967  *
0968  * The zero return case handles partial receives and callback Replies.
0969  * The state of a partial receive is preserved in the svc_sock for
0970  * the next call to svc_tcp_recvfrom.
0971  */
0972 static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
0973 {
0974     struct svc_sock *svsk =
0975         container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
0976     struct svc_serv *serv = svsk->sk_xprt.xpt_server;
0977     size_t want, base;
0978     ssize_t len;
0979     __be32 *p;
0980     __be32 calldir;
0981 
0982     clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
0983     len = svc_tcp_read_marker(svsk, rqstp);
0984     if (len < 0)
0985         goto error;
0986 
0987     base = svc_tcp_restore_pages(svsk, rqstp);
0988     want = len - (svsk->sk_tcplen - sizeof(rpc_fraghdr));
0989     len = svc_tcp_read_msg(rqstp, base + want, base);
0990     if (len >= 0) {
0991         trace_svcsock_tcp_recv(&svsk->sk_xprt, len);
0992         svsk->sk_tcplen += len;
0993         svsk->sk_datalen += len;
0994     }
0995     if (len != want || !svc_sock_final_rec(svsk))
0996         goto err_incomplete;
0997     if (svsk->sk_datalen < 8)
0998         goto err_nuts;
0999 
1000     rqstp->rq_arg.len = svsk->sk_datalen;
1001     rqstp->rq_arg.page_base = 0;
1002     if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
1003         rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
1004         rqstp->rq_arg.page_len = 0;
1005     } else
1006         rqstp->rq_arg.page_len = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
1007 
1008     rqstp->rq_xprt_ctxt   = NULL;
1009     rqstp->rq_prot        = IPPROTO_TCP;
1010     if (test_bit(XPT_LOCAL, &svsk->sk_xprt.xpt_flags))
1011         __set_bit(RQ_LOCAL, &rqstp->rq_flags);
1012     else
1013         __clear_bit(RQ_LOCAL, &rqstp->rq_flags);
1014 
1015     p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
1016     calldir = p[1];
1017     if (calldir)
1018         len = receive_cb_reply(svsk, rqstp);
1019 
1020     /* Reset TCP read info */
1021     svsk->sk_datalen = 0;
1022     svc_tcp_fragment_received(svsk);
1023 
1024     if (len < 0)
1025         goto error;
1026 
1027     svc_xprt_copy_addrs(rqstp, &svsk->sk_xprt);
1028     if (serv->sv_stats)
1029         serv->sv_stats->nettcpcnt++;
1030 
1031     svc_xprt_received(rqstp->rq_xprt);
1032     return rqstp->rq_arg.len;
1033 
1034 err_incomplete:
1035     svc_tcp_save_pages(svsk, rqstp);
1036     if (len < 0 && len != -EAGAIN)
1037         goto err_delete;
1038     if (len == want)
1039         svc_tcp_fragment_received(svsk);
1040     else
1041         trace_svcsock_tcp_recv_short(&svsk->sk_xprt,
1042                 svc_sock_reclen(svsk),
1043                 svsk->sk_tcplen - sizeof(rpc_fraghdr));
1044     goto err_noclose;
1045 error:
1046     if (len != -EAGAIN)
1047         goto err_delete;
1048     trace_svcsock_tcp_recv_eagain(&svsk->sk_xprt, 0);
1049     goto err_noclose;
1050 err_nuts:
1051     svsk->sk_datalen = 0;
1052 err_delete:
1053     trace_svcsock_tcp_recv_err(&svsk->sk_xprt, len);
1054     svc_xprt_deferred_close(&svsk->sk_xprt);
1055 err_noclose:
1056     svc_xprt_received(rqstp->rq_xprt);
1057     return 0;   /* record not complete */
1058 }
1059 
1060 static int svc_tcp_send_kvec(struct socket *sock, const struct kvec *vec,
1061                   int flags)
1062 {
1063     return kernel_sendpage(sock, virt_to_page(vec->iov_base),
1064                    offset_in_page(vec->iov_base),
1065                    vec->iov_len, flags);
1066 }
1067 
1068 /*
1069  * kernel_sendpage() is used exclusively to reduce the number of
1070  * copy operations in this path. Therefore the caller must ensure
1071  * that the pages backing @xdr are unchanging.
1072  *
1073  * In addition, the logic assumes that * .bv_len is never larger
1074  * than PAGE_SIZE.
1075  */
1076 static int svc_tcp_sendmsg(struct socket *sock, struct xdr_buf *xdr,
1077                rpc_fraghdr marker, unsigned int *sentp)
1078 {
1079     const struct kvec *head = xdr->head;
1080     const struct kvec *tail = xdr->tail;
1081     struct kvec rm = {
1082         .iov_base   = &marker,
1083         .iov_len    = sizeof(marker),
1084     };
1085     struct msghdr msg = {
1086         .msg_flags  = 0,
1087     };
1088     int ret;
1089 
1090     *sentp = 0;
1091     ret = xdr_alloc_bvec(xdr, GFP_KERNEL);
1092     if (ret < 0)
1093         return ret;
1094 
1095     ret = kernel_sendmsg(sock, &msg, &rm, 1, rm.iov_len);
1096     if (ret < 0)
1097         return ret;
1098     *sentp += ret;
1099     if (ret != rm.iov_len)
1100         return -EAGAIN;
1101 
1102     ret = svc_tcp_send_kvec(sock, head, 0);
1103     if (ret < 0)
1104         return ret;
1105     *sentp += ret;
1106     if (ret != head->iov_len)
1107         goto out;
1108 
1109     if (xdr->page_len) {
1110         unsigned int offset, len, remaining;
1111         struct bio_vec *bvec;
1112 
1113         bvec = xdr->bvec + (xdr->page_base >> PAGE_SHIFT);
1114         offset = offset_in_page(xdr->page_base);
1115         remaining = xdr->page_len;
1116         while (remaining > 0) {
1117             len = min(remaining, bvec->bv_len - offset);
1118             ret = kernel_sendpage(sock, bvec->bv_page,
1119                           bvec->bv_offset + offset,
1120                           len, 0);
1121             if (ret < 0)
1122                 return ret;
1123             *sentp += ret;
1124             if (ret != len)
1125                 goto out;
1126             remaining -= len;
1127             offset = 0;
1128             bvec++;
1129         }
1130     }
1131 
1132     if (tail->iov_len) {
1133         ret = svc_tcp_send_kvec(sock, tail, 0);
1134         if (ret < 0)
1135             return ret;
1136         *sentp += ret;
1137     }
1138 
1139 out:
1140     return 0;
1141 }
1142 
1143 /**
1144  * svc_tcp_sendto - Send out a reply on a TCP socket
1145  * @rqstp: completed svc_rqst
1146  *
1147  * xpt_mutex ensures @rqstp's whole message is written to the socket
1148  * without interruption.
1149  *
1150  * Returns the number of bytes sent, or a negative errno.
1151  */
1152 static int svc_tcp_sendto(struct svc_rqst *rqstp)
1153 {
1154     struct svc_xprt *xprt = rqstp->rq_xprt;
1155     struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1156     struct xdr_buf *xdr = &rqstp->rq_res;
1157     rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
1158                      (u32)xdr->len);
1159     unsigned int sent;
1160     int err;
1161 
1162     svc_tcp_release_rqst(rqstp);
1163 
1164     atomic_inc(&svsk->sk_sendqlen);
1165     mutex_lock(&xprt->xpt_mutex);
1166     if (svc_xprt_is_dead(xprt))
1167         goto out_notconn;
1168     tcp_sock_set_cork(svsk->sk_sk, true);
1169     err = svc_tcp_sendmsg(svsk->sk_sock, xdr, marker, &sent);
1170     xdr_free_bvec(xdr);
1171     trace_svcsock_tcp_send(xprt, err < 0 ? (long)err : sent);
1172     if (err < 0 || sent != (xdr->len + sizeof(marker)))
1173         goto out_close;
1174     if (atomic_dec_and_test(&svsk->sk_sendqlen))
1175         tcp_sock_set_cork(svsk->sk_sk, false);
1176     mutex_unlock(&xprt->xpt_mutex);
1177     return sent;
1178 
1179 out_notconn:
1180     atomic_dec(&svsk->sk_sendqlen);
1181     mutex_unlock(&xprt->xpt_mutex);
1182     return -ENOTCONN;
1183 out_close:
1184     pr_notice("rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n",
1185           xprt->xpt_server->sv_name,
1186           (err < 0) ? "got error" : "sent",
1187           (err < 0) ? err : sent, xdr->len);
1188     svc_xprt_deferred_close(xprt);
1189     atomic_dec(&svsk->sk_sendqlen);
1190     mutex_unlock(&xprt->xpt_mutex);
1191     return -EAGAIN;
1192 }
1193 
1194 static struct svc_xprt *svc_tcp_create(struct svc_serv *serv,
1195                        struct net *net,
1196                        struct sockaddr *sa, int salen,
1197                        int flags)
1198 {
1199     return svc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
1200 }
1201 
1202 static const struct svc_xprt_ops svc_tcp_ops = {
1203     .xpo_create = svc_tcp_create,
1204     .xpo_recvfrom = svc_tcp_recvfrom,
1205     .xpo_sendto = svc_tcp_sendto,
1206     .xpo_result_payload = svc_sock_result_payload,
1207     .xpo_release_rqst = svc_tcp_release_rqst,
1208     .xpo_detach = svc_tcp_sock_detach,
1209     .xpo_free = svc_sock_free,
1210     .xpo_has_wspace = svc_tcp_has_wspace,
1211     .xpo_accept = svc_tcp_accept,
1212     .xpo_secure_port = svc_sock_secure_port,
1213     .xpo_kill_temp_xprt = svc_tcp_kill_temp_xprt,
1214 };
1215 
1216 static struct svc_xprt_class svc_tcp_class = {
1217     .xcl_name = "tcp",
1218     .xcl_owner = THIS_MODULE,
1219     .xcl_ops = &svc_tcp_ops,
1220     .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
1221     .xcl_ident = XPRT_TRANSPORT_TCP,
1222 };
1223 
1224 void svc_init_xprt_sock(void)
1225 {
1226     svc_reg_xprt_class(&svc_tcp_class);
1227     svc_reg_xprt_class(&svc_udp_class);
1228 }
1229 
1230 void svc_cleanup_xprt_sock(void)
1231 {
1232     svc_unreg_xprt_class(&svc_tcp_class);
1233     svc_unreg_xprt_class(&svc_udp_class);
1234 }
1235 
1236 static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
1237 {
1238     struct sock *sk = svsk->sk_sk;
1239 
1240     svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_tcp_class,
1241               &svsk->sk_xprt, serv);
1242     set_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
1243     set_bit(XPT_CONG_CTRL, &svsk->sk_xprt.xpt_flags);
1244     if (sk->sk_state == TCP_LISTEN) {
1245         strcpy(svsk->sk_xprt.xpt_remotebuf, "listener");
1246         set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags);
1247         sk->sk_data_ready = svc_tcp_listen_data_ready;
1248         set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
1249     } else {
1250         sk->sk_state_change = svc_tcp_state_change;
1251         sk->sk_data_ready = svc_data_ready;
1252         sk->sk_write_space = svc_write_space;
1253 
1254         svsk->sk_marker = xdr_zero;
1255         svsk->sk_tcplen = 0;
1256         svsk->sk_datalen = 0;
1257         memset(&svsk->sk_pages[0], 0, sizeof(svsk->sk_pages));
1258 
1259         tcp_sock_set_nodelay(sk);
1260 
1261         set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
1262         switch (sk->sk_state) {
1263         case TCP_SYN_RECV:
1264         case TCP_ESTABLISHED:
1265             break;
1266         default:
1267             svc_xprt_deferred_close(&svsk->sk_xprt);
1268         }
1269     }
1270 }
1271 
1272 void svc_sock_update_bufs(struct svc_serv *serv)
1273 {
1274     /*
1275      * The number of server threads has changed. Update
1276      * rcvbuf and sndbuf accordingly on all sockets
1277      */
1278     struct svc_sock *svsk;
1279 
1280     spin_lock_bh(&serv->sv_lock);
1281     list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list)
1282         set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
1283     spin_unlock_bh(&serv->sv_lock);
1284 }
1285 EXPORT_SYMBOL_GPL(svc_sock_update_bufs);
1286 
1287 /*
1288  * Initialize socket for RPC use and create svc_sock struct
1289  */
1290 static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1291                         struct socket *sock,
1292                         int flags)
1293 {
1294     struct svc_sock *svsk;
1295     struct sock *inet;
1296     int     pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
1297     int     err = 0;
1298 
1299     svsk = kzalloc(sizeof(*svsk), GFP_KERNEL);
1300     if (!svsk)
1301         return ERR_PTR(-ENOMEM);
1302 
1303     inet = sock->sk;
1304 
1305     /* Register socket with portmapper */
1306     if (pmap_register)
1307         err = svc_register(serv, sock_net(sock->sk), inet->sk_family,
1308                      inet->sk_protocol,
1309                      ntohs(inet_sk(inet)->inet_sport));
1310 
1311     if (err < 0) {
1312         kfree(svsk);
1313         return ERR_PTR(err);
1314     }
1315 
1316     svsk->sk_sock = sock;
1317     svsk->sk_sk = inet;
1318     svsk->sk_ostate = inet->sk_state_change;
1319     svsk->sk_odata = inet->sk_data_ready;
1320     svsk->sk_owspace = inet->sk_write_space;
1321     /*
1322      * This barrier is necessary in order to prevent race condition
1323      * with svc_data_ready(), svc_listen_data_ready() and others
1324      * when calling callbacks above.
1325      */
1326     wmb();
1327     inet->sk_user_data = svsk;
1328 
1329     /* Initialize the socket */
1330     if (sock->type == SOCK_DGRAM)
1331         svc_udp_init(svsk, serv);
1332     else
1333         svc_tcp_init(svsk, serv);
1334 
1335     trace_svcsock_new_socket(sock);
1336     return svsk;
1337 }
1338 
1339 bool svc_alien_sock(struct net *net, int fd)
1340 {
1341     int err;
1342     struct socket *sock = sockfd_lookup(fd, &err);
1343     bool ret = false;
1344 
1345     if (!sock)
1346         goto out;
1347     if (sock_net(sock->sk) != net)
1348         ret = true;
1349     sockfd_put(sock);
1350 out:
1351     return ret;
1352 }
1353 EXPORT_SYMBOL_GPL(svc_alien_sock);
1354 
1355 /**
1356  * svc_addsock - add a listener socket to an RPC service
1357  * @serv: pointer to RPC service to which to add a new listener
1358  * @fd: file descriptor of the new listener
1359  * @name_return: pointer to buffer to fill in with name of listener
1360  * @len: size of the buffer
1361  * @cred: credential
1362  *
1363  * Fills in socket name and returns positive length of name if successful.
1364  * Name is terminated with '\n'.  On error, returns a negative errno
1365  * value.
1366  */
1367 int svc_addsock(struct svc_serv *serv, const int fd, char *name_return,
1368         const size_t len, const struct cred *cred)
1369 {
1370     int err = 0;
1371     struct socket *so = sockfd_lookup(fd, &err);
1372     struct svc_sock *svsk = NULL;
1373     struct sockaddr_storage addr;
1374     struct sockaddr *sin = (struct sockaddr *)&addr;
1375     int salen;
1376 
1377     if (!so)
1378         return err;
1379     err = -EAFNOSUPPORT;
1380     if ((so->sk->sk_family != PF_INET) && (so->sk->sk_family != PF_INET6))
1381         goto out;
1382     err =  -EPROTONOSUPPORT;
1383     if (so->sk->sk_protocol != IPPROTO_TCP &&
1384         so->sk->sk_protocol != IPPROTO_UDP)
1385         goto out;
1386     err = -EISCONN;
1387     if (so->state > SS_UNCONNECTED)
1388         goto out;
1389     err = -ENOENT;
1390     if (!try_module_get(THIS_MODULE))
1391         goto out;
1392     svsk = svc_setup_socket(serv, so, SVC_SOCK_DEFAULTS);
1393     if (IS_ERR(svsk)) {
1394         module_put(THIS_MODULE);
1395         err = PTR_ERR(svsk);
1396         goto out;
1397     }
1398     salen = kernel_getsockname(svsk->sk_sock, sin);
1399     if (salen >= 0)
1400         svc_xprt_set_local(&svsk->sk_xprt, sin, salen);
1401     svsk->sk_xprt.xpt_cred = get_cred(cred);
1402     svc_add_new_perm_xprt(serv, &svsk->sk_xprt);
1403     return svc_one_sock_name(svsk, name_return, len);
1404 out:
1405     sockfd_put(so);
1406     return err;
1407 }
1408 EXPORT_SYMBOL_GPL(svc_addsock);
1409 
1410 /*
1411  * Create socket for RPC service.
1412  */
1413 static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
1414                       int protocol,
1415                       struct net *net,
1416                       struct sockaddr *sin, int len,
1417                       int flags)
1418 {
1419     struct svc_sock *svsk;
1420     struct socket   *sock;
1421     int     error;
1422     int     type;
1423     struct sockaddr_storage addr;
1424     struct sockaddr *newsin = (struct sockaddr *)&addr;
1425     int     newlen;
1426     int     family;
1427 
1428     if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1429         printk(KERN_WARNING "svc: only UDP and TCP "
1430                 "sockets supported\n");
1431         return ERR_PTR(-EINVAL);
1432     }
1433 
1434     type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1435     switch (sin->sa_family) {
1436     case AF_INET6:
1437         family = PF_INET6;
1438         break;
1439     case AF_INET:
1440         family = PF_INET;
1441         break;
1442     default:
1443         return ERR_PTR(-EINVAL);
1444     }
1445 
1446     error = __sock_create(net, family, type, protocol, &sock, 1);
1447     if (error < 0)
1448         return ERR_PTR(error);
1449 
1450     svc_reclassify_socket(sock);
1451 
1452     /*
1453      * If this is an PF_INET6 listener, we want to avoid
1454      * getting requests from IPv4 remotes.  Those should
1455      * be shunted to a PF_INET listener via rpcbind.
1456      */
1457     if (family == PF_INET6)
1458         ip6_sock_set_v6only(sock->sk);
1459     if (type == SOCK_STREAM)
1460         sock->sk->sk_reuse = SK_CAN_REUSE; /* allow address reuse */
1461     error = kernel_bind(sock, sin, len);
1462     if (error < 0)
1463         goto bummer;
1464 
1465     error = kernel_getsockname(sock, newsin);
1466     if (error < 0)
1467         goto bummer;
1468     newlen = error;
1469 
1470     if (protocol == IPPROTO_TCP) {
1471         if ((error = kernel_listen(sock, 64)) < 0)
1472             goto bummer;
1473     }
1474 
1475     svsk = svc_setup_socket(serv, sock, flags);
1476     if (IS_ERR(svsk)) {
1477         error = PTR_ERR(svsk);
1478         goto bummer;
1479     }
1480     svc_xprt_set_local(&svsk->sk_xprt, newsin, newlen);
1481     return (struct svc_xprt *)svsk;
1482 bummer:
1483     sock_release(sock);
1484     return ERR_PTR(error);
1485 }
1486 
1487 /*
1488  * Detach the svc_sock from the socket so that no
1489  * more callbacks occur.
1490  */
1491 static void svc_sock_detach(struct svc_xprt *xprt)
1492 {
1493     struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1494     struct sock *sk = svsk->sk_sk;
1495 
1496     /* put back the old socket callbacks */
1497     lock_sock(sk);
1498     sk->sk_state_change = svsk->sk_ostate;
1499     sk->sk_data_ready = svsk->sk_odata;
1500     sk->sk_write_space = svsk->sk_owspace;
1501     sk->sk_user_data = NULL;
1502     release_sock(sk);
1503 }
1504 
1505 /*
1506  * Disconnect the socket, and reset the callbacks
1507  */
1508 static void svc_tcp_sock_detach(struct svc_xprt *xprt)
1509 {
1510     struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1511 
1512     svc_sock_detach(xprt);
1513 
1514     if (!test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
1515         svc_tcp_clear_pages(svsk);
1516         kernel_sock_shutdown(svsk->sk_sock, SHUT_RDWR);
1517     }
1518 }
1519 
1520 /*
1521  * Free the svc_sock's socket resources and the svc_sock itself.
1522  */
1523 static void svc_sock_free(struct svc_xprt *xprt)
1524 {
1525     struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1526 
1527     if (svsk->sk_sock->file)
1528         sockfd_put(svsk->sk_sock);
1529     else
1530         sock_release(svsk->sk_sock);
1531     kfree(svsk);
1532 }