Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-drm-fsl-dcu.git] / net / sunrpc / svcsock.c
1 /*
2  * linux/net/sunrpc/svcsock.c
3  *
4  * These are the RPC server socket internals.
5  *
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...
9  *
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.
18  *
19  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
20  */
21
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>
28 #include <linux/in.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>
38 #include <net/sock.h>
39 #include <net/checksum.h>
40 #include <net/ip.h>
41 #include <net/ipv6.h>
42 #include <net/tcp.h>
43 #include <net/tcp_states.h>
44 #include <asm/uaccess.h>
45 #include <asm/ioctls.h>
46 #include <trace/events/skb.h>
47
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>
55
56 #include "sunrpc.h"
57
58 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
59
60
61 static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
62                                          int flags);
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 *);
69
70 static struct svc_xprt *svc_create_socket(struct svc_serv *, int,
71                                           struct net *, struct sockaddr *,
72                                           int, int);
73 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
74 static struct svc_xprt *svc_bc_create_socket(struct svc_serv *, int,
75                                              struct net *, struct sockaddr *,
76                                              int, int);
77 static void svc_bc_sock_free(struct svc_xprt *xprt);
78 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
79
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];
83
84 static void svc_reclassify_socket(struct socket *sock)
85 {
86         struct sock *sk = sock->sk;
87
88         WARN_ON_ONCE(sock_owned_by_user(sk));
89         if (sock_owned_by_user(sk))
90                 return;
91
92         switch (sk->sk_family) {
93         case AF_INET:
94                 sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
95                                               &svc_slock_key[0],
96                                               "sk_xprt.xpt_lock-AF_INET-NFSD",
97                                               &svc_key[0]);
98                 break;
99
100         case AF_INET6:
101                 sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
102                                               &svc_slock_key[1],
103                                               "sk_xprt.xpt_lock-AF_INET6-NFSD",
104                                               &svc_key[1]);
105                 break;
106
107         default:
108                 BUG();
109         }
110 }
111 #else
112 static void svc_reclassify_socket(struct socket *sock)
113 {
114 }
115 #endif
116
117 /*
118  * Release an skbuff after use
119  */
120 static void svc_release_skb(struct svc_rqst *rqstp)
121 {
122         struct sk_buff *skb = rqstp->rq_xprt_ctxt;
123
124         if (skb) {
125                 struct svc_sock *svsk =
126                         container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
127                 rqstp->rq_xprt_ctxt = NULL;
128
129                 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
130                 skb_free_datagram_locked(svsk->sk_sk, skb);
131         }
132 }
133
134 union svc_pktinfo_u {
135         struct in_pktinfo pkti;
136         struct in6_pktinfo pkti6;
137 };
138 #define SVC_PKTINFO_SPACE \
139         CMSG_SPACE(sizeof(union svc_pktinfo_u))
140
141 static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
142 {
143         struct svc_sock *svsk =
144                 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
145         switch (svsk->sk_sk->sk_family) {
146         case AF_INET: {
147                         struct in_pktinfo *pki = CMSG_DATA(cmh);
148
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));
155                 }
156                 break;
157
158         case AF_INET6: {
159                         struct in6_pktinfo *pki = CMSG_DATA(cmh);
160                         struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
161
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));
167                 }
168                 break;
169         }
170 }
171
172 /*
173  * send routine intended to be shared by the fore- and back-channel
174  */
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)
178 {
179         int             result;
180         int             size;
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;
185         int             slen;
186         int             len = 0;
187
188         slen = xdr->len;
189
190         /* send head */
191         if (slen == xdr->head[0].iov_len)
192                 flags = 0;
193         len = kernel_sendpage(sock, headpage, headoffset,
194                                   xdr->head[0].iov_len, flags);
195         if (len != xdr->head[0].iov_len)
196                 goto out;
197         slen -= xdr->head[0].iov_len;
198         if (slen == 0)
199                 goto out;
200
201         /* send page data */
202         size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
203         while (pglen > 0) {
204                 if (slen == size)
205                         flags = 0;
206                 result = kernel_sendpage(sock, *ppage, base, size, flags);
207                 if (result > 0)
208                         len += result;
209                 if (result != size)
210                         goto out;
211                 slen -= size;
212                 pglen -= size;
213                 size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
214                 base = 0;
215                 ppage++;
216         }
217
218         /* send tail */
219         if (xdr->tail[0].iov_len) {
220                 result = kernel_sendpage(sock, tailpage, tailoffset,
221                                    xdr->tail[0].iov_len, 0);
222                 if (result > 0)
223                         len += result;
224         }
225
226 out:
227         return len;
228 }
229
230
231 /*
232  * Generic sendto routine
233  */
234 static int svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
235 {
236         struct svc_sock *svsk =
237                 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
238         struct socket   *sock = svsk->sk_sock;
239         union {
240                 struct cmsghdr  hdr;
241                 long            all[SVC_PKTINFO_SPACE / sizeof(long)];
242         } buffer;
243         struct cmsghdr *cmh = &buffer.hdr;
244         int             len = 0;
245         unsigned long tailoff;
246         unsigned long headoff;
247         RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
248
249         if (rqstp->rq_prot == IPPROTO_UDP) {
250                 struct msghdr msg = {
251                         .msg_name       = &rqstp->rq_addr,
252                         .msg_namelen    = rqstp->rq_addrlen,
253                         .msg_control    = cmh,
254                         .msg_controllen = sizeof(buffer),
255                         .msg_flags      = MSG_MORE,
256                 };
257
258                 svc_set_cmsg_data(rqstp, cmh);
259
260                 if (sock_sendmsg(sock, &msg, 0) < 0)
261                         goto out;
262         }
263
264         tailoff = ((unsigned long)xdr->tail[0].iov_base) & (PAGE_SIZE-1);
265         headoff = 0;
266         len = svc_send_common(sock, xdr, rqstp->rq_respages[0], headoff,
267                                rqstp->rq_respages[0], tailoff);
268
269 out:
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)));
273
274         return len;
275 }
276
277 /*
278  * Report socket names for nfsdfs
279  */
280 static int svc_one_sock_name(struct svc_sock *svsk, char *buf, int remaining)
281 {
282         const struct sock *sk = svsk->sk_sk;
283         const char *proto_name = sk->sk_protocol == IPPROTO_UDP ?
284                                                         "udp" : "tcp";
285         int len;
286
287         switch (sk->sk_family) {
288         case PF_INET:
289                 len = snprintf(buf, remaining, "ipv4 %s %pI4 %d\n",
290                                 proto_name,
291                                 &inet_sk(sk)->inet_rcv_saddr,
292                                 inet_sk(sk)->inet_num);
293                 break;
294         case PF_INET6:
295                 len = snprintf(buf, remaining, "ipv6 %s %pI6 %d\n",
296                                 proto_name,
297                                 &inet6_sk(sk)->rcv_saddr,
298                                 inet_sk(sk)->inet_num);
299                 break;
300         default:
301                 len = snprintf(buf, remaining, "*unknown-%d*\n",
302                                 sk->sk_family);
303         }
304
305         if (len >= remaining) {
306                 *buf = '\0';
307                 return -ENAMETOOLONG;
308         }
309         return len;
310 }
311
312 /*
313  * Check input queue length
314  */
315 static int svc_recv_available(struct svc_sock *svsk)
316 {
317         struct socket   *sock = svsk->sk_sock;
318         int             avail, err;
319
320         err = kernel_sock_ioctl(sock, TIOCINQ, (unsigned long) &avail);
321
322         return (err >= 0)? avail : err;
323 }
324
325 /*
326  * Generic recvfrom routine.
327  */
328 static int svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr,
329                         int buflen)
330 {
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,
335         };
336         int len;
337
338         rqstp->rq_xprt_hlen = 0;
339
340         len = kernel_recvmsg(svsk->sk_sock, &msg, iov, nr, buflen,
341                                 msg.msg_flags);
342
343         dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
344                 svsk, iov[0].iov_base, iov[0].iov_len, len);
345         return len;
346 }
347
348 static int svc_partial_recvfrom(struct svc_rqst *rqstp,
349                                 struct kvec *iov, int nr,
350                                 int buflen, unsigned int base)
351 {
352         size_t save_iovlen;
353         void *save_iovbase;
354         unsigned int i;
355         int ret;
356
357         if (base == 0)
358                 return svc_recvfrom(rqstp, iov, nr, buflen);
359
360         for (i = 0; i < nr; i++) {
361                 if (iov[i].iov_len > base)
362                         break;
363                 base -= iov[i].iov_len;
364         }
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;
372         return ret;
373 }
374
375 /*
376  * Set socket snd and rcv buffer lengths
377  */
378 static void svc_sock_setbufsize(struct socket *sock, unsigned int snd,
379                                 unsigned int rcv)
380 {
381 #if 0
382         mm_segment_t    oldfs;
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));
388 #else
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!
393          */
394         lock_sock(sock->sk);
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);
399 #endif
400 }
401 /*
402  * INET callback when data has been received on the socket.
403  */
404 static void svc_udp_data_ready(struct sock *sk, int count)
405 {
406         struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
407         wait_queue_head_t *wq = sk_sleep(sk);
408
409         if (svsk) {
410                 dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
411                         svsk, sk, count,
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);
415         }
416         if (wq && waitqueue_active(wq))
417                 wake_up_interruptible(wq);
418 }
419
420 /*
421  * INET callback when space is newly available on the socket.
422  */
423 static void svc_write_space(struct sock *sk)
424 {
425         struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
426         wait_queue_head_t *wq = sk_sleep(sk);
427
428         if (svsk) {
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);
432         }
433
434         if (wq && waitqueue_active(wq)) {
435                 dprintk("RPC svc_write_space: someone sleeping on %p\n",
436                        svsk);
437                 wake_up_interruptible(wq);
438         }
439 }
440
441 static void svc_tcp_write_space(struct sock *sk)
442 {
443         struct socket *sock = sk->sk_socket;
444
445         if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk) && sock)
446                 clear_bit(SOCK_NOSPACE, &sock->flags);
447         svc_write_space(sk);
448 }
449
450 /*
451  * See net/ipv6/ip_sockglue.c : ip_cmsg_recv_pktinfo
452  */
453 static int svc_udp_get_dest_address4(struct svc_rqst *rqstp,
454                                      struct cmsghdr *cmh)
455 {
456         struct in_pktinfo *pki = CMSG_DATA(cmh);
457         struct sockaddr_in *daddr = svc_daddr_in(rqstp);
458
459         if (cmh->cmsg_type != IP_PKTINFO)
460                 return 0;
461
462         daddr->sin_family = AF_INET;
463         daddr->sin_addr.s_addr = pki->ipi_spec_dst.s_addr;
464         return 1;
465 }
466
467 /*
468  * See net/ipv6/datagram.c : datagram_recv_ctl
469  */
470 static int svc_udp_get_dest_address6(struct svc_rqst *rqstp,
471                                      struct cmsghdr *cmh)
472 {
473         struct in6_pktinfo *pki = CMSG_DATA(cmh);
474         struct sockaddr_in6 *daddr = svc_daddr_in6(rqstp);
475
476         if (cmh->cmsg_type != IPV6_PKTINFO)
477                 return 0;
478
479         daddr->sin6_family = AF_INET6;
480         daddr->sin6_addr = pki->ipi6_addr;
481         daddr->sin6_scope_id = pki->ipi6_ifindex;
482         return 1;
483 }
484
485 /*
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.
491  */
492 static int svc_udp_get_dest_address(struct svc_rqst *rqstp,
493                                     struct cmsghdr *cmh)
494 {
495         switch (cmh->cmsg_level) {
496         case SOL_IP:
497                 return svc_udp_get_dest_address4(rqstp, cmh);
498         case SOL_IPV6:
499                 return svc_udp_get_dest_address6(rqstp, cmh);
500         }
501
502         return 0;
503 }
504
505 /*
506  * Receive a datagram from a UDP socket.
507  */
508 static int svc_udp_recvfrom(struct svc_rqst *rqstp)
509 {
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;
513         struct sk_buff  *skb;
514         union {
515                 struct cmsghdr  hdr;
516                 long            all[SVC_PKTINFO_SPACE / sizeof(long)];
517         } buffer;
518         struct cmsghdr *cmh = &buffer.hdr;
519         struct msghdr msg = {
520                 .msg_name = svc_addr(rqstp),
521                 .msg_control = cmh,
522                 .msg_controllen = sizeof(buffer),
523                 .msg_flags = MSG_DONTWAIT,
524         };
525         size_t len;
526         int err;
527
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.
536              */
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);
540
541         clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
542         skb = NULL;
543         err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
544                              0, 0, MSG_PEEK | MSG_DONTWAIT);
545         if (err >= 0)
546                 skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err);
547
548         if (skb == NULL) {
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);
553                 }
554                 return 0;
555         }
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 */
562         }
563         svsk->sk_sk->sk_stamp = skb->tstamp;
564         set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
565
566         len  = skb->len - sizeof(struct udphdr);
567         rqstp->rq_arg.len = len;
568
569         rqstp->rq_prot = IPPROTO_UDP;
570
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);
574                 goto out_free;
575         }
576         rqstp->rq_daddrlen = svc_addr_len(svc_daddr(rqstp));
577
578         if (skb_is_nonlinear(skb)) {
579                 /* we have to copy */
580                 local_bh_disable();
581                 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
582                         local_bh_enable();
583                         /* checksum error */
584                         goto out_free;
585                 }
586                 local_bh_enable();
587                 skb_free_datagram_locked(svsk->sk_sk, skb);
588         } else {
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))
594                         goto out_free;
595                 rqstp->rq_xprt_ctxt = skb;
596         }
597
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;
603         } else {
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);
607         }
608
609         if (serv->sv_stats)
610                 serv->sv_stats->netudpcnt++;
611
612         return len;
613 out_free:
614         trace_kfree_skb(skb, svc_udp_recvfrom);
615         skb_free_datagram_locked(svsk->sk_sk, skb);
616         return 0;
617 }
618
619 static int
620 svc_udp_sendto(struct svc_rqst *rqstp)
621 {
622         int             error;
623
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);
628
629         return error;
630 }
631
632 static void svc_udp_prep_reply_hdr(struct svc_rqst *rqstp)
633 {
634 }
635
636 static int svc_udp_has_wspace(struct svc_xprt *xprt)
637 {
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;
641
642         /*
643          * Set the SOCK_NOSPACE flag before checking the available
644          * sock space.
645          */
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))
649                 return 0;
650         clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
651         return 1;
652 }
653
654 static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt)
655 {
656         BUG();
657         return NULL;
658 }
659
660 static struct svc_xprt *svc_udp_create(struct svc_serv *serv,
661                                        struct net *net,
662                                        struct sockaddr *sa, int salen,
663                                        int flags)
664 {
665         return svc_create_socket(serv, IPPROTO_UDP, net, sa, salen, flags);
666 }
667
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,
678 };
679
680 static struct svc_xprt_class svc_udp_class = {
681         .xcl_name = "udp",
682         .xcl_owner = THIS_MODULE,
683         .xcl_ops = &svc_udp_ops,
684         .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP,
685 };
686
687 static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
688 {
689         int err, level, optname, one = 1;
690
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;
696
697         /* initialise setting must have enough space to
698          * receive and respond to one request.
699          * svc_udp_recvfrom will re-adjust if necessary
700          */
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);
704
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);
708
709         /* make sure we get destination address info */
710         switch (svsk->sk_sk->sk_family) {
711         case AF_INET:
712                 level = SOL_IP;
713                 optname = IP_PKTINFO;
714                 break;
715         case AF_INET6:
716                 level = SOL_IPV6;
717                 optname = IPV6_RECVPKTINFO;
718                 break;
719         default:
720                 BUG();
721         }
722         err = kernel_setsockopt(svsk->sk_sock, level, optname,
723                                         (char *)&one, sizeof(one));
724         dprintk("svc: kernel_setsockopt returned %d\n", err);
725 }
726
727 /*
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.
730  */
731 static void svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
732 {
733         struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
734         wait_queue_head_t *wq;
735
736         dprintk("svc: socket %p TCP (listen) state change %d\n",
737                 sk, sk->sk_state);
738
739         /*
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.
748          */
749         if (sk->sk_state == TCP_LISTEN) {
750                 if (svsk) {
751                         set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
752                         svc_xprt_enqueue(&svsk->sk_xprt);
753                 } else
754                         printk("svc: socket %p: no user data\n", sk);
755         }
756
757         wq = sk_sleep(sk);
758         if (wq && waitqueue_active(wq))
759                 wake_up_interruptible_all(wq);
760 }
761
762 /*
763  * A state change on a connected socket means it's dying or dead.
764  */
765 static void svc_tcp_state_change(struct sock *sk)
766 {
767         struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
768         wait_queue_head_t *wq = sk_sleep(sk);
769
770         dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
771                 sk, sk->sk_state, sk->sk_user_data);
772
773         if (!svsk)
774                 printk("svc: socket %p: no user data\n", sk);
775         else {
776                 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
777                 svc_xprt_enqueue(&svsk->sk_xprt);
778         }
779         if (wq && waitqueue_active(wq))
780                 wake_up_interruptible_all(wq);
781 }
782
783 static void svc_tcp_data_ready(struct sock *sk, int count)
784 {
785         struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
786         wait_queue_head_t *wq = sk_sleep(sk);
787
788         dprintk("svc: socket %p TCP data ready (svsk %p)\n",
789                 sk, sk->sk_user_data);
790         if (svsk) {
791                 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
792                 svc_xprt_enqueue(&svsk->sk_xprt);
793         }
794         if (wq && waitqueue_active(wq))
795                 wake_up_interruptible(wq);
796 }
797
798 /*
799  * Accept a TCP connection
800  */
801 static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt)
802 {
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;
810         int             err, slen;
811         RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
812
813         dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
814         if (!sock)
815                 return NULL;
816
817         clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
818         err = kernel_accept(sock, &newsock, O_NONBLOCK);
819         if (err < 0) {
820                 if (err == -ENOMEM)
821                         printk(KERN_WARNING "%s: no more sockets!\n",
822                                serv->sv_name);
823                 else if (err != -EAGAIN)
824                         net_warn_ratelimited("%s: accept failed (err %d)!\n",
825                                              serv->sv_name, -err);
826                 return NULL;
827         }
828         set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
829
830         err = kernel_getpeername(newsock, sin, &slen);
831         if (err < 0) {
832                 net_warn_ratelimited("%s: peername failed (err %d)!\n",
833                                      serv->sv_name, -err);
834                 goto failed;            /* aborted connection or whatever */
835         }
836
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.
840          */
841         if (!svc_port_is_privileged(sin)) {
842                 dprintk(KERN_WARNING
843                         "%s: connect from unprivileged port: %s\n",
844                         serv->sv_name,
845                         __svc_print_addr(sin, buf, sizeof(buf)));
846         }
847         dprintk("%s: connect from %s\n", serv->sv_name,
848                 __svc_print_addr(sin, buf, sizeof(buf)));
849
850         /* make sure that a write doesn't block forever when
851          * low on memory
852          */
853         newsock->sk->sk_sndtimeo = HZ*30;
854
855         newsvsk = svc_setup_socket(serv, newsock,
856                                  (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY));
857         if (IS_ERR(newsvsk))
858                 goto failed;
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);
864         }
865         svc_xprt_set_local(&newsvsk->sk_xprt, sin, slen);
866
867         if (serv->sv_stats)
868                 serv->sv_stats->nettcpconn++;
869
870         return &newsvsk->sk_xprt;
871
872 failed:
873         sock_release(newsock);
874         return NULL;
875 }
876
877 static unsigned int svc_tcp_restore_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
878 {
879         unsigned int i, len, npages;
880
881         if (svsk->sk_tcplen <= sizeof(rpc_fraghdr))
882                 return 0;
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;
891         }
892         rqstp->rq_arg.head[0].iov_base = page_address(rqstp->rq_pages[0]);
893         return len;
894 }
895
896 static void svc_tcp_save_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
897 {
898         unsigned int i, len, npages;
899
900         if (svsk->sk_tcplen <= sizeof(rpc_fraghdr))
901                 return;
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;
907         }
908 }
909
910 static void svc_tcp_clear_pages(struct svc_sock *svsk)
911 {
912         unsigned int i, len, npages;
913
914         if (svsk->sk_tcplen <= sizeof(rpc_fraghdr))
915                 goto out;
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;
922         }
923 out:
924         svsk->sk_tcplen = 0;
925 }
926
927 /*
928  * Receive data.
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
931  * record length.
932  */
933 static int svc_tcp_recv_record(struct svc_sock *svsk, struct svc_rqst *rqstp)
934 {
935         struct svc_serv *serv = svsk->sk_xprt.xpt_server;
936         unsigned int want;
937         int len;
938
939         clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
940
941         if (svsk->sk_tcplen < sizeof(rpc_fraghdr)) {
942                 struct kvec     iov;
943
944                 want = sizeof(rpc_fraghdr) - svsk->sk_tcplen;
945                 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
946                 iov.iov_len  = want;
947                 if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
948                         goto error;
949                 svsk->sk_tcplen += len;
950
951                 if (len < want) {
952                         dprintk("svc: short recvfrom while reading record "
953                                 "length (%d of %d)\n", len, want);
954                         return -EAGAIN;
955                 }
956
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
963                          *  records. */
964                         net_notice_ratelimited("RPC: multiple fragments per record not supported\n");
965                         goto err_delete;
966                 }
967
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);
973                         goto err_delete;
974                 }
975         }
976
977         if (svsk->sk_reclen < 8)
978                 goto err_delete; /* client is nuts. */
979
980         len = svsk->sk_reclen;
981
982         return len;
983 error:
984         dprintk("RPC: TCP recv_record got %d\n", len);
985         return len;
986 err_delete:
987         set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
988         return -EAGAIN;
989 }
990
991 static int receive_cb_reply(struct svc_sock *svsk, struct svc_rqst *rqstp)
992 {
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;
997         __be32 xid;
998         __be32 calldir;
999
1000         xid = *p++;
1001         calldir = *p;
1002
1003         if (bc_xprt)
1004                 req = xprt_lookup_rqst(bc_xprt, xid);
1005
1006         if (!req) {
1007                 printk(KERN_NOTICE
1008                         "%s: Got unrecognized reply: "
1009                         "calldir 0x%x xpt_bc_xprt %p xid %08x\n",
1010                         __func__, ntohl(calldir),
1011                         bc_xprt, xid);
1012                 return -EAGAIN;
1013         }
1014
1015         memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
1016         /*
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).
1020          */
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;
1028         return 0;
1029 }
1030
1031 static int copy_pages_to_kvecs(struct kvec *vec, struct page **pages, int len)
1032 {
1033         int i = 0;
1034         int t = 0;
1035
1036         while (t < len) {
1037                 vec[i].iov_base = page_address(pages[i]);
1038                 vec[i].iov_len = PAGE_SIZE;
1039                 i++;
1040                 t += PAGE_SIZE;
1041         }
1042         return i;
1043 }
1044
1045
1046 /*
1047  * Receive data from a TCP socket.
1048  */
1049 static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
1050 {
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;
1054         int             len;
1055         struct kvec *vec;
1056         unsigned int want, base;
1057         __be32 *p;
1058         __be32 calldir;
1059         int pnum;
1060
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));
1065
1066         len = svc_tcp_recv_record(svsk, rqstp);
1067         if (len < 0)
1068                 goto error;
1069
1070         base = svc_tcp_restore_pages(svsk, rqstp);
1071         want = svsk->sk_reclen - base;
1072
1073         vec = rqstp->rq_vec;
1074
1075         pnum = copy_pages_to_kvecs(&vec[0], &rqstp->rq_pages[0],
1076                                                 svsk->sk_reclen);
1077
1078         rqstp->rq_respages = &rqstp->rq_pages[pnum];
1079
1080         /* Now receive data */
1081         len = svc_partial_recvfrom(rqstp, vec, pnum, want, base);
1082         if (len >= 0)
1083                 svsk->sk_tcplen += len;
1084         if (len != want) {
1085                 svc_tcp_save_pages(svsk, rqstp);
1086                 if (len < 0 && len != -EAGAIN)
1087                         goto err_other;
1088                 dprintk("svc: incomplete TCP record (%d of %d)\n",
1089                         svsk->sk_tcplen, svsk->sk_reclen);
1090                 goto err_noclose;
1091         }
1092
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;
1098         } else
1099                 rqstp->rq_arg.page_len = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
1100
1101         rqstp->rq_xprt_ctxt   = NULL;
1102         rqstp->rq_prot        = IPPROTO_TCP;
1103
1104         p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
1105         calldir = p[1];
1106         if (calldir)
1107                 len = receive_cb_reply(svsk, rqstp);
1108
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);
1115
1116         if (len < 0)
1117                 goto error;
1118
1119         svc_xprt_copy_addrs(rqstp, &svsk->sk_xprt);
1120         if (serv->sv_stats)
1121                 serv->sv_stats->nettcpcnt++;
1122
1123         dprintk("svc: TCP complete record (%d bytes)\n", rqstp->rq_arg.len);
1124         return rqstp->rq_arg.len;
1125
1126 error:
1127         if (len != -EAGAIN)
1128                 goto err_other;
1129         dprintk("RPC: TCP recvfrom got EAGAIN\n");
1130         return 0;
1131 err_other:
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);
1135 err_noclose:
1136         return 0;       /* record not complete */
1137 }
1138
1139 /*
1140  * Send out data on TCP socket.
1141  */
1142 static int svc_tcp_sendto(struct svc_rqst *rqstp)
1143 {
1144         struct xdr_buf  *xbufp = &rqstp->rq_res;
1145         int sent;
1146         __be32 reclen;
1147
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.
1151          */
1152         reclen = htonl(0x80000000|((xbufp->len ) - 4));
1153         memcpy(xbufp->head[0].iov_base, &reclen, 4);
1154
1155         sent = svc_sendto(rqstp, &rqstp->rq_res);
1156         if (sent != xbufp->len) {
1157                 printk(KERN_NOTICE
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",
1162                        sent, xbufp->len);
1163                 set_bit(XPT_CLOSE, &rqstp->rq_xprt->xpt_flags);
1164                 svc_xprt_enqueue(rqstp->rq_xprt);
1165                 sent = -EAGAIN;
1166         }
1167         return sent;
1168 }
1169
1170 /*
1171  * Setup response header. TCP has a 4B record length field.
1172  */
1173 static void svc_tcp_prep_reply_hdr(struct svc_rqst *rqstp)
1174 {
1175         struct kvec *resv = &rqstp->rq_res.head[0];
1176
1177         /* tcp needs a space for the record length... */
1178         svc_putnl(resv, 0);
1179 }
1180
1181 static int svc_tcp_has_wspace(struct svc_xprt *xprt)
1182 {
1183         struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1184         struct svc_serv *serv = svsk->sk_xprt.xpt_server;
1185         int required;
1186
1187         if (test_bit(XPT_LISTENER, &xprt->xpt_flags))
1188                 return 1;
1189         required = atomic_read(&xprt->xpt_reserved) + serv->sv_max_mesg;
1190         if (sk_stream_wspace(svsk->sk_sk) >= required)
1191                 return 1;
1192         set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
1193         return 0;
1194 }
1195
1196 static struct svc_xprt *svc_tcp_create(struct svc_serv *serv,
1197                                        struct net *net,
1198                                        struct sockaddr *sa, int salen,
1199                                        int flags)
1200 {
1201         return svc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
1202 }
1203
1204 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1205 static struct svc_xprt *svc_bc_create_socket(struct svc_serv *, int,
1206                                              struct net *, struct sockaddr *,
1207                                              int, int);
1208 static void svc_bc_sock_free(struct svc_xprt *xprt);
1209
1210 static struct svc_xprt *svc_bc_tcp_create(struct svc_serv *serv,
1211                                        struct net *net,
1212                                        struct sockaddr *sa, int salen,
1213                                        int flags)
1214 {
1215         return svc_bc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
1216 }
1217
1218 static void svc_bc_tcp_sock_detach(struct svc_xprt *xprt)
1219 {
1220 }
1221
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,
1227 };
1228
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,
1234 };
1235
1236 static void svc_init_bc_xprt_sock(void)
1237 {
1238         svc_reg_xprt_class(&svc_tcp_bc_class);
1239 }
1240
1241 static void svc_cleanup_bc_xprt_sock(void)
1242 {
1243         svc_unreg_xprt_class(&svc_tcp_bc_class);
1244 }
1245 #else /* CONFIG_SUNRPC_BACKCHANNEL */
1246 static void svc_init_bc_xprt_sock(void)
1247 {
1248 }
1249
1250 static void svc_cleanup_bc_xprt_sock(void)
1251 {
1252 }
1253 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1254
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,
1265 };
1266
1267 static struct svc_xprt_class svc_tcp_class = {
1268         .xcl_name = "tcp",
1269         .xcl_owner = THIS_MODULE,
1270         .xcl_ops = &svc_tcp_ops,
1271         .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
1272 };
1273
1274 void svc_init_xprt_sock(void)
1275 {
1276         svc_reg_xprt_class(&svc_tcp_class);
1277         svc_reg_xprt_class(&svc_udp_class);
1278         svc_init_bc_xprt_sock();
1279 }
1280
1281 void svc_cleanup_xprt_sock(void)
1282 {
1283         svc_unreg_xprt_class(&svc_tcp_class);
1284         svc_unreg_xprt_class(&svc_udp_class);
1285         svc_cleanup_bc_xprt_sock();
1286 }
1287
1288 static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
1289 {
1290         struct sock     *sk = svsk->sk_sk;
1291
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);
1300         } else {
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;
1305
1306                 svsk->sk_reclen = 0;
1307                 svsk->sk_tcplen = 0;
1308                 memset(&svsk->sk_pages[0], 0, sizeof(svsk->sk_pages));
1309
1310                 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
1311
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);
1315         }
1316 }
1317
1318 void svc_sock_update_bufs(struct svc_serv *serv)
1319 {
1320         /*
1321          * The number of server threads has changed. Update
1322          * rcvbuf and sndbuf accordingly on all sockets
1323          */
1324         struct svc_sock *svsk;
1325
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);
1330 }
1331 EXPORT_SYMBOL_GPL(svc_sock_update_bufs);
1332
1333 /*
1334  * Initialize socket for RPC use and create svc_sock struct
1335  * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
1336  */
1337 static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1338                                                 struct socket *sock,
1339                                                 int flags)
1340 {
1341         struct svc_sock *svsk;
1342         struct sock     *inet;
1343         int             pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
1344         int             err = 0;
1345
1346         dprintk("svc: svc_setup_socket %p\n", sock);
1347         svsk = kzalloc(sizeof(*svsk), GFP_KERNEL);
1348         if (!svsk)
1349                 return ERR_PTR(-ENOMEM);
1350
1351         inet = sock->sk;
1352
1353         /* Register socket with portmapper */
1354         if (pmap_register)
1355                 err = svc_register(serv, sock_net(sock->sk), inet->sk_family,
1356                                      inet->sk_protocol,
1357                                      ntohs(inet_sk(inet)->inet_sport));
1358
1359         if (err < 0) {
1360                 kfree(svsk);
1361                 return ERR_PTR(err);
1362         }
1363
1364         inet->sk_user_data = svsk;
1365         svsk->sk_sock = sock;
1366         svsk->sk_sk = inet;
1367         svsk->sk_ostate = inet->sk_state_change;
1368         svsk->sk_odata = inet->sk_data_ready;
1369         svsk->sk_owspace = inet->sk_write_space;
1370
1371         /* Initialize the socket */
1372         if (sock->type == SOCK_DGRAM)
1373                 svc_udp_init(svsk, serv);
1374         else {
1375                 /* initialise setting must have enough space to
1376                  * receive and respond to one request.
1377                  */
1378                 svc_sock_setbufsize(svsk->sk_sock, 4 * serv->sv_max_mesg,
1379                                         4 * serv->sv_max_mesg);
1380                 svc_tcp_init(svsk, serv);
1381         }
1382
1383         dprintk("svc: svc_setup_socket created %p (inet %p)\n",
1384                                 svsk, svsk->sk_sk);
1385
1386         return svsk;
1387 }
1388
1389 /**
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
1395  *
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
1398  * value.
1399  */
1400 int svc_addsock(struct svc_serv *serv, const int fd, char *name_return,
1401                 const size_t len)
1402 {
1403         int err = 0;
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;
1408         int salen;
1409
1410         if (!so)
1411                 return err;
1412         err = -EAFNOSUPPORT;
1413         if ((so->sk->sk_family != PF_INET) && (so->sk->sk_family != PF_INET6))
1414                 goto out;
1415         err =  -EPROTONOSUPPORT;
1416         if (so->sk->sk_protocol != IPPROTO_TCP &&
1417             so->sk->sk_protocol != IPPROTO_UDP)
1418                 goto out;
1419         err = -EISCONN;
1420         if (so->state > SS_UNCONNECTED)
1421                 goto out;
1422         err = -ENOENT;
1423         if (!try_module_get(THIS_MODULE))
1424                 goto out;
1425         svsk = svc_setup_socket(serv, so, SVC_SOCK_DEFAULTS);
1426         if (IS_ERR(svsk)) {
1427                 module_put(THIS_MODULE);
1428                 err = PTR_ERR(svsk);
1429                 goto out;
1430         }
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);
1435 out:
1436         sockfd_put(so);
1437         return err;
1438 }
1439 EXPORT_SYMBOL_GPL(svc_addsock);
1440
1441 /*
1442  * Create socket for RPC service.
1443  */
1444 static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
1445                                           int protocol,
1446                                           struct net *net,
1447                                           struct sockaddr *sin, int len,
1448                                           int flags)
1449 {
1450         struct svc_sock *svsk;
1451         struct socket   *sock;
1452         int             error;
1453         int             type;
1454         struct sockaddr_storage addr;
1455         struct sockaddr *newsin = (struct sockaddr *)&addr;
1456         int             newlen;
1457         int             family;
1458         int             val;
1459         RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
1460
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)));
1464
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);
1469         }
1470
1471         type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1472         switch (sin->sa_family) {
1473         case AF_INET6:
1474                 family = PF_INET6;
1475                 break;
1476         case AF_INET:
1477                 family = PF_INET;
1478                 break;
1479         default:
1480                 return ERR_PTR(-EINVAL);
1481         }
1482
1483         error = __sock_create(net, family, type, protocol, &sock, 1);
1484         if (error < 0)
1485                 return ERR_PTR(error);
1486
1487         svc_reclassify_socket(sock);
1488
1489         /*
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.
1493          */
1494         val = 1;
1495         if (family == PF_INET6)
1496                 kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
1497                                         (char *)&val, sizeof(val));
1498
1499         if (type == SOCK_STREAM)
1500                 sock->sk->sk_reuse = SK_CAN_REUSE; /* allow address reuse */
1501         error = kernel_bind(sock, sin, len);
1502         if (error < 0)
1503                 goto bummer;
1504
1505         newlen = len;
1506         error = kernel_getsockname(sock, newsin, &newlen);
1507         if (error < 0)
1508                 goto bummer;
1509
1510         if (protocol == IPPROTO_TCP) {
1511                 if ((error = kernel_listen(sock, 64)) < 0)
1512                         goto bummer;
1513         }
1514
1515         svsk = svc_setup_socket(serv, sock, flags);
1516         if (IS_ERR(svsk)) {
1517                 error = PTR_ERR(svsk);
1518                 goto bummer;
1519         }
1520         svc_xprt_set_local(&svsk->sk_xprt, newsin, newlen);
1521         return (struct svc_xprt *)svsk;
1522 bummer:
1523         dprintk("svc: svc_create_socket error = %d\n", -error);
1524         sock_release(sock);
1525         return ERR_PTR(error);
1526 }
1527
1528 /*
1529  * Detach the svc_sock from the socket so that no
1530  * more callbacks occur.
1531  */
1532 static void svc_sock_detach(struct svc_xprt *xprt)
1533 {
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;
1537
1538         dprintk("svc: svc_sock_detach(%p)\n", svsk);
1539
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;
1544
1545         wq = sk_sleep(sk);
1546         if (wq && waitqueue_active(wq))
1547                 wake_up_interruptible(wq);
1548 }
1549
1550 /*
1551  * Disconnect the socket, and reset the callbacks
1552  */
1553 static void svc_tcp_sock_detach(struct svc_xprt *xprt)
1554 {
1555         struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1556
1557         dprintk("svc: svc_tcp_sock_detach(%p)\n", svsk);
1558
1559         svc_sock_detach(xprt);
1560
1561         if (!test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
1562                 svc_tcp_clear_pages(svsk);
1563                 kernel_sock_shutdown(svsk->sk_sock, SHUT_RDWR);
1564         }
1565 }
1566
1567 /*
1568  * Free the svc_sock's socket resources and the svc_sock itself.
1569  */
1570 static void svc_sock_free(struct svc_xprt *xprt)
1571 {
1572         struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1573         dprintk("svc: svc_sock_free(%p)\n", svsk);
1574
1575         if (svsk->sk_sock->file)
1576                 sockfd_put(svsk->sk_sock);
1577         else
1578                 sock_release(svsk->sk_sock);
1579         kfree(svsk);
1580 }
1581
1582 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1583 /*
1584  * Create a back channel svc_xprt which shares the fore channel socket.
1585  */
1586 static struct svc_xprt *svc_bc_create_socket(struct svc_serv *serv,
1587                                              int protocol,
1588                                              struct net *net,
1589                                              struct sockaddr *sin, int len,
1590                                              int flags)
1591 {
1592         struct svc_sock *svsk;
1593         struct svc_xprt *xprt;
1594
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);
1599         }
1600
1601         svsk = kzalloc(sizeof(*svsk), GFP_KERNEL);
1602         if (!svsk)
1603                 return ERR_PTR(-ENOMEM);
1604
1605         xprt = &svsk->sk_xprt;
1606         svc_xprt_init(net, &svc_tcp_bc_class, xprt, serv);
1607
1608         serv->sv_bc_xprt = xprt;
1609
1610         return xprt;
1611 }
1612
1613 /*
1614  * Free a back channel svc_sock.
1615  */
1616 static void svc_bc_sock_free(struct svc_xprt *xprt)
1617 {
1618         if (xprt)
1619                 kfree(container_of(xprt, struct svc_sock, sk_xprt));
1620 }
1621 #endif /* CONFIG_SUNRPC_BACKCHANNEL */