]> Git Repo - linux.git/blob - net/sunrpc/svcsock.c
SUNRPC: Don't wait for full record to receive tcp data
[linux.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 <[email protected]>
20  */
21
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/errno.h>
25 #include <linux/fcntl.h>
26 #include <linux/net.h>
27 #include <linux/in.h>
28 #include <linux/inet.h>
29 #include <linux/udp.h>
30 #include <linux/tcp.h>
31 #include <linux/unistd.h>
32 #include <linux/slab.h>
33 #include <linux/netdevice.h>
34 #include <linux/skbuff.h>
35 #include <linux/file.h>
36 #include <linux/freezer.h>
37 #include <net/sock.h>
38 #include <net/checksum.h>
39 #include <net/ip.h>
40 #include <net/ipv6.h>
41 #include <net/tcp.h>
42 #include <net/tcp_states.h>
43 #include <asm/uaccess.h>
44 #include <asm/ioctls.h>
45
46 #include <linux/sunrpc/types.h>
47 #include <linux/sunrpc/clnt.h>
48 #include <linux/sunrpc/xdr.h>
49 #include <linux/sunrpc/msg_prot.h>
50 #include <linux/sunrpc/svcsock.h>
51 #include <linux/sunrpc/stats.h>
52 #include <linux/sunrpc/xprt.h>
53
54 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
55
56
57 static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
58                                          int *errp, int flags);
59 static void             svc_udp_data_ready(struct sock *, int);
60 static int              svc_udp_recvfrom(struct svc_rqst *);
61 static int              svc_udp_sendto(struct svc_rqst *);
62 static void             svc_sock_detach(struct svc_xprt *);
63 static void             svc_tcp_sock_detach(struct svc_xprt *);
64 static void             svc_sock_free(struct svc_xprt *);
65
66 static struct svc_xprt *svc_create_socket(struct svc_serv *, int,
67                                           struct net *, struct sockaddr *,
68                                           int, int);
69 #if defined(CONFIG_NFS_V4_1)
70 static struct svc_xprt *svc_bc_create_socket(struct svc_serv *, int,
71                                              struct net *, struct sockaddr *,
72                                              int, int);
73 static void svc_bc_sock_free(struct svc_xprt *xprt);
74 #endif /* CONFIG_NFS_V4_1 */
75
76 #ifdef CONFIG_DEBUG_LOCK_ALLOC
77 static struct lock_class_key svc_key[2];
78 static struct lock_class_key svc_slock_key[2];
79
80 static void svc_reclassify_socket(struct socket *sock)
81 {
82         struct sock *sk = sock->sk;
83         BUG_ON(sock_owned_by_user(sk));
84         switch (sk->sk_family) {
85         case AF_INET:
86                 sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
87                                               &svc_slock_key[0],
88                                               "sk_xprt.xpt_lock-AF_INET-NFSD",
89                                               &svc_key[0]);
90                 break;
91
92         case AF_INET6:
93                 sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
94                                               &svc_slock_key[1],
95                                               "sk_xprt.xpt_lock-AF_INET6-NFSD",
96                                               &svc_key[1]);
97                 break;
98
99         default:
100                 BUG();
101         }
102 }
103 #else
104 static void svc_reclassify_socket(struct socket *sock)
105 {
106 }
107 #endif
108
109 /*
110  * Release an skbuff after use
111  */
112 static void svc_release_skb(struct svc_rqst *rqstp)
113 {
114         struct sk_buff *skb = rqstp->rq_xprt_ctxt;
115
116         if (skb) {
117                 struct svc_sock *svsk =
118                         container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
119                 rqstp->rq_xprt_ctxt = NULL;
120
121                 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
122                 skb_free_datagram_locked(svsk->sk_sk, skb);
123         }
124 }
125
126 union svc_pktinfo_u {
127         struct in_pktinfo pkti;
128         struct in6_pktinfo pkti6;
129 };
130 #define SVC_PKTINFO_SPACE \
131         CMSG_SPACE(sizeof(union svc_pktinfo_u))
132
133 static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
134 {
135         struct svc_sock *svsk =
136                 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
137         switch (svsk->sk_sk->sk_family) {
138         case AF_INET: {
139                         struct in_pktinfo *pki = CMSG_DATA(cmh);
140
141                         cmh->cmsg_level = SOL_IP;
142                         cmh->cmsg_type = IP_PKTINFO;
143                         pki->ipi_ifindex = 0;
144                         pki->ipi_spec_dst.s_addr = rqstp->rq_daddr.addr.s_addr;
145                         cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
146                 }
147                 break;
148
149         case AF_INET6: {
150                         struct in6_pktinfo *pki = CMSG_DATA(cmh);
151
152                         cmh->cmsg_level = SOL_IPV6;
153                         cmh->cmsg_type = IPV6_PKTINFO;
154                         pki->ipi6_ifindex = 0;
155                         ipv6_addr_copy(&pki->ipi6_addr,
156                                         &rqstp->rq_daddr.addr6);
157                         cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
158                 }
159                 break;
160         }
161 }
162
163 /*
164  * send routine intended to be shared by the fore- and back-channel
165  */
166 int svc_send_common(struct socket *sock, struct xdr_buf *xdr,
167                     struct page *headpage, unsigned long headoffset,
168                     struct page *tailpage, unsigned long tailoffset)
169 {
170         int             result;
171         int             size;
172         struct page     **ppage = xdr->pages;
173         size_t          base = xdr->page_base;
174         unsigned int    pglen = xdr->page_len;
175         unsigned int    flags = MSG_MORE;
176         int             slen;
177         int             len = 0;
178
179         slen = xdr->len;
180
181         /* send head */
182         if (slen == xdr->head[0].iov_len)
183                 flags = 0;
184         len = kernel_sendpage(sock, headpage, headoffset,
185                                   xdr->head[0].iov_len, flags);
186         if (len != xdr->head[0].iov_len)
187                 goto out;
188         slen -= xdr->head[0].iov_len;
189         if (slen == 0)
190                 goto out;
191
192         /* send page data */
193         size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
194         while (pglen > 0) {
195                 if (slen == size)
196                         flags = 0;
197                 result = kernel_sendpage(sock, *ppage, base, size, flags);
198                 if (result > 0)
199                         len += result;
200                 if (result != size)
201                         goto out;
202                 slen -= size;
203                 pglen -= size;
204                 size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
205                 base = 0;
206                 ppage++;
207         }
208
209         /* send tail */
210         if (xdr->tail[0].iov_len) {
211                 result = kernel_sendpage(sock, tailpage, tailoffset,
212                                    xdr->tail[0].iov_len, 0);
213                 if (result > 0)
214                         len += result;
215         }
216
217 out:
218         return len;
219 }
220
221
222 /*
223  * Generic sendto routine
224  */
225 static int svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
226 {
227         struct svc_sock *svsk =
228                 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
229         struct socket   *sock = svsk->sk_sock;
230         union {
231                 struct cmsghdr  hdr;
232                 long            all[SVC_PKTINFO_SPACE / sizeof(long)];
233         } buffer;
234         struct cmsghdr *cmh = &buffer.hdr;
235         int             len = 0;
236         unsigned long tailoff;
237         unsigned long headoff;
238         RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
239
240         if (rqstp->rq_prot == IPPROTO_UDP) {
241                 struct msghdr msg = {
242                         .msg_name       = &rqstp->rq_addr,
243                         .msg_namelen    = rqstp->rq_addrlen,
244                         .msg_control    = cmh,
245                         .msg_controllen = sizeof(buffer),
246                         .msg_flags      = MSG_MORE,
247                 };
248
249                 svc_set_cmsg_data(rqstp, cmh);
250
251                 if (sock_sendmsg(sock, &msg, 0) < 0)
252                         goto out;
253         }
254
255         tailoff = ((unsigned long)xdr->tail[0].iov_base) & (PAGE_SIZE-1);
256         headoff = 0;
257         len = svc_send_common(sock, xdr, rqstp->rq_respages[0], headoff,
258                                rqstp->rq_respages[0], tailoff);
259
260 out:
261         dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %s)\n",
262                 svsk, xdr->head[0].iov_base, xdr->head[0].iov_len,
263                 xdr->len, len, svc_print_addr(rqstp, buf, sizeof(buf)));
264
265         return len;
266 }
267
268 /*
269  * Report socket names for nfsdfs
270  */
271 static int svc_one_sock_name(struct svc_sock *svsk, char *buf, int remaining)
272 {
273         const struct sock *sk = svsk->sk_sk;
274         const char *proto_name = sk->sk_protocol == IPPROTO_UDP ?
275                                                         "udp" : "tcp";
276         int len;
277
278         switch (sk->sk_family) {
279         case PF_INET:
280                 len = snprintf(buf, remaining, "ipv4 %s %pI4 %d\n",
281                                 proto_name,
282                                 &inet_sk(sk)->inet_rcv_saddr,
283                                 inet_sk(sk)->inet_num);
284                 break;
285         case PF_INET6:
286                 len = snprintf(buf, remaining, "ipv6 %s %pI6 %d\n",
287                                 proto_name,
288                                 &inet6_sk(sk)->rcv_saddr,
289                                 inet_sk(sk)->inet_num);
290                 break;
291         default:
292                 len = snprintf(buf, remaining, "*unknown-%d*\n",
293                                 sk->sk_family);
294         }
295
296         if (len >= remaining) {
297                 *buf = '\0';
298                 return -ENAMETOOLONG;
299         }
300         return len;
301 }
302
303 /**
304  * svc_sock_names - construct a list of listener names in a string
305  * @serv: pointer to RPC service
306  * @buf: pointer to a buffer to fill in with socket names
307  * @buflen: size of the buffer to be filled
308  * @toclose: pointer to '\0'-terminated C string containing the name
309  *              of a listener to be closed
310  *
311  * Fills in @buf with a '\n'-separated list of names of listener
312  * sockets.  If @toclose is not NULL, the socket named by @toclose
313  * is closed, and is not included in the output list.
314  *
315  * Returns positive length of the socket name string, or a negative
316  * errno value on error.
317  */
318 int svc_sock_names(struct svc_serv *serv, char *buf, const size_t buflen,
319                    const char *toclose)
320 {
321         struct svc_sock *svsk, *closesk = NULL;
322         int len = 0;
323
324         if (!serv)
325                 return 0;
326
327         spin_lock_bh(&serv->sv_lock);
328         list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list) {
329                 int onelen = svc_one_sock_name(svsk, buf + len, buflen - len);
330                 if (onelen < 0) {
331                         len = onelen;
332                         break;
333                 }
334                 if (toclose && strcmp(toclose, buf + len) == 0) {
335                         closesk = svsk;
336                         svc_xprt_get(&closesk->sk_xprt);
337                 } else
338                         len += onelen;
339         }
340         spin_unlock_bh(&serv->sv_lock);
341
342         if (closesk) {
343                 /* Should unregister with portmap, but you cannot
344                  * unregister just one protocol...
345                  */
346                 svc_close_xprt(&closesk->sk_xprt);
347                 svc_xprt_put(&closesk->sk_xprt);
348         } else if (toclose)
349                 return -ENOENT;
350         return len;
351 }
352 EXPORT_SYMBOL_GPL(svc_sock_names);
353
354 /*
355  * Check input queue length
356  */
357 static int svc_recv_available(struct svc_sock *svsk)
358 {
359         struct socket   *sock = svsk->sk_sock;
360         int             avail, err;
361
362         err = kernel_sock_ioctl(sock, TIOCINQ, (unsigned long) &avail);
363
364         return (err >= 0)? avail : err;
365 }
366
367 /*
368  * Generic recvfrom routine.
369  */
370 static int svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr,
371                         int buflen)
372 {
373         struct svc_sock *svsk =
374                 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
375         struct msghdr msg = {
376                 .msg_flags      = MSG_DONTWAIT,
377         };
378         int len;
379
380         rqstp->rq_xprt_hlen = 0;
381
382         len = kernel_recvmsg(svsk->sk_sock, &msg, iov, nr, buflen,
383                                 msg.msg_flags);
384
385         dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
386                 svsk, iov[0].iov_base, iov[0].iov_len, len);
387         return len;
388 }
389
390 static int svc_partial_recvfrom(struct svc_rqst *rqstp,
391                                 struct kvec *iov, int nr,
392                                 int buflen, unsigned int base)
393 {
394         size_t save_iovlen;
395         void __user *save_iovbase;
396         unsigned int i;
397         int ret;
398
399         if (base == 0)
400                 return svc_recvfrom(rqstp, iov, nr, buflen);
401
402         for (i = 0; i < nr; i++) {
403                 if (iov[i].iov_len > base)
404                         break;
405                 base -= iov[i].iov_len;
406         }
407         save_iovlen = iov[i].iov_len;
408         save_iovbase = iov[i].iov_base;
409         iov[i].iov_len -= base;
410         iov[i].iov_base += base;
411         ret = svc_recvfrom(rqstp, &iov[i], nr - i, buflen);
412         iov[i].iov_len = save_iovlen;
413         iov[i].iov_base = save_iovbase;
414         return ret;
415 }
416
417 /*
418  * Set socket snd and rcv buffer lengths
419  */
420 static void svc_sock_setbufsize(struct socket *sock, unsigned int snd,
421                                 unsigned int rcv)
422 {
423 #if 0
424         mm_segment_t    oldfs;
425         oldfs = get_fs(); set_fs(KERNEL_DS);
426         sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
427                         (char*)&snd, sizeof(snd));
428         sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
429                         (char*)&rcv, sizeof(rcv));
430 #else
431         /* sock_setsockopt limits use to sysctl_?mem_max,
432          * which isn't acceptable.  Until that is made conditional
433          * on not having CAP_SYS_RESOURCE or similar, we go direct...
434          * DaveM said I could!
435          */
436         lock_sock(sock->sk);
437         sock->sk->sk_sndbuf = snd * 2;
438         sock->sk->sk_rcvbuf = rcv * 2;
439         sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK|SOCK_RCVBUF_LOCK;
440         sock->sk->sk_write_space(sock->sk);
441         release_sock(sock->sk);
442 #endif
443 }
444 /*
445  * INET callback when data has been received on the socket.
446  */
447 static void svc_udp_data_ready(struct sock *sk, int count)
448 {
449         struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
450         wait_queue_head_t *wq = sk_sleep(sk);
451
452         if (svsk) {
453                 dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
454                         svsk, sk, count,
455                         test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
456                 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
457                 svc_xprt_enqueue(&svsk->sk_xprt);
458         }
459         if (wq && waitqueue_active(wq))
460                 wake_up_interruptible(wq);
461 }
462
463 /*
464  * INET callback when space is newly available on the socket.
465  */
466 static void svc_write_space(struct sock *sk)
467 {
468         struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
469         wait_queue_head_t *wq = sk_sleep(sk);
470
471         if (svsk) {
472                 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
473                         svsk, sk, test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
474                 svc_xprt_enqueue(&svsk->sk_xprt);
475         }
476
477         if (wq && waitqueue_active(wq)) {
478                 dprintk("RPC svc_write_space: someone sleeping on %p\n",
479                        svsk);
480                 wake_up_interruptible(wq);
481         }
482 }
483
484 static void svc_tcp_write_space(struct sock *sk)
485 {
486         struct socket *sock = sk->sk_socket;
487
488         if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk) && sock)
489                 clear_bit(SOCK_NOSPACE, &sock->flags);
490         svc_write_space(sk);
491 }
492
493 /*
494  * See net/ipv6/ip_sockglue.c : ip_cmsg_recv_pktinfo
495  */
496 static int svc_udp_get_dest_address4(struct svc_rqst *rqstp,
497                                      struct cmsghdr *cmh)
498 {
499         struct in_pktinfo *pki = CMSG_DATA(cmh);
500         if (cmh->cmsg_type != IP_PKTINFO)
501                 return 0;
502         rqstp->rq_daddr.addr.s_addr = pki->ipi_spec_dst.s_addr;
503         return 1;
504 }
505
506 /*
507  * See net/ipv6/datagram.c : datagram_recv_ctl
508  */
509 static int svc_udp_get_dest_address6(struct svc_rqst *rqstp,
510                                      struct cmsghdr *cmh)
511 {
512         struct in6_pktinfo *pki = CMSG_DATA(cmh);
513         if (cmh->cmsg_type != IPV6_PKTINFO)
514                 return 0;
515         ipv6_addr_copy(&rqstp->rq_daddr.addr6, &pki->ipi6_addr);
516         return 1;
517 }
518
519 /*
520  * Copy the UDP datagram's destination address to the rqstp structure.
521  * The 'destination' address in this case is the address to which the
522  * peer sent the datagram, i.e. our local address. For multihomed
523  * hosts, this can change from msg to msg. Note that only the IP
524  * address changes, the port number should remain the same.
525  */
526 static int svc_udp_get_dest_address(struct svc_rqst *rqstp,
527                                     struct cmsghdr *cmh)
528 {
529         switch (cmh->cmsg_level) {
530         case SOL_IP:
531                 return svc_udp_get_dest_address4(rqstp, cmh);
532         case SOL_IPV6:
533                 return svc_udp_get_dest_address6(rqstp, cmh);
534         }
535
536         return 0;
537 }
538
539 /*
540  * Receive a datagram from a UDP socket.
541  */
542 static int svc_udp_recvfrom(struct svc_rqst *rqstp)
543 {
544         struct svc_sock *svsk =
545                 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
546         struct svc_serv *serv = svsk->sk_xprt.xpt_server;
547         struct sk_buff  *skb;
548         union {
549                 struct cmsghdr  hdr;
550                 long            all[SVC_PKTINFO_SPACE / sizeof(long)];
551         } buffer;
552         struct cmsghdr *cmh = &buffer.hdr;
553         struct msghdr msg = {
554                 .msg_name = svc_addr(rqstp),
555                 .msg_control = cmh,
556                 .msg_controllen = sizeof(buffer),
557                 .msg_flags = MSG_DONTWAIT,
558         };
559         size_t len;
560         int err;
561
562         if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
563             /* udp sockets need large rcvbuf as all pending
564              * requests are still in that buffer.  sndbuf must
565              * also be large enough that there is enough space
566              * for one reply per thread.  We count all threads
567              * rather than threads in a particular pool, which
568              * provides an upper bound on the number of threads
569              * which will access the socket.
570              */
571             svc_sock_setbufsize(svsk->sk_sock,
572                                 (serv->sv_nrthreads+3) * serv->sv_max_mesg,
573                                 (serv->sv_nrthreads+3) * serv->sv_max_mesg);
574
575         clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
576         skb = NULL;
577         err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
578                              0, 0, MSG_PEEK | MSG_DONTWAIT);
579         if (err >= 0)
580                 skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err);
581
582         if (skb == NULL) {
583                 if (err != -EAGAIN) {
584                         /* possibly an icmp error */
585                         dprintk("svc: recvfrom returned error %d\n", -err);
586                         set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
587                 }
588                 return -EAGAIN;
589         }
590         len = svc_addr_len(svc_addr(rqstp));
591         if (len == 0)
592                 return -EAFNOSUPPORT;
593         rqstp->rq_addrlen = len;
594         if (skb->tstamp.tv64 == 0) {
595                 skb->tstamp = ktime_get_real();
596                 /* Don't enable netstamp, sunrpc doesn't
597                    need that much accuracy */
598         }
599         svsk->sk_sk->sk_stamp = skb->tstamp;
600         set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
601
602         len  = skb->len - sizeof(struct udphdr);
603         rqstp->rq_arg.len = len;
604
605         rqstp->rq_prot = IPPROTO_UDP;
606
607         if (!svc_udp_get_dest_address(rqstp, cmh)) {
608                 if (net_ratelimit())
609                         printk(KERN_WARNING
610                                 "svc: received unknown control message %d/%d; "
611                                 "dropping RPC reply datagram\n",
612                                         cmh->cmsg_level, cmh->cmsg_type);
613                 skb_free_datagram_locked(svsk->sk_sk, skb);
614                 return 0;
615         }
616
617         if (skb_is_nonlinear(skb)) {
618                 /* we have to copy */
619                 local_bh_disable();
620                 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
621                         local_bh_enable();
622                         /* checksum error */
623                         skb_free_datagram_locked(svsk->sk_sk, skb);
624                         return 0;
625                 }
626                 local_bh_enable();
627                 skb_free_datagram_locked(svsk->sk_sk, skb);
628         } else {
629                 /* we can use it in-place */
630                 rqstp->rq_arg.head[0].iov_base = skb->data +
631                         sizeof(struct udphdr);
632                 rqstp->rq_arg.head[0].iov_len = len;
633                 if (skb_checksum_complete(skb)) {
634                         skb_free_datagram_locked(svsk->sk_sk, skb);
635                         return 0;
636                 }
637                 rqstp->rq_xprt_ctxt = skb;
638         }
639
640         rqstp->rq_arg.page_base = 0;
641         if (len <= rqstp->rq_arg.head[0].iov_len) {
642                 rqstp->rq_arg.head[0].iov_len = len;
643                 rqstp->rq_arg.page_len = 0;
644                 rqstp->rq_respages = rqstp->rq_pages+1;
645         } else {
646                 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
647                 rqstp->rq_respages = rqstp->rq_pages + 1 +
648                         DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE);
649         }
650
651         if (serv->sv_stats)
652                 serv->sv_stats->netudpcnt++;
653
654         return len;
655 }
656
657 static int
658 svc_udp_sendto(struct svc_rqst *rqstp)
659 {
660         int             error;
661
662         error = svc_sendto(rqstp, &rqstp->rq_res);
663         if (error == -ECONNREFUSED)
664                 /* ICMP error on earlier request. */
665                 error = svc_sendto(rqstp, &rqstp->rq_res);
666
667         return error;
668 }
669
670 static void svc_udp_prep_reply_hdr(struct svc_rqst *rqstp)
671 {
672 }
673
674 static int svc_udp_has_wspace(struct svc_xprt *xprt)
675 {
676         struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
677         struct svc_serv *serv = xprt->xpt_server;
678         unsigned long required;
679
680         /*
681          * Set the SOCK_NOSPACE flag before checking the available
682          * sock space.
683          */
684         set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
685         required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
686         if (required*2 > sock_wspace(svsk->sk_sk))
687                 return 0;
688         clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
689         return 1;
690 }
691
692 static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt)
693 {
694         BUG();
695         return NULL;
696 }
697
698 static struct svc_xprt *svc_udp_create(struct svc_serv *serv,
699                                        struct net *net,
700                                        struct sockaddr *sa, int salen,
701                                        int flags)
702 {
703         return svc_create_socket(serv, IPPROTO_UDP, net, sa, salen, flags);
704 }
705
706 static struct svc_xprt_ops svc_udp_ops = {
707         .xpo_create = svc_udp_create,
708         .xpo_recvfrom = svc_udp_recvfrom,
709         .xpo_sendto = svc_udp_sendto,
710         .xpo_release_rqst = svc_release_skb,
711         .xpo_detach = svc_sock_detach,
712         .xpo_free = svc_sock_free,
713         .xpo_prep_reply_hdr = svc_udp_prep_reply_hdr,
714         .xpo_has_wspace = svc_udp_has_wspace,
715         .xpo_accept = svc_udp_accept,
716 };
717
718 static struct svc_xprt_class svc_udp_class = {
719         .xcl_name = "udp",
720         .xcl_owner = THIS_MODULE,
721         .xcl_ops = &svc_udp_ops,
722         .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP,
723 };
724
725 static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
726 {
727         int err, level, optname, one = 1;
728
729         svc_xprt_init(&svc_udp_class, &svsk->sk_xprt, serv);
730         clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
731         svsk->sk_sk->sk_data_ready = svc_udp_data_ready;
732         svsk->sk_sk->sk_write_space = svc_write_space;
733
734         /* initialise setting must have enough space to
735          * receive and respond to one request.
736          * svc_udp_recvfrom will re-adjust if necessary
737          */
738         svc_sock_setbufsize(svsk->sk_sock,
739                             3 * svsk->sk_xprt.xpt_server->sv_max_mesg,
740                             3 * svsk->sk_xprt.xpt_server->sv_max_mesg);
741
742         /* data might have come in before data_ready set up */
743         set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
744         set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
745
746         /* make sure we get destination address info */
747         switch (svsk->sk_sk->sk_family) {
748         case AF_INET:
749                 level = SOL_IP;
750                 optname = IP_PKTINFO;
751                 break;
752         case AF_INET6:
753                 level = SOL_IPV6;
754                 optname = IPV6_RECVPKTINFO;
755                 break;
756         default:
757                 BUG();
758         }
759         err = kernel_setsockopt(svsk->sk_sock, level, optname,
760                                         (char *)&one, sizeof(one));
761         dprintk("svc: kernel_setsockopt returned %d\n", err);
762 }
763
764 /*
765  * A data_ready event on a listening socket means there's a connection
766  * pending. Do not use state_change as a substitute for it.
767  */
768 static void svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
769 {
770         struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
771         wait_queue_head_t *wq;
772
773         dprintk("svc: socket %p TCP (listen) state change %d\n",
774                 sk, sk->sk_state);
775
776         /*
777          * This callback may called twice when a new connection
778          * is established as a child socket inherits everything
779          * from a parent LISTEN socket.
780          * 1) data_ready method of the parent socket will be called
781          *    when one of child sockets become ESTABLISHED.
782          * 2) data_ready method of the child socket may be called
783          *    when it receives data before the socket is accepted.
784          * In case of 2, we should ignore it silently.
785          */
786         if (sk->sk_state == TCP_LISTEN) {
787                 if (svsk) {
788                         set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
789                         svc_xprt_enqueue(&svsk->sk_xprt);
790                 } else
791                         printk("svc: socket %p: no user data\n", sk);
792         }
793
794         wq = sk_sleep(sk);
795         if (wq && waitqueue_active(wq))
796                 wake_up_interruptible_all(wq);
797 }
798
799 /*
800  * A state change on a connected socket means it's dying or dead.
801  */
802 static void svc_tcp_state_change(struct sock *sk)
803 {
804         struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
805         wait_queue_head_t *wq = sk_sleep(sk);
806
807         dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
808                 sk, sk->sk_state, sk->sk_user_data);
809
810         if (!svsk)
811                 printk("svc: socket %p: no user data\n", sk);
812         else {
813                 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
814                 svc_xprt_enqueue(&svsk->sk_xprt);
815         }
816         if (wq && waitqueue_active(wq))
817                 wake_up_interruptible_all(wq);
818 }
819
820 static void svc_tcp_data_ready(struct sock *sk, int count)
821 {
822         struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
823         wait_queue_head_t *wq = sk_sleep(sk);
824
825         dprintk("svc: socket %p TCP data ready (svsk %p)\n",
826                 sk, sk->sk_user_data);
827         if (svsk) {
828                 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
829                 svc_xprt_enqueue(&svsk->sk_xprt);
830         }
831         if (wq && waitqueue_active(wq))
832                 wake_up_interruptible(wq);
833 }
834
835 /*
836  * Accept a TCP connection
837  */
838 static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt)
839 {
840         struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
841         struct sockaddr_storage addr;
842         struct sockaddr *sin = (struct sockaddr *) &addr;
843         struct svc_serv *serv = svsk->sk_xprt.xpt_server;
844         struct socket   *sock = svsk->sk_sock;
845         struct socket   *newsock;
846         struct svc_sock *newsvsk;
847         int             err, slen;
848         RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
849
850         dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
851         if (!sock)
852                 return NULL;
853
854         clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
855         err = kernel_accept(sock, &newsock, O_NONBLOCK);
856         if (err < 0) {
857                 if (err == -ENOMEM)
858                         printk(KERN_WARNING "%s: no more sockets!\n",
859                                serv->sv_name);
860                 else if (err != -EAGAIN && net_ratelimit())
861                         printk(KERN_WARNING "%s: accept failed (err %d)!\n",
862                                    serv->sv_name, -err);
863                 return NULL;
864         }
865         set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
866
867         err = kernel_getpeername(newsock, sin, &slen);
868         if (err < 0) {
869                 if (net_ratelimit())
870                         printk(KERN_WARNING "%s: peername failed (err %d)!\n",
871                                    serv->sv_name, -err);
872                 goto failed;            /* aborted connection or whatever */
873         }
874
875         /* Ideally, we would want to reject connections from unauthorized
876          * hosts here, but when we get encryption, the IP of the host won't
877          * tell us anything.  For now just warn about unpriv connections.
878          */
879         if (!svc_port_is_privileged(sin)) {
880                 dprintk(KERN_WARNING
881                         "%s: connect from unprivileged port: %s\n",
882                         serv->sv_name,
883                         __svc_print_addr(sin, buf, sizeof(buf)));
884         }
885         dprintk("%s: connect from %s\n", serv->sv_name,
886                 __svc_print_addr(sin, buf, sizeof(buf)));
887
888         /* make sure that a write doesn't block forever when
889          * low on memory
890          */
891         newsock->sk->sk_sndtimeo = HZ*30;
892
893         if (!(newsvsk = svc_setup_socket(serv, newsock, &err,
894                                  (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY))))
895                 goto failed;
896         svc_xprt_set_remote(&newsvsk->sk_xprt, sin, slen);
897         err = kernel_getsockname(newsock, sin, &slen);
898         if (unlikely(err < 0)) {
899                 dprintk("svc_tcp_accept: kernel_getsockname error %d\n", -err);
900                 slen = offsetof(struct sockaddr, sa_data);
901         }
902         svc_xprt_set_local(&newsvsk->sk_xprt, sin, slen);
903
904         if (serv->sv_stats)
905                 serv->sv_stats->nettcpconn++;
906
907         return &newsvsk->sk_xprt;
908
909 failed:
910         sock_release(newsock);
911         return NULL;
912 }
913
914 static unsigned int svc_tcp_restore_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
915 {
916         unsigned int i, len, npages;
917
918         if (svsk->sk_tcplen <= sizeof(rpc_fraghdr))
919                 return 0;
920         len = svsk->sk_tcplen - sizeof(rpc_fraghdr);
921         npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
922         for (i = 0; i < npages; i++) {
923                 if (rqstp->rq_pages[i] != NULL)
924                         put_page(rqstp->rq_pages[i]);
925                 BUG_ON(svsk->sk_pages[i] == NULL);
926                 rqstp->rq_pages[i] = svsk->sk_pages[i];
927                 svsk->sk_pages[i] = NULL;
928         }
929         rqstp->rq_arg.head[0].iov_base = page_address(rqstp->rq_pages[0]);
930         return len;
931 }
932
933 static void svc_tcp_save_pages(struct svc_sock *svsk, struct svc_rqst *rqstp)
934 {
935         unsigned int i, len, npages;
936
937         if (svsk->sk_tcplen <= sizeof(rpc_fraghdr))
938                 return;
939         len = svsk->sk_tcplen - sizeof(rpc_fraghdr);
940         npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
941         for (i = 0; i < npages; i++) {
942                 svsk->sk_pages[i] = rqstp->rq_pages[i];
943                 rqstp->rq_pages[i] = NULL;
944         }
945 }
946
947 static void svc_tcp_clear_pages(struct svc_sock *svsk)
948 {
949         unsigned int i, len, npages;
950
951         if (svsk->sk_tcplen <= sizeof(rpc_fraghdr))
952                 goto out;
953         len = svsk->sk_tcplen - sizeof(rpc_fraghdr);
954         npages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
955         for (i = 0; i < npages; i++) {
956                 BUG_ON(svsk->sk_pages[i] == NULL);
957                 put_page(svsk->sk_pages[i]);
958                 svsk->sk_pages[i] = NULL;
959         }
960 out:
961         svsk->sk_tcplen = 0;
962 }
963
964 /*
965  * Receive data.
966  * If we haven't gotten the record length yet, get the next four bytes.
967  * Otherwise try to gobble up as much as possible up to the complete
968  * record length.
969  */
970 static int svc_tcp_recv_record(struct svc_sock *svsk, struct svc_rqst *rqstp)
971 {
972         struct svc_serv *serv = svsk->sk_xprt.xpt_server;
973         unsigned int want;
974         int len;
975
976         if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
977                 /* sndbuf needs to have room for one request
978                  * per thread, otherwise we can stall even when the
979                  * network isn't a bottleneck.
980                  *
981                  * We count all threads rather than threads in a
982                  * particular pool, which provides an upper bound
983                  * on the number of threads which will access the socket.
984                  *
985                  * rcvbuf just needs to be able to hold a few requests.
986                  * Normally they will be removed from the queue
987                  * as soon a a complete request arrives.
988                  */
989                 svc_sock_setbufsize(svsk->sk_sock,
990                                     (serv->sv_nrthreads+3) * serv->sv_max_mesg,
991                                     3 * serv->sv_max_mesg);
992
993         clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
994
995         if (svsk->sk_tcplen < sizeof(rpc_fraghdr)) {
996                 struct kvec     iov;
997
998                 want = sizeof(rpc_fraghdr) - svsk->sk_tcplen;
999                 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
1000                 iov.iov_len  = want;
1001                 if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
1002                         goto error;
1003                 svsk->sk_tcplen += len;
1004
1005                 if (len < want) {
1006                         dprintk("svc: short recvfrom while reading record "
1007                                 "length (%d of %d)\n", len, want);
1008                         return -EAGAIN;
1009                 }
1010
1011                 svsk->sk_reclen = ntohl(svsk->sk_reclen);
1012                 if (!(svsk->sk_reclen & RPC_LAST_STREAM_FRAGMENT)) {
1013                         /* FIXME: technically, a record can be fragmented,
1014                          *  and non-terminal fragments will not have the top
1015                          *  bit set in the fragment length header.
1016                          *  But apparently no known nfs clients send fragmented
1017                          *  records. */
1018                         if (net_ratelimit())
1019                                 printk(KERN_NOTICE "RPC: multiple fragments "
1020                                         "per record not supported\n");
1021                         goto err_delete;
1022                 }
1023
1024                 svsk->sk_reclen &= RPC_FRAGMENT_SIZE_MASK;
1025                 dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
1026                 if (svsk->sk_reclen > serv->sv_max_mesg) {
1027                         if (net_ratelimit())
1028                                 printk(KERN_NOTICE "RPC: "
1029                                         "fragment too large: 0x%08lx\n",
1030                                         (unsigned long)svsk->sk_reclen);
1031                         goto err_delete;
1032                 }
1033         }
1034
1035         if (svsk->sk_reclen < 8)
1036                 goto err_delete; /* client is nuts. */
1037
1038         len = svsk->sk_reclen;
1039
1040         return len;
1041 error:
1042         dprintk("RPC: TCP recv_record got %d\n", len);
1043         return len;
1044 err_delete:
1045         set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1046         return -EAGAIN;
1047 }
1048
1049 static int receive_cb_reply(struct svc_sock *svsk, struct svc_rqst *rqstp)
1050 {
1051         struct rpc_xprt *bc_xprt = svsk->sk_xprt.xpt_bc_xprt;
1052         struct rpc_rqst *req = NULL;
1053         struct kvec *src, *dst;
1054         __be32 *p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
1055         __be32 xid;
1056         __be32 calldir;
1057
1058         xid = *p++;
1059         calldir = *p;
1060
1061         if (bc_xprt)
1062                 req = xprt_lookup_rqst(bc_xprt, xid);
1063
1064         if (!req) {
1065                 printk(KERN_NOTICE
1066                         "%s: Got unrecognized reply: "
1067                         "calldir 0x%x xpt_bc_xprt %p xid %08x\n",
1068                         __func__, ntohl(calldir),
1069                         bc_xprt, xid);
1070                 return -EAGAIN;
1071         }
1072
1073         memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
1074         /*
1075          * XXX!: cheating for now!  Only copying HEAD.
1076          * But we know this is good enough for now (in fact, for any
1077          * callback reply in the forseeable future).
1078          */
1079         dst = &req->rq_private_buf.head[0];
1080         src = &rqstp->rq_arg.head[0];
1081         if (dst->iov_len < src->iov_len)
1082                 return -EAGAIN; /* whatever; just giving up. */
1083         memcpy(dst->iov_base, src->iov_base, src->iov_len);
1084         xprt_complete_rqst(req->rq_task, svsk->sk_reclen);
1085         rqstp->rq_arg.len = 0;
1086         return 0;
1087 }
1088
1089 static int copy_pages_to_kvecs(struct kvec *vec, struct page **pages, int len)
1090 {
1091         int i = 0;
1092         int t = 0;
1093
1094         while (t < len) {
1095                 vec[i].iov_base = page_address(pages[i]);
1096                 vec[i].iov_len = PAGE_SIZE;
1097                 i++;
1098                 t += PAGE_SIZE;
1099         }
1100         return i;
1101 }
1102
1103
1104 /*
1105  * Receive data from a TCP socket.
1106  */
1107 static int svc_tcp_recvfrom(struct svc_rqst *rqstp)
1108 {
1109         struct svc_sock *svsk =
1110                 container_of(rqstp->rq_xprt, struct svc_sock, sk_xprt);
1111         struct svc_serv *serv = svsk->sk_xprt.xpt_server;
1112         int             len;
1113         struct kvec *vec;
1114         unsigned int want, base;
1115         __be32 *p;
1116         __be32 calldir;
1117         int pnum;
1118
1119         dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
1120                 svsk, test_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags),
1121                 test_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags),
1122                 test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags));
1123
1124         len = svc_tcp_recv_record(svsk, rqstp);
1125         if (len < 0)
1126                 goto error;
1127
1128         base = svc_tcp_restore_pages(svsk, rqstp);
1129         want = svsk->sk_reclen - base;
1130
1131         vec = rqstp->rq_vec;
1132
1133         pnum = copy_pages_to_kvecs(&vec[0], &rqstp->rq_pages[0],
1134                                                 svsk->sk_reclen);
1135
1136         rqstp->rq_respages = &rqstp->rq_pages[pnum];
1137
1138         /* Now receive data */
1139         len = svc_partial_recvfrom(rqstp, vec, pnum, want, base);
1140         if (len >= 0)
1141                 svsk->sk_tcplen += len;
1142         if (len != want) {
1143                 if (len < 0 && len != -EAGAIN)
1144                         goto err_other;
1145                 svc_tcp_save_pages(svsk, rqstp);
1146                 dprintk("svc: incomplete TCP record (%d of %d)\n",
1147                         svsk->sk_tcplen, svsk->sk_reclen);
1148                 goto err_noclose;
1149         }
1150
1151         rqstp->rq_arg.len = svsk->sk_reclen;
1152         rqstp->rq_arg.page_base = 0;
1153         if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
1154                 rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
1155                 rqstp->rq_arg.page_len = 0;
1156         } else
1157                 rqstp->rq_arg.page_len = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
1158
1159         rqstp->rq_xprt_ctxt   = NULL;
1160         rqstp->rq_prot        = IPPROTO_TCP;
1161
1162         p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
1163         calldir = p[1];
1164         if (calldir) {
1165                 len = receive_cb_reply(svsk, rqstp);
1166                 if (len < 0)
1167                         goto error;
1168         }
1169
1170         /* Reset TCP read info */
1171         svsk->sk_reclen = 0;
1172         svsk->sk_tcplen = 0;
1173         /* If we have more data, signal svc_xprt_enqueue() to try again */
1174         if (svc_recv_available(svsk) > sizeof(rpc_fraghdr))
1175                 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
1176
1177
1178         svc_xprt_copy_addrs(rqstp, &svsk->sk_xprt);
1179         if (serv->sv_stats)
1180                 serv->sv_stats->nettcpcnt++;
1181
1182         dprintk("svc: TCP complete record (%d bytes)\n", rqstp->rq_arg.len);
1183         return rqstp->rq_arg.len;
1184
1185 error:
1186         if (len != -EAGAIN)
1187                 goto err_other;
1188         dprintk("RPC: TCP recvfrom got EAGAIN\n");
1189         return -EAGAIN;
1190 err_other:
1191         printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
1192                svsk->sk_xprt.xpt_server->sv_name, -len);
1193         set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1194 err_noclose:
1195         return -EAGAIN; /* record not complete */
1196 }
1197
1198 /*
1199  * Send out data on TCP socket.
1200  */
1201 static int svc_tcp_sendto(struct svc_rqst *rqstp)
1202 {
1203         struct xdr_buf  *xbufp = &rqstp->rq_res;
1204         int sent;
1205         __be32 reclen;
1206
1207         /* Set up the first element of the reply kvec.
1208          * Any other kvecs that may be in use have been taken
1209          * care of by the server implementation itself.
1210          */
1211         reclen = htonl(0x80000000|((xbufp->len ) - 4));
1212         memcpy(xbufp->head[0].iov_base, &reclen, 4);
1213
1214         sent = svc_sendto(rqstp, &rqstp->rq_res);
1215         if (sent != xbufp->len) {
1216                 printk(KERN_NOTICE
1217                        "rpc-srv/tcp: %s: %s %d when sending %d bytes "
1218                        "- shutting down socket\n",
1219                        rqstp->rq_xprt->xpt_server->sv_name,
1220                        (sent<0)?"got error":"sent only",
1221                        sent, xbufp->len);
1222                 set_bit(XPT_CLOSE, &rqstp->rq_xprt->xpt_flags);
1223                 svc_xprt_enqueue(rqstp->rq_xprt);
1224                 sent = -EAGAIN;
1225         }
1226         return sent;
1227 }
1228
1229 /*
1230  * Setup response header. TCP has a 4B record length field.
1231  */
1232 static void svc_tcp_prep_reply_hdr(struct svc_rqst *rqstp)
1233 {
1234         struct kvec *resv = &rqstp->rq_res.head[0];
1235
1236         /* tcp needs a space for the record length... */
1237         svc_putnl(resv, 0);
1238 }
1239
1240 static int svc_tcp_has_wspace(struct svc_xprt *xprt)
1241 {
1242         struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1243         struct svc_serv *serv = svsk->sk_xprt.xpt_server;
1244         int required;
1245
1246         if (test_bit(XPT_LISTENER, &xprt->xpt_flags))
1247                 return 1;
1248         required = atomic_read(&xprt->xpt_reserved) + serv->sv_max_mesg;
1249         if (sk_stream_wspace(svsk->sk_sk) >= required)
1250                 return 1;
1251         set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
1252         return 0;
1253 }
1254
1255 static struct svc_xprt *svc_tcp_create(struct svc_serv *serv,
1256                                        struct net *net,
1257                                        struct sockaddr *sa, int salen,
1258                                        int flags)
1259 {
1260         return svc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
1261 }
1262
1263 #if defined(CONFIG_NFS_V4_1)
1264 static struct svc_xprt *svc_bc_create_socket(struct svc_serv *, int,
1265                                              struct net *, struct sockaddr *,
1266                                              int, int);
1267 static void svc_bc_sock_free(struct svc_xprt *xprt);
1268
1269 static struct svc_xprt *svc_bc_tcp_create(struct svc_serv *serv,
1270                                        struct net *net,
1271                                        struct sockaddr *sa, int salen,
1272                                        int flags)
1273 {
1274         return svc_bc_create_socket(serv, IPPROTO_TCP, net, sa, salen, flags);
1275 }
1276
1277 static void svc_bc_tcp_sock_detach(struct svc_xprt *xprt)
1278 {
1279 }
1280
1281 static struct svc_xprt_ops svc_tcp_bc_ops = {
1282         .xpo_create = svc_bc_tcp_create,
1283         .xpo_detach = svc_bc_tcp_sock_detach,
1284         .xpo_free = svc_bc_sock_free,
1285         .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr,
1286 };
1287
1288 static struct svc_xprt_class svc_tcp_bc_class = {
1289         .xcl_name = "tcp-bc",
1290         .xcl_owner = THIS_MODULE,
1291         .xcl_ops = &svc_tcp_bc_ops,
1292         .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
1293 };
1294
1295 static void svc_init_bc_xprt_sock(void)
1296 {
1297         svc_reg_xprt_class(&svc_tcp_bc_class);
1298 }
1299
1300 static void svc_cleanup_bc_xprt_sock(void)
1301 {
1302         svc_unreg_xprt_class(&svc_tcp_bc_class);
1303 }
1304 #else /* CONFIG_NFS_V4_1 */
1305 static void svc_init_bc_xprt_sock(void)
1306 {
1307 }
1308
1309 static void svc_cleanup_bc_xprt_sock(void)
1310 {
1311 }
1312 #endif /* CONFIG_NFS_V4_1 */
1313
1314 static struct svc_xprt_ops svc_tcp_ops = {
1315         .xpo_create = svc_tcp_create,
1316         .xpo_recvfrom = svc_tcp_recvfrom,
1317         .xpo_sendto = svc_tcp_sendto,
1318         .xpo_release_rqst = svc_release_skb,
1319         .xpo_detach = svc_tcp_sock_detach,
1320         .xpo_free = svc_sock_free,
1321         .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr,
1322         .xpo_has_wspace = svc_tcp_has_wspace,
1323         .xpo_accept = svc_tcp_accept,
1324 };
1325
1326 static struct svc_xprt_class svc_tcp_class = {
1327         .xcl_name = "tcp",
1328         .xcl_owner = THIS_MODULE,
1329         .xcl_ops = &svc_tcp_ops,
1330         .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
1331 };
1332
1333 void svc_init_xprt_sock(void)
1334 {
1335         svc_reg_xprt_class(&svc_tcp_class);
1336         svc_reg_xprt_class(&svc_udp_class);
1337         svc_init_bc_xprt_sock();
1338 }
1339
1340 void svc_cleanup_xprt_sock(void)
1341 {
1342         svc_unreg_xprt_class(&svc_tcp_class);
1343         svc_unreg_xprt_class(&svc_udp_class);
1344         svc_cleanup_bc_xprt_sock();
1345 }
1346
1347 static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
1348 {
1349         struct sock     *sk = svsk->sk_sk;
1350
1351         svc_xprt_init(&svc_tcp_class, &svsk->sk_xprt, serv);
1352         set_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
1353         if (sk->sk_state == TCP_LISTEN) {
1354                 dprintk("setting up TCP socket for listening\n");
1355                 set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags);
1356                 sk->sk_data_ready = svc_tcp_listen_data_ready;
1357                 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
1358         } else {
1359                 dprintk("setting up TCP socket for reading\n");
1360                 sk->sk_state_change = svc_tcp_state_change;
1361                 sk->sk_data_ready = svc_tcp_data_ready;
1362                 sk->sk_write_space = svc_tcp_write_space;
1363
1364                 svsk->sk_reclen = 0;
1365                 svsk->sk_tcplen = 0;
1366                 memset(&svsk->sk_pages[0], 0, sizeof(svsk->sk_pages));
1367
1368                 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
1369
1370                 /* initialise setting must have enough space to
1371                  * receive and respond to one request.
1372                  * svc_tcp_recvfrom will re-adjust if necessary
1373                  */
1374                 svc_sock_setbufsize(svsk->sk_sock,
1375                                     3 * svsk->sk_xprt.xpt_server->sv_max_mesg,
1376                                     3 * svsk->sk_xprt.xpt_server->sv_max_mesg);
1377
1378                 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
1379                 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
1380                 if (sk->sk_state != TCP_ESTABLISHED)
1381                         set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1382         }
1383 }
1384
1385 void svc_sock_update_bufs(struct svc_serv *serv)
1386 {
1387         /*
1388          * The number of server threads has changed. Update
1389          * rcvbuf and sndbuf accordingly on all sockets
1390          */
1391         struct svc_sock *svsk;
1392
1393         spin_lock_bh(&serv->sv_lock);
1394         list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list)
1395                 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
1396         list_for_each_entry(svsk, &serv->sv_tempsocks, sk_xprt.xpt_list)
1397                 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
1398         spin_unlock_bh(&serv->sv_lock);
1399 }
1400 EXPORT_SYMBOL_GPL(svc_sock_update_bufs);
1401
1402 /*
1403  * Initialize socket for RPC use and create svc_sock struct
1404  * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
1405  */
1406 static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1407                                                 struct socket *sock,
1408                                                 int *errp, int flags)
1409 {
1410         struct svc_sock *svsk;
1411         struct sock     *inet;
1412         int             pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
1413
1414         dprintk("svc: svc_setup_socket %p\n", sock);
1415         if (!(svsk = kzalloc(sizeof(*svsk), GFP_KERNEL))) {
1416                 *errp = -ENOMEM;
1417                 return NULL;
1418         }
1419
1420         inet = sock->sk;
1421
1422         /* Register socket with portmapper */
1423         if (*errp >= 0 && pmap_register)
1424                 *errp = svc_register(serv, inet->sk_family, inet->sk_protocol,
1425                                      ntohs(inet_sk(inet)->inet_sport));
1426
1427         if (*errp < 0) {
1428                 kfree(svsk);
1429                 return NULL;
1430         }
1431
1432         inet->sk_user_data = svsk;
1433         svsk->sk_sock = sock;
1434         svsk->sk_sk = inet;
1435         svsk->sk_ostate = inet->sk_state_change;
1436         svsk->sk_odata = inet->sk_data_ready;
1437         svsk->sk_owspace = inet->sk_write_space;
1438
1439         /* Initialize the socket */
1440         if (sock->type == SOCK_DGRAM)
1441                 svc_udp_init(svsk, serv);
1442         else
1443                 svc_tcp_init(svsk, serv);
1444
1445         dprintk("svc: svc_setup_socket created %p (inet %p)\n",
1446                                 svsk, svsk->sk_sk);
1447
1448         return svsk;
1449 }
1450
1451 /**
1452  * svc_addsock - add a listener socket to an RPC service
1453  * @serv: pointer to RPC service to which to add a new listener
1454  * @fd: file descriptor of the new listener
1455  * @name_return: pointer to buffer to fill in with name of listener
1456  * @len: size of the buffer
1457  *
1458  * Fills in socket name and returns positive length of name if successful.
1459  * Name is terminated with '\n'.  On error, returns a negative errno
1460  * value.
1461  */
1462 int svc_addsock(struct svc_serv *serv, const int fd, char *name_return,
1463                 const size_t len)
1464 {
1465         int err = 0;
1466         struct socket *so = sockfd_lookup(fd, &err);
1467         struct svc_sock *svsk = NULL;
1468
1469         if (!so)
1470                 return err;
1471         if ((so->sk->sk_family != PF_INET) && (so->sk->sk_family != PF_INET6))
1472                 err =  -EAFNOSUPPORT;
1473         else if (so->sk->sk_protocol != IPPROTO_TCP &&
1474             so->sk->sk_protocol != IPPROTO_UDP)
1475                 err =  -EPROTONOSUPPORT;
1476         else if (so->state > SS_UNCONNECTED)
1477                 err = -EISCONN;
1478         else {
1479                 if (!try_module_get(THIS_MODULE))
1480                         err = -ENOENT;
1481                 else
1482                         svsk = svc_setup_socket(serv, so, &err,
1483                                                 SVC_SOCK_DEFAULTS);
1484                 if (svsk) {
1485                         struct sockaddr_storage addr;
1486                         struct sockaddr *sin = (struct sockaddr *)&addr;
1487                         int salen;
1488                         if (kernel_getsockname(svsk->sk_sock, sin, &salen) == 0)
1489                                 svc_xprt_set_local(&svsk->sk_xprt, sin, salen);
1490                         clear_bit(XPT_TEMP, &svsk->sk_xprt.xpt_flags);
1491                         spin_lock_bh(&serv->sv_lock);
1492                         list_add(&svsk->sk_xprt.xpt_list, &serv->sv_permsocks);
1493                         spin_unlock_bh(&serv->sv_lock);
1494                         svc_xprt_received(&svsk->sk_xprt);
1495                         err = 0;
1496                 } else
1497                         module_put(THIS_MODULE);
1498         }
1499         if (err) {
1500                 sockfd_put(so);
1501                 return err;
1502         }
1503         return svc_one_sock_name(svsk, name_return, len);
1504 }
1505 EXPORT_SYMBOL_GPL(svc_addsock);
1506
1507 /*
1508  * Create socket for RPC service.
1509  */
1510 static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
1511                                           int protocol,
1512                                           struct net *net,
1513                                           struct sockaddr *sin, int len,
1514                                           int flags)
1515 {
1516         struct svc_sock *svsk;
1517         struct socket   *sock;
1518         int             error;
1519         int             type;
1520         struct sockaddr_storage addr;
1521         struct sockaddr *newsin = (struct sockaddr *)&addr;
1522         int             newlen;
1523         int             family;
1524         int             val;
1525         RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
1526
1527         dprintk("svc: svc_create_socket(%s, %d, %s)\n",
1528                         serv->sv_program->pg_name, protocol,
1529                         __svc_print_addr(sin, buf, sizeof(buf)));
1530
1531         if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1532                 printk(KERN_WARNING "svc: only UDP and TCP "
1533                                 "sockets supported\n");
1534                 return ERR_PTR(-EINVAL);
1535         }
1536
1537         type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1538         switch (sin->sa_family) {
1539         case AF_INET6:
1540                 family = PF_INET6;
1541                 break;
1542         case AF_INET:
1543                 family = PF_INET;
1544                 break;
1545         default:
1546                 return ERR_PTR(-EINVAL);
1547         }
1548
1549         error = __sock_create(net, family, type, protocol, &sock, 1);
1550         if (error < 0)
1551                 return ERR_PTR(error);
1552
1553         svc_reclassify_socket(sock);
1554
1555         /*
1556          * If this is an PF_INET6 listener, we want to avoid
1557          * getting requests from IPv4 remotes.  Those should
1558          * be shunted to a PF_INET listener via rpcbind.
1559          */
1560         val = 1;
1561         if (family == PF_INET6)
1562                 kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
1563                                         (char *)&val, sizeof(val));
1564
1565         if (type == SOCK_STREAM)
1566                 sock->sk->sk_reuse = 1;         /* allow address reuse */
1567         error = kernel_bind(sock, sin, len);
1568         if (error < 0)
1569                 goto bummer;
1570
1571         newlen = len;
1572         error = kernel_getsockname(sock, newsin, &newlen);
1573         if (error < 0)
1574                 goto bummer;
1575
1576         if (protocol == IPPROTO_TCP) {
1577                 if ((error = kernel_listen(sock, 64)) < 0)
1578                         goto bummer;
1579         }
1580
1581         if ((svsk = svc_setup_socket(serv, sock, &error, flags)) != NULL) {
1582                 svc_xprt_set_local(&svsk->sk_xprt, newsin, newlen);
1583                 return (struct svc_xprt *)svsk;
1584         }
1585
1586 bummer:
1587         dprintk("svc: svc_create_socket error = %d\n", -error);
1588         sock_release(sock);
1589         return ERR_PTR(error);
1590 }
1591
1592 /*
1593  * Detach the svc_sock from the socket so that no
1594  * more callbacks occur.
1595  */
1596 static void svc_sock_detach(struct svc_xprt *xprt)
1597 {
1598         struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1599         struct sock *sk = svsk->sk_sk;
1600         wait_queue_head_t *wq;
1601
1602         dprintk("svc: svc_sock_detach(%p)\n", svsk);
1603
1604         /* put back the old socket callbacks */
1605         sk->sk_state_change = svsk->sk_ostate;
1606         sk->sk_data_ready = svsk->sk_odata;
1607         sk->sk_write_space = svsk->sk_owspace;
1608
1609         wq = sk_sleep(sk);
1610         if (wq && waitqueue_active(wq))
1611                 wake_up_interruptible(wq);
1612 }
1613
1614 /*
1615  * Disconnect the socket, and reset the callbacks
1616  */
1617 static void svc_tcp_sock_detach(struct svc_xprt *xprt)
1618 {
1619         struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1620
1621         dprintk("svc: svc_tcp_sock_detach(%p)\n", svsk);
1622
1623         svc_sock_detach(xprt);
1624
1625         if (!test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
1626                 svc_tcp_clear_pages(svsk);
1627                 kernel_sock_shutdown(svsk->sk_sock, SHUT_RDWR);
1628         }
1629 }
1630
1631 /*
1632  * Free the svc_sock's socket resources and the svc_sock itself.
1633  */
1634 static void svc_sock_free(struct svc_xprt *xprt)
1635 {
1636         struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1637         dprintk("svc: svc_sock_free(%p)\n", svsk);
1638
1639         if (svsk->sk_sock->file)
1640                 sockfd_put(svsk->sk_sock);
1641         else
1642                 sock_release(svsk->sk_sock);
1643         kfree(svsk);
1644 }
1645
1646 #if defined(CONFIG_NFS_V4_1)
1647 /*
1648  * Create a back channel svc_xprt which shares the fore channel socket.
1649  */
1650 static struct svc_xprt *svc_bc_create_socket(struct svc_serv *serv,
1651                                              int protocol,
1652                                              struct net *net,
1653                                              struct sockaddr *sin, int len,
1654                                              int flags)
1655 {
1656         struct svc_sock *svsk;
1657         struct svc_xprt *xprt;
1658
1659         if (protocol != IPPROTO_TCP) {
1660                 printk(KERN_WARNING "svc: only TCP sockets"
1661                         " supported on shared back channel\n");
1662                 return ERR_PTR(-EINVAL);
1663         }
1664
1665         svsk = kzalloc(sizeof(*svsk), GFP_KERNEL);
1666         if (!svsk)
1667                 return ERR_PTR(-ENOMEM);
1668
1669         xprt = &svsk->sk_xprt;
1670         svc_xprt_init(&svc_tcp_bc_class, xprt, serv);
1671
1672         serv->sv_bc_xprt = xprt;
1673
1674         return xprt;
1675 }
1676
1677 /*
1678  * Free a back channel svc_sock.
1679  */
1680 static void svc_bc_sock_free(struct svc_xprt *xprt)
1681 {
1682         if (xprt)
1683                 kfree(container_of(xprt, struct svc_sock, sk_xprt));
1684 }
1685 #endif /* CONFIG_NFS_V4_1 */
This page took 0.133032 seconds and 4 git commands to generate.