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_serv->sv_lock protects most stuff for that service.
50 * svc_sock->sk_defer_lock protects the svc_sock->sk_deferred list
52 * Some flags can be set to certain values at any time
53 * providing that certain rules are followed:
55 * SK_BUSY can be set to 0 at any time.
56 * svc_sock_enqueue must be called afterwards
57 * SK_CONN, SK_DATA, can be set or cleared at any time.
58 * after a set, svc_sock_enqueue must be called.
59 * after a clear, the socket must be read/accepted
60 * if this succeeds, it must be set again.
61 * SK_CLOSE can set at any time. It is never cleared.
65 #define RPCDBG_FACILITY RPCDBG_SVCSOCK
68 static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
69 int *errp, int pmap_reg);
70 static void svc_udp_data_ready(struct sock *, int);
71 static int svc_udp_recvfrom(struct svc_rqst *);
72 static int svc_udp_sendto(struct svc_rqst *);
74 static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk);
75 static int svc_deferred_recv(struct svc_rqst *rqstp);
76 static struct cache_deferred_req *svc_defer(struct cache_req *req);
78 /* apparently the "standard" is that clients close
79 * idle connections after 5 minutes, servers after
81 * http://www.connectathon.org/talks96/nfstcp.pdf
83 static int svc_conn_age_period = 6*60;
86 * Queue up an idle server thread. Must have serv->sv_lock held.
87 * Note: this is really a stack rather than a queue, so that we only
88 * use as many different threads as we need, and the rest don't polute
92 svc_serv_enqueue(struct svc_serv *serv, struct svc_rqst *rqstp)
94 list_add(&rqstp->rq_list, &serv->sv_threads);
98 * Dequeue an nfsd thread. Must have serv->sv_lock held.
101 svc_serv_dequeue(struct svc_serv *serv, struct svc_rqst *rqstp)
103 list_del(&rqstp->rq_list);
107 * Release an skbuff after use
110 svc_release_skb(struct svc_rqst *rqstp)
112 struct sk_buff *skb = rqstp->rq_skbuff;
113 struct svc_deferred_req *dr = rqstp->rq_deferred;
116 rqstp->rq_skbuff = NULL;
118 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
119 skb_free_datagram(rqstp->rq_sock->sk_sk, skb);
122 rqstp->rq_deferred = NULL;
128 * Any space to write?
130 static inline unsigned long
131 svc_sock_wspace(struct svc_sock *svsk)
135 if (svsk->sk_sock->type == SOCK_STREAM)
136 wspace = sk_stream_wspace(svsk->sk_sk);
138 wspace = sock_wspace(svsk->sk_sk);
144 * Queue up a socket with data pending. If there are idle nfsd
145 * processes, wake 'em up.
149 svc_sock_enqueue(struct svc_sock *svsk)
151 struct svc_serv *serv = svsk->sk_server;
152 struct svc_rqst *rqstp;
154 if (!(svsk->sk_flags &
155 ( (1<<SK_CONN)|(1<<SK_DATA)|(1<<SK_CLOSE)|(1<<SK_DEFERRED)) ))
157 if (test_bit(SK_DEAD, &svsk->sk_flags))
160 spin_lock_bh(&serv->sv_lock);
162 if (!list_empty(&serv->sv_threads) &&
163 !list_empty(&serv->sv_sockets))
165 "svc_sock_enqueue: threads and sockets both waiting??\n");
167 if (test_bit(SK_DEAD, &svsk->sk_flags)) {
168 /* Don't enqueue dead sockets */
169 dprintk("svc: socket %p is dead, not enqueued\n", svsk->sk_sk);
173 if (test_bit(SK_BUSY, &svsk->sk_flags)) {
174 /* Don't enqueue socket while daemon is receiving */
175 dprintk("svc: socket %p busy, not enqueued\n", svsk->sk_sk);
179 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
180 if (((svsk->sk_reserved + serv->sv_bufsz)*2
181 > svc_sock_wspace(svsk))
182 && !test_bit(SK_CLOSE, &svsk->sk_flags)
183 && !test_bit(SK_CONN, &svsk->sk_flags)) {
184 /* Don't enqueue while not enough space for reply */
185 dprintk("svc: socket %p no space, %d*2 > %ld, not enqueued\n",
186 svsk->sk_sk, svsk->sk_reserved+serv->sv_bufsz,
187 svc_sock_wspace(svsk));
190 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
192 /* Mark socket as busy. It will remain in this state until the
193 * server has processed all pending data and put the socket back
196 set_bit(SK_BUSY, &svsk->sk_flags);
198 if (!list_empty(&serv->sv_threads)) {
199 rqstp = list_entry(serv->sv_threads.next,
202 dprintk("svc: socket %p served by daemon %p\n",
204 svc_serv_dequeue(serv, rqstp);
207 "svc_sock_enqueue: server %p, rq_sock=%p!\n",
208 rqstp, rqstp->rq_sock);
209 rqstp->rq_sock = svsk;
210 atomic_inc(&svsk->sk_inuse);
211 rqstp->rq_reserved = serv->sv_bufsz;
212 svsk->sk_reserved += rqstp->rq_reserved;
213 wake_up(&rqstp->rq_wait);
215 dprintk("svc: socket %p put into queue\n", svsk->sk_sk);
216 list_add_tail(&svsk->sk_ready, &serv->sv_sockets);
220 spin_unlock_bh(&serv->sv_lock);
224 * Dequeue the first socket. Must be called with the serv->sv_lock held.
226 static inline struct svc_sock *
227 svc_sock_dequeue(struct svc_serv *serv)
229 struct svc_sock *svsk;
231 if (list_empty(&serv->sv_sockets))
234 svsk = list_entry(serv->sv_sockets.next,
235 struct svc_sock, sk_ready);
236 list_del_init(&svsk->sk_ready);
238 dprintk("svc: socket %p dequeued, inuse=%d\n",
239 svsk->sk_sk, atomic_read(&svsk->sk_inuse));
245 * Having read something from a socket, check whether it
246 * needs to be re-enqueued.
247 * Note: SK_DATA only gets cleared when a read-attempt finds
248 * no (or insufficient) data.
251 svc_sock_received(struct svc_sock *svsk)
253 clear_bit(SK_BUSY, &svsk->sk_flags);
254 svc_sock_enqueue(svsk);
259 * svc_reserve - change the space reserved for the reply to a request.
260 * @rqstp: The request in question
261 * @space: new max space to reserve
263 * Each request reserves some space on the output queue of the socket
264 * to make sure the reply fits. This function reduces that reserved
265 * space to be the amount of space used already, plus @space.
268 void svc_reserve(struct svc_rqst *rqstp, int space)
270 space += rqstp->rq_res.head[0].iov_len;
272 if (space < rqstp->rq_reserved) {
273 struct svc_sock *svsk = rqstp->rq_sock;
274 spin_lock_bh(&svsk->sk_server->sv_lock);
275 svsk->sk_reserved -= (rqstp->rq_reserved - space);
276 rqstp->rq_reserved = space;
277 spin_unlock_bh(&svsk->sk_server->sv_lock);
279 svc_sock_enqueue(svsk);
284 * Release a socket after use.
287 svc_sock_put(struct svc_sock *svsk)
289 if (atomic_dec_and_test(&svsk->sk_inuse) && test_bit(SK_DEAD, &svsk->sk_flags)) {
290 dprintk("svc: releasing dead socket\n");
291 sock_release(svsk->sk_sock);
297 svc_sock_release(struct svc_rqst *rqstp)
299 struct svc_sock *svsk = rqstp->rq_sock;
301 svc_release_skb(rqstp);
303 svc_free_allpages(rqstp);
304 rqstp->rq_res.page_len = 0;
305 rqstp->rq_res.page_base = 0;
308 /* Reset response buffer and release
310 * But first, check that enough space was reserved
311 * for the reply, otherwise we have a bug!
313 if ((rqstp->rq_res.len) > rqstp->rq_reserved)
314 printk(KERN_ERR "RPC request reserved %d but used %d\n",
318 rqstp->rq_res.head[0].iov_len = 0;
319 svc_reserve(rqstp, 0);
320 rqstp->rq_sock = NULL;
326 * External function to wake up a server waiting for data
329 svc_wake_up(struct svc_serv *serv)
331 struct svc_rqst *rqstp;
333 spin_lock_bh(&serv->sv_lock);
334 if (!list_empty(&serv->sv_threads)) {
335 rqstp = list_entry(serv->sv_threads.next,
338 dprintk("svc: daemon %p woken up.\n", rqstp);
340 svc_serv_dequeue(serv, rqstp);
341 rqstp->rq_sock = NULL;
343 wake_up(&rqstp->rq_wait);
345 spin_unlock_bh(&serv->sv_lock);
349 * Generic sendto routine
352 svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
354 struct svc_sock *svsk = rqstp->rq_sock;
355 struct socket *sock = svsk->sk_sock;
357 char buffer[CMSG_SPACE(sizeof(struct in_pktinfo))];
358 struct cmsghdr *cmh = (struct cmsghdr *)buffer;
359 struct in_pktinfo *pki = (struct in_pktinfo *)CMSG_DATA(cmh);
363 struct page **ppage = xdr->pages;
364 size_t base = xdr->page_base;
365 unsigned int pglen = xdr->page_len;
366 unsigned int flags = MSG_MORE;
370 if (rqstp->rq_prot == IPPROTO_UDP) {
371 /* set the source and destination */
373 msg.msg_name = &rqstp->rq_addr;
374 msg.msg_namelen = sizeof(rqstp->rq_addr);
377 msg.msg_flags = MSG_MORE;
379 msg.msg_control = cmh;
380 msg.msg_controllen = sizeof(buffer);
381 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
382 cmh->cmsg_level = SOL_IP;
383 cmh->cmsg_type = IP_PKTINFO;
384 pki->ipi_ifindex = 0;
385 pki->ipi_spec_dst.s_addr = rqstp->rq_daddr;
387 if (sock_sendmsg(sock, &msg, 0) < 0)
392 if (slen == xdr->head[0].iov_len)
394 len = kernel_sendpage(sock, rqstp->rq_respages[0], 0, xdr->head[0].iov_len, flags);
395 if (len != xdr->head[0].iov_len)
397 slen -= xdr->head[0].iov_len;
402 size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
406 result = kernel_sendpage(sock, *ppage, base, size, flags);
413 size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
418 if (xdr->tail[0].iov_len) {
419 result = kernel_sendpage(sock, rqstp->rq_respages[rqstp->rq_restailpage],
420 ((unsigned long)xdr->tail[0].iov_base)& (PAGE_SIZE-1),
421 xdr->tail[0].iov_len, 0);
427 dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %x)\n",
428 rqstp->rq_sock, xdr->head[0].iov_base, xdr->head[0].iov_len, xdr->len, len,
429 rqstp->rq_addr.sin_addr.s_addr);
435 * Report socket names for nfsdfs
437 static int one_sock_name(char *buf, struct svc_sock *svsk)
441 switch(svsk->sk_sk->sk_family) {
443 len = sprintf(buf, "ipv4 %s %u.%u.%u.%u %d\n",
444 svsk->sk_sk->sk_protocol==IPPROTO_UDP?
446 NIPQUAD(inet_sk(svsk->sk_sk)->rcv_saddr),
447 inet_sk(svsk->sk_sk)->num);
450 len = sprintf(buf, "*unknown-%d*\n",
451 svsk->sk_sk->sk_family);
457 svc_sock_names(char *buf, struct svc_serv *serv, char *toclose)
459 struct svc_sock *svsk, *closesk = NULL;
464 spin_lock(&serv->sv_lock);
465 list_for_each_entry(svsk, &serv->sv_permsocks, sk_list) {
466 int onelen = one_sock_name(buf+len, svsk);
467 if (toclose && strcmp(toclose, buf+len) == 0)
472 spin_unlock(&serv->sv_lock);
474 svc_delete_socket(closesk);
477 EXPORT_SYMBOL(svc_sock_names);
480 * Check input queue length
483 svc_recv_available(struct svc_sock *svsk)
485 struct socket *sock = svsk->sk_sock;
488 err = kernel_sock_ioctl(sock, TIOCINQ, (unsigned long) &avail);
490 return (err >= 0)? avail : err;
494 * Generic recvfrom routine.
497 svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr, int buflen)
503 rqstp->rq_addrlen = sizeof(rqstp->rq_addr);
504 sock = rqstp->rq_sock->sk_sock;
506 msg.msg_name = &rqstp->rq_addr;
507 msg.msg_namelen = sizeof(rqstp->rq_addr);
508 msg.msg_control = NULL;
509 msg.msg_controllen = 0;
511 msg.msg_flags = MSG_DONTWAIT;
513 len = kernel_recvmsg(sock, &msg, iov, nr, buflen, MSG_DONTWAIT);
515 /* sock_recvmsg doesn't fill in the name/namelen, so we must..
516 * possibly we should cache this in the svc_sock structure
517 * at accept time. FIXME
519 alen = sizeof(rqstp->rq_addr);
520 kernel_getpeername(sock, (struct sockaddr *)&rqstp->rq_addr, &alen);
522 dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
523 rqstp->rq_sock, iov[0].iov_base, iov[0].iov_len, len);
529 * Set socket snd and rcv buffer lengths
532 svc_sock_setbufsize(struct socket *sock, unsigned int snd, unsigned int rcv)
536 oldfs = get_fs(); set_fs(KERNEL_DS);
537 sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
538 (char*)&snd, sizeof(snd));
539 sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
540 (char*)&rcv, sizeof(rcv));
542 /* sock_setsockopt limits use to sysctl_?mem_max,
543 * which isn't acceptable. Until that is made conditional
544 * on not having CAP_SYS_RESOURCE or similar, we go direct...
545 * DaveM said I could!
548 sock->sk->sk_sndbuf = snd * 2;
549 sock->sk->sk_rcvbuf = rcv * 2;
550 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK|SOCK_RCVBUF_LOCK;
551 release_sock(sock->sk);
555 * INET callback when data has been received on the socket.
558 svc_udp_data_ready(struct sock *sk, int count)
560 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
563 dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
564 svsk, sk, count, test_bit(SK_BUSY, &svsk->sk_flags));
565 set_bit(SK_DATA, &svsk->sk_flags);
566 svc_sock_enqueue(svsk);
568 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
569 wake_up_interruptible(sk->sk_sleep);
573 * INET callback when space is newly available on the socket.
576 svc_write_space(struct sock *sk)
578 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
581 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
582 svsk, sk, test_bit(SK_BUSY, &svsk->sk_flags));
583 svc_sock_enqueue(svsk);
586 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) {
587 dprintk("RPC svc_write_space: someone sleeping on %p\n",
589 wake_up_interruptible(sk->sk_sleep);
594 * Receive a datagram from a UDP socket.
597 svc_udp_recvfrom(struct svc_rqst *rqstp)
599 struct svc_sock *svsk = rqstp->rq_sock;
600 struct svc_serv *serv = svsk->sk_server;
604 if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
605 /* udp sockets need large rcvbuf as all pending
606 * requests are still in that buffer. sndbuf must
607 * also be large enough that there is enough space
608 * for one reply per thread.
610 svc_sock_setbufsize(svsk->sk_sock,
611 (serv->sv_nrthreads+3) * serv->sv_bufsz,
612 (serv->sv_nrthreads+3) * serv->sv_bufsz);
614 if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
615 svc_sock_received(svsk);
616 return svc_deferred_recv(rqstp);
619 clear_bit(SK_DATA, &svsk->sk_flags);
620 while ((skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err)) == NULL) {
621 if (err == -EAGAIN) {
622 svc_sock_received(svsk);
625 /* possibly an icmp error */
626 dprintk("svc: recvfrom returned error %d\n", -err);
628 if (skb->tstamp.off_sec == 0) {
631 tv.tv_sec = xtime.tv_sec;
632 tv.tv_usec = xtime.tv_nsec / NSEC_PER_USEC;
633 skb_set_timestamp(skb, &tv);
634 /* Don't enable netstamp, sunrpc doesn't
635 need that much accuracy */
637 skb_get_timestamp(skb, &svsk->sk_sk->sk_stamp);
638 set_bit(SK_DATA, &svsk->sk_flags); /* there may be more data... */
641 * Maybe more packets - kick another thread ASAP.
643 svc_sock_received(svsk);
645 len = skb->len - sizeof(struct udphdr);
646 rqstp->rq_arg.len = len;
648 rqstp->rq_prot = IPPROTO_UDP;
650 /* Get sender address */
651 rqstp->rq_addr.sin_family = AF_INET;
652 rqstp->rq_addr.sin_port = skb->h.uh->source;
653 rqstp->rq_addr.sin_addr.s_addr = skb->nh.iph->saddr;
654 rqstp->rq_daddr = skb->nh.iph->daddr;
656 if (skb_is_nonlinear(skb)) {
657 /* we have to copy */
659 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
662 skb_free_datagram(svsk->sk_sk, skb);
666 skb_free_datagram(svsk->sk_sk, skb);
668 /* we can use it in-place */
669 rqstp->rq_arg.head[0].iov_base = skb->data + sizeof(struct udphdr);
670 rqstp->rq_arg.head[0].iov_len = len;
671 if (skb_checksum_complete(skb)) {
672 skb_free_datagram(svsk->sk_sk, skb);
675 rqstp->rq_skbuff = skb;
678 rqstp->rq_arg.page_base = 0;
679 if (len <= rqstp->rq_arg.head[0].iov_len) {
680 rqstp->rq_arg.head[0].iov_len = len;
681 rqstp->rq_arg.page_len = 0;
683 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
684 rqstp->rq_argused += (rqstp->rq_arg.page_len + PAGE_SIZE - 1)/ PAGE_SIZE;
688 serv->sv_stats->netudpcnt++;
694 svc_udp_sendto(struct svc_rqst *rqstp)
698 error = svc_sendto(rqstp, &rqstp->rq_res);
699 if (error == -ECONNREFUSED)
700 /* ICMP error on earlier request. */
701 error = svc_sendto(rqstp, &rqstp->rq_res);
707 svc_udp_init(struct svc_sock *svsk)
709 svsk->sk_sk->sk_data_ready = svc_udp_data_ready;
710 svsk->sk_sk->sk_write_space = svc_write_space;
711 svsk->sk_recvfrom = svc_udp_recvfrom;
712 svsk->sk_sendto = svc_udp_sendto;
714 /* initialise setting must have enough space to
715 * receive and respond to one request.
716 * svc_udp_recvfrom will re-adjust if necessary
718 svc_sock_setbufsize(svsk->sk_sock,
719 3 * svsk->sk_server->sv_bufsz,
720 3 * svsk->sk_server->sv_bufsz);
722 set_bit(SK_DATA, &svsk->sk_flags); /* might have come in before data_ready set up */
723 set_bit(SK_CHNGBUF, &svsk->sk_flags);
727 * A data_ready event on a listening socket means there's a connection
728 * pending. Do not use state_change as a substitute for it.
731 svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
733 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
735 dprintk("svc: socket %p TCP (listen) state change %d\n",
739 * This callback may called twice when a new connection
740 * is established as a child socket inherits everything
741 * from a parent LISTEN socket.
742 * 1) data_ready method of the parent socket will be called
743 * when one of child sockets become ESTABLISHED.
744 * 2) data_ready method of the child socket may be called
745 * when it receives data before the socket is accepted.
746 * In case of 2, we should ignore it silently.
748 if (sk->sk_state == TCP_LISTEN) {
750 set_bit(SK_CONN, &svsk->sk_flags);
751 svc_sock_enqueue(svsk);
753 printk("svc: socket %p: no user data\n", sk);
756 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
757 wake_up_interruptible_all(sk->sk_sleep);
761 * A state change on a connected socket means it's dying or dead.
764 svc_tcp_state_change(struct sock *sk)
766 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
768 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
769 sk, sk->sk_state, sk->sk_user_data);
772 printk("svc: socket %p: no user data\n", sk);
774 set_bit(SK_CLOSE, &svsk->sk_flags);
775 svc_sock_enqueue(svsk);
777 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
778 wake_up_interruptible_all(sk->sk_sleep);
782 svc_tcp_data_ready(struct sock *sk, int count)
784 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
786 dprintk("svc: socket %p TCP data ready (svsk %p)\n",
787 sk, sk->sk_user_data);
789 set_bit(SK_DATA, &svsk->sk_flags);
790 svc_sock_enqueue(svsk);
792 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
793 wake_up_interruptible(sk->sk_sleep);
797 * Accept a TCP connection
800 svc_tcp_accept(struct svc_sock *svsk)
802 struct sockaddr_in sin;
803 struct svc_serv *serv = svsk->sk_server;
804 struct socket *sock = svsk->sk_sock;
805 struct socket *newsock;
806 struct svc_sock *newsvsk;
809 dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
813 clear_bit(SK_CONN, &svsk->sk_flags);
814 err = kernel_accept(sock, &newsock, O_NONBLOCK);
817 printk(KERN_WARNING "%s: no more sockets!\n",
819 else if (err != -EAGAIN && net_ratelimit())
820 printk(KERN_WARNING "%s: accept failed (err %d)!\n",
821 serv->sv_name, -err);
825 set_bit(SK_CONN, &svsk->sk_flags);
826 svc_sock_enqueue(svsk);
829 err = kernel_getpeername(newsock, (struct sockaddr *) &sin, &slen);
832 printk(KERN_WARNING "%s: peername failed (err %d)!\n",
833 serv->sv_name, -err);
834 goto failed; /* aborted connection or whatever */
837 /* Ideally, we would want to reject connections from unauthorized
838 * hosts here, but when we get encription, the IP of the host won't
839 * tell us anything. For now just warn about unpriv connections.
841 if (ntohs(sin.sin_port) >= 1024) {
843 "%s: connect from unprivileged port: %u.%u.%u.%u:%d\n",
845 NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
848 dprintk("%s: connect from %u.%u.%u.%u:%04x\n", serv->sv_name,
849 NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
851 /* make sure that a write doesn't block forever when
854 newsock->sk->sk_sndtimeo = HZ*30;
856 if (!(newsvsk = svc_setup_socket(serv, newsock, &err, 0)))
860 /* make sure that we don't have too many active connections.
861 * If we have, something must be dropped.
863 * There's no point in trying to do random drop here for
864 * DoS prevention. The NFS clients does 1 reconnect in 15
865 * seconds. An attacker can easily beat that.
867 * The only somewhat efficient mechanism would be if drop
868 * old connections from the same IP first. But right now
869 * we don't even record the client IP in svc_sock.
871 if (serv->sv_tmpcnt > (serv->sv_nrthreads+3)*20) {
872 struct svc_sock *svsk = NULL;
873 spin_lock_bh(&serv->sv_lock);
874 if (!list_empty(&serv->sv_tempsocks)) {
875 if (net_ratelimit()) {
876 /* Try to help the admin */
877 printk(KERN_NOTICE "%s: too many open TCP "
878 "sockets, consider increasing the "
879 "number of nfsd threads\n",
881 printk(KERN_NOTICE "%s: last TCP connect from "
884 NIPQUAD(sin.sin_addr.s_addr),
885 ntohs(sin.sin_port));
888 * Always select the oldest socket. It's not fair,
891 svsk = list_entry(serv->sv_tempsocks.prev,
894 set_bit(SK_CLOSE, &svsk->sk_flags);
895 atomic_inc(&svsk->sk_inuse);
897 spin_unlock_bh(&serv->sv_lock);
900 svc_sock_enqueue(svsk);
907 serv->sv_stats->nettcpconn++;
912 sock_release(newsock);
917 * Receive data from a TCP socket.
920 svc_tcp_recvfrom(struct svc_rqst *rqstp)
922 struct svc_sock *svsk = rqstp->rq_sock;
923 struct svc_serv *serv = svsk->sk_server;
925 struct kvec vec[RPCSVC_MAXPAGES];
928 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
929 svsk, test_bit(SK_DATA, &svsk->sk_flags),
930 test_bit(SK_CONN, &svsk->sk_flags),
931 test_bit(SK_CLOSE, &svsk->sk_flags));
933 if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
934 svc_sock_received(svsk);
935 return svc_deferred_recv(rqstp);
938 if (test_bit(SK_CLOSE, &svsk->sk_flags)) {
939 svc_delete_socket(svsk);
943 if (test_bit(SK_CONN, &svsk->sk_flags)) {
944 svc_tcp_accept(svsk);
945 svc_sock_received(svsk);
949 if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
950 /* sndbuf needs to have room for one request
951 * per thread, otherwise we can stall even when the
952 * network isn't a bottleneck.
953 * rcvbuf just needs to be able to hold a few requests.
954 * Normally they will be removed from the queue
955 * as soon a a complete request arrives.
957 svc_sock_setbufsize(svsk->sk_sock,
958 (serv->sv_nrthreads+3) * serv->sv_bufsz,
961 clear_bit(SK_DATA, &svsk->sk_flags);
963 /* Receive data. If we haven't got the record length yet, get
964 * the next four bytes. Otherwise try to gobble up as much as
965 * possible up to the complete record length.
967 if (svsk->sk_tcplen < 4) {
968 unsigned long want = 4 - svsk->sk_tcplen;
971 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
973 if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
975 svsk->sk_tcplen += len;
978 dprintk("svc: short recvfrom while reading record length (%d of %lu)\n",
980 svc_sock_received(svsk);
981 return -EAGAIN; /* record header not complete */
984 svsk->sk_reclen = ntohl(svsk->sk_reclen);
985 if (!(svsk->sk_reclen & 0x80000000)) {
986 /* FIXME: technically, a record can be fragmented,
987 * and non-terminal fragments will not have the top
988 * bit set in the fragment length header.
989 * But apparently no known nfs clients send fragmented
991 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx (non-terminal)\n",
992 (unsigned long) svsk->sk_reclen);
995 svsk->sk_reclen &= 0x7fffffff;
996 dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
997 if (svsk->sk_reclen > serv->sv_bufsz) {
998 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx (large)\n",
999 (unsigned long) svsk->sk_reclen);
1004 /* Check whether enough data is available */
1005 len = svc_recv_available(svsk);
1009 if (len < svsk->sk_reclen) {
1010 dprintk("svc: incomplete TCP record (%d of %d)\n",
1011 len, svsk->sk_reclen);
1012 svc_sock_received(svsk);
1013 return -EAGAIN; /* record not complete */
1015 len = svsk->sk_reclen;
1016 set_bit(SK_DATA, &svsk->sk_flags);
1018 vec[0] = rqstp->rq_arg.head[0];
1021 while (vlen < len) {
1022 vec[pnum].iov_base = page_address(rqstp->rq_argpages[rqstp->rq_argused++]);
1023 vec[pnum].iov_len = PAGE_SIZE;
1028 /* Now receive data */
1029 len = svc_recvfrom(rqstp, vec, pnum, len);
1033 dprintk("svc: TCP complete record (%d bytes)\n", len);
1034 rqstp->rq_arg.len = len;
1035 rqstp->rq_arg.page_base = 0;
1036 if (len <= rqstp->rq_arg.head[0].iov_len) {
1037 rqstp->rq_arg.head[0].iov_len = len;
1038 rqstp->rq_arg.page_len = 0;
1040 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
1043 rqstp->rq_skbuff = NULL;
1044 rqstp->rq_prot = IPPROTO_TCP;
1046 /* Reset TCP read info */
1047 svsk->sk_reclen = 0;
1048 svsk->sk_tcplen = 0;
1050 svc_sock_received(svsk);
1052 serv->sv_stats->nettcpcnt++;
1057 svc_delete_socket(svsk);
1061 if (len == -EAGAIN) {
1062 dprintk("RPC: TCP recvfrom got EAGAIN\n");
1063 svc_sock_received(svsk);
1065 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
1066 svsk->sk_server->sv_name, -len);
1074 * Send out data on TCP socket.
1077 svc_tcp_sendto(struct svc_rqst *rqstp)
1079 struct xdr_buf *xbufp = &rqstp->rq_res;
1083 /* Set up the first element of the reply kvec.
1084 * Any other kvecs that may be in use have been taken
1085 * care of by the server implementation itself.
1087 reclen = htonl(0x80000000|((xbufp->len ) - 4));
1088 memcpy(xbufp->head[0].iov_base, &reclen, 4);
1090 if (test_bit(SK_DEAD, &rqstp->rq_sock->sk_flags))
1093 sent = svc_sendto(rqstp, &rqstp->rq_res);
1094 if (sent != xbufp->len) {
1095 printk(KERN_NOTICE "rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n",
1096 rqstp->rq_sock->sk_server->sv_name,
1097 (sent<0)?"got error":"sent only",
1099 svc_delete_socket(rqstp->rq_sock);
1106 svc_tcp_init(struct svc_sock *svsk)
1108 struct sock *sk = svsk->sk_sk;
1109 struct tcp_sock *tp = tcp_sk(sk);
1111 svsk->sk_recvfrom = svc_tcp_recvfrom;
1112 svsk->sk_sendto = svc_tcp_sendto;
1114 if (sk->sk_state == TCP_LISTEN) {
1115 dprintk("setting up TCP socket for listening\n");
1116 sk->sk_data_ready = svc_tcp_listen_data_ready;
1117 set_bit(SK_CONN, &svsk->sk_flags);
1119 dprintk("setting up TCP socket for reading\n");
1120 sk->sk_state_change = svc_tcp_state_change;
1121 sk->sk_data_ready = svc_tcp_data_ready;
1122 sk->sk_write_space = svc_write_space;
1124 svsk->sk_reclen = 0;
1125 svsk->sk_tcplen = 0;
1127 tp->nonagle = 1; /* disable Nagle's algorithm */
1129 /* initialise setting must have enough space to
1130 * receive and respond to one request.
1131 * svc_tcp_recvfrom will re-adjust if necessary
1133 svc_sock_setbufsize(svsk->sk_sock,
1134 3 * svsk->sk_server->sv_bufsz,
1135 3 * svsk->sk_server->sv_bufsz);
1137 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1138 set_bit(SK_DATA, &svsk->sk_flags);
1139 if (sk->sk_state != TCP_ESTABLISHED)
1140 set_bit(SK_CLOSE, &svsk->sk_flags);
1145 svc_sock_update_bufs(struct svc_serv *serv)
1148 * The number of server threads has changed. Update
1149 * rcvbuf and sndbuf accordingly on all sockets
1151 struct list_head *le;
1153 spin_lock_bh(&serv->sv_lock);
1154 list_for_each(le, &serv->sv_permsocks) {
1155 struct svc_sock *svsk =
1156 list_entry(le, struct svc_sock, sk_list);
1157 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1159 list_for_each(le, &serv->sv_tempsocks) {
1160 struct svc_sock *svsk =
1161 list_entry(le, struct svc_sock, sk_list);
1162 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1164 spin_unlock_bh(&serv->sv_lock);
1168 * Receive the next request on any socket.
1171 svc_recv(struct svc_rqst *rqstp, long timeout)
1173 struct svc_sock *svsk =NULL;
1174 struct svc_serv *serv = rqstp->rq_server;
1177 struct xdr_buf *arg;
1178 DECLARE_WAITQUEUE(wait, current);
1180 dprintk("svc: server %p waiting for data (to = %ld)\n",
1185 "svc_recv: service %p, socket not NULL!\n",
1187 if (waitqueue_active(&rqstp->rq_wait))
1189 "svc_recv: service %p, wait queue active!\n",
1192 /* Initialize the buffers */
1193 /* first reclaim pages that were moved to response list */
1194 svc_pushback_allpages(rqstp);
1196 /* now allocate needed pages. If we get a failure, sleep briefly */
1197 pages = 2 + (serv->sv_bufsz + PAGE_SIZE -1) / PAGE_SIZE;
1198 while (rqstp->rq_arghi < pages) {
1199 struct page *p = alloc_page(GFP_KERNEL);
1201 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
1204 rqstp->rq_argpages[rqstp->rq_arghi++] = p;
1207 /* Make arg->head point to first page and arg->pages point to rest */
1208 arg = &rqstp->rq_arg;
1209 arg->head[0].iov_base = page_address(rqstp->rq_argpages[0]);
1210 arg->head[0].iov_len = PAGE_SIZE;
1211 rqstp->rq_argused = 1;
1212 arg->pages = rqstp->rq_argpages + 1;
1214 /* save at least one page for response */
1215 arg->page_len = (pages-2)*PAGE_SIZE;
1216 arg->len = (pages-1)*PAGE_SIZE;
1217 arg->tail[0].iov_len = 0;
1224 spin_lock_bh(&serv->sv_lock);
1225 if ((svsk = svc_sock_dequeue(serv)) != NULL) {
1226 rqstp->rq_sock = svsk;
1227 atomic_inc(&svsk->sk_inuse);
1228 rqstp->rq_reserved = serv->sv_bufsz;
1229 svsk->sk_reserved += rqstp->rq_reserved;
1231 /* No data pending. Go to sleep */
1232 svc_serv_enqueue(serv, rqstp);
1235 * We have to be able to interrupt this wait
1236 * to bring down the daemons ...
1238 set_current_state(TASK_INTERRUPTIBLE);
1239 add_wait_queue(&rqstp->rq_wait, &wait);
1240 spin_unlock_bh(&serv->sv_lock);
1242 schedule_timeout(timeout);
1246 spin_lock_bh(&serv->sv_lock);
1247 remove_wait_queue(&rqstp->rq_wait, &wait);
1249 if (!(svsk = rqstp->rq_sock)) {
1250 svc_serv_dequeue(serv, rqstp);
1251 spin_unlock_bh(&serv->sv_lock);
1252 dprintk("svc: server %p, no data yet\n", rqstp);
1253 return signalled()? -EINTR : -EAGAIN;
1256 spin_unlock_bh(&serv->sv_lock);
1258 dprintk("svc: server %p, socket %p, inuse=%d\n",
1259 rqstp, svsk, atomic_read(&svsk->sk_inuse));
1260 len = svsk->sk_recvfrom(rqstp);
1261 dprintk("svc: got len=%d\n", len);
1263 /* No data, incomplete (TCP) read, or accept() */
1264 if (len == 0 || len == -EAGAIN) {
1265 rqstp->rq_res.len = 0;
1266 svc_sock_release(rqstp);
1269 svsk->sk_lastrecv = get_seconds();
1270 clear_bit(SK_OLD, &svsk->sk_flags);
1272 rqstp->rq_secure = ntohs(rqstp->rq_addr.sin_port) < 1024;
1273 rqstp->rq_chandle.defer = svc_defer;
1276 serv->sv_stats->netcnt++;
1284 svc_drop(struct svc_rqst *rqstp)
1286 dprintk("svc: socket %p dropped request\n", rqstp->rq_sock);
1287 svc_sock_release(rqstp);
1291 * Return reply to client.
1294 svc_send(struct svc_rqst *rqstp)
1296 struct svc_sock *svsk;
1300 if ((svsk = rqstp->rq_sock) == NULL) {
1301 printk(KERN_WARNING "NULL socket pointer in %s:%d\n",
1302 __FILE__, __LINE__);
1306 /* release the receive skb before sending the reply */
1307 svc_release_skb(rqstp);
1309 /* calculate over-all length */
1310 xb = & rqstp->rq_res;
1311 xb->len = xb->head[0].iov_len +
1313 xb->tail[0].iov_len;
1315 /* Grab svsk->sk_mutex to serialize outgoing data. */
1316 mutex_lock(&svsk->sk_mutex);
1317 if (test_bit(SK_DEAD, &svsk->sk_flags))
1320 len = svsk->sk_sendto(rqstp);
1321 mutex_unlock(&svsk->sk_mutex);
1322 svc_sock_release(rqstp);
1324 if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
1330 * Timer function to close old temporary sockets, using
1331 * a mark-and-sweep algorithm.
1334 svc_age_temp_sockets(unsigned long closure)
1336 struct svc_serv *serv = (struct svc_serv *)closure;
1337 struct svc_sock *svsk;
1338 struct list_head *le, *next;
1339 LIST_HEAD(to_be_aged);
1341 dprintk("svc_age_temp_sockets\n");
1343 if (!spin_trylock_bh(&serv->sv_lock)) {
1344 /* busy, try again 1 sec later */
1345 dprintk("svc_age_temp_sockets: busy\n");
1346 mod_timer(&serv->sv_temptimer, jiffies + HZ);
1350 list_for_each_safe(le, next, &serv->sv_tempsocks) {
1351 svsk = list_entry(le, struct svc_sock, sk_list);
1353 if (!test_and_set_bit(SK_OLD, &svsk->sk_flags))
1355 if (atomic_read(&svsk->sk_inuse) || test_bit(SK_BUSY, &svsk->sk_flags))
1357 atomic_inc(&svsk->sk_inuse);
1358 list_move(le, &to_be_aged);
1359 set_bit(SK_CLOSE, &svsk->sk_flags);
1360 set_bit(SK_DETACHED, &svsk->sk_flags);
1362 spin_unlock_bh(&serv->sv_lock);
1364 while (!list_empty(&to_be_aged)) {
1365 le = to_be_aged.next;
1366 /* fiddling the sk_list node is safe 'cos we're SK_DETACHED */
1368 svsk = list_entry(le, struct svc_sock, sk_list);
1370 dprintk("queuing svsk %p for closing, %lu seconds old\n",
1371 svsk, get_seconds() - svsk->sk_lastrecv);
1373 /* a thread will dequeue and close it soon */
1374 svc_sock_enqueue(svsk);
1378 mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
1382 * Initialize socket for RPC use and create svc_sock struct
1383 * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
1385 static struct svc_sock *
1386 svc_setup_socket(struct svc_serv *serv, struct socket *sock,
1387 int *errp, int pmap_register)
1389 struct svc_sock *svsk;
1392 dprintk("svc: svc_setup_socket %p\n", sock);
1393 if (!(svsk = kzalloc(sizeof(*svsk), GFP_KERNEL))) {
1400 /* Register socket with portmapper */
1401 if (*errp >= 0 && pmap_register)
1402 *errp = svc_register(serv, inet->sk_protocol,
1403 ntohs(inet_sk(inet)->sport));
1410 set_bit(SK_BUSY, &svsk->sk_flags);
1411 inet->sk_user_data = svsk;
1412 svsk->sk_sock = sock;
1414 svsk->sk_ostate = inet->sk_state_change;
1415 svsk->sk_odata = inet->sk_data_ready;
1416 svsk->sk_owspace = inet->sk_write_space;
1417 svsk->sk_server = serv;
1418 atomic_set(&svsk->sk_inuse, 0);
1419 svsk->sk_lastrecv = get_seconds();
1420 spin_lock_init(&svsk->sk_defer_lock);
1421 INIT_LIST_HEAD(&svsk->sk_deferred);
1422 INIT_LIST_HEAD(&svsk->sk_ready);
1423 mutex_init(&svsk->sk_mutex);
1425 /* Initialize the socket */
1426 if (sock->type == SOCK_DGRAM)
1431 spin_lock_bh(&serv->sv_lock);
1432 if (!pmap_register) {
1433 set_bit(SK_TEMP, &svsk->sk_flags);
1434 list_add(&svsk->sk_list, &serv->sv_tempsocks);
1436 if (serv->sv_temptimer.function == NULL) {
1437 /* setup timer to age temp sockets */
1438 setup_timer(&serv->sv_temptimer, svc_age_temp_sockets,
1439 (unsigned long)serv);
1440 mod_timer(&serv->sv_temptimer,
1441 jiffies + svc_conn_age_period * HZ);
1444 clear_bit(SK_TEMP, &svsk->sk_flags);
1445 list_add(&svsk->sk_list, &serv->sv_permsocks);
1447 spin_unlock_bh(&serv->sv_lock);
1449 dprintk("svc: svc_setup_socket created %p (inet %p)\n",
1452 clear_bit(SK_BUSY, &svsk->sk_flags);
1453 svc_sock_enqueue(svsk);
1457 int svc_addsock(struct svc_serv *serv,
1463 struct socket *so = sockfd_lookup(fd, &err);
1464 struct svc_sock *svsk = NULL;
1468 if (so->sk->sk_family != AF_INET)
1469 err = -EAFNOSUPPORT;
1470 else if (so->sk->sk_protocol != IPPROTO_TCP &&
1471 so->sk->sk_protocol != IPPROTO_UDP)
1472 err = -EPROTONOSUPPORT;
1473 else if (so->state > SS_UNCONNECTED)
1476 svsk = svc_setup_socket(serv, so, &err, 1);
1484 if (proto) *proto = so->sk->sk_protocol;
1485 return one_sock_name(name_return, svsk);
1487 EXPORT_SYMBOL_GPL(svc_addsock);
1490 * Create socket for RPC service.
1493 svc_create_socket(struct svc_serv *serv, int protocol, struct sockaddr_in *sin)
1495 struct svc_sock *svsk;
1496 struct socket *sock;
1500 dprintk("svc: svc_create_socket(%s, %d, %u.%u.%u.%u:%d)\n",
1501 serv->sv_program->pg_name, protocol,
1502 NIPQUAD(sin->sin_addr.s_addr),
1503 ntohs(sin->sin_port));
1505 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1506 printk(KERN_WARNING "svc: only UDP and TCP "
1507 "sockets supported\n");
1510 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1512 if ((error = sock_create_kern(PF_INET, type, protocol, &sock)) < 0)
1515 if (type == SOCK_STREAM)
1516 sock->sk->sk_reuse = 1; /* allow address reuse */
1517 error = kernel_bind(sock, (struct sockaddr *) sin,
1522 if (protocol == IPPROTO_TCP) {
1523 if ((error = kernel_listen(sock, 64)) < 0)
1527 if ((svsk = svc_setup_socket(serv, sock, &error, 1)) != NULL)
1531 dprintk("svc: svc_create_socket error = %d\n", -error);
1537 * Remove a dead socket
1540 svc_delete_socket(struct svc_sock *svsk)
1542 struct svc_serv *serv;
1545 dprintk("svc: svc_delete_socket(%p)\n", svsk);
1547 serv = svsk->sk_server;
1550 sk->sk_state_change = svsk->sk_ostate;
1551 sk->sk_data_ready = svsk->sk_odata;
1552 sk->sk_write_space = svsk->sk_owspace;
1554 spin_lock_bh(&serv->sv_lock);
1556 if (!test_and_set_bit(SK_DETACHED, &svsk->sk_flags))
1557 list_del_init(&svsk->sk_list);
1558 list_del_init(&svsk->sk_ready);
1559 if (!test_and_set_bit(SK_DEAD, &svsk->sk_flags))
1560 if (test_bit(SK_TEMP, &svsk->sk_flags))
1563 if (!atomic_read(&svsk->sk_inuse)) {
1564 spin_unlock_bh(&serv->sv_lock);
1565 if (svsk->sk_sock->file)
1566 sockfd_put(svsk->sk_sock);
1568 sock_release(svsk->sk_sock);
1571 spin_unlock_bh(&serv->sv_lock);
1572 dprintk(KERN_NOTICE "svc: server socket destroy delayed\n");
1573 /* svsk->sk_server = NULL; */
1578 * Make a socket for nfsd and lockd
1581 svc_makesock(struct svc_serv *serv, int protocol, unsigned short port)
1583 struct sockaddr_in sin;
1585 dprintk("svc: creating socket proto = %d\n", protocol);
1586 sin.sin_family = AF_INET;
1587 sin.sin_addr.s_addr = INADDR_ANY;
1588 sin.sin_port = htons(port);
1589 return svc_create_socket(serv, protocol, &sin);
1593 * Handle defer and revisit of requests
1596 static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
1598 struct svc_deferred_req *dr = container_of(dreq, struct svc_deferred_req, handle);
1599 struct svc_sock *svsk;
1602 svc_sock_put(dr->svsk);
1606 dprintk("revisit queued\n");
1609 spin_lock_bh(&svsk->sk_defer_lock);
1610 list_add(&dr->handle.recent, &svsk->sk_deferred);
1611 spin_unlock_bh(&svsk->sk_defer_lock);
1612 set_bit(SK_DEFERRED, &svsk->sk_flags);
1613 svc_sock_enqueue(svsk);
1617 static struct cache_deferred_req *
1618 svc_defer(struct cache_req *req)
1620 struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
1621 int size = sizeof(struct svc_deferred_req) + (rqstp->rq_arg.len);
1622 struct svc_deferred_req *dr;
1624 if (rqstp->rq_arg.page_len)
1625 return NULL; /* if more than a page, give up FIXME */
1626 if (rqstp->rq_deferred) {
1627 dr = rqstp->rq_deferred;
1628 rqstp->rq_deferred = NULL;
1630 int skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
1631 /* FIXME maybe discard if size too large */
1632 dr = kmalloc(size, GFP_KERNEL);
1636 dr->handle.owner = rqstp->rq_server;
1637 dr->prot = rqstp->rq_prot;
1638 dr->addr = rqstp->rq_addr;
1639 dr->daddr = rqstp->rq_daddr;
1640 dr->argslen = rqstp->rq_arg.len >> 2;
1641 memcpy(dr->args, rqstp->rq_arg.head[0].iov_base-skip, dr->argslen<<2);
1643 atomic_inc(&rqstp->rq_sock->sk_inuse);
1644 dr->svsk = rqstp->rq_sock;
1646 dr->handle.revisit = svc_revisit;
1651 * recv data from a deferred request into an active one
1653 static int svc_deferred_recv(struct svc_rqst *rqstp)
1655 struct svc_deferred_req *dr = rqstp->rq_deferred;
1657 rqstp->rq_arg.head[0].iov_base = dr->args;
1658 rqstp->rq_arg.head[0].iov_len = dr->argslen<<2;
1659 rqstp->rq_arg.page_len = 0;
1660 rqstp->rq_arg.len = dr->argslen<<2;
1661 rqstp->rq_prot = dr->prot;
1662 rqstp->rq_addr = dr->addr;
1663 rqstp->rq_daddr = dr->daddr;
1664 return dr->argslen<<2;
1668 static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk)
1670 struct svc_deferred_req *dr = NULL;
1672 if (!test_bit(SK_DEFERRED, &svsk->sk_flags))
1674 spin_lock_bh(&svsk->sk_defer_lock);
1675 clear_bit(SK_DEFERRED, &svsk->sk_flags);
1676 if (!list_empty(&svsk->sk_deferred)) {
1677 dr = list_entry(svsk->sk_deferred.next,
1678 struct svc_deferred_req,
1680 list_del_init(&dr->handle.recent);
1681 set_bit(SK_DEFERRED, &svsk->sk_flags);
1683 spin_unlock_bh(&svsk->sk_defer_lock);