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