2 * linux/net/sunrpc/svcsock.c
4 * These are the RPC server socket internals.
6 * The server scheduling algorithm does not always distribute the load
7 * evenly when servicing a single client. May need to modify the
8 * svc_xprt_enqueue procedure...
10 * TCP support is largely untested and may be a little slow. The problem
11 * is that we currently do two separate recvfrom's, one for the 4-byte
12 * record length, and the second for the actual record. This could possibly
13 * be improved by always reading a minimum size of around 100 bytes and
14 * tucking any superfluous bytes away in a temporary store. Still, that
15 * leaves write requests out in the rain. An alternative may be to peek at
16 * the first skb in the queue, and if it matches the next TCP sequence
17 * number, to extract the record marker. Yuck.
19 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/module.h>
25 #include <linux/errno.h>
26 #include <linux/fcntl.h>
27 #include <linux/net.h>
29 #include <linux/inet.h>
30 #include <linux/udp.h>
31 #include <linux/tcp.h>
32 #include <linux/unistd.h>
33 #include <linux/slab.h>
34 #include <linux/netdevice.h>
35 #include <linux/skbuff.h>
36 #include <linux/file.h>
37 #include <linux/freezer.h>
39 #include <net/checksum.h>
43 #include <net/tcp_states.h>
44 #include <asm/uaccess.h>
45 #include <asm/ioctls.h>
46 #include <trace/events/skb.h>
48 #include <linux/sunrpc/types.h>
49 #include <linux/sunrpc/clnt.h>
50 #include <linux/sunrpc/xdr.h>
51 #include <linux/sunrpc/msg_prot.h>
52 #include <linux/sunrpc/svcsock.h>
53 #include <linux/sunrpc/stats.h>
54 #include <linux/sunrpc/xprt.h>
58 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
61 static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
63 static void svc_udp_data_ready(struct sock *, int);
64 static int svc_udp_recvfrom(struct svc_rqst *);
65 static int svc_udp_sendto(struct svc_rqst *);
66 static void svc_sock_detach(struct svc_xprt *);
67 static void svc_tcp_sock_detach(struct svc_xprt *);
68 static void svc_sock_free(struct svc_xprt *);
70 static struct svc_xprt *svc_create_socket(struct svc_serv *, int,
71 struct net *, struct sockaddr *,
73 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
74 static struct svc_xprt *svc_bc_create_socket(struct svc_serv *, int,
75 struct net *, struct sockaddr *,
77 static void svc_bc_sock_free(struct svc_xprt *xprt);
78 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
80 #ifdef CONFIG_DEBUG_LOCK_ALLOC
81 static struct lock_class_key svc_key[2];
82 static struct lock_class_key svc_slock_key[2];
84 static void svc_reclassify_socket(struct socket *sock)
86 struct sock *sk = sock->sk;
88 WARN_ON_ONCE(sock_owned_by_user(sk));
89 if (sock_owned_by_user(sk))
92 switch (sk->sk_family) {
94 sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
96 "sk_xprt.xpt_lock-AF_INET-NFSD",
101 sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
103 "sk_xprt.xpt_lock-AF_INET6-NFSD",
112 static void svc_reclassify_socket(struct socket *sock)
118 * Release an skbuff after use
120 static void svc_release_skb(struct svc_rqst *rqstp)
122 struct sk_buff *skb = rqstp->rq_xprt_ctxt;
125 struct svc_sock *svsk =
126 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
127 rqstp->rq_xprt_ctxt = NULL;
129 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
130 skb_free_datagram_locked(svsk->sk_sk, skb);
134 union svc_pktinfo_u {
135 struct in_pktinfo pkti;
136 struct in6_pktinfo pkti6;
138 #define SVC_PKTINFO_SPACE \
139 CMSG_SPACE(sizeof(union svc_pktinfo_u))
141 static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
143 struct svc_sock *svsk =
144 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
145 switch (svsk->sk_sk->sk_family) {
147 struct in_pktinfo *pki = CMSG_DATA(cmh);
149 cmh->cmsg_level = SOL_IP;
150 cmh->cmsg_type = IP_PKTINFO;
151 pki->ipi_ifindex = 0;
152 pki->ipi_spec_dst.s_addr =
153 svc_daddr_in(rqstp)->sin_addr.s_addr;
154 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
159 struct in6_pktinfo *pki = CMSG_DATA(cmh);
160 struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
162 cmh->cmsg_level = SOL_IPV6;
163 cmh->cmsg_type = IPV6_PKTINFO;
164 pki->ipi6_ifindex = daddr->sin6_scope_id;
165 pki->ipi6_addr = daddr->sin6_addr;
166 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
173 * send routine intended to be shared by the fore- and back-channel
175 int svc_send_common(struct socket *sock, struct xdr_buf *xdr,
176 struct page *headpage, unsigned long headoffset,
177 struct page *tailpage, unsigned long tailoffset)
181 struct page **ppage = xdr->pages;
182 size_t base = xdr->page_base;
183 unsigned int pglen = xdr->page_len;
184 unsigned int flags = MSG_MORE;
191 if (slen == xdr->head[0].iov_len)
193 len = kernel_sendpage(sock, headpage, headoffset,
194 xdr->head[0].iov_len, flags);
195 if (len != xdr->head[0].iov_len)
197 slen -= xdr->head[0].iov_len;
202 size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
206 result = kernel_sendpage(sock, *ppage, base, size, flags);
213 size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
219 if (xdr->tail[0].iov_len) {
220 result = kernel_sendpage(sock, tailpage, tailoffset,
221 xdr->tail[0].iov_len, 0);
232 * Generic sendto routine
234 static int svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
236 struct svc_sock *svsk =
237 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
238 struct socket *sock = svsk->sk_sock;
241 long all[SVC_PKTINFO_SPACE / sizeof(long)];
243 struct cmsghdr *cmh = &buffer.hdr;
245 unsigned long tailoff;
246 unsigned long headoff;
247 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
249 if (rqstp->rq_prot == IPPROTO_UDP) {
250 struct msghdr msg = {
251 .msg_name = &rqstp->rq_addr,
252 .msg_namelen = rqstp->rq_addrlen,
254 .msg_controllen = sizeof(buffer),
255 .msg_flags = MSG_MORE,
258 svc_set_cmsg_data(rqstp, cmh);
260 if (sock_sendmsg(sock, &msg, 0) < 0)
264 tailoff = ((unsigned long)xdr->tail[0].iov_base) & (PAGE_SIZE-1);
266 len = svc_send_common(sock, xdr, rqstp->rq_respages[0], headoff,
267 rqstp->rq_respages[0], tailoff);
270 dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %s)\n",
271 svsk, xdr->head[0].iov_base, xdr->head[0].iov_len,
272 xdr->len, len, svc_print_addr(rqstp, buf, sizeof(buf)));
278 * Report socket names for nfsdfs
280 static int svc_one_sock_name(struct svc_sock *svsk, char *buf, int remaining)
282 const struct sock *sk = svsk->sk_sk;
283 const char *proto_name = sk->sk_protocol == IPPROTO_UDP ?
287 switch (sk->sk_family) {
289 len = snprintf(buf, remaining, "ipv4 %s %pI4 %d\n",
291 &inet_sk(sk)->inet_rcv_saddr,
292 inet_sk(sk)->inet_num);
295 len = snprintf(buf, remaining, "ipv6 %s %pI6 %d\n",
297 &inet6_sk(sk)->rcv_saddr,
298 inet_sk(sk)->inet_num);
301 len = snprintf(buf, remaining, "*unknown-%d*\n",
305 if (len >= remaining) {
307 return -ENAMETOOLONG;
313 * Check input queue length
315 static int svc_recv_available(struct svc_sock *svsk)
317 struct socket *sock = svsk->sk_sock;
320 err = kernel_sock_ioctl(sock, TIOCINQ, (unsigned long) &avail);
322 return (err >= 0)? avail : err;
326 * Generic recvfrom routine.
328 static int svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr,
331 struct svc_sock *svsk =
332 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
333 struct msghdr msg = {
334 .msg_flags = MSG_DONTWAIT,
338 rqstp->rq_xprt_hlen = 0;
340 len = kernel_recvmsg(svsk->sk_sock, &msg, iov, nr, buflen,
343 dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
344 svsk, iov[0].iov_base, iov[0].iov_len, len);
348 static int svc_partial_recvfrom(struct svc_rqst *rqstp,
349 struct kvec *iov, int nr,
350 int buflen, unsigned int base)
358 return svc_recvfrom(rqstp, iov, nr, buflen);
360 for (i = 0; i < nr; i++) {
361 if (iov[i].iov_len > base)
363 base -= iov[i].iov_len;
365 save_iovlen = iov[i].iov_len;
366 save_iovbase = iov[i].iov_base;
367 iov[i].iov_len -= base;
368 iov[i].iov_base += base;
369 ret = svc_recvfrom(rqstp, &iov[i], nr - i, buflen);
370 iov[i].iov_len = save_iovlen;
371 iov[i].iov_base = save_iovbase;
376 * Set socket snd and rcv buffer lengths
378 static void svc_sock_setbufsize(struct socket *sock, unsigned int snd,
383 oldfs = get_fs(); set_fs(KERNEL_DS);
384 sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
385 (char*)&snd, sizeof(snd));
386 sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
387 (char*)&rcv, sizeof(rcv));
389 /* sock_setsockopt limits use to sysctl_?mem_max,
390 * which isn't acceptable. Until that is made conditional
391 * on not having CAP_SYS_RESOURCE or similar, we go direct...
392 * DaveM said I could!
395 sock->sk->sk_sndbuf = snd * 2;
396 sock->sk->sk_rcvbuf = rcv * 2;
397 sock->sk->sk_write_space(sock->sk);
398 release_sock(sock->sk);
402 * INET callback when data has been received on the socket.
404 static void svc_udp_data_ready(struct sock *sk, int count)
406 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
407 wait_queue_head_t *wq = sk_sleep(sk);
410 dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
412 test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
413 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
414 svc_xprt_enqueue(&svsk->sk_xprt);
416 if (wq && waitqueue_active(wq))
417 wake_up_interruptible(wq);
421 * INET callback when space is newly available on the socket.
423 static void svc_write_space(struct sock *sk)
425 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
426 wait_queue_head_t *wq = sk_sleep(sk);
429 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
430 svsk, sk, test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
431 svc_xprt_enqueue(&svsk->sk_xprt);
434 if (wq && waitqueue_active(wq)) {
435 dprintk("RPC svc_write_space: someone sleeping on %p\n",
437 wake_up_interruptible(wq);
441 static void svc_tcp_write_space(struct sock *sk)
443 struct socket *sock = sk->sk_socket;
445 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk) && sock)
446 clear_bit(SOCK_NOSPACE, &sock->flags);
451 * See net/ipv6/ip_sockglue.c : ip_cmsg_recv_pktinfo
453 static int svc_udp_get_dest_address4(struct svc_rqst *rqstp,
456 struct in_pktinfo *pki = CMSG_DATA(cmh);
457 struct sockaddr_in *daddr = svc_daddr_in(rqstp);
459 if (cmh->cmsg_type != IP_PKTINFO)
462 daddr->sin_family = AF_INET;
463 daddr->sin_addr.s_addr = pki->ipi_spec_dst.s_addr;
468 * See net/ipv6/datagram.c : datagram_recv_ctl
470 static int svc_udp_get_dest_address6(struct svc_rqst *rqstp,
473 struct in6_pktinfo *pki = CMSG_DATA(cmh);
474 struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
476 if (cmh->cmsg_type != IPV6_PKTINFO)
479 daddr->sin6_family = AF_INET6;
480 daddr->sin6_addr = pki->ipi6_addr;
481 daddr->sin6_scope_id = pki->ipi6_ifindex;
486 * Copy the UDP datagram's destination address to the rqstp structure.
487 * The 'destination' address in this case is the address to which the
488 * peer sent the datagram, i.e. our local address. For multihomed
489 * hosts, this can change from msg to msg. Note that only the IP
490 * address changes, the port number should remain the same.
492 static int svc_udp_get_dest_address(struct svc_rqst *rqstp,
495 switch (cmh->cmsg_level) {
497 return svc_udp_get_dest_address4(rqstp, cmh);
499 return svc_udp_get_dest_address6(rqstp, cmh);
506 * Receive a datagram from a UDP socket.
508 static int svc_udp_recvfrom(struct svc_rqst *rqstp)
510 struct svc_sock *svsk =
511 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
512 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
516 long all[SVC_PKTINFO_SPACE / sizeof(long)];
518 struct cmsghdr *cmh = &buffer.hdr;
519 struct msghdr msg = {
520 .msg_name = svc_addr(rqstp),
522 .msg_controllen = sizeof(buffer),
523 .msg_flags = MSG_DONTWAIT,
528 if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
529 /* udp sockets need large rcvbuf as all pending
530 * requests are still in that buffer. sndbuf must
531 * also be large enough that there is enough space
532 * for one reply per thread. We count all threads
533 * rather than threads in a particular pool, which
534 * provides an upper bound on the number of threads
535 * which will access the socket.
537 svc_sock_setbufsize(svsk->sk_sock,
538 (serv->sv_nrthreads+3) * serv->sv_max_mesg,
539 (serv->sv_nrthreads+3) * serv->sv_max_mesg);
541 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
543 err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
544 0, 0, MSG_PEEK | MSG_DONTWAIT);
546 skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err);
549 if (err != -EAGAIN) {
550 /* possibly an icmp error */
551 dprintk("svc: recvfrom returned error %d\n", -err);
552 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
556 len = svc_addr_len(svc_addr(rqstp));
557 rqstp->rq_addrlen = len;
558 if (skb->tstamp.tv64 == 0) {
559 skb->tstamp = ktime_get_real();
560 /* Don't enable netstamp, sunrpc doesn't
561 need that much accuracy */
563 svsk->sk_sk->sk_stamp = skb->tstamp;
564 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
566 len = skb->len - sizeof(struct udphdr);
567 rqstp->rq_arg.len = len;
569 rqstp->rq_prot = IPPROTO_UDP;
571 if (!svc_udp_get_dest_address(rqstp, cmh)) {
572 net_warn_ratelimited("svc: received unknown control message %d/%d; dropping RPC reply datagram\n",
573 cmh->cmsg_level, cmh->cmsg_type);
576 rqstp->rq_daddrlen = svc_addr_len(svc_daddr(rqstp));
578 if (skb_is_nonlinear(skb)) {
579 /* we have to copy */
581 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
587 skb_free_datagram_locked(svsk->sk_sk, skb);
589 /* we can use it in-place */
590 rqstp->rq_arg.head[0].iov_base = skb->data +
591 sizeof(struct udphdr);
592 rqstp->rq_arg.head[0].iov_len = len;
593 if (skb_checksum_complete(skb))
595 rqstp->rq_xprt_ctxt = skb;
598 rqstp->rq_arg.page_base = 0;
599 if (len <= rqstp->rq_arg.head[0].iov_len) {
600 rqstp->rq_arg.head[0].iov_len = len;
601 rqstp->rq_arg.page_len = 0;
602 rqstp->rq_respages = rqstp->rq_pages+1;
604 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
605 rqstp->rq_respages = rqstp->rq_pages + 1 +
606 DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE);
610 serv->sv_stats->netudpcnt++;
614 trace_kfree_skb(skb, svc_udp_recvfrom);
615 skb_free_datagram_locked(svsk->sk_sk, skb);
620 svc_udp_sendto(struct svc_rqst *rqstp)
624 error = svc_sendto(rqstp, &rqstp->rq_res);
625 if (error == -ECONNREFUSED)
626 /* ICMP error on earlier request. */
627 error = svc_sendto(rqstp, &rqstp->rq_res);
632 static void svc_udp_prep_reply_hdr(struct svc_rqst *rqstp)
636 static int svc_udp_has_wspace(struct svc_xprt *xprt)
638 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
639 struct svc_serv *serv = xprt->xpt_server;
640 unsigned long required;
643 * Set the SOCK_NOSPACE flag before checking the available
646 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
647 required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
648 if (required*2 > sock_wspace(svsk->sk_sk))
650 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
654 static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt)
660 static struct svc_xprt *svc_udp_create(struct svc_serv *serv,
662 struct sockaddr *sa, int salen,
665 return svc_create_socket(serv, IPPROTO_UDP, net, sa, salen, flags);
668 static struct svc_xprt_ops svc_udp_ops = {
669 .xpo_create = svc_udp_create,
670 .xpo_recvfrom = svc_udp_recvfrom,
671 .xpo_sendto = svc_udp_sendto,
672 .xpo_release_rqst = svc_release_skb,
673 .xpo_detach = svc_sock_detach,
674 .xpo_free = svc_sock_free,
675 .xpo_prep_reply_hdr = svc_udp_prep_reply_hdr,
676 .xpo_has_wspace = svc_udp_has_wspace,
677 .xpo_accept = svc_udp_accept,
680 static struct svc_xprt_class svc_udp_class = {
682 .xcl_owner = THIS_MODULE,
683 .xcl_ops = &svc_udp_ops,
684 .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP,
687 static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
689 int err, level, optname, one = 1;
691 svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_udp_class,
692 &svsk->sk_xprt, serv);
693 clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
694 svsk->sk_sk->sk_data_ready = svc_udp_data_ready;
695 svsk->sk_sk->sk_write_space = svc_write_space;
697 /* initialise setting must have enough space to
698 * receive and respond to one request.
699 * svc_udp_recvfrom will re-adjust if necessary
701 svc_sock_setbufsize(svsk->sk_sock,
702 3 * svsk->sk_xprt.xpt_server->sv_max_mesg,
703 3 * svsk->sk_xprt.xpt_server->sv_max_mesg);
705 /* data might have come in before data_ready set up */
706 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
707 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
709 /* make sure we get destination address info */
710 switch (svsk->sk_sk->sk_family) {
713 optname = IP_PKTINFO;
717 optname = IPV6_RECVPKTINFO;
722 err = kernel_setsockopt(svsk->sk_sock, level, optname,
723 (char *)&one, sizeof(one));
724 dprintk("svc: kernel_setsockopt returned %d\n", err);
728 * A data_ready event on a listening socket means there's a connection
729 * pending. Do not use state_change as a substitute for it.
731 static void svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
733 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
734 wait_queue_head_t *wq;
736 dprintk("svc: socket %p TCP (listen) state change %d\n",
740 * This callback may called twice when a new connection
741 * is established as a child socket inherits everything
742 * from a parent LISTEN socket.
743 * 1) data_ready method of the parent socket will be called
744 * when one of child sockets become ESTABLISHED.
745 * 2) data_ready method of the child socket may be called
746 * when it receives data before the socket is accepted.
747 * In case of 2, we should ignore it silently.
749 if (sk->sk_state == TCP_LISTEN) {
751 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
752 svc_xprt_enqueue(&svsk->sk_xprt);
754 printk("svc: socket %p: no user data\n", sk);
758 if (wq && waitqueue_active(wq))
759 wake_up_interruptible_all(wq);
763 * A state change on a connected socket means it's dying or dead.
765 static void svc_tcp_state_change(struct sock *sk)
767 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
768 wait_queue_head_t *wq = sk_sleep(sk);
770 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
771 sk, sk->sk_state, sk->sk_user_data);
774 printk("svc: socket %p: no user data\n", sk);
776 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
777 svc_xprt_enqueue(&svsk->sk_xprt);
779 if (wq && waitqueue_active(wq))
780 wake_up_interruptible_all(wq);
783 static void svc_tcp_data_ready(struct sock *sk, int count)
785 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
786 wait_queue_head_t *wq = sk_sleep(sk);
788 dprintk("svc: socket %p TCP data ready (svsk %p)\n",
789 sk, sk->sk_user_data);
791 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
792 svc_xprt_enqueue(&svsk->sk_xprt);
794 if (wq && waitqueue_active(wq))
795 wake_up_interruptible(wq);
799 * Accept a TCP connection
801 static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt)
803 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
804 struct sockaddr_storage addr;
805 struct sockaddr *sin = (struct sockaddr *) &addr;
806 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
807 struct socket *sock = svsk->sk_sock;
808 struct socket *newsock;
809 struct svc_sock *newsvsk;
811 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
813 dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
817 clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
818 err = kernel_accept(sock, &newsock, O_NONBLOCK);
821 printk(KERN_WARNING "%s: no more sockets!\n",
823 else if (err != -EAGAIN)
824 net_warn_ratelimited("%s: accept failed (err %d)!\n",
825 serv->sv_name, -err);
828 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
830 err = kernel_getpeername(newsock, sin, &slen);
832 net_warn_ratelimited("%s: peername failed (err %d)!\n",
833 serv->sv_name, -err);
834 goto failed; /* aborted connection or whatever */
837 /* Ideally, we would want to reject connections from unauthorized
838 * hosts here, but when we get encryption, the IP of the host won't
839 * tell us anything. For now just warn about unpriv connections.
841 if (!svc_port_is_privileged(sin)) {
843 "%s: connect from unprivileged port: %s\n",
845 __svc_print_addr(sin, buf, sizeof(buf)));
847 dprintk("%s: connect from %s\n", serv->sv_name,
848 __svc_print_addr(sin, buf, sizeof(buf)));
850 /* make sure that a write doesn't block forever when
853 newsock->sk->sk_sndtimeo = HZ*30;
855 newsvsk = svc_setup_socket(serv, newsock,
856 (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY));
859 svc_xprt_set_remote(&newsvsk->sk_xprt, sin, slen);
860 err = kernel_getsockname(newsock, sin, &slen);
861 if (unlikely(err < 0)) {
862 dprintk("svc_tcp_accept: kernel_getsockname error %d\n", -err);
863 slen = offsetof(struct sockaddr, sa_data);
865 svc_xprt_set_local(&newsvsk->sk_xprt, sin, slen);
868 serv->sv_stats->nettcpconn++;
870 return &newsvsk->sk_xprt;
873 sock_release(newsock);
877 static unsigned int svc_tcp_restore_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
879 unsigned int i, len, npages;
881 if (svsk->sk_tcplen <= sizeof(rpc_fraghdr))
883 len = svsk->sk_tcplen - sizeof(rpc_fraghdr);
884 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
885 for (i = 0; i < npages; i++) {
886 if (rqstp->rq_pages[i] != NULL)
887 put_page(rqstp->rq_pages[i]);
888 BUG_ON(svsk->sk_pages[i] == NULL);
889 rqstp->rq_pages[i] = svsk->sk_pages[i];
890 svsk->sk_pages[i] = NULL;
892 rqstp->rq_arg.head[0].iov_base = page_address(rqstp->rq_pages[0]);
896 static void svc_tcp_save_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
898 unsigned int i, len, npages;
900 if (svsk->sk_tcplen <= sizeof(rpc_fraghdr))
902 len = svsk->sk_tcplen - sizeof(rpc_fraghdr);
903 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
904 for (i = 0; i < npages; i++) {
905 svsk->sk_pages[i] = rqstp->rq_pages[i];
906 rqstp->rq_pages[i] = NULL;
910 static void svc_tcp_clear_pages(struct svc_sock *svsk)
912 unsigned int i, len, npages;
914 if (svsk->sk_tcplen <= sizeof(rpc_fraghdr))
916 len = svsk->sk_tcplen - sizeof(rpc_fraghdr);
917 npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
918 for (i = 0; i < npages; i++) {
919 BUG_ON(svsk->sk_pages[i] == NULL);
920 put_page(svsk->sk_pages[i]);
921 svsk->sk_pages[i] = NULL;
929 * If we haven't gotten the record length yet, get the next four bytes.
930 * Otherwise try to gobble up as much as possible up to the complete
933 static int svc_tcp_recv_record(struct svc_sock *svsk, struct svc_rqst *rqstp)
935 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
939 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
941 if (svsk->sk_tcplen < sizeof(rpc_fraghdr)) {
944 want = sizeof(rpc_fraghdr) - svsk->sk_tcplen;
945 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
947 if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
949 svsk->sk_tcplen += len;
952 dprintk("svc: short recvfrom while reading record "
953 "length (%d of %d)\n", len, want);
957 svsk->sk_reclen = ntohl(svsk->sk_reclen);
958 if (!(svsk->sk_reclen & RPC_LAST_STREAM_FRAGMENT)) {
959 /* FIXME: technically, a record can be fragmented,
960 * and non-terminal fragments will not have the top
961 * bit set in the fragment length header.
962 * But apparently no known nfs clients send fragmented
964 net_notice_ratelimited("RPC: multiple fragments per record not supported\n");
968 svsk->sk_reclen &= RPC_FRAGMENT_SIZE_MASK;
969 dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
970 if (svsk->sk_reclen > serv->sv_max_mesg) {
971 net_notice_ratelimited("RPC: fragment too large: 0x%08lx\n",
972 (unsigned long)svsk->sk_reclen);
977 if (svsk->sk_reclen < 8)
978 goto err_delete; /* client is nuts. */
980 len = svsk->sk_reclen;
984 dprintk("RPC: TCP recv_record got %d\n", len);
987 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
991 static int receive_cb_reply(struct svc_sock *svsk, struct svc_rqst *rqstp)
993 struct rpc_xprt *bc_xprt = svsk->sk_xprt.xpt_bc_xprt;
994 struct rpc_rqst *req = NULL;
995 struct kvec *src, *dst;
996 __be32 *p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
1004 req = xprt_lookup_rqst(bc_xprt, xid);
1008 "%s: Got unrecognized reply: "
1009 "calldir 0x%x xpt_bc_xprt %p xid %08x\n",
1010 __func__, ntohl(calldir),
1015 memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
1017 * XXX!: cheating for now! Only copying HEAD.
1018 * But we know this is good enough for now (in fact, for any
1019 * callback reply in the forseeable future).
1021 dst = &req->rq_private_buf.head[0];
1022 src = &rqstp->rq_arg.head[0];
1023 if (dst->iov_len < src->iov_len)
1024 return -EAGAIN; /* whatever; just giving up. */
1025 memcpy(dst->iov_base, src->iov_base, src->iov_len);
1026 xprt_complete_rqst(req->rq_task, svsk->sk_reclen);
1027 rqstp->rq_arg.len = 0;
1031 static int copy_pages_to_kvecs(struct kvec *vec, struct page **pages, int len)
1037 vec[i].iov_base = page_address(pages[i]);
1038 vec[i].iov_len = PAGE_SIZE;
1047 * Receive data from a TCP socket.
1049 static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
1051 struct svc_sock *svsk =
1052 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
1053 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
1056 unsigned int want, base;
1061 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
1062 svsk, test_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags),
1063 test_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags),
1064 test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags));
1066 len = svc_tcp_recv_record(svsk, rqstp);
1070 base = svc_tcp_restore_pages(svsk, rqstp);
1071 want = svsk->sk_reclen - base;
1073 vec = rqstp->rq_vec;
1075 pnum = copy_pages_to_kvecs(&vec[0], &rqstp->rq_pages[0],
1078 rqstp->rq_respages = &rqstp->rq_pages[pnum];
1080 /* Now receive data */
1081 len = svc_partial_recvfrom(rqstp, vec, pnum, want, base);
1083 svsk->sk_tcplen += len;
1085 svc_tcp_save_pages(svsk, rqstp);
1086 if (len < 0 && len != -EAGAIN)
1088 dprintk("svc: incomplete TCP record (%d of %d)\n",
1089 svsk->sk_tcplen, svsk->sk_reclen);
1093 rqstp->rq_arg.len = svsk->sk_reclen;
1094 rqstp->rq_arg.page_base = 0;
1095 if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
1096 rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
1097 rqstp->rq_arg.page_len = 0;
1099 rqstp->rq_arg.page_len = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
1101 rqstp->rq_xprt_ctxt = NULL;
1102 rqstp->rq_prot = IPPROTO_TCP;
1104 p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
1107 len = receive_cb_reply(svsk, rqstp);
1109 /* Reset TCP read info */
1110 svsk->sk_reclen = 0;
1111 svsk->sk_tcplen = 0;
1112 /* If we have more data, signal svc_xprt_enqueue() to try again */
1113 if (svc_recv_available(svsk) > sizeof(rpc_fraghdr))
1114 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
1119 svc_xprt_copy_addrs(rqstp, &svsk->sk_xprt);
1121 serv->sv_stats->nettcpcnt++;
1123 dprintk("svc: TCP complete record (%d bytes)\n", rqstp->rq_arg.len);
1124 return rqstp->rq_arg.len;
1129 dprintk("RPC: TCP recvfrom got EAGAIN\n");
1132 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
1133 svsk->sk_xprt.xpt_server->sv_name, -len);
1134 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1136 return 0; /* record not complete */
1140 * Send out data on TCP socket.
1142 static int svc_tcp_sendto(struct svc_rqst *rqstp)
1144 struct xdr_buf *xbufp = &rqstp->rq_res;
1148 /* Set up the first element of the reply kvec.
1149 * Any other kvecs that may be in use have been taken
1150 * care of by the server implementation itself.
1152 reclen = htonl(0x80000000|((xbufp->len ) - 4));
1153 memcpy(xbufp->head[0].iov_base, &reclen, 4);
1155 sent = svc_sendto(rqstp, &rqstp->rq_res);
1156 if (sent != xbufp->len) {
1158 "rpc-srv/tcp: %s: %s %d when sending %d bytes "
1159 "- shutting down socket\n",
1160 rqstp->rq_xprt->xpt_server->sv_name,
1161 (sent<0)?"got error":"sent only",
1163 set_bit(XPT_CLOSE, &rqstp->rq_xprt->xpt_flags);
1164 svc_xprt_enqueue(rqstp->rq_xprt);
1171 * Setup response header. TCP has a 4B record length field.
1173 static void svc_tcp_prep_reply_hdr(struct svc_rqst *rqstp)
1175 struct kvec *resv = &rqstp->rq_res.head[0];
1177 /* tcp needs a space for the record length... */
1181 static int svc_tcp_has_wspace(struct svc_xprt *xprt)
1183 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1184 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
1187 if (test_bit(XPT_LISTENER, &xprt->xpt_flags))
1189 required = atomic_read(&xprt->xpt_reserved) + serv->sv_max_mesg;
1190 if (sk_stream_wspace(svsk->sk_sk) >= required)
1192 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
1196 static struct svc_xprt *svc_tcp_create(struct svc_serv *serv,
1198 struct sockaddr *sa, int salen,
1201 return svc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
1204 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1205 static struct svc_xprt *svc_bc_create_socket(struct svc_serv *, int,
1206 struct net *, struct sockaddr *,
1208 static void svc_bc_sock_free(struct svc_xprt *xprt);
1210 static struct svc_xprt *svc_bc_tcp_create(struct svc_serv *serv,
1212 struct sockaddr *sa, int salen,
1215 return svc_bc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
1218 static void svc_bc_tcp_sock_detach(struct svc_xprt *xprt)
1222 static struct svc_xprt_ops svc_tcp_bc_ops = {
1223 .xpo_create = svc_bc_tcp_create,
1224 .xpo_detach = svc_bc_tcp_sock_detach,
1225 .xpo_free = svc_bc_sock_free,
1226 .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr,
1229 static struct svc_xprt_class svc_tcp_bc_class = {
1230 .xcl_name = "tcp-bc",
1231 .xcl_owner = THIS_MODULE,
1232 .xcl_ops = &svc_tcp_bc_ops,
1233 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
1236 static void svc_init_bc_xprt_sock(void)
1238 svc_reg_xprt_class(&svc_tcp_bc_class);
1241 static void svc_cleanup_bc_xprt_sock(void)
1243 svc_unreg_xprt_class(&svc_tcp_bc_class);
1245 #else /* CONFIG_SUNRPC_BACKCHANNEL */
1246 static void svc_init_bc_xprt_sock(void)
1250 static void svc_cleanup_bc_xprt_sock(void)
1253 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1255 static struct svc_xprt_ops svc_tcp_ops = {
1256 .xpo_create = svc_tcp_create,
1257 .xpo_recvfrom = svc_tcp_recvfrom,
1258 .xpo_sendto = svc_tcp_sendto,
1259 .xpo_release_rqst = svc_release_skb,
1260 .xpo_detach = svc_tcp_sock_detach,
1261 .xpo_free = svc_sock_free,
1262 .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr,
1263 .xpo_has_wspace = svc_tcp_has_wspace,
1264 .xpo_accept = svc_tcp_accept,
1267 static struct svc_xprt_class svc_tcp_class = {
1269 .xcl_owner = THIS_MODULE,
1270 .xcl_ops = &svc_tcp_ops,
1271 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
1274 void svc_init_xprt_sock(void)
1276 svc_reg_xprt_class(&svc_tcp_class);
1277 svc_reg_xprt_class(&svc_udp_class);
1278 svc_init_bc_xprt_sock();
1281 void svc_cleanup_xprt_sock(void)
1283 svc_unreg_xprt_class(&svc_tcp_class);
1284 svc_unreg_xprt_class(&svc_udp_class);
1285 svc_cleanup_bc_xprt_sock();
1288 static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
1290 struct sock *sk = svsk->sk_sk;
1292 svc_xprt_init(sock_net(svsk->sk_sock->sk), &svc_tcp_class,
1293 &svsk->sk_xprt, serv);
1294 set_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
1295 if (sk->sk_state == TCP_LISTEN) {
1296 dprintk("setting up TCP socket for listening\n");
1297 set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags);
1298 sk->sk_data_ready = svc_tcp_listen_data_ready;
1299 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
1301 dprintk("setting up TCP socket for reading\n");
1302 sk->sk_state_change = svc_tcp_state_change;
1303 sk->sk_data_ready = svc_tcp_data_ready;
1304 sk->sk_write_space = svc_tcp_write_space;
1306 svsk->sk_reclen = 0;
1307 svsk->sk_tcplen = 0;
1308 memset(&svsk->sk_pages[0], 0, sizeof(svsk->sk_pages));
1310 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
1312 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
1313 if (sk->sk_state != TCP_ESTABLISHED)
1314 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1318 void svc_sock_update_bufs(struct svc_serv *serv)
1321 * The number of server threads has changed. Update
1322 * rcvbuf and sndbuf accordingly on all sockets
1324 struct svc_sock *svsk;
1326 spin_lock_bh(&serv->sv_lock);
1327 list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list)
1328 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
1329 spin_unlock_bh(&serv->sv_lock);
1331 EXPORT_SYMBOL_GPL(svc_sock_update_bufs);
1334 * Initialize socket for RPC use and create svc_sock struct
1335 * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
1337 static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1338 struct socket *sock,
1341 struct svc_sock *svsk;
1343 int pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
1346 dprintk("svc: svc_setup_socket %p\n", sock);
1347 svsk = kzalloc(sizeof(*svsk), GFP_KERNEL);
1349 return ERR_PTR(-ENOMEM);
1353 /* Register socket with portmapper */
1355 err = svc_register(serv, sock_net(sock->sk), inet->sk_family,
1357 ntohs(inet_sk(inet)->inet_sport));
1361 return ERR_PTR(err);
1364 inet->sk_user_data = svsk;
1365 svsk->sk_sock = sock;
1367 svsk->sk_ostate = inet->sk_state_change;
1368 svsk->sk_odata = inet->sk_data_ready;
1369 svsk->sk_owspace = inet->sk_write_space;
1371 /* Initialize the socket */
1372 if (sock->type == SOCK_DGRAM)
1373 svc_udp_init(svsk, serv);
1375 /* initialise setting must have enough space to
1376 * receive and respond to one request.
1378 svc_sock_setbufsize(svsk->sk_sock, 4 * serv->sv_max_mesg,
1379 4 * serv->sv_max_mesg);
1380 svc_tcp_init(svsk, serv);
1383 dprintk("svc: svc_setup_socket created %p (inet %p)\n",
1390 * svc_addsock - add a listener socket to an RPC service
1391 * @serv: pointer to RPC service to which to add a new listener
1392 * @fd: file descriptor of the new listener
1393 * @name_return: pointer to buffer to fill in with name of listener
1394 * @len: size of the buffer
1396 * Fills in socket name and returns positive length of name if successful.
1397 * Name is terminated with '\n'. On error, returns a negative errno
1400 int svc_addsock(struct svc_serv *serv, const int fd, char *name_return,
1404 struct socket *so = sockfd_lookup(fd, &err);
1405 struct svc_sock *svsk = NULL;
1406 struct sockaddr_storage addr;
1407 struct sockaddr *sin = (struct sockaddr *)&addr;
1412 err = -EAFNOSUPPORT;
1413 if ((so->sk->sk_family != PF_INET) && (so->sk->sk_family != PF_INET6))
1415 err = -EPROTONOSUPPORT;
1416 if (so->sk->sk_protocol != IPPROTO_TCP &&
1417 so->sk->sk_protocol != IPPROTO_UDP)
1420 if (so->state > SS_UNCONNECTED)
1423 if (!try_module_get(THIS_MODULE))
1425 svsk = svc_setup_socket(serv, so, SVC_SOCK_DEFAULTS);
1427 module_put(THIS_MODULE);
1428 err = PTR_ERR(svsk);
1431 if (kernel_getsockname(svsk->sk_sock, sin, &salen) == 0)
1432 svc_xprt_set_local(&svsk->sk_xprt, sin, salen);
1433 svc_add_new_perm_xprt(serv, &svsk->sk_xprt);
1434 return svc_one_sock_name(svsk, name_return, len);
1439 EXPORT_SYMBOL_GPL(svc_addsock);
1442 * Create socket for RPC service.
1444 static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
1447 struct sockaddr *sin, int len,
1450 struct svc_sock *svsk;
1451 struct socket *sock;
1454 struct sockaddr_storage addr;
1455 struct sockaddr *newsin = (struct sockaddr *)&addr;
1459 RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
1461 dprintk("svc: svc_create_socket(%s, %d, %s)\n",
1462 serv->sv_program->pg_name, protocol,
1463 __svc_print_addr(sin, buf, sizeof(buf)));
1465 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1466 printk(KERN_WARNING "svc: only UDP and TCP "
1467 "sockets supported\n");
1468 return ERR_PTR(-EINVAL);
1471 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1472 switch (sin->sa_family) {
1480 return ERR_PTR(-EINVAL);
1483 error = __sock_create(net, family, type, protocol, &sock, 1);
1485 return ERR_PTR(error);
1487 svc_reclassify_socket(sock);
1490 * If this is an PF_INET6 listener, we want to avoid
1491 * getting requests from IPv4 remotes. Those should
1492 * be shunted to a PF_INET listener via rpcbind.
1495 if (family == PF_INET6)
1496 kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
1497 (char *)&val, sizeof(val));
1499 if (type == SOCK_STREAM)
1500 sock->sk->sk_reuse = SK_CAN_REUSE; /* allow address reuse */
1501 error = kernel_bind(sock, sin, len);
1506 error = kernel_getsockname(sock, newsin, &newlen);
1510 if (protocol == IPPROTO_TCP) {
1511 if ((error = kernel_listen(sock, 64)) < 0)
1515 svsk = svc_setup_socket(serv, sock, flags);
1517 error = PTR_ERR(svsk);
1520 svc_xprt_set_local(&svsk->sk_xprt, newsin, newlen);
1521 return (struct svc_xprt *)svsk;
1523 dprintk("svc: svc_create_socket error = %d\n", -error);
1525 return ERR_PTR(error);
1529 * Detach the svc_sock from the socket so that no
1530 * more callbacks occur.
1532 static void svc_sock_detach(struct svc_xprt *xprt)
1534 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1535 struct sock *sk = svsk->sk_sk;
1536 wait_queue_head_t *wq;
1538 dprintk("svc: svc_sock_detach(%p)\n", svsk);
1540 /* put back the old socket callbacks */
1541 sk->sk_state_change = svsk->sk_ostate;
1542 sk->sk_data_ready = svsk->sk_odata;
1543 sk->sk_write_space = svsk->sk_owspace;
1546 if (wq && waitqueue_active(wq))
1547 wake_up_interruptible(wq);
1551 * Disconnect the socket, and reset the callbacks
1553 static void svc_tcp_sock_detach(struct svc_xprt *xprt)
1555 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1557 dprintk("svc: svc_tcp_sock_detach(%p)\n", svsk);
1559 svc_sock_detach(xprt);
1561 if (!test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
1562 svc_tcp_clear_pages(svsk);
1563 kernel_sock_shutdown(svsk->sk_sock, SHUT_RDWR);
1568 * Free the svc_sock's socket resources and the svc_sock itself.
1570 static void svc_sock_free(struct svc_xprt *xprt)
1572 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1573 dprintk("svc: svc_sock_free(%p)\n", svsk);
1575 if (svsk->sk_sock->file)
1576 sockfd_put(svsk->sk_sock);
1578 sock_release(svsk->sk_sock);
1582 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1584 * Create a back channel svc_xprt which shares the fore channel socket.
1586 static struct svc_xprt *svc_bc_create_socket(struct svc_serv *serv,
1589 struct sockaddr *sin, int len,
1592 struct svc_sock *svsk;
1593 struct svc_xprt *xprt;
1595 if (protocol != IPPROTO_TCP) {
1596 printk(KERN_WARNING "svc: only TCP sockets"
1597 " supported on shared back channel\n");
1598 return ERR_PTR(-EINVAL);
1601 svsk = kzalloc(sizeof(*svsk), GFP_KERNEL);
1603 return ERR_PTR(-ENOMEM);
1605 xprt = &svsk->sk_xprt;
1606 svc_xprt_init(net, &svc_tcp_bc_class, xprt, serv);
1608 serv->sv_bc_xprt = xprt;
1614 * Free a back channel svc_sock.
1616 static void svc_bc_sock_free(struct svc_xprt *xprt)
1619 kfree(container_of(xprt, struct svc_sock, sk_xprt));
1621 #endif /* CONFIG_SUNRPC_BACKCHANNEL */