2 * linux/net/sunrpc/svcsock.c
4 * These are the RPC server socket internals.
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...
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.
19 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
22 #include <linux/sched.h>
23 #include <linux/errno.h>
24 #include <linux/fcntl.h>
25 #include <linux/net.h>
27 #include <linux/inet.h>
28 #include <linux/udp.h>
29 #include <linux/tcp.h>
30 #include <linux/unistd.h>
31 #include <linux/slab.h>
32 #include <linux/netdevice.h>
33 #include <linux/skbuff.h>
34 #include <linux/file.h>
35 #include <linux/freezer.h>
37 #include <net/checksum.h>
39 #include <net/tcp_states.h>
40 #include <asm/uaccess.h>
41 #include <asm/ioctls.h>
43 #include <linux/sunrpc/types.h>
44 #include <linux/sunrpc/xdr.h>
45 #include <linux/sunrpc/svcsock.h>
46 #include <linux/sunrpc/stats.h>
48 /* SMP locking strategy:
50 * svc_pool->sp_lock protects most of the fields of that pool.
51 * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt.
52 * when both need to be taken (rare), svc_serv->sv_lock is first.
53 * BKL protects svc_serv->sv_nrthread.
54 * svc_sock->sk_defer_lock protects the svc_sock->sk_deferred list
55 * svc_sock->sk_flags.SK_BUSY prevents a svc_sock being enqueued multiply.
57 * Some flags can be set to certain values at any time
58 * providing that certain rules are followed:
60 * SK_CONN, SK_DATA, can be set or cleared at any time.
61 * after a set, svc_sock_enqueue must be called.
62 * after a clear, the socket must be read/accepted
63 * if this succeeds, it must be set again.
64 * SK_CLOSE can set at any time. It is never cleared.
65 * sk_inuse contains a bias of '1' until SK_DEAD is set.
66 * so when sk_inuse hits zero, we know the socket is dead
67 * and no-one is using it.
68 * SK_DEAD can only be set while SK_BUSY is held which ensures
69 * no other thread will be using the socket or will try to
74 #define RPCDBG_FACILITY RPCDBG_SVCSOCK
77 static struct svc_sock
*svc_setup_socket(struct svc_serv
*, struct socket
*,
78 int *errp
, int pmap_reg
);
79 static void svc_delete_socket(struct svc_sock
*svsk
);
80 static void svc_udp_data_ready(struct sock
*, int);
81 static int svc_udp_recvfrom(struct svc_rqst
*);
82 static int svc_udp_sendto(struct svc_rqst
*);
83 static void svc_close_socket(struct svc_sock
*svsk
);
85 static struct svc_deferred_req
*svc_deferred_dequeue(struct svc_sock
*svsk
);
86 static int svc_deferred_recv(struct svc_rqst
*rqstp
);
87 static struct cache_deferred_req
*svc_defer(struct cache_req
*req
);
89 /* apparently the "standard" is that clients close
90 * idle connections after 5 minutes, servers after
92 * http://www.connectathon.org/talks96/nfstcp.pdf
94 static int svc_conn_age_period
= 6*60;
96 #ifdef CONFIG_DEBUG_LOCK_ALLOC
97 static struct lock_class_key svc_key
[2];
98 static struct lock_class_key svc_slock_key
[2];
100 static inline void svc_reclassify_socket(struct socket
*sock
)
102 struct sock
*sk
= sock
->sk
;
103 BUG_ON(sk
->sk_lock
.owner
!= NULL
);
104 switch (sk
->sk_family
) {
106 sock_lock_init_class_and_name(sk
, "slock-AF_INET-NFSD",
107 &svc_slock_key
[0], "sk_lock-AF_INET-NFSD", &svc_key
[0]);
111 sock_lock_init_class_and_name(sk
, "slock-AF_INET6-NFSD",
112 &svc_slock_key
[1], "sk_lock-AF_INET6-NFSD", &svc_key
[1]);
120 static inline void svc_reclassify_socket(struct socket
*sock
)
126 * Queue up an idle server thread. Must have pool->sp_lock held.
127 * Note: this is really a stack rather than a queue, so that we only
128 * use as many different threads as we need, and the rest don't pollute
132 svc_thread_enqueue(struct svc_pool
*pool
, struct svc_rqst
*rqstp
)
134 list_add(&rqstp
->rq_list
, &pool
->sp_threads
);
138 * Dequeue an nfsd thread. Must have pool->sp_lock held.
141 svc_thread_dequeue(struct svc_pool
*pool
, struct svc_rqst
*rqstp
)
143 list_del(&rqstp
->rq_list
);
147 * Release an skbuff after use
150 svc_release_skb(struct svc_rqst
*rqstp
)
152 struct sk_buff
*skb
= rqstp
->rq_skbuff
;
153 struct svc_deferred_req
*dr
= rqstp
->rq_deferred
;
156 rqstp
->rq_skbuff
= NULL
;
158 dprintk("svc: service %p, releasing skb %p\n", rqstp
, skb
);
159 skb_free_datagram(rqstp
->rq_sock
->sk_sk
, skb
);
162 rqstp
->rq_deferred
= NULL
;
168 * Any space to write?
170 static inline unsigned long
171 svc_sock_wspace(struct svc_sock
*svsk
)
175 if (svsk
->sk_sock
->type
== SOCK_STREAM
)
176 wspace
= sk_stream_wspace(svsk
->sk_sk
);
178 wspace
= sock_wspace(svsk
->sk_sk
);
184 * Queue up a socket with data pending. If there are idle nfsd
185 * processes, wake 'em up.
189 svc_sock_enqueue(struct svc_sock
*svsk
)
191 struct svc_serv
*serv
= svsk
->sk_server
;
192 struct svc_pool
*pool
;
193 struct svc_rqst
*rqstp
;
196 if (!(svsk
->sk_flags
&
197 ( (1<<SK_CONN
)|(1<<SK_DATA
)|(1<<SK_CLOSE
)|(1<<SK_DEFERRED
)) ))
199 if (test_bit(SK_DEAD
, &svsk
->sk_flags
))
203 pool
= svc_pool_for_cpu(svsk
->sk_server
, cpu
);
206 spin_lock_bh(&pool
->sp_lock
);
208 if (!list_empty(&pool
->sp_threads
) &&
209 !list_empty(&pool
->sp_sockets
))
211 "svc_sock_enqueue: threads and sockets both waiting??\n");
213 if (test_bit(SK_DEAD
, &svsk
->sk_flags
)) {
214 /* Don't enqueue dead sockets */
215 dprintk("svc: socket %p is dead, not enqueued\n", svsk
->sk_sk
);
219 /* Mark socket as busy. It will remain in this state until the
220 * server has processed all pending data and put the socket back
221 * on the idle list. We update SK_BUSY atomically because
222 * it also guards against trying to enqueue the svc_sock twice.
224 if (test_and_set_bit(SK_BUSY
, &svsk
->sk_flags
)) {
225 /* Don't enqueue socket while already enqueued */
226 dprintk("svc: socket %p busy, not enqueued\n", svsk
->sk_sk
);
229 BUG_ON(svsk
->sk_pool
!= NULL
);
230 svsk
->sk_pool
= pool
;
232 set_bit(SOCK_NOSPACE
, &svsk
->sk_sock
->flags
);
233 if (((atomic_read(&svsk
->sk_reserved
) + serv
->sv_max_mesg
)*2
234 > svc_sock_wspace(svsk
))
235 && !test_bit(SK_CLOSE
, &svsk
->sk_flags
)
236 && !test_bit(SK_CONN
, &svsk
->sk_flags
)) {
237 /* Don't enqueue while not enough space for reply */
238 dprintk("svc: socket %p no space, %d*2 > %ld, not enqueued\n",
239 svsk
->sk_sk
, atomic_read(&svsk
->sk_reserved
)+serv
->sv_max_mesg
,
240 svc_sock_wspace(svsk
));
241 svsk
->sk_pool
= NULL
;
242 clear_bit(SK_BUSY
, &svsk
->sk_flags
);
245 clear_bit(SOCK_NOSPACE
, &svsk
->sk_sock
->flags
);
248 if (!list_empty(&pool
->sp_threads
)) {
249 rqstp
= list_entry(pool
->sp_threads
.next
,
252 dprintk("svc: socket %p served by daemon %p\n",
254 svc_thread_dequeue(pool
, rqstp
);
257 "svc_sock_enqueue: server %p, rq_sock=%p!\n",
258 rqstp
, rqstp
->rq_sock
);
259 rqstp
->rq_sock
= svsk
;
260 atomic_inc(&svsk
->sk_inuse
);
261 rqstp
->rq_reserved
= serv
->sv_max_mesg
;
262 atomic_add(rqstp
->rq_reserved
, &svsk
->sk_reserved
);
263 BUG_ON(svsk
->sk_pool
!= pool
);
264 wake_up(&rqstp
->rq_wait
);
266 dprintk("svc: socket %p put into queue\n", svsk
->sk_sk
);
267 list_add_tail(&svsk
->sk_ready
, &pool
->sp_sockets
);
268 BUG_ON(svsk
->sk_pool
!= pool
);
272 spin_unlock_bh(&pool
->sp_lock
);
276 * Dequeue the first socket. Must be called with the pool->sp_lock held.
278 static inline struct svc_sock
*
279 svc_sock_dequeue(struct svc_pool
*pool
)
281 struct svc_sock
*svsk
;
283 if (list_empty(&pool
->sp_sockets
))
286 svsk
= list_entry(pool
->sp_sockets
.next
,
287 struct svc_sock
, sk_ready
);
288 list_del_init(&svsk
->sk_ready
);
290 dprintk("svc: socket %p dequeued, inuse=%d\n",
291 svsk
->sk_sk
, atomic_read(&svsk
->sk_inuse
));
297 * Having read something from a socket, check whether it
298 * needs to be re-enqueued.
299 * Note: SK_DATA only gets cleared when a read-attempt finds
300 * no (or insufficient) data.
303 svc_sock_received(struct svc_sock
*svsk
)
305 svsk
->sk_pool
= NULL
;
306 clear_bit(SK_BUSY
, &svsk
->sk_flags
);
307 svc_sock_enqueue(svsk
);
312 * svc_reserve - change the space reserved for the reply to a request.
313 * @rqstp: The request in question
314 * @space: new max space to reserve
316 * Each request reserves some space on the output queue of the socket
317 * to make sure the reply fits. This function reduces that reserved
318 * space to be the amount of space used already, plus @space.
321 void svc_reserve(struct svc_rqst
*rqstp
, int space
)
323 space
+= rqstp
->rq_res
.head
[0].iov_len
;
325 if (space
< rqstp
->rq_reserved
) {
326 struct svc_sock
*svsk
= rqstp
->rq_sock
;
327 atomic_sub((rqstp
->rq_reserved
- space
), &svsk
->sk_reserved
);
328 rqstp
->rq_reserved
= space
;
330 svc_sock_enqueue(svsk
);
335 * Release a socket after use.
338 svc_sock_put(struct svc_sock
*svsk
)
340 if (atomic_dec_and_test(&svsk
->sk_inuse
)) {
341 BUG_ON(! test_bit(SK_DEAD
, &svsk
->sk_flags
));
343 dprintk("svc: releasing dead socket\n");
344 if (svsk
->sk_sock
->file
)
345 sockfd_put(svsk
->sk_sock
);
347 sock_release(svsk
->sk_sock
);
348 if (svsk
->sk_info_authunix
!= NULL
)
349 svcauth_unix_info_release(svsk
->sk_info_authunix
);
355 svc_sock_release(struct svc_rqst
*rqstp
)
357 struct svc_sock
*svsk
= rqstp
->rq_sock
;
359 svc_release_skb(rqstp
);
361 svc_free_res_pages(rqstp
);
362 rqstp
->rq_res
.page_len
= 0;
363 rqstp
->rq_res
.page_base
= 0;
366 /* Reset response buffer and release
368 * But first, check that enough space was reserved
369 * for the reply, otherwise we have a bug!
371 if ((rqstp
->rq_res
.len
) > rqstp
->rq_reserved
)
372 printk(KERN_ERR
"RPC request reserved %d but used %d\n",
376 rqstp
->rq_res
.head
[0].iov_len
= 0;
377 svc_reserve(rqstp
, 0);
378 rqstp
->rq_sock
= NULL
;
384 * External function to wake up a server waiting for data
385 * This really only makes sense for services like lockd
386 * which have exactly one thread anyway.
389 svc_wake_up(struct svc_serv
*serv
)
391 struct svc_rqst
*rqstp
;
393 struct svc_pool
*pool
;
395 for (i
= 0; i
< serv
->sv_nrpools
; i
++) {
396 pool
= &serv
->sv_pools
[i
];
398 spin_lock_bh(&pool
->sp_lock
);
399 if (!list_empty(&pool
->sp_threads
)) {
400 rqstp
= list_entry(pool
->sp_threads
.next
,
403 dprintk("svc: daemon %p woken up.\n", rqstp
);
405 svc_thread_dequeue(pool, rqstp);
406 rqstp->rq_sock = NULL;
408 wake_up(&rqstp
->rq_wait
);
410 spin_unlock_bh(&pool
->sp_lock
);
415 * Generic sendto routine
418 svc_sendto(struct svc_rqst
*rqstp
, struct xdr_buf
*xdr
)
420 struct svc_sock
*svsk
= rqstp
->rq_sock
;
421 struct socket
*sock
= svsk
->sk_sock
;
423 char buffer
[CMSG_SPACE(sizeof(struct in_pktinfo
))];
424 struct cmsghdr
*cmh
= (struct cmsghdr
*)buffer
;
425 struct in_pktinfo
*pki
= (struct in_pktinfo
*)CMSG_DATA(cmh
);
429 struct page
**ppage
= xdr
->pages
;
430 size_t base
= xdr
->page_base
;
431 unsigned int pglen
= xdr
->page_len
;
432 unsigned int flags
= MSG_MORE
;
436 if (rqstp
->rq_prot
== IPPROTO_UDP
) {
437 /* set the source and destination */
439 msg
.msg_name
= &rqstp
->rq_addr
;
440 msg
.msg_namelen
= sizeof(rqstp
->rq_addr
);
443 msg
.msg_flags
= MSG_MORE
;
445 msg
.msg_control
= cmh
;
446 msg
.msg_controllen
= sizeof(buffer
);
447 cmh
->cmsg_len
= CMSG_LEN(sizeof(*pki
));
448 cmh
->cmsg_level
= SOL_IP
;
449 cmh
->cmsg_type
= IP_PKTINFO
;
450 pki
->ipi_ifindex
= 0;
451 pki
->ipi_spec_dst
.s_addr
= rqstp
->rq_daddr
;
453 if (sock_sendmsg(sock
, &msg
, 0) < 0)
458 if (slen
== xdr
->head
[0].iov_len
)
460 len
= kernel_sendpage(sock
, rqstp
->rq_respages
[0], 0,
461 xdr
->head
[0].iov_len
, flags
);
462 if (len
!= xdr
->head
[0].iov_len
)
464 slen
-= xdr
->head
[0].iov_len
;
469 size
= PAGE_SIZE
- base
< pglen
? PAGE_SIZE
- base
: pglen
;
473 result
= kernel_sendpage(sock
, *ppage
, base
, size
, flags
);
480 size
= PAGE_SIZE
< pglen
? PAGE_SIZE
: pglen
;
485 if (xdr
->tail
[0].iov_len
) {
486 result
= kernel_sendpage(sock
, rqstp
->rq_respages
[0],
487 ((unsigned long)xdr
->tail
[0].iov_base
)
489 xdr
->tail
[0].iov_len
, 0);
495 dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %x)\n",
496 rqstp
->rq_sock
, xdr
->head
[0].iov_base
, xdr
->head
[0].iov_len
, xdr
->len
, len
,
497 rqstp
->rq_addr
.sin_addr
.s_addr
);
503 * Report socket names for nfsdfs
505 static int one_sock_name(char *buf
, struct svc_sock
*svsk
)
509 switch(svsk
->sk_sk
->sk_family
) {
511 len
= sprintf(buf
, "ipv4 %s %u.%u.%u.%u %d\n",
512 svsk
->sk_sk
->sk_protocol
==IPPROTO_UDP
?
514 NIPQUAD(inet_sk(svsk
->sk_sk
)->rcv_saddr
),
515 inet_sk(svsk
->sk_sk
)->num
);
518 len
= sprintf(buf
, "*unknown-%d*\n",
519 svsk
->sk_sk
->sk_family
);
525 svc_sock_names(char *buf
, struct svc_serv
*serv
, char *toclose
)
527 struct svc_sock
*svsk
, *closesk
= NULL
;
532 spin_lock_bh(&serv
->sv_lock
);
533 list_for_each_entry(svsk
, &serv
->sv_permsocks
, sk_list
) {
534 int onelen
= one_sock_name(buf
+len
, svsk
);
535 if (toclose
&& strcmp(toclose
, buf
+len
) == 0)
540 spin_unlock_bh(&serv
->sv_lock
);
542 /* Should unregister with portmap, but you cannot
543 * unregister just one protocol...
545 svc_close_socket(closesk
);
550 EXPORT_SYMBOL(svc_sock_names
);
553 * Check input queue length
556 svc_recv_available(struct svc_sock
*svsk
)
558 struct socket
*sock
= svsk
->sk_sock
;
561 err
= kernel_sock_ioctl(sock
, TIOCINQ
, (unsigned long) &avail
);
563 return (err
>= 0)? avail
: err
;
567 * Generic recvfrom routine.
570 svc_recvfrom(struct svc_rqst
*rqstp
, struct kvec
*iov
, int nr
, int buflen
)
576 rqstp
->rq_addrlen
= sizeof(rqstp
->rq_addr
);
577 sock
= rqstp
->rq_sock
->sk_sock
;
579 msg
.msg_name
= &rqstp
->rq_addr
;
580 msg
.msg_namelen
= sizeof(rqstp
->rq_addr
);
581 msg
.msg_control
= NULL
;
582 msg
.msg_controllen
= 0;
584 msg
.msg_flags
= MSG_DONTWAIT
;
586 len
= kernel_recvmsg(sock
, &msg
, iov
, nr
, buflen
, MSG_DONTWAIT
);
588 /* sock_recvmsg doesn't fill in the name/namelen, so we must..
589 * possibly we should cache this in the svc_sock structure
590 * at accept time. FIXME
592 alen
= sizeof(rqstp
->rq_addr
);
593 kernel_getpeername(sock
, (struct sockaddr
*)&rqstp
->rq_addr
, &alen
);
595 dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
596 rqstp
->rq_sock
, iov
[0].iov_base
, iov
[0].iov_len
, len
);
602 * Set socket snd and rcv buffer lengths
605 svc_sock_setbufsize(struct socket
*sock
, unsigned int snd
, unsigned int rcv
)
609 oldfs
= get_fs(); set_fs(KERNEL_DS
);
610 sock_setsockopt(sock
, SOL_SOCKET
, SO_SNDBUF
,
611 (char*)&snd
, sizeof(snd
));
612 sock_setsockopt(sock
, SOL_SOCKET
, SO_RCVBUF
,
613 (char*)&rcv
, sizeof(rcv
));
615 /* sock_setsockopt limits use to sysctl_?mem_max,
616 * which isn't acceptable. Until that is made conditional
617 * on not having CAP_SYS_RESOURCE or similar, we go direct...
618 * DaveM said I could!
621 sock
->sk
->sk_sndbuf
= snd
* 2;
622 sock
->sk
->sk_rcvbuf
= rcv
* 2;
623 sock
->sk
->sk_userlocks
|= SOCK_SNDBUF_LOCK
|SOCK_RCVBUF_LOCK
;
624 release_sock(sock
->sk
);
628 * INET callback when data has been received on the socket.
631 svc_udp_data_ready(struct sock
*sk
, int count
)
633 struct svc_sock
*svsk
= (struct svc_sock
*)sk
->sk_user_data
;
636 dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
637 svsk
, sk
, count
, test_bit(SK_BUSY
, &svsk
->sk_flags
));
638 set_bit(SK_DATA
, &svsk
->sk_flags
);
639 svc_sock_enqueue(svsk
);
641 if (sk
->sk_sleep
&& waitqueue_active(sk
->sk_sleep
))
642 wake_up_interruptible(sk
->sk_sleep
);
646 * INET callback when space is newly available on the socket.
649 svc_write_space(struct sock
*sk
)
651 struct svc_sock
*svsk
= (struct svc_sock
*)(sk
->sk_user_data
);
654 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
655 svsk
, sk
, test_bit(SK_BUSY
, &svsk
->sk_flags
));
656 svc_sock_enqueue(svsk
);
659 if (sk
->sk_sleep
&& waitqueue_active(sk
->sk_sleep
)) {
660 dprintk("RPC svc_write_space: someone sleeping on %p\n",
662 wake_up_interruptible(sk
->sk_sleep
);
667 * Receive a datagram from a UDP socket.
670 svc_udp_recvfrom(struct svc_rqst
*rqstp
)
672 struct svc_sock
*svsk
= rqstp
->rq_sock
;
673 struct svc_serv
*serv
= svsk
->sk_server
;
677 if (test_and_clear_bit(SK_CHNGBUF
, &svsk
->sk_flags
))
678 /* udp sockets need large rcvbuf as all pending
679 * requests are still in that buffer. sndbuf must
680 * also be large enough that there is enough space
681 * for one reply per thread. We count all threads
682 * rather than threads in a particular pool, which
683 * provides an upper bound on the number of threads
684 * which will access the socket.
686 svc_sock_setbufsize(svsk
->sk_sock
,
687 (serv
->sv_nrthreads
+3) * serv
->sv_max_mesg
,
688 (serv
->sv_nrthreads
+3) * serv
->sv_max_mesg
);
690 if ((rqstp
->rq_deferred
= svc_deferred_dequeue(svsk
))) {
691 svc_sock_received(svsk
);
692 return svc_deferred_recv(rqstp
);
695 if (test_bit(SK_CLOSE
, &svsk
->sk_flags
)) {
696 svc_delete_socket(svsk
);
700 clear_bit(SK_DATA
, &svsk
->sk_flags
);
701 while ((skb
= skb_recv_datagram(svsk
->sk_sk
, 0, 1, &err
)) == NULL
) {
702 if (err
== -EAGAIN
) {
703 svc_sock_received(svsk
);
706 /* possibly an icmp error */
707 dprintk("svc: recvfrom returned error %d\n", -err
);
709 if (skb
->tstamp
.off_sec
== 0) {
712 tv
.tv_sec
= xtime
.tv_sec
;
713 tv
.tv_usec
= xtime
.tv_nsec
/ NSEC_PER_USEC
;
714 skb_set_timestamp(skb
, &tv
);
715 /* Don't enable netstamp, sunrpc doesn't
716 need that much accuracy */
718 skb_get_timestamp(skb
, &svsk
->sk_sk
->sk_stamp
);
719 set_bit(SK_DATA
, &svsk
->sk_flags
); /* there may be more data... */
722 * Maybe more packets - kick another thread ASAP.
724 svc_sock_received(svsk
);
726 len
= skb
->len
- sizeof(struct udphdr
);
727 rqstp
->rq_arg
.len
= len
;
729 rqstp
->rq_prot
= IPPROTO_UDP
;
731 /* Get sender address */
732 rqstp
->rq_addr
.sin_family
= AF_INET
;
733 rqstp
->rq_addr
.sin_port
= skb
->h
.uh
->source
;
734 rqstp
->rq_addr
.sin_addr
.s_addr
= skb
->nh
.iph
->saddr
;
735 rqstp
->rq_daddr
= skb
->nh
.iph
->daddr
;
737 if (skb_is_nonlinear(skb
)) {
738 /* we have to copy */
740 if (csum_partial_copy_to_xdr(&rqstp
->rq_arg
, skb
)) {
743 skb_free_datagram(svsk
->sk_sk
, skb
);
747 skb_free_datagram(svsk
->sk_sk
, skb
);
749 /* we can use it in-place */
750 rqstp
->rq_arg
.head
[0].iov_base
= skb
->data
+ sizeof(struct udphdr
);
751 rqstp
->rq_arg
.head
[0].iov_len
= len
;
752 if (skb_checksum_complete(skb
)) {
753 skb_free_datagram(svsk
->sk_sk
, skb
);
756 rqstp
->rq_skbuff
= skb
;
759 rqstp
->rq_arg
.page_base
= 0;
760 if (len
<= rqstp
->rq_arg
.head
[0].iov_len
) {
761 rqstp
->rq_arg
.head
[0].iov_len
= len
;
762 rqstp
->rq_arg
.page_len
= 0;
763 rqstp
->rq_respages
= rqstp
->rq_pages
+1;
765 rqstp
->rq_arg
.page_len
= len
- rqstp
->rq_arg
.head
[0].iov_len
;
766 rqstp
->rq_respages
= rqstp
->rq_pages
+ 1 +
767 (rqstp
->rq_arg
.page_len
+ PAGE_SIZE
- 1)/ PAGE_SIZE
;
771 serv
->sv_stats
->netudpcnt
++;
777 svc_udp_sendto(struct svc_rqst
*rqstp
)
781 error
= svc_sendto(rqstp
, &rqstp
->rq_res
);
782 if (error
== -ECONNREFUSED
)
783 /* ICMP error on earlier request. */
784 error
= svc_sendto(rqstp
, &rqstp
->rq_res
);
790 svc_udp_init(struct svc_sock
*svsk
)
792 svsk
->sk_sk
->sk_data_ready
= svc_udp_data_ready
;
793 svsk
->sk_sk
->sk_write_space
= svc_write_space
;
794 svsk
->sk_recvfrom
= svc_udp_recvfrom
;
795 svsk
->sk_sendto
= svc_udp_sendto
;
797 /* initialise setting must have enough space to
798 * receive and respond to one request.
799 * svc_udp_recvfrom will re-adjust if necessary
801 svc_sock_setbufsize(svsk
->sk_sock
,
802 3 * svsk
->sk_server
->sv_max_mesg
,
803 3 * svsk
->sk_server
->sv_max_mesg
);
805 set_bit(SK_DATA
, &svsk
->sk_flags
); /* might have come in before data_ready set up */
806 set_bit(SK_CHNGBUF
, &svsk
->sk_flags
);
810 * A data_ready event on a listening socket means there's a connection
811 * pending. Do not use state_change as a substitute for it.
814 svc_tcp_listen_data_ready(struct sock
*sk
, int count_unused
)
816 struct svc_sock
*svsk
= (struct svc_sock
*)sk
->sk_user_data
;
818 dprintk("svc: socket %p TCP (listen) state change %d\n",
822 * This callback may called twice when a new connection
823 * is established as a child socket inherits everything
824 * from a parent LISTEN socket.
825 * 1) data_ready method of the parent socket will be called
826 * when one of child sockets become ESTABLISHED.
827 * 2) data_ready method of the child socket may be called
828 * when it receives data before the socket is accepted.
829 * In case of 2, we should ignore it silently.
831 if (sk
->sk_state
== TCP_LISTEN
) {
833 set_bit(SK_CONN
, &svsk
->sk_flags
);
834 svc_sock_enqueue(svsk
);
836 printk("svc: socket %p: no user data\n", sk
);
839 if (sk
->sk_sleep
&& waitqueue_active(sk
->sk_sleep
))
840 wake_up_interruptible_all(sk
->sk_sleep
);
844 * A state change on a connected socket means it's dying or dead.
847 svc_tcp_state_change(struct sock
*sk
)
849 struct svc_sock
*svsk
= (struct svc_sock
*)sk
->sk_user_data
;
851 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
852 sk
, sk
->sk_state
, sk
->sk_user_data
);
855 printk("svc: socket %p: no user data\n", sk
);
857 set_bit(SK_CLOSE
, &svsk
->sk_flags
);
858 svc_sock_enqueue(svsk
);
860 if (sk
->sk_sleep
&& waitqueue_active(sk
->sk_sleep
))
861 wake_up_interruptible_all(sk
->sk_sleep
);
865 svc_tcp_data_ready(struct sock
*sk
, int count
)
867 struct svc_sock
*svsk
= (struct svc_sock
*)sk
->sk_user_data
;
869 dprintk("svc: socket %p TCP data ready (svsk %p)\n",
870 sk
, sk
->sk_user_data
);
872 set_bit(SK_DATA
, &svsk
->sk_flags
);
873 svc_sock_enqueue(svsk
);
875 if (sk
->sk_sleep
&& waitqueue_active(sk
->sk_sleep
))
876 wake_up_interruptible(sk
->sk_sleep
);
880 * Accept a TCP connection
883 svc_tcp_accept(struct svc_sock
*svsk
)
885 struct sockaddr_in sin
;
886 struct svc_serv
*serv
= svsk
->sk_server
;
887 struct socket
*sock
= svsk
->sk_sock
;
888 struct socket
*newsock
;
889 struct svc_sock
*newsvsk
;
892 dprintk("svc: tcp_accept %p sock %p\n", svsk
, sock
);
896 clear_bit(SK_CONN
, &svsk
->sk_flags
);
897 err
= kernel_accept(sock
, &newsock
, O_NONBLOCK
);
900 printk(KERN_WARNING
"%s: no more sockets!\n",
902 else if (err
!= -EAGAIN
&& net_ratelimit())
903 printk(KERN_WARNING
"%s: accept failed (err %d)!\n",
904 serv
->sv_name
, -err
);
908 set_bit(SK_CONN
, &svsk
->sk_flags
);
909 svc_sock_enqueue(svsk
);
912 err
= kernel_getpeername(newsock
, (struct sockaddr
*) &sin
, &slen
);
915 printk(KERN_WARNING
"%s: peername failed (err %d)!\n",
916 serv
->sv_name
, -err
);
917 goto failed
; /* aborted connection or whatever */
920 /* Ideally, we would want to reject connections from unauthorized
921 * hosts here, but when we get encription, the IP of the host won't
922 * tell us anything. For now just warn about unpriv connections.
924 if (ntohs(sin
.sin_port
) >= 1024) {
926 "%s: connect from unprivileged port: %u.%u.%u.%u:%d\n",
928 NIPQUAD(sin
.sin_addr
.s_addr
), ntohs(sin
.sin_port
));
931 dprintk("%s: connect from %u.%u.%u.%u:%04x\n", serv
->sv_name
,
932 NIPQUAD(sin
.sin_addr
.s_addr
), ntohs(sin
.sin_port
));
934 /* make sure that a write doesn't block forever when
937 newsock
->sk
->sk_sndtimeo
= HZ
*30;
939 if (!(newsvsk
= svc_setup_socket(serv
, newsock
, &err
, 0)))
943 /* make sure that we don't have too many active connections.
944 * If we have, something must be dropped.
946 * There's no point in trying to do random drop here for
947 * DoS prevention. The NFS clients does 1 reconnect in 15
948 * seconds. An attacker can easily beat that.
950 * The only somewhat efficient mechanism would be if drop
951 * old connections from the same IP first. But right now
952 * we don't even record the client IP in svc_sock.
954 if (serv
->sv_tmpcnt
> (serv
->sv_nrthreads
+3)*20) {
955 struct svc_sock
*svsk
= NULL
;
956 spin_lock_bh(&serv
->sv_lock
);
957 if (!list_empty(&serv
->sv_tempsocks
)) {
958 if (net_ratelimit()) {
959 /* Try to help the admin */
960 printk(KERN_NOTICE
"%s: too many open TCP "
961 "sockets, consider increasing the "
962 "number of nfsd threads\n",
964 printk(KERN_NOTICE
"%s: last TCP connect from "
967 NIPQUAD(sin
.sin_addr
.s_addr
),
968 ntohs(sin
.sin_port
));
971 * Always select the oldest socket. It's not fair,
974 svsk
= list_entry(serv
->sv_tempsocks
.prev
,
977 set_bit(SK_CLOSE
, &svsk
->sk_flags
);
978 atomic_inc(&svsk
->sk_inuse
);
980 spin_unlock_bh(&serv
->sv_lock
);
983 svc_sock_enqueue(svsk
);
990 serv
->sv_stats
->nettcpconn
++;
995 sock_release(newsock
);
1000 * Receive data from a TCP socket.
1003 svc_tcp_recvfrom(struct svc_rqst
*rqstp
)
1005 struct svc_sock
*svsk
= rqstp
->rq_sock
;
1006 struct svc_serv
*serv
= svsk
->sk_server
;
1011 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
1012 svsk
, test_bit(SK_DATA
, &svsk
->sk_flags
),
1013 test_bit(SK_CONN
, &svsk
->sk_flags
),
1014 test_bit(SK_CLOSE
, &svsk
->sk_flags
));
1016 if ((rqstp
->rq_deferred
= svc_deferred_dequeue(svsk
))) {
1017 svc_sock_received(svsk
);
1018 return svc_deferred_recv(rqstp
);
1021 if (test_bit(SK_CLOSE
, &svsk
->sk_flags
)) {
1022 svc_delete_socket(svsk
);
1026 if (svsk
->sk_sk
->sk_state
== TCP_LISTEN
) {
1027 svc_tcp_accept(svsk
);
1028 svc_sock_received(svsk
);
1032 if (test_and_clear_bit(SK_CHNGBUF
, &svsk
->sk_flags
))
1033 /* sndbuf needs to have room for one request
1034 * per thread, otherwise we can stall even when the
1035 * network isn't a bottleneck.
1037 * We count all threads rather than threads in a
1038 * particular pool, which provides an upper bound
1039 * on the number of threads which will access the socket.
1041 * rcvbuf just needs to be able to hold a few requests.
1042 * Normally they will be removed from the queue
1043 * as soon a a complete request arrives.
1045 svc_sock_setbufsize(svsk
->sk_sock
,
1046 (serv
->sv_nrthreads
+3) * serv
->sv_max_mesg
,
1047 3 * serv
->sv_max_mesg
);
1049 clear_bit(SK_DATA
, &svsk
->sk_flags
);
1051 /* Receive data. If we haven't got the record length yet, get
1052 * the next four bytes. Otherwise try to gobble up as much as
1053 * possible up to the complete record length.
1055 if (svsk
->sk_tcplen
< 4) {
1056 unsigned long want
= 4 - svsk
->sk_tcplen
;
1059 iov
.iov_base
= ((char *) &svsk
->sk_reclen
) + svsk
->sk_tcplen
;
1061 if ((len
= svc_recvfrom(rqstp
, &iov
, 1, want
)) < 0)
1063 svsk
->sk_tcplen
+= len
;
1066 dprintk("svc: short recvfrom while reading record length (%d of %lu)\n",
1068 svc_sock_received(svsk
);
1069 return -EAGAIN
; /* record header not complete */
1072 svsk
->sk_reclen
= ntohl(svsk
->sk_reclen
);
1073 if (!(svsk
->sk_reclen
& 0x80000000)) {
1074 /* FIXME: technically, a record can be fragmented,
1075 * and non-terminal fragments will not have the top
1076 * bit set in the fragment length header.
1077 * But apparently no known nfs clients send fragmented
1079 if (net_ratelimit())
1080 printk(KERN_NOTICE
"RPC: bad TCP reclen 0x%08lx"
1081 " (non-terminal)\n",
1082 (unsigned long) svsk
->sk_reclen
);
1085 svsk
->sk_reclen
&= 0x7fffffff;
1086 dprintk("svc: TCP record, %d bytes\n", svsk
->sk_reclen
);
1087 if (svsk
->sk_reclen
> serv
->sv_max_mesg
) {
1088 if (net_ratelimit())
1089 printk(KERN_NOTICE
"RPC: bad TCP reclen 0x%08lx"
1091 (unsigned long) svsk
->sk_reclen
);
1096 /* Check whether enough data is available */
1097 len
= svc_recv_available(svsk
);
1101 if (len
< svsk
->sk_reclen
) {
1102 dprintk("svc: incomplete TCP record (%d of %d)\n",
1103 len
, svsk
->sk_reclen
);
1104 svc_sock_received(svsk
);
1105 return -EAGAIN
; /* record not complete */
1107 len
= svsk
->sk_reclen
;
1108 set_bit(SK_DATA
, &svsk
->sk_flags
);
1110 vec
= rqstp
->rq_vec
;
1111 vec
[0] = rqstp
->rq_arg
.head
[0];
1114 while (vlen
< len
) {
1115 vec
[pnum
].iov_base
= page_address(rqstp
->rq_pages
[pnum
]);
1116 vec
[pnum
].iov_len
= PAGE_SIZE
;
1120 rqstp
->rq_respages
= &rqstp
->rq_pages
[pnum
];
1122 /* Now receive data */
1123 len
= svc_recvfrom(rqstp
, vec
, pnum
, len
);
1127 dprintk("svc: TCP complete record (%d bytes)\n", len
);
1128 rqstp
->rq_arg
.len
= len
;
1129 rqstp
->rq_arg
.page_base
= 0;
1130 if (len
<= rqstp
->rq_arg
.head
[0].iov_len
) {
1131 rqstp
->rq_arg
.head
[0].iov_len
= len
;
1132 rqstp
->rq_arg
.page_len
= 0;
1134 rqstp
->rq_arg
.page_len
= len
- rqstp
->rq_arg
.head
[0].iov_len
;
1137 rqstp
->rq_skbuff
= NULL
;
1138 rqstp
->rq_prot
= IPPROTO_TCP
;
1140 /* Reset TCP read info */
1141 svsk
->sk_reclen
= 0;
1142 svsk
->sk_tcplen
= 0;
1144 svc_sock_received(svsk
);
1146 serv
->sv_stats
->nettcpcnt
++;
1151 svc_delete_socket(svsk
);
1155 if (len
== -EAGAIN
) {
1156 dprintk("RPC: TCP recvfrom got EAGAIN\n");
1157 svc_sock_received(svsk
);
1159 printk(KERN_NOTICE
"%s: recvfrom returned errno %d\n",
1160 svsk
->sk_server
->sv_name
, -len
);
1168 * Send out data on TCP socket.
1171 svc_tcp_sendto(struct svc_rqst
*rqstp
)
1173 struct xdr_buf
*xbufp
= &rqstp
->rq_res
;
1177 /* Set up the first element of the reply kvec.
1178 * Any other kvecs that may be in use have been taken
1179 * care of by the server implementation itself.
1181 reclen
= htonl(0x80000000|((xbufp
->len
) - 4));
1182 memcpy(xbufp
->head
[0].iov_base
, &reclen
, 4);
1184 if (test_bit(SK_DEAD
, &rqstp
->rq_sock
->sk_flags
))
1187 sent
= svc_sendto(rqstp
, &rqstp
->rq_res
);
1188 if (sent
!= xbufp
->len
) {
1189 printk(KERN_NOTICE
"rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n",
1190 rqstp
->rq_sock
->sk_server
->sv_name
,
1191 (sent
<0)?"got error":"sent only",
1193 set_bit(SK_CLOSE
, &rqstp
->rq_sock
->sk_flags
);
1194 svc_sock_enqueue(rqstp
->rq_sock
);
1201 svc_tcp_init(struct svc_sock
*svsk
)
1203 struct sock
*sk
= svsk
->sk_sk
;
1204 struct tcp_sock
*tp
= tcp_sk(sk
);
1206 svsk
->sk_recvfrom
= svc_tcp_recvfrom
;
1207 svsk
->sk_sendto
= svc_tcp_sendto
;
1209 if (sk
->sk_state
== TCP_LISTEN
) {
1210 dprintk("setting up TCP socket for listening\n");
1211 sk
->sk_data_ready
= svc_tcp_listen_data_ready
;
1212 set_bit(SK_CONN
, &svsk
->sk_flags
);
1214 dprintk("setting up TCP socket for reading\n");
1215 sk
->sk_state_change
= svc_tcp_state_change
;
1216 sk
->sk_data_ready
= svc_tcp_data_ready
;
1217 sk
->sk_write_space
= svc_write_space
;
1219 svsk
->sk_reclen
= 0;
1220 svsk
->sk_tcplen
= 0;
1222 tp
->nonagle
= 1; /* disable Nagle's algorithm */
1224 /* initialise setting must have enough space to
1225 * receive and respond to one request.
1226 * svc_tcp_recvfrom will re-adjust if necessary
1228 svc_sock_setbufsize(svsk
->sk_sock
,
1229 3 * svsk
->sk_server
->sv_max_mesg
,
1230 3 * svsk
->sk_server
->sv_max_mesg
);
1232 set_bit(SK_CHNGBUF
, &svsk
->sk_flags
);
1233 set_bit(SK_DATA
, &svsk
->sk_flags
);
1234 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1235 set_bit(SK_CLOSE
, &svsk
->sk_flags
);
1240 svc_sock_update_bufs(struct svc_serv
*serv
)
1243 * The number of server threads has changed. Update
1244 * rcvbuf and sndbuf accordingly on all sockets
1246 struct list_head
*le
;
1248 spin_lock_bh(&serv
->sv_lock
);
1249 list_for_each(le
, &serv
->sv_permsocks
) {
1250 struct svc_sock
*svsk
=
1251 list_entry(le
, struct svc_sock
, sk_list
);
1252 set_bit(SK_CHNGBUF
, &svsk
->sk_flags
);
1254 list_for_each(le
, &serv
->sv_tempsocks
) {
1255 struct svc_sock
*svsk
=
1256 list_entry(le
, struct svc_sock
, sk_list
);
1257 set_bit(SK_CHNGBUF
, &svsk
->sk_flags
);
1259 spin_unlock_bh(&serv
->sv_lock
);
1263 * Receive the next request on any socket. This code is carefully
1264 * organised not to touch any cachelines in the shared svc_serv
1265 * structure, only cachelines in the local svc_pool.
1268 svc_recv(struct svc_rqst
*rqstp
, long timeout
)
1270 struct svc_sock
*svsk
=NULL
;
1271 struct svc_serv
*serv
= rqstp
->rq_server
;
1272 struct svc_pool
*pool
= rqstp
->rq_pool
;
1275 struct xdr_buf
*arg
;
1276 DECLARE_WAITQUEUE(wait
, current
);
1278 dprintk("svc: server %p waiting for data (to = %ld)\n",
1283 "svc_recv: service %p, socket not NULL!\n",
1285 if (waitqueue_active(&rqstp
->rq_wait
))
1287 "svc_recv: service %p, wait queue active!\n",
1291 /* now allocate needed pages. If we get a failure, sleep briefly */
1292 pages
= (serv
->sv_max_mesg
+ PAGE_SIZE
) / PAGE_SIZE
;
1293 for (i
=0; i
< pages
; i
++)
1294 while (rqstp
->rq_pages
[i
] == NULL
) {
1295 struct page
*p
= alloc_page(GFP_KERNEL
);
1297 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
1298 rqstp
->rq_pages
[i
] = p
;
1300 rqstp
->rq_pages
[i
++] = NULL
; /* this might be seen in nfs_read_actor */
1301 BUG_ON(pages
>= RPCSVC_MAXPAGES
);
1303 /* Make arg->head point to first page and arg->pages point to rest */
1304 arg
= &rqstp
->rq_arg
;
1305 arg
->head
[0].iov_base
= page_address(rqstp
->rq_pages
[0]);
1306 arg
->head
[0].iov_len
= PAGE_SIZE
;
1307 arg
->pages
= rqstp
->rq_pages
+ 1;
1309 /* save at least one page for response */
1310 arg
->page_len
= (pages
-2)*PAGE_SIZE
;
1311 arg
->len
= (pages
-1)*PAGE_SIZE
;
1312 arg
->tail
[0].iov_len
= 0;
1319 spin_lock_bh(&pool
->sp_lock
);
1320 if ((svsk
= svc_sock_dequeue(pool
)) != NULL
) {
1321 rqstp
->rq_sock
= svsk
;
1322 atomic_inc(&svsk
->sk_inuse
);
1323 rqstp
->rq_reserved
= serv
->sv_max_mesg
;
1324 atomic_add(rqstp
->rq_reserved
, &svsk
->sk_reserved
);
1326 /* No data pending. Go to sleep */
1327 svc_thread_enqueue(pool
, rqstp
);
1330 * We have to be able to interrupt this wait
1331 * to bring down the daemons ...
1333 set_current_state(TASK_INTERRUPTIBLE
);
1334 add_wait_queue(&rqstp
->rq_wait
, &wait
);
1335 spin_unlock_bh(&pool
->sp_lock
);
1337 schedule_timeout(timeout
);
1341 spin_lock_bh(&pool
->sp_lock
);
1342 remove_wait_queue(&rqstp
->rq_wait
, &wait
);
1344 if (!(svsk
= rqstp
->rq_sock
)) {
1345 svc_thread_dequeue(pool
, rqstp
);
1346 spin_unlock_bh(&pool
->sp_lock
);
1347 dprintk("svc: server %p, no data yet\n", rqstp
);
1348 return signalled()? -EINTR
: -EAGAIN
;
1351 spin_unlock_bh(&pool
->sp_lock
);
1353 dprintk("svc: server %p, pool %u, socket %p, inuse=%d\n",
1354 rqstp
, pool
->sp_id
, svsk
, atomic_read(&svsk
->sk_inuse
));
1355 len
= svsk
->sk_recvfrom(rqstp
);
1356 dprintk("svc: got len=%d\n", len
);
1358 /* No data, incomplete (TCP) read, or accept() */
1359 if (len
== 0 || len
== -EAGAIN
) {
1360 rqstp
->rq_res
.len
= 0;
1361 svc_sock_release(rqstp
);
1364 svsk
->sk_lastrecv
= get_seconds();
1365 clear_bit(SK_OLD
, &svsk
->sk_flags
);
1367 rqstp
->rq_secure
= ntohs(rqstp
->rq_addr
.sin_port
) < 1024;
1368 rqstp
->rq_chandle
.defer
= svc_defer
;
1371 serv
->sv_stats
->netcnt
++;
1379 svc_drop(struct svc_rqst
*rqstp
)
1381 dprintk("svc: socket %p dropped request\n", rqstp
->rq_sock
);
1382 svc_sock_release(rqstp
);
1386 * Return reply to client.
1389 svc_send(struct svc_rqst
*rqstp
)
1391 struct svc_sock
*svsk
;
1395 if ((svsk
= rqstp
->rq_sock
) == NULL
) {
1396 printk(KERN_WARNING
"NULL socket pointer in %s:%d\n",
1397 __FILE__
, __LINE__
);
1401 /* release the receive skb before sending the reply */
1402 svc_release_skb(rqstp
);
1404 /* calculate over-all length */
1405 xb
= & rqstp
->rq_res
;
1406 xb
->len
= xb
->head
[0].iov_len
+
1408 xb
->tail
[0].iov_len
;
1410 /* Grab svsk->sk_mutex to serialize outgoing data. */
1411 mutex_lock(&svsk
->sk_mutex
);
1412 if (test_bit(SK_DEAD
, &svsk
->sk_flags
))
1415 len
= svsk
->sk_sendto(rqstp
);
1416 mutex_unlock(&svsk
->sk_mutex
);
1417 svc_sock_release(rqstp
);
1419 if (len
== -ECONNREFUSED
|| len
== -ENOTCONN
|| len
== -EAGAIN
)
1425 * Timer function to close old temporary sockets, using
1426 * a mark-and-sweep algorithm.
1429 svc_age_temp_sockets(unsigned long closure
)
1431 struct svc_serv
*serv
= (struct svc_serv
*)closure
;
1432 struct svc_sock
*svsk
;
1433 struct list_head
*le
, *next
;
1434 LIST_HEAD(to_be_aged
);
1436 dprintk("svc_age_temp_sockets\n");
1438 if (!spin_trylock_bh(&serv
->sv_lock
)) {
1439 /* busy, try again 1 sec later */
1440 dprintk("svc_age_temp_sockets: busy\n");
1441 mod_timer(&serv
->sv_temptimer
, jiffies
+ HZ
);
1445 list_for_each_safe(le
, next
, &serv
->sv_tempsocks
) {
1446 svsk
= list_entry(le
, struct svc_sock
, sk_list
);
1448 if (!test_and_set_bit(SK_OLD
, &svsk
->sk_flags
))
1450 if (atomic_read(&svsk
->sk_inuse
) || test_bit(SK_BUSY
, &svsk
->sk_flags
))
1452 atomic_inc(&svsk
->sk_inuse
);
1453 list_move(le
, &to_be_aged
);
1454 set_bit(SK_CLOSE
, &svsk
->sk_flags
);
1455 set_bit(SK_DETACHED
, &svsk
->sk_flags
);
1457 spin_unlock_bh(&serv
->sv_lock
);
1459 while (!list_empty(&to_be_aged
)) {
1460 le
= to_be_aged
.next
;
1461 /* fiddling the sk_list node is safe 'cos we're SK_DETACHED */
1463 svsk
= list_entry(le
, struct svc_sock
, sk_list
);
1465 dprintk("queuing svsk %p for closing, %lu seconds old\n",
1466 svsk
, get_seconds() - svsk
->sk_lastrecv
);
1468 /* a thread will dequeue and close it soon */
1469 svc_sock_enqueue(svsk
);
1473 mod_timer(&serv
->sv_temptimer
, jiffies
+ svc_conn_age_period
* HZ
);
1477 * Initialize socket for RPC use and create svc_sock struct
1478 * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
1480 static struct svc_sock
*
1481 svc_setup_socket(struct svc_serv
*serv
, struct socket
*sock
,
1482 int *errp
, int pmap_register
)
1484 struct svc_sock
*svsk
;
1487 dprintk("svc: svc_setup_socket %p\n", sock
);
1488 if (!(svsk
= kzalloc(sizeof(*svsk
), GFP_KERNEL
))) {
1495 /* Register socket with portmapper */
1496 if (*errp
>= 0 && pmap_register
)
1497 *errp
= svc_register(serv
, inet
->sk_protocol
,
1498 ntohs(inet_sk(inet
)->sport
));
1505 set_bit(SK_BUSY
, &svsk
->sk_flags
);
1506 inet
->sk_user_data
= svsk
;
1507 svsk
->sk_sock
= sock
;
1509 svsk
->sk_ostate
= inet
->sk_state_change
;
1510 svsk
->sk_odata
= inet
->sk_data_ready
;
1511 svsk
->sk_owspace
= inet
->sk_write_space
;
1512 svsk
->sk_server
= serv
;
1513 atomic_set(&svsk
->sk_inuse
, 1);
1514 svsk
->sk_lastrecv
= get_seconds();
1515 spin_lock_init(&svsk
->sk_defer_lock
);
1516 INIT_LIST_HEAD(&svsk
->sk_deferred
);
1517 INIT_LIST_HEAD(&svsk
->sk_ready
);
1518 mutex_init(&svsk
->sk_mutex
);
1520 /* Initialize the socket */
1521 if (sock
->type
== SOCK_DGRAM
)
1526 spin_lock_bh(&serv
->sv_lock
);
1527 if (!pmap_register
) {
1528 set_bit(SK_TEMP
, &svsk
->sk_flags
);
1529 list_add(&svsk
->sk_list
, &serv
->sv_tempsocks
);
1531 if (serv
->sv_temptimer
.function
== NULL
) {
1532 /* setup timer to age temp sockets */
1533 setup_timer(&serv
->sv_temptimer
, svc_age_temp_sockets
,
1534 (unsigned long)serv
);
1535 mod_timer(&serv
->sv_temptimer
,
1536 jiffies
+ svc_conn_age_period
* HZ
);
1539 clear_bit(SK_TEMP
, &svsk
->sk_flags
);
1540 list_add(&svsk
->sk_list
, &serv
->sv_permsocks
);
1542 spin_unlock_bh(&serv
->sv_lock
);
1544 dprintk("svc: svc_setup_socket created %p (inet %p)\n",
1547 clear_bit(SK_BUSY
, &svsk
->sk_flags
);
1548 svc_sock_enqueue(svsk
);
1552 int svc_addsock(struct svc_serv
*serv
,
1558 struct socket
*so
= sockfd_lookup(fd
, &err
);
1559 struct svc_sock
*svsk
= NULL
;
1563 if (so
->sk
->sk_family
!= AF_INET
)
1564 err
= -EAFNOSUPPORT
;
1565 else if (so
->sk
->sk_protocol
!= IPPROTO_TCP
&&
1566 so
->sk
->sk_protocol
!= IPPROTO_UDP
)
1567 err
= -EPROTONOSUPPORT
;
1568 else if (so
->state
> SS_UNCONNECTED
)
1571 svsk
= svc_setup_socket(serv
, so
, &err
, 1);
1579 if (proto
) *proto
= so
->sk
->sk_protocol
;
1580 return one_sock_name(name_return
, svsk
);
1582 EXPORT_SYMBOL_GPL(svc_addsock
);
1585 * Create socket for RPC service.
1588 svc_create_socket(struct svc_serv
*serv
, int protocol
, struct sockaddr_in
*sin
)
1590 struct svc_sock
*svsk
;
1591 struct socket
*sock
;
1595 dprintk("svc: svc_create_socket(%s, %d, %u.%u.%u.%u:%d)\n",
1596 serv
->sv_program
->pg_name
, protocol
,
1597 NIPQUAD(sin
->sin_addr
.s_addr
),
1598 ntohs(sin
->sin_port
));
1600 if (protocol
!= IPPROTO_UDP
&& protocol
!= IPPROTO_TCP
) {
1601 printk(KERN_WARNING
"svc: only UDP and TCP "
1602 "sockets supported\n");
1605 type
= (protocol
== IPPROTO_UDP
)? SOCK_DGRAM
: SOCK_STREAM
;
1607 if ((error
= sock_create_kern(PF_INET
, type
, protocol
, &sock
)) < 0)
1610 svc_reclassify_socket(sock
);
1612 if (type
== SOCK_STREAM
)
1613 sock
->sk
->sk_reuse
= 1; /* allow address reuse */
1614 error
= kernel_bind(sock
, (struct sockaddr
*) sin
,
1619 if (protocol
== IPPROTO_TCP
) {
1620 if ((error
= kernel_listen(sock
, 64)) < 0)
1624 if ((svsk
= svc_setup_socket(serv
, sock
, &error
, 1)) != NULL
)
1628 dprintk("svc: svc_create_socket error = %d\n", -error
);
1634 * Remove a dead socket
1637 svc_delete_socket(struct svc_sock
*svsk
)
1639 struct svc_serv
*serv
;
1642 dprintk("svc: svc_delete_socket(%p)\n", svsk
);
1644 serv
= svsk
->sk_server
;
1647 sk
->sk_state_change
= svsk
->sk_ostate
;
1648 sk
->sk_data_ready
= svsk
->sk_odata
;
1649 sk
->sk_write_space
= svsk
->sk_owspace
;
1651 spin_lock_bh(&serv
->sv_lock
);
1653 if (!test_and_set_bit(SK_DETACHED
, &svsk
->sk_flags
))
1654 list_del_init(&svsk
->sk_list
);
1656 * We used to delete the svc_sock from whichever list
1657 * it's sk_ready node was on, but we don't actually
1658 * need to. This is because the only time we're called
1659 * while still attached to a queue, the queue itself
1660 * is about to be destroyed (in svc_destroy).
1662 if (!test_and_set_bit(SK_DEAD
, &svsk
->sk_flags
)) {
1663 BUG_ON(atomic_read(&svsk
->sk_inuse
)<2);
1664 atomic_dec(&svsk
->sk_inuse
);
1665 if (test_bit(SK_TEMP
, &svsk
->sk_flags
))
1669 spin_unlock_bh(&serv
->sv_lock
);
1672 static void svc_close_socket(struct svc_sock
*svsk
)
1674 set_bit(SK_CLOSE
, &svsk
->sk_flags
);
1675 if (test_and_set_bit(SK_BUSY
, &svsk
->sk_flags
))
1676 /* someone else will have to effect the close */
1679 atomic_inc(&svsk
->sk_inuse
);
1680 svc_delete_socket(svsk
);
1681 clear_bit(SK_BUSY
, &svsk
->sk_flags
);
1685 void svc_force_close_socket(struct svc_sock
*svsk
)
1687 set_bit(SK_CLOSE
, &svsk
->sk_flags
);
1688 if (test_bit(SK_BUSY
, &svsk
->sk_flags
)) {
1689 /* Waiting to be processed, but no threads left,
1690 * So just remove it from the waiting list
1692 list_del_init(&svsk
->sk_ready
);
1693 clear_bit(SK_BUSY
, &svsk
->sk_flags
);
1695 svc_close_socket(svsk
);
1699 * Make a socket for nfsd and lockd
1702 svc_makesock(struct svc_serv
*serv
, int protocol
, unsigned short port
)
1704 struct sockaddr_in sin
;
1706 dprintk("svc: creating socket proto = %d\n", protocol
);
1707 sin
.sin_family
= AF_INET
;
1708 sin
.sin_addr
.s_addr
= INADDR_ANY
;
1709 sin
.sin_port
= htons(port
);
1710 return svc_create_socket(serv
, protocol
, &sin
);
1714 * Handle defer and revisit of requests
1717 static void svc_revisit(struct cache_deferred_req
*dreq
, int too_many
)
1719 struct svc_deferred_req
*dr
= container_of(dreq
, struct svc_deferred_req
, handle
);
1720 struct svc_sock
*svsk
;
1723 svc_sock_put(dr
->svsk
);
1727 dprintk("revisit queued\n");
1730 spin_lock_bh(&svsk
->sk_defer_lock
);
1731 list_add(&dr
->handle
.recent
, &svsk
->sk_deferred
);
1732 spin_unlock_bh(&svsk
->sk_defer_lock
);
1733 set_bit(SK_DEFERRED
, &svsk
->sk_flags
);
1734 svc_sock_enqueue(svsk
);
1738 static struct cache_deferred_req
*
1739 svc_defer(struct cache_req
*req
)
1741 struct svc_rqst
*rqstp
= container_of(req
, struct svc_rqst
, rq_chandle
);
1742 int size
= sizeof(struct svc_deferred_req
) + (rqstp
->rq_arg
.len
);
1743 struct svc_deferred_req
*dr
;
1745 if (rqstp
->rq_arg
.page_len
)
1746 return NULL
; /* if more than a page, give up FIXME */
1747 if (rqstp
->rq_deferred
) {
1748 dr
= rqstp
->rq_deferred
;
1749 rqstp
->rq_deferred
= NULL
;
1751 int skip
= rqstp
->rq_arg
.len
- rqstp
->rq_arg
.head
[0].iov_len
;
1752 /* FIXME maybe discard if size too large */
1753 dr
= kmalloc(size
, GFP_KERNEL
);
1757 dr
->handle
.owner
= rqstp
->rq_server
;
1758 dr
->prot
= rqstp
->rq_prot
;
1759 dr
->addr
= rqstp
->rq_addr
;
1760 dr
->daddr
= rqstp
->rq_daddr
;
1761 dr
->argslen
= rqstp
->rq_arg
.len
>> 2;
1762 memcpy(dr
->args
, rqstp
->rq_arg
.head
[0].iov_base
-skip
, dr
->argslen
<<2);
1764 atomic_inc(&rqstp
->rq_sock
->sk_inuse
);
1765 dr
->svsk
= rqstp
->rq_sock
;
1767 dr
->handle
.revisit
= svc_revisit
;
1772 * recv data from a deferred request into an active one
1774 static int svc_deferred_recv(struct svc_rqst
*rqstp
)
1776 struct svc_deferred_req
*dr
= rqstp
->rq_deferred
;
1778 rqstp
->rq_arg
.head
[0].iov_base
= dr
->args
;
1779 rqstp
->rq_arg
.head
[0].iov_len
= dr
->argslen
<<2;
1780 rqstp
->rq_arg
.page_len
= 0;
1781 rqstp
->rq_arg
.len
= dr
->argslen
<<2;
1782 rqstp
->rq_prot
= dr
->prot
;
1783 rqstp
->rq_addr
= dr
->addr
;
1784 rqstp
->rq_daddr
= dr
->daddr
;
1785 rqstp
->rq_respages
= rqstp
->rq_pages
;
1786 return dr
->argslen
<<2;
1790 static struct svc_deferred_req
*svc_deferred_dequeue(struct svc_sock
*svsk
)
1792 struct svc_deferred_req
*dr
= NULL
;
1794 if (!test_bit(SK_DEFERRED
, &svsk
->sk_flags
))
1796 spin_lock_bh(&svsk
->sk_defer_lock
);
1797 clear_bit(SK_DEFERRED
, &svsk
->sk_flags
);
1798 if (!list_empty(&svsk
->sk_deferred
)) {
1799 dr
= list_entry(svsk
->sk_deferred
.next
,
1800 struct svc_deferred_req
,
1802 list_del_init(&dr
->handle
.recent
);
1803 set_bit(SK_DEFERRED
, &svsk
->sk_flags
);
1805 spin_unlock_bh(&svsk
->sk_defer_lock
);