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_xprt_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/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/errno.h>
25 #include <linux/fcntl.h>
26 #include <linux/net.h>
28 #include <linux/inet.h>
29 #include <linux/udp.h>
30 #include <linux/tcp.h>
31 #include <linux/unistd.h>
32 #include <linux/slab.h>
33 #include <linux/netdevice.h>
34 #include <linux/skbuff.h>
35 #include <linux/file.h>
36 #include <linux/freezer.h>
38 #include <net/checksum.h>
41 #include <net/tcp_states.h>
42 #include <asm/uaccess.h>
43 #include <asm/ioctls.h>
45 #include <linux/sunrpc/types.h>
46 #include <linux/sunrpc/clnt.h>
47 #include <linux/sunrpc/xdr.h>
48 #include <linux/sunrpc/svcsock.h>
49 #include <linux/sunrpc/stats.h>
51 /* SMP locking strategy:
53 * svc_pool->sp_lock protects most of the fields of that pool.
54 * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt.
55 * when both need to be taken (rare), svc_serv->sv_lock is first.
56 * BKL protects svc_serv->sv_nrthread.
57 * svc_sock->sk_lock protects the svc_sock->sk_deferred list
58 * and the ->sk_info_authunix cache.
59 * svc_sock->sk_xprt.xpt_flags.XPT_BUSY prevents a svc_sock being
62 * Some flags can be set to certain values at any time
63 * providing that certain rules are followed:
65 * XPT_CONN, XPT_DATA, can be set or cleared at any time.
66 * after a set, svc_xprt_enqueue must be called.
67 * after a clear, the socket must be read/accepted
68 * if this succeeds, it must be set again.
69 * XPT_CLOSE can set at any time. It is never cleared.
70 * xpt_ref contains a bias of '1' until XPT_DEAD is set.
71 * so when xprt_ref hits zero, we know the transport is dead
72 * and no-one is using it.
73 * XPT_DEAD can only be set while XPT_BUSY is held which ensures
74 * no other thread will be using the socket or will try to
79 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
82 static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
83 int *errp, int flags);
84 static void svc_delete_xprt(struct svc_xprt *xprt);
85 static void svc_udp_data_ready(struct sock *, int);
86 static int svc_udp_recvfrom(struct svc_rqst *);
87 static int svc_udp_sendto(struct svc_rqst *);
88 static void svc_close_xprt(struct svc_xprt *xprt);
89 static void svc_sock_detach(struct svc_xprt *);
90 static void svc_sock_free(struct svc_xprt *);
92 static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk);
93 static int svc_deferred_recv(struct svc_rqst *rqstp);
94 static struct cache_deferred_req *svc_defer(struct cache_req *req);
95 static struct svc_xprt *svc_create_socket(struct svc_serv *, int,
96 struct sockaddr *, int, int);
98 /* apparently the "standard" is that clients close
99 * idle connections after 5 minutes, servers after
101 * http://www.connectathon.org/talks96/nfstcp.pdf
103 static int svc_conn_age_period = 6*60;
105 #ifdef CONFIG_DEBUG_LOCK_ALLOC
106 static struct lock_class_key svc_key[2];
107 static struct lock_class_key svc_slock_key[2];
109 static inline void svc_reclassify_socket(struct socket *sock)
111 struct sock *sk = sock->sk;
112 BUG_ON(sock_owned_by_user(sk));
113 switch (sk->sk_family) {
115 sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
116 &svc_slock_key[0], "sk_lock-AF_INET-NFSD", &svc_key[0]);
120 sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
121 &svc_slock_key[1], "sk_lock-AF_INET6-NFSD", &svc_key[1]);
129 static inline void svc_reclassify_socket(struct socket *sock)
134 static char *__svc_print_addr(struct sockaddr *addr, char *buf, size_t len)
136 switch (addr->sa_family) {
138 snprintf(buf, len, "%u.%u.%u.%u, port=%u",
139 NIPQUAD(((struct sockaddr_in *) addr)->sin_addr),
140 ntohs(((struct sockaddr_in *) addr)->sin_port));
144 snprintf(buf, len, "%x:%x:%x:%x:%x:%x:%x:%x, port=%u",
145 NIP6(((struct sockaddr_in6 *) addr)->sin6_addr),
146 ntohs(((struct sockaddr_in6 *) addr)->sin6_port));
150 snprintf(buf, len, "unknown address type: %d", addr->sa_family);
157 * svc_print_addr - Format rq_addr field for printing
158 * @rqstp: svc_rqst struct containing address to print
159 * @buf: target buffer for formatted address
160 * @len: length of target buffer
163 char *svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len)
165 return __svc_print_addr(svc_addr(rqstp), buf, len);
167 EXPORT_SYMBOL_GPL(svc_print_addr);
170 * Queue up an idle server thread. Must have pool->sp_lock held.
171 * Note: this is really a stack rather than a queue, so that we only
172 * use as many different threads as we need, and the rest don't pollute
176 svc_thread_enqueue(struct svc_pool *pool, struct svc_rqst *rqstp)
178 list_add(&rqstp->rq_list, &pool->sp_threads);
182 * Dequeue an nfsd thread. Must have pool->sp_lock held.
185 svc_thread_dequeue(struct svc_pool *pool, struct svc_rqst *rqstp)
187 list_del(&rqstp->rq_list);
191 * Release an skbuff after use
193 static void svc_release_skb(struct svc_rqst *rqstp)
195 struct sk_buff *skb = rqstp->rq_xprt_ctxt;
196 struct svc_deferred_req *dr = rqstp->rq_deferred;
199 rqstp->rq_xprt_ctxt = NULL;
201 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
202 skb_free_datagram(rqstp->rq_sock->sk_sk, skb);
205 rqstp->rq_deferred = NULL;
211 * Queue up a socket with data pending. If there are idle nfsd
212 * processes, wake 'em up.
215 void svc_xprt_enqueue(struct svc_xprt *xprt)
217 struct svc_serv *serv = xprt->xpt_server;
218 struct svc_pool *pool;
219 struct svc_rqst *rqstp;
222 if (!(xprt->xpt_flags &
223 ((1<<XPT_CONN)|(1<<XPT_DATA)|(1<<XPT_CLOSE)|(1<<XPT_DEFERRED))))
225 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
229 pool = svc_pool_for_cpu(xprt->xpt_server, cpu);
232 spin_lock_bh(&pool->sp_lock);
234 if (!list_empty(&pool->sp_threads) &&
235 !list_empty(&pool->sp_sockets))
238 "threads and transports both waiting??\n");
240 if (test_bit(XPT_DEAD, &xprt->xpt_flags)) {
241 /* Don't enqueue dead sockets */
242 dprintk("svc: transport %p is dead, not enqueued\n", xprt);
246 /* Mark socket as busy. It will remain in this state until the
247 * server has processed all pending data and put the socket back
248 * on the idle list. We update XPT_BUSY atomically because
249 * it also guards against trying to enqueue the svc_sock twice.
251 if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags)) {
252 /* Don't enqueue socket while already enqueued */
253 dprintk("svc: transport %p busy, not enqueued\n", xprt);
256 BUG_ON(xprt->xpt_pool != NULL);
257 xprt->xpt_pool = pool;
259 /* Handle pending connection */
260 if (test_bit(XPT_CONN, &xprt->xpt_flags))
263 /* Handle close in-progress */
264 if (test_bit(XPT_CLOSE, &xprt->xpt_flags))
267 /* Check if we have space to reply to a request */
268 if (!xprt->xpt_ops->xpo_has_wspace(xprt)) {
269 /* Don't enqueue while not enough space for reply */
270 dprintk("svc: no write space, transport %p not enqueued\n",
272 xprt->xpt_pool = NULL;
273 clear_bit(XPT_BUSY, &xprt->xpt_flags);
278 if (!list_empty(&pool->sp_threads)) {
279 rqstp = list_entry(pool->sp_threads.next,
282 dprintk("svc: transport %p served by daemon %p\n",
284 svc_thread_dequeue(pool, rqstp);
287 "svc_xprt_enqueue: server %p, rq_xprt=%p!\n",
288 rqstp, rqstp->rq_xprt);
289 rqstp->rq_xprt = xprt;
291 rqstp->rq_reserved = serv->sv_max_mesg;
292 atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved);
293 BUG_ON(xprt->xpt_pool != pool);
294 wake_up(&rqstp->rq_wait);
296 dprintk("svc: transport %p put into queue\n", xprt);
297 list_add_tail(&xprt->xpt_ready, &pool->sp_sockets);
298 BUG_ON(xprt->xpt_pool != pool);
302 spin_unlock_bh(&pool->sp_lock);
304 EXPORT_SYMBOL_GPL(svc_xprt_enqueue);
307 * Dequeue the first socket. Must be called with the pool->sp_lock held.
309 static inline struct svc_sock *
310 svc_sock_dequeue(struct svc_pool *pool)
312 struct svc_sock *svsk;
314 if (list_empty(&pool->sp_sockets))
317 svsk = list_entry(pool->sp_sockets.next,
318 struct svc_sock, sk_xprt.xpt_ready);
319 list_del_init(&svsk->sk_xprt.xpt_ready);
321 dprintk("svc: socket %p dequeued, inuse=%d\n",
322 svsk->sk_sk, atomic_read(&svsk->sk_xprt.xpt_ref.refcount));
328 * Having read something from a socket, check whether it
329 * needs to be re-enqueued.
330 * Note: XPT_DATA only gets cleared when a read-attempt finds
331 * no (or insufficient) data.
334 svc_sock_received(struct svc_sock *svsk)
336 svsk->sk_xprt.xpt_pool = NULL;
337 clear_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags);
338 svc_xprt_enqueue(&svsk->sk_xprt);
343 * svc_reserve - change the space reserved for the reply to a request.
344 * @rqstp: The request in question
345 * @space: new max space to reserve
347 * Each request reserves some space on the output queue of the socket
348 * to make sure the reply fits. This function reduces that reserved
349 * space to be the amount of space used already, plus @space.
352 void svc_reserve(struct svc_rqst *rqstp, int space)
354 space += rqstp->rq_res.head[0].iov_len;
356 if (space < rqstp->rq_reserved) {
357 struct svc_xprt *xprt = rqstp->rq_xprt;
358 atomic_sub((rqstp->rq_reserved - space), &xprt->xpt_reserved);
359 rqstp->rq_reserved = space;
361 svc_xprt_enqueue(xprt);
366 svc_sock_release(struct svc_rqst *rqstp)
368 struct svc_sock *svsk = rqstp->rq_sock;
370 rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
372 svc_free_res_pages(rqstp);
373 rqstp->rq_res.page_len = 0;
374 rqstp->rq_res.page_base = 0;
377 /* Reset response buffer and release
379 * But first, check that enough space was reserved
380 * for the reply, otherwise we have a bug!
382 if ((rqstp->rq_res.len) > rqstp->rq_reserved)
383 printk(KERN_ERR "RPC request reserved %d but used %d\n",
387 rqstp->rq_res.head[0].iov_len = 0;
388 svc_reserve(rqstp, 0);
389 rqstp->rq_sock = NULL;
391 svc_xprt_put(&svsk->sk_xprt);
395 * External function to wake up a server waiting for data
396 * This really only makes sense for services like lockd
397 * which have exactly one thread anyway.
400 svc_wake_up(struct svc_serv *serv)
402 struct svc_rqst *rqstp;
404 struct svc_pool *pool;
406 for (i = 0; i < serv->sv_nrpools; i++) {
407 pool = &serv->sv_pools[i];
409 spin_lock_bh(&pool->sp_lock);
410 if (!list_empty(&pool->sp_threads)) {
411 rqstp = list_entry(pool->sp_threads.next,
414 dprintk("svc: daemon %p woken up.\n", rqstp);
416 svc_thread_dequeue(pool, rqstp);
417 rqstp->rq_sock = NULL;
419 wake_up(&rqstp->rq_wait);
421 spin_unlock_bh(&pool->sp_lock);
425 union svc_pktinfo_u {
426 struct in_pktinfo pkti;
427 struct in6_pktinfo pkti6;
429 #define SVC_PKTINFO_SPACE \
430 CMSG_SPACE(sizeof(union svc_pktinfo_u))
432 static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
434 switch (rqstp->rq_sock->sk_sk->sk_family) {
436 struct in_pktinfo *pki = CMSG_DATA(cmh);
438 cmh->cmsg_level = SOL_IP;
439 cmh->cmsg_type = IP_PKTINFO;
440 pki->ipi_ifindex = 0;
441 pki->ipi_spec_dst.s_addr = rqstp->rq_daddr.addr.s_addr;
442 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
447 struct in6_pktinfo *pki = CMSG_DATA(cmh);
449 cmh->cmsg_level = SOL_IPV6;
450 cmh->cmsg_type = IPV6_PKTINFO;
451 pki->ipi6_ifindex = 0;
452 ipv6_addr_copy(&pki->ipi6_addr,
453 &rqstp->rq_daddr.addr6);
454 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
462 * Generic sendto routine
465 svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
467 struct svc_sock *svsk = rqstp->rq_sock;
468 struct socket *sock = svsk->sk_sock;
472 long all[SVC_PKTINFO_SPACE / sizeof(long)];
474 struct cmsghdr *cmh = &buffer.hdr;
478 struct page **ppage = xdr->pages;
479 size_t base = xdr->page_base;
480 unsigned int pglen = xdr->page_len;
481 unsigned int flags = MSG_MORE;
482 char buf[RPC_MAX_ADDRBUFLEN];
486 if (rqstp->rq_prot == IPPROTO_UDP) {
487 struct msghdr msg = {
488 .msg_name = &rqstp->rq_addr,
489 .msg_namelen = rqstp->rq_addrlen,
491 .msg_controllen = sizeof(buffer),
492 .msg_flags = MSG_MORE,
495 svc_set_cmsg_data(rqstp, cmh);
497 if (sock_sendmsg(sock, &msg, 0) < 0)
502 if (slen == xdr->head[0].iov_len)
504 len = kernel_sendpage(sock, rqstp->rq_respages[0], 0,
505 xdr->head[0].iov_len, flags);
506 if (len != xdr->head[0].iov_len)
508 slen -= xdr->head[0].iov_len;
513 size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
517 result = kernel_sendpage(sock, *ppage, base, size, flags);
524 size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
529 if (xdr->tail[0].iov_len) {
530 result = kernel_sendpage(sock, rqstp->rq_respages[0],
531 ((unsigned long)xdr->tail[0].iov_base)
533 xdr->tail[0].iov_len, 0);
539 dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %s)\n",
540 rqstp->rq_sock, xdr->head[0].iov_base, xdr->head[0].iov_len,
541 xdr->len, len, svc_print_addr(rqstp, buf, sizeof(buf)));
547 * Report socket names for nfsdfs
549 static int one_sock_name(char *buf, struct svc_sock *svsk)
553 switch(svsk->sk_sk->sk_family) {
555 len = sprintf(buf, "ipv4 %s %u.%u.%u.%u %d\n",
556 svsk->sk_sk->sk_protocol==IPPROTO_UDP?
558 NIPQUAD(inet_sk(svsk->sk_sk)->rcv_saddr),
559 inet_sk(svsk->sk_sk)->num);
562 len = sprintf(buf, "*unknown-%d*\n",
563 svsk->sk_sk->sk_family);
569 svc_sock_names(char *buf, struct svc_serv *serv, char *toclose)
571 struct svc_sock *svsk, *closesk = NULL;
576 spin_lock_bh(&serv->sv_lock);
577 list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list) {
578 int onelen = one_sock_name(buf+len, svsk);
579 if (toclose && strcmp(toclose, buf+len) == 0)
584 spin_unlock_bh(&serv->sv_lock);
586 /* Should unregister with portmap, but you cannot
587 * unregister just one protocol...
589 svc_close_xprt(&closesk->sk_xprt);
594 EXPORT_SYMBOL(svc_sock_names);
597 * Check input queue length
600 svc_recv_available(struct svc_sock *svsk)
602 struct socket *sock = svsk->sk_sock;
605 err = kernel_sock_ioctl(sock, TIOCINQ, (unsigned long) &avail);
607 return (err >= 0)? avail : err;
611 * Generic recvfrom routine.
614 svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr, int buflen)
616 struct svc_sock *svsk = rqstp->rq_sock;
617 struct msghdr msg = {
618 .msg_flags = MSG_DONTWAIT,
620 struct sockaddr *sin;
623 len = kernel_recvmsg(svsk->sk_sock, &msg, iov, nr, buflen,
626 /* sock_recvmsg doesn't fill in the name/namelen, so we must..
628 memcpy(&rqstp->rq_addr, &svsk->sk_remote, svsk->sk_remotelen);
629 rqstp->rq_addrlen = svsk->sk_remotelen;
631 /* Destination address in request is needed for binding the
632 * source address in RPC callbacks later.
634 sin = (struct sockaddr *)&svsk->sk_local;
635 switch (sin->sa_family) {
637 rqstp->rq_daddr.addr = ((struct sockaddr_in *)sin)->sin_addr;
640 rqstp->rq_daddr.addr6 = ((struct sockaddr_in6 *)sin)->sin6_addr;
644 dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
645 svsk, iov[0].iov_base, iov[0].iov_len, len);
651 * Set socket snd and rcv buffer lengths
654 svc_sock_setbufsize(struct socket *sock, unsigned int snd, unsigned int rcv)
658 oldfs = get_fs(); set_fs(KERNEL_DS);
659 sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
660 (char*)&snd, sizeof(snd));
661 sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
662 (char*)&rcv, sizeof(rcv));
664 /* sock_setsockopt limits use to sysctl_?mem_max,
665 * which isn't acceptable. Until that is made conditional
666 * on not having CAP_SYS_RESOURCE or similar, we go direct...
667 * DaveM said I could!
670 sock->sk->sk_sndbuf = snd * 2;
671 sock->sk->sk_rcvbuf = rcv * 2;
672 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK|SOCK_RCVBUF_LOCK;
673 release_sock(sock->sk);
677 * INET callback when data has been received on the socket.
680 svc_udp_data_ready(struct sock *sk, int count)
682 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
685 dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
687 test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
688 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
689 svc_xprt_enqueue(&svsk->sk_xprt);
691 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
692 wake_up_interruptible(sk->sk_sleep);
696 * INET callback when space is newly available on the socket.
699 svc_write_space(struct sock *sk)
701 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
704 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
705 svsk, sk, test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
706 svc_xprt_enqueue(&svsk->sk_xprt);
709 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) {
710 dprintk("RPC svc_write_space: someone sleeping on %p\n",
712 wake_up_interruptible(sk->sk_sleep);
716 static inline void svc_udp_get_dest_address(struct svc_rqst *rqstp,
719 switch (rqstp->rq_sock->sk_sk->sk_family) {
721 struct in_pktinfo *pki = CMSG_DATA(cmh);
722 rqstp->rq_daddr.addr.s_addr = pki->ipi_spec_dst.s_addr;
726 struct in6_pktinfo *pki = CMSG_DATA(cmh);
727 ipv6_addr_copy(&rqstp->rq_daddr.addr6, &pki->ipi6_addr);
734 * Receive a datagram from a UDP socket.
737 svc_udp_recvfrom(struct svc_rqst *rqstp)
739 struct svc_sock *svsk = rqstp->rq_sock;
740 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
744 long all[SVC_PKTINFO_SPACE / sizeof(long)];
746 struct cmsghdr *cmh = &buffer.hdr;
748 struct msghdr msg = {
749 .msg_name = svc_addr(rqstp),
751 .msg_controllen = sizeof(buffer),
752 .msg_flags = MSG_DONTWAIT,
755 if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
756 /* udp sockets need large rcvbuf as all pending
757 * requests are still in that buffer. sndbuf must
758 * also be large enough that there is enough space
759 * for one reply per thread. We count all threads
760 * rather than threads in a particular pool, which
761 * provides an upper bound on the number of threads
762 * which will access the socket.
764 svc_sock_setbufsize(svsk->sk_sock,
765 (serv->sv_nrthreads+3) * serv->sv_max_mesg,
766 (serv->sv_nrthreads+3) * serv->sv_max_mesg);
768 if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
769 svc_sock_received(svsk);
770 return svc_deferred_recv(rqstp);
773 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
775 err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
776 0, 0, MSG_PEEK | MSG_DONTWAIT);
778 skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err);
781 if (err != -EAGAIN) {
782 /* possibly an icmp error */
783 dprintk("svc: recvfrom returned error %d\n", -err);
784 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
786 svc_sock_received(svsk);
789 rqstp->rq_addrlen = sizeof(rqstp->rq_addr);
790 if (skb->tstamp.tv64 == 0) {
791 skb->tstamp = ktime_get_real();
792 /* Don't enable netstamp, sunrpc doesn't
793 need that much accuracy */
795 svsk->sk_sk->sk_stamp = skb->tstamp;
796 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
799 * Maybe more packets - kick another thread ASAP.
801 svc_sock_received(svsk);
803 len = skb->len - sizeof(struct udphdr);
804 rqstp->rq_arg.len = len;
806 rqstp->rq_prot = IPPROTO_UDP;
808 if (cmh->cmsg_level != IPPROTO_IP ||
809 cmh->cmsg_type != IP_PKTINFO) {
811 printk("rpcsvc: received unknown control message:"
813 cmh->cmsg_level, cmh->cmsg_type);
814 skb_free_datagram(svsk->sk_sk, skb);
817 svc_udp_get_dest_address(rqstp, cmh);
819 if (skb_is_nonlinear(skb)) {
820 /* we have to copy */
822 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
825 skb_free_datagram(svsk->sk_sk, skb);
829 skb_free_datagram(svsk->sk_sk, skb);
831 /* we can use it in-place */
832 rqstp->rq_arg.head[0].iov_base = skb->data + sizeof(struct udphdr);
833 rqstp->rq_arg.head[0].iov_len = len;
834 if (skb_checksum_complete(skb)) {
835 skb_free_datagram(svsk->sk_sk, skb);
838 rqstp->rq_xprt_ctxt = skb;
841 rqstp->rq_arg.page_base = 0;
842 if (len <= rqstp->rq_arg.head[0].iov_len) {
843 rqstp->rq_arg.head[0].iov_len = len;
844 rqstp->rq_arg.page_len = 0;
845 rqstp->rq_respages = rqstp->rq_pages+1;
847 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
848 rqstp->rq_respages = rqstp->rq_pages + 1 +
849 DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE);
853 serv->sv_stats->netudpcnt++;
859 svc_udp_sendto(struct svc_rqst *rqstp)
863 error = svc_sendto(rqstp, &rqstp->rq_res);
864 if (error == -ECONNREFUSED)
865 /* ICMP error on earlier request. */
866 error = svc_sendto(rqstp, &rqstp->rq_res);
871 static void svc_udp_prep_reply_hdr(struct svc_rqst *rqstp)
875 static int svc_udp_has_wspace(struct svc_xprt *xprt)
877 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
878 struct svc_serv *serv = xprt->xpt_server;
879 unsigned long required;
882 * Set the SOCK_NOSPACE flag before checking the available
885 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
886 required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
887 if (required*2 > sock_wspace(svsk->sk_sk))
889 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
893 static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt)
899 static struct svc_xprt *svc_udp_create(struct svc_serv *serv,
900 struct sockaddr *sa, int salen,
903 return svc_create_socket(serv, IPPROTO_UDP, sa, salen, flags);
906 static struct svc_xprt_ops svc_udp_ops = {
907 .xpo_create = svc_udp_create,
908 .xpo_recvfrom = svc_udp_recvfrom,
909 .xpo_sendto = svc_udp_sendto,
910 .xpo_release_rqst = svc_release_skb,
911 .xpo_detach = svc_sock_detach,
912 .xpo_free = svc_sock_free,
913 .xpo_prep_reply_hdr = svc_udp_prep_reply_hdr,
914 .xpo_has_wspace = svc_udp_has_wspace,
915 .xpo_accept = svc_udp_accept,
918 static struct svc_xprt_class svc_udp_class = {
920 .xcl_owner = THIS_MODULE,
921 .xcl_ops = &svc_udp_ops,
922 .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP,
925 static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
930 svc_xprt_init(&svc_udp_class, &svsk->sk_xprt, serv);
931 svsk->sk_sk->sk_data_ready = svc_udp_data_ready;
932 svsk->sk_sk->sk_write_space = svc_write_space;
934 /* initialise setting must have enough space to
935 * receive and respond to one request.
936 * svc_udp_recvfrom will re-adjust if necessary
938 svc_sock_setbufsize(svsk->sk_sock,
939 3 * svsk->sk_xprt.xpt_server->sv_max_mesg,
940 3 * svsk->sk_xprt.xpt_server->sv_max_mesg);
942 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* might have come in before data_ready set up */
943 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
947 /* make sure we get destination address info */
948 svsk->sk_sock->ops->setsockopt(svsk->sk_sock, IPPROTO_IP, IP_PKTINFO,
949 (char __user *)&one, sizeof(one));
954 * A data_ready event on a listening socket means there's a connection
955 * pending. Do not use state_change as a substitute for it.
958 svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
960 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
962 dprintk("svc: socket %p TCP (listen) state change %d\n",
966 * This callback may called twice when a new connection
967 * is established as a child socket inherits everything
968 * from a parent LISTEN socket.
969 * 1) data_ready method of the parent socket will be called
970 * when one of child sockets become ESTABLISHED.
971 * 2) data_ready method of the child socket may be called
972 * when it receives data before the socket is accepted.
973 * In case of 2, we should ignore it silently.
975 if (sk->sk_state == TCP_LISTEN) {
977 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
978 svc_xprt_enqueue(&svsk->sk_xprt);
980 printk("svc: socket %p: no user data\n", sk);
983 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
984 wake_up_interruptible_all(sk->sk_sleep);
988 * A state change on a connected socket means it's dying or dead.
991 svc_tcp_state_change(struct sock *sk)
993 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
995 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
996 sk, sk->sk_state, sk->sk_user_data);
999 printk("svc: socket %p: no user data\n", sk);
1001 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1002 svc_xprt_enqueue(&svsk->sk_xprt);
1004 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
1005 wake_up_interruptible_all(sk->sk_sleep);
1009 svc_tcp_data_ready(struct sock *sk, int count)
1011 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
1013 dprintk("svc: socket %p TCP data ready (svsk %p)\n",
1014 sk, sk->sk_user_data);
1016 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
1017 svc_xprt_enqueue(&svsk->sk_xprt);
1019 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
1020 wake_up_interruptible(sk->sk_sleep);
1023 static inline int svc_port_is_privileged(struct sockaddr *sin)
1025 switch (sin->sa_family) {
1027 return ntohs(((struct sockaddr_in *)sin)->sin_port)
1030 return ntohs(((struct sockaddr_in6 *)sin)->sin6_port)
1038 * Accept a TCP connection
1040 static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt)
1042 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1043 struct sockaddr_storage addr;
1044 struct sockaddr *sin = (struct sockaddr *) &addr;
1045 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
1046 struct socket *sock = svsk->sk_sock;
1047 struct socket *newsock;
1048 struct svc_sock *newsvsk;
1050 char buf[RPC_MAX_ADDRBUFLEN];
1052 dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
1056 clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
1057 err = kernel_accept(sock, &newsock, O_NONBLOCK);
1060 printk(KERN_WARNING "%s: no more sockets!\n",
1062 else if (err != -EAGAIN && net_ratelimit())
1063 printk(KERN_WARNING "%s: accept failed (err %d)!\n",
1064 serv->sv_name, -err);
1067 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
1069 err = kernel_getpeername(newsock, sin, &slen);
1071 if (net_ratelimit())
1072 printk(KERN_WARNING "%s: peername failed (err %d)!\n",
1073 serv->sv_name, -err);
1074 goto failed; /* aborted connection or whatever */
1077 /* Ideally, we would want to reject connections from unauthorized
1078 * hosts here, but when we get encryption, the IP of the host won't
1079 * tell us anything. For now just warn about unpriv connections.
1081 if (!svc_port_is_privileged(sin)) {
1082 dprintk(KERN_WARNING
1083 "%s: connect from unprivileged port: %s\n",
1085 __svc_print_addr(sin, buf, sizeof(buf)));
1087 dprintk("%s: connect from %s\n", serv->sv_name,
1088 __svc_print_addr(sin, buf, sizeof(buf)));
1090 /* make sure that a write doesn't block forever when
1093 newsock->sk->sk_sndtimeo = HZ*30;
1095 if (!(newsvsk = svc_setup_socket(serv, newsock, &err,
1096 (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY))))
1098 memcpy(&newsvsk->sk_remote, sin, slen);
1099 newsvsk->sk_remotelen = slen;
1100 err = kernel_getsockname(newsock, sin, &slen);
1101 if (unlikely(err < 0)) {
1102 dprintk("svc_tcp_accept: kernel_getsockname error %d\n", -err);
1103 slen = offsetof(struct sockaddr, sa_data);
1105 memcpy(&newsvsk->sk_local, sin, slen);
1107 svc_sock_received(newsvsk);
1110 serv->sv_stats->nettcpconn++;
1112 return &newsvsk->sk_xprt;
1115 sock_release(newsock);
1120 * Receive data from a TCP socket.
1123 svc_tcp_recvfrom(struct svc_rqst *rqstp)
1125 struct svc_sock *svsk = rqstp->rq_sock;
1126 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
1131 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
1132 svsk, test_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags),
1133 test_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags),
1134 test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags));
1136 if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
1137 svc_sock_received(svsk);
1138 return svc_deferred_recv(rqstp);
1141 if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
1142 /* sndbuf needs to have room for one request
1143 * per thread, otherwise we can stall even when the
1144 * network isn't a bottleneck.
1146 * We count all threads rather than threads in a
1147 * particular pool, which provides an upper bound
1148 * on the number of threads which will access the socket.
1150 * rcvbuf just needs to be able to hold a few requests.
1151 * Normally they will be removed from the queue
1152 * as soon a a complete request arrives.
1154 svc_sock_setbufsize(svsk->sk_sock,
1155 (serv->sv_nrthreads+3) * serv->sv_max_mesg,
1156 3 * serv->sv_max_mesg);
1158 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
1160 /* Receive data. If we haven't got the record length yet, get
1161 * the next four bytes. Otherwise try to gobble up as much as
1162 * possible up to the complete record length.
1164 if (svsk->sk_tcplen < 4) {
1165 unsigned long want = 4 - svsk->sk_tcplen;
1168 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
1170 if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
1172 svsk->sk_tcplen += len;
1175 dprintk("svc: short recvfrom while reading record length (%d of %lu)\n",
1177 svc_sock_received(svsk);
1178 return -EAGAIN; /* record header not complete */
1181 svsk->sk_reclen = ntohl(svsk->sk_reclen);
1182 if (!(svsk->sk_reclen & 0x80000000)) {
1183 /* FIXME: technically, a record can be fragmented,
1184 * and non-terminal fragments will not have the top
1185 * bit set in the fragment length header.
1186 * But apparently no known nfs clients send fragmented
1188 if (net_ratelimit())
1189 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx"
1190 " (non-terminal)\n",
1191 (unsigned long) svsk->sk_reclen);
1194 svsk->sk_reclen &= 0x7fffffff;
1195 dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
1196 if (svsk->sk_reclen > serv->sv_max_mesg) {
1197 if (net_ratelimit())
1198 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx"
1200 (unsigned long) svsk->sk_reclen);
1205 /* Check whether enough data is available */
1206 len = svc_recv_available(svsk);
1210 if (len < svsk->sk_reclen) {
1211 dprintk("svc: incomplete TCP record (%d of %d)\n",
1212 len, svsk->sk_reclen);
1213 svc_sock_received(svsk);
1214 return -EAGAIN; /* record not complete */
1216 len = svsk->sk_reclen;
1217 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
1219 vec = rqstp->rq_vec;
1220 vec[0] = rqstp->rq_arg.head[0];
1223 while (vlen < len) {
1224 vec[pnum].iov_base = page_address(rqstp->rq_pages[pnum]);
1225 vec[pnum].iov_len = PAGE_SIZE;
1229 rqstp->rq_respages = &rqstp->rq_pages[pnum];
1231 /* Now receive data */
1232 len = svc_recvfrom(rqstp, vec, pnum, len);
1236 dprintk("svc: TCP complete record (%d bytes)\n", len);
1237 rqstp->rq_arg.len = len;
1238 rqstp->rq_arg.page_base = 0;
1239 if (len <= rqstp->rq_arg.head[0].iov_len) {
1240 rqstp->rq_arg.head[0].iov_len = len;
1241 rqstp->rq_arg.page_len = 0;
1243 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
1246 rqstp->rq_xprt_ctxt = NULL;
1247 rqstp->rq_prot = IPPROTO_TCP;
1249 /* Reset TCP read info */
1250 svsk->sk_reclen = 0;
1251 svsk->sk_tcplen = 0;
1253 svc_sock_received(svsk);
1255 serv->sv_stats->nettcpcnt++;
1260 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1264 if (len == -EAGAIN) {
1265 dprintk("RPC: TCP recvfrom got EAGAIN\n");
1266 svc_sock_received(svsk);
1268 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
1269 svsk->sk_xprt.xpt_server->sv_name, -len);
1277 * Send out data on TCP socket.
1280 svc_tcp_sendto(struct svc_rqst *rqstp)
1282 struct xdr_buf *xbufp = &rqstp->rq_res;
1286 /* Set up the first element of the reply kvec.
1287 * Any other kvecs that may be in use have been taken
1288 * care of by the server implementation itself.
1290 reclen = htonl(0x80000000|((xbufp->len ) - 4));
1291 memcpy(xbufp->head[0].iov_base, &reclen, 4);
1293 if (test_bit(XPT_DEAD, &rqstp->rq_sock->sk_xprt.xpt_flags))
1296 sent = svc_sendto(rqstp, &rqstp->rq_res);
1297 if (sent != xbufp->len) {
1298 printk(KERN_NOTICE "rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n",
1299 rqstp->rq_sock->sk_xprt.xpt_server->sv_name,
1300 (sent<0)?"got error":"sent only",
1302 set_bit(XPT_CLOSE, &rqstp->rq_sock->sk_xprt.xpt_flags);
1303 svc_xprt_enqueue(rqstp->rq_xprt);
1310 * Setup response header. TCP has a 4B record length field.
1312 static void svc_tcp_prep_reply_hdr(struct svc_rqst *rqstp)
1314 struct kvec *resv = &rqstp->rq_res.head[0];
1316 /* tcp needs a space for the record length... */
1320 static int svc_tcp_has_wspace(struct svc_xprt *xprt)
1322 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1323 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
1328 * Set the SOCK_NOSPACE flag before checking the available
1331 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
1332 required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
1333 wspace = sk_stream_wspace(svsk->sk_sk);
1335 if (wspace < sk_stream_min_wspace(svsk->sk_sk))
1337 if (required * 2 > wspace)
1340 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
1344 static struct svc_xprt *svc_tcp_create(struct svc_serv *serv,
1345 struct sockaddr *sa, int salen,
1348 return svc_create_socket(serv, IPPROTO_TCP, sa, salen, flags);
1351 static struct svc_xprt_ops svc_tcp_ops = {
1352 .xpo_create = svc_tcp_create,
1353 .xpo_recvfrom = svc_tcp_recvfrom,
1354 .xpo_sendto = svc_tcp_sendto,
1355 .xpo_release_rqst = svc_release_skb,
1356 .xpo_detach = svc_sock_detach,
1357 .xpo_free = svc_sock_free,
1358 .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr,
1359 .xpo_has_wspace = svc_tcp_has_wspace,
1360 .xpo_accept = svc_tcp_accept,
1363 static struct svc_xprt_class svc_tcp_class = {
1365 .xcl_owner = THIS_MODULE,
1366 .xcl_ops = &svc_tcp_ops,
1367 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
1370 void svc_init_xprt_sock(void)
1372 svc_reg_xprt_class(&svc_tcp_class);
1373 svc_reg_xprt_class(&svc_udp_class);
1376 void svc_cleanup_xprt_sock(void)
1378 svc_unreg_xprt_class(&svc_tcp_class);
1379 svc_unreg_xprt_class(&svc_udp_class);
1382 static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
1384 struct sock *sk = svsk->sk_sk;
1385 struct tcp_sock *tp = tcp_sk(sk);
1387 svc_xprt_init(&svc_tcp_class, &svsk->sk_xprt, serv);
1389 if (sk->sk_state == TCP_LISTEN) {
1390 dprintk("setting up TCP socket for listening\n");
1391 set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags);
1392 sk->sk_data_ready = svc_tcp_listen_data_ready;
1393 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
1395 dprintk("setting up TCP socket for reading\n");
1396 sk->sk_state_change = svc_tcp_state_change;
1397 sk->sk_data_ready = svc_tcp_data_ready;
1398 sk->sk_write_space = svc_write_space;
1400 svsk->sk_reclen = 0;
1401 svsk->sk_tcplen = 0;
1403 tp->nonagle = 1; /* disable Nagle's algorithm */
1405 /* initialise setting must have enough space to
1406 * receive and respond to one request.
1407 * svc_tcp_recvfrom will re-adjust if necessary
1409 svc_sock_setbufsize(svsk->sk_sock,
1410 3 * svsk->sk_xprt.xpt_server->sv_max_mesg,
1411 3 * svsk->sk_xprt.xpt_server->sv_max_mesg);
1413 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
1414 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
1415 if (sk->sk_state != TCP_ESTABLISHED)
1416 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1421 svc_sock_update_bufs(struct svc_serv *serv)
1424 * The number of server threads has changed. Update
1425 * rcvbuf and sndbuf accordingly on all sockets
1427 struct list_head *le;
1429 spin_lock_bh(&serv->sv_lock);
1430 list_for_each(le, &serv->sv_permsocks) {
1431 struct svc_sock *svsk =
1432 list_entry(le, struct svc_sock, sk_xprt.xpt_list);
1433 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
1435 list_for_each(le, &serv->sv_tempsocks) {
1436 struct svc_sock *svsk =
1437 list_entry(le, struct svc_sock, sk_xprt.xpt_list);
1438 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
1440 spin_unlock_bh(&serv->sv_lock);
1444 * Make sure that we don't have too many active connections. If we
1445 * have, something must be dropped.
1447 * There's no point in trying to do random drop here for DoS
1448 * prevention. The NFS clients does 1 reconnect in 15 seconds. An
1449 * attacker can easily beat that.
1451 * The only somewhat efficient mechanism would be if drop old
1452 * connections from the same IP first. But right now we don't even
1453 * record the client IP in svc_sock.
1455 static void svc_check_conn_limits(struct svc_serv *serv)
1457 if (serv->sv_tmpcnt > (serv->sv_nrthreads+3)*20) {
1458 struct svc_sock *svsk = NULL;
1459 spin_lock_bh(&serv->sv_lock);
1460 if (!list_empty(&serv->sv_tempsocks)) {
1461 if (net_ratelimit()) {
1462 /* Try to help the admin */
1463 printk(KERN_NOTICE "%s: too many open TCP "
1464 "sockets, consider increasing the "
1465 "number of nfsd threads\n",
1469 * Always select the oldest socket. It's not fair,
1472 svsk = list_entry(serv->sv_tempsocks.prev,
1475 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1476 svc_xprt_get(&svsk->sk_xprt);
1478 spin_unlock_bh(&serv->sv_lock);
1481 svc_xprt_enqueue(&svsk->sk_xprt);
1482 svc_xprt_put(&svsk->sk_xprt);
1488 * Receive the next request on any socket. This code is carefully
1489 * organised not to touch any cachelines in the shared svc_serv
1490 * structure, only cachelines in the local svc_pool.
1493 svc_recv(struct svc_rqst *rqstp, long timeout)
1495 struct svc_sock *svsk = NULL;
1496 struct svc_serv *serv = rqstp->rq_server;
1497 struct svc_pool *pool = rqstp->rq_pool;
1500 struct xdr_buf *arg;
1501 DECLARE_WAITQUEUE(wait, current);
1503 dprintk("svc: server %p waiting for data (to = %ld)\n",
1508 "svc_recv: service %p, socket not NULL!\n",
1510 if (waitqueue_active(&rqstp->rq_wait))
1512 "svc_recv: service %p, wait queue active!\n",
1516 /* now allocate needed pages. If we get a failure, sleep briefly */
1517 pages = (serv->sv_max_mesg + PAGE_SIZE) / PAGE_SIZE;
1518 for (i=0; i < pages ; i++)
1519 while (rqstp->rq_pages[i] == NULL) {
1520 struct page *p = alloc_page(GFP_KERNEL);
1522 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
1523 rqstp->rq_pages[i] = p;
1525 rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */
1526 BUG_ON(pages >= RPCSVC_MAXPAGES);
1528 /* Make arg->head point to first page and arg->pages point to rest */
1529 arg = &rqstp->rq_arg;
1530 arg->head[0].iov_base = page_address(rqstp->rq_pages[0]);
1531 arg->head[0].iov_len = PAGE_SIZE;
1532 arg->pages = rqstp->rq_pages + 1;
1534 /* save at least one page for response */
1535 arg->page_len = (pages-2)*PAGE_SIZE;
1536 arg->len = (pages-1)*PAGE_SIZE;
1537 arg->tail[0].iov_len = 0;
1544 spin_lock_bh(&pool->sp_lock);
1545 if ((svsk = svc_sock_dequeue(pool)) != NULL) {
1546 rqstp->rq_sock = svsk;
1547 svc_xprt_get(&svsk->sk_xprt);
1548 rqstp->rq_reserved = serv->sv_max_mesg;
1549 atomic_add(rqstp->rq_reserved, &svsk->sk_xprt.xpt_reserved);
1551 /* No data pending. Go to sleep */
1552 svc_thread_enqueue(pool, rqstp);
1555 * We have to be able to interrupt this wait
1556 * to bring down the daemons ...
1558 set_current_state(TASK_INTERRUPTIBLE);
1559 add_wait_queue(&rqstp->rq_wait, &wait);
1560 spin_unlock_bh(&pool->sp_lock);
1562 schedule_timeout(timeout);
1566 spin_lock_bh(&pool->sp_lock);
1567 remove_wait_queue(&rqstp->rq_wait, &wait);
1569 if (!(svsk = rqstp->rq_sock)) {
1570 svc_thread_dequeue(pool, rqstp);
1571 spin_unlock_bh(&pool->sp_lock);
1572 dprintk("svc: server %p, no data yet\n", rqstp);
1573 return signalled()? -EINTR : -EAGAIN;
1576 spin_unlock_bh(&pool->sp_lock);
1579 if (test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags)) {
1580 dprintk("svc_recv: found XPT_CLOSE\n");
1581 svc_delete_xprt(&svsk->sk_xprt);
1582 } else if (test_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags)) {
1583 struct svc_xprt *newxpt;
1584 newxpt = svsk->sk_xprt.xpt_ops->xpo_accept(&svsk->sk_xprt);
1587 * We know this module_get will succeed because the
1588 * listener holds a reference too
1590 __module_get(newxpt->xpt_class->xcl_owner);
1591 svc_check_conn_limits(svsk->sk_xprt.xpt_server);
1593 svc_sock_received(svsk);
1595 dprintk("svc: server %p, pool %u, socket %p, inuse=%d\n",
1596 rqstp, pool->sp_id, svsk,
1597 atomic_read(&svsk->sk_xprt.xpt_ref.refcount));
1598 len = svsk->sk_xprt.xpt_ops->xpo_recvfrom(rqstp);
1599 dprintk("svc: got len=%d\n", len);
1602 /* No data, incomplete (TCP) read, or accept() */
1603 if (len == 0 || len == -EAGAIN) {
1604 rqstp->rq_res.len = 0;
1605 svc_sock_release(rqstp);
1608 svsk->sk_lastrecv = get_seconds();
1609 clear_bit(XPT_OLD, &svsk->sk_xprt.xpt_flags);
1611 rqstp->rq_secure = svc_port_is_privileged(svc_addr(rqstp));
1612 rqstp->rq_chandle.defer = svc_defer;
1615 serv->sv_stats->netcnt++;
1623 svc_drop(struct svc_rqst *rqstp)
1625 dprintk("svc: socket %p dropped request\n", rqstp->rq_sock);
1626 svc_sock_release(rqstp);
1630 * Return reply to client.
1633 svc_send(struct svc_rqst *rqstp)
1635 struct svc_xprt *xprt;
1639 xprt = rqstp->rq_xprt;
1643 /* release the receive skb before sending the reply */
1644 rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
1646 /* calculate over-all length */
1647 xb = & rqstp->rq_res;
1648 xb->len = xb->head[0].iov_len +
1650 xb->tail[0].iov_len;
1652 /* Grab mutex to serialize outgoing data. */
1653 mutex_lock(&xprt->xpt_mutex);
1654 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
1657 len = xprt->xpt_ops->xpo_sendto(rqstp);
1658 mutex_unlock(&xprt->xpt_mutex);
1659 svc_sock_release(rqstp);
1661 if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
1667 * Timer function to close old temporary sockets, using
1668 * a mark-and-sweep algorithm.
1671 svc_age_temp_sockets(unsigned long closure)
1673 struct svc_serv *serv = (struct svc_serv *)closure;
1674 struct svc_sock *svsk;
1675 struct list_head *le, *next;
1676 LIST_HEAD(to_be_aged);
1678 dprintk("svc_age_temp_sockets\n");
1680 if (!spin_trylock_bh(&serv->sv_lock)) {
1681 /* busy, try again 1 sec later */
1682 dprintk("svc_age_temp_sockets: busy\n");
1683 mod_timer(&serv->sv_temptimer, jiffies + HZ);
1687 list_for_each_safe(le, next, &serv->sv_tempsocks) {
1688 svsk = list_entry(le, struct svc_sock, sk_xprt.xpt_list);
1690 if (!test_and_set_bit(XPT_OLD, &svsk->sk_xprt.xpt_flags))
1692 if (atomic_read(&svsk->sk_xprt.xpt_ref.refcount) > 1
1693 || test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags))
1695 svc_xprt_get(&svsk->sk_xprt);
1696 list_move(le, &to_be_aged);
1697 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1698 set_bit(XPT_DETACHED, &svsk->sk_xprt.xpt_flags);
1700 spin_unlock_bh(&serv->sv_lock);
1702 while (!list_empty(&to_be_aged)) {
1703 le = to_be_aged.next;
1704 /* fiddling the sk_xprt.xpt_list node is safe 'cos we're XPT_DETACHED */
1706 svsk = list_entry(le, struct svc_sock, sk_xprt.xpt_list);
1708 dprintk("queuing svsk %p for closing, %lu seconds old\n",
1709 svsk, get_seconds() - svsk->sk_lastrecv);
1711 /* a thread will dequeue and close it soon */
1712 svc_xprt_enqueue(&svsk->sk_xprt);
1713 svc_xprt_put(&svsk->sk_xprt);
1716 mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
1720 * Initialize socket for RPC use and create svc_sock struct
1721 * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
1723 static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1724 struct socket *sock,
1725 int *errp, int flags)
1727 struct svc_sock *svsk;
1729 int pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
1730 int is_temporary = flags & SVC_SOCK_TEMPORARY;
1732 dprintk("svc: svc_setup_socket %p\n", sock);
1733 if (!(svsk = kzalloc(sizeof(*svsk), GFP_KERNEL))) {
1740 /* Register socket with portmapper */
1741 if (*errp >= 0 && pmap_register)
1742 *errp = svc_register(serv, inet->sk_protocol,
1743 ntohs(inet_sk(inet)->sport));
1750 set_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags);
1751 inet->sk_user_data = svsk;
1752 svsk->sk_sock = sock;
1754 svsk->sk_ostate = inet->sk_state_change;
1755 svsk->sk_odata = inet->sk_data_ready;
1756 svsk->sk_owspace = inet->sk_write_space;
1757 svsk->sk_lastrecv = get_seconds();
1758 spin_lock_init(&svsk->sk_lock);
1759 INIT_LIST_HEAD(&svsk->sk_deferred);
1761 /* Initialize the socket */
1762 if (sock->type == SOCK_DGRAM)
1763 svc_udp_init(svsk, serv);
1765 svc_tcp_init(svsk, serv);
1767 spin_lock_bh(&serv->sv_lock);
1769 set_bit(XPT_TEMP, &svsk->sk_xprt.xpt_flags);
1770 list_add(&svsk->sk_xprt.xpt_list, &serv->sv_tempsocks);
1772 if (serv->sv_temptimer.function == NULL) {
1773 /* setup timer to age temp sockets */
1774 setup_timer(&serv->sv_temptimer, svc_age_temp_sockets,
1775 (unsigned long)serv);
1776 mod_timer(&serv->sv_temptimer,
1777 jiffies + svc_conn_age_period * HZ);
1780 clear_bit(XPT_TEMP, &svsk->sk_xprt.xpt_flags);
1781 list_add(&svsk->sk_xprt.xpt_list, &serv->sv_permsocks);
1783 spin_unlock_bh(&serv->sv_lock);
1785 dprintk("svc: svc_setup_socket created %p (inet %p)\n",
1791 int svc_addsock(struct svc_serv *serv,
1797 struct socket *so = sockfd_lookup(fd, &err);
1798 struct svc_sock *svsk = NULL;
1802 if (so->sk->sk_family != AF_INET)
1803 err = -EAFNOSUPPORT;
1804 else if (so->sk->sk_protocol != IPPROTO_TCP &&
1805 so->sk->sk_protocol != IPPROTO_UDP)
1806 err = -EPROTONOSUPPORT;
1807 else if (so->state > SS_UNCONNECTED)
1810 svsk = svc_setup_socket(serv, so, &err, SVC_SOCK_DEFAULTS);
1812 svc_sock_received(svsk);
1820 if (proto) *proto = so->sk->sk_protocol;
1821 return one_sock_name(name_return, svsk);
1823 EXPORT_SYMBOL_GPL(svc_addsock);
1826 * Create socket for RPC service.
1828 static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
1830 struct sockaddr *sin, int len,
1833 struct svc_sock *svsk;
1834 struct socket *sock;
1837 char buf[RPC_MAX_ADDRBUFLEN];
1839 dprintk("svc: svc_create_socket(%s, %d, %s)\n",
1840 serv->sv_program->pg_name, protocol,
1841 __svc_print_addr(sin, buf, sizeof(buf)));
1843 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1844 printk(KERN_WARNING "svc: only UDP and TCP "
1845 "sockets supported\n");
1846 return ERR_PTR(-EINVAL);
1848 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1850 error = sock_create_kern(sin->sa_family, type, protocol, &sock);
1852 return ERR_PTR(error);
1854 svc_reclassify_socket(sock);
1856 if (type == SOCK_STREAM)
1857 sock->sk->sk_reuse = 1; /* allow address reuse */
1858 error = kernel_bind(sock, sin, len);
1862 if (protocol == IPPROTO_TCP) {
1863 if ((error = kernel_listen(sock, 64)) < 0)
1867 if ((svsk = svc_setup_socket(serv, sock, &error, flags)) != NULL) {
1868 svc_sock_received(svsk);
1869 return (struct svc_xprt *)svsk;
1873 dprintk("svc: svc_create_socket error = %d\n", -error);
1875 return ERR_PTR(error);
1879 * Detach the svc_sock from the socket so that no
1880 * more callbacks occur.
1882 static void svc_sock_detach(struct svc_xprt *xprt)
1884 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1885 struct sock *sk = svsk->sk_sk;
1887 dprintk("svc: svc_sock_detach(%p)\n", svsk);
1889 /* put back the old socket callbacks */
1890 sk->sk_state_change = svsk->sk_ostate;
1891 sk->sk_data_ready = svsk->sk_odata;
1892 sk->sk_write_space = svsk->sk_owspace;
1896 * Free the svc_sock's socket resources and the svc_sock itself.
1898 static void svc_sock_free(struct svc_xprt *xprt)
1900 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1901 dprintk("svc: svc_sock_free(%p)\n", svsk);
1903 if (svsk->sk_info_authunix != NULL)
1904 svcauth_unix_info_release(svsk->sk_info_authunix);
1905 if (svsk->sk_sock->file)
1906 sockfd_put(svsk->sk_sock);
1908 sock_release(svsk->sk_sock);
1913 * Remove a dead transport
1915 static void svc_delete_xprt(struct svc_xprt *xprt)
1917 struct svc_serv *serv = xprt->xpt_server;
1919 dprintk("svc: svc_delete_xprt(%p)\n", xprt);
1920 xprt->xpt_ops->xpo_detach(xprt);
1922 spin_lock_bh(&serv->sv_lock);
1923 if (!test_and_set_bit(XPT_DETACHED, &xprt->xpt_flags))
1924 list_del_init(&xprt->xpt_list);
1926 * We used to delete the transport from whichever list
1927 * it's sk_xprt.xpt_ready node was on, but we don't actually
1928 * need to. This is because the only time we're called
1929 * while still attached to a queue, the queue itself
1930 * is about to be destroyed (in svc_destroy).
1932 if (!test_and_set_bit(XPT_DEAD, &xprt->xpt_flags)) {
1933 BUG_ON(atomic_read(&xprt->xpt_ref.refcount) < 2);
1934 if (test_bit(XPT_TEMP, &xprt->xpt_flags))
1938 spin_unlock_bh(&serv->sv_lock);
1941 static void svc_close_xprt(struct svc_xprt *xprt)
1943 set_bit(XPT_CLOSE, &xprt->xpt_flags);
1944 if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags))
1945 /* someone else will have to effect the close */
1949 svc_delete_xprt(xprt);
1950 clear_bit(XPT_BUSY, &xprt->xpt_flags);
1954 void svc_close_all(struct list_head *xprt_list)
1956 struct svc_xprt *xprt;
1957 struct svc_xprt *tmp;
1959 list_for_each_entry_safe(xprt, tmp, xprt_list, xpt_list) {
1960 set_bit(XPT_CLOSE, &xprt->xpt_flags);
1961 if (test_bit(XPT_BUSY, &xprt->xpt_flags)) {
1962 /* Waiting to be processed, but no threads left,
1963 * So just remove it from the waiting list
1965 list_del_init(&xprt->xpt_ready);
1966 clear_bit(XPT_BUSY, &xprt->xpt_flags);
1968 svc_close_xprt(xprt);
1973 * Handle defer and revisit of requests
1976 static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
1978 struct svc_deferred_req *dr = container_of(dreq, struct svc_deferred_req, handle);
1979 struct svc_sock *svsk;
1982 svc_xprt_put(&dr->svsk->sk_xprt);
1986 dprintk("revisit queued\n");
1989 spin_lock(&svsk->sk_lock);
1990 list_add(&dr->handle.recent, &svsk->sk_deferred);
1991 spin_unlock(&svsk->sk_lock);
1992 set_bit(XPT_DEFERRED, &svsk->sk_xprt.xpt_flags);
1993 svc_xprt_enqueue(&svsk->sk_xprt);
1994 svc_xprt_put(&svsk->sk_xprt);
1997 static struct cache_deferred_req *
1998 svc_defer(struct cache_req *req)
2000 struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
2001 int size = sizeof(struct svc_deferred_req) + (rqstp->rq_arg.len);
2002 struct svc_deferred_req *dr;
2004 if (rqstp->rq_arg.page_len)
2005 return NULL; /* if more than a page, give up FIXME */
2006 if (rqstp->rq_deferred) {
2007 dr = rqstp->rq_deferred;
2008 rqstp->rq_deferred = NULL;
2010 int skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
2011 /* FIXME maybe discard if size too large */
2012 dr = kmalloc(size, GFP_KERNEL);
2016 dr->handle.owner = rqstp->rq_server;
2017 dr->prot = rqstp->rq_prot;
2018 memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen);
2019 dr->addrlen = rqstp->rq_addrlen;
2020 dr->daddr = rqstp->rq_daddr;
2021 dr->argslen = rqstp->rq_arg.len >> 2;
2022 memcpy(dr->args, rqstp->rq_arg.head[0].iov_base-skip, dr->argslen<<2);
2024 svc_xprt_get(rqstp->rq_xprt);
2025 dr->svsk = rqstp->rq_sock;
2027 dr->handle.revisit = svc_revisit;
2032 * recv data from a deferred request into an active one
2034 static int svc_deferred_recv(struct svc_rqst *rqstp)
2036 struct svc_deferred_req *dr = rqstp->rq_deferred;
2038 rqstp->rq_arg.head[0].iov_base = dr->args;
2039 rqstp->rq_arg.head[0].iov_len = dr->argslen<<2;
2040 rqstp->rq_arg.page_len = 0;
2041 rqstp->rq_arg.len = dr->argslen<<2;
2042 rqstp->rq_prot = dr->prot;
2043 memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen);
2044 rqstp->rq_addrlen = dr->addrlen;
2045 rqstp->rq_daddr = dr->daddr;
2046 rqstp->rq_respages = rqstp->rq_pages;
2047 return dr->argslen<<2;
2051 static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk)
2053 struct svc_deferred_req *dr = NULL;
2055 if (!test_bit(XPT_DEFERRED, &svsk->sk_xprt.xpt_flags))
2057 spin_lock(&svsk->sk_lock);
2058 clear_bit(XPT_DEFERRED, &svsk->sk_xprt.xpt_flags);
2059 if (!list_empty(&svsk->sk_deferred)) {
2060 dr = list_entry(svsk->sk_deferred.next,
2061 struct svc_deferred_req,
2063 list_del_init(&dr->handle.recent);
2064 set_bit(XPT_DEFERRED, &svsk->sk_xprt.xpt_flags);
2066 spin_unlock(&svsk->sk_lock);