blob: b11669670baa406614f458633cf1daeac1f5c8b9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Andrew Morton91483c42005-08-09 20:20:07 -070029#include <linux/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/unistd.h>
31#include <linux/slab.h>
32#include <linux/netdevice.h>
33#include <linux/skbuff.h>
NeilBrownb41b66d2006-10-02 02:17:48 -070034#include <linux/file.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080035#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <net/sock.h>
37#include <net/checksum.h>
38#include <net/ip.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070039#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/uaccess.h>
41#include <asm/ioctls.h>
42
43#include <linux/sunrpc/types.h>
Chuck Leverad06e4b2007-02-12 00:53:32 -080044#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/xdr.h>
46#include <linux/sunrpc/svcsock.h>
47#include <linux/sunrpc/stats.h>
48
49/* SMP locking strategy:
50 *
Greg Banks3262c812006-10-02 02:17:58 -070051 * svc_pool->sp_lock protects most of the fields of that pool.
52 * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt.
53 * when both need to be taken (rare), svc_serv->sv_lock is first.
54 * BKL protects svc_serv->sv_nrthread.
Greg Banks1a68d952006-10-02 02:17:55 -070055 * svc_sock->sk_defer_lock protects the svc_sock->sk_deferred list
Greg Banksc081a0c2006-10-02 02:17:57 -070056 * svc_sock->sk_flags.SK_BUSY prevents a svc_sock being enqueued multiply.
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 *
58 * Some flags can be set to certain values at any time
59 * providing that certain rules are followed:
60 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 * SK_CONN, SK_DATA, can be set or cleared at any time.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -080062 * after a set, svc_sock_enqueue must be called.
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * after a clear, the socket must be read/accepted
64 * if this succeeds, it must be set again.
65 * SK_CLOSE can set at any time. It is never cleared.
NeilBrownaaf68cf2007-02-08 14:20:30 -080066 * sk_inuse contains a bias of '1' until SK_DEAD is set.
67 * so when sk_inuse hits zero, we know the socket is dead
68 * and no-one is using it.
69 * SK_DEAD can only be set while SK_BUSY is held which ensures
70 * no other thread will be using the socket or will try to
71 * set SK_DEAD.
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 *
73 */
74
75#define RPCDBG_FACILITY RPCDBG_SVCSOCK
76
77
78static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
Chuck Lever6b174332007-02-12 00:53:28 -080079 int *errp, int flags);
NeilBrownaaf68cf2007-02-08 14:20:30 -080080static void svc_delete_socket(struct svc_sock *svsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static void svc_udp_data_ready(struct sock *, int);
82static int svc_udp_recvfrom(struct svc_rqst *);
83static int svc_udp_sendto(struct svc_rqst *);
84
85static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk);
86static int svc_deferred_recv(struct svc_rqst *rqstp);
87static struct cache_deferred_req *svc_defer(struct cache_req *req);
88
Greg Banks36bdfc82006-10-02 02:17:54 -070089/* apparently the "standard" is that clients close
90 * idle connections after 5 minutes, servers after
91 * 6 minutes
92 * http://www.connectathon.org/talks96/nfstcp.pdf
93 */
94static int svc_conn_age_period = 6*60;
95
Peter Zijlstraed075362006-12-06 20:35:24 -080096#ifdef CONFIG_DEBUG_LOCK_ALLOC
97static struct lock_class_key svc_key[2];
98static struct lock_class_key svc_slock_key[2];
99
100static inline void svc_reclassify_socket(struct socket *sock)
101{
102 struct sock *sk = sock->sk;
103 BUG_ON(sk->sk_lock.owner != NULL);
104 switch (sk->sk_family) {
105 case AF_INET:
106 sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
107 &svc_slock_key[0], "sk_lock-AF_INET-NFSD", &svc_key[0]);
108 break;
109
110 case AF_INET6:
111 sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
112 &svc_slock_key[1], "sk_lock-AF_INET6-NFSD", &svc_key[1]);
113 break;
114
115 default:
116 BUG();
117 }
118}
119#else
120static inline void svc_reclassify_socket(struct socket *sock)
121{
122}
123#endif
124
Chuck Leverad06e4b2007-02-12 00:53:32 -0800125static char *__svc_print_addr(struct sockaddr *addr, char *buf, size_t len)
126{
127 switch (addr->sa_family) {
128 case AF_INET:
129 snprintf(buf, len, "%u.%u.%u.%u, port=%u",
130 NIPQUAD(((struct sockaddr_in *) addr)->sin_addr),
131 htons(((struct sockaddr_in *) addr)->sin_port));
132 break;
133#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
134 case AF_INET6:
135 snprintf(buf, len, "%x:%x:%x:%x:%x:%x:%x:%x, port=%u",
136 NIP6(((struct sockaddr_in6 *) addr)->sin6_addr),
137 htons(((struct sockaddr_in6 *) addr)->sin6_port));
138 break;
139#endif
140 default:
141 snprintf(buf, len, "unknown address type: %d", addr->sa_family);
142 break;
143 }
144 return buf;
145}
146
147/**
148 * svc_print_addr - Format rq_addr field for printing
149 * @rqstp: svc_rqst struct containing address to print
150 * @buf: target buffer for formatted address
151 * @len: length of target buffer
152 *
153 */
154char *svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len)
155{
Chuck Lever27459f02007-02-12 00:53:34 -0800156 return __svc_print_addr(svc_addr(rqstp), buf, len);
Chuck Leverad06e4b2007-02-12 00:53:32 -0800157}
158EXPORT_SYMBOL_GPL(svc_print_addr);
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/*
Greg Banks3262c812006-10-02 02:17:58 -0700161 * Queue up an idle server thread. Must have pool->sp_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 * Note: this is really a stack rather than a queue, so that we only
Greg Banks3262c812006-10-02 02:17:58 -0700163 * use as many different threads as we need, and the rest don't pollute
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 * the cache.
165 */
166static inline void
Greg Banks3262c812006-10-02 02:17:58 -0700167svc_thread_enqueue(struct svc_pool *pool, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Greg Banks3262c812006-10-02 02:17:58 -0700169 list_add(&rqstp->rq_list, &pool->sp_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172/*
Greg Banks3262c812006-10-02 02:17:58 -0700173 * Dequeue an nfsd thread. Must have pool->sp_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 */
175static inline void
Greg Banks3262c812006-10-02 02:17:58 -0700176svc_thread_dequeue(struct svc_pool *pool, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 list_del(&rqstp->rq_list);
179}
180
181/*
182 * Release an skbuff after use
183 */
184static inline void
185svc_release_skb(struct svc_rqst *rqstp)
186{
187 struct sk_buff *skb = rqstp->rq_skbuff;
188 struct svc_deferred_req *dr = rqstp->rq_deferred;
189
190 if (skb) {
191 rqstp->rq_skbuff = NULL;
192
193 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
194 skb_free_datagram(rqstp->rq_sock->sk_sk, skb);
195 }
196 if (dr) {
197 rqstp->rq_deferred = NULL;
198 kfree(dr);
199 }
200}
201
202/*
203 * Any space to write?
204 */
205static inline unsigned long
206svc_sock_wspace(struct svc_sock *svsk)
207{
208 int wspace;
209
210 if (svsk->sk_sock->type == SOCK_STREAM)
211 wspace = sk_stream_wspace(svsk->sk_sk);
212 else
213 wspace = sock_wspace(svsk->sk_sk);
214
215 return wspace;
216}
217
218/*
219 * Queue up a socket with data pending. If there are idle nfsd
220 * processes, wake 'em up.
221 *
222 */
223static void
224svc_sock_enqueue(struct svc_sock *svsk)
225{
226 struct svc_serv *serv = svsk->sk_server;
Greg Banksbfd24162006-10-02 02:18:01 -0700227 struct svc_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 struct svc_rqst *rqstp;
Greg Banksbfd24162006-10-02 02:18:01 -0700229 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 if (!(svsk->sk_flags &
232 ( (1<<SK_CONN)|(1<<SK_DATA)|(1<<SK_CLOSE)|(1<<SK_DEFERRED)) ))
233 return;
234 if (test_bit(SK_DEAD, &svsk->sk_flags))
235 return;
236
Greg Banksbfd24162006-10-02 02:18:01 -0700237 cpu = get_cpu();
238 pool = svc_pool_for_cpu(svsk->sk_server, cpu);
239 put_cpu();
240
Greg Banks3262c812006-10-02 02:17:58 -0700241 spin_lock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Greg Banks3262c812006-10-02 02:17:58 -0700243 if (!list_empty(&pool->sp_threads) &&
244 !list_empty(&pool->sp_sockets))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 printk(KERN_ERR
246 "svc_sock_enqueue: threads and sockets both waiting??\n");
247
248 if (test_bit(SK_DEAD, &svsk->sk_flags)) {
249 /* Don't enqueue dead sockets */
250 dprintk("svc: socket %p is dead, not enqueued\n", svsk->sk_sk);
251 goto out_unlock;
252 }
253
Greg Banksc081a0c2006-10-02 02:17:57 -0700254 /* Mark socket as busy. It will remain in this state until the
255 * server has processed all pending data and put the socket back
256 * on the idle list. We update SK_BUSY atomically because
257 * it also guards against trying to enqueue the svc_sock twice.
258 */
259 if (test_and_set_bit(SK_BUSY, &svsk->sk_flags)) {
260 /* Don't enqueue socket while already enqueued */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 dprintk("svc: socket %p busy, not enqueued\n", svsk->sk_sk);
262 goto out_unlock;
263 }
Greg Banks3262c812006-10-02 02:17:58 -0700264 BUG_ON(svsk->sk_pool != NULL);
265 svsk->sk_pool = pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700268 if (((atomic_read(&svsk->sk_reserved) + serv->sv_max_mesg)*2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 > svc_sock_wspace(svsk))
270 && !test_bit(SK_CLOSE, &svsk->sk_flags)
271 && !test_bit(SK_CONN, &svsk->sk_flags)) {
272 /* Don't enqueue while not enough space for reply */
273 dprintk("svc: socket %p no space, %d*2 > %ld, not enqueued\n",
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700274 svsk->sk_sk, atomic_read(&svsk->sk_reserved)+serv->sv_max_mesg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 svc_sock_wspace(svsk));
Greg Banks3262c812006-10-02 02:17:58 -0700276 svsk->sk_pool = NULL;
Greg Banksc081a0c2006-10-02 02:17:57 -0700277 clear_bit(SK_BUSY, &svsk->sk_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 goto out_unlock;
279 }
280 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Greg Banks3262c812006-10-02 02:17:58 -0700283 if (!list_empty(&pool->sp_threads)) {
284 rqstp = list_entry(pool->sp_threads.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 struct svc_rqst,
286 rq_list);
287 dprintk("svc: socket %p served by daemon %p\n",
288 svsk->sk_sk, rqstp);
Greg Banks3262c812006-10-02 02:17:58 -0700289 svc_thread_dequeue(pool, rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (rqstp->rq_sock)
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800291 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 "svc_sock_enqueue: server %p, rq_sock=%p!\n",
293 rqstp, rqstp->rq_sock);
294 rqstp->rq_sock = svsk;
Greg Banksc45c3572006-10-02 02:17:54 -0700295 atomic_inc(&svsk->sk_inuse);
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700296 rqstp->rq_reserved = serv->sv_max_mesg;
Greg Banks5685f0f2006-10-02 02:17:56 -0700297 atomic_add(rqstp->rq_reserved, &svsk->sk_reserved);
Greg Banks3262c812006-10-02 02:17:58 -0700298 BUG_ON(svsk->sk_pool != pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 wake_up(&rqstp->rq_wait);
300 } else {
301 dprintk("svc: socket %p put into queue\n", svsk->sk_sk);
Greg Banks3262c812006-10-02 02:17:58 -0700302 list_add_tail(&svsk->sk_ready, &pool->sp_sockets);
303 BUG_ON(svsk->sk_pool != pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305
306out_unlock:
Greg Banks3262c812006-10-02 02:17:58 -0700307 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
310/*
Greg Banks3262c812006-10-02 02:17:58 -0700311 * Dequeue the first socket. Must be called with the pool->sp_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 */
313static inline struct svc_sock *
Greg Banks3262c812006-10-02 02:17:58 -0700314svc_sock_dequeue(struct svc_pool *pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 struct svc_sock *svsk;
317
Greg Banks3262c812006-10-02 02:17:58 -0700318 if (list_empty(&pool->sp_sockets))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return NULL;
320
Greg Banks3262c812006-10-02 02:17:58 -0700321 svsk = list_entry(pool->sp_sockets.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 struct svc_sock, sk_ready);
323 list_del_init(&svsk->sk_ready);
324
325 dprintk("svc: socket %p dequeued, inuse=%d\n",
Greg Banksc45c3572006-10-02 02:17:54 -0700326 svsk->sk_sk, atomic_read(&svsk->sk_inuse));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 return svsk;
329}
330
331/*
332 * Having read something from a socket, check whether it
333 * needs to be re-enqueued.
334 * Note: SK_DATA only gets cleared when a read-attempt finds
335 * no (or insufficient) data.
336 */
337static inline void
338svc_sock_received(struct svc_sock *svsk)
339{
Greg Banks3262c812006-10-02 02:17:58 -0700340 svsk->sk_pool = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 clear_bit(SK_BUSY, &svsk->sk_flags);
342 svc_sock_enqueue(svsk);
343}
344
345
346/**
347 * svc_reserve - change the space reserved for the reply to a request.
348 * @rqstp: The request in question
349 * @space: new max space to reserve
350 *
351 * Each request reserves some space on the output queue of the socket
352 * to make sure the reply fits. This function reduces that reserved
353 * space to be the amount of space used already, plus @space.
354 *
355 */
356void svc_reserve(struct svc_rqst *rqstp, int space)
357{
358 space += rqstp->rq_res.head[0].iov_len;
359
360 if (space < rqstp->rq_reserved) {
361 struct svc_sock *svsk = rqstp->rq_sock;
Greg Banks5685f0f2006-10-02 02:17:56 -0700362 atomic_sub((rqstp->rq_reserved - space), &svsk->sk_reserved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 rqstp->rq_reserved = space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 svc_sock_enqueue(svsk);
366 }
367}
368
369/*
370 * Release a socket after use.
371 */
372static inline void
373svc_sock_put(struct svc_sock *svsk)
374{
NeilBrownaaf68cf2007-02-08 14:20:30 -0800375 if (atomic_dec_and_test(&svsk->sk_inuse)) {
376 BUG_ON(! test_bit(SK_DEAD, &svsk->sk_flags));
377
Andrew Morton202dd452006-10-29 22:57:57 -0800378 dprintk("svc: releasing dead socket\n");
Neil Brownd6740df2006-10-29 22:46:45 -0800379 if (svsk->sk_sock->file)
380 sockfd_put(svsk->sk_sock);
381 else
382 sock_release(svsk->sk_sock);
383 if (svsk->sk_info_authunix != NULL)
384 svcauth_unix_info_release(svsk->sk_info_authunix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 kfree(svsk);
386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
389static void
390svc_sock_release(struct svc_rqst *rqstp)
391{
392 struct svc_sock *svsk = rqstp->rq_sock;
393
394 svc_release_skb(rqstp);
395
NeilBrown44524352006-10-04 02:15:46 -0700396 svc_free_res_pages(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 rqstp->rq_res.page_len = 0;
398 rqstp->rq_res.page_base = 0;
399
400
401 /* Reset response buffer and release
402 * the reservation.
403 * But first, check that enough space was reserved
404 * for the reply, otherwise we have a bug!
405 */
406 if ((rqstp->rq_res.len) > rqstp->rq_reserved)
407 printk(KERN_ERR "RPC request reserved %d but used %d\n",
408 rqstp->rq_reserved,
409 rqstp->rq_res.len);
410
411 rqstp->rq_res.head[0].iov_len = 0;
412 svc_reserve(rqstp, 0);
413 rqstp->rq_sock = NULL;
414
415 svc_sock_put(svsk);
416}
417
418/*
419 * External function to wake up a server waiting for data
Greg Banks3262c812006-10-02 02:17:58 -0700420 * This really only makes sense for services like lockd
421 * which have exactly one thread anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 */
423void
424svc_wake_up(struct svc_serv *serv)
425{
426 struct svc_rqst *rqstp;
Greg Banks3262c812006-10-02 02:17:58 -0700427 unsigned int i;
428 struct svc_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Greg Banks3262c812006-10-02 02:17:58 -0700430 for (i = 0; i < serv->sv_nrpools; i++) {
431 pool = &serv->sv_pools[i];
432
433 spin_lock_bh(&pool->sp_lock);
434 if (!list_empty(&pool->sp_threads)) {
435 rqstp = list_entry(pool->sp_threads.next,
436 struct svc_rqst,
437 rq_list);
438 dprintk("svc: daemon %p woken up.\n", rqstp);
439 /*
440 svc_thread_dequeue(pool, rqstp);
441 rqstp->rq_sock = NULL;
442 */
443 wake_up(&rqstp->rq_wait);
444 }
445 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
449/*
450 * Generic sendto routine
451 */
452static int
453svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
454{
455 struct svc_sock *svsk = rqstp->rq_sock;
456 struct socket *sock = svsk->sk_sock;
457 int slen;
458 char buffer[CMSG_SPACE(sizeof(struct in_pktinfo))];
459 struct cmsghdr *cmh = (struct cmsghdr *)buffer;
460 struct in_pktinfo *pki = (struct in_pktinfo *)CMSG_DATA(cmh);
461 int len = 0;
462 int result;
463 int size;
464 struct page **ppage = xdr->pages;
465 size_t base = xdr->page_base;
466 unsigned int pglen = xdr->page_len;
467 unsigned int flags = MSG_MORE;
Chuck Leverad06e4b2007-02-12 00:53:32 -0800468 char buf[RPC_MAX_ADDRBUFLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 slen = xdr->len;
471
472 if (rqstp->rq_prot == IPPROTO_UDP) {
473 /* set the source and destination */
474 struct msghdr msg;
475 msg.msg_name = &rqstp->rq_addr;
Chuck Lever27459f02007-02-12 00:53:34 -0800476 msg.msg_namelen = rqstp->rq_addrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 msg.msg_iov = NULL;
478 msg.msg_iovlen = 0;
479 msg.msg_flags = MSG_MORE;
480
481 msg.msg_control = cmh;
482 msg.msg_controllen = sizeof(buffer);
483 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
484 cmh->cmsg_level = SOL_IP;
485 cmh->cmsg_type = IP_PKTINFO;
486 pki->ipi_ifindex = 0;
487 pki->ipi_spec_dst.s_addr = rqstp->rq_daddr;
488
489 if (sock_sendmsg(sock, &msg, 0) < 0)
490 goto out;
491 }
492
493 /* send head */
494 if (slen == xdr->head[0].iov_len)
495 flags = 0;
NeilBrown44524352006-10-04 02:15:46 -0700496 len = kernel_sendpage(sock, rqstp->rq_respages[0], 0,
497 xdr->head[0].iov_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (len != xdr->head[0].iov_len)
499 goto out;
500 slen -= xdr->head[0].iov_len;
501 if (slen == 0)
502 goto out;
503
504 /* send page data */
505 size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
506 while (pglen > 0) {
507 if (slen == size)
508 flags = 0;
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700509 result = kernel_sendpage(sock, *ppage, base, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (result > 0)
511 len += result;
512 if (result != size)
513 goto out;
514 slen -= size;
515 pglen -= size;
516 size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
517 base = 0;
518 ppage++;
519 }
520 /* send tail */
521 if (xdr->tail[0].iov_len) {
NeilBrown44524352006-10-04 02:15:46 -0700522 result = kernel_sendpage(sock, rqstp->rq_respages[0],
523 ((unsigned long)xdr->tail[0].iov_base)
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800524 & (PAGE_SIZE-1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 xdr->tail[0].iov_len, 0);
526
527 if (result > 0)
528 len += result;
529 }
530out:
Chuck Leverad06e4b2007-02-12 00:53:32 -0800531 dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %s)\n",
532 rqstp->rq_sock, xdr->head[0].iov_base, xdr->head[0].iov_len,
533 xdr->len, len, svc_print_addr(rqstp, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 return len;
536}
537
538/*
NeilBrown80212d52006-10-02 02:17:47 -0700539 * Report socket names for nfsdfs
540 */
541static int one_sock_name(char *buf, struct svc_sock *svsk)
542{
543 int len;
544
545 switch(svsk->sk_sk->sk_family) {
546 case AF_INET:
547 len = sprintf(buf, "ipv4 %s %u.%u.%u.%u %d\n",
548 svsk->sk_sk->sk_protocol==IPPROTO_UDP?
549 "udp" : "tcp",
550 NIPQUAD(inet_sk(svsk->sk_sk)->rcv_saddr),
551 inet_sk(svsk->sk_sk)->num);
552 break;
553 default:
554 len = sprintf(buf, "*unknown-%d*\n",
555 svsk->sk_sk->sk_family);
556 }
557 return len;
558}
559
560int
NeilBrownb41b66d2006-10-02 02:17:48 -0700561svc_sock_names(char *buf, struct svc_serv *serv, char *toclose)
NeilBrown80212d52006-10-02 02:17:47 -0700562{
NeilBrownb41b66d2006-10-02 02:17:48 -0700563 struct svc_sock *svsk, *closesk = NULL;
NeilBrown80212d52006-10-02 02:17:47 -0700564 int len = 0;
565
566 if (!serv)
567 return 0;
NeilBrownaaf68cf2007-02-08 14:20:30 -0800568 spin_lock_bh(&serv->sv_lock);
NeilBrown80212d52006-10-02 02:17:47 -0700569 list_for_each_entry(svsk, &serv->sv_permsocks, sk_list) {
570 int onelen = one_sock_name(buf+len, svsk);
NeilBrownb41b66d2006-10-02 02:17:48 -0700571 if (toclose && strcmp(toclose, buf+len) == 0)
572 closesk = svsk;
573 else
574 len += onelen;
NeilBrown80212d52006-10-02 02:17:47 -0700575 }
NeilBrownaaf68cf2007-02-08 14:20:30 -0800576 spin_unlock_bh(&serv->sv_lock);
NeilBrownb41b66d2006-10-02 02:17:48 -0700577 if (closesk)
NeilBrown5680c442006-10-04 02:15:45 -0700578 /* Should unregister with portmap, but you cannot
579 * unregister just one protocol...
580 */
NeilBrownaaf68cf2007-02-08 14:20:30 -0800581 svc_close_socket(closesk);
NeilBrown37a03472006-10-04 02:15:44 -0700582 else if (toclose)
583 return -ENOENT;
NeilBrown80212d52006-10-02 02:17:47 -0700584 return len;
585}
586EXPORT_SYMBOL(svc_sock_names);
587
588/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 * Check input queue length
590 */
591static int
592svc_recv_available(struct svc_sock *svsk)
593{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 struct socket *sock = svsk->sk_sock;
595 int avail, err;
596
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700597 err = kernel_sock_ioctl(sock, TIOCINQ, (unsigned long) &avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 return (err >= 0)? avail : err;
600}
601
602/*
603 * Generic recvfrom routine.
604 */
605static int
606svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr, int buflen)
607{
Chuck Lever067d7812007-02-12 00:53:30 -0800608 struct svc_sock *svsk = rqstp->rq_sock;
Chuck Lever1ba95102007-02-12 00:53:31 -0800609 struct msghdr msg = {
610 .msg_flags = MSG_DONTWAIT,
611 };
612 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Chuck Lever1ba95102007-02-12 00:53:31 -0800614 len = kernel_recvmsg(svsk->sk_sock, &msg, iov, nr, buflen,
615 msg.msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 /* sock_recvmsg doesn't fill in the name/namelen, so we must..
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 */
Chuck Lever067d7812007-02-12 00:53:30 -0800619 memcpy(&rqstp->rq_addr, &svsk->sk_remote, svsk->sk_remotelen);
620 rqstp->rq_addrlen = svsk->sk_remotelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
Chuck Lever1ba95102007-02-12 00:53:31 -0800623 svsk, iov[0].iov_base, iov[0].iov_len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 return len;
626}
627
628/*
629 * Set socket snd and rcv buffer lengths
630 */
631static inline void
632svc_sock_setbufsize(struct socket *sock, unsigned int snd, unsigned int rcv)
633{
634#if 0
635 mm_segment_t oldfs;
636 oldfs = get_fs(); set_fs(KERNEL_DS);
637 sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
638 (char*)&snd, sizeof(snd));
639 sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
640 (char*)&rcv, sizeof(rcv));
641#else
642 /* sock_setsockopt limits use to sysctl_?mem_max,
643 * which isn't acceptable. Until that is made conditional
644 * on not having CAP_SYS_RESOURCE or similar, we go direct...
645 * DaveM said I could!
646 */
647 lock_sock(sock->sk);
648 sock->sk->sk_sndbuf = snd * 2;
649 sock->sk->sk_rcvbuf = rcv * 2;
650 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK|SOCK_RCVBUF_LOCK;
651 release_sock(sock->sk);
652#endif
653}
654/*
655 * INET callback when data has been received on the socket.
656 */
657static void
658svc_udp_data_ready(struct sock *sk, int count)
659{
Neil Brown939bb7e2005-09-13 01:25:39 -0700660 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Neil Brown939bb7e2005-09-13 01:25:39 -0700662 if (svsk) {
663 dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
664 svsk, sk, count, test_bit(SK_BUSY, &svsk->sk_flags));
665 set_bit(SK_DATA, &svsk->sk_flags);
666 svc_sock_enqueue(svsk);
667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
669 wake_up_interruptible(sk->sk_sleep);
670}
671
672/*
673 * INET callback when space is newly available on the socket.
674 */
675static void
676svc_write_space(struct sock *sk)
677{
678 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
679
680 if (svsk) {
681 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
682 svsk, sk, test_bit(SK_BUSY, &svsk->sk_flags));
683 svc_sock_enqueue(svsk);
684 }
685
686 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) {
Neil Brown939bb7e2005-09-13 01:25:39 -0700687 dprintk("RPC svc_write_space: someone sleeping on %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 svsk);
689 wake_up_interruptible(sk->sk_sleep);
690 }
691}
692
693/*
694 * Receive a datagram from a UDP socket.
695 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696static int
697svc_udp_recvfrom(struct svc_rqst *rqstp)
698{
Chuck Lever27459f02007-02-12 00:53:34 -0800699 struct sockaddr_in *sin = svc_addr_in(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 struct svc_sock *svsk = rqstp->rq_sock;
701 struct svc_serv *serv = svsk->sk_server;
702 struct sk_buff *skb;
703 int err, len;
704
705 if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
706 /* udp sockets need large rcvbuf as all pending
707 * requests are still in that buffer. sndbuf must
708 * also be large enough that there is enough space
Greg Banks3262c812006-10-02 02:17:58 -0700709 * for one reply per thread. We count all threads
710 * rather than threads in a particular pool, which
711 * provides an upper bound on the number of threads
712 * which will access the socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 */
714 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700715 (serv->sv_nrthreads+3) * serv->sv_max_mesg,
716 (serv->sv_nrthreads+3) * serv->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
719 svc_sock_received(svsk);
720 return svc_deferred_recv(rqstp);
721 }
722
NeilBrownaaf68cf2007-02-08 14:20:30 -0800723 if (test_bit(SK_CLOSE, &svsk->sk_flags)) {
724 svc_delete_socket(svsk);
725 return 0;
726 }
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 clear_bit(SK_DATA, &svsk->sk_flags);
729 while ((skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err)) == NULL) {
730 if (err == -EAGAIN) {
731 svc_sock_received(svsk);
732 return err;
733 }
734 /* possibly an icmp error */
735 dprintk("svc: recvfrom returned error %d\n", -err);
736 }
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700737 if (skb->tstamp.off_sec == 0) {
738 struct timeval tv;
739
740 tv.tv_sec = xtime.tv_sec;
Andrew Morton4bcde032005-10-26 01:59:03 -0700741 tv.tv_usec = xtime.tv_nsec / NSEC_PER_USEC;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700742 skb_set_timestamp(skb, &tv);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800743 /* Don't enable netstamp, sunrpc doesn't
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 need that much accuracy */
745 }
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700746 skb_get_timestamp(skb, &svsk->sk_sk->sk_stamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 set_bit(SK_DATA, &svsk->sk_flags); /* there may be more data... */
748
749 /*
750 * Maybe more packets - kick another thread ASAP.
751 */
752 svc_sock_received(svsk);
753
754 len = skb->len - sizeof(struct udphdr);
755 rqstp->rq_arg.len = len;
756
757 rqstp->rq_prot = IPPROTO_UDP;
758
759 /* Get sender address */
Chuck Lever27459f02007-02-12 00:53:34 -0800760 sin->sin_family = AF_INET;
761 sin->sin_port = skb->h.uh->source;
762 sin->sin_addr.s_addr = skb->nh.iph->saddr;
763 rqstp->rq_addrlen = sizeof(struct sockaddr_in);
764
765 /* Remember which interface received this request */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 rqstp->rq_daddr = skb->nh.iph->daddr;
767
768 if (skb_is_nonlinear(skb)) {
769 /* we have to copy */
770 local_bh_disable();
771 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
772 local_bh_enable();
773 /* checksum error */
774 skb_free_datagram(svsk->sk_sk, skb);
775 return 0;
776 }
777 local_bh_enable();
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800778 skb_free_datagram(svsk->sk_sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 } else {
780 /* we can use it in-place */
781 rqstp->rq_arg.head[0].iov_base = skb->data + sizeof(struct udphdr);
782 rqstp->rq_arg.head[0].iov_len = len;
Herbert Xufb286bb2005-11-10 13:01:24 -0800783 if (skb_checksum_complete(skb)) {
784 skb_free_datagram(svsk->sk_sk, skb);
785 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787 rqstp->rq_skbuff = skb;
788 }
789
790 rqstp->rq_arg.page_base = 0;
791 if (len <= rqstp->rq_arg.head[0].iov_len) {
792 rqstp->rq_arg.head[0].iov_len = len;
793 rqstp->rq_arg.page_len = 0;
NeilBrown44524352006-10-04 02:15:46 -0700794 rqstp->rq_respages = rqstp->rq_pages+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 } else {
796 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
NeilBrown44524352006-10-04 02:15:46 -0700797 rqstp->rq_respages = rqstp->rq_pages + 1 +
798 (rqstp->rq_arg.page_len + PAGE_SIZE - 1)/ PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
800
801 if (serv->sv_stats)
802 serv->sv_stats->netudpcnt++;
803
804 return len;
805}
806
807static int
808svc_udp_sendto(struct svc_rqst *rqstp)
809{
810 int error;
811
812 error = svc_sendto(rqstp, &rqstp->rq_res);
813 if (error == -ECONNREFUSED)
814 /* ICMP error on earlier request. */
815 error = svc_sendto(rqstp, &rqstp->rq_res);
816
817 return error;
818}
819
820static void
821svc_udp_init(struct svc_sock *svsk)
822{
823 svsk->sk_sk->sk_data_ready = svc_udp_data_ready;
824 svsk->sk_sk->sk_write_space = svc_write_space;
825 svsk->sk_recvfrom = svc_udp_recvfrom;
826 svsk->sk_sendto = svc_udp_sendto;
827
828 /* initialise setting must have enough space to
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800829 * receive and respond to one request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 * svc_udp_recvfrom will re-adjust if necessary
831 */
832 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700833 3 * svsk->sk_server->sv_max_mesg,
834 3 * svsk->sk_server->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 set_bit(SK_DATA, &svsk->sk_flags); /* might have come in before data_ready set up */
837 set_bit(SK_CHNGBUF, &svsk->sk_flags);
838}
839
840/*
841 * A data_ready event on a listening socket means there's a connection
842 * pending. Do not use state_change as a substitute for it.
843 */
844static void
845svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
846{
Neil Brown939bb7e2005-09-13 01:25:39 -0700847 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 dprintk("svc: socket %p TCP (listen) state change %d\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700850 sk, sk->sk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Neil Brown939bb7e2005-09-13 01:25:39 -0700852 /*
853 * This callback may called twice when a new connection
854 * is established as a child socket inherits everything
855 * from a parent LISTEN socket.
856 * 1) data_ready method of the parent socket will be called
857 * when one of child sockets become ESTABLISHED.
858 * 2) data_ready method of the child socket may be called
859 * when it receives data before the socket is accepted.
860 * In case of 2, we should ignore it silently.
861 */
862 if (sk->sk_state == TCP_LISTEN) {
863 if (svsk) {
864 set_bit(SK_CONN, &svsk->sk_flags);
865 svc_sock_enqueue(svsk);
866 } else
867 printk("svc: socket %p: no user data\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
Neil Brown939bb7e2005-09-13 01:25:39 -0700869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
871 wake_up_interruptible_all(sk->sk_sleep);
872}
873
874/*
875 * A state change on a connected socket means it's dying or dead.
876 */
877static void
878svc_tcp_state_change(struct sock *sk)
879{
Neil Brown939bb7e2005-09-13 01:25:39 -0700880 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700883 sk, sk->sk_state, sk->sk_user_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Neil Brown939bb7e2005-09-13 01:25:39 -0700885 if (!svsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 printk("svc: socket %p: no user data\n", sk);
Neil Brown939bb7e2005-09-13 01:25:39 -0700887 else {
888 set_bit(SK_CLOSE, &svsk->sk_flags);
889 svc_sock_enqueue(svsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
892 wake_up_interruptible_all(sk->sk_sleep);
893}
894
895static void
896svc_tcp_data_ready(struct sock *sk, int count)
897{
Neil Brown939bb7e2005-09-13 01:25:39 -0700898 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 dprintk("svc: socket %p TCP data ready (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700901 sk, sk->sk_user_data);
902 if (svsk) {
903 set_bit(SK_DATA, &svsk->sk_flags);
904 svc_sock_enqueue(svsk);
905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
907 wake_up_interruptible(sk->sk_sleep);
908}
909
910/*
911 * Accept a TCP connection
912 */
913static void
914svc_tcp_accept(struct svc_sock *svsk)
915{
916 struct sockaddr_in sin;
917 struct svc_serv *serv = svsk->sk_server;
918 struct socket *sock = svsk->sk_sock;
919 struct socket *newsock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 struct svc_sock *newsvsk;
921 int err, slen;
Chuck Leverad06e4b2007-02-12 00:53:32 -0800922 char buf[RPC_MAX_ADDRBUFLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
925 if (!sock)
926 return;
927
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700928 clear_bit(SK_CONN, &svsk->sk_flags);
929 err = kernel_accept(sock, &newsock, O_NONBLOCK);
930 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (err == -ENOMEM)
932 printk(KERN_WARNING "%s: no more sockets!\n",
933 serv->sv_name);
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700934 else if (err != -EAGAIN && net_ratelimit())
935 printk(KERN_WARNING "%s: accept failed (err %d)!\n",
936 serv->sv_name, -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return;
938 }
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 set_bit(SK_CONN, &svsk->sk_flags);
941 svc_sock_enqueue(svsk);
942
943 slen = sizeof(sin);
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700944 err = kernel_getpeername(newsock, (struct sockaddr *) &sin, &slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (err < 0) {
946 if (net_ratelimit())
947 printk(KERN_WARNING "%s: peername failed (err %d)!\n",
948 serv->sv_name, -err);
949 goto failed; /* aborted connection or whatever */
950 }
951
952 /* Ideally, we would want to reject connections from unauthorized
Chuck Leverad06e4b2007-02-12 00:53:32 -0800953 * hosts here, but when we get encryption, the IP of the host won't
954 * tell us anything. For now just warn about unpriv connections.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 */
956 if (ntohs(sin.sin_port) >= 1024) {
957 dprintk(KERN_WARNING
Chuck Leverad06e4b2007-02-12 00:53:32 -0800958 "%s: connect from unprivileged port: %s\n",
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800959 serv->sv_name,
Chuck Leverad06e4b2007-02-12 00:53:32 -0800960 __svc_print_addr((struct sockaddr *) &sin, buf,
961 sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
Chuck Leverad06e4b2007-02-12 00:53:32 -0800963 dprintk("%s: connect from %s\n", serv->sv_name,
964 __svc_print_addr((struct sockaddr *) &sin, buf,
965 sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 /* make sure that a write doesn't block forever when
968 * low on memory
969 */
970 newsock->sk->sk_sndtimeo = HZ*30;
971
Chuck Lever6b174332007-02-12 00:53:28 -0800972 if (!(newsvsk = svc_setup_socket(serv, newsock, &err,
973 (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 goto failed;
Chuck Lever067d7812007-02-12 00:53:30 -0800975 memcpy(&newsvsk->sk_remote, &sin, slen);
976 newsvsk->sk_remotelen = slen;
977
NeilBrowne79eff12007-02-12 00:53:30 -0800978 svc_sock_received(newsvsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 /* make sure that we don't have too many active connections.
981 * If we have, something must be dropped.
982 *
983 * There's no point in trying to do random drop here for
984 * DoS prevention. The NFS clients does 1 reconnect in 15
985 * seconds. An attacker can easily beat that.
986 *
987 * The only somewhat efficient mechanism would be if drop
988 * old connections from the same IP first. But right now
989 * we don't even record the client IP in svc_sock.
990 */
991 if (serv->sv_tmpcnt > (serv->sv_nrthreads+3)*20) {
992 struct svc_sock *svsk = NULL;
993 spin_lock_bh(&serv->sv_lock);
994 if (!list_empty(&serv->sv_tempsocks)) {
995 if (net_ratelimit()) {
996 /* Try to help the admin */
997 printk(KERN_NOTICE "%s: too many open TCP "
998 "sockets, consider increasing the "
999 "number of nfsd threads\n",
1000 serv->sv_name);
Chuck Leverad06e4b2007-02-12 00:53:32 -08001001 printk(KERN_NOTICE
1002 "%s: last TCP connect from %s\n",
1003 serv->sv_name, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
1005 /*
1006 * Always select the oldest socket. It's not fair,
1007 * but so is life
1008 */
1009 svsk = list_entry(serv->sv_tempsocks.prev,
1010 struct svc_sock,
1011 sk_list);
1012 set_bit(SK_CLOSE, &svsk->sk_flags);
Greg Banksc45c3572006-10-02 02:17:54 -07001013 atomic_inc(&svsk->sk_inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015 spin_unlock_bh(&serv->sv_lock);
1016
1017 if (svsk) {
1018 svc_sock_enqueue(svsk);
1019 svc_sock_put(svsk);
1020 }
1021
1022 }
1023
1024 if (serv->sv_stats)
1025 serv->sv_stats->nettcpconn++;
1026
1027 return;
1028
1029failed:
1030 sock_release(newsock);
1031 return;
1032}
1033
1034/*
1035 * Receive data from a TCP socket.
1036 */
1037static int
1038svc_tcp_recvfrom(struct svc_rqst *rqstp)
1039{
1040 struct svc_sock *svsk = rqstp->rq_sock;
1041 struct svc_serv *serv = svsk->sk_server;
1042 int len;
NeilBrown3cc03b12006-10-04 02:15:47 -07001043 struct kvec *vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 int pnum, vlen;
1045
1046 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
1047 svsk, test_bit(SK_DATA, &svsk->sk_flags),
1048 test_bit(SK_CONN, &svsk->sk_flags),
1049 test_bit(SK_CLOSE, &svsk->sk_flags));
1050
1051 if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
1052 svc_sock_received(svsk);
1053 return svc_deferred_recv(rqstp);
1054 }
1055
1056 if (test_bit(SK_CLOSE, &svsk->sk_flags)) {
1057 svc_delete_socket(svsk);
1058 return 0;
1059 }
1060
NeilBrown1a047062006-10-19 23:29:13 -07001061 if (svsk->sk_sk->sk_state == TCP_LISTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 svc_tcp_accept(svsk);
1063 svc_sock_received(svsk);
1064 return 0;
1065 }
1066
1067 if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
1068 /* sndbuf needs to have room for one request
1069 * per thread, otherwise we can stall even when the
1070 * network isn't a bottleneck.
Greg Banks3262c812006-10-02 02:17:58 -07001071 *
1072 * We count all threads rather than threads in a
1073 * particular pool, which provides an upper bound
1074 * on the number of threads which will access the socket.
1075 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 * rcvbuf just needs to be able to hold a few requests.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001077 * Normally they will be removed from the queue
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 * as soon a a complete request arrives.
1079 */
1080 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001081 (serv->sv_nrthreads+3) * serv->sv_max_mesg,
1082 3 * serv->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 clear_bit(SK_DATA, &svsk->sk_flags);
1085
1086 /* Receive data. If we haven't got the record length yet, get
1087 * the next four bytes. Otherwise try to gobble up as much as
1088 * possible up to the complete record length.
1089 */
1090 if (svsk->sk_tcplen < 4) {
1091 unsigned long want = 4 - svsk->sk_tcplen;
1092 struct kvec iov;
1093
1094 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
1095 iov.iov_len = want;
1096 if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
1097 goto error;
1098 svsk->sk_tcplen += len;
1099
1100 if (len < want) {
1101 dprintk("svc: short recvfrom while reading record length (%d of %lu)\n",
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001102 len, want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 svc_sock_received(svsk);
1104 return -EAGAIN; /* record header not complete */
1105 }
1106
1107 svsk->sk_reclen = ntohl(svsk->sk_reclen);
1108 if (!(svsk->sk_reclen & 0x80000000)) {
1109 /* FIXME: technically, a record can be fragmented,
1110 * and non-terminal fragments will not have the top
1111 * bit set in the fragment length header.
1112 * But apparently no known nfs clients send fragmented
1113 * records. */
NeilBrown34e9a632007-01-29 13:19:52 -08001114 if (net_ratelimit())
1115 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx"
1116 " (non-terminal)\n",
1117 (unsigned long) svsk->sk_reclen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 goto err_delete;
1119 }
1120 svsk->sk_reclen &= 0x7fffffff;
1121 dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001122 if (svsk->sk_reclen > serv->sv_max_mesg) {
NeilBrown34e9a632007-01-29 13:19:52 -08001123 if (net_ratelimit())
1124 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx"
1125 " (large)\n",
1126 (unsigned long) svsk->sk_reclen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 goto err_delete;
1128 }
1129 }
1130
1131 /* Check whether enough data is available */
1132 len = svc_recv_available(svsk);
1133 if (len < 0)
1134 goto error;
1135
1136 if (len < svsk->sk_reclen) {
1137 dprintk("svc: incomplete TCP record (%d of %d)\n",
1138 len, svsk->sk_reclen);
1139 svc_sock_received(svsk);
1140 return -EAGAIN; /* record not complete */
1141 }
1142 len = svsk->sk_reclen;
1143 set_bit(SK_DATA, &svsk->sk_flags);
1144
NeilBrown3cc03b12006-10-04 02:15:47 -07001145 vec = rqstp->rq_vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 vec[0] = rqstp->rq_arg.head[0];
1147 vlen = PAGE_SIZE;
1148 pnum = 1;
1149 while (vlen < len) {
NeilBrown44524352006-10-04 02:15:46 -07001150 vec[pnum].iov_base = page_address(rqstp->rq_pages[pnum]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 vec[pnum].iov_len = PAGE_SIZE;
1152 pnum++;
1153 vlen += PAGE_SIZE;
1154 }
NeilBrown44524352006-10-04 02:15:46 -07001155 rqstp->rq_respages = &rqstp->rq_pages[pnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 /* Now receive data */
1158 len = svc_recvfrom(rqstp, vec, pnum, len);
1159 if (len < 0)
1160 goto error;
1161
1162 dprintk("svc: TCP complete record (%d bytes)\n", len);
1163 rqstp->rq_arg.len = len;
1164 rqstp->rq_arg.page_base = 0;
1165 if (len <= rqstp->rq_arg.head[0].iov_len) {
1166 rqstp->rq_arg.head[0].iov_len = len;
1167 rqstp->rq_arg.page_len = 0;
1168 } else {
1169 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
1170 }
1171
1172 rqstp->rq_skbuff = NULL;
1173 rqstp->rq_prot = IPPROTO_TCP;
1174
1175 /* Reset TCP read info */
1176 svsk->sk_reclen = 0;
1177 svsk->sk_tcplen = 0;
1178
1179 svc_sock_received(svsk);
1180 if (serv->sv_stats)
1181 serv->sv_stats->nettcpcnt++;
1182
1183 return len;
1184
1185 err_delete:
1186 svc_delete_socket(svsk);
1187 return -EAGAIN;
1188
1189 error:
1190 if (len == -EAGAIN) {
1191 dprintk("RPC: TCP recvfrom got EAGAIN\n");
1192 svc_sock_received(svsk);
1193 } else {
1194 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
1195 svsk->sk_server->sv_name, -len);
Olaf Kirch93fbf1a2006-01-06 00:19:56 -08001196 goto err_delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
1198
1199 return len;
1200}
1201
1202/*
1203 * Send out data on TCP socket.
1204 */
1205static int
1206svc_tcp_sendto(struct svc_rqst *rqstp)
1207{
1208 struct xdr_buf *xbufp = &rqstp->rq_res;
1209 int sent;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001210 __be32 reclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 /* Set up the first element of the reply kvec.
1213 * Any other kvecs that may be in use have been taken
1214 * care of by the server implementation itself.
1215 */
1216 reclen = htonl(0x80000000|((xbufp->len ) - 4));
1217 memcpy(xbufp->head[0].iov_base, &reclen, 4);
1218
1219 if (test_bit(SK_DEAD, &rqstp->rq_sock->sk_flags))
1220 return -ENOTCONN;
1221
1222 sent = svc_sendto(rqstp, &rqstp->rq_res);
1223 if (sent != xbufp->len) {
1224 printk(KERN_NOTICE "rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n",
1225 rqstp->rq_sock->sk_server->sv_name,
1226 (sent<0)?"got error":"sent only",
1227 sent, xbufp->len);
NeilBrownaaf68cf2007-02-08 14:20:30 -08001228 set_bit(SK_CLOSE, &rqstp->rq_sock->sk_flags);
1229 svc_sock_enqueue(rqstp->rq_sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 sent = -EAGAIN;
1231 }
1232 return sent;
1233}
1234
1235static void
1236svc_tcp_init(struct svc_sock *svsk)
1237{
1238 struct sock *sk = svsk->sk_sk;
1239 struct tcp_sock *tp = tcp_sk(sk);
1240
1241 svsk->sk_recvfrom = svc_tcp_recvfrom;
1242 svsk->sk_sendto = svc_tcp_sendto;
1243
1244 if (sk->sk_state == TCP_LISTEN) {
1245 dprintk("setting up TCP socket for listening\n");
1246 sk->sk_data_ready = svc_tcp_listen_data_ready;
1247 set_bit(SK_CONN, &svsk->sk_flags);
1248 } else {
1249 dprintk("setting up TCP socket for reading\n");
1250 sk->sk_state_change = svc_tcp_state_change;
1251 sk->sk_data_ready = svc_tcp_data_ready;
1252 sk->sk_write_space = svc_write_space;
1253
1254 svsk->sk_reclen = 0;
1255 svsk->sk_tcplen = 0;
1256
1257 tp->nonagle = 1; /* disable Nagle's algorithm */
1258
1259 /* initialise setting must have enough space to
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001260 * receive and respond to one request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 * svc_tcp_recvfrom will re-adjust if necessary
1262 */
1263 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001264 3 * svsk->sk_server->sv_max_mesg,
1265 3 * svsk->sk_server->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1268 set_bit(SK_DATA, &svsk->sk_flags);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001269 if (sk->sk_state != TCP_ESTABLISHED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 set_bit(SK_CLOSE, &svsk->sk_flags);
1271 }
1272}
1273
1274void
1275svc_sock_update_bufs(struct svc_serv *serv)
1276{
1277 /*
1278 * The number of server threads has changed. Update
1279 * rcvbuf and sndbuf accordingly on all sockets
1280 */
1281 struct list_head *le;
1282
1283 spin_lock_bh(&serv->sv_lock);
1284 list_for_each(le, &serv->sv_permsocks) {
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001285 struct svc_sock *svsk =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 list_entry(le, struct svc_sock, sk_list);
1287 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1288 }
1289 list_for_each(le, &serv->sv_tempsocks) {
1290 struct svc_sock *svsk =
1291 list_entry(le, struct svc_sock, sk_list);
1292 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1293 }
1294 spin_unlock_bh(&serv->sv_lock);
1295}
1296
1297/*
Greg Banks3262c812006-10-02 02:17:58 -07001298 * Receive the next request on any socket. This code is carefully
1299 * organised not to touch any cachelines in the shared svc_serv
1300 * structure, only cachelines in the local svc_pool.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 */
1302int
NeilBrown6fb2b472006-10-02 02:17:50 -07001303svc_recv(struct svc_rqst *rqstp, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
Chuck Lever27459f02007-02-12 00:53:34 -08001305 struct svc_sock *svsk = NULL;
1306 struct sockaddr_in *sin = svc_addr_in(rqstp);
NeilBrown6fb2b472006-10-02 02:17:50 -07001307 struct svc_serv *serv = rqstp->rq_server;
Greg Banks3262c812006-10-02 02:17:58 -07001308 struct svc_pool *pool = rqstp->rq_pool;
NeilBrown44524352006-10-04 02:15:46 -07001309 int len, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 int pages;
1311 struct xdr_buf *arg;
1312 DECLARE_WAITQUEUE(wait, current);
1313
1314 dprintk("svc: server %p waiting for data (to = %ld)\n",
1315 rqstp, timeout);
1316
1317 if (rqstp->rq_sock)
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001318 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 "svc_recv: service %p, socket not NULL!\n",
1320 rqstp);
1321 if (waitqueue_active(&rqstp->rq_wait))
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001322 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 "svc_recv: service %p, wait queue active!\n",
1324 rqstp);
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 /* now allocate needed pages. If we get a failure, sleep briefly */
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001328 pages = (serv->sv_max_mesg + PAGE_SIZE) / PAGE_SIZE;
NeilBrown44524352006-10-04 02:15:46 -07001329 for (i=0; i < pages ; i++)
1330 while (rqstp->rq_pages[i] == NULL) {
1331 struct page *p = alloc_page(GFP_KERNEL);
1332 if (!p)
1333 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
1334 rqstp->rq_pages[i] = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 }
NeilBrown250f3912007-01-26 00:56:59 -08001336 rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */
1337 BUG_ON(pages >= RPCSVC_MAXPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 /* Make arg->head point to first page and arg->pages point to rest */
1340 arg = &rqstp->rq_arg;
NeilBrown44524352006-10-04 02:15:46 -07001341 arg->head[0].iov_base = page_address(rqstp->rq_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 arg->head[0].iov_len = PAGE_SIZE;
NeilBrown44524352006-10-04 02:15:46 -07001343 arg->pages = rqstp->rq_pages + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 arg->page_base = 0;
1345 /* save at least one page for response */
1346 arg->page_len = (pages-2)*PAGE_SIZE;
1347 arg->len = (pages-1)*PAGE_SIZE;
1348 arg->tail[0].iov_len = 0;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001349
1350 try_to_freeze();
NeilBrown1887b932005-11-15 00:09:10 -08001351 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (signalled())
1353 return -EINTR;
1354
Greg Banks3262c812006-10-02 02:17:58 -07001355 spin_lock_bh(&pool->sp_lock);
1356 if ((svsk = svc_sock_dequeue(pool)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 rqstp->rq_sock = svsk;
Greg Banksc45c3572006-10-02 02:17:54 -07001358 atomic_inc(&svsk->sk_inuse);
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001359 rqstp->rq_reserved = serv->sv_max_mesg;
Greg Banks5685f0f2006-10-02 02:17:56 -07001360 atomic_add(rqstp->rq_reserved, &svsk->sk_reserved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 } else {
1362 /* No data pending. Go to sleep */
Greg Banks3262c812006-10-02 02:17:58 -07001363 svc_thread_enqueue(pool, rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 /*
1366 * We have to be able to interrupt this wait
1367 * to bring down the daemons ...
1368 */
1369 set_current_state(TASK_INTERRUPTIBLE);
1370 add_wait_queue(&rqstp->rq_wait, &wait);
Greg Banks3262c812006-10-02 02:17:58 -07001371 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 schedule_timeout(timeout);
1374
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001375 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Greg Banks3262c812006-10-02 02:17:58 -07001377 spin_lock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 remove_wait_queue(&rqstp->rq_wait, &wait);
1379
1380 if (!(svsk = rqstp->rq_sock)) {
Greg Banks3262c812006-10-02 02:17:58 -07001381 svc_thread_dequeue(pool, rqstp);
1382 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 dprintk("svc: server %p, no data yet\n", rqstp);
1384 return signalled()? -EINTR : -EAGAIN;
1385 }
1386 }
Greg Banks3262c812006-10-02 02:17:58 -07001387 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Greg Banks3262c812006-10-02 02:17:58 -07001389 dprintk("svc: server %p, pool %u, socket %p, inuse=%d\n",
1390 rqstp, pool->sp_id, svsk, atomic_read(&svsk->sk_inuse));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 len = svsk->sk_recvfrom(rqstp);
1392 dprintk("svc: got len=%d\n", len);
1393
1394 /* No data, incomplete (TCP) read, or accept() */
1395 if (len == 0 || len == -EAGAIN) {
1396 rqstp->rq_res.len = 0;
1397 svc_sock_release(rqstp);
1398 return -EAGAIN;
1399 }
1400 svsk->sk_lastrecv = get_seconds();
Greg Banks36bdfc82006-10-02 02:17:54 -07001401 clear_bit(SK_OLD, &svsk->sk_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Chuck Lever27459f02007-02-12 00:53:34 -08001403 rqstp->rq_secure = ntohs(sin->sin_port) < PROT_SOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 rqstp->rq_chandle.defer = svc_defer;
1405
1406 if (serv->sv_stats)
1407 serv->sv_stats->netcnt++;
1408 return len;
1409}
1410
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001411/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 * Drop request
1413 */
1414void
1415svc_drop(struct svc_rqst *rqstp)
1416{
1417 dprintk("svc: socket %p dropped request\n", rqstp->rq_sock);
1418 svc_sock_release(rqstp);
1419}
1420
1421/*
1422 * Return reply to client.
1423 */
1424int
1425svc_send(struct svc_rqst *rqstp)
1426{
1427 struct svc_sock *svsk;
1428 int len;
1429 struct xdr_buf *xb;
1430
1431 if ((svsk = rqstp->rq_sock) == NULL) {
1432 printk(KERN_WARNING "NULL socket pointer in %s:%d\n",
1433 __FILE__, __LINE__);
1434 return -EFAULT;
1435 }
1436
1437 /* release the receive skb before sending the reply */
1438 svc_release_skb(rqstp);
1439
1440 /* calculate over-all length */
1441 xb = & rqstp->rq_res;
1442 xb->len = xb->head[0].iov_len +
1443 xb->page_len +
1444 xb->tail[0].iov_len;
1445
Ingo Molnar57b47a52006-03-20 22:35:41 -08001446 /* Grab svsk->sk_mutex to serialize outgoing data. */
1447 mutex_lock(&svsk->sk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (test_bit(SK_DEAD, &svsk->sk_flags))
1449 len = -ENOTCONN;
1450 else
1451 len = svsk->sk_sendto(rqstp);
Ingo Molnar57b47a52006-03-20 22:35:41 -08001452 mutex_unlock(&svsk->sk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 svc_sock_release(rqstp);
1454
1455 if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
1456 return 0;
1457 return len;
1458}
1459
1460/*
Greg Banks36bdfc82006-10-02 02:17:54 -07001461 * Timer function to close old temporary sockets, using
1462 * a mark-and-sweep algorithm.
1463 */
1464static void
1465svc_age_temp_sockets(unsigned long closure)
1466{
1467 struct svc_serv *serv = (struct svc_serv *)closure;
1468 struct svc_sock *svsk;
1469 struct list_head *le, *next;
1470 LIST_HEAD(to_be_aged);
1471
1472 dprintk("svc_age_temp_sockets\n");
1473
1474 if (!spin_trylock_bh(&serv->sv_lock)) {
1475 /* busy, try again 1 sec later */
1476 dprintk("svc_age_temp_sockets: busy\n");
1477 mod_timer(&serv->sv_temptimer, jiffies + HZ);
1478 return;
1479 }
1480
1481 list_for_each_safe(le, next, &serv->sv_tempsocks) {
1482 svsk = list_entry(le, struct svc_sock, sk_list);
1483
1484 if (!test_and_set_bit(SK_OLD, &svsk->sk_flags))
1485 continue;
Greg Banksc45c3572006-10-02 02:17:54 -07001486 if (atomic_read(&svsk->sk_inuse) || test_bit(SK_BUSY, &svsk->sk_flags))
Greg Banks36bdfc82006-10-02 02:17:54 -07001487 continue;
Greg Banksc45c3572006-10-02 02:17:54 -07001488 atomic_inc(&svsk->sk_inuse);
Greg Banks36bdfc82006-10-02 02:17:54 -07001489 list_move(le, &to_be_aged);
1490 set_bit(SK_CLOSE, &svsk->sk_flags);
1491 set_bit(SK_DETACHED, &svsk->sk_flags);
1492 }
1493 spin_unlock_bh(&serv->sv_lock);
1494
1495 while (!list_empty(&to_be_aged)) {
1496 le = to_be_aged.next;
1497 /* fiddling the sk_list node is safe 'cos we're SK_DETACHED */
1498 list_del_init(le);
1499 svsk = list_entry(le, struct svc_sock, sk_list);
1500
1501 dprintk("queuing svsk %p for closing, %lu seconds old\n",
1502 svsk, get_seconds() - svsk->sk_lastrecv);
1503
1504 /* a thread will dequeue and close it soon */
1505 svc_sock_enqueue(svsk);
1506 svc_sock_put(svsk);
1507 }
1508
1509 mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
1510}
1511
1512/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 * Initialize socket for RPC use and create svc_sock struct
1514 * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
1515 */
Chuck Lever6b174332007-02-12 00:53:28 -08001516static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1517 struct socket *sock,
1518 int *errp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
1520 struct svc_sock *svsk;
1521 struct sock *inet;
Chuck Lever6b174332007-02-12 00:53:28 -08001522 int pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
1523 int is_temporary = flags & SVC_SOCK_TEMPORARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525 dprintk("svc: svc_setup_socket %p\n", sock);
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001526 if (!(svsk = kzalloc(sizeof(*svsk), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 *errp = -ENOMEM;
1528 return NULL;
1529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531 inet = sock->sk;
1532
1533 /* Register socket with portmapper */
1534 if (*errp >= 0 && pmap_register)
1535 *errp = svc_register(serv, inet->sk_protocol,
1536 ntohs(inet_sk(inet)->sport));
1537
1538 if (*errp < 0) {
1539 kfree(svsk);
1540 return NULL;
1541 }
1542
1543 set_bit(SK_BUSY, &svsk->sk_flags);
1544 inet->sk_user_data = svsk;
1545 svsk->sk_sock = sock;
1546 svsk->sk_sk = inet;
1547 svsk->sk_ostate = inet->sk_state_change;
1548 svsk->sk_odata = inet->sk_data_ready;
1549 svsk->sk_owspace = inet->sk_write_space;
1550 svsk->sk_server = serv;
NeilBrownaaf68cf2007-02-08 14:20:30 -08001551 atomic_set(&svsk->sk_inuse, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 svsk->sk_lastrecv = get_seconds();
Greg Banks1a68d952006-10-02 02:17:55 -07001553 spin_lock_init(&svsk->sk_defer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 INIT_LIST_HEAD(&svsk->sk_deferred);
1555 INIT_LIST_HEAD(&svsk->sk_ready);
Ingo Molnar57b47a52006-03-20 22:35:41 -08001556 mutex_init(&svsk->sk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 /* Initialize the socket */
1559 if (sock->type == SOCK_DGRAM)
1560 svc_udp_init(svsk);
1561 else
1562 svc_tcp_init(svsk);
1563
1564 spin_lock_bh(&serv->sv_lock);
Chuck Lever6b174332007-02-12 00:53:28 -08001565 if (is_temporary) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 set_bit(SK_TEMP, &svsk->sk_flags);
1567 list_add(&svsk->sk_list, &serv->sv_tempsocks);
1568 serv->sv_tmpcnt++;
Greg Banks36bdfc82006-10-02 02:17:54 -07001569 if (serv->sv_temptimer.function == NULL) {
1570 /* setup timer to age temp sockets */
1571 setup_timer(&serv->sv_temptimer, svc_age_temp_sockets,
1572 (unsigned long)serv);
1573 mod_timer(&serv->sv_temptimer,
1574 jiffies + svc_conn_age_period * HZ);
1575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 } else {
1577 clear_bit(SK_TEMP, &svsk->sk_flags);
1578 list_add(&svsk->sk_list, &serv->sv_permsocks);
1579 }
1580 spin_unlock_bh(&serv->sv_lock);
1581
1582 dprintk("svc: svc_setup_socket created %p (inet %p)\n",
1583 svsk, svsk->sk_sk);
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return svsk;
1586}
1587
NeilBrownb41b66d2006-10-02 02:17:48 -07001588int svc_addsock(struct svc_serv *serv,
1589 int fd,
1590 char *name_return,
1591 int *proto)
1592{
1593 int err = 0;
1594 struct socket *so = sockfd_lookup(fd, &err);
1595 struct svc_sock *svsk = NULL;
1596
1597 if (!so)
1598 return err;
1599 if (so->sk->sk_family != AF_INET)
1600 err = -EAFNOSUPPORT;
1601 else if (so->sk->sk_protocol != IPPROTO_TCP &&
1602 so->sk->sk_protocol != IPPROTO_UDP)
1603 err = -EPROTONOSUPPORT;
1604 else if (so->state > SS_UNCONNECTED)
1605 err = -EISCONN;
1606 else {
Chuck Lever6b174332007-02-12 00:53:28 -08001607 svsk = svc_setup_socket(serv, so, &err, SVC_SOCK_DEFAULTS);
NeilBrowne79eff12007-02-12 00:53:30 -08001608 if (svsk) {
1609 svc_sock_received(svsk);
NeilBrownb41b66d2006-10-02 02:17:48 -07001610 err = 0;
NeilBrowne79eff12007-02-12 00:53:30 -08001611 }
NeilBrownb41b66d2006-10-02 02:17:48 -07001612 }
1613 if (err) {
1614 sockfd_put(so);
1615 return err;
1616 }
1617 if (proto) *proto = so->sk->sk_protocol;
1618 return one_sock_name(name_return, svsk);
1619}
1620EXPORT_SYMBOL_GPL(svc_addsock);
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622/*
1623 * Create socket for RPC service.
1624 */
Chuck Lever6b174332007-02-12 00:53:28 -08001625static int svc_create_socket(struct svc_serv *serv, int protocol,
1626 struct sockaddr_in *sin, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
1628 struct svc_sock *svsk;
1629 struct socket *sock;
1630 int error;
1631 int type;
Chuck Leverad06e4b2007-02-12 00:53:32 -08001632 char buf[RPC_MAX_ADDRBUFLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Chuck Leverad06e4b2007-02-12 00:53:32 -08001634 dprintk("svc: svc_create_socket(%s, %d, %s)\n",
1635 serv->sv_program->pg_name, protocol,
1636 __svc_print_addr((struct sockaddr *) sin, buf,
1637 sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
1639 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1640 printk(KERN_WARNING "svc: only UDP and TCP "
1641 "sockets supported\n");
1642 return -EINVAL;
1643 }
1644 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1645
1646 if ((error = sock_create_kern(PF_INET, type, protocol, &sock)) < 0)
1647 return error;
1648
Peter Zijlstraed075362006-12-06 20:35:24 -08001649 svc_reclassify_socket(sock);
1650
Eric Sesterhenn18114742006-09-28 14:37:07 -07001651 if (type == SOCK_STREAM)
1652 sock->sk->sk_reuse = 1; /* allow address reuse */
1653 error = kernel_bind(sock, (struct sockaddr *) sin,
1654 sizeof(*sin));
1655 if (error < 0)
1656 goto bummer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658 if (protocol == IPPROTO_TCP) {
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001659 if ((error = kernel_listen(sock, 64)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 goto bummer;
1661 }
1662
NeilBrowne79eff12007-02-12 00:53:30 -08001663 if ((svsk = svc_setup_socket(serv, sock, &error, flags)) != NULL) {
1664 svc_sock_received(svsk);
Chuck Lever6b174332007-02-12 00:53:28 -08001665 return ntohs(inet_sk(svsk->sk_sk)->sport);
NeilBrowne79eff12007-02-12 00:53:30 -08001666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668bummer:
1669 dprintk("svc: svc_create_socket error = %d\n", -error);
1670 sock_release(sock);
1671 return error;
1672}
1673
1674/*
1675 * Remove a dead socket
1676 */
NeilBrownaaf68cf2007-02-08 14:20:30 -08001677static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678svc_delete_socket(struct svc_sock *svsk)
1679{
1680 struct svc_serv *serv;
1681 struct sock *sk;
1682
1683 dprintk("svc: svc_delete_socket(%p)\n", svsk);
1684
1685 serv = svsk->sk_server;
1686 sk = svsk->sk_sk;
1687
1688 sk->sk_state_change = svsk->sk_ostate;
1689 sk->sk_data_ready = svsk->sk_odata;
1690 sk->sk_write_space = svsk->sk_owspace;
1691
1692 spin_lock_bh(&serv->sv_lock);
1693
Greg Banks36bdfc82006-10-02 02:17:54 -07001694 if (!test_and_set_bit(SK_DETACHED, &svsk->sk_flags))
1695 list_del_init(&svsk->sk_list);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001696 /*
Greg Banks3262c812006-10-02 02:17:58 -07001697 * We used to delete the svc_sock from whichever list
1698 * it's sk_ready node was on, but we don't actually
1699 * need to. This is because the only time we're called
1700 * while still attached to a queue, the queue itself
1701 * is about to be destroyed (in svc_destroy).
1702 */
NeilBrownaaf68cf2007-02-08 14:20:30 -08001703 if (!test_and_set_bit(SK_DEAD, &svsk->sk_flags)) {
1704 BUG_ON(atomic_read(&svsk->sk_inuse)<2);
1705 atomic_dec(&svsk->sk_inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (test_bit(SK_TEMP, &svsk->sk_flags))
1707 serv->sv_tmpcnt--;
NeilBrownaaf68cf2007-02-08 14:20:30 -08001708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Neil Brownd6740df2006-10-29 22:46:45 -08001710 spin_unlock_bh(&serv->sv_lock);
NeilBrownaaf68cf2007-02-08 14:20:30 -08001711}
1712
1713void svc_close_socket(struct svc_sock *svsk)
1714{
1715 set_bit(SK_CLOSE, &svsk->sk_flags);
1716 if (test_and_set_bit(SK_BUSY, &svsk->sk_flags))
1717 /* someone else will have to effect the close */
1718 return;
1719
1720 atomic_inc(&svsk->sk_inuse);
1721 svc_delete_socket(svsk);
1722 clear_bit(SK_BUSY, &svsk->sk_flags);
Neil Brownd6740df2006-10-29 22:46:45 -08001723 svc_sock_put(svsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
Chuck Lever6b174332007-02-12 00:53:28 -08001726/**
1727 * svc_makesock - Make a socket for nfsd and lockd
1728 * @serv: RPC server structure
1729 * @protocol: transport protocol to use
1730 * @port: port to use
Chuck Lever482fb942007-02-12 00:53:29 -08001731 * @flags: requested socket characteristics
Chuck Lever6b174332007-02-12 00:53:28 -08001732 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 */
Chuck Lever482fb942007-02-12 00:53:29 -08001734int svc_makesock(struct svc_serv *serv, int protocol, unsigned short port,
1735 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
Chuck Lever6b174332007-02-12 00:53:28 -08001737 struct sockaddr_in sin = {
1738 .sin_family = AF_INET,
1739 .sin_addr.s_addr = INADDR_ANY,
1740 .sin_port = htons(port),
1741 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
1743 dprintk("svc: creating socket proto = %d\n", protocol);
Chuck Lever482fb942007-02-12 00:53:29 -08001744 return svc_create_socket(serv, protocol, &sin, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745}
1746
1747/*
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001748 * Handle defer and revisit of requests
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 */
1750
1751static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
1752{
1753 struct svc_deferred_req *dr = container_of(dreq, struct svc_deferred_req, handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 struct svc_sock *svsk;
1755
1756 if (too_many) {
1757 svc_sock_put(dr->svsk);
1758 kfree(dr);
1759 return;
1760 }
1761 dprintk("revisit queued\n");
1762 svsk = dr->svsk;
1763 dr->svsk = NULL;
Greg Banks1a68d952006-10-02 02:17:55 -07001764 spin_lock_bh(&svsk->sk_defer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 list_add(&dr->handle.recent, &svsk->sk_deferred);
Greg Banks1a68d952006-10-02 02:17:55 -07001766 spin_unlock_bh(&svsk->sk_defer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 set_bit(SK_DEFERRED, &svsk->sk_flags);
1768 svc_sock_enqueue(svsk);
1769 svc_sock_put(svsk);
1770}
1771
1772static struct cache_deferred_req *
1773svc_defer(struct cache_req *req)
1774{
1775 struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
1776 int size = sizeof(struct svc_deferred_req) + (rqstp->rq_arg.len);
1777 struct svc_deferred_req *dr;
1778
1779 if (rqstp->rq_arg.page_len)
1780 return NULL; /* if more than a page, give up FIXME */
1781 if (rqstp->rq_deferred) {
1782 dr = rqstp->rq_deferred;
1783 rqstp->rq_deferred = NULL;
1784 } else {
1785 int skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
1786 /* FIXME maybe discard if size too large */
1787 dr = kmalloc(size, GFP_KERNEL);
1788 if (dr == NULL)
1789 return NULL;
1790
1791 dr->handle.owner = rqstp->rq_server;
1792 dr->prot = rqstp->rq_prot;
Chuck Lever24422222007-02-12 00:53:33 -08001793 memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen);
1794 dr->addrlen = rqstp->rq_addrlen;
J. Bruce Fields1918e342006-01-18 17:43:16 -08001795 dr->daddr = rqstp->rq_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 dr->argslen = rqstp->rq_arg.len >> 2;
1797 memcpy(dr->args, rqstp->rq_arg.head[0].iov_base-skip, dr->argslen<<2);
1798 }
Greg Banksc45c3572006-10-02 02:17:54 -07001799 atomic_inc(&rqstp->rq_sock->sk_inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 dr->svsk = rqstp->rq_sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 dr->handle.revisit = svc_revisit;
1803 return &dr->handle;
1804}
1805
1806/*
1807 * recv data from a deferred request into an active one
1808 */
1809static int svc_deferred_recv(struct svc_rqst *rqstp)
1810{
1811 struct svc_deferred_req *dr = rqstp->rq_deferred;
1812
1813 rqstp->rq_arg.head[0].iov_base = dr->args;
1814 rqstp->rq_arg.head[0].iov_len = dr->argslen<<2;
1815 rqstp->rq_arg.page_len = 0;
1816 rqstp->rq_arg.len = dr->argslen<<2;
1817 rqstp->rq_prot = dr->prot;
Chuck Lever24422222007-02-12 00:53:33 -08001818 memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen);
1819 rqstp->rq_addrlen = dr->addrlen;
J. Bruce Fields1918e342006-01-18 17:43:16 -08001820 rqstp->rq_daddr = dr->daddr;
NeilBrown44524352006-10-04 02:15:46 -07001821 rqstp->rq_respages = rqstp->rq_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 return dr->argslen<<2;
1823}
1824
1825
1826static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk)
1827{
1828 struct svc_deferred_req *dr = NULL;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (!test_bit(SK_DEFERRED, &svsk->sk_flags))
1831 return NULL;
Greg Banks1a68d952006-10-02 02:17:55 -07001832 spin_lock_bh(&svsk->sk_defer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 clear_bit(SK_DEFERRED, &svsk->sk_flags);
1834 if (!list_empty(&svsk->sk_deferred)) {
1835 dr = list_entry(svsk->sk_deferred.next,
1836 struct svc_deferred_req,
1837 handle.recent);
1838 list_del_init(&dr->handle.recent);
1839 set_bit(SK_DEFERRED, &svsk->sk_flags);
1840 }
Greg Banks1a68d952006-10-02 02:17:55 -07001841 spin_unlock_bh(&svsk->sk_defer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 return dr;
1843}