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