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>
36 #include <net/checksum.h>
38 #include <net/tcp_states.h>
39 #include <asm/uaccess.h>
40 #include <asm/ioctls.h>
42 #include <linux/sunrpc/types.h>
43 #include <linux/sunrpc/xdr.h>
44 #include <linux/sunrpc/svcsock.h>
45 #include <linux/sunrpc/stats.h>
47 /* SMP locking strategy:
49 * svc_pool->sp_lock protects most of the fields of that pool.
50 * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt.
51 * when both need to be taken (rare), svc_serv->sv_lock is first.
52 * BKL protects svc_serv->sv_nrthread.
53 * svc_sock->sk_defer_lock protects the svc_sock->sk_deferred list
54 * svc_sock->sk_flags.SK_BUSY prevents a svc_sock being enqueued multiply.
56 * Some flags can be set to certain values at any time
57 * providing that certain rules are followed:
59 * SK_CONN, SK_DATA, can be set or cleared at any time.
60 * after a set, svc_sock_enqueue must be called.
61 * after a clear, the socket must be read/accepted
62 * if this succeeds, it must be set again.
63 * SK_CLOSE can set at any time. It is never cleared.
67 #define RPCDBG_FACILITY RPCDBG_SVCSOCK
70 static struct svc_sock
*svc_setup_socket(struct svc_serv
*, struct socket
*,
71 int *errp
, int pmap_reg
);
72 static void svc_udp_data_ready(struct sock
*, int);
73 static int svc_udp_recvfrom(struct svc_rqst
*);
74 static int svc_udp_sendto(struct svc_rqst
*);
76 static struct svc_deferred_req
*svc_deferred_dequeue(struct svc_sock
*svsk
);
77 static int svc_deferred_recv(struct svc_rqst
*rqstp
);
78 static struct cache_deferred_req
*svc_defer(struct cache_req
*req
);
80 /* apparently the "standard" is that clients close
81 * idle connections after 5 minutes, servers after
83 * http://www.connectathon.org/talks96/nfstcp.pdf
85 static int svc_conn_age_period
= 6*60;
88 * Queue up an idle server thread. Must have pool->sp_lock held.
89 * Note: this is really a stack rather than a queue, so that we only
90 * use as many different threads as we need, and the rest don't pollute
94 svc_thread_enqueue(struct svc_pool
*pool
, struct svc_rqst
*rqstp
)
96 list_add(&rqstp
->rq_list
, &pool
->sp_threads
);
100 * Dequeue an nfsd thread. Must have pool->sp_lock held.
103 svc_thread_dequeue(struct svc_pool
*pool
, struct svc_rqst
*rqstp
)
105 list_del(&rqstp
->rq_list
);
109 * Release an skbuff after use
112 svc_release_skb(struct svc_rqst
*rqstp
)
114 struct sk_buff
*skb
= rqstp
->rq_skbuff
;
115 struct svc_deferred_req
*dr
= rqstp
->rq_deferred
;
118 rqstp
->rq_skbuff
= NULL
;
120 dprintk("svc: service %p, releasing skb %p\n", rqstp
, skb
);
121 skb_free_datagram(rqstp
->rq_sock
->sk_sk
, skb
);
124 rqstp
->rq_deferred
= NULL
;
130 * Any space to write?
132 static inline unsigned long
133 svc_sock_wspace(struct svc_sock
*svsk
)
137 if (svsk
->sk_sock
->type
== SOCK_STREAM
)
138 wspace
= sk_stream_wspace(svsk
->sk_sk
);
140 wspace
= sock_wspace(svsk
->sk_sk
);
146 * Queue up a socket with data pending. If there are idle nfsd
147 * processes, wake 'em up.
151 svc_sock_enqueue(struct svc_sock
*svsk
)
153 struct svc_serv
*serv
= svsk
->sk_server
;
154 struct svc_pool
*pool
;
155 struct svc_rqst
*rqstp
;
158 if (!(svsk
->sk_flags
&
159 ( (1<<SK_CONN
)|(1<<SK_DATA
)|(1<<SK_CLOSE
)|(1<<SK_DEFERRED
)) ))
161 if (test_bit(SK_DEAD
, &svsk
->sk_flags
))
165 pool
= svc_pool_for_cpu(svsk
->sk_server
, cpu
);
168 spin_lock_bh(&pool
->sp_lock
);
170 if (!list_empty(&pool
->sp_threads
) &&
171 !list_empty(&pool
->sp_sockets
))
173 "svc_sock_enqueue: threads and sockets both waiting??\n");
175 if (test_bit(SK_DEAD
, &svsk
->sk_flags
)) {
176 /* Don't enqueue dead sockets */
177 dprintk("svc: socket %p is dead, not enqueued\n", svsk
->sk_sk
);
181 /* Mark socket as busy. It will remain in this state until the
182 * server has processed all pending data and put the socket back
183 * on the idle list. We update SK_BUSY atomically because
184 * it also guards against trying to enqueue the svc_sock twice.
186 if (test_and_set_bit(SK_BUSY
, &svsk
->sk_flags
)) {
187 /* Don't enqueue socket while already enqueued */
188 dprintk("svc: socket %p busy, not enqueued\n", svsk
->sk_sk
);
191 BUG_ON(svsk
->sk_pool
!= NULL
);
192 svsk
->sk_pool
= pool
;
194 set_bit(SOCK_NOSPACE
, &svsk
->sk_sock
->flags
);
195 if (((atomic_read(&svsk
->sk_reserved
) + serv
->sv_bufsz
)*2
196 > svc_sock_wspace(svsk
))
197 && !test_bit(SK_CLOSE
, &svsk
->sk_flags
)
198 && !test_bit(SK_CONN
, &svsk
->sk_flags
)) {
199 /* Don't enqueue while not enough space for reply */
200 dprintk("svc: socket %p no space, %d*2 > %ld, not enqueued\n",
201 svsk
->sk_sk
, atomic_read(&svsk
->sk_reserved
)+serv
->sv_bufsz
,
202 svc_sock_wspace(svsk
));
203 svsk
->sk_pool
= NULL
;
204 clear_bit(SK_BUSY
, &svsk
->sk_flags
);
207 clear_bit(SOCK_NOSPACE
, &svsk
->sk_sock
->flags
);
210 if (!list_empty(&pool
->sp_threads
)) {
211 rqstp
= list_entry(pool
->sp_threads
.next
,
214 dprintk("svc: socket %p served by daemon %p\n",
216 svc_thread_dequeue(pool
, rqstp
);
219 "svc_sock_enqueue: server %p, rq_sock=%p!\n",
220 rqstp
, rqstp
->rq_sock
);
221 rqstp
->rq_sock
= svsk
;
222 atomic_inc(&svsk
->sk_inuse
);
223 rqstp
->rq_reserved
= serv
->sv_bufsz
;
224 atomic_add(rqstp
->rq_reserved
, &svsk
->sk_reserved
);
225 BUG_ON(svsk
->sk_pool
!= pool
);
226 wake_up(&rqstp
->rq_wait
);
228 dprintk("svc: socket %p put into queue\n", svsk
->sk_sk
);
229 list_add_tail(&svsk
->sk_ready
, &pool
->sp_sockets
);
230 BUG_ON(svsk
->sk_pool
!= pool
);
234 spin_unlock_bh(&pool
->sp_lock
);
238 * Dequeue the first socket. Must be called with the pool->sp_lock held.
240 static inline struct svc_sock
*
241 svc_sock_dequeue(struct svc_pool
*pool
)
243 struct svc_sock
*svsk
;
245 if (list_empty(&pool
->sp_sockets
))
248 svsk
= list_entry(pool
->sp_sockets
.next
,
249 struct svc_sock
, sk_ready
);
250 list_del_init(&svsk
->sk_ready
);
252 dprintk("svc: socket %p dequeued, inuse=%d\n",
253 svsk
->sk_sk
, atomic_read(&svsk
->sk_inuse
));
259 * Having read something from a socket, check whether it
260 * needs to be re-enqueued.
261 * Note: SK_DATA only gets cleared when a read-attempt finds
262 * no (or insufficient) data.
265 svc_sock_received(struct svc_sock
*svsk
)
267 svsk
->sk_pool
= NULL
;
268 clear_bit(SK_BUSY
, &svsk
->sk_flags
);
269 svc_sock_enqueue(svsk
);
274 * svc_reserve - change the space reserved for the reply to a request.
275 * @rqstp: The request in question
276 * @space: new max space to reserve
278 * Each request reserves some space on the output queue of the socket
279 * to make sure the reply fits. This function reduces that reserved
280 * space to be the amount of space used already, plus @space.
283 void svc_reserve(struct svc_rqst
*rqstp
, int space
)
285 space
+= rqstp
->rq_res
.head
[0].iov_len
;
287 if (space
< rqstp
->rq_reserved
) {
288 struct svc_sock
*svsk
= rqstp
->rq_sock
;
289 atomic_sub((rqstp
->rq_reserved
- space
), &svsk
->sk_reserved
);
290 rqstp
->rq_reserved
= space
;
292 svc_sock_enqueue(svsk
);
297 * Release a socket after use.
300 svc_sock_put(struct svc_sock
*svsk
)
302 if (atomic_dec_and_test(&svsk
->sk_inuse
) && test_bit(SK_DEAD
, &svsk
->sk_flags
)) {
303 dprintk("svc: releasing dead socket\n");
304 sock_release(svsk
->sk_sock
);
310 svc_sock_release(struct svc_rqst
*rqstp
)
312 struct svc_sock
*svsk
= rqstp
->rq_sock
;
314 svc_release_skb(rqstp
);
316 svc_free_allpages(rqstp
);
317 rqstp
->rq_res
.page_len
= 0;
318 rqstp
->rq_res
.page_base
= 0;
321 /* Reset response buffer and release
323 * But first, check that enough space was reserved
324 * for the reply, otherwise we have a bug!
326 if ((rqstp
->rq_res
.len
) > rqstp
->rq_reserved
)
327 printk(KERN_ERR
"RPC request reserved %d but used %d\n",
331 rqstp
->rq_res
.head
[0].iov_len
= 0;
332 svc_reserve(rqstp
, 0);
333 rqstp
->rq_sock
= NULL
;
339 * External function to wake up a server waiting for data
340 * This really only makes sense for services like lockd
341 * which have exactly one thread anyway.
344 svc_wake_up(struct svc_serv
*serv
)
346 struct svc_rqst
*rqstp
;
348 struct svc_pool
*pool
;
350 for (i
= 0; i
< serv
->sv_nrpools
; i
++) {
351 pool
= &serv
->sv_pools
[i
];
353 spin_lock_bh(&pool
->sp_lock
);
354 if (!list_empty(&pool
->sp_threads
)) {
355 rqstp
= list_entry(pool
->sp_threads
.next
,
358 dprintk("svc: daemon %p woken up.\n", rqstp
);
360 svc_thread_dequeue(pool, rqstp);
361 rqstp->rq_sock = NULL;
363 wake_up(&rqstp
->rq_wait
);
365 spin_unlock_bh(&pool
->sp_lock
);
370 * Generic sendto routine
373 svc_sendto(struct svc_rqst
*rqstp
, struct xdr_buf
*xdr
)
375 struct svc_sock
*svsk
= rqstp
->rq_sock
;
376 struct socket
*sock
= svsk
->sk_sock
;
378 char buffer
[CMSG_SPACE(sizeof(struct in_pktinfo
))];
379 struct cmsghdr
*cmh
= (struct cmsghdr
*)buffer
;
380 struct in_pktinfo
*pki
= (struct in_pktinfo
*)CMSG_DATA(cmh
);
384 struct page
**ppage
= xdr
->pages
;
385 size_t base
= xdr
->page_base
;
386 unsigned int pglen
= xdr
->page_len
;
387 unsigned int flags
= MSG_MORE
;
391 if (rqstp
->rq_prot
== IPPROTO_UDP
) {
392 /* set the source and destination */
394 msg
.msg_name
= &rqstp
->rq_addr
;
395 msg
.msg_namelen
= sizeof(rqstp
->rq_addr
);
398 msg
.msg_flags
= MSG_MORE
;
400 msg
.msg_control
= cmh
;
401 msg
.msg_controllen
= sizeof(buffer
);
402 cmh
->cmsg_len
= CMSG_LEN(sizeof(*pki
));
403 cmh
->cmsg_level
= SOL_IP
;
404 cmh
->cmsg_type
= IP_PKTINFO
;
405 pki
->ipi_ifindex
= 0;
406 pki
->ipi_spec_dst
.s_addr
= rqstp
->rq_daddr
;
408 if (sock_sendmsg(sock
, &msg
, 0) < 0)
413 if (slen
== xdr
->head
[0].iov_len
)
415 len
= kernel_sendpage(sock
, rqstp
->rq_respages
[0], 0, xdr
->head
[0].iov_len
, flags
);
416 if (len
!= xdr
->head
[0].iov_len
)
418 slen
-= xdr
->head
[0].iov_len
;
423 size
= PAGE_SIZE
- base
< pglen
? PAGE_SIZE
- base
: pglen
;
427 result
= kernel_sendpage(sock
, *ppage
, base
, size
, flags
);
434 size
= PAGE_SIZE
< pglen
? PAGE_SIZE
: pglen
;
439 if (xdr
->tail
[0].iov_len
) {
440 result
= kernel_sendpage(sock
, rqstp
->rq_respages
[rqstp
->rq_restailpage
],
441 ((unsigned long)xdr
->tail
[0].iov_base
)& (PAGE_SIZE
-1),
442 xdr
->tail
[0].iov_len
, 0);
448 dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %x)\n",
449 rqstp
->rq_sock
, xdr
->head
[0].iov_base
, xdr
->head
[0].iov_len
, xdr
->len
, len
,
450 rqstp
->rq_addr
.sin_addr
.s_addr
);
456 * Report socket names for nfsdfs
458 static int one_sock_name(char *buf
, struct svc_sock
*svsk
)
462 switch(svsk
->sk_sk
->sk_family
) {
464 len
= sprintf(buf
, "ipv4 %s %u.%u.%u.%u %d\n",
465 svsk
->sk_sk
->sk_protocol
==IPPROTO_UDP
?
467 NIPQUAD(inet_sk(svsk
->sk_sk
)->rcv_saddr
),
468 inet_sk(svsk
->sk_sk
)->num
);
471 len
= sprintf(buf
, "*unknown-%d*\n",
472 svsk
->sk_sk
->sk_family
);
478 svc_sock_names(char *buf
, struct svc_serv
*serv
, char *toclose
)
480 struct svc_sock
*svsk
, *closesk
= NULL
;
485 spin_lock(&serv
->sv_lock
);
486 list_for_each_entry(svsk
, &serv
->sv_permsocks
, sk_list
) {
487 int onelen
= one_sock_name(buf
+len
, svsk
);
488 if (toclose
&& strcmp(toclose
, buf
+len
) == 0)
493 spin_unlock(&serv
->sv_lock
);
495 /* Should unregister with portmap, but you cannot
496 * unregister just one protocol...
498 svc_delete_socket(closesk
);
503 EXPORT_SYMBOL(svc_sock_names
);
506 * Check input queue length
509 svc_recv_available(struct svc_sock
*svsk
)
511 struct socket
*sock
= svsk
->sk_sock
;
514 err
= kernel_sock_ioctl(sock
, TIOCINQ
, (unsigned long) &avail
);
516 return (err
>= 0)? avail
: err
;
520 * Generic recvfrom routine.
523 svc_recvfrom(struct svc_rqst
*rqstp
, struct kvec
*iov
, int nr
, int buflen
)
529 rqstp
->rq_addrlen
= sizeof(rqstp
->rq_addr
);
530 sock
= rqstp
->rq_sock
->sk_sock
;
532 msg
.msg_name
= &rqstp
->rq_addr
;
533 msg
.msg_namelen
= sizeof(rqstp
->rq_addr
);
534 msg
.msg_control
= NULL
;
535 msg
.msg_controllen
= 0;
537 msg
.msg_flags
= MSG_DONTWAIT
;
539 len
= kernel_recvmsg(sock
, &msg
, iov
, nr
, buflen
, MSG_DONTWAIT
);
541 /* sock_recvmsg doesn't fill in the name/namelen, so we must..
542 * possibly we should cache this in the svc_sock structure
543 * at accept time. FIXME
545 alen
= sizeof(rqstp
->rq_addr
);
546 kernel_getpeername(sock
, (struct sockaddr
*)&rqstp
->rq_addr
, &alen
);
548 dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
549 rqstp
->rq_sock
, iov
[0].iov_base
, iov
[0].iov_len
, len
);
555 * Set socket snd and rcv buffer lengths
558 svc_sock_setbufsize(struct socket
*sock
, unsigned int snd
, unsigned int rcv
)
562 oldfs
= get_fs(); set_fs(KERNEL_DS
);
563 sock_setsockopt(sock
, SOL_SOCKET
, SO_SNDBUF
,
564 (char*)&snd
, sizeof(snd
));
565 sock_setsockopt(sock
, SOL_SOCKET
, SO_RCVBUF
,
566 (char*)&rcv
, sizeof(rcv
));
568 /* sock_setsockopt limits use to sysctl_?mem_max,
569 * which isn't acceptable. Until that is made conditional
570 * on not having CAP_SYS_RESOURCE or similar, we go direct...
571 * DaveM said I could!
574 sock
->sk
->sk_sndbuf
= snd
* 2;
575 sock
->sk
->sk_rcvbuf
= rcv
* 2;
576 sock
->sk
->sk_userlocks
|= SOCK_SNDBUF_LOCK
|SOCK_RCVBUF_LOCK
;
577 release_sock(sock
->sk
);
581 * INET callback when data has been received on the socket.
584 svc_udp_data_ready(struct sock
*sk
, int count
)
586 struct svc_sock
*svsk
= (struct svc_sock
*)sk
->sk_user_data
;
589 dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
590 svsk
, sk
, count
, test_bit(SK_BUSY
, &svsk
->sk_flags
));
591 set_bit(SK_DATA
, &svsk
->sk_flags
);
592 svc_sock_enqueue(svsk
);
594 if (sk
->sk_sleep
&& waitqueue_active(sk
->sk_sleep
))
595 wake_up_interruptible(sk
->sk_sleep
);
599 * INET callback when space is newly available on the socket.
602 svc_write_space(struct sock
*sk
)
604 struct svc_sock
*svsk
= (struct svc_sock
*)(sk
->sk_user_data
);
607 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
608 svsk
, sk
, test_bit(SK_BUSY
, &svsk
->sk_flags
));
609 svc_sock_enqueue(svsk
);
612 if (sk
->sk_sleep
&& waitqueue_active(sk
->sk_sleep
)) {
613 dprintk("RPC svc_write_space: someone sleeping on %p\n",
615 wake_up_interruptible(sk
->sk_sleep
);
620 * Receive a datagram from a UDP socket.
623 svc_udp_recvfrom(struct svc_rqst
*rqstp
)
625 struct svc_sock
*svsk
= rqstp
->rq_sock
;
626 struct svc_serv
*serv
= svsk
->sk_server
;
630 if (test_and_clear_bit(SK_CHNGBUF
, &svsk
->sk_flags
))
631 /* udp sockets need large rcvbuf as all pending
632 * requests are still in that buffer. sndbuf must
633 * also be large enough that there is enough space
634 * for one reply per thread. We count all threads
635 * rather than threads in a particular pool, which
636 * provides an upper bound on the number of threads
637 * which will access the socket.
639 svc_sock_setbufsize(svsk
->sk_sock
,
640 (serv
->sv_nrthreads
+3) * serv
->sv_bufsz
,
641 (serv
->sv_nrthreads
+3) * serv
->sv_bufsz
);
643 if ((rqstp
->rq_deferred
= svc_deferred_dequeue(svsk
))) {
644 svc_sock_received(svsk
);
645 return svc_deferred_recv(rqstp
);
648 clear_bit(SK_DATA
, &svsk
->sk_flags
);
649 while ((skb
= skb_recv_datagram(svsk
->sk_sk
, 0, 1, &err
)) == NULL
) {
650 if (err
== -EAGAIN
) {
651 svc_sock_received(svsk
);
654 /* possibly an icmp error */
655 dprintk("svc: recvfrom returned error %d\n", -err
);
657 if (skb
->tstamp
.off_sec
== 0) {
660 tv
.tv_sec
= xtime
.tv_sec
;
661 tv
.tv_usec
= xtime
.tv_nsec
/ NSEC_PER_USEC
;
662 skb_set_timestamp(skb
, &tv
);
663 /* Don't enable netstamp, sunrpc doesn't
664 need that much accuracy */
666 skb_get_timestamp(skb
, &svsk
->sk_sk
->sk_stamp
);
667 set_bit(SK_DATA
, &svsk
->sk_flags
); /* there may be more data... */
670 * Maybe more packets - kick another thread ASAP.
672 svc_sock_received(svsk
);
674 len
= skb
->len
- sizeof(struct udphdr
);
675 rqstp
->rq_arg
.len
= len
;
677 rqstp
->rq_prot
= IPPROTO_UDP
;
679 /* Get sender address */
680 rqstp
->rq_addr
.sin_family
= AF_INET
;
681 rqstp
->rq_addr
.sin_port
= skb
->h
.uh
->source
;
682 rqstp
->rq_addr
.sin_addr
.s_addr
= skb
->nh
.iph
->saddr
;
683 rqstp
->rq_daddr
= skb
->nh
.iph
->daddr
;
685 if (skb_is_nonlinear(skb
)) {
686 /* we have to copy */
688 if (csum_partial_copy_to_xdr(&rqstp
->rq_arg
, skb
)) {
691 skb_free_datagram(svsk
->sk_sk
, skb
);
695 skb_free_datagram(svsk
->sk_sk
, skb
);
697 /* we can use it in-place */
698 rqstp
->rq_arg
.head
[0].iov_base
= skb
->data
+ sizeof(struct udphdr
);
699 rqstp
->rq_arg
.head
[0].iov_len
= len
;
700 if (skb_checksum_complete(skb
)) {
701 skb_free_datagram(svsk
->sk_sk
, skb
);
704 rqstp
->rq_skbuff
= skb
;
707 rqstp
->rq_arg
.page_base
= 0;
708 if (len
<= rqstp
->rq_arg
.head
[0].iov_len
) {
709 rqstp
->rq_arg
.head
[0].iov_len
= len
;
710 rqstp
->rq_arg
.page_len
= 0;
712 rqstp
->rq_arg
.page_len
= len
- rqstp
->rq_arg
.head
[0].iov_len
;
713 rqstp
->rq_argused
+= (rqstp
->rq_arg
.page_len
+ PAGE_SIZE
- 1)/ PAGE_SIZE
;
717 serv
->sv_stats
->netudpcnt
++;
723 svc_udp_sendto(struct svc_rqst
*rqstp
)
727 error
= svc_sendto(rqstp
, &rqstp
->rq_res
);
728 if (error
== -ECONNREFUSED
)
729 /* ICMP error on earlier request. */
730 error
= svc_sendto(rqstp
, &rqstp
->rq_res
);
736 svc_udp_init(struct svc_sock
*svsk
)
738 svsk
->sk_sk
->sk_data_ready
= svc_udp_data_ready
;
739 svsk
->sk_sk
->sk_write_space
= svc_write_space
;
740 svsk
->sk_recvfrom
= svc_udp_recvfrom
;
741 svsk
->sk_sendto
= svc_udp_sendto
;
743 /* initialise setting must have enough space to
744 * receive and respond to one request.
745 * svc_udp_recvfrom will re-adjust if necessary
747 svc_sock_setbufsize(svsk
->sk_sock
,
748 3 * svsk
->sk_server
->sv_bufsz
,
749 3 * svsk
->sk_server
->sv_bufsz
);
751 set_bit(SK_DATA
, &svsk
->sk_flags
); /* might have come in before data_ready set up */
752 set_bit(SK_CHNGBUF
, &svsk
->sk_flags
);
756 * A data_ready event on a listening socket means there's a connection
757 * pending. Do not use state_change as a substitute for it.
760 svc_tcp_listen_data_ready(struct sock
*sk
, int count_unused
)
762 struct svc_sock
*svsk
= (struct svc_sock
*)sk
->sk_user_data
;
764 dprintk("svc: socket %p TCP (listen) state change %d\n",
768 * This callback may called twice when a new connection
769 * is established as a child socket inherits everything
770 * from a parent LISTEN socket.
771 * 1) data_ready method of the parent socket will be called
772 * when one of child sockets become ESTABLISHED.
773 * 2) data_ready method of the child socket may be called
774 * when it receives data before the socket is accepted.
775 * In case of 2, we should ignore it silently.
777 if (sk
->sk_state
== TCP_LISTEN
) {
779 set_bit(SK_CONN
, &svsk
->sk_flags
);
780 svc_sock_enqueue(svsk
);
782 printk("svc: socket %p: no user data\n", sk
);
785 if (sk
->sk_sleep
&& waitqueue_active(sk
->sk_sleep
))
786 wake_up_interruptible_all(sk
->sk_sleep
);
790 * A state change on a connected socket means it's dying or dead.
793 svc_tcp_state_change(struct sock
*sk
)
795 struct svc_sock
*svsk
= (struct svc_sock
*)sk
->sk_user_data
;
797 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
798 sk
, sk
->sk_state
, sk
->sk_user_data
);
801 printk("svc: socket %p: no user data\n", sk
);
803 set_bit(SK_CLOSE
, &svsk
->sk_flags
);
804 svc_sock_enqueue(svsk
);
806 if (sk
->sk_sleep
&& waitqueue_active(sk
->sk_sleep
))
807 wake_up_interruptible_all(sk
->sk_sleep
);
811 svc_tcp_data_ready(struct sock
*sk
, int count
)
813 struct svc_sock
*svsk
= (struct svc_sock
*)sk
->sk_user_data
;
815 dprintk("svc: socket %p TCP data ready (svsk %p)\n",
816 sk
, sk
->sk_user_data
);
818 set_bit(SK_DATA
, &svsk
->sk_flags
);
819 svc_sock_enqueue(svsk
);
821 if (sk
->sk_sleep
&& waitqueue_active(sk
->sk_sleep
))
822 wake_up_interruptible(sk
->sk_sleep
);
826 * Accept a TCP connection
829 svc_tcp_accept(struct svc_sock
*svsk
)
831 struct sockaddr_in sin
;
832 struct svc_serv
*serv
= svsk
->sk_server
;
833 struct socket
*sock
= svsk
->sk_sock
;
834 struct socket
*newsock
;
835 struct svc_sock
*newsvsk
;
838 dprintk("svc: tcp_accept %p sock %p\n", svsk
, sock
);
842 clear_bit(SK_CONN
, &svsk
->sk_flags
);
843 err
= kernel_accept(sock
, &newsock
, O_NONBLOCK
);
846 printk(KERN_WARNING
"%s: no more sockets!\n",
848 else if (err
!= -EAGAIN
&& net_ratelimit())
849 printk(KERN_WARNING
"%s: accept failed (err %d)!\n",
850 serv
->sv_name
, -err
);
854 set_bit(SK_CONN
, &svsk
->sk_flags
);
855 svc_sock_enqueue(svsk
);
858 err
= kernel_getpeername(newsock
, (struct sockaddr
*) &sin
, &slen
);
861 printk(KERN_WARNING
"%s: peername failed (err %d)!\n",
862 serv
->sv_name
, -err
);
863 goto failed
; /* aborted connection or whatever */
866 /* Ideally, we would want to reject connections from unauthorized
867 * hosts here, but when we get encription, the IP of the host won't
868 * tell us anything. For now just warn about unpriv connections.
870 if (ntohs(sin
.sin_port
) >= 1024) {
872 "%s: connect from unprivileged port: %u.%u.%u.%u:%d\n",
874 NIPQUAD(sin
.sin_addr
.s_addr
), ntohs(sin
.sin_port
));
877 dprintk("%s: connect from %u.%u.%u.%u:%04x\n", serv
->sv_name
,
878 NIPQUAD(sin
.sin_addr
.s_addr
), ntohs(sin
.sin_port
));
880 /* make sure that a write doesn't block forever when
883 newsock
->sk
->sk_sndtimeo
= HZ
*30;
885 if (!(newsvsk
= svc_setup_socket(serv
, newsock
, &err
, 0)))
889 /* make sure that we don't have too many active connections.
890 * If we have, something must be dropped.
892 * There's no point in trying to do random drop here for
893 * DoS prevention. The NFS clients does 1 reconnect in 15
894 * seconds. An attacker can easily beat that.
896 * The only somewhat efficient mechanism would be if drop
897 * old connections from the same IP first. But right now
898 * we don't even record the client IP in svc_sock.
900 if (serv
->sv_tmpcnt
> (serv
->sv_nrthreads
+3)*20) {
901 struct svc_sock
*svsk
= NULL
;
902 spin_lock_bh(&serv
->sv_lock
);
903 if (!list_empty(&serv
->sv_tempsocks
)) {
904 if (net_ratelimit()) {
905 /* Try to help the admin */
906 printk(KERN_NOTICE
"%s: too many open TCP "
907 "sockets, consider increasing the "
908 "number of nfsd threads\n",
910 printk(KERN_NOTICE
"%s: last TCP connect from "
913 NIPQUAD(sin
.sin_addr
.s_addr
),
914 ntohs(sin
.sin_port
));
917 * Always select the oldest socket. It's not fair,
920 svsk
= list_entry(serv
->sv_tempsocks
.prev
,
923 set_bit(SK_CLOSE
, &svsk
->sk_flags
);
924 atomic_inc(&svsk
->sk_inuse
);
926 spin_unlock_bh(&serv
->sv_lock
);
929 svc_sock_enqueue(svsk
);
936 serv
->sv_stats
->nettcpconn
++;
941 sock_release(newsock
);
946 * Receive data from a TCP socket.
949 svc_tcp_recvfrom(struct svc_rqst
*rqstp
)
951 struct svc_sock
*svsk
= rqstp
->rq_sock
;
952 struct svc_serv
*serv
= svsk
->sk_server
;
954 struct kvec vec
[RPCSVC_MAXPAGES
];
957 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
958 svsk
, test_bit(SK_DATA
, &svsk
->sk_flags
),
959 test_bit(SK_CONN
, &svsk
->sk_flags
),
960 test_bit(SK_CLOSE
, &svsk
->sk_flags
));
962 if ((rqstp
->rq_deferred
= svc_deferred_dequeue(svsk
))) {
963 svc_sock_received(svsk
);
964 return svc_deferred_recv(rqstp
);
967 if (test_bit(SK_CLOSE
, &svsk
->sk_flags
)) {
968 svc_delete_socket(svsk
);
972 if (test_bit(SK_CONN
, &svsk
->sk_flags
)) {
973 svc_tcp_accept(svsk
);
974 svc_sock_received(svsk
);
978 if (test_and_clear_bit(SK_CHNGBUF
, &svsk
->sk_flags
))
979 /* sndbuf needs to have room for one request
980 * per thread, otherwise we can stall even when the
981 * network isn't a bottleneck.
983 * We count all threads rather than threads in a
984 * particular pool, which provides an upper bound
985 * on the number of threads which will access the socket.
987 * rcvbuf just needs to be able to hold a few requests.
988 * Normally they will be removed from the queue
989 * as soon a a complete request arrives.
991 svc_sock_setbufsize(svsk
->sk_sock
,
992 (serv
->sv_nrthreads
+3) * serv
->sv_bufsz
,
995 clear_bit(SK_DATA
, &svsk
->sk_flags
);
997 /* Receive data. If we haven't got the record length yet, get
998 * the next four bytes. Otherwise try to gobble up as much as
999 * possible up to the complete record length.
1001 if (svsk
->sk_tcplen
< 4) {
1002 unsigned long want
= 4 - svsk
->sk_tcplen
;
1005 iov
.iov_base
= ((char *) &svsk
->sk_reclen
) + svsk
->sk_tcplen
;
1007 if ((len
= svc_recvfrom(rqstp
, &iov
, 1, want
)) < 0)
1009 svsk
->sk_tcplen
+= len
;
1012 dprintk("svc: short recvfrom while reading record length (%d of %lu)\n",
1014 svc_sock_received(svsk
);
1015 return -EAGAIN
; /* record header not complete */
1018 svsk
->sk_reclen
= ntohl(svsk
->sk_reclen
);
1019 if (!(svsk
->sk_reclen
& 0x80000000)) {
1020 /* FIXME: technically, a record can be fragmented,
1021 * and non-terminal fragments will not have the top
1022 * bit set in the fragment length header.
1023 * But apparently no known nfs clients send fragmented
1025 printk(KERN_NOTICE
"RPC: bad TCP reclen 0x%08lx (non-terminal)\n",
1026 (unsigned long) svsk
->sk_reclen
);
1029 svsk
->sk_reclen
&= 0x7fffffff;
1030 dprintk("svc: TCP record, %d bytes\n", svsk
->sk_reclen
);
1031 if (svsk
->sk_reclen
> serv
->sv_bufsz
) {
1032 printk(KERN_NOTICE
"RPC: bad TCP reclen 0x%08lx (large)\n",
1033 (unsigned long) svsk
->sk_reclen
);
1038 /* Check whether enough data is available */
1039 len
= svc_recv_available(svsk
);
1043 if (len
< svsk
->sk_reclen
) {
1044 dprintk("svc: incomplete TCP record (%d of %d)\n",
1045 len
, svsk
->sk_reclen
);
1046 svc_sock_received(svsk
);
1047 return -EAGAIN
; /* record not complete */
1049 len
= svsk
->sk_reclen
;
1050 set_bit(SK_DATA
, &svsk
->sk_flags
);
1052 vec
[0] = rqstp
->rq_arg
.head
[0];
1055 while (vlen
< len
) {
1056 vec
[pnum
].iov_base
= page_address(rqstp
->rq_argpages
[rqstp
->rq_argused
++]);
1057 vec
[pnum
].iov_len
= PAGE_SIZE
;
1062 /* Now receive data */
1063 len
= svc_recvfrom(rqstp
, vec
, pnum
, len
);
1067 dprintk("svc: TCP complete record (%d bytes)\n", len
);
1068 rqstp
->rq_arg
.len
= len
;
1069 rqstp
->rq_arg
.page_base
= 0;
1070 if (len
<= rqstp
->rq_arg
.head
[0].iov_len
) {
1071 rqstp
->rq_arg
.head
[0].iov_len
= len
;
1072 rqstp
->rq_arg
.page_len
= 0;
1074 rqstp
->rq_arg
.page_len
= len
- rqstp
->rq_arg
.head
[0].iov_len
;
1077 rqstp
->rq_skbuff
= NULL
;
1078 rqstp
->rq_prot
= IPPROTO_TCP
;
1080 /* Reset TCP read info */
1081 svsk
->sk_reclen
= 0;
1082 svsk
->sk_tcplen
= 0;
1084 svc_sock_received(svsk
);
1086 serv
->sv_stats
->nettcpcnt
++;
1091 svc_delete_socket(svsk
);
1095 if (len
== -EAGAIN
) {
1096 dprintk("RPC: TCP recvfrom got EAGAIN\n");
1097 svc_sock_received(svsk
);
1099 printk(KERN_NOTICE
"%s: recvfrom returned errno %d\n",
1100 svsk
->sk_server
->sv_name
, -len
);
1108 * Send out data on TCP socket.
1111 svc_tcp_sendto(struct svc_rqst
*rqstp
)
1113 struct xdr_buf
*xbufp
= &rqstp
->rq_res
;
1117 /* Set up the first element of the reply kvec.
1118 * Any other kvecs that may be in use have been taken
1119 * care of by the server implementation itself.
1121 reclen
= htonl(0x80000000|((xbufp
->len
) - 4));
1122 memcpy(xbufp
->head
[0].iov_base
, &reclen
, 4);
1124 if (test_bit(SK_DEAD
, &rqstp
->rq_sock
->sk_flags
))
1127 sent
= svc_sendto(rqstp
, &rqstp
->rq_res
);
1128 if (sent
!= xbufp
->len
) {
1129 printk(KERN_NOTICE
"rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n",
1130 rqstp
->rq_sock
->sk_server
->sv_name
,
1131 (sent
<0)?"got error":"sent only",
1133 svc_delete_socket(rqstp
->rq_sock
);
1140 svc_tcp_init(struct svc_sock
*svsk
)
1142 struct sock
*sk
= svsk
->sk_sk
;
1143 struct tcp_sock
*tp
= tcp_sk(sk
);
1145 svsk
->sk_recvfrom
= svc_tcp_recvfrom
;
1146 svsk
->sk_sendto
= svc_tcp_sendto
;
1148 if (sk
->sk_state
== TCP_LISTEN
) {
1149 dprintk("setting up TCP socket for listening\n");
1150 sk
->sk_data_ready
= svc_tcp_listen_data_ready
;
1151 set_bit(SK_CONN
, &svsk
->sk_flags
);
1153 dprintk("setting up TCP socket for reading\n");
1154 sk
->sk_state_change
= svc_tcp_state_change
;
1155 sk
->sk_data_ready
= svc_tcp_data_ready
;
1156 sk
->sk_write_space
= svc_write_space
;
1158 svsk
->sk_reclen
= 0;
1159 svsk
->sk_tcplen
= 0;
1161 tp
->nonagle
= 1; /* disable Nagle's algorithm */
1163 /* initialise setting must have enough space to
1164 * receive and respond to one request.
1165 * svc_tcp_recvfrom will re-adjust if necessary
1167 svc_sock_setbufsize(svsk
->sk_sock
,
1168 3 * svsk
->sk_server
->sv_bufsz
,
1169 3 * svsk
->sk_server
->sv_bufsz
);
1171 set_bit(SK_CHNGBUF
, &svsk
->sk_flags
);
1172 set_bit(SK_DATA
, &svsk
->sk_flags
);
1173 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1174 set_bit(SK_CLOSE
, &svsk
->sk_flags
);
1179 svc_sock_update_bufs(struct svc_serv
*serv
)
1182 * The number of server threads has changed. Update
1183 * rcvbuf and sndbuf accordingly on all sockets
1185 struct list_head
*le
;
1187 spin_lock_bh(&serv
->sv_lock
);
1188 list_for_each(le
, &serv
->sv_permsocks
) {
1189 struct svc_sock
*svsk
=
1190 list_entry(le
, struct svc_sock
, sk_list
);
1191 set_bit(SK_CHNGBUF
, &svsk
->sk_flags
);
1193 list_for_each(le
, &serv
->sv_tempsocks
) {
1194 struct svc_sock
*svsk
=
1195 list_entry(le
, struct svc_sock
, sk_list
);
1196 set_bit(SK_CHNGBUF
, &svsk
->sk_flags
);
1198 spin_unlock_bh(&serv
->sv_lock
);
1202 * Receive the next request on any socket. This code is carefully
1203 * organised not to touch any cachelines in the shared svc_serv
1204 * structure, only cachelines in the local svc_pool.
1207 svc_recv(struct svc_rqst
*rqstp
, long timeout
)
1209 struct svc_sock
*svsk
=NULL
;
1210 struct svc_serv
*serv
= rqstp
->rq_server
;
1211 struct svc_pool
*pool
= rqstp
->rq_pool
;
1214 struct xdr_buf
*arg
;
1215 DECLARE_WAITQUEUE(wait
, current
);
1217 dprintk("svc: server %p waiting for data (to = %ld)\n",
1222 "svc_recv: service %p, socket not NULL!\n",
1224 if (waitqueue_active(&rqstp
->rq_wait
))
1226 "svc_recv: service %p, wait queue active!\n",
1229 /* Initialize the buffers */
1230 /* first reclaim pages that were moved to response list */
1231 svc_pushback_allpages(rqstp
);
1233 /* now allocate needed pages. If we get a failure, sleep briefly */
1234 pages
= 2 + (serv
->sv_bufsz
+ PAGE_SIZE
-1) / PAGE_SIZE
;
1235 while (rqstp
->rq_arghi
< pages
) {
1236 struct page
*p
= alloc_page(GFP_KERNEL
);
1238 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
1241 rqstp
->rq_argpages
[rqstp
->rq_arghi
++] = p
;
1244 /* Make arg->head point to first page and arg->pages point to rest */
1245 arg
= &rqstp
->rq_arg
;
1246 arg
->head
[0].iov_base
= page_address(rqstp
->rq_argpages
[0]);
1247 arg
->head
[0].iov_len
= PAGE_SIZE
;
1248 rqstp
->rq_argused
= 1;
1249 arg
->pages
= rqstp
->rq_argpages
+ 1;
1251 /* save at least one page for response */
1252 arg
->page_len
= (pages
-2)*PAGE_SIZE
;
1253 arg
->len
= (pages
-1)*PAGE_SIZE
;
1254 arg
->tail
[0].iov_len
= 0;
1261 spin_lock_bh(&pool
->sp_lock
);
1262 if ((svsk
= svc_sock_dequeue(pool
)) != NULL
) {
1263 rqstp
->rq_sock
= svsk
;
1264 atomic_inc(&svsk
->sk_inuse
);
1265 rqstp
->rq_reserved
= serv
->sv_bufsz
;
1266 atomic_add(rqstp
->rq_reserved
, &svsk
->sk_reserved
);
1268 /* No data pending. Go to sleep */
1269 svc_thread_enqueue(pool
, rqstp
);
1272 * We have to be able to interrupt this wait
1273 * to bring down the daemons ...
1275 set_current_state(TASK_INTERRUPTIBLE
);
1276 add_wait_queue(&rqstp
->rq_wait
, &wait
);
1277 spin_unlock_bh(&pool
->sp_lock
);
1279 schedule_timeout(timeout
);
1283 spin_lock_bh(&pool
->sp_lock
);
1284 remove_wait_queue(&rqstp
->rq_wait
, &wait
);
1286 if (!(svsk
= rqstp
->rq_sock
)) {
1287 svc_thread_dequeue(pool
, rqstp
);
1288 spin_unlock_bh(&pool
->sp_lock
);
1289 dprintk("svc: server %p, no data yet\n", rqstp
);
1290 return signalled()? -EINTR
: -EAGAIN
;
1293 spin_unlock_bh(&pool
->sp_lock
);
1295 dprintk("svc: server %p, pool %u, socket %p, inuse=%d\n",
1296 rqstp
, pool
->sp_id
, svsk
, atomic_read(&svsk
->sk_inuse
));
1297 len
= svsk
->sk_recvfrom(rqstp
);
1298 dprintk("svc: got len=%d\n", len
);
1300 /* No data, incomplete (TCP) read, or accept() */
1301 if (len
== 0 || len
== -EAGAIN
) {
1302 rqstp
->rq_res
.len
= 0;
1303 svc_sock_release(rqstp
);
1306 svsk
->sk_lastrecv
= get_seconds();
1307 clear_bit(SK_OLD
, &svsk
->sk_flags
);
1309 rqstp
->rq_secure
= ntohs(rqstp
->rq_addr
.sin_port
) < 1024;
1310 rqstp
->rq_chandle
.defer
= svc_defer
;
1313 serv
->sv_stats
->netcnt
++;
1321 svc_drop(struct svc_rqst
*rqstp
)
1323 dprintk("svc: socket %p dropped request\n", rqstp
->rq_sock
);
1324 svc_sock_release(rqstp
);
1328 * Return reply to client.
1331 svc_send(struct svc_rqst
*rqstp
)
1333 struct svc_sock
*svsk
;
1337 if ((svsk
= rqstp
->rq_sock
) == NULL
) {
1338 printk(KERN_WARNING
"NULL socket pointer in %s:%d\n",
1339 __FILE__
, __LINE__
);
1343 /* release the receive skb before sending the reply */
1344 svc_release_skb(rqstp
);
1346 /* calculate over-all length */
1347 xb
= & rqstp
->rq_res
;
1348 xb
->len
= xb
->head
[0].iov_len
+
1350 xb
->tail
[0].iov_len
;
1352 /* Grab svsk->sk_mutex to serialize outgoing data. */
1353 mutex_lock(&svsk
->sk_mutex
);
1354 if (test_bit(SK_DEAD
, &svsk
->sk_flags
))
1357 len
= svsk
->sk_sendto(rqstp
);
1358 mutex_unlock(&svsk
->sk_mutex
);
1359 svc_sock_release(rqstp
);
1361 if (len
== -ECONNREFUSED
|| len
== -ENOTCONN
|| len
== -EAGAIN
)
1367 * Timer function to close old temporary sockets, using
1368 * a mark-and-sweep algorithm.
1371 svc_age_temp_sockets(unsigned long closure
)
1373 struct svc_serv
*serv
= (struct svc_serv
*)closure
;
1374 struct svc_sock
*svsk
;
1375 struct list_head
*le
, *next
;
1376 LIST_HEAD(to_be_aged
);
1378 dprintk("svc_age_temp_sockets\n");
1380 if (!spin_trylock_bh(&serv
->sv_lock
)) {
1381 /* busy, try again 1 sec later */
1382 dprintk("svc_age_temp_sockets: busy\n");
1383 mod_timer(&serv
->sv_temptimer
, jiffies
+ HZ
);
1387 list_for_each_safe(le
, next
, &serv
->sv_tempsocks
) {
1388 svsk
= list_entry(le
, struct svc_sock
, sk_list
);
1390 if (!test_and_set_bit(SK_OLD
, &svsk
->sk_flags
))
1392 if (atomic_read(&svsk
->sk_inuse
) || test_bit(SK_BUSY
, &svsk
->sk_flags
))
1394 atomic_inc(&svsk
->sk_inuse
);
1395 list_move(le
, &to_be_aged
);
1396 set_bit(SK_CLOSE
, &svsk
->sk_flags
);
1397 set_bit(SK_DETACHED
, &svsk
->sk_flags
);
1399 spin_unlock_bh(&serv
->sv_lock
);
1401 while (!list_empty(&to_be_aged
)) {
1402 le
= to_be_aged
.next
;
1403 /* fiddling the sk_list node is safe 'cos we're SK_DETACHED */
1405 svsk
= list_entry(le
, struct svc_sock
, sk_list
);
1407 dprintk("queuing svsk %p for closing, %lu seconds old\n",
1408 svsk
, get_seconds() - svsk
->sk_lastrecv
);
1410 /* a thread will dequeue and close it soon */
1411 svc_sock_enqueue(svsk
);
1415 mod_timer(&serv
->sv_temptimer
, jiffies
+ svc_conn_age_period
* HZ
);
1419 * Initialize socket for RPC use and create svc_sock struct
1420 * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
1422 static struct svc_sock
*
1423 svc_setup_socket(struct svc_serv
*serv
, struct socket
*sock
,
1424 int *errp
, int pmap_register
)
1426 struct svc_sock
*svsk
;
1429 dprintk("svc: svc_setup_socket %p\n", sock
);
1430 if (!(svsk
= kzalloc(sizeof(*svsk
), GFP_KERNEL
))) {
1437 /* Register socket with portmapper */
1438 if (*errp
>= 0 && pmap_register
)
1439 *errp
= svc_register(serv
, inet
->sk_protocol
,
1440 ntohs(inet_sk(inet
)->sport
));
1447 set_bit(SK_BUSY
, &svsk
->sk_flags
);
1448 inet
->sk_user_data
= svsk
;
1449 svsk
->sk_sock
= sock
;
1451 svsk
->sk_ostate
= inet
->sk_state_change
;
1452 svsk
->sk_odata
= inet
->sk_data_ready
;
1453 svsk
->sk_owspace
= inet
->sk_write_space
;
1454 svsk
->sk_server
= serv
;
1455 atomic_set(&svsk
->sk_inuse
, 0);
1456 svsk
->sk_lastrecv
= get_seconds();
1457 spin_lock_init(&svsk
->sk_defer_lock
);
1458 INIT_LIST_HEAD(&svsk
->sk_deferred
);
1459 INIT_LIST_HEAD(&svsk
->sk_ready
);
1460 mutex_init(&svsk
->sk_mutex
);
1462 /* Initialize the socket */
1463 if (sock
->type
== SOCK_DGRAM
)
1468 spin_lock_bh(&serv
->sv_lock
);
1469 if (!pmap_register
) {
1470 set_bit(SK_TEMP
, &svsk
->sk_flags
);
1471 list_add(&svsk
->sk_list
, &serv
->sv_tempsocks
);
1473 if (serv
->sv_temptimer
.function
== NULL
) {
1474 /* setup timer to age temp sockets */
1475 setup_timer(&serv
->sv_temptimer
, svc_age_temp_sockets
,
1476 (unsigned long)serv
);
1477 mod_timer(&serv
->sv_temptimer
,
1478 jiffies
+ svc_conn_age_period
* HZ
);
1481 clear_bit(SK_TEMP
, &svsk
->sk_flags
);
1482 list_add(&svsk
->sk_list
, &serv
->sv_permsocks
);
1484 spin_unlock_bh(&serv
->sv_lock
);
1486 dprintk("svc: svc_setup_socket created %p (inet %p)\n",
1489 clear_bit(SK_BUSY
, &svsk
->sk_flags
);
1490 svc_sock_enqueue(svsk
);
1494 int svc_addsock(struct svc_serv
*serv
,
1500 struct socket
*so
= sockfd_lookup(fd
, &err
);
1501 struct svc_sock
*svsk
= NULL
;
1505 if (so
->sk
->sk_family
!= AF_INET
)
1506 err
= -EAFNOSUPPORT
;
1507 else if (so
->sk
->sk_protocol
!= IPPROTO_TCP
&&
1508 so
->sk
->sk_protocol
!= IPPROTO_UDP
)
1509 err
= -EPROTONOSUPPORT
;
1510 else if (so
->state
> SS_UNCONNECTED
)
1513 svsk
= svc_setup_socket(serv
, so
, &err
, 1);
1521 if (proto
) *proto
= so
->sk
->sk_protocol
;
1522 return one_sock_name(name_return
, svsk
);
1524 EXPORT_SYMBOL_GPL(svc_addsock
);
1527 * Create socket for RPC service.
1530 svc_create_socket(struct svc_serv
*serv
, int protocol
, struct sockaddr_in
*sin
)
1532 struct svc_sock
*svsk
;
1533 struct socket
*sock
;
1537 dprintk("svc: svc_create_socket(%s, %d, %u.%u.%u.%u:%d)\n",
1538 serv
->sv_program
->pg_name
, protocol
,
1539 NIPQUAD(sin
->sin_addr
.s_addr
),
1540 ntohs(sin
->sin_port
));
1542 if (protocol
!= IPPROTO_UDP
&& protocol
!= IPPROTO_TCP
) {
1543 printk(KERN_WARNING
"svc: only UDP and TCP "
1544 "sockets supported\n");
1547 type
= (protocol
== IPPROTO_UDP
)? SOCK_DGRAM
: SOCK_STREAM
;
1549 if ((error
= sock_create_kern(PF_INET
, type
, protocol
, &sock
)) < 0)
1552 if (type
== SOCK_STREAM
)
1553 sock
->sk
->sk_reuse
= 1; /* allow address reuse */
1554 error
= kernel_bind(sock
, (struct sockaddr
*) sin
,
1559 if (protocol
== IPPROTO_TCP
) {
1560 if ((error
= kernel_listen(sock
, 64)) < 0)
1564 if ((svsk
= svc_setup_socket(serv
, sock
, &error
, 1)) != NULL
)
1568 dprintk("svc: svc_create_socket error = %d\n", -error
);
1574 * Remove a dead socket
1577 svc_delete_socket(struct svc_sock
*svsk
)
1579 struct svc_serv
*serv
;
1582 dprintk("svc: svc_delete_socket(%p)\n", svsk
);
1584 serv
= svsk
->sk_server
;
1587 sk
->sk_state_change
= svsk
->sk_ostate
;
1588 sk
->sk_data_ready
= svsk
->sk_odata
;
1589 sk
->sk_write_space
= svsk
->sk_owspace
;
1591 spin_lock_bh(&serv
->sv_lock
);
1593 if (!test_and_set_bit(SK_DETACHED
, &svsk
->sk_flags
))
1594 list_del_init(&svsk
->sk_list
);
1596 * We used to delete the svc_sock from whichever list
1597 * it's sk_ready node was on, but we don't actually
1598 * need to. This is because the only time we're called
1599 * while still attached to a queue, the queue itself
1600 * is about to be destroyed (in svc_destroy).
1602 if (!test_and_set_bit(SK_DEAD
, &svsk
->sk_flags
))
1603 if (test_bit(SK_TEMP
, &svsk
->sk_flags
))
1606 if (!atomic_read(&svsk
->sk_inuse
)) {
1607 spin_unlock_bh(&serv
->sv_lock
);
1608 if (svsk
->sk_sock
->file
)
1609 sockfd_put(svsk
->sk_sock
);
1611 sock_release(svsk
->sk_sock
);
1614 spin_unlock_bh(&serv
->sv_lock
);
1615 dprintk(KERN_NOTICE
"svc: server socket destroy delayed\n");
1616 /* svsk->sk_server = NULL; */
1621 * Make a socket for nfsd and lockd
1624 svc_makesock(struct svc_serv
*serv
, int protocol
, unsigned short port
)
1626 struct sockaddr_in sin
;
1628 dprintk("svc: creating socket proto = %d\n", protocol
);
1629 sin
.sin_family
= AF_INET
;
1630 sin
.sin_addr
.s_addr
= INADDR_ANY
;
1631 sin
.sin_port
= htons(port
);
1632 return svc_create_socket(serv
, protocol
, &sin
);
1636 * Handle defer and revisit of requests
1639 static void svc_revisit(struct cache_deferred_req
*dreq
, int too_many
)
1641 struct svc_deferred_req
*dr
= container_of(dreq
, struct svc_deferred_req
, handle
);
1642 struct svc_sock
*svsk
;
1645 svc_sock_put(dr
->svsk
);
1649 dprintk("revisit queued\n");
1652 spin_lock_bh(&svsk
->sk_defer_lock
);
1653 list_add(&dr
->handle
.recent
, &svsk
->sk_deferred
);
1654 spin_unlock_bh(&svsk
->sk_defer_lock
);
1655 set_bit(SK_DEFERRED
, &svsk
->sk_flags
);
1656 svc_sock_enqueue(svsk
);
1660 static struct cache_deferred_req
*
1661 svc_defer(struct cache_req
*req
)
1663 struct svc_rqst
*rqstp
= container_of(req
, struct svc_rqst
, rq_chandle
);
1664 int size
= sizeof(struct svc_deferred_req
) + (rqstp
->rq_arg
.len
);
1665 struct svc_deferred_req
*dr
;
1667 if (rqstp
->rq_arg
.page_len
)
1668 return NULL
; /* if more than a page, give up FIXME */
1669 if (rqstp
->rq_deferred
) {
1670 dr
= rqstp
->rq_deferred
;
1671 rqstp
->rq_deferred
= NULL
;
1673 int skip
= rqstp
->rq_arg
.len
- rqstp
->rq_arg
.head
[0].iov_len
;
1674 /* FIXME maybe discard if size too large */
1675 dr
= kmalloc(size
, GFP_KERNEL
);
1679 dr
->handle
.owner
= rqstp
->rq_server
;
1680 dr
->prot
= rqstp
->rq_prot
;
1681 dr
->addr
= rqstp
->rq_addr
;
1682 dr
->daddr
= rqstp
->rq_daddr
;
1683 dr
->argslen
= rqstp
->rq_arg
.len
>> 2;
1684 memcpy(dr
->args
, rqstp
->rq_arg
.head
[0].iov_base
-skip
, dr
->argslen
<<2);
1686 atomic_inc(&rqstp
->rq_sock
->sk_inuse
);
1687 dr
->svsk
= rqstp
->rq_sock
;
1689 dr
->handle
.revisit
= svc_revisit
;
1694 * recv data from a deferred request into an active one
1696 static int svc_deferred_recv(struct svc_rqst
*rqstp
)
1698 struct svc_deferred_req
*dr
= rqstp
->rq_deferred
;
1700 rqstp
->rq_arg
.head
[0].iov_base
= dr
->args
;
1701 rqstp
->rq_arg
.head
[0].iov_len
= dr
->argslen
<<2;
1702 rqstp
->rq_arg
.page_len
= 0;
1703 rqstp
->rq_arg
.len
= dr
->argslen
<<2;
1704 rqstp
->rq_prot
= dr
->prot
;
1705 rqstp
->rq_addr
= dr
->addr
;
1706 rqstp
->rq_daddr
= dr
->daddr
;
1707 return dr
->argslen
<<2;
1711 static struct svc_deferred_req
*svc_deferred_dequeue(struct svc_sock
*svsk
)
1713 struct svc_deferred_req
*dr
= NULL
;
1715 if (!test_bit(SK_DEFERRED
, &svsk
->sk_flags
))
1717 spin_lock_bh(&svsk
->sk_defer_lock
);
1718 clear_bit(SK_DEFERRED
, &svsk
->sk_flags
);
1719 if (!list_empty(&svsk
->sk_deferred
)) {
1720 dr
= list_entry(svsk
->sk_deferred
.next
,
1721 struct svc_deferred_req
,
1723 list_del_init(&dr
->handle
.recent
);
1724 set_bit(SK_DEFERRED
, &svsk
->sk_flags
);
1726 spin_unlock_bh(&svsk
->sk_defer_lock
);