blob: 32b94cf19f892638099df580031aec3fe377fc9e [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>
Chuck Leverb92503b2007-02-12 00:53:36 -080039#include <net/ipv6.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070040#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/uaccess.h>
42#include <asm/ioctls.h>
43
44#include <linux/sunrpc/types.h>
Chuck Leverad06e4b2007-02-12 00:53:32 -080045#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/sunrpc/xdr.h>
47#include <linux/sunrpc/svcsock.h>
48#include <linux/sunrpc/stats.h>
49
50/* SMP locking strategy:
51 *
Greg Banks3262c812006-10-02 02:17:58 -070052 * 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.
Greg Banks1a68d952006-10-02 02:17:55 -070056 * svc_sock->sk_defer_lock protects the svc_sock->sk_deferred list
Greg Banksc081a0c2006-10-02 02:17:57 -070057 * svc_sock->sk_flags.SK_BUSY prevents a svc_sock being enqueued multiply.
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 *
59 * Some flags can be set to certain values at any time
60 * providing that certain rules are followed:
61 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * SK_CONN, SK_DATA, can be set or cleared at any time.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -080063 * after a set, svc_sock_enqueue must be called.
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * after a clear, the socket must be read/accepted
65 * if this succeeds, it must be set again.
66 * SK_CLOSE can set at any time. It is never cleared.
NeilBrownaaf68cf2007-02-08 14:20:30 -080067 * sk_inuse contains a bias of '1' until SK_DEAD is set.
68 * so when sk_inuse hits zero, we know the socket is dead
69 * and no-one is using it.
70 * SK_DEAD can only be set while SK_BUSY is held which ensures
71 * no other thread will be using the socket or will try to
72 * set SK_DEAD.
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 *
74 */
75
76#define RPCDBG_FACILITY RPCDBG_SVCSOCK
77
78
79static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
Chuck Lever6b174332007-02-12 00:53:28 -080080 int *errp, int flags);
NeilBrownaaf68cf2007-02-08 14:20:30 -080081static void svc_delete_socket(struct svc_sock *svsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082static void svc_udp_data_ready(struct sock *, int);
83static int svc_udp_recvfrom(struct svc_rqst *);
84static int svc_udp_sendto(struct svc_rqst *);
85
86static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk);
87static int svc_deferred_recv(struct svc_rqst *rqstp);
88static struct cache_deferred_req *svc_defer(struct cache_req *req);
89
Greg Banks36bdfc82006-10-02 02:17:54 -070090/* apparently the "standard" is that clients close
91 * idle connections after 5 minutes, servers after
92 * 6 minutes
93 * http://www.connectathon.org/talks96/nfstcp.pdf
94 */
95static int svc_conn_age_period = 6*60;
96
Peter Zijlstraed075362006-12-06 20:35:24 -080097#ifdef CONFIG_DEBUG_LOCK_ALLOC
98static struct lock_class_key svc_key[2];
99static struct lock_class_key svc_slock_key[2];
100
101static inline void svc_reclassify_socket(struct socket *sock)
102{
103 struct sock *sk = sock->sk;
104 BUG_ON(sk->sk_lock.owner != NULL);
105 switch (sk->sk_family) {
106 case AF_INET:
107 sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
108 &svc_slock_key[0], "sk_lock-AF_INET-NFSD", &svc_key[0]);
109 break;
110
111 case AF_INET6:
112 sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
113 &svc_slock_key[1], "sk_lock-AF_INET6-NFSD", &svc_key[1]);
114 break;
115
116 default:
117 BUG();
118 }
119}
120#else
121static inline void svc_reclassify_socket(struct socket *sock)
122{
123}
124#endif
125
Chuck Leverad06e4b2007-02-12 00:53:32 -0800126static char *__svc_print_addr(struct sockaddr *addr, char *buf, size_t len)
127{
128 switch (addr->sa_family) {
129 case AF_INET:
130 snprintf(buf, len, "%u.%u.%u.%u, port=%u",
131 NIPQUAD(((struct sockaddr_in *) addr)->sin_addr),
132 htons(((struct sockaddr_in *) addr)->sin_port));
133 break;
134#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
135 case AF_INET6:
136 snprintf(buf, len, "%x:%x:%x:%x:%x:%x:%x:%x, port=%u",
137 NIP6(((struct sockaddr_in6 *) addr)->sin6_addr),
138 htons(((struct sockaddr_in6 *) addr)->sin6_port));
139 break;
140#endif
141 default:
142 snprintf(buf, len, "unknown address type: %d", addr->sa_family);
143 break;
144 }
145 return buf;
146}
147
148/**
149 * svc_print_addr - Format rq_addr field for printing
150 * @rqstp: svc_rqst struct containing address to print
151 * @buf: target buffer for formatted address
152 * @len: length of target buffer
153 *
154 */
155char *svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len)
156{
Chuck Lever27459f02007-02-12 00:53:34 -0800157 return __svc_print_addr(svc_addr(rqstp), buf, len);
Chuck Leverad06e4b2007-02-12 00:53:32 -0800158}
159EXPORT_SYMBOL_GPL(svc_print_addr);
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/*
Greg Banks3262c812006-10-02 02:17:58 -0700162 * Queue up an idle server thread. Must have pool->sp_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 * Note: this is really a stack rather than a queue, so that we only
Greg Banks3262c812006-10-02 02:17:58 -0700164 * use as many different threads as we need, and the rest don't pollute
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 * the cache.
166 */
167static inline void
Greg Banks3262c812006-10-02 02:17:58 -0700168svc_thread_enqueue(struct svc_pool *pool, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Greg Banks3262c812006-10-02 02:17:58 -0700170 list_add(&rqstp->rq_list, &pool->sp_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173/*
Greg Banks3262c812006-10-02 02:17:58 -0700174 * Dequeue an nfsd thread. Must have pool->sp_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 */
176static inline void
Greg Banks3262c812006-10-02 02:17:58 -0700177svc_thread_dequeue(struct svc_pool *pool, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 list_del(&rqstp->rq_list);
180}
181
182/*
183 * Release an skbuff after use
184 */
185static inline void
186svc_release_skb(struct svc_rqst *rqstp)
187{
188 struct sk_buff *skb = rqstp->rq_skbuff;
189 struct svc_deferred_req *dr = rqstp->rq_deferred;
190
191 if (skb) {
192 rqstp->rq_skbuff = NULL;
193
194 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
195 skb_free_datagram(rqstp->rq_sock->sk_sk, skb);
196 }
197 if (dr) {
198 rqstp->rq_deferred = NULL;
199 kfree(dr);
200 }
201}
202
203/*
204 * Any space to write?
205 */
206static inline unsigned long
207svc_sock_wspace(struct svc_sock *svsk)
208{
209 int wspace;
210
211 if (svsk->sk_sock->type == SOCK_STREAM)
212 wspace = sk_stream_wspace(svsk->sk_sk);
213 else
214 wspace = sock_wspace(svsk->sk_sk);
215
216 return wspace;
217}
218
219/*
220 * Queue up a socket with data pending. If there are idle nfsd
221 * processes, wake 'em up.
222 *
223 */
224static void
225svc_sock_enqueue(struct svc_sock *svsk)
226{
227 struct svc_serv *serv = svsk->sk_server;
Greg Banksbfd24162006-10-02 02:18:01 -0700228 struct svc_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 struct svc_rqst *rqstp;
Greg Banksbfd24162006-10-02 02:18:01 -0700230 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 if (!(svsk->sk_flags &
233 ( (1<<SK_CONN)|(1<<SK_DATA)|(1<<SK_CLOSE)|(1<<SK_DEFERRED)) ))
234 return;
235 if (test_bit(SK_DEAD, &svsk->sk_flags))
236 return;
237
Greg Banksbfd24162006-10-02 02:18:01 -0700238 cpu = get_cpu();
239 pool = svc_pool_for_cpu(svsk->sk_server, cpu);
240 put_cpu();
241
Greg Banks3262c812006-10-02 02:17:58 -0700242 spin_lock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Greg Banks3262c812006-10-02 02:17:58 -0700244 if (!list_empty(&pool->sp_threads) &&
245 !list_empty(&pool->sp_sockets))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 printk(KERN_ERR
247 "svc_sock_enqueue: threads and sockets both waiting??\n");
248
249 if (test_bit(SK_DEAD, &svsk->sk_flags)) {
250 /* Don't enqueue dead sockets */
251 dprintk("svc: socket %p is dead, not enqueued\n", svsk->sk_sk);
252 goto out_unlock;
253 }
254
Greg Banksc081a0c2006-10-02 02:17:57 -0700255 /* Mark socket as busy. It will remain in this state until the
256 * server has processed all pending data and put the socket back
257 * on the idle list. We update SK_BUSY atomically because
258 * it also guards against trying to enqueue the svc_sock twice.
259 */
260 if (test_and_set_bit(SK_BUSY, &svsk->sk_flags)) {
261 /* Don't enqueue socket while already enqueued */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 dprintk("svc: socket %p busy, not enqueued\n", svsk->sk_sk);
263 goto out_unlock;
264 }
Greg Banks3262c812006-10-02 02:17:58 -0700265 BUG_ON(svsk->sk_pool != NULL);
266 svsk->sk_pool = pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700269 if (((atomic_read(&svsk->sk_reserved) + serv->sv_max_mesg)*2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 > svc_sock_wspace(svsk))
271 && !test_bit(SK_CLOSE, &svsk->sk_flags)
272 && !test_bit(SK_CONN, &svsk->sk_flags)) {
273 /* Don't enqueue while not enough space for reply */
274 dprintk("svc: socket %p no space, %d*2 > %ld, not enqueued\n",
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700275 svsk->sk_sk, atomic_read(&svsk->sk_reserved)+serv->sv_max_mesg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 svc_sock_wspace(svsk));
Greg Banks3262c812006-10-02 02:17:58 -0700277 svsk->sk_pool = NULL;
Greg Banksc081a0c2006-10-02 02:17:57 -0700278 clear_bit(SK_BUSY, &svsk->sk_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 goto out_unlock;
280 }
281 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Greg Banks3262c812006-10-02 02:17:58 -0700284 if (!list_empty(&pool->sp_threads)) {
285 rqstp = list_entry(pool->sp_threads.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 struct svc_rqst,
287 rq_list);
288 dprintk("svc: socket %p served by daemon %p\n",
289 svsk->sk_sk, rqstp);
Greg Banks3262c812006-10-02 02:17:58 -0700290 svc_thread_dequeue(pool, rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (rqstp->rq_sock)
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800292 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 "svc_sock_enqueue: server %p, rq_sock=%p!\n",
294 rqstp, rqstp->rq_sock);
295 rqstp->rq_sock = svsk;
Greg Banksc45c3572006-10-02 02:17:54 -0700296 atomic_inc(&svsk->sk_inuse);
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700297 rqstp->rq_reserved = serv->sv_max_mesg;
Greg Banks5685f0f2006-10-02 02:17:56 -0700298 atomic_add(rqstp->rq_reserved, &svsk->sk_reserved);
Greg Banks3262c812006-10-02 02:17:58 -0700299 BUG_ON(svsk->sk_pool != pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 wake_up(&rqstp->rq_wait);
301 } else {
302 dprintk("svc: socket %p put into queue\n", svsk->sk_sk);
Greg Banks3262c812006-10-02 02:17:58 -0700303 list_add_tail(&svsk->sk_ready, &pool->sp_sockets);
304 BUG_ON(svsk->sk_pool != pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306
307out_unlock:
Greg Banks3262c812006-10-02 02:17:58 -0700308 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
311/*
Greg Banks3262c812006-10-02 02:17:58 -0700312 * Dequeue the first socket. Must be called with the pool->sp_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 */
314static inline struct svc_sock *
Greg Banks3262c812006-10-02 02:17:58 -0700315svc_sock_dequeue(struct svc_pool *pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 struct svc_sock *svsk;
318
Greg Banks3262c812006-10-02 02:17:58 -0700319 if (list_empty(&pool->sp_sockets))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return NULL;
321
Greg Banks3262c812006-10-02 02:17:58 -0700322 svsk = list_entry(pool->sp_sockets.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 struct svc_sock, sk_ready);
324 list_del_init(&svsk->sk_ready);
325
326 dprintk("svc: socket %p dequeued, inuse=%d\n",
Greg Banksc45c3572006-10-02 02:17:54 -0700327 svsk->sk_sk, atomic_read(&svsk->sk_inuse));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 return svsk;
330}
331
332/*
333 * Having read something from a socket, check whether it
334 * needs to be re-enqueued.
335 * Note: SK_DATA only gets cleared when a read-attempt finds
336 * no (or insufficient) data.
337 */
338static inline void
339svc_sock_received(struct svc_sock *svsk)
340{
Greg Banks3262c812006-10-02 02:17:58 -0700341 svsk->sk_pool = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 clear_bit(SK_BUSY, &svsk->sk_flags);
343 svc_sock_enqueue(svsk);
344}
345
346
347/**
348 * svc_reserve - change the space reserved for the reply to a request.
349 * @rqstp: The request in question
350 * @space: new max space to reserve
351 *
352 * Each request reserves some space on the output queue of the socket
353 * to make sure the reply fits. This function reduces that reserved
354 * space to be the amount of space used already, plus @space.
355 *
356 */
357void svc_reserve(struct svc_rqst *rqstp, int space)
358{
359 space += rqstp->rq_res.head[0].iov_len;
360
361 if (space < rqstp->rq_reserved) {
362 struct svc_sock *svsk = rqstp->rq_sock;
Greg Banks5685f0f2006-10-02 02:17:56 -0700363 atomic_sub((rqstp->rq_reserved - space), &svsk->sk_reserved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 rqstp->rq_reserved = space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 svc_sock_enqueue(svsk);
367 }
368}
369
370/*
371 * Release a socket after use.
372 */
373static inline void
374svc_sock_put(struct svc_sock *svsk)
375{
NeilBrownaaf68cf2007-02-08 14:20:30 -0800376 if (atomic_dec_and_test(&svsk->sk_inuse)) {
377 BUG_ON(! test_bit(SK_DEAD, &svsk->sk_flags));
378
Andrew Morton202dd452006-10-29 22:57:57 -0800379 dprintk("svc: releasing dead socket\n");
Neil Brownd6740df2006-10-29 22:46:45 -0800380 if (svsk->sk_sock->file)
381 sockfd_put(svsk->sk_sock);
382 else
383 sock_release(svsk->sk_sock);
384 if (svsk->sk_info_authunix != NULL)
385 svcauth_unix_info_release(svsk->sk_info_authunix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 kfree(svsk);
387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
390static void
391svc_sock_release(struct svc_rqst *rqstp)
392{
393 struct svc_sock *svsk = rqstp->rq_sock;
394
395 svc_release_skb(rqstp);
396
NeilBrown44524352006-10-04 02:15:46 -0700397 svc_free_res_pages(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 rqstp->rq_res.page_len = 0;
399 rqstp->rq_res.page_base = 0;
400
401
402 /* Reset response buffer and release
403 * the reservation.
404 * But first, check that enough space was reserved
405 * for the reply, otherwise we have a bug!
406 */
407 if ((rqstp->rq_res.len) > rqstp->rq_reserved)
408 printk(KERN_ERR "RPC request reserved %d but used %d\n",
409 rqstp->rq_reserved,
410 rqstp->rq_res.len);
411
412 rqstp->rq_res.head[0].iov_len = 0;
413 svc_reserve(rqstp, 0);
414 rqstp->rq_sock = NULL;
415
416 svc_sock_put(svsk);
417}
418
419/*
420 * External function to wake up a server waiting for data
Greg Banks3262c812006-10-02 02:17:58 -0700421 * This really only makes sense for services like lockd
422 * which have exactly one thread anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 */
424void
425svc_wake_up(struct svc_serv *serv)
426{
427 struct svc_rqst *rqstp;
Greg Banks3262c812006-10-02 02:17:58 -0700428 unsigned int i;
429 struct svc_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Greg Banks3262c812006-10-02 02:17:58 -0700431 for (i = 0; i < serv->sv_nrpools; i++) {
432 pool = &serv->sv_pools[i];
433
434 spin_lock_bh(&pool->sp_lock);
435 if (!list_empty(&pool->sp_threads)) {
436 rqstp = list_entry(pool->sp_threads.next,
437 struct svc_rqst,
438 rq_list);
439 dprintk("svc: daemon %p woken up.\n", rqstp);
440 /*
441 svc_thread_dequeue(pool, rqstp);
442 rqstp->rq_sock = NULL;
443 */
444 wake_up(&rqstp->rq_wait);
445 }
446 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Chuck Leverb92503b2007-02-12 00:53:36 -0800450union svc_pktinfo_u {
451 struct in_pktinfo pkti;
452#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
453 struct in6_pktinfo pkti6;
454#endif
455};
456
457static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
458{
459 switch (rqstp->rq_sock->sk_sk->sk_family) {
460 case AF_INET: {
461 struct in_pktinfo *pki = CMSG_DATA(cmh);
462
463 cmh->cmsg_level = SOL_IP;
464 cmh->cmsg_type = IP_PKTINFO;
465 pki->ipi_ifindex = 0;
466 pki->ipi_spec_dst.s_addr = rqstp->rq_daddr.addr.s_addr;
467 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
468 }
469 break;
470#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
471 case AF_INET6: {
472 struct in6_pktinfo *pki = CMSG_DATA(cmh);
473
474 cmh->cmsg_level = SOL_IPV6;
475 cmh->cmsg_type = IPV6_PKTINFO;
476 pki->ipi6_ifindex = 0;
477 ipv6_addr_copy(&pki->ipi6_addr,
478 &rqstp->rq_daddr.addr6);
479 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
480 }
481 break;
482#endif
483 }
484 return;
485}
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487/*
488 * Generic sendto routine
489 */
490static int
491svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
492{
493 struct svc_sock *svsk = rqstp->rq_sock;
494 struct socket *sock = svsk->sk_sock;
495 int slen;
Chuck Leverb92503b2007-02-12 00:53:36 -0800496 char buffer[CMSG_SPACE(sizeof(union svc_pktinfo_u))];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 struct cmsghdr *cmh = (struct cmsghdr *)buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 int len = 0;
499 int result;
500 int size;
501 struct page **ppage = xdr->pages;
502 size_t base = xdr->page_base;
503 unsigned int pglen = xdr->page_len;
504 unsigned int flags = MSG_MORE;
Chuck Leverad06e4b2007-02-12 00:53:32 -0800505 char buf[RPC_MAX_ADDRBUFLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 slen = xdr->len;
508
509 if (rqstp->rq_prot == IPPROTO_UDP) {
Chuck Leverb92503b2007-02-12 00:53:36 -0800510 struct msghdr msg = {
511 .msg_name = &rqstp->rq_addr,
512 .msg_namelen = rqstp->rq_addrlen,
513 .msg_control = cmh,
514 .msg_controllen = sizeof(buffer),
515 .msg_flags = MSG_MORE,
516 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Chuck Leverb92503b2007-02-12 00:53:36 -0800518 svc_set_cmsg_data(rqstp, cmh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 if (sock_sendmsg(sock, &msg, 0) < 0)
521 goto out;
522 }
523
524 /* send head */
525 if (slen == xdr->head[0].iov_len)
526 flags = 0;
NeilBrown44524352006-10-04 02:15:46 -0700527 len = kernel_sendpage(sock, rqstp->rq_respages[0], 0,
528 xdr->head[0].iov_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (len != xdr->head[0].iov_len)
530 goto out;
531 slen -= xdr->head[0].iov_len;
532 if (slen == 0)
533 goto out;
534
535 /* send page data */
536 size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
537 while (pglen > 0) {
538 if (slen == size)
539 flags = 0;
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700540 result = kernel_sendpage(sock, *ppage, base, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (result > 0)
542 len += result;
543 if (result != size)
544 goto out;
545 slen -= size;
546 pglen -= size;
547 size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
548 base = 0;
549 ppage++;
550 }
551 /* send tail */
552 if (xdr->tail[0].iov_len) {
NeilBrown44524352006-10-04 02:15:46 -0700553 result = kernel_sendpage(sock, rqstp->rq_respages[0],
554 ((unsigned long)xdr->tail[0].iov_base)
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800555 & (PAGE_SIZE-1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 xdr->tail[0].iov_len, 0);
557
558 if (result > 0)
559 len += result;
560 }
561out:
Chuck Leverad06e4b2007-02-12 00:53:32 -0800562 dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %s)\n",
563 rqstp->rq_sock, xdr->head[0].iov_base, xdr->head[0].iov_len,
564 xdr->len, len, svc_print_addr(rqstp, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 return len;
567}
568
569/*
NeilBrown80212d52006-10-02 02:17:47 -0700570 * Report socket names for nfsdfs
571 */
572static int one_sock_name(char *buf, struct svc_sock *svsk)
573{
574 int len;
575
576 switch(svsk->sk_sk->sk_family) {
577 case AF_INET:
578 len = sprintf(buf, "ipv4 %s %u.%u.%u.%u %d\n",
579 svsk->sk_sk->sk_protocol==IPPROTO_UDP?
580 "udp" : "tcp",
581 NIPQUAD(inet_sk(svsk->sk_sk)->rcv_saddr),
582 inet_sk(svsk->sk_sk)->num);
583 break;
584 default:
585 len = sprintf(buf, "*unknown-%d*\n",
586 svsk->sk_sk->sk_family);
587 }
588 return len;
589}
590
591int
NeilBrownb41b66d2006-10-02 02:17:48 -0700592svc_sock_names(char *buf, struct svc_serv *serv, char *toclose)
NeilBrown80212d52006-10-02 02:17:47 -0700593{
NeilBrownb41b66d2006-10-02 02:17:48 -0700594 struct svc_sock *svsk, *closesk = NULL;
NeilBrown80212d52006-10-02 02:17:47 -0700595 int len = 0;
596
597 if (!serv)
598 return 0;
NeilBrownaaf68cf2007-02-08 14:20:30 -0800599 spin_lock_bh(&serv->sv_lock);
NeilBrown80212d52006-10-02 02:17:47 -0700600 list_for_each_entry(svsk, &serv->sv_permsocks, sk_list) {
601 int onelen = one_sock_name(buf+len, svsk);
NeilBrownb41b66d2006-10-02 02:17:48 -0700602 if (toclose && strcmp(toclose, buf+len) == 0)
603 closesk = svsk;
604 else
605 len += onelen;
NeilBrown80212d52006-10-02 02:17:47 -0700606 }
NeilBrownaaf68cf2007-02-08 14:20:30 -0800607 spin_unlock_bh(&serv->sv_lock);
NeilBrownb41b66d2006-10-02 02:17:48 -0700608 if (closesk)
NeilBrown5680c442006-10-04 02:15:45 -0700609 /* Should unregister with portmap, but you cannot
610 * unregister just one protocol...
611 */
NeilBrownaaf68cf2007-02-08 14:20:30 -0800612 svc_close_socket(closesk);
NeilBrown37a03472006-10-04 02:15:44 -0700613 else if (toclose)
614 return -ENOENT;
NeilBrown80212d52006-10-02 02:17:47 -0700615 return len;
616}
617EXPORT_SYMBOL(svc_sock_names);
618
619/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 * Check input queue length
621 */
622static int
623svc_recv_available(struct svc_sock *svsk)
624{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 struct socket *sock = svsk->sk_sock;
626 int avail, err;
627
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700628 err = kernel_sock_ioctl(sock, TIOCINQ, (unsigned long) &avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 return (err >= 0)? avail : err;
631}
632
633/*
634 * Generic recvfrom routine.
635 */
636static int
637svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr, int buflen)
638{
Chuck Lever067d7812007-02-12 00:53:30 -0800639 struct svc_sock *svsk = rqstp->rq_sock;
Chuck Lever1ba95102007-02-12 00:53:31 -0800640 struct msghdr msg = {
641 .msg_flags = MSG_DONTWAIT,
642 };
643 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Chuck Lever1ba95102007-02-12 00:53:31 -0800645 len = kernel_recvmsg(svsk->sk_sock, &msg, iov, nr, buflen,
646 msg.msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 /* sock_recvmsg doesn't fill in the name/namelen, so we must..
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 */
Chuck Lever067d7812007-02-12 00:53:30 -0800650 memcpy(&rqstp->rq_addr, &svsk->sk_remote, svsk->sk_remotelen);
651 rqstp->rq_addrlen = svsk->sk_remotelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
Chuck Lever1ba95102007-02-12 00:53:31 -0800654 svsk, iov[0].iov_base, iov[0].iov_len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 return len;
657}
658
659/*
660 * Set socket snd and rcv buffer lengths
661 */
662static inline void
663svc_sock_setbufsize(struct socket *sock, unsigned int snd, unsigned int rcv)
664{
665#if 0
666 mm_segment_t oldfs;
667 oldfs = get_fs(); set_fs(KERNEL_DS);
668 sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
669 (char*)&snd, sizeof(snd));
670 sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
671 (char*)&rcv, sizeof(rcv));
672#else
673 /* sock_setsockopt limits use to sysctl_?mem_max,
674 * which isn't acceptable. Until that is made conditional
675 * on not having CAP_SYS_RESOURCE or similar, we go direct...
676 * DaveM said I could!
677 */
678 lock_sock(sock->sk);
679 sock->sk->sk_sndbuf = snd * 2;
680 sock->sk->sk_rcvbuf = rcv * 2;
681 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK|SOCK_RCVBUF_LOCK;
682 release_sock(sock->sk);
683#endif
684}
685/*
686 * INET callback when data has been received on the socket.
687 */
688static void
689svc_udp_data_ready(struct sock *sk, int count)
690{
Neil Brown939bb7e2005-09-13 01:25:39 -0700691 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Neil Brown939bb7e2005-09-13 01:25:39 -0700693 if (svsk) {
694 dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
695 svsk, sk, count, test_bit(SK_BUSY, &svsk->sk_flags));
696 set_bit(SK_DATA, &svsk->sk_flags);
697 svc_sock_enqueue(svsk);
698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
700 wake_up_interruptible(sk->sk_sleep);
701}
702
703/*
704 * INET callback when space is newly available on the socket.
705 */
706static void
707svc_write_space(struct sock *sk)
708{
709 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
710
711 if (svsk) {
712 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
713 svsk, sk, test_bit(SK_BUSY, &svsk->sk_flags));
714 svc_sock_enqueue(svsk);
715 }
716
717 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) {
Neil Brown939bb7e2005-09-13 01:25:39 -0700718 dprintk("RPC svc_write_space: someone sleeping on %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 svsk);
720 wake_up_interruptible(sk->sk_sleep);
721 }
722}
723
NeilBrown7a37f572007-03-06 01:42:21 -0800724static inline void svc_udp_get_dest_address(struct svc_rqst *rqstp,
725 struct cmsghdr *cmh)
Chuck Lever95756482007-02-12 00:53:38 -0800726{
727 switch (rqstp->rq_sock->sk_sk->sk_family) {
728 case AF_INET: {
NeilBrown7a37f572007-03-06 01:42:21 -0800729 struct in_pktinfo *pki = CMSG_DATA(cmh);
730 rqstp->rq_daddr.addr.s_addr = pki->ipi_spec_dst.s_addr;
Chuck Lever95756482007-02-12 00:53:38 -0800731 break;
NeilBrown7a37f572007-03-06 01:42:21 -0800732 }
Chuck Lever95756482007-02-12 00:53:38 -0800733#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
734 case AF_INET6: {
NeilBrown7a37f572007-03-06 01:42:21 -0800735 struct in6_pktinfo *pki = CMSG_DATA(cmh);
736 ipv6_addr_copy(&rqstp->rq_daddr.addr6, &pki->ipi6_addr);
Chuck Lever95756482007-02-12 00:53:38 -0800737 break;
NeilBrown7a37f572007-03-06 01:42:21 -0800738 }
Chuck Lever95756482007-02-12 00:53:38 -0800739#endif
740 }
Chuck Lever95756482007-02-12 00:53:38 -0800741}
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743/*
744 * Receive a datagram from a UDP socket.
745 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746static int
747svc_udp_recvfrom(struct svc_rqst *rqstp)
748{
749 struct svc_sock *svsk = rqstp->rq_sock;
750 struct svc_serv *serv = svsk->sk_server;
751 struct sk_buff *skb;
NeilBrown7a37f572007-03-06 01:42:21 -0800752 char buffer[CMSG_SPACE(sizeof(union svc_pktinfo_u))];
753 struct cmsghdr *cmh = (struct cmsghdr *)buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 int err, len;
NeilBrown7a37f572007-03-06 01:42:21 -0800755 struct msghdr msg = {
756 .msg_name = svc_addr(rqstp),
757 .msg_control = cmh,
758 .msg_controllen = sizeof(buffer),
759 .msg_flags = MSG_DONTWAIT,
760 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
763 /* udp sockets need large rcvbuf as all pending
764 * requests are still in that buffer. sndbuf must
765 * also be large enough that there is enough space
Greg Banks3262c812006-10-02 02:17:58 -0700766 * for one reply per thread. We count all threads
767 * rather than threads in a particular pool, which
768 * provides an upper bound on the number of threads
769 * which will access the socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 */
771 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700772 (serv->sv_nrthreads+3) * serv->sv_max_mesg,
773 (serv->sv_nrthreads+3) * serv->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
776 svc_sock_received(svsk);
777 return svc_deferred_recv(rqstp);
778 }
779
NeilBrownaaf68cf2007-02-08 14:20:30 -0800780 if (test_bit(SK_CLOSE, &svsk->sk_flags)) {
781 svc_delete_socket(svsk);
782 return 0;
783 }
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 clear_bit(SK_DATA, &svsk->sk_flags);
NeilBrown7a37f572007-03-06 01:42:21 -0800786 while ((err == kernel_recvmsg(svsk->sk_sock, &msg, NULL,
787 0, 0, MSG_PEEK | MSG_DONTWAIT)) < 0 ||
788 (skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (err == -EAGAIN) {
790 svc_sock_received(svsk);
791 return err;
792 }
793 /* possibly an icmp error */
794 dprintk("svc: recvfrom returned error %d\n", -err);
795 }
NeilBrown7a37f572007-03-06 01:42:21 -0800796 rqstp->rq_addrlen = sizeof(rqstp->rq_addr);
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700797 if (skb->tstamp.off_sec == 0) {
798 struct timeval tv;
799
800 tv.tv_sec = xtime.tv_sec;
Andrew Morton4bcde032005-10-26 01:59:03 -0700801 tv.tv_usec = xtime.tv_nsec / NSEC_PER_USEC;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700802 skb_set_timestamp(skb, &tv);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800803 /* Don't enable netstamp, sunrpc doesn't
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 need that much accuracy */
805 }
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700806 skb_get_timestamp(skb, &svsk->sk_sk->sk_stamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 set_bit(SK_DATA, &svsk->sk_flags); /* there may be more data... */
808
809 /*
810 * Maybe more packets - kick another thread ASAP.
811 */
812 svc_sock_received(svsk);
813
814 len = skb->len - sizeof(struct udphdr);
815 rqstp->rq_arg.len = len;
816
Chuck Lever95756482007-02-12 00:53:38 -0800817 rqstp->rq_prot = IPPROTO_UDP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
NeilBrown7a37f572007-03-06 01:42:21 -0800819 if (cmh->cmsg_level != IPPROTO_IP ||
820 cmh->cmsg_type != IP_PKTINFO) {
821 if (net_ratelimit())
822 printk("rpcsvc: received unknown control message:"
823 "%d/%d\n",
824 cmh->cmsg_level, cmh->cmsg_type);
825 skb_free_datagram(svsk->sk_sk, skb);
826 return 0;
827 }
828 svc_udp_get_dest_address(rqstp, cmh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 if (skb_is_nonlinear(skb)) {
831 /* we have to copy */
832 local_bh_disable();
833 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
834 local_bh_enable();
835 /* checksum error */
836 skb_free_datagram(svsk->sk_sk, skb);
837 return 0;
838 }
839 local_bh_enable();
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800840 skb_free_datagram(svsk->sk_sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 } else {
842 /* we can use it in-place */
843 rqstp->rq_arg.head[0].iov_base = skb->data + sizeof(struct udphdr);
844 rqstp->rq_arg.head[0].iov_len = len;
Herbert Xufb286bb2005-11-10 13:01:24 -0800845 if (skb_checksum_complete(skb)) {
846 skb_free_datagram(svsk->sk_sk, skb);
847 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849 rqstp->rq_skbuff = skb;
850 }
851
852 rqstp->rq_arg.page_base = 0;
853 if (len <= rqstp->rq_arg.head[0].iov_len) {
854 rqstp->rq_arg.head[0].iov_len = len;
855 rqstp->rq_arg.page_len = 0;
NeilBrown44524352006-10-04 02:15:46 -0700856 rqstp->rq_respages = rqstp->rq_pages+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 } else {
858 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
NeilBrown44524352006-10-04 02:15:46 -0700859 rqstp->rq_respages = rqstp->rq_pages + 1 +
860 (rqstp->rq_arg.page_len + PAGE_SIZE - 1)/ PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862
863 if (serv->sv_stats)
864 serv->sv_stats->netudpcnt++;
865
866 return len;
867}
868
869static int
870svc_udp_sendto(struct svc_rqst *rqstp)
871{
872 int error;
873
874 error = svc_sendto(rqstp, &rqstp->rq_res);
875 if (error == -ECONNREFUSED)
876 /* ICMP error on earlier request. */
877 error = svc_sendto(rqstp, &rqstp->rq_res);
878
879 return error;
880}
881
882static void
883svc_udp_init(struct svc_sock *svsk)
884{
NeilBrown7a37f572007-03-06 01:42:21 -0800885 int one = 1;
886 mm_segment_t oldfs;
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 svsk->sk_sk->sk_data_ready = svc_udp_data_ready;
889 svsk->sk_sk->sk_write_space = svc_write_space;
890 svsk->sk_recvfrom = svc_udp_recvfrom;
891 svsk->sk_sendto = svc_udp_sendto;
892
893 /* initialise setting must have enough space to
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800894 * receive and respond to one request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 * svc_udp_recvfrom will re-adjust if necessary
896 */
897 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700898 3 * svsk->sk_server->sv_max_mesg,
899 3 * svsk->sk_server->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 set_bit(SK_DATA, &svsk->sk_flags); /* might have come in before data_ready set up */
902 set_bit(SK_CHNGBUF, &svsk->sk_flags);
NeilBrown7a37f572007-03-06 01:42:21 -0800903
904 oldfs = get_fs();
905 set_fs(KERNEL_DS);
906 /* make sure we get destination address info */
907 svsk->sk_sock->ops->setsockopt(svsk->sk_sock, IPPROTO_IP, IP_PKTINFO,
908 (char __user *)&one, sizeof(one));
909 set_fs(oldfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
912/*
913 * A data_ready event on a listening socket means there's a connection
914 * pending. Do not use state_change as a substitute for it.
915 */
916static void
917svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
918{
Neil Brown939bb7e2005-09-13 01:25:39 -0700919 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 dprintk("svc: socket %p TCP (listen) state change %d\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700922 sk, sk->sk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Neil Brown939bb7e2005-09-13 01:25:39 -0700924 /*
925 * This callback may called twice when a new connection
926 * is established as a child socket inherits everything
927 * from a parent LISTEN socket.
928 * 1) data_ready method of the parent socket will be called
929 * when one of child sockets become ESTABLISHED.
930 * 2) data_ready method of the child socket may be called
931 * when it receives data before the socket is accepted.
932 * In case of 2, we should ignore it silently.
933 */
934 if (sk->sk_state == TCP_LISTEN) {
935 if (svsk) {
936 set_bit(SK_CONN, &svsk->sk_flags);
937 svc_sock_enqueue(svsk);
938 } else
939 printk("svc: socket %p: no user data\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
Neil Brown939bb7e2005-09-13 01:25:39 -0700941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
943 wake_up_interruptible_all(sk->sk_sleep);
944}
945
946/*
947 * A state change on a connected socket means it's dying or dead.
948 */
949static void
950svc_tcp_state_change(struct sock *sk)
951{
Neil Brown939bb7e2005-09-13 01:25:39 -0700952 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700955 sk, sk->sk_state, sk->sk_user_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Neil Brown939bb7e2005-09-13 01:25:39 -0700957 if (!svsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 printk("svc: socket %p: no user data\n", sk);
Neil Brown939bb7e2005-09-13 01:25:39 -0700959 else {
960 set_bit(SK_CLOSE, &svsk->sk_flags);
961 svc_sock_enqueue(svsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
964 wake_up_interruptible_all(sk->sk_sleep);
965}
966
967static void
968svc_tcp_data_ready(struct sock *sk, int count)
969{
Neil Brown939bb7e2005-09-13 01:25:39 -0700970 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 dprintk("svc: socket %p TCP data ready (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700973 sk, sk->sk_user_data);
974 if (svsk) {
975 set_bit(SK_DATA, &svsk->sk_flags);
976 svc_sock_enqueue(svsk);
977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
979 wake_up_interruptible(sk->sk_sleep);
980}
981
Chuck Leverbcdb81a2007-02-12 00:53:37 -0800982static inline int svc_port_is_privileged(struct sockaddr *sin)
983{
984 switch (sin->sa_family) {
985 case AF_INET:
986 return ntohs(((struct sockaddr_in *)sin)->sin_port)
987 < PROT_SOCK;
988#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
989 case AF_INET6:
990 return ntohs(((struct sockaddr_in6 *)sin)->sin6_port)
991 < PROT_SOCK;
992#endif
993 default:
994 return 0;
995 }
996}
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998/*
999 * Accept a TCP connection
1000 */
1001static void
1002svc_tcp_accept(struct svc_sock *svsk)
1003{
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001004 struct sockaddr_storage addr;
1005 struct sockaddr *sin = (struct sockaddr *) &addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 struct svc_serv *serv = svsk->sk_server;
1007 struct socket *sock = svsk->sk_sock;
1008 struct socket *newsock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 struct svc_sock *newsvsk;
1010 int err, slen;
Chuck Leverad06e4b2007-02-12 00:53:32 -08001011 char buf[RPC_MAX_ADDRBUFLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
1014 if (!sock)
1015 return;
1016
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001017 clear_bit(SK_CONN, &svsk->sk_flags);
1018 err = kernel_accept(sock, &newsock, O_NONBLOCK);
1019 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (err == -ENOMEM)
1021 printk(KERN_WARNING "%s: no more sockets!\n",
1022 serv->sv_name);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001023 else if (err != -EAGAIN && net_ratelimit())
1024 printk(KERN_WARNING "%s: accept failed (err %d)!\n",
1025 serv->sv_name, -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return;
1027 }
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 set_bit(SK_CONN, &svsk->sk_flags);
1030 svc_sock_enqueue(svsk);
1031
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001032 err = kernel_getpeername(newsock, sin, &slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (err < 0) {
1034 if (net_ratelimit())
1035 printk(KERN_WARNING "%s: peername failed (err %d)!\n",
1036 serv->sv_name, -err);
1037 goto failed; /* aborted connection or whatever */
1038 }
1039
1040 /* Ideally, we would want to reject connections from unauthorized
Chuck Leverad06e4b2007-02-12 00:53:32 -08001041 * hosts here, but when we get encryption, the IP of the host won't
1042 * tell us anything. For now just warn about unpriv connections.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 */
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001044 if (!svc_port_is_privileged(sin)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 dprintk(KERN_WARNING
Chuck Leverad06e4b2007-02-12 00:53:32 -08001046 "%s: connect from unprivileged port: %s\n",
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001047 serv->sv_name,
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001048 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
Chuck Leverad06e4b2007-02-12 00:53:32 -08001050 dprintk("%s: connect from %s\n", serv->sv_name,
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001051 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 /* make sure that a write doesn't block forever when
1054 * low on memory
1055 */
1056 newsock->sk->sk_sndtimeo = HZ*30;
1057
Chuck Lever6b174332007-02-12 00:53:28 -08001058 if (!(newsvsk = svc_setup_socket(serv, newsock, &err,
1059 (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 goto failed;
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001061 memcpy(&newsvsk->sk_remote, sin, slen);
Chuck Lever067d7812007-02-12 00:53:30 -08001062 newsvsk->sk_remotelen = slen;
1063
NeilBrowne79eff12007-02-12 00:53:30 -08001064 svc_sock_received(newsvsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 /* make sure that we don't have too many active connections.
1067 * If we have, something must be dropped.
1068 *
1069 * There's no point in trying to do random drop here for
1070 * DoS prevention. The NFS clients does 1 reconnect in 15
1071 * seconds. An attacker can easily beat that.
1072 *
1073 * The only somewhat efficient mechanism would be if drop
1074 * old connections from the same IP first. But right now
1075 * we don't even record the client IP in svc_sock.
1076 */
1077 if (serv->sv_tmpcnt > (serv->sv_nrthreads+3)*20) {
1078 struct svc_sock *svsk = NULL;
1079 spin_lock_bh(&serv->sv_lock);
1080 if (!list_empty(&serv->sv_tempsocks)) {
1081 if (net_ratelimit()) {
1082 /* Try to help the admin */
1083 printk(KERN_NOTICE "%s: too many open TCP "
1084 "sockets, consider increasing the "
1085 "number of nfsd threads\n",
1086 serv->sv_name);
Chuck Leverad06e4b2007-02-12 00:53:32 -08001087 printk(KERN_NOTICE
1088 "%s: last TCP connect from %s\n",
1089 serv->sv_name, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091 /*
1092 * Always select the oldest socket. It's not fair,
1093 * but so is life
1094 */
1095 svsk = list_entry(serv->sv_tempsocks.prev,
1096 struct svc_sock,
1097 sk_list);
1098 set_bit(SK_CLOSE, &svsk->sk_flags);
Greg Banksc45c3572006-10-02 02:17:54 -07001099 atomic_inc(&svsk->sk_inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
1101 spin_unlock_bh(&serv->sv_lock);
1102
1103 if (svsk) {
1104 svc_sock_enqueue(svsk);
1105 svc_sock_put(svsk);
1106 }
1107
1108 }
1109
1110 if (serv->sv_stats)
1111 serv->sv_stats->nettcpconn++;
1112
1113 return;
1114
1115failed:
1116 sock_release(newsock);
1117 return;
1118}
1119
1120/*
1121 * Receive data from a TCP socket.
1122 */
1123static int
1124svc_tcp_recvfrom(struct svc_rqst *rqstp)
1125{
1126 struct svc_sock *svsk = rqstp->rq_sock;
1127 struct svc_serv *serv = svsk->sk_server;
1128 int len;
NeilBrown3cc03b12006-10-04 02:15:47 -07001129 struct kvec *vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 int pnum, vlen;
1131
1132 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
1133 svsk, test_bit(SK_DATA, &svsk->sk_flags),
1134 test_bit(SK_CONN, &svsk->sk_flags),
1135 test_bit(SK_CLOSE, &svsk->sk_flags));
1136
1137 if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
1138 svc_sock_received(svsk);
1139 return svc_deferred_recv(rqstp);
1140 }
1141
1142 if (test_bit(SK_CLOSE, &svsk->sk_flags)) {
1143 svc_delete_socket(svsk);
1144 return 0;
1145 }
1146
NeilBrown1a047062006-10-19 23:29:13 -07001147 if (svsk->sk_sk->sk_state == TCP_LISTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 svc_tcp_accept(svsk);
1149 svc_sock_received(svsk);
1150 return 0;
1151 }
1152
1153 if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
1154 /* sndbuf needs to have room for one request
1155 * per thread, otherwise we can stall even when the
1156 * network isn't a bottleneck.
Greg Banks3262c812006-10-02 02:17:58 -07001157 *
1158 * We count all threads rather than threads in a
1159 * particular pool, which provides an upper bound
1160 * on the number of threads which will access the socket.
1161 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 * rcvbuf just needs to be able to hold a few requests.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001163 * Normally they will be removed from the queue
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 * as soon a a complete request arrives.
1165 */
1166 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001167 (serv->sv_nrthreads+3) * serv->sv_max_mesg,
1168 3 * serv->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 clear_bit(SK_DATA, &svsk->sk_flags);
1171
1172 /* Receive data. If we haven't got the record length yet, get
1173 * the next four bytes. Otherwise try to gobble up as much as
1174 * possible up to the complete record length.
1175 */
1176 if (svsk->sk_tcplen < 4) {
1177 unsigned long want = 4 - svsk->sk_tcplen;
1178 struct kvec iov;
1179
1180 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
1181 iov.iov_len = want;
1182 if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
1183 goto error;
1184 svsk->sk_tcplen += len;
1185
1186 if (len < want) {
1187 dprintk("svc: short recvfrom while reading record length (%d of %lu)\n",
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001188 len, want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 svc_sock_received(svsk);
1190 return -EAGAIN; /* record header not complete */
1191 }
1192
1193 svsk->sk_reclen = ntohl(svsk->sk_reclen);
1194 if (!(svsk->sk_reclen & 0x80000000)) {
1195 /* FIXME: technically, a record can be fragmented,
1196 * and non-terminal fragments will not have the top
1197 * bit set in the fragment length header.
1198 * But apparently no known nfs clients send fragmented
1199 * records. */
NeilBrown34e9a632007-01-29 13:19:52 -08001200 if (net_ratelimit())
1201 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx"
1202 " (non-terminal)\n",
1203 (unsigned long) svsk->sk_reclen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 goto err_delete;
1205 }
1206 svsk->sk_reclen &= 0x7fffffff;
1207 dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001208 if (svsk->sk_reclen > serv->sv_max_mesg) {
NeilBrown34e9a632007-01-29 13:19:52 -08001209 if (net_ratelimit())
1210 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx"
1211 " (large)\n",
1212 (unsigned long) svsk->sk_reclen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 goto err_delete;
1214 }
1215 }
1216
1217 /* Check whether enough data is available */
1218 len = svc_recv_available(svsk);
1219 if (len < 0)
1220 goto error;
1221
1222 if (len < svsk->sk_reclen) {
1223 dprintk("svc: incomplete TCP record (%d of %d)\n",
1224 len, svsk->sk_reclen);
1225 svc_sock_received(svsk);
1226 return -EAGAIN; /* record not complete */
1227 }
1228 len = svsk->sk_reclen;
1229 set_bit(SK_DATA, &svsk->sk_flags);
1230
NeilBrown3cc03b12006-10-04 02:15:47 -07001231 vec = rqstp->rq_vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 vec[0] = rqstp->rq_arg.head[0];
1233 vlen = PAGE_SIZE;
1234 pnum = 1;
1235 while (vlen < len) {
NeilBrown44524352006-10-04 02:15:46 -07001236 vec[pnum].iov_base = page_address(rqstp->rq_pages[pnum]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 vec[pnum].iov_len = PAGE_SIZE;
1238 pnum++;
1239 vlen += PAGE_SIZE;
1240 }
NeilBrown44524352006-10-04 02:15:46 -07001241 rqstp->rq_respages = &rqstp->rq_pages[pnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 /* Now receive data */
1244 len = svc_recvfrom(rqstp, vec, pnum, len);
1245 if (len < 0)
1246 goto error;
1247
1248 dprintk("svc: TCP complete record (%d bytes)\n", len);
1249 rqstp->rq_arg.len = len;
1250 rqstp->rq_arg.page_base = 0;
1251 if (len <= rqstp->rq_arg.head[0].iov_len) {
1252 rqstp->rq_arg.head[0].iov_len = len;
1253 rqstp->rq_arg.page_len = 0;
1254 } else {
1255 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
1256 }
1257
1258 rqstp->rq_skbuff = NULL;
1259 rqstp->rq_prot = IPPROTO_TCP;
1260
1261 /* Reset TCP read info */
1262 svsk->sk_reclen = 0;
1263 svsk->sk_tcplen = 0;
1264
1265 svc_sock_received(svsk);
1266 if (serv->sv_stats)
1267 serv->sv_stats->nettcpcnt++;
1268
1269 return len;
1270
1271 err_delete:
1272 svc_delete_socket(svsk);
1273 return -EAGAIN;
1274
1275 error:
1276 if (len == -EAGAIN) {
1277 dprintk("RPC: TCP recvfrom got EAGAIN\n");
1278 svc_sock_received(svsk);
1279 } else {
1280 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
1281 svsk->sk_server->sv_name, -len);
Olaf Kirch93fbf1a2006-01-06 00:19:56 -08001282 goto err_delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
1284
1285 return len;
1286}
1287
1288/*
1289 * Send out data on TCP socket.
1290 */
1291static int
1292svc_tcp_sendto(struct svc_rqst *rqstp)
1293{
1294 struct xdr_buf *xbufp = &rqstp->rq_res;
1295 int sent;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001296 __be32 reclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 /* Set up the first element of the reply kvec.
1299 * Any other kvecs that may be in use have been taken
1300 * care of by the server implementation itself.
1301 */
1302 reclen = htonl(0x80000000|((xbufp->len ) - 4));
1303 memcpy(xbufp->head[0].iov_base, &reclen, 4);
1304
1305 if (test_bit(SK_DEAD, &rqstp->rq_sock->sk_flags))
1306 return -ENOTCONN;
1307
1308 sent = svc_sendto(rqstp, &rqstp->rq_res);
1309 if (sent != xbufp->len) {
1310 printk(KERN_NOTICE "rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n",
1311 rqstp->rq_sock->sk_server->sv_name,
1312 (sent<0)?"got error":"sent only",
1313 sent, xbufp->len);
NeilBrownaaf68cf2007-02-08 14:20:30 -08001314 set_bit(SK_CLOSE, &rqstp->rq_sock->sk_flags);
1315 svc_sock_enqueue(rqstp->rq_sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 sent = -EAGAIN;
1317 }
1318 return sent;
1319}
1320
1321static void
1322svc_tcp_init(struct svc_sock *svsk)
1323{
1324 struct sock *sk = svsk->sk_sk;
1325 struct tcp_sock *tp = tcp_sk(sk);
1326
1327 svsk->sk_recvfrom = svc_tcp_recvfrom;
1328 svsk->sk_sendto = svc_tcp_sendto;
1329
1330 if (sk->sk_state == TCP_LISTEN) {
1331 dprintk("setting up TCP socket for listening\n");
1332 sk->sk_data_ready = svc_tcp_listen_data_ready;
1333 set_bit(SK_CONN, &svsk->sk_flags);
1334 } else {
1335 dprintk("setting up TCP socket for reading\n");
1336 sk->sk_state_change = svc_tcp_state_change;
1337 sk->sk_data_ready = svc_tcp_data_ready;
1338 sk->sk_write_space = svc_write_space;
1339
1340 svsk->sk_reclen = 0;
1341 svsk->sk_tcplen = 0;
1342
1343 tp->nonagle = 1; /* disable Nagle's algorithm */
1344
1345 /* initialise setting must have enough space to
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001346 * receive and respond to one request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 * svc_tcp_recvfrom will re-adjust if necessary
1348 */
1349 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001350 3 * svsk->sk_server->sv_max_mesg,
1351 3 * svsk->sk_server->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1354 set_bit(SK_DATA, &svsk->sk_flags);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001355 if (sk->sk_state != TCP_ESTABLISHED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 set_bit(SK_CLOSE, &svsk->sk_flags);
1357 }
1358}
1359
1360void
1361svc_sock_update_bufs(struct svc_serv *serv)
1362{
1363 /*
1364 * The number of server threads has changed. Update
1365 * rcvbuf and sndbuf accordingly on all sockets
1366 */
1367 struct list_head *le;
1368
1369 spin_lock_bh(&serv->sv_lock);
1370 list_for_each(le, &serv->sv_permsocks) {
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001371 struct svc_sock *svsk =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 list_entry(le, struct svc_sock, sk_list);
1373 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1374 }
1375 list_for_each(le, &serv->sv_tempsocks) {
1376 struct svc_sock *svsk =
1377 list_entry(le, struct svc_sock, sk_list);
1378 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1379 }
1380 spin_unlock_bh(&serv->sv_lock);
1381}
1382
1383/*
Greg Banks3262c812006-10-02 02:17:58 -07001384 * Receive the next request on any socket. This code is carefully
1385 * organised not to touch any cachelines in the shared svc_serv
1386 * structure, only cachelines in the local svc_pool.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 */
1388int
NeilBrown6fb2b472006-10-02 02:17:50 -07001389svc_recv(struct svc_rqst *rqstp, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
Chuck Lever27459f02007-02-12 00:53:34 -08001391 struct svc_sock *svsk = NULL;
NeilBrown6fb2b472006-10-02 02:17:50 -07001392 struct svc_serv *serv = rqstp->rq_server;
Greg Banks3262c812006-10-02 02:17:58 -07001393 struct svc_pool *pool = rqstp->rq_pool;
NeilBrown44524352006-10-04 02:15:46 -07001394 int len, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 int pages;
1396 struct xdr_buf *arg;
1397 DECLARE_WAITQUEUE(wait, current);
1398
1399 dprintk("svc: server %p waiting for data (to = %ld)\n",
1400 rqstp, timeout);
1401
1402 if (rqstp->rq_sock)
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001403 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 "svc_recv: service %p, socket not NULL!\n",
1405 rqstp);
1406 if (waitqueue_active(&rqstp->rq_wait))
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001407 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 "svc_recv: service %p, wait queue active!\n",
1409 rqstp);
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 /* now allocate needed pages. If we get a failure, sleep briefly */
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001413 pages = (serv->sv_max_mesg + PAGE_SIZE) / PAGE_SIZE;
NeilBrown44524352006-10-04 02:15:46 -07001414 for (i=0; i < pages ; i++)
1415 while (rqstp->rq_pages[i] == NULL) {
1416 struct page *p = alloc_page(GFP_KERNEL);
1417 if (!p)
1418 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
1419 rqstp->rq_pages[i] = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
NeilBrown250f3912007-01-26 00:56:59 -08001421 rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */
1422 BUG_ON(pages >= RPCSVC_MAXPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 /* Make arg->head point to first page and arg->pages point to rest */
1425 arg = &rqstp->rq_arg;
NeilBrown44524352006-10-04 02:15:46 -07001426 arg->head[0].iov_base = page_address(rqstp->rq_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 arg->head[0].iov_len = PAGE_SIZE;
NeilBrown44524352006-10-04 02:15:46 -07001428 arg->pages = rqstp->rq_pages + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 arg->page_base = 0;
1430 /* save at least one page for response */
1431 arg->page_len = (pages-2)*PAGE_SIZE;
1432 arg->len = (pages-1)*PAGE_SIZE;
1433 arg->tail[0].iov_len = 0;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001434
1435 try_to_freeze();
NeilBrown1887b932005-11-15 00:09:10 -08001436 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if (signalled())
1438 return -EINTR;
1439
Greg Banks3262c812006-10-02 02:17:58 -07001440 spin_lock_bh(&pool->sp_lock);
1441 if ((svsk = svc_sock_dequeue(pool)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 rqstp->rq_sock = svsk;
Greg Banksc45c3572006-10-02 02:17:54 -07001443 atomic_inc(&svsk->sk_inuse);
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001444 rqstp->rq_reserved = serv->sv_max_mesg;
Greg Banks5685f0f2006-10-02 02:17:56 -07001445 atomic_add(rqstp->rq_reserved, &svsk->sk_reserved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 } else {
1447 /* No data pending. Go to sleep */
Greg Banks3262c812006-10-02 02:17:58 -07001448 svc_thread_enqueue(pool, rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 /*
1451 * We have to be able to interrupt this wait
1452 * to bring down the daemons ...
1453 */
1454 set_current_state(TASK_INTERRUPTIBLE);
1455 add_wait_queue(&rqstp->rq_wait, &wait);
Greg Banks3262c812006-10-02 02:17:58 -07001456 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 schedule_timeout(timeout);
1459
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001460 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Greg Banks3262c812006-10-02 02:17:58 -07001462 spin_lock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 remove_wait_queue(&rqstp->rq_wait, &wait);
1464
1465 if (!(svsk = rqstp->rq_sock)) {
Greg Banks3262c812006-10-02 02:17:58 -07001466 svc_thread_dequeue(pool, rqstp);
1467 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 dprintk("svc: server %p, no data yet\n", rqstp);
1469 return signalled()? -EINTR : -EAGAIN;
1470 }
1471 }
Greg Banks3262c812006-10-02 02:17:58 -07001472 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Greg Banks3262c812006-10-02 02:17:58 -07001474 dprintk("svc: server %p, pool %u, socket %p, inuse=%d\n",
1475 rqstp, pool->sp_id, svsk, atomic_read(&svsk->sk_inuse));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 len = svsk->sk_recvfrom(rqstp);
1477 dprintk("svc: got len=%d\n", len);
1478
1479 /* No data, incomplete (TCP) read, or accept() */
1480 if (len == 0 || len == -EAGAIN) {
1481 rqstp->rq_res.len = 0;
1482 svc_sock_release(rqstp);
1483 return -EAGAIN;
1484 }
1485 svsk->sk_lastrecv = get_seconds();
Greg Banks36bdfc82006-10-02 02:17:54 -07001486 clear_bit(SK_OLD, &svsk->sk_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Chuck Leverbcdb81a2007-02-12 00:53:37 -08001488 rqstp->rq_secure = svc_port_is_privileged(svc_addr(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 rqstp->rq_chandle.defer = svc_defer;
1490
1491 if (serv->sv_stats)
1492 serv->sv_stats->netcnt++;
1493 return len;
1494}
1495
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001496/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 * Drop request
1498 */
1499void
1500svc_drop(struct svc_rqst *rqstp)
1501{
1502 dprintk("svc: socket %p dropped request\n", rqstp->rq_sock);
1503 svc_sock_release(rqstp);
1504}
1505
1506/*
1507 * Return reply to client.
1508 */
1509int
1510svc_send(struct svc_rqst *rqstp)
1511{
1512 struct svc_sock *svsk;
1513 int len;
1514 struct xdr_buf *xb;
1515
1516 if ((svsk = rqstp->rq_sock) == NULL) {
1517 printk(KERN_WARNING "NULL socket pointer in %s:%d\n",
1518 __FILE__, __LINE__);
1519 return -EFAULT;
1520 }
1521
1522 /* release the receive skb before sending the reply */
1523 svc_release_skb(rqstp);
1524
1525 /* calculate over-all length */
1526 xb = & rqstp->rq_res;
1527 xb->len = xb->head[0].iov_len +
1528 xb->page_len +
1529 xb->tail[0].iov_len;
1530
Ingo Molnar57b47a52006-03-20 22:35:41 -08001531 /* Grab svsk->sk_mutex to serialize outgoing data. */
1532 mutex_lock(&svsk->sk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 if (test_bit(SK_DEAD, &svsk->sk_flags))
1534 len = -ENOTCONN;
1535 else
1536 len = svsk->sk_sendto(rqstp);
Ingo Molnar57b47a52006-03-20 22:35:41 -08001537 mutex_unlock(&svsk->sk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 svc_sock_release(rqstp);
1539
1540 if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
1541 return 0;
1542 return len;
1543}
1544
1545/*
Greg Banks36bdfc82006-10-02 02:17:54 -07001546 * Timer function to close old temporary sockets, using
1547 * a mark-and-sweep algorithm.
1548 */
1549static void
1550svc_age_temp_sockets(unsigned long closure)
1551{
1552 struct svc_serv *serv = (struct svc_serv *)closure;
1553 struct svc_sock *svsk;
1554 struct list_head *le, *next;
1555 LIST_HEAD(to_be_aged);
1556
1557 dprintk("svc_age_temp_sockets\n");
1558
1559 if (!spin_trylock_bh(&serv->sv_lock)) {
1560 /* busy, try again 1 sec later */
1561 dprintk("svc_age_temp_sockets: busy\n");
1562 mod_timer(&serv->sv_temptimer, jiffies + HZ);
1563 return;
1564 }
1565
1566 list_for_each_safe(le, next, &serv->sv_tempsocks) {
1567 svsk = list_entry(le, struct svc_sock, sk_list);
1568
1569 if (!test_and_set_bit(SK_OLD, &svsk->sk_flags))
1570 continue;
Greg Banksc45c3572006-10-02 02:17:54 -07001571 if (atomic_read(&svsk->sk_inuse) || test_bit(SK_BUSY, &svsk->sk_flags))
Greg Banks36bdfc82006-10-02 02:17:54 -07001572 continue;
Greg Banksc45c3572006-10-02 02:17:54 -07001573 atomic_inc(&svsk->sk_inuse);
Greg Banks36bdfc82006-10-02 02:17:54 -07001574 list_move(le, &to_be_aged);
1575 set_bit(SK_CLOSE, &svsk->sk_flags);
1576 set_bit(SK_DETACHED, &svsk->sk_flags);
1577 }
1578 spin_unlock_bh(&serv->sv_lock);
1579
1580 while (!list_empty(&to_be_aged)) {
1581 le = to_be_aged.next;
1582 /* fiddling the sk_list node is safe 'cos we're SK_DETACHED */
1583 list_del_init(le);
1584 svsk = list_entry(le, struct svc_sock, sk_list);
1585
1586 dprintk("queuing svsk %p for closing, %lu seconds old\n",
1587 svsk, get_seconds() - svsk->sk_lastrecv);
1588
1589 /* a thread will dequeue and close it soon */
1590 svc_sock_enqueue(svsk);
1591 svc_sock_put(svsk);
1592 }
1593
1594 mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
1595}
1596
1597/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 * Initialize socket for RPC use and create svc_sock struct
1599 * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
1600 */
Chuck Lever6b174332007-02-12 00:53:28 -08001601static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1602 struct socket *sock,
1603 int *errp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
1605 struct svc_sock *svsk;
1606 struct sock *inet;
Chuck Lever6b174332007-02-12 00:53:28 -08001607 int pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
1608 int is_temporary = flags & SVC_SOCK_TEMPORARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 dprintk("svc: svc_setup_socket %p\n", sock);
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001611 if (!(svsk = kzalloc(sizeof(*svsk), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 *errp = -ENOMEM;
1613 return NULL;
1614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 inet = sock->sk;
1617
1618 /* Register socket with portmapper */
1619 if (*errp >= 0 && pmap_register)
1620 *errp = svc_register(serv, inet->sk_protocol,
1621 ntohs(inet_sk(inet)->sport));
1622
1623 if (*errp < 0) {
1624 kfree(svsk);
1625 return NULL;
1626 }
1627
1628 set_bit(SK_BUSY, &svsk->sk_flags);
1629 inet->sk_user_data = svsk;
1630 svsk->sk_sock = sock;
1631 svsk->sk_sk = inet;
1632 svsk->sk_ostate = inet->sk_state_change;
1633 svsk->sk_odata = inet->sk_data_ready;
1634 svsk->sk_owspace = inet->sk_write_space;
1635 svsk->sk_server = serv;
NeilBrownaaf68cf2007-02-08 14:20:30 -08001636 atomic_set(&svsk->sk_inuse, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 svsk->sk_lastrecv = get_seconds();
Greg Banks1a68d952006-10-02 02:17:55 -07001638 spin_lock_init(&svsk->sk_defer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 INIT_LIST_HEAD(&svsk->sk_deferred);
1640 INIT_LIST_HEAD(&svsk->sk_ready);
Ingo Molnar57b47a52006-03-20 22:35:41 -08001641 mutex_init(&svsk->sk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643 /* Initialize the socket */
1644 if (sock->type == SOCK_DGRAM)
1645 svc_udp_init(svsk);
1646 else
1647 svc_tcp_init(svsk);
1648
1649 spin_lock_bh(&serv->sv_lock);
Chuck Lever6b174332007-02-12 00:53:28 -08001650 if (is_temporary) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 set_bit(SK_TEMP, &svsk->sk_flags);
1652 list_add(&svsk->sk_list, &serv->sv_tempsocks);
1653 serv->sv_tmpcnt++;
Greg Banks36bdfc82006-10-02 02:17:54 -07001654 if (serv->sv_temptimer.function == NULL) {
1655 /* setup timer to age temp sockets */
1656 setup_timer(&serv->sv_temptimer, svc_age_temp_sockets,
1657 (unsigned long)serv);
1658 mod_timer(&serv->sv_temptimer,
1659 jiffies + svc_conn_age_period * HZ);
1660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 } else {
1662 clear_bit(SK_TEMP, &svsk->sk_flags);
1663 list_add(&svsk->sk_list, &serv->sv_permsocks);
1664 }
1665 spin_unlock_bh(&serv->sv_lock);
1666
1667 dprintk("svc: svc_setup_socket created %p (inet %p)\n",
1668 svsk, svsk->sk_sk);
1669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 return svsk;
1671}
1672
NeilBrownb41b66d2006-10-02 02:17:48 -07001673int svc_addsock(struct svc_serv *serv,
1674 int fd,
1675 char *name_return,
1676 int *proto)
1677{
1678 int err = 0;
1679 struct socket *so = sockfd_lookup(fd, &err);
1680 struct svc_sock *svsk = NULL;
1681
1682 if (!so)
1683 return err;
1684 if (so->sk->sk_family != AF_INET)
1685 err = -EAFNOSUPPORT;
1686 else if (so->sk->sk_protocol != IPPROTO_TCP &&
1687 so->sk->sk_protocol != IPPROTO_UDP)
1688 err = -EPROTONOSUPPORT;
1689 else if (so->state > SS_UNCONNECTED)
1690 err = -EISCONN;
1691 else {
Chuck Lever6b174332007-02-12 00:53:28 -08001692 svsk = svc_setup_socket(serv, so, &err, SVC_SOCK_DEFAULTS);
NeilBrowne79eff12007-02-12 00:53:30 -08001693 if (svsk) {
1694 svc_sock_received(svsk);
NeilBrownb41b66d2006-10-02 02:17:48 -07001695 err = 0;
NeilBrowne79eff12007-02-12 00:53:30 -08001696 }
NeilBrownb41b66d2006-10-02 02:17:48 -07001697 }
1698 if (err) {
1699 sockfd_put(so);
1700 return err;
1701 }
1702 if (proto) *proto = so->sk->sk_protocol;
1703 return one_sock_name(name_return, svsk);
1704}
1705EXPORT_SYMBOL_GPL(svc_addsock);
1706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707/*
1708 * Create socket for RPC service.
1709 */
Chuck Lever6b174332007-02-12 00:53:28 -08001710static int svc_create_socket(struct svc_serv *serv, int protocol,
Chuck Lever77f1f672007-02-12 00:53:39 -08001711 struct sockaddr *sin, int len, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
1713 struct svc_sock *svsk;
1714 struct socket *sock;
1715 int error;
1716 int type;
Chuck Leverad06e4b2007-02-12 00:53:32 -08001717 char buf[RPC_MAX_ADDRBUFLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Chuck Leverad06e4b2007-02-12 00:53:32 -08001719 dprintk("svc: svc_create_socket(%s, %d, %s)\n",
1720 serv->sv_program->pg_name, protocol,
Chuck Lever77f1f672007-02-12 00:53:39 -08001721 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1724 printk(KERN_WARNING "svc: only UDP and TCP "
1725 "sockets supported\n");
1726 return -EINVAL;
1727 }
1728 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1729
Chuck Lever77f1f672007-02-12 00:53:39 -08001730 error = sock_create_kern(sin->sa_family, type, protocol, &sock);
1731 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return error;
1733
Peter Zijlstraed075362006-12-06 20:35:24 -08001734 svc_reclassify_socket(sock);
1735
Eric Sesterhenn18114742006-09-28 14:37:07 -07001736 if (type == SOCK_STREAM)
Chuck Lever77f1f672007-02-12 00:53:39 -08001737 sock->sk->sk_reuse = 1; /* allow address reuse */
1738 error = kernel_bind(sock, sin, len);
Eric Sesterhenn18114742006-09-28 14:37:07 -07001739 if (error < 0)
1740 goto bummer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 if (protocol == IPPROTO_TCP) {
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001743 if ((error = kernel_listen(sock, 64)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 goto bummer;
1745 }
1746
NeilBrowne79eff12007-02-12 00:53:30 -08001747 if ((svsk = svc_setup_socket(serv, sock, &error, flags)) != NULL) {
1748 svc_sock_received(svsk);
Chuck Lever6b174332007-02-12 00:53:28 -08001749 return ntohs(inet_sk(svsk->sk_sk)->sport);
NeilBrowne79eff12007-02-12 00:53:30 -08001750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
1752bummer:
1753 dprintk("svc: svc_create_socket error = %d\n", -error);
1754 sock_release(sock);
1755 return error;
1756}
1757
1758/*
1759 * Remove a dead socket
1760 */
NeilBrownaaf68cf2007-02-08 14:20:30 -08001761static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762svc_delete_socket(struct svc_sock *svsk)
1763{
1764 struct svc_serv *serv;
1765 struct sock *sk;
1766
1767 dprintk("svc: svc_delete_socket(%p)\n", svsk);
1768
1769 serv = svsk->sk_server;
1770 sk = svsk->sk_sk;
1771
1772 sk->sk_state_change = svsk->sk_ostate;
1773 sk->sk_data_ready = svsk->sk_odata;
1774 sk->sk_write_space = svsk->sk_owspace;
1775
1776 spin_lock_bh(&serv->sv_lock);
1777
Greg Banks36bdfc82006-10-02 02:17:54 -07001778 if (!test_and_set_bit(SK_DETACHED, &svsk->sk_flags))
1779 list_del_init(&svsk->sk_list);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001780 /*
Greg Banks3262c812006-10-02 02:17:58 -07001781 * We used to delete the svc_sock from whichever list
1782 * it's sk_ready node was on, but we don't actually
1783 * need to. This is because the only time we're called
1784 * while still attached to a queue, the queue itself
1785 * is about to be destroyed (in svc_destroy).
1786 */
NeilBrownaaf68cf2007-02-08 14:20:30 -08001787 if (!test_and_set_bit(SK_DEAD, &svsk->sk_flags)) {
1788 BUG_ON(atomic_read(&svsk->sk_inuse)<2);
1789 atomic_dec(&svsk->sk_inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if (test_bit(SK_TEMP, &svsk->sk_flags))
1791 serv->sv_tmpcnt--;
NeilBrownaaf68cf2007-02-08 14:20:30 -08001792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Neil Brownd6740df2006-10-29 22:46:45 -08001794 spin_unlock_bh(&serv->sv_lock);
NeilBrownaaf68cf2007-02-08 14:20:30 -08001795}
1796
1797void svc_close_socket(struct svc_sock *svsk)
1798{
1799 set_bit(SK_CLOSE, &svsk->sk_flags);
1800 if (test_and_set_bit(SK_BUSY, &svsk->sk_flags))
1801 /* someone else will have to effect the close */
1802 return;
1803
1804 atomic_inc(&svsk->sk_inuse);
1805 svc_delete_socket(svsk);
1806 clear_bit(SK_BUSY, &svsk->sk_flags);
Neil Brownd6740df2006-10-29 22:46:45 -08001807 svc_sock_put(svsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808}
1809
Chuck Lever6b174332007-02-12 00:53:28 -08001810/**
1811 * svc_makesock - Make a socket for nfsd and lockd
1812 * @serv: RPC server structure
1813 * @protocol: transport protocol to use
1814 * @port: port to use
Chuck Lever482fb942007-02-12 00:53:29 -08001815 * @flags: requested socket characteristics
Chuck Lever6b174332007-02-12 00:53:28 -08001816 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 */
Chuck Lever482fb942007-02-12 00:53:29 -08001818int svc_makesock(struct svc_serv *serv, int protocol, unsigned short port,
1819 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
Chuck Lever6b174332007-02-12 00:53:28 -08001821 struct sockaddr_in sin = {
1822 .sin_family = AF_INET,
1823 .sin_addr.s_addr = INADDR_ANY,
1824 .sin_port = htons(port),
1825 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827 dprintk("svc: creating socket proto = %d\n", protocol);
Chuck Lever77f1f672007-02-12 00:53:39 -08001828 return svc_create_socket(serv, protocol, (struct sockaddr *) &sin,
1829 sizeof(sin), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830}
1831
1832/*
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001833 * Handle defer and revisit of requests
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 */
1835
1836static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
1837{
1838 struct svc_deferred_req *dr = container_of(dreq, struct svc_deferred_req, handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 struct svc_sock *svsk;
1840
1841 if (too_many) {
1842 svc_sock_put(dr->svsk);
1843 kfree(dr);
1844 return;
1845 }
1846 dprintk("revisit queued\n");
1847 svsk = dr->svsk;
1848 dr->svsk = NULL;
Greg Banks1a68d952006-10-02 02:17:55 -07001849 spin_lock_bh(&svsk->sk_defer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 list_add(&dr->handle.recent, &svsk->sk_deferred);
Greg Banks1a68d952006-10-02 02:17:55 -07001851 spin_unlock_bh(&svsk->sk_defer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 set_bit(SK_DEFERRED, &svsk->sk_flags);
1853 svc_sock_enqueue(svsk);
1854 svc_sock_put(svsk);
1855}
1856
1857static struct cache_deferred_req *
1858svc_defer(struct cache_req *req)
1859{
1860 struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
1861 int size = sizeof(struct svc_deferred_req) + (rqstp->rq_arg.len);
1862 struct svc_deferred_req *dr;
1863
1864 if (rqstp->rq_arg.page_len)
1865 return NULL; /* if more than a page, give up FIXME */
1866 if (rqstp->rq_deferred) {
1867 dr = rqstp->rq_deferred;
1868 rqstp->rq_deferred = NULL;
1869 } else {
1870 int skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
1871 /* FIXME maybe discard if size too large */
1872 dr = kmalloc(size, GFP_KERNEL);
1873 if (dr == NULL)
1874 return NULL;
1875
1876 dr->handle.owner = rqstp->rq_server;
1877 dr->prot = rqstp->rq_prot;
Chuck Lever24422222007-02-12 00:53:33 -08001878 memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen);
1879 dr->addrlen = rqstp->rq_addrlen;
J. Bruce Fields1918e342006-01-18 17:43:16 -08001880 dr->daddr = rqstp->rq_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 dr->argslen = rqstp->rq_arg.len >> 2;
1882 memcpy(dr->args, rqstp->rq_arg.head[0].iov_base-skip, dr->argslen<<2);
1883 }
Greg Banksc45c3572006-10-02 02:17:54 -07001884 atomic_inc(&rqstp->rq_sock->sk_inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 dr->svsk = rqstp->rq_sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 dr->handle.revisit = svc_revisit;
1888 return &dr->handle;
1889}
1890
1891/*
1892 * recv data from a deferred request into an active one
1893 */
1894static int svc_deferred_recv(struct svc_rqst *rqstp)
1895{
1896 struct svc_deferred_req *dr = rqstp->rq_deferred;
1897
1898 rqstp->rq_arg.head[0].iov_base = dr->args;
1899 rqstp->rq_arg.head[0].iov_len = dr->argslen<<2;
1900 rqstp->rq_arg.page_len = 0;
1901 rqstp->rq_arg.len = dr->argslen<<2;
1902 rqstp->rq_prot = dr->prot;
Chuck Lever24422222007-02-12 00:53:33 -08001903 memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen);
1904 rqstp->rq_addrlen = dr->addrlen;
J. Bruce Fields1918e342006-01-18 17:43:16 -08001905 rqstp->rq_daddr = dr->daddr;
NeilBrown44524352006-10-04 02:15:46 -07001906 rqstp->rq_respages = rqstp->rq_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 return dr->argslen<<2;
1908}
1909
1910
1911static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk)
1912{
1913 struct svc_deferred_req *dr = NULL;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if (!test_bit(SK_DEFERRED, &svsk->sk_flags))
1916 return NULL;
Greg Banks1a68d952006-10-02 02:17:55 -07001917 spin_lock_bh(&svsk->sk_defer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 clear_bit(SK_DEFERRED, &svsk->sk_flags);
1919 if (!list_empty(&svsk->sk_deferred)) {
1920 dr = list_entry(svsk->sk_deferred.next,
1921 struct svc_deferred_req,
1922 handle.recent);
1923 list_del_init(&dr->handle.recent);
1924 set_bit(SK_DEFERRED, &svsk->sk_flags);
1925 }
Greg Banks1a68d952006-10-02 02:17:55 -07001926 spin_unlock_bh(&svsk->sk_defer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 return dr;
1928}