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