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