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