]> err.no Git - linux-2.6/blob - net/sunrpc/svcsock.c
[PATCH] knfsd: use new lock for svc_sock deferred list
[linux-2.6] / 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_sock_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/sched.h>
23 #include <linux/errno.h>
24 #include <linux/fcntl.h>
25 #include <linux/net.h>
26 #include <linux/in.h>
27 #include <linux/inet.h>
28 #include <linux/udp.h>
29 #include <linux/tcp.h>
30 #include <linux/unistd.h>
31 #include <linux/slab.h>
32 #include <linux/netdevice.h>
33 #include <linux/skbuff.h>
34 #include <linux/file.h>
35 #include <net/sock.h>
36 #include <net/checksum.h>
37 #include <net/ip.h>
38 #include <net/tcp_states.h>
39 #include <asm/uaccess.h>
40 #include <asm/ioctls.h>
41
42 #include <linux/sunrpc/types.h>
43 #include <linux/sunrpc/xdr.h>
44 #include <linux/sunrpc/svcsock.h>
45 #include <linux/sunrpc/stats.h>
46
47 /* SMP locking strategy:
48  *
49  *      svc_serv->sv_lock protects most stuff for that service.
50  *      svc_sock->sk_defer_lock protects the svc_sock->sk_deferred list
51  *
52  *      Some flags can be set to certain values at any time
53  *      providing that certain rules are followed:
54  *
55  *      SK_BUSY  can be set to 0 at any time.  
56  *              svc_sock_enqueue must be called afterwards
57  *      SK_CONN, SK_DATA, can be set or cleared at any time.
58  *              after a set, svc_sock_enqueue must be called.   
59  *              after a clear, the socket must be read/accepted
60  *               if this succeeds, it must be set again.
61  *      SK_CLOSE can set at any time. It is never cleared.
62  *
63  */
64
65 #define RPCDBG_FACILITY RPCDBG_SVCSOCK
66
67
68 static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
69                                          int *errp, int pmap_reg);
70 static void             svc_udp_data_ready(struct sock *, int);
71 static int              svc_udp_recvfrom(struct svc_rqst *);
72 static int              svc_udp_sendto(struct svc_rqst *);
73
74 static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk);
75 static int svc_deferred_recv(struct svc_rqst *rqstp);
76 static struct cache_deferred_req *svc_defer(struct cache_req *req);
77
78 /* apparently the "standard" is that clients close
79  * idle connections after 5 minutes, servers after
80  * 6 minutes
81  *   http://www.connectathon.org/talks96/nfstcp.pdf
82  */
83 static int svc_conn_age_period = 6*60;
84
85 /*
86  * Queue up an idle server thread.  Must have serv->sv_lock held.
87  * Note: this is really a stack rather than a queue, so that we only
88  * use as many different threads as we need, and the rest don't polute
89  * the cache.
90  */
91 static inline void
92 svc_serv_enqueue(struct svc_serv *serv, struct svc_rqst *rqstp)
93 {
94         list_add(&rqstp->rq_list, &serv->sv_threads);
95 }
96
97 /*
98  * Dequeue an nfsd thread.  Must have serv->sv_lock held.
99  */
100 static inline void
101 svc_serv_dequeue(struct svc_serv *serv, struct svc_rqst *rqstp)
102 {
103         list_del(&rqstp->rq_list);
104 }
105
106 /*
107  * Release an skbuff after use
108  */
109 static inline void
110 svc_release_skb(struct svc_rqst *rqstp)
111 {
112         struct sk_buff *skb = rqstp->rq_skbuff;
113         struct svc_deferred_req *dr = rqstp->rq_deferred;
114
115         if (skb) {
116                 rqstp->rq_skbuff = NULL;
117
118                 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
119                 skb_free_datagram(rqstp->rq_sock->sk_sk, skb);
120         }
121         if (dr) {
122                 rqstp->rq_deferred = NULL;
123                 kfree(dr);
124         }
125 }
126
127 /*
128  * Any space to write?
129  */
130 static inline unsigned long
131 svc_sock_wspace(struct svc_sock *svsk)
132 {
133         int wspace;
134
135         if (svsk->sk_sock->type == SOCK_STREAM)
136                 wspace = sk_stream_wspace(svsk->sk_sk);
137         else
138                 wspace = sock_wspace(svsk->sk_sk);
139
140         return wspace;
141 }
142
143 /*
144  * Queue up a socket with data pending. If there are idle nfsd
145  * processes, wake 'em up.
146  *
147  */
148 static void
149 svc_sock_enqueue(struct svc_sock *svsk)
150 {
151         struct svc_serv *serv = svsk->sk_server;
152         struct svc_rqst *rqstp;
153
154         if (!(svsk->sk_flags &
155               ( (1<<SK_CONN)|(1<<SK_DATA)|(1<<SK_CLOSE)|(1<<SK_DEFERRED)) ))
156                 return;
157         if (test_bit(SK_DEAD, &svsk->sk_flags))
158                 return;
159
160         spin_lock_bh(&serv->sv_lock);
161
162         if (!list_empty(&serv->sv_threads) && 
163             !list_empty(&serv->sv_sockets))
164                 printk(KERN_ERR
165                         "svc_sock_enqueue: threads and sockets both waiting??\n");
166
167         if (test_bit(SK_DEAD, &svsk->sk_flags)) {
168                 /* Don't enqueue dead sockets */
169                 dprintk("svc: socket %p is dead, not enqueued\n", svsk->sk_sk);
170                 goto out_unlock;
171         }
172
173         if (test_bit(SK_BUSY, &svsk->sk_flags)) {
174                 /* Don't enqueue socket while daemon is receiving */
175                 dprintk("svc: socket %p busy, not enqueued\n", svsk->sk_sk);
176                 goto out_unlock;
177         }
178
179         set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
180         if (((svsk->sk_reserved + serv->sv_bufsz)*2
181              > svc_sock_wspace(svsk))
182             && !test_bit(SK_CLOSE, &svsk->sk_flags)
183             && !test_bit(SK_CONN, &svsk->sk_flags)) {
184                 /* Don't enqueue while not enough space for reply */
185                 dprintk("svc: socket %p  no space, %d*2 > %ld, not enqueued\n",
186                         svsk->sk_sk, svsk->sk_reserved+serv->sv_bufsz,
187                         svc_sock_wspace(svsk));
188                 goto out_unlock;
189         }
190         clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
191
192         /* Mark socket as busy. It will remain in this state until the
193          * server has processed all pending data and put the socket back
194          * on the idle list.
195          */
196         set_bit(SK_BUSY, &svsk->sk_flags);
197
198         if (!list_empty(&serv->sv_threads)) {
199                 rqstp = list_entry(serv->sv_threads.next,
200                                    struct svc_rqst,
201                                    rq_list);
202                 dprintk("svc: socket %p served by daemon %p\n",
203                         svsk->sk_sk, rqstp);
204                 svc_serv_dequeue(serv, rqstp);
205                 if (rqstp->rq_sock)
206                         printk(KERN_ERR 
207                                 "svc_sock_enqueue: server %p, rq_sock=%p!\n",
208                                 rqstp, rqstp->rq_sock);
209                 rqstp->rq_sock = svsk;
210                 atomic_inc(&svsk->sk_inuse);
211                 rqstp->rq_reserved = serv->sv_bufsz;
212                 svsk->sk_reserved += rqstp->rq_reserved;
213                 wake_up(&rqstp->rq_wait);
214         } else {
215                 dprintk("svc: socket %p put into queue\n", svsk->sk_sk);
216                 list_add_tail(&svsk->sk_ready, &serv->sv_sockets);
217         }
218
219 out_unlock:
220         spin_unlock_bh(&serv->sv_lock);
221 }
222
223 /*
224  * Dequeue the first socket.  Must be called with the serv->sv_lock held.
225  */
226 static inline struct svc_sock *
227 svc_sock_dequeue(struct svc_serv *serv)
228 {
229         struct svc_sock *svsk;
230
231         if (list_empty(&serv->sv_sockets))
232                 return NULL;
233
234         svsk = list_entry(serv->sv_sockets.next,
235                           struct svc_sock, sk_ready);
236         list_del_init(&svsk->sk_ready);
237
238         dprintk("svc: socket %p dequeued, inuse=%d\n",
239                 svsk->sk_sk, atomic_read(&svsk->sk_inuse));
240
241         return svsk;
242 }
243
244 /*
245  * Having read something from a socket, check whether it
246  * needs to be re-enqueued.
247  * Note: SK_DATA only gets cleared when a read-attempt finds
248  * no (or insufficient) data.
249  */
250 static inline void
251 svc_sock_received(struct svc_sock *svsk)
252 {
253         clear_bit(SK_BUSY, &svsk->sk_flags);
254         svc_sock_enqueue(svsk);
255 }
256
257
258 /**
259  * svc_reserve - change the space reserved for the reply to a request.
260  * @rqstp:  The request in question
261  * @space: new max space to reserve
262  *
263  * Each request reserves some space on the output queue of the socket
264  * to make sure the reply fits.  This function reduces that reserved
265  * space to be the amount of space used already, plus @space.
266  *
267  */
268 void svc_reserve(struct svc_rqst *rqstp, int space)
269 {
270         space += rqstp->rq_res.head[0].iov_len;
271
272         if (space < rqstp->rq_reserved) {
273                 struct svc_sock *svsk = rqstp->rq_sock;
274                 spin_lock_bh(&svsk->sk_server->sv_lock);
275                 svsk->sk_reserved -= (rqstp->rq_reserved - space);
276                 rqstp->rq_reserved = space;
277                 spin_unlock_bh(&svsk->sk_server->sv_lock);
278
279                 svc_sock_enqueue(svsk);
280         }
281 }
282
283 /*
284  * Release a socket after use.
285  */
286 static inline void
287 svc_sock_put(struct svc_sock *svsk)
288 {
289         if (atomic_dec_and_test(&svsk->sk_inuse) && test_bit(SK_DEAD, &svsk->sk_flags)) {
290                 dprintk("svc: releasing dead socket\n");
291                 sock_release(svsk->sk_sock);
292                 kfree(svsk);
293         }
294 }
295
296 static void
297 svc_sock_release(struct svc_rqst *rqstp)
298 {
299         struct svc_sock *svsk = rqstp->rq_sock;
300
301         svc_release_skb(rqstp);
302
303         svc_free_allpages(rqstp);
304         rqstp->rq_res.page_len = 0;
305         rqstp->rq_res.page_base = 0;
306
307
308         /* Reset response buffer and release
309          * the reservation.
310          * But first, check that enough space was reserved
311          * for the reply, otherwise we have a bug!
312          */
313         if ((rqstp->rq_res.len) >  rqstp->rq_reserved)
314                 printk(KERN_ERR "RPC request reserved %d but used %d\n",
315                        rqstp->rq_reserved,
316                        rqstp->rq_res.len);
317
318         rqstp->rq_res.head[0].iov_len = 0;
319         svc_reserve(rqstp, 0);
320         rqstp->rq_sock = NULL;
321
322         svc_sock_put(svsk);
323 }
324
325 /*
326  * External function to wake up a server waiting for data
327  */
328 void
329 svc_wake_up(struct svc_serv *serv)
330 {
331         struct svc_rqst *rqstp;
332
333         spin_lock_bh(&serv->sv_lock);
334         if (!list_empty(&serv->sv_threads)) {
335                 rqstp = list_entry(serv->sv_threads.next,
336                                    struct svc_rqst,
337                                    rq_list);
338                 dprintk("svc: daemon %p woken up.\n", rqstp);
339                 /*
340                 svc_serv_dequeue(serv, rqstp);
341                 rqstp->rq_sock = NULL;
342                  */
343                 wake_up(&rqstp->rq_wait);
344         }
345         spin_unlock_bh(&serv->sv_lock);
346 }
347
348 /*
349  * Generic sendto routine
350  */
351 static int
352 svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
353 {
354         struct svc_sock *svsk = rqstp->rq_sock;
355         struct socket   *sock = svsk->sk_sock;
356         int             slen;
357         char            buffer[CMSG_SPACE(sizeof(struct in_pktinfo))];
358         struct cmsghdr *cmh = (struct cmsghdr *)buffer;
359         struct in_pktinfo *pki = (struct in_pktinfo *)CMSG_DATA(cmh);
360         int             len = 0;
361         int             result;
362         int             size;
363         struct page     **ppage = xdr->pages;
364         size_t          base = xdr->page_base;
365         unsigned int    pglen = xdr->page_len;
366         unsigned int    flags = MSG_MORE;
367
368         slen = xdr->len;
369
370         if (rqstp->rq_prot == IPPROTO_UDP) {
371                 /* set the source and destination */
372                 struct msghdr   msg;
373                 msg.msg_name    = &rqstp->rq_addr;
374                 msg.msg_namelen = sizeof(rqstp->rq_addr);
375                 msg.msg_iov     = NULL;
376                 msg.msg_iovlen  = 0;
377                 msg.msg_flags   = MSG_MORE;
378
379                 msg.msg_control = cmh;
380                 msg.msg_controllen = sizeof(buffer);
381                 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
382                 cmh->cmsg_level = SOL_IP;
383                 cmh->cmsg_type = IP_PKTINFO;
384                 pki->ipi_ifindex = 0;
385                 pki->ipi_spec_dst.s_addr = rqstp->rq_daddr;
386
387                 if (sock_sendmsg(sock, &msg, 0) < 0)
388                         goto out;
389         }
390
391         /* send head */
392         if (slen == xdr->head[0].iov_len)
393                 flags = 0;
394         len = kernel_sendpage(sock, rqstp->rq_respages[0], 0, xdr->head[0].iov_len, flags);
395         if (len != xdr->head[0].iov_len)
396                 goto out;
397         slen -= xdr->head[0].iov_len;
398         if (slen == 0)
399                 goto out;
400
401         /* send page data */
402         size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
403         while (pglen > 0) {
404                 if (slen == size)
405                         flags = 0;
406                 result = kernel_sendpage(sock, *ppage, base, size, flags);
407                 if (result > 0)
408                         len += result;
409                 if (result != size)
410                         goto out;
411                 slen -= size;
412                 pglen -= size;
413                 size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
414                 base = 0;
415                 ppage++;
416         }
417         /* send tail */
418         if (xdr->tail[0].iov_len) {
419                 result = kernel_sendpage(sock, rqstp->rq_respages[rqstp->rq_restailpage],
420                                              ((unsigned long)xdr->tail[0].iov_base)& (PAGE_SIZE-1),
421                                              xdr->tail[0].iov_len, 0);
422
423                 if (result > 0)
424                         len += result;
425         }
426 out:
427         dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %x)\n",
428                         rqstp->rq_sock, xdr->head[0].iov_base, xdr->head[0].iov_len, xdr->len, len,
429                 rqstp->rq_addr.sin_addr.s_addr);
430
431         return len;
432 }
433
434 /*
435  * Report socket names for nfsdfs
436  */
437 static int one_sock_name(char *buf, struct svc_sock *svsk)
438 {
439         int len;
440
441         switch(svsk->sk_sk->sk_family) {
442         case AF_INET:
443                 len = sprintf(buf, "ipv4 %s %u.%u.%u.%u %d\n",
444                               svsk->sk_sk->sk_protocol==IPPROTO_UDP?
445                               "udp" : "tcp",
446                               NIPQUAD(inet_sk(svsk->sk_sk)->rcv_saddr),
447                               inet_sk(svsk->sk_sk)->num);
448                 break;
449         default:
450                 len = sprintf(buf, "*unknown-%d*\n",
451                                svsk->sk_sk->sk_family);
452         }
453         return len;
454 }
455
456 int
457 svc_sock_names(char *buf, struct svc_serv *serv, char *toclose)
458 {
459         struct svc_sock *svsk, *closesk = NULL;
460         int len = 0;
461
462         if (!serv)
463                 return 0;
464         spin_lock(&serv->sv_lock);
465         list_for_each_entry(svsk, &serv->sv_permsocks, sk_list) {
466                 int onelen = one_sock_name(buf+len, svsk);
467                 if (toclose && strcmp(toclose, buf+len) == 0)
468                         closesk = svsk;
469                 else
470                         len += onelen;
471         }
472         spin_unlock(&serv->sv_lock);
473         if (closesk)
474                 svc_delete_socket(closesk);
475         return len;
476 }
477 EXPORT_SYMBOL(svc_sock_names);
478
479 /*
480  * Check input queue length
481  */
482 static int
483 svc_recv_available(struct svc_sock *svsk)
484 {
485         struct socket   *sock = svsk->sk_sock;
486         int             avail, err;
487
488         err = kernel_sock_ioctl(sock, TIOCINQ, (unsigned long) &avail);
489
490         return (err >= 0)? avail : err;
491 }
492
493 /*
494  * Generic recvfrom routine.
495  */
496 static int
497 svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr, int buflen)
498 {
499         struct msghdr   msg;
500         struct socket   *sock;
501         int             len, alen;
502
503         rqstp->rq_addrlen = sizeof(rqstp->rq_addr);
504         sock = rqstp->rq_sock->sk_sock;
505
506         msg.msg_name    = &rqstp->rq_addr;
507         msg.msg_namelen = sizeof(rqstp->rq_addr);
508         msg.msg_control = NULL;
509         msg.msg_controllen = 0;
510
511         msg.msg_flags   = MSG_DONTWAIT;
512
513         len = kernel_recvmsg(sock, &msg, iov, nr, buflen, MSG_DONTWAIT);
514
515         /* sock_recvmsg doesn't fill in the name/namelen, so we must..
516          * possibly we should cache this in the svc_sock structure
517          * at accept time. FIXME
518          */
519         alen = sizeof(rqstp->rq_addr);
520         kernel_getpeername(sock, (struct sockaddr *)&rqstp->rq_addr, &alen);
521
522         dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
523                 rqstp->rq_sock, iov[0].iov_base, iov[0].iov_len, len);
524
525         return len;
526 }
527
528 /*
529  * Set socket snd and rcv buffer lengths
530  */
531 static inline void
532 svc_sock_setbufsize(struct socket *sock, unsigned int snd, unsigned int rcv)
533 {
534 #if 0
535         mm_segment_t    oldfs;
536         oldfs = get_fs(); set_fs(KERNEL_DS);
537         sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
538                         (char*)&snd, sizeof(snd));
539         sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
540                         (char*)&rcv, sizeof(rcv));
541 #else
542         /* sock_setsockopt limits use to sysctl_?mem_max,
543          * which isn't acceptable.  Until that is made conditional
544          * on not having CAP_SYS_RESOURCE or similar, we go direct...
545          * DaveM said I could!
546          */
547         lock_sock(sock->sk);
548         sock->sk->sk_sndbuf = snd * 2;
549         sock->sk->sk_rcvbuf = rcv * 2;
550         sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK|SOCK_RCVBUF_LOCK;
551         release_sock(sock->sk);
552 #endif
553 }
554 /*
555  * INET callback when data has been received on the socket.
556  */
557 static void
558 svc_udp_data_ready(struct sock *sk, int count)
559 {
560         struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
561
562         if (svsk) {
563                 dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
564                         svsk, sk, count, test_bit(SK_BUSY, &svsk->sk_flags));
565                 set_bit(SK_DATA, &svsk->sk_flags);
566                 svc_sock_enqueue(svsk);
567         }
568         if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
569                 wake_up_interruptible(sk->sk_sleep);
570 }
571
572 /*
573  * INET callback when space is newly available on the socket.
574  */
575 static void
576 svc_write_space(struct sock *sk)
577 {
578         struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
579
580         if (svsk) {
581                 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
582                         svsk, sk, test_bit(SK_BUSY, &svsk->sk_flags));
583                 svc_sock_enqueue(svsk);
584         }
585
586         if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) {
587                 dprintk("RPC svc_write_space: someone sleeping on %p\n",
588                        svsk);
589                 wake_up_interruptible(sk->sk_sleep);
590         }
591 }
592
593 /*
594  * Receive a datagram from a UDP socket.
595  */
596 static int
597 svc_udp_recvfrom(struct svc_rqst *rqstp)
598 {
599         struct svc_sock *svsk = rqstp->rq_sock;
600         struct svc_serv *serv = svsk->sk_server;
601         struct sk_buff  *skb;
602         int             err, len;
603
604         if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
605             /* udp sockets need large rcvbuf as all pending
606              * requests are still in that buffer.  sndbuf must
607              * also be large enough that there is enough space
608              * for one reply per thread.
609              */
610             svc_sock_setbufsize(svsk->sk_sock,
611                                 (serv->sv_nrthreads+3) * serv->sv_bufsz,
612                                 (serv->sv_nrthreads+3) * serv->sv_bufsz);
613
614         if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
615                 svc_sock_received(svsk);
616                 return svc_deferred_recv(rqstp);
617         }
618
619         clear_bit(SK_DATA, &svsk->sk_flags);
620         while ((skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err)) == NULL) {
621                 if (err == -EAGAIN) {
622                         svc_sock_received(svsk);
623                         return err;
624                 }
625                 /* possibly an icmp error */
626                 dprintk("svc: recvfrom returned error %d\n", -err);
627         }
628         if (skb->tstamp.off_sec == 0) {
629                 struct timeval tv;
630
631                 tv.tv_sec = xtime.tv_sec;
632                 tv.tv_usec = xtime.tv_nsec / NSEC_PER_USEC;
633                 skb_set_timestamp(skb, &tv);
634                 /* Don't enable netstamp, sunrpc doesn't 
635                    need that much accuracy */
636         }
637         skb_get_timestamp(skb, &svsk->sk_sk->sk_stamp);
638         set_bit(SK_DATA, &svsk->sk_flags); /* there may be more data... */
639
640         /*
641          * Maybe more packets - kick another thread ASAP.
642          */
643         svc_sock_received(svsk);
644
645         len  = skb->len - sizeof(struct udphdr);
646         rqstp->rq_arg.len = len;
647
648         rqstp->rq_prot        = IPPROTO_UDP;
649
650         /* Get sender address */
651         rqstp->rq_addr.sin_family = AF_INET;
652         rqstp->rq_addr.sin_port = skb->h.uh->source;
653         rqstp->rq_addr.sin_addr.s_addr = skb->nh.iph->saddr;
654         rqstp->rq_daddr = skb->nh.iph->daddr;
655
656         if (skb_is_nonlinear(skb)) {
657                 /* we have to copy */
658                 local_bh_disable();
659                 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
660                         local_bh_enable();
661                         /* checksum error */
662                         skb_free_datagram(svsk->sk_sk, skb);
663                         return 0;
664                 }
665                 local_bh_enable();
666                 skb_free_datagram(svsk->sk_sk, skb); 
667         } else {
668                 /* we can use it in-place */
669                 rqstp->rq_arg.head[0].iov_base = skb->data + sizeof(struct udphdr);
670                 rqstp->rq_arg.head[0].iov_len = len;
671                 if (skb_checksum_complete(skb)) {
672                         skb_free_datagram(svsk->sk_sk, skb);
673                         return 0;
674                 }
675                 rqstp->rq_skbuff = skb;
676         }
677
678         rqstp->rq_arg.page_base = 0;
679         if (len <= rqstp->rq_arg.head[0].iov_len) {
680                 rqstp->rq_arg.head[0].iov_len = len;
681                 rqstp->rq_arg.page_len = 0;
682         } else {
683                 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
684                 rqstp->rq_argused += (rqstp->rq_arg.page_len + PAGE_SIZE - 1)/ PAGE_SIZE;
685         }
686
687         if (serv->sv_stats)
688                 serv->sv_stats->netudpcnt++;
689
690         return len;
691 }
692
693 static int
694 svc_udp_sendto(struct svc_rqst *rqstp)
695 {
696         int             error;
697
698         error = svc_sendto(rqstp, &rqstp->rq_res);
699         if (error == -ECONNREFUSED)
700                 /* ICMP error on earlier request. */
701                 error = svc_sendto(rqstp, &rqstp->rq_res);
702
703         return error;
704 }
705
706 static void
707 svc_udp_init(struct svc_sock *svsk)
708 {
709         svsk->sk_sk->sk_data_ready = svc_udp_data_ready;
710         svsk->sk_sk->sk_write_space = svc_write_space;
711         svsk->sk_recvfrom = svc_udp_recvfrom;
712         svsk->sk_sendto = svc_udp_sendto;
713
714         /* initialise setting must have enough space to
715          * receive and respond to one request.  
716          * svc_udp_recvfrom will re-adjust if necessary
717          */
718         svc_sock_setbufsize(svsk->sk_sock,
719                             3 * svsk->sk_server->sv_bufsz,
720                             3 * svsk->sk_server->sv_bufsz);
721
722         set_bit(SK_DATA, &svsk->sk_flags); /* might have come in before data_ready set up */
723         set_bit(SK_CHNGBUF, &svsk->sk_flags);
724 }
725
726 /*
727  * A data_ready event on a listening socket means there's a connection
728  * pending. Do not use state_change as a substitute for it.
729  */
730 static void
731 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
735         dprintk("svc: socket %p TCP (listen) state change %d\n",
736                 sk, sk->sk_state);
737
738         /*
739          * This callback may called twice when a new connection
740          * is established as a child socket inherits everything
741          * from a parent LISTEN socket.
742          * 1) data_ready method of the parent socket will be called
743          *    when one of child sockets become ESTABLISHED.
744          * 2) data_ready method of the child socket may be called
745          *    when it receives data before the socket is accepted.
746          * In case of 2, we should ignore it silently.
747          */
748         if (sk->sk_state == TCP_LISTEN) {
749                 if (svsk) {
750                         set_bit(SK_CONN, &svsk->sk_flags);
751                         svc_sock_enqueue(svsk);
752                 } else
753                         printk("svc: socket %p: no user data\n", sk);
754         }
755
756         if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
757                 wake_up_interruptible_all(sk->sk_sleep);
758 }
759
760 /*
761  * A state change on a connected socket means it's dying or dead.
762  */
763 static void
764 svc_tcp_state_change(struct sock *sk)
765 {
766         struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
767
768         dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
769                 sk, sk->sk_state, sk->sk_user_data);
770
771         if (!svsk)
772                 printk("svc: socket %p: no user data\n", sk);
773         else {
774                 set_bit(SK_CLOSE, &svsk->sk_flags);
775                 svc_sock_enqueue(svsk);
776         }
777         if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
778                 wake_up_interruptible_all(sk->sk_sleep);
779 }
780
781 static void
782 svc_tcp_data_ready(struct sock *sk, int count)
783 {
784         struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
785
786         dprintk("svc: socket %p TCP data ready (svsk %p)\n",
787                 sk, sk->sk_user_data);
788         if (svsk) {
789                 set_bit(SK_DATA, &svsk->sk_flags);
790                 svc_sock_enqueue(svsk);
791         }
792         if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
793                 wake_up_interruptible(sk->sk_sleep);
794 }
795
796 /*
797  * Accept a TCP connection
798  */
799 static void
800 svc_tcp_accept(struct svc_sock *svsk)
801 {
802         struct sockaddr_in sin;
803         struct svc_serv *serv = svsk->sk_server;
804         struct socket   *sock = svsk->sk_sock;
805         struct socket   *newsock;
806         struct svc_sock *newsvsk;
807         int             err, slen;
808
809         dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
810         if (!sock)
811                 return;
812
813         clear_bit(SK_CONN, &svsk->sk_flags);
814         err = kernel_accept(sock, &newsock, O_NONBLOCK);
815         if (err < 0) {
816                 if (err == -ENOMEM)
817                         printk(KERN_WARNING "%s: no more sockets!\n",
818                                serv->sv_name);
819                 else if (err != -EAGAIN && net_ratelimit())
820                         printk(KERN_WARNING "%s: accept failed (err %d)!\n",
821                                    serv->sv_name, -err);
822                 return;
823         }
824
825         set_bit(SK_CONN, &svsk->sk_flags);
826         svc_sock_enqueue(svsk);
827
828         slen = sizeof(sin);
829         err = kernel_getpeername(newsock, (struct sockaddr *) &sin, &slen);
830         if (err < 0) {
831                 if (net_ratelimit())
832                         printk(KERN_WARNING "%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 encription, the IP of the host won't
839          * tell us anything. For now just warn about unpriv connections.
840          */
841         if (ntohs(sin.sin_port) >= 1024) {
842                 dprintk(KERN_WARNING
843                         "%s: connect from unprivileged port: %u.%u.%u.%u:%d\n",
844                         serv->sv_name, 
845                         NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
846         }
847
848         dprintk("%s: connect from %u.%u.%u.%u:%04x\n", serv->sv_name,
849                         NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
850
851         /* make sure that a write doesn't block forever when
852          * low on memory
853          */
854         newsock->sk->sk_sndtimeo = HZ*30;
855
856         if (!(newsvsk = svc_setup_socket(serv, newsock, &err, 0)))
857                 goto failed;
858
859
860         /* make sure that we don't have too many active connections.
861          * If we have, something must be dropped.
862          *
863          * There's no point in trying to do random drop here for
864          * DoS prevention. The NFS clients does 1 reconnect in 15
865          * seconds. An attacker can easily beat that.
866          *
867          * The only somewhat efficient mechanism would be if drop
868          * old connections from the same IP first. But right now
869          * we don't even record the client IP in svc_sock.
870          */
871         if (serv->sv_tmpcnt > (serv->sv_nrthreads+3)*20) {
872                 struct svc_sock *svsk = NULL;
873                 spin_lock_bh(&serv->sv_lock);
874                 if (!list_empty(&serv->sv_tempsocks)) {
875                         if (net_ratelimit()) {
876                                 /* Try to help the admin */
877                                 printk(KERN_NOTICE "%s: too many open TCP "
878                                         "sockets, consider increasing the "
879                                         "number of nfsd threads\n",
880                                                    serv->sv_name);
881                                 printk(KERN_NOTICE "%s: last TCP connect from "
882                                         "%u.%u.%u.%u:%d\n",
883                                         serv->sv_name,
884                                         NIPQUAD(sin.sin_addr.s_addr),
885                                         ntohs(sin.sin_port));
886                         }
887                         /*
888                          * Always select the oldest socket. It's not fair,
889                          * but so is life
890                          */
891                         svsk = list_entry(serv->sv_tempsocks.prev,
892                                           struct svc_sock,
893                                           sk_list);
894                         set_bit(SK_CLOSE, &svsk->sk_flags);
895                         atomic_inc(&svsk->sk_inuse);
896                 }
897                 spin_unlock_bh(&serv->sv_lock);
898
899                 if (svsk) {
900                         svc_sock_enqueue(svsk);
901                         svc_sock_put(svsk);
902                 }
903
904         }
905
906         if (serv->sv_stats)
907                 serv->sv_stats->nettcpconn++;
908
909         return;
910
911 failed:
912         sock_release(newsock);
913         return;
914 }
915
916 /*
917  * Receive data from a TCP socket.
918  */
919 static int
920 svc_tcp_recvfrom(struct svc_rqst *rqstp)
921 {
922         struct svc_sock *svsk = rqstp->rq_sock;
923         struct svc_serv *serv = svsk->sk_server;
924         int             len;
925         struct kvec vec[RPCSVC_MAXPAGES];
926         int pnum, vlen;
927
928         dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
929                 svsk, test_bit(SK_DATA, &svsk->sk_flags),
930                 test_bit(SK_CONN, &svsk->sk_flags),
931                 test_bit(SK_CLOSE, &svsk->sk_flags));
932
933         if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
934                 svc_sock_received(svsk);
935                 return svc_deferred_recv(rqstp);
936         }
937
938         if (test_bit(SK_CLOSE, &svsk->sk_flags)) {
939                 svc_delete_socket(svsk);
940                 return 0;
941         }
942
943         if (test_bit(SK_CONN, &svsk->sk_flags)) {
944                 svc_tcp_accept(svsk);
945                 svc_sock_received(svsk);
946                 return 0;
947         }
948
949         if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
950                 /* sndbuf needs to have room for one request
951                  * per thread, otherwise we can stall even when the
952                  * network isn't a bottleneck.
953                  * rcvbuf just needs to be able to hold a few requests.
954                  * Normally they will be removed from the queue 
955                  * as soon a a complete request arrives.
956                  */
957                 svc_sock_setbufsize(svsk->sk_sock,
958                                     (serv->sv_nrthreads+3) * serv->sv_bufsz,
959                                     3 * serv->sv_bufsz);
960
961         clear_bit(SK_DATA, &svsk->sk_flags);
962
963         /* Receive data. If we haven't got the record length yet, get
964          * the next four bytes. Otherwise try to gobble up as much as
965          * possible up to the complete record length.
966          */
967         if (svsk->sk_tcplen < 4) {
968                 unsigned long   want = 4 - svsk->sk_tcplen;
969                 struct kvec     iov;
970
971                 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
972                 iov.iov_len  = want;
973                 if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
974                         goto error;
975                 svsk->sk_tcplen += len;
976
977                 if (len < want) {
978                         dprintk("svc: short recvfrom while reading record length (%d of %lu)\n",
979                                 len, want);
980                         svc_sock_received(svsk);
981                         return -EAGAIN; /* record header not complete */
982                 }
983
984                 svsk->sk_reclen = ntohl(svsk->sk_reclen);
985                 if (!(svsk->sk_reclen & 0x80000000)) {
986                         /* FIXME: technically, a record can be fragmented,
987                          *  and non-terminal fragments will not have the top
988                          *  bit set in the fragment length header.
989                          *  But apparently no known nfs clients send fragmented
990                          *  records. */
991                         printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx (non-terminal)\n",
992                                (unsigned long) svsk->sk_reclen);
993                         goto err_delete;
994                 }
995                 svsk->sk_reclen &= 0x7fffffff;
996                 dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
997                 if (svsk->sk_reclen > serv->sv_bufsz) {
998                         printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx (large)\n",
999                                (unsigned long) svsk->sk_reclen);
1000                         goto err_delete;
1001                 }
1002         }
1003
1004         /* Check whether enough data is available */
1005         len = svc_recv_available(svsk);
1006         if (len < 0)
1007                 goto error;
1008
1009         if (len < svsk->sk_reclen) {
1010                 dprintk("svc: incomplete TCP record (%d of %d)\n",
1011                         len, svsk->sk_reclen);
1012                 svc_sock_received(svsk);
1013                 return -EAGAIN; /* record not complete */
1014         }
1015         len = svsk->sk_reclen;
1016         set_bit(SK_DATA, &svsk->sk_flags);
1017
1018         vec[0] = rqstp->rq_arg.head[0];
1019         vlen = PAGE_SIZE;
1020         pnum = 1;
1021         while (vlen < len) {
1022                 vec[pnum].iov_base = page_address(rqstp->rq_argpages[rqstp->rq_argused++]);
1023                 vec[pnum].iov_len = PAGE_SIZE;
1024                 pnum++;
1025                 vlen += PAGE_SIZE;
1026         }
1027
1028         /* Now receive data */
1029         len = svc_recvfrom(rqstp, vec, pnum, len);
1030         if (len < 0)
1031                 goto error;
1032
1033         dprintk("svc: TCP complete record (%d bytes)\n", len);
1034         rqstp->rq_arg.len = len;
1035         rqstp->rq_arg.page_base = 0;
1036         if (len <= rqstp->rq_arg.head[0].iov_len) {
1037                 rqstp->rq_arg.head[0].iov_len = len;
1038                 rqstp->rq_arg.page_len = 0;
1039         } else {
1040                 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
1041         }
1042
1043         rqstp->rq_skbuff      = NULL;
1044         rqstp->rq_prot        = IPPROTO_TCP;
1045
1046         /* Reset TCP read info */
1047         svsk->sk_reclen = 0;
1048         svsk->sk_tcplen = 0;
1049
1050         svc_sock_received(svsk);
1051         if (serv->sv_stats)
1052                 serv->sv_stats->nettcpcnt++;
1053
1054         return len;
1055
1056  err_delete:
1057         svc_delete_socket(svsk);
1058         return -EAGAIN;
1059
1060  error:
1061         if (len == -EAGAIN) {
1062                 dprintk("RPC: TCP recvfrom got EAGAIN\n");
1063                 svc_sock_received(svsk);
1064         } else {
1065                 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
1066                                         svsk->sk_server->sv_name, -len);
1067                 goto err_delete;
1068         }
1069
1070         return len;
1071 }
1072
1073 /*
1074  * Send out data on TCP socket.
1075  */
1076 static int
1077 svc_tcp_sendto(struct svc_rqst *rqstp)
1078 {
1079         struct xdr_buf  *xbufp = &rqstp->rq_res;
1080         int sent;
1081         __be32 reclen;
1082
1083         /* Set up the first element of the reply kvec.
1084          * Any other kvecs that may be in use have been taken
1085          * care of by the server implementation itself.
1086          */
1087         reclen = htonl(0x80000000|((xbufp->len ) - 4));
1088         memcpy(xbufp->head[0].iov_base, &reclen, 4);
1089
1090         if (test_bit(SK_DEAD, &rqstp->rq_sock->sk_flags))
1091                 return -ENOTCONN;
1092
1093         sent = svc_sendto(rqstp, &rqstp->rq_res);
1094         if (sent != xbufp->len) {
1095                 printk(KERN_NOTICE "rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n",
1096                        rqstp->rq_sock->sk_server->sv_name,
1097                        (sent<0)?"got error":"sent only",
1098                        sent, xbufp->len);
1099                 svc_delete_socket(rqstp->rq_sock);
1100                 sent = -EAGAIN;
1101         }
1102         return sent;
1103 }
1104
1105 static void
1106 svc_tcp_init(struct svc_sock *svsk)
1107 {
1108         struct sock     *sk = svsk->sk_sk;
1109         struct tcp_sock *tp = tcp_sk(sk);
1110
1111         svsk->sk_recvfrom = svc_tcp_recvfrom;
1112         svsk->sk_sendto = svc_tcp_sendto;
1113
1114         if (sk->sk_state == TCP_LISTEN) {
1115                 dprintk("setting up TCP socket for listening\n");
1116                 sk->sk_data_ready = svc_tcp_listen_data_ready;
1117                 set_bit(SK_CONN, &svsk->sk_flags);
1118         } else {
1119                 dprintk("setting up TCP socket for reading\n");
1120                 sk->sk_state_change = svc_tcp_state_change;
1121                 sk->sk_data_ready = svc_tcp_data_ready;
1122                 sk->sk_write_space = svc_write_space;
1123
1124                 svsk->sk_reclen = 0;
1125                 svsk->sk_tcplen = 0;
1126
1127                 tp->nonagle = 1;        /* disable Nagle's algorithm */
1128
1129                 /* initialise setting must have enough space to
1130                  * receive and respond to one request.  
1131                  * svc_tcp_recvfrom will re-adjust if necessary
1132                  */
1133                 svc_sock_setbufsize(svsk->sk_sock,
1134                                     3 * svsk->sk_server->sv_bufsz,
1135                                     3 * svsk->sk_server->sv_bufsz);
1136
1137                 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1138                 set_bit(SK_DATA, &svsk->sk_flags);
1139                 if (sk->sk_state != TCP_ESTABLISHED) 
1140                         set_bit(SK_CLOSE, &svsk->sk_flags);
1141         }
1142 }
1143
1144 void
1145 svc_sock_update_bufs(struct svc_serv *serv)
1146 {
1147         /*
1148          * The number of server threads has changed. Update
1149          * rcvbuf and sndbuf accordingly on all sockets
1150          */
1151         struct list_head *le;
1152
1153         spin_lock_bh(&serv->sv_lock);
1154         list_for_each(le, &serv->sv_permsocks) {
1155                 struct svc_sock *svsk = 
1156                         list_entry(le, struct svc_sock, sk_list);
1157                 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1158         }
1159         list_for_each(le, &serv->sv_tempsocks) {
1160                 struct svc_sock *svsk =
1161                         list_entry(le, struct svc_sock, sk_list);
1162                 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1163         }
1164         spin_unlock_bh(&serv->sv_lock);
1165 }
1166
1167 /*
1168  * Receive the next request on any socket.
1169  */
1170 int
1171 svc_recv(struct svc_rqst *rqstp, long timeout)
1172 {
1173         struct svc_sock         *svsk =NULL;
1174         struct svc_serv         *serv = rqstp->rq_server;
1175         int                     len;
1176         int                     pages;
1177         struct xdr_buf          *arg;
1178         DECLARE_WAITQUEUE(wait, current);
1179
1180         dprintk("svc: server %p waiting for data (to = %ld)\n",
1181                 rqstp, timeout);
1182
1183         if (rqstp->rq_sock)
1184                 printk(KERN_ERR 
1185                         "svc_recv: service %p, socket not NULL!\n",
1186                          rqstp);
1187         if (waitqueue_active(&rqstp->rq_wait))
1188                 printk(KERN_ERR 
1189                         "svc_recv: service %p, wait queue active!\n",
1190                          rqstp);
1191
1192         /* Initialize the buffers */
1193         /* first reclaim pages that were moved to response list */
1194         svc_pushback_allpages(rqstp);
1195
1196         /* now allocate needed pages.  If we get a failure, sleep briefly */
1197         pages = 2 + (serv->sv_bufsz + PAGE_SIZE -1) / PAGE_SIZE;
1198         while (rqstp->rq_arghi < pages) {
1199                 struct page *p = alloc_page(GFP_KERNEL);
1200                 if (!p) {
1201                         schedule_timeout_uninterruptible(msecs_to_jiffies(500));
1202                         continue;
1203                 }
1204                 rqstp->rq_argpages[rqstp->rq_arghi++] = p;
1205         }
1206
1207         /* Make arg->head point to first page and arg->pages point to rest */
1208         arg = &rqstp->rq_arg;
1209         arg->head[0].iov_base = page_address(rqstp->rq_argpages[0]);
1210         arg->head[0].iov_len = PAGE_SIZE;
1211         rqstp->rq_argused = 1;
1212         arg->pages = rqstp->rq_argpages + 1;
1213         arg->page_base = 0;
1214         /* save at least one page for response */
1215         arg->page_len = (pages-2)*PAGE_SIZE;
1216         arg->len = (pages-1)*PAGE_SIZE;
1217         arg->tail[0].iov_len = 0;
1218
1219         try_to_freeze();
1220         cond_resched();
1221         if (signalled())
1222                 return -EINTR;
1223
1224         spin_lock_bh(&serv->sv_lock);
1225         if ((svsk = svc_sock_dequeue(serv)) != NULL) {
1226                 rqstp->rq_sock = svsk;
1227                 atomic_inc(&svsk->sk_inuse);
1228                 rqstp->rq_reserved = serv->sv_bufsz;    
1229                 svsk->sk_reserved += rqstp->rq_reserved;
1230         } else {
1231                 /* No data pending. Go to sleep */
1232                 svc_serv_enqueue(serv, rqstp);
1233
1234                 /*
1235                  * We have to be able to interrupt this wait
1236                  * to bring down the daemons ...
1237                  */
1238                 set_current_state(TASK_INTERRUPTIBLE);
1239                 add_wait_queue(&rqstp->rq_wait, &wait);
1240                 spin_unlock_bh(&serv->sv_lock);
1241
1242                 schedule_timeout(timeout);
1243
1244                 try_to_freeze();
1245
1246                 spin_lock_bh(&serv->sv_lock);
1247                 remove_wait_queue(&rqstp->rq_wait, &wait);
1248
1249                 if (!(svsk = rqstp->rq_sock)) {
1250                         svc_serv_dequeue(serv, rqstp);
1251                         spin_unlock_bh(&serv->sv_lock);
1252                         dprintk("svc: server %p, no data yet\n", rqstp);
1253                         return signalled()? -EINTR : -EAGAIN;
1254                 }
1255         }
1256         spin_unlock_bh(&serv->sv_lock);
1257
1258         dprintk("svc: server %p, socket %p, inuse=%d\n",
1259                  rqstp, svsk, atomic_read(&svsk->sk_inuse));
1260         len = svsk->sk_recvfrom(rqstp);
1261         dprintk("svc: got len=%d\n", len);
1262
1263         /* No data, incomplete (TCP) read, or accept() */
1264         if (len == 0 || len == -EAGAIN) {
1265                 rqstp->rq_res.len = 0;
1266                 svc_sock_release(rqstp);
1267                 return -EAGAIN;
1268         }
1269         svsk->sk_lastrecv = get_seconds();
1270         clear_bit(SK_OLD, &svsk->sk_flags);
1271
1272         rqstp->rq_secure  = ntohs(rqstp->rq_addr.sin_port) < 1024;
1273         rqstp->rq_chandle.defer = svc_defer;
1274
1275         if (serv->sv_stats)
1276                 serv->sv_stats->netcnt++;
1277         return len;
1278 }
1279
1280 /* 
1281  * Drop request
1282  */
1283 void
1284 svc_drop(struct svc_rqst *rqstp)
1285 {
1286         dprintk("svc: socket %p dropped request\n", rqstp->rq_sock);
1287         svc_sock_release(rqstp);
1288 }
1289
1290 /*
1291  * Return reply to client.
1292  */
1293 int
1294 svc_send(struct svc_rqst *rqstp)
1295 {
1296         struct svc_sock *svsk;
1297         int             len;
1298         struct xdr_buf  *xb;
1299
1300         if ((svsk = rqstp->rq_sock) == NULL) {
1301                 printk(KERN_WARNING "NULL socket pointer in %s:%d\n",
1302                                 __FILE__, __LINE__);
1303                 return -EFAULT;
1304         }
1305
1306         /* release the receive skb before sending the reply */
1307         svc_release_skb(rqstp);
1308
1309         /* calculate over-all length */
1310         xb = & rqstp->rq_res;
1311         xb->len = xb->head[0].iov_len +
1312                 xb->page_len +
1313                 xb->tail[0].iov_len;
1314
1315         /* Grab svsk->sk_mutex to serialize outgoing data. */
1316         mutex_lock(&svsk->sk_mutex);
1317         if (test_bit(SK_DEAD, &svsk->sk_flags))
1318                 len = -ENOTCONN;
1319         else
1320                 len = svsk->sk_sendto(rqstp);
1321         mutex_unlock(&svsk->sk_mutex);
1322         svc_sock_release(rqstp);
1323
1324         if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
1325                 return 0;
1326         return len;
1327 }
1328
1329 /*
1330  * Timer function to close old temporary sockets, using
1331  * a mark-and-sweep algorithm.
1332  */
1333 static void
1334 svc_age_temp_sockets(unsigned long closure)
1335 {
1336         struct svc_serv *serv = (struct svc_serv *)closure;
1337         struct svc_sock *svsk;
1338         struct list_head *le, *next;
1339         LIST_HEAD(to_be_aged);
1340
1341         dprintk("svc_age_temp_sockets\n");
1342
1343         if (!spin_trylock_bh(&serv->sv_lock)) {
1344                 /* busy, try again 1 sec later */
1345                 dprintk("svc_age_temp_sockets: busy\n");
1346                 mod_timer(&serv->sv_temptimer, jiffies + HZ);
1347                 return;
1348         }
1349
1350         list_for_each_safe(le, next, &serv->sv_tempsocks) {
1351                 svsk = list_entry(le, struct svc_sock, sk_list);
1352
1353                 if (!test_and_set_bit(SK_OLD, &svsk->sk_flags))
1354                         continue;
1355                 if (atomic_read(&svsk->sk_inuse) || test_bit(SK_BUSY, &svsk->sk_flags))
1356                         continue;
1357                 atomic_inc(&svsk->sk_inuse);
1358                 list_move(le, &to_be_aged);
1359                 set_bit(SK_CLOSE, &svsk->sk_flags);
1360                 set_bit(SK_DETACHED, &svsk->sk_flags);
1361         }
1362         spin_unlock_bh(&serv->sv_lock);
1363
1364         while (!list_empty(&to_be_aged)) {
1365                 le = to_be_aged.next;
1366                 /* fiddling the sk_list node is safe 'cos we're SK_DETACHED */
1367                 list_del_init(le);
1368                 svsk = list_entry(le, struct svc_sock, sk_list);
1369
1370                 dprintk("queuing svsk %p for closing, %lu seconds old\n",
1371                         svsk, get_seconds() - svsk->sk_lastrecv);
1372
1373                 /* a thread will dequeue and close it soon */
1374                 svc_sock_enqueue(svsk);
1375                 svc_sock_put(svsk);
1376         }
1377
1378         mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
1379 }
1380
1381 /*
1382  * Initialize socket for RPC use and create svc_sock struct
1383  * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
1384  */
1385 static struct svc_sock *
1386 svc_setup_socket(struct svc_serv *serv, struct socket *sock,
1387                                         int *errp, int pmap_register)
1388 {
1389         struct svc_sock *svsk;
1390         struct sock     *inet;
1391
1392         dprintk("svc: svc_setup_socket %p\n", sock);
1393         if (!(svsk = kzalloc(sizeof(*svsk), GFP_KERNEL))) {
1394                 *errp = -ENOMEM;
1395                 return NULL;
1396         }
1397
1398         inet = sock->sk;
1399
1400         /* Register socket with portmapper */
1401         if (*errp >= 0 && pmap_register)
1402                 *errp = svc_register(serv, inet->sk_protocol,
1403                                      ntohs(inet_sk(inet)->sport));
1404
1405         if (*errp < 0) {
1406                 kfree(svsk);
1407                 return NULL;
1408         }
1409
1410         set_bit(SK_BUSY, &svsk->sk_flags);
1411         inet->sk_user_data = svsk;
1412         svsk->sk_sock = sock;
1413         svsk->sk_sk = inet;
1414         svsk->sk_ostate = inet->sk_state_change;
1415         svsk->sk_odata = inet->sk_data_ready;
1416         svsk->sk_owspace = inet->sk_write_space;
1417         svsk->sk_server = serv;
1418         atomic_set(&svsk->sk_inuse, 0);
1419         svsk->sk_lastrecv = get_seconds();
1420         spin_lock_init(&svsk->sk_defer_lock);
1421         INIT_LIST_HEAD(&svsk->sk_deferred);
1422         INIT_LIST_HEAD(&svsk->sk_ready);
1423         mutex_init(&svsk->sk_mutex);
1424
1425         /* Initialize the socket */
1426         if (sock->type == SOCK_DGRAM)
1427                 svc_udp_init(svsk);
1428         else
1429                 svc_tcp_init(svsk);
1430
1431         spin_lock_bh(&serv->sv_lock);
1432         if (!pmap_register) {
1433                 set_bit(SK_TEMP, &svsk->sk_flags);
1434                 list_add(&svsk->sk_list, &serv->sv_tempsocks);
1435                 serv->sv_tmpcnt++;
1436                 if (serv->sv_temptimer.function == NULL) {
1437                         /* setup timer to age temp sockets */
1438                         setup_timer(&serv->sv_temptimer, svc_age_temp_sockets,
1439                                         (unsigned long)serv);
1440                         mod_timer(&serv->sv_temptimer,
1441                                         jiffies + svc_conn_age_period * HZ);
1442                 }
1443         } else {
1444                 clear_bit(SK_TEMP, &svsk->sk_flags);
1445                 list_add(&svsk->sk_list, &serv->sv_permsocks);
1446         }
1447         spin_unlock_bh(&serv->sv_lock);
1448
1449         dprintk("svc: svc_setup_socket created %p (inet %p)\n",
1450                                 svsk, svsk->sk_sk);
1451
1452         clear_bit(SK_BUSY, &svsk->sk_flags);
1453         svc_sock_enqueue(svsk);
1454         return svsk;
1455 }
1456
1457 int svc_addsock(struct svc_serv *serv,
1458                 int fd,
1459                 char *name_return,
1460                 int *proto)
1461 {
1462         int err = 0;
1463         struct socket *so = sockfd_lookup(fd, &err);
1464         struct svc_sock *svsk = NULL;
1465
1466         if (!so)
1467                 return err;
1468         if (so->sk->sk_family != AF_INET)
1469                 err =  -EAFNOSUPPORT;
1470         else if (so->sk->sk_protocol != IPPROTO_TCP &&
1471             so->sk->sk_protocol != IPPROTO_UDP)
1472                 err =  -EPROTONOSUPPORT;
1473         else if (so->state > SS_UNCONNECTED)
1474                 err = -EISCONN;
1475         else {
1476                 svsk = svc_setup_socket(serv, so, &err, 1);
1477                 if (svsk)
1478                         err = 0;
1479         }
1480         if (err) {
1481                 sockfd_put(so);
1482                 return err;
1483         }
1484         if (proto) *proto = so->sk->sk_protocol;
1485         return one_sock_name(name_return, svsk);
1486 }
1487 EXPORT_SYMBOL_GPL(svc_addsock);
1488
1489 /*
1490  * Create socket for RPC service.
1491  */
1492 static int
1493 svc_create_socket(struct svc_serv *serv, int protocol, struct sockaddr_in *sin)
1494 {
1495         struct svc_sock *svsk;
1496         struct socket   *sock;
1497         int             error;
1498         int             type;
1499
1500         dprintk("svc: svc_create_socket(%s, %d, %u.%u.%u.%u:%d)\n",
1501                                 serv->sv_program->pg_name, protocol,
1502                                 NIPQUAD(sin->sin_addr.s_addr),
1503                                 ntohs(sin->sin_port));
1504
1505         if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1506                 printk(KERN_WARNING "svc: only UDP and TCP "
1507                                 "sockets supported\n");
1508                 return -EINVAL;
1509         }
1510         type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1511
1512         if ((error = sock_create_kern(PF_INET, type, protocol, &sock)) < 0)
1513                 return error;
1514
1515         if (type == SOCK_STREAM)
1516                 sock->sk->sk_reuse = 1; /* allow address reuse */
1517         error = kernel_bind(sock, (struct sockaddr *) sin,
1518                                         sizeof(*sin));
1519         if (error < 0)
1520                 goto bummer;
1521
1522         if (protocol == IPPROTO_TCP) {
1523                 if ((error = kernel_listen(sock, 64)) < 0)
1524                         goto bummer;
1525         }
1526
1527         if ((svsk = svc_setup_socket(serv, sock, &error, 1)) != NULL)
1528                 return 0;
1529
1530 bummer:
1531         dprintk("svc: svc_create_socket error = %d\n", -error);
1532         sock_release(sock);
1533         return error;
1534 }
1535
1536 /*
1537  * Remove a dead socket
1538  */
1539 void
1540 svc_delete_socket(struct svc_sock *svsk)
1541 {
1542         struct svc_serv *serv;
1543         struct sock     *sk;
1544
1545         dprintk("svc: svc_delete_socket(%p)\n", svsk);
1546
1547         serv = svsk->sk_server;
1548         sk = svsk->sk_sk;
1549
1550         sk->sk_state_change = svsk->sk_ostate;
1551         sk->sk_data_ready = svsk->sk_odata;
1552         sk->sk_write_space = svsk->sk_owspace;
1553
1554         spin_lock_bh(&serv->sv_lock);
1555
1556         if (!test_and_set_bit(SK_DETACHED, &svsk->sk_flags))
1557                 list_del_init(&svsk->sk_list);
1558         list_del_init(&svsk->sk_ready);
1559         if (!test_and_set_bit(SK_DEAD, &svsk->sk_flags))
1560                 if (test_bit(SK_TEMP, &svsk->sk_flags))
1561                         serv->sv_tmpcnt--;
1562
1563         if (!atomic_read(&svsk->sk_inuse)) {
1564                 spin_unlock_bh(&serv->sv_lock);
1565                 if (svsk->sk_sock->file)
1566                         sockfd_put(svsk->sk_sock);
1567                 else
1568                         sock_release(svsk->sk_sock);
1569                 kfree(svsk);
1570         } else {
1571                 spin_unlock_bh(&serv->sv_lock);
1572                 dprintk(KERN_NOTICE "svc: server socket destroy delayed\n");
1573                 /* svsk->sk_server = NULL; */
1574         }
1575 }
1576
1577 /*
1578  * Make a socket for nfsd and lockd
1579  */
1580 int
1581 svc_makesock(struct svc_serv *serv, int protocol, unsigned short port)
1582 {
1583         struct sockaddr_in      sin;
1584
1585         dprintk("svc: creating socket proto = %d\n", protocol);
1586         sin.sin_family      = AF_INET;
1587         sin.sin_addr.s_addr = INADDR_ANY;
1588         sin.sin_port        = htons(port);
1589         return svc_create_socket(serv, protocol, &sin);
1590 }
1591
1592 /*
1593  * Handle defer and revisit of requests 
1594  */
1595
1596 static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
1597 {
1598         struct svc_deferred_req *dr = container_of(dreq, struct svc_deferred_req, handle);
1599         struct svc_sock *svsk;
1600
1601         if (too_many) {
1602                 svc_sock_put(dr->svsk);
1603                 kfree(dr);
1604                 return;
1605         }
1606         dprintk("revisit queued\n");
1607         svsk = dr->svsk;
1608         dr->svsk = NULL;
1609         spin_lock_bh(&svsk->sk_defer_lock);
1610         list_add(&dr->handle.recent, &svsk->sk_deferred);
1611         spin_unlock_bh(&svsk->sk_defer_lock);
1612         set_bit(SK_DEFERRED, &svsk->sk_flags);
1613         svc_sock_enqueue(svsk);
1614         svc_sock_put(svsk);
1615 }
1616
1617 static struct cache_deferred_req *
1618 svc_defer(struct cache_req *req)
1619 {
1620         struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
1621         int size = sizeof(struct svc_deferred_req) + (rqstp->rq_arg.len);
1622         struct svc_deferred_req *dr;
1623
1624         if (rqstp->rq_arg.page_len)
1625                 return NULL; /* if more than a page, give up FIXME */
1626         if (rqstp->rq_deferred) {
1627                 dr = rqstp->rq_deferred;
1628                 rqstp->rq_deferred = NULL;
1629         } else {
1630                 int skip  = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
1631                 /* FIXME maybe discard if size too large */
1632                 dr = kmalloc(size, GFP_KERNEL);
1633                 if (dr == NULL)
1634                         return NULL;
1635
1636                 dr->handle.owner = rqstp->rq_server;
1637                 dr->prot = rqstp->rq_prot;
1638                 dr->addr = rqstp->rq_addr;
1639                 dr->daddr = rqstp->rq_daddr;
1640                 dr->argslen = rqstp->rq_arg.len >> 2;
1641                 memcpy(dr->args, rqstp->rq_arg.head[0].iov_base-skip, dr->argslen<<2);
1642         }
1643         atomic_inc(&rqstp->rq_sock->sk_inuse);
1644         dr->svsk = rqstp->rq_sock;
1645
1646         dr->handle.revisit = svc_revisit;
1647         return &dr->handle;
1648 }
1649
1650 /*
1651  * recv data from a deferred request into an active one
1652  */
1653 static int svc_deferred_recv(struct svc_rqst *rqstp)
1654 {
1655         struct svc_deferred_req *dr = rqstp->rq_deferred;
1656
1657         rqstp->rq_arg.head[0].iov_base = dr->args;
1658         rqstp->rq_arg.head[0].iov_len = dr->argslen<<2;
1659         rqstp->rq_arg.page_len = 0;
1660         rqstp->rq_arg.len = dr->argslen<<2;
1661         rqstp->rq_prot        = dr->prot;
1662         rqstp->rq_addr        = dr->addr;
1663         rqstp->rq_daddr       = dr->daddr;
1664         return dr->argslen<<2;
1665 }
1666
1667
1668 static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk)
1669 {
1670         struct svc_deferred_req *dr = NULL;
1671         
1672         if (!test_bit(SK_DEFERRED, &svsk->sk_flags))
1673                 return NULL;
1674         spin_lock_bh(&svsk->sk_defer_lock);
1675         clear_bit(SK_DEFERRED, &svsk->sk_flags);
1676         if (!list_empty(&svsk->sk_deferred)) {
1677                 dr = list_entry(svsk->sk_deferred.next,
1678                                 struct svc_deferred_req,
1679                                 handle.recent);
1680                 list_del_init(&dr->handle.recent);
1681                 set_bit(SK_DEFERRED, &svsk->sk_flags);
1682         }
1683         spin_unlock_bh(&svsk->sk_defer_lock);
1684         return dr;
1685 }