blob: fdb1386f5dcd7b3570fda8913a184bea6bb06e71 [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.
NeilBrown7ac1bea2007-05-09 02:34:48 -070056 * svc_sock->sk_lock protects the svc_sock->sk_deferred list
57 * and the ->sk_info_authunix cache.
Greg Banksc081a0c2006-10-02 02:17:57 -070058 * svc_sock->sk_flags.SK_BUSY prevents a svc_sock being enqueued multiply.
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 *
60 * Some flags can be set to certain values at any time
61 * providing that certain rules are followed:
62 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * SK_CONN, SK_DATA, can be set or cleared at any time.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -080064 * after a set, svc_sock_enqueue must be called.
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 * after a clear, the socket must be read/accepted
66 * if this succeeds, it must be set again.
67 * SK_CLOSE can set at any time. It is never cleared.
NeilBrownaaf68cf2007-02-08 14:20:30 -080068 * sk_inuse contains a bias of '1' until SK_DEAD is set.
69 * so when sk_inuse hits zero, we know the socket is dead
70 * and no-one is using it.
71 * SK_DEAD can only be set while SK_BUSY is held which ensures
72 * no other thread will be using the socket or will try to
73 * set SK_DEAD.
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 *
75 */
76
77#define RPCDBG_FACILITY RPCDBG_SVCSOCK
78
79
80static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
Chuck Lever6b174332007-02-12 00:53:28 -080081 int *errp, int flags);
NeilBrownaaf68cf2007-02-08 14:20:30 -080082static void svc_delete_socket(struct svc_sock *svsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static void svc_udp_data_ready(struct sock *, int);
84static int svc_udp_recvfrom(struct svc_rqst *);
85static int svc_udp_sendto(struct svc_rqst *);
NeilBrowncda1fd42007-03-06 01:42:22 -080086static void svc_close_socket(struct svc_sock *svsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk);
89static int svc_deferred_recv(struct svc_rqst *rqstp);
90static struct cache_deferred_req *svc_defer(struct cache_req *req);
91
Greg Banks36bdfc82006-10-02 02:17:54 -070092/* apparently the "standard" is that clients close
93 * idle connections after 5 minutes, servers after
94 * 6 minutes
95 * http://www.connectathon.org/talks96/nfstcp.pdf
96 */
97static int svc_conn_age_period = 6*60;
98
Peter Zijlstraed075362006-12-06 20:35:24 -080099#ifdef CONFIG_DEBUG_LOCK_ALLOC
100static struct lock_class_key svc_key[2];
101static struct lock_class_key svc_slock_key[2];
102
103static inline void svc_reclassify_socket(struct socket *sock)
104{
105 struct sock *sk = sock->sk;
106 BUG_ON(sk->sk_lock.owner != NULL);
107 switch (sk->sk_family) {
108 case AF_INET:
109 sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
110 &svc_slock_key[0], "sk_lock-AF_INET-NFSD", &svc_key[0]);
111 break;
112
113 case AF_INET6:
114 sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
115 &svc_slock_key[1], "sk_lock-AF_INET6-NFSD", &svc_key[1]);
116 break;
117
118 default:
119 BUG();
120 }
121}
122#else
123static inline void svc_reclassify_socket(struct socket *sock)
124{
125}
126#endif
127
Chuck Leverad06e4b2007-02-12 00:53:32 -0800128static char *__svc_print_addr(struct sockaddr *addr, char *buf, size_t len)
129{
130 switch (addr->sa_family) {
131 case AF_INET:
132 snprintf(buf, len, "%u.%u.%u.%u, port=%u",
133 NIPQUAD(((struct sockaddr_in *) addr)->sin_addr),
134 htons(((struct sockaddr_in *) addr)->sin_port));
135 break;
NeilBrown5a05ed72007-03-06 01:42:22 -0800136
Chuck Leverad06e4b2007-02-12 00:53:32 -0800137 case AF_INET6:
138 snprintf(buf, len, "%x:%x:%x:%x:%x:%x:%x:%x, port=%u",
139 NIP6(((struct sockaddr_in6 *) addr)->sin6_addr),
140 htons(((struct sockaddr_in6 *) addr)->sin6_port));
141 break;
NeilBrown5a05ed72007-03-06 01:42:22 -0800142
Chuck Leverad06e4b2007-02-12 00:53:32 -0800143 default:
144 snprintf(buf, len, "unknown address type: %d", addr->sa_family);
145 break;
146 }
147 return buf;
148}
149
150/**
151 * svc_print_addr - Format rq_addr field for printing
152 * @rqstp: svc_rqst struct containing address to print
153 * @buf: target buffer for formatted address
154 * @len: length of target buffer
155 *
156 */
157char *svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len)
158{
Chuck Lever27459f02007-02-12 00:53:34 -0800159 return __svc_print_addr(svc_addr(rqstp), buf, len);
Chuck Leverad06e4b2007-02-12 00:53:32 -0800160}
161EXPORT_SYMBOL_GPL(svc_print_addr);
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/*
Greg Banks3262c812006-10-02 02:17:58 -0700164 * Queue up an idle server thread. Must have pool->sp_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 * Note: this is really a stack rather than a queue, so that we only
Greg Banks3262c812006-10-02 02:17:58 -0700166 * use as many different threads as we need, and the rest don't pollute
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 * the cache.
168 */
169static inline void
Greg Banks3262c812006-10-02 02:17:58 -0700170svc_thread_enqueue(struct svc_pool *pool, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Greg Banks3262c812006-10-02 02:17:58 -0700172 list_add(&rqstp->rq_list, &pool->sp_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175/*
Greg Banks3262c812006-10-02 02:17:58 -0700176 * Dequeue an nfsd thread. Must have pool->sp_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 */
178static inline void
Greg Banks3262c812006-10-02 02:17:58 -0700179svc_thread_dequeue(struct svc_pool *pool, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 list_del(&rqstp->rq_list);
182}
183
184/*
185 * Release an skbuff after use
186 */
187static inline void
188svc_release_skb(struct svc_rqst *rqstp)
189{
190 struct sk_buff *skb = rqstp->rq_skbuff;
191 struct svc_deferred_req *dr = rqstp->rq_deferred;
192
193 if (skb) {
194 rqstp->rq_skbuff = NULL;
195
196 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
197 skb_free_datagram(rqstp->rq_sock->sk_sk, skb);
198 }
199 if (dr) {
200 rqstp->rq_deferred = NULL;
201 kfree(dr);
202 }
203}
204
205/*
206 * Any space to write?
207 */
208static inline unsigned long
209svc_sock_wspace(struct svc_sock *svsk)
210{
211 int wspace;
212
213 if (svsk->sk_sock->type == SOCK_STREAM)
214 wspace = sk_stream_wspace(svsk->sk_sk);
215 else
216 wspace = sock_wspace(svsk->sk_sk);
217
218 return wspace;
219}
220
221/*
222 * Queue up a socket with data pending. If there are idle nfsd
223 * processes, wake 'em up.
224 *
225 */
226static void
227svc_sock_enqueue(struct svc_sock *svsk)
228{
229 struct svc_serv *serv = svsk->sk_server;
Greg Banksbfd24162006-10-02 02:18:01 -0700230 struct svc_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 struct svc_rqst *rqstp;
Greg Banksbfd24162006-10-02 02:18:01 -0700232 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 if (!(svsk->sk_flags &
235 ( (1<<SK_CONN)|(1<<SK_DATA)|(1<<SK_CLOSE)|(1<<SK_DEFERRED)) ))
236 return;
237 if (test_bit(SK_DEAD, &svsk->sk_flags))
238 return;
239
Greg Banksbfd24162006-10-02 02:18:01 -0700240 cpu = get_cpu();
241 pool = svc_pool_for_cpu(svsk->sk_server, cpu);
242 put_cpu();
243
Greg Banks3262c812006-10-02 02:17:58 -0700244 spin_lock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Greg Banks3262c812006-10-02 02:17:58 -0700246 if (!list_empty(&pool->sp_threads) &&
247 !list_empty(&pool->sp_sockets))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 printk(KERN_ERR
249 "svc_sock_enqueue: threads and sockets both waiting??\n");
250
251 if (test_bit(SK_DEAD, &svsk->sk_flags)) {
252 /* Don't enqueue dead sockets */
253 dprintk("svc: socket %p is dead, not enqueued\n", svsk->sk_sk);
254 goto out_unlock;
255 }
256
Greg Banksc081a0c2006-10-02 02:17:57 -0700257 /* Mark socket as busy. It will remain in this state until the
258 * server has processed all pending data and put the socket back
259 * on the idle list. We update SK_BUSY atomically because
260 * it also guards against trying to enqueue the svc_sock twice.
261 */
262 if (test_and_set_bit(SK_BUSY, &svsk->sk_flags)) {
263 /* Don't enqueue socket while already enqueued */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 dprintk("svc: socket %p busy, not enqueued\n", svsk->sk_sk);
265 goto out_unlock;
266 }
Greg Banks3262c812006-10-02 02:17:58 -0700267 BUG_ON(svsk->sk_pool != NULL);
268 svsk->sk_pool = pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700271 if (((atomic_read(&svsk->sk_reserved) + serv->sv_max_mesg)*2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 > svc_sock_wspace(svsk))
273 && !test_bit(SK_CLOSE, &svsk->sk_flags)
274 && !test_bit(SK_CONN, &svsk->sk_flags)) {
275 /* Don't enqueue while not enough space for reply */
276 dprintk("svc: socket %p no space, %d*2 > %ld, not enqueued\n",
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700277 svsk->sk_sk, atomic_read(&svsk->sk_reserved)+serv->sv_max_mesg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 svc_sock_wspace(svsk));
Greg Banks3262c812006-10-02 02:17:58 -0700279 svsk->sk_pool = NULL;
Greg Banksc081a0c2006-10-02 02:17:57 -0700280 clear_bit(SK_BUSY, &svsk->sk_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 goto out_unlock;
282 }
283 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Greg Banks3262c812006-10-02 02:17:58 -0700286 if (!list_empty(&pool->sp_threads)) {
287 rqstp = list_entry(pool->sp_threads.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 struct svc_rqst,
289 rq_list);
290 dprintk("svc: socket %p served by daemon %p\n",
291 svsk->sk_sk, rqstp);
Greg Banks3262c812006-10-02 02:17:58 -0700292 svc_thread_dequeue(pool, rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (rqstp->rq_sock)
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800294 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 "svc_sock_enqueue: server %p, rq_sock=%p!\n",
296 rqstp, rqstp->rq_sock);
297 rqstp->rq_sock = svsk;
Greg Banksc45c3572006-10-02 02:17:54 -0700298 atomic_inc(&svsk->sk_inuse);
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700299 rqstp->rq_reserved = serv->sv_max_mesg;
Greg Banks5685f0f2006-10-02 02:17:56 -0700300 atomic_add(rqstp->rq_reserved, &svsk->sk_reserved);
Greg Banks3262c812006-10-02 02:17:58 -0700301 BUG_ON(svsk->sk_pool != pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 wake_up(&rqstp->rq_wait);
303 } else {
304 dprintk("svc: socket %p put into queue\n", svsk->sk_sk);
Greg Banks3262c812006-10-02 02:17:58 -0700305 list_add_tail(&svsk->sk_ready, &pool->sp_sockets);
306 BUG_ON(svsk->sk_pool != pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308
309out_unlock:
Greg Banks3262c812006-10-02 02:17:58 -0700310 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313/*
Greg Banks3262c812006-10-02 02:17:58 -0700314 * Dequeue the first socket. Must be called with the pool->sp_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 */
316static inline struct svc_sock *
Greg Banks3262c812006-10-02 02:17:58 -0700317svc_sock_dequeue(struct svc_pool *pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 struct svc_sock *svsk;
320
Greg Banks3262c812006-10-02 02:17:58 -0700321 if (list_empty(&pool->sp_sockets))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return NULL;
323
Greg Banks3262c812006-10-02 02:17:58 -0700324 svsk = list_entry(pool->sp_sockets.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 struct svc_sock, sk_ready);
326 list_del_init(&svsk->sk_ready);
327
328 dprintk("svc: socket %p dequeued, inuse=%d\n",
Greg Banksc45c3572006-10-02 02:17:54 -0700329 svsk->sk_sk, atomic_read(&svsk->sk_inuse));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 return svsk;
332}
333
334/*
335 * Having read something from a socket, check whether it
336 * needs to be re-enqueued.
337 * Note: SK_DATA only gets cleared when a read-attempt finds
338 * no (or insufficient) data.
339 */
340static inline void
341svc_sock_received(struct svc_sock *svsk)
342{
Greg Banks3262c812006-10-02 02:17:58 -0700343 svsk->sk_pool = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 clear_bit(SK_BUSY, &svsk->sk_flags);
345 svc_sock_enqueue(svsk);
346}
347
348
349/**
350 * svc_reserve - change the space reserved for the reply to a request.
351 * @rqstp: The request in question
352 * @space: new max space to reserve
353 *
354 * Each request reserves some space on the output queue of the socket
355 * to make sure the reply fits. This function reduces that reserved
356 * space to be the amount of space used already, plus @space.
357 *
358 */
359void svc_reserve(struct svc_rqst *rqstp, int space)
360{
361 space += rqstp->rq_res.head[0].iov_len;
362
363 if (space < rqstp->rq_reserved) {
364 struct svc_sock *svsk = rqstp->rq_sock;
Greg Banks5685f0f2006-10-02 02:17:56 -0700365 atomic_sub((rqstp->rq_reserved - space), &svsk->sk_reserved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 rqstp->rq_reserved = space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 svc_sock_enqueue(svsk);
369 }
370}
371
372/*
373 * Release a socket after use.
374 */
375static inline void
376svc_sock_put(struct svc_sock *svsk)
377{
NeilBrownaaf68cf2007-02-08 14:20:30 -0800378 if (atomic_dec_and_test(&svsk->sk_inuse)) {
379 BUG_ON(! test_bit(SK_DEAD, &svsk->sk_flags));
380
Andrew Morton202dd452006-10-29 22:57:57 -0800381 dprintk("svc: releasing dead socket\n");
Neil Brownd6740df2006-10-29 22:46:45 -0800382 if (svsk->sk_sock->file)
383 sockfd_put(svsk->sk_sock);
384 else
385 sock_release(svsk->sk_sock);
386 if (svsk->sk_info_authunix != NULL)
387 svcauth_unix_info_release(svsk->sk_info_authunix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 kfree(svsk);
389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
392static void
393svc_sock_release(struct svc_rqst *rqstp)
394{
395 struct svc_sock *svsk = rqstp->rq_sock;
396
397 svc_release_skb(rqstp);
398
NeilBrown44524352006-10-04 02:15:46 -0700399 svc_free_res_pages(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 rqstp->rq_res.page_len = 0;
401 rqstp->rq_res.page_base = 0;
402
403
404 /* Reset response buffer and release
405 * the reservation.
406 * But first, check that enough space was reserved
407 * for the reply, otherwise we have a bug!
408 */
409 if ((rqstp->rq_res.len) > rqstp->rq_reserved)
410 printk(KERN_ERR "RPC request reserved %d but used %d\n",
411 rqstp->rq_reserved,
412 rqstp->rq_res.len);
413
414 rqstp->rq_res.head[0].iov_len = 0;
415 svc_reserve(rqstp, 0);
416 rqstp->rq_sock = NULL;
417
418 svc_sock_put(svsk);
419}
420
421/*
422 * External function to wake up a server waiting for data
Greg Banks3262c812006-10-02 02:17:58 -0700423 * This really only makes sense for services like lockd
424 * which have exactly one thread anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 */
426void
427svc_wake_up(struct svc_serv *serv)
428{
429 struct svc_rqst *rqstp;
Greg Banks3262c812006-10-02 02:17:58 -0700430 unsigned int i;
431 struct svc_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Greg Banks3262c812006-10-02 02:17:58 -0700433 for (i = 0; i < serv->sv_nrpools; i++) {
434 pool = &serv->sv_pools[i];
435
436 spin_lock_bh(&pool->sp_lock);
437 if (!list_empty(&pool->sp_threads)) {
438 rqstp = list_entry(pool->sp_threads.next,
439 struct svc_rqst,
440 rq_list);
441 dprintk("svc: daemon %p woken up.\n", rqstp);
442 /*
443 svc_thread_dequeue(pool, rqstp);
444 rqstp->rq_sock = NULL;
445 */
446 wake_up(&rqstp->rq_wait);
447 }
448 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Chuck Leverb92503b2007-02-12 00:53:36 -0800452union svc_pktinfo_u {
453 struct in_pktinfo pkti;
Chuck Leverb92503b2007-02-12 00:53:36 -0800454 struct in6_pktinfo pkti6;
Chuck Leverb92503b2007-02-12 00:53:36 -0800455};
David S. Millerbc375ea2007-04-12 13:35:59 -0700456#define SVC_PKTINFO_SPACE \
457 CMSG_SPACE(sizeof(union svc_pktinfo_u))
Chuck Leverb92503b2007-02-12 00:53:36 -0800458
459static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
460{
461 switch (rqstp->rq_sock->sk_sk->sk_family) {
462 case AF_INET: {
463 struct in_pktinfo *pki = CMSG_DATA(cmh);
464
465 cmh->cmsg_level = SOL_IP;
466 cmh->cmsg_type = IP_PKTINFO;
467 pki->ipi_ifindex = 0;
468 pki->ipi_spec_dst.s_addr = rqstp->rq_daddr.addr.s_addr;
469 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
470 }
471 break;
NeilBrown5a05ed72007-03-06 01:42:22 -0800472
Chuck Leverb92503b2007-02-12 00:53:36 -0800473 case AF_INET6: {
474 struct in6_pktinfo *pki = CMSG_DATA(cmh);
475
476 cmh->cmsg_level = SOL_IPV6;
477 cmh->cmsg_type = IPV6_PKTINFO;
478 pki->ipi6_ifindex = 0;
479 ipv6_addr_copy(&pki->ipi6_addr,
480 &rqstp->rq_daddr.addr6);
481 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
482 }
483 break;
Chuck Leverb92503b2007-02-12 00:53:36 -0800484 }
485 return;
486}
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488/*
489 * Generic sendto routine
490 */
491static int
492svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
493{
494 struct svc_sock *svsk = rqstp->rq_sock;
495 struct socket *sock = svsk->sk_sock;
496 int slen;
David S. Millerbc375ea2007-04-12 13:35:59 -0700497 union {
498 struct cmsghdr hdr;
499 long all[SVC_PKTINFO_SPACE / sizeof(long)];
500 } buffer;
501 struct cmsghdr *cmh = &buffer.hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 int len = 0;
503 int result;
504 int size;
505 struct page **ppage = xdr->pages;
506 size_t base = xdr->page_base;
507 unsigned int pglen = xdr->page_len;
508 unsigned int flags = MSG_MORE;
Chuck Leverad06e4b2007-02-12 00:53:32 -0800509 char buf[RPC_MAX_ADDRBUFLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 slen = xdr->len;
512
513 if (rqstp->rq_prot == IPPROTO_UDP) {
Chuck Leverb92503b2007-02-12 00:53:36 -0800514 struct msghdr msg = {
515 .msg_name = &rqstp->rq_addr,
516 .msg_namelen = rqstp->rq_addrlen,
517 .msg_control = cmh,
518 .msg_controllen = sizeof(buffer),
519 .msg_flags = MSG_MORE,
520 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Chuck Leverb92503b2007-02-12 00:53:36 -0800522 svc_set_cmsg_data(rqstp, cmh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 if (sock_sendmsg(sock, &msg, 0) < 0)
525 goto out;
526 }
527
528 /* send head */
529 if (slen == xdr->head[0].iov_len)
530 flags = 0;
NeilBrown44524352006-10-04 02:15:46 -0700531 len = kernel_sendpage(sock, rqstp->rq_respages[0], 0,
532 xdr->head[0].iov_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (len != xdr->head[0].iov_len)
534 goto out;
535 slen -= xdr->head[0].iov_len;
536 if (slen == 0)
537 goto out;
538
539 /* send page data */
540 size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
541 while (pglen > 0) {
542 if (slen == size)
543 flags = 0;
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700544 result = kernel_sendpage(sock, *ppage, base, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (result > 0)
546 len += result;
547 if (result != size)
548 goto out;
549 slen -= size;
550 pglen -= size;
551 size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
552 base = 0;
553 ppage++;
554 }
555 /* send tail */
556 if (xdr->tail[0].iov_len) {
NeilBrown44524352006-10-04 02:15:46 -0700557 result = kernel_sendpage(sock, rqstp->rq_respages[0],
558 ((unsigned long)xdr->tail[0].iov_base)
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800559 & (PAGE_SIZE-1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 xdr->tail[0].iov_len, 0);
561
562 if (result > 0)
563 len += result;
564 }
565out:
Chuck Leverad06e4b2007-02-12 00:53:32 -0800566 dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %s)\n",
567 rqstp->rq_sock, xdr->head[0].iov_base, xdr->head[0].iov_len,
568 xdr->len, len, svc_print_addr(rqstp, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 return len;
571}
572
573/*
NeilBrown80212d52006-10-02 02:17:47 -0700574 * Report socket names for nfsdfs
575 */
576static int one_sock_name(char *buf, struct svc_sock *svsk)
577{
578 int len;
579
580 switch(svsk->sk_sk->sk_family) {
581 case AF_INET:
582 len = sprintf(buf, "ipv4 %s %u.%u.%u.%u %d\n",
583 svsk->sk_sk->sk_protocol==IPPROTO_UDP?
584 "udp" : "tcp",
585 NIPQUAD(inet_sk(svsk->sk_sk)->rcv_saddr),
586 inet_sk(svsk->sk_sk)->num);
587 break;
588 default:
589 len = sprintf(buf, "*unknown-%d*\n",
590 svsk->sk_sk->sk_family);
591 }
592 return len;
593}
594
595int
NeilBrownb41b66d2006-10-02 02:17:48 -0700596svc_sock_names(char *buf, struct svc_serv *serv, char *toclose)
NeilBrown80212d52006-10-02 02:17:47 -0700597{
NeilBrownb41b66d2006-10-02 02:17:48 -0700598 struct svc_sock *svsk, *closesk = NULL;
NeilBrown80212d52006-10-02 02:17:47 -0700599 int len = 0;
600
601 if (!serv)
602 return 0;
NeilBrownaaf68cf2007-02-08 14:20:30 -0800603 spin_lock_bh(&serv->sv_lock);
NeilBrown80212d52006-10-02 02:17:47 -0700604 list_for_each_entry(svsk, &serv->sv_permsocks, sk_list) {
605 int onelen = one_sock_name(buf+len, svsk);
NeilBrownb41b66d2006-10-02 02:17:48 -0700606 if (toclose && strcmp(toclose, buf+len) == 0)
607 closesk = svsk;
608 else
609 len += onelen;
NeilBrown80212d52006-10-02 02:17:47 -0700610 }
NeilBrownaaf68cf2007-02-08 14:20:30 -0800611 spin_unlock_bh(&serv->sv_lock);
NeilBrownb41b66d2006-10-02 02:17:48 -0700612 if (closesk)
NeilBrown5680c442006-10-04 02:15:45 -0700613 /* Should unregister with portmap, but you cannot
614 * unregister just one protocol...
615 */
NeilBrownaaf68cf2007-02-08 14:20:30 -0800616 svc_close_socket(closesk);
NeilBrown37a03472006-10-04 02:15:44 -0700617 else if (toclose)
618 return -ENOENT;
NeilBrown80212d52006-10-02 02:17:47 -0700619 return len;
620}
621EXPORT_SYMBOL(svc_sock_names);
622
623/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 * Check input queue length
625 */
626static int
627svc_recv_available(struct svc_sock *svsk)
628{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 struct socket *sock = svsk->sk_sock;
630 int avail, err;
631
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700632 err = kernel_sock_ioctl(sock, TIOCINQ, (unsigned long) &avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 return (err >= 0)? avail : err;
635}
636
637/*
638 * Generic recvfrom routine.
639 */
640static int
641svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr, int buflen)
642{
Chuck Lever067d7812007-02-12 00:53:30 -0800643 struct svc_sock *svsk = rqstp->rq_sock;
Chuck Lever1ba95102007-02-12 00:53:31 -0800644 struct msghdr msg = {
645 .msg_flags = MSG_DONTWAIT,
646 };
647 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Chuck Lever1ba95102007-02-12 00:53:31 -0800649 len = kernel_recvmsg(svsk->sk_sock, &msg, iov, nr, buflen,
650 msg.msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 /* sock_recvmsg doesn't fill in the name/namelen, so we must..
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 */
Chuck Lever067d7812007-02-12 00:53:30 -0800654 memcpy(&rqstp->rq_addr, &svsk->sk_remote, svsk->sk_remotelen);
655 rqstp->rq_addrlen = svsk->sk_remotelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
Chuck Lever1ba95102007-02-12 00:53:31 -0800658 svsk, iov[0].iov_base, iov[0].iov_len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 return len;
661}
662
663/*
664 * Set socket snd and rcv buffer lengths
665 */
666static inline void
667svc_sock_setbufsize(struct socket *sock, unsigned int snd, unsigned int rcv)
668{
669#if 0
670 mm_segment_t oldfs;
671 oldfs = get_fs(); set_fs(KERNEL_DS);
672 sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
673 (char*)&snd, sizeof(snd));
674 sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
675 (char*)&rcv, sizeof(rcv));
676#else
677 /* sock_setsockopt limits use to sysctl_?mem_max,
678 * which isn't acceptable. Until that is made conditional
679 * on not having CAP_SYS_RESOURCE or similar, we go direct...
680 * DaveM said I could!
681 */
682 lock_sock(sock->sk);
683 sock->sk->sk_sndbuf = snd * 2;
684 sock->sk->sk_rcvbuf = rcv * 2;
685 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK|SOCK_RCVBUF_LOCK;
686 release_sock(sock->sk);
687#endif
688}
689/*
690 * INET callback when data has been received on the socket.
691 */
692static void
693svc_udp_data_ready(struct sock *sk, int count)
694{
Neil Brown939bb7e2005-09-13 01:25:39 -0700695 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Neil Brown939bb7e2005-09-13 01:25:39 -0700697 if (svsk) {
698 dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
699 svsk, sk, count, test_bit(SK_BUSY, &svsk->sk_flags));
700 set_bit(SK_DATA, &svsk->sk_flags);
701 svc_sock_enqueue(svsk);
702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
704 wake_up_interruptible(sk->sk_sleep);
705}
706
707/*
708 * INET callback when space is newly available on the socket.
709 */
710static void
711svc_write_space(struct sock *sk)
712{
713 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
714
715 if (svsk) {
716 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
717 svsk, sk, test_bit(SK_BUSY, &svsk->sk_flags));
718 svc_sock_enqueue(svsk);
719 }
720
721 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) {
Neil Brown939bb7e2005-09-13 01:25:39 -0700722 dprintk("RPC svc_write_space: someone sleeping on %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 svsk);
724 wake_up_interruptible(sk->sk_sleep);
725 }
726}
727
NeilBrown7a37f572007-03-06 01:42:21 -0800728static inline void svc_udp_get_dest_address(struct svc_rqst *rqstp,
729 struct cmsghdr *cmh)
Chuck Lever95756482007-02-12 00:53:38 -0800730{
731 switch (rqstp->rq_sock->sk_sk->sk_family) {
732 case AF_INET: {
NeilBrown7a37f572007-03-06 01:42:21 -0800733 struct in_pktinfo *pki = CMSG_DATA(cmh);
734 rqstp->rq_daddr.addr.s_addr = pki->ipi_spec_dst.s_addr;
Chuck Lever95756482007-02-12 00:53:38 -0800735 break;
NeilBrown7a37f572007-03-06 01:42:21 -0800736 }
Chuck Lever95756482007-02-12 00:53:38 -0800737 case AF_INET6: {
NeilBrown7a37f572007-03-06 01:42:21 -0800738 struct in6_pktinfo *pki = CMSG_DATA(cmh);
739 ipv6_addr_copy(&rqstp->rq_daddr.addr6, &pki->ipi6_addr);
Chuck Lever95756482007-02-12 00:53:38 -0800740 break;
NeilBrown7a37f572007-03-06 01:42:21 -0800741 }
Chuck Lever95756482007-02-12 00:53:38 -0800742 }
Chuck Lever95756482007-02-12 00:53:38 -0800743}
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745/*
746 * Receive a datagram from a UDP socket.
747 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748static int
749svc_udp_recvfrom(struct svc_rqst *rqstp)
750{
751 struct svc_sock *svsk = rqstp->rq_sock;
752 struct svc_serv *serv = svsk->sk_server;
753 struct sk_buff *skb;
David S. Millerbc375ea2007-04-12 13:35:59 -0700754 union {
755 struct cmsghdr hdr;
756 long all[SVC_PKTINFO_SPACE / sizeof(long)];
757 } buffer;
758 struct cmsghdr *cmh = &buffer.hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 int err, len;
NeilBrown7a37f572007-03-06 01:42:21 -0800760 struct msghdr msg = {
761 .msg_name = svc_addr(rqstp),
762 .msg_control = cmh,
763 .msg_controllen = sizeof(buffer),
764 .msg_flags = MSG_DONTWAIT,
765 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
768 /* udp sockets need large rcvbuf as all pending
769 * requests are still in that buffer. sndbuf must
770 * also be large enough that there is enough space
Greg Banks3262c812006-10-02 02:17:58 -0700771 * for one reply per thread. We count all threads
772 * rather than threads in a particular pool, which
773 * provides an upper bound on the number of threads
774 * which will access the socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 */
776 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700777 (serv->sv_nrthreads+3) * serv->sv_max_mesg,
778 (serv->sv_nrthreads+3) * serv->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
781 svc_sock_received(svsk);
782 return svc_deferred_recv(rqstp);
783 }
784
NeilBrownaaf68cf2007-02-08 14:20:30 -0800785 if (test_bit(SK_CLOSE, &svsk->sk_flags)) {
786 svc_delete_socket(svsk);
787 return 0;
788 }
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 clear_bit(SK_DATA, &svsk->sk_flags);
Adrian Bunk418106d2007-04-04 19:08:21 -0700791 while ((err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
792 0, 0, MSG_PEEK | MSG_DONTWAIT)) < 0 ||
NeilBrown7a37f572007-03-06 01:42:21 -0800793 (skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (err == -EAGAIN) {
795 svc_sock_received(svsk);
796 return err;
797 }
798 /* possibly an icmp error */
799 dprintk("svc: recvfrom returned error %d\n", -err);
800 }
NeilBrown7a37f572007-03-06 01:42:21 -0800801 rqstp->rq_addrlen = sizeof(rqstp->rq_addr);
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700802 if (skb->tstamp.tv64 == 0) {
803 skb->tstamp = ktime_get_real();
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800804 /* Don't enable netstamp, sunrpc doesn't
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 need that much accuracy */
806 }
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700807 svsk->sk_sk->sk_stamp = skb->tstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 set_bit(SK_DATA, &svsk->sk_flags); /* there may be more data... */
809
810 /*
811 * Maybe more packets - kick another thread ASAP.
812 */
813 svc_sock_received(svsk);
814
815 len = skb->len - sizeof(struct udphdr);
816 rqstp->rq_arg.len = len;
817
Chuck Lever95756482007-02-12 00:53:38 -0800818 rqstp->rq_prot = IPPROTO_UDP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
NeilBrown7a37f572007-03-06 01:42:21 -0800820 if (cmh->cmsg_level != IPPROTO_IP ||
821 cmh->cmsg_type != IP_PKTINFO) {
822 if (net_ratelimit())
823 printk("rpcsvc: received unknown control message:"
824 "%d/%d\n",
825 cmh->cmsg_level, cmh->cmsg_type);
826 skb_free_datagram(svsk->sk_sk, skb);
827 return 0;
828 }
829 svc_udp_get_dest_address(rqstp, cmh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 if (skb_is_nonlinear(skb)) {
832 /* we have to copy */
833 local_bh_disable();
834 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
835 local_bh_enable();
836 /* checksum error */
837 skb_free_datagram(svsk->sk_sk, skb);
838 return 0;
839 }
840 local_bh_enable();
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800841 skb_free_datagram(svsk->sk_sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 } else {
843 /* we can use it in-place */
844 rqstp->rq_arg.head[0].iov_base = skb->data + sizeof(struct udphdr);
845 rqstp->rq_arg.head[0].iov_len = len;
Herbert Xufb286bb2005-11-10 13:01:24 -0800846 if (skb_checksum_complete(skb)) {
847 skb_free_datagram(svsk->sk_sk, skb);
848 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850 rqstp->rq_skbuff = skb;
851 }
852
853 rqstp->rq_arg.page_base = 0;
854 if (len <= rqstp->rq_arg.head[0].iov_len) {
855 rqstp->rq_arg.head[0].iov_len = len;
856 rqstp->rq_arg.page_len = 0;
NeilBrown44524352006-10-04 02:15:46 -0700857 rqstp->rq_respages = rqstp->rq_pages+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 } else {
859 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
NeilBrown44524352006-10-04 02:15:46 -0700860 rqstp->rq_respages = rqstp->rq_pages + 1 +
861 (rqstp->rq_arg.page_len + PAGE_SIZE - 1)/ PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863
864 if (serv->sv_stats)
865 serv->sv_stats->netudpcnt++;
866
867 return len;
868}
869
870static int
871svc_udp_sendto(struct svc_rqst *rqstp)
872{
873 int error;
874
875 error = svc_sendto(rqstp, &rqstp->rq_res);
876 if (error == -ECONNREFUSED)
877 /* ICMP error on earlier request. */
878 error = svc_sendto(rqstp, &rqstp->rq_res);
879
880 return error;
881}
882
883static void
884svc_udp_init(struct svc_sock *svsk)
885{
NeilBrown7a37f572007-03-06 01:42:21 -0800886 int one = 1;
887 mm_segment_t oldfs;
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 svsk->sk_sk->sk_data_ready = svc_udp_data_ready;
890 svsk->sk_sk->sk_write_space = svc_write_space;
891 svsk->sk_recvfrom = svc_udp_recvfrom;
892 svsk->sk_sendto = svc_udp_sendto;
893
894 /* initialise setting must have enough space to
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800895 * receive and respond to one request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 * svc_udp_recvfrom will re-adjust if necessary
897 */
898 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700899 3 * svsk->sk_server->sv_max_mesg,
900 3 * svsk->sk_server->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 set_bit(SK_DATA, &svsk->sk_flags); /* might have come in before data_ready set up */
903 set_bit(SK_CHNGBUF, &svsk->sk_flags);
NeilBrown7a37f572007-03-06 01:42:21 -0800904
905 oldfs = get_fs();
906 set_fs(KERNEL_DS);
907 /* make sure we get destination address info */
908 svsk->sk_sock->ops->setsockopt(svsk->sk_sock, IPPROTO_IP, IP_PKTINFO,
909 (char __user *)&one, sizeof(one));
910 set_fs(oldfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
913/*
914 * A data_ready event on a listening socket means there's a connection
915 * pending. Do not use state_change as a substitute for it.
916 */
917static void
918svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
919{
Neil Brown939bb7e2005-09-13 01:25:39 -0700920 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 dprintk("svc: socket %p TCP (listen) state change %d\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700923 sk, sk->sk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Neil Brown939bb7e2005-09-13 01:25:39 -0700925 /*
926 * This callback may called twice when a new connection
927 * is established as a child socket inherits everything
928 * from a parent LISTEN socket.
929 * 1) data_ready method of the parent socket will be called
930 * when one of child sockets become ESTABLISHED.
931 * 2) data_ready method of the child socket may be called
932 * when it receives data before the socket is accepted.
933 * In case of 2, we should ignore it silently.
934 */
935 if (sk->sk_state == TCP_LISTEN) {
936 if (svsk) {
937 set_bit(SK_CONN, &svsk->sk_flags);
938 svc_sock_enqueue(svsk);
939 } else
940 printk("svc: socket %p: no user data\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
Neil Brown939bb7e2005-09-13 01:25:39 -0700942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
944 wake_up_interruptible_all(sk->sk_sleep);
945}
946
947/*
948 * A state change on a connected socket means it's dying or dead.
949 */
950static void
951svc_tcp_state_change(struct sock *sk)
952{
Neil Brown939bb7e2005-09-13 01:25:39 -0700953 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700956 sk, sk->sk_state, sk->sk_user_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Neil Brown939bb7e2005-09-13 01:25:39 -0700958 if (!svsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 printk("svc: socket %p: no user data\n", sk);
Neil Brown939bb7e2005-09-13 01:25:39 -0700960 else {
961 set_bit(SK_CLOSE, &svsk->sk_flags);
962 svc_sock_enqueue(svsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
965 wake_up_interruptible_all(sk->sk_sleep);
966}
967
968static void
969svc_tcp_data_ready(struct sock *sk, int count)
970{
Neil Brown939bb7e2005-09-13 01:25:39 -0700971 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 dprintk("svc: socket %p TCP data ready (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700974 sk, sk->sk_user_data);
975 if (svsk) {
976 set_bit(SK_DATA, &svsk->sk_flags);
977 svc_sock_enqueue(svsk);
978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
980 wake_up_interruptible(sk->sk_sleep);
981}
982
Chuck Leverbcdb81a2007-02-12 00:53:37 -0800983static inline int svc_port_is_privileged(struct sockaddr *sin)
984{
985 switch (sin->sa_family) {
986 case AF_INET:
987 return ntohs(((struct sockaddr_in *)sin)->sin_port)
988 < PROT_SOCK;
Chuck Leverbcdb81a2007-02-12 00:53:37 -0800989 case AF_INET6:
990 return ntohs(((struct sockaddr_in6 *)sin)->sin6_port)
991 < PROT_SOCK;
Chuck Leverbcdb81a2007-02-12 00:53:37 -0800992 default:
993 return 0;
994 }
995}
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997/*
998 * Accept a TCP connection
999 */
1000static void
1001svc_tcp_accept(struct svc_sock *svsk)
1002{
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001003 struct sockaddr_storage addr;
1004 struct sockaddr *sin = (struct sockaddr *) &addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 struct svc_serv *serv = svsk->sk_server;
1006 struct socket *sock = svsk->sk_sock;
1007 struct socket *newsock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 struct svc_sock *newsvsk;
1009 int err, slen;
Chuck Leverad06e4b2007-02-12 00:53:32 -08001010 char buf[RPC_MAX_ADDRBUFLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
1013 if (!sock)
1014 return;
1015
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001016 clear_bit(SK_CONN, &svsk->sk_flags);
1017 err = kernel_accept(sock, &newsock, O_NONBLOCK);
1018 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (err == -ENOMEM)
1020 printk(KERN_WARNING "%s: no more sockets!\n",
1021 serv->sv_name);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001022 else if (err != -EAGAIN && net_ratelimit())
1023 printk(KERN_WARNING "%s: accept failed (err %d)!\n",
1024 serv->sv_name, -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return;
1026 }
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 set_bit(SK_CONN, &svsk->sk_flags);
1029 svc_sock_enqueue(svsk);
1030
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001031 err = kernel_getpeername(newsock, sin, &slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (err < 0) {
1033 if (net_ratelimit())
1034 printk(KERN_WARNING "%s: peername failed (err %d)!\n",
1035 serv->sv_name, -err);
1036 goto failed; /* aborted connection or whatever */
1037 }
1038
1039 /* Ideally, we would want to reject connections from unauthorized
Chuck Leverad06e4b2007-02-12 00:53:32 -08001040 * hosts here, but when we get encryption, the IP of the host won't
1041 * tell us anything. For now just warn about unpriv connections.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 */
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001043 if (!svc_port_is_privileged(sin)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 dprintk(KERN_WARNING
Chuck Leverad06e4b2007-02-12 00:53:32 -08001045 "%s: connect from unprivileged port: %s\n",
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001046 serv->sv_name,
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001047 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
Chuck Leverad06e4b2007-02-12 00:53:32 -08001049 dprintk("%s: connect from %s\n", serv->sv_name,
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001050 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 /* make sure that a write doesn't block forever when
1053 * low on memory
1054 */
1055 newsock->sk->sk_sndtimeo = HZ*30;
1056
Chuck Lever6b174332007-02-12 00:53:28 -08001057 if (!(newsvsk = svc_setup_socket(serv, newsock, &err,
1058 (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 goto failed;
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001060 memcpy(&newsvsk->sk_remote, sin, slen);
Chuck Lever067d7812007-02-12 00:53:30 -08001061 newsvsk->sk_remotelen = slen;
1062
NeilBrowne79eff12007-02-12 00:53:30 -08001063 svc_sock_received(newsvsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 /* make sure that we don't have too many active connections.
1066 * If we have, something must be dropped.
1067 *
1068 * There's no point in trying to do random drop here for
1069 * DoS prevention. The NFS clients does 1 reconnect in 15
1070 * seconds. An attacker can easily beat that.
1071 *
1072 * The only somewhat efficient mechanism would be if drop
1073 * old connections from the same IP first. But right now
1074 * we don't even record the client IP in svc_sock.
1075 */
1076 if (serv->sv_tmpcnt > (serv->sv_nrthreads+3)*20) {
1077 struct svc_sock *svsk = NULL;
1078 spin_lock_bh(&serv->sv_lock);
1079 if (!list_empty(&serv->sv_tempsocks)) {
1080 if (net_ratelimit()) {
1081 /* Try to help the admin */
1082 printk(KERN_NOTICE "%s: too many open TCP "
1083 "sockets, consider increasing the "
1084 "number of nfsd threads\n",
1085 serv->sv_name);
Chuck Leverad06e4b2007-02-12 00:53:32 -08001086 printk(KERN_NOTICE
1087 "%s: last TCP connect from %s\n",
1088 serv->sv_name, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090 /*
1091 * Always select the oldest socket. It's not fair,
1092 * but so is life
1093 */
1094 svsk = list_entry(serv->sv_tempsocks.prev,
1095 struct svc_sock,
1096 sk_list);
1097 set_bit(SK_CLOSE, &svsk->sk_flags);
Greg Banksc45c3572006-10-02 02:17:54 -07001098 atomic_inc(&svsk->sk_inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100 spin_unlock_bh(&serv->sv_lock);
1101
1102 if (svsk) {
1103 svc_sock_enqueue(svsk);
1104 svc_sock_put(svsk);
1105 }
1106
1107 }
1108
1109 if (serv->sv_stats)
1110 serv->sv_stats->nettcpconn++;
1111
1112 return;
1113
1114failed:
1115 sock_release(newsock);
1116 return;
1117}
1118
1119/*
1120 * Receive data from a TCP socket.
1121 */
1122static int
1123svc_tcp_recvfrom(struct svc_rqst *rqstp)
1124{
1125 struct svc_sock *svsk = rqstp->rq_sock;
1126 struct svc_serv *serv = svsk->sk_server;
1127 int len;
NeilBrown3cc03b12006-10-04 02:15:47 -07001128 struct kvec *vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 int pnum, vlen;
1130
1131 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
1132 svsk, test_bit(SK_DATA, &svsk->sk_flags),
1133 test_bit(SK_CONN, &svsk->sk_flags),
1134 test_bit(SK_CLOSE, &svsk->sk_flags));
1135
1136 if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
1137 svc_sock_received(svsk);
1138 return svc_deferred_recv(rqstp);
1139 }
1140
1141 if (test_bit(SK_CLOSE, &svsk->sk_flags)) {
1142 svc_delete_socket(svsk);
1143 return 0;
1144 }
1145
NeilBrown1a047062006-10-19 23:29:13 -07001146 if (svsk->sk_sk->sk_state == TCP_LISTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 svc_tcp_accept(svsk);
1148 svc_sock_received(svsk);
1149 return 0;
1150 }
1151
1152 if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
1153 /* sndbuf needs to have room for one request
1154 * per thread, otherwise we can stall even when the
1155 * network isn't a bottleneck.
Greg Banks3262c812006-10-02 02:17:58 -07001156 *
1157 * We count all threads rather than threads in a
1158 * particular pool, which provides an upper bound
1159 * on the number of threads which will access the socket.
1160 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 * rcvbuf just needs to be able to hold a few requests.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001162 * Normally they will be removed from the queue
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 * as soon a a complete request arrives.
1164 */
1165 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001166 (serv->sv_nrthreads+3) * serv->sv_max_mesg,
1167 3 * serv->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 clear_bit(SK_DATA, &svsk->sk_flags);
1170
1171 /* Receive data. If we haven't got the record length yet, get
1172 * the next four bytes. Otherwise try to gobble up as much as
1173 * possible up to the complete record length.
1174 */
1175 if (svsk->sk_tcplen < 4) {
1176 unsigned long want = 4 - svsk->sk_tcplen;
1177 struct kvec iov;
1178
1179 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
1180 iov.iov_len = want;
1181 if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
1182 goto error;
1183 svsk->sk_tcplen += len;
1184
1185 if (len < want) {
1186 dprintk("svc: short recvfrom while reading record length (%d of %lu)\n",
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001187 len, want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 svc_sock_received(svsk);
1189 return -EAGAIN; /* record header not complete */
1190 }
1191
1192 svsk->sk_reclen = ntohl(svsk->sk_reclen);
1193 if (!(svsk->sk_reclen & 0x80000000)) {
1194 /* FIXME: technically, a record can be fragmented,
1195 * and non-terminal fragments will not have the top
1196 * bit set in the fragment length header.
1197 * But apparently no known nfs clients send fragmented
1198 * records. */
NeilBrown34e9a632007-01-29 13:19:52 -08001199 if (net_ratelimit())
1200 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx"
1201 " (non-terminal)\n",
1202 (unsigned long) svsk->sk_reclen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 goto err_delete;
1204 }
1205 svsk->sk_reclen &= 0x7fffffff;
1206 dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001207 if (svsk->sk_reclen > serv->sv_max_mesg) {
NeilBrown34e9a632007-01-29 13:19:52 -08001208 if (net_ratelimit())
1209 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx"
1210 " (large)\n",
1211 (unsigned long) svsk->sk_reclen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 goto err_delete;
1213 }
1214 }
1215
1216 /* Check whether enough data is available */
1217 len = svc_recv_available(svsk);
1218 if (len < 0)
1219 goto error;
1220
1221 if (len < svsk->sk_reclen) {
1222 dprintk("svc: incomplete TCP record (%d of %d)\n",
1223 len, svsk->sk_reclen);
1224 svc_sock_received(svsk);
1225 return -EAGAIN; /* record not complete */
1226 }
1227 len = svsk->sk_reclen;
1228 set_bit(SK_DATA, &svsk->sk_flags);
1229
NeilBrown3cc03b12006-10-04 02:15:47 -07001230 vec = rqstp->rq_vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 vec[0] = rqstp->rq_arg.head[0];
1232 vlen = PAGE_SIZE;
1233 pnum = 1;
1234 while (vlen < len) {
NeilBrown44524352006-10-04 02:15:46 -07001235 vec[pnum].iov_base = page_address(rqstp->rq_pages[pnum]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 vec[pnum].iov_len = PAGE_SIZE;
1237 pnum++;
1238 vlen += PAGE_SIZE;
1239 }
NeilBrown44524352006-10-04 02:15:46 -07001240 rqstp->rq_respages = &rqstp->rq_pages[pnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 /* Now receive data */
1243 len = svc_recvfrom(rqstp, vec, pnum, len);
1244 if (len < 0)
1245 goto error;
1246
1247 dprintk("svc: TCP complete record (%d bytes)\n", len);
1248 rqstp->rq_arg.len = len;
1249 rqstp->rq_arg.page_base = 0;
1250 if (len <= rqstp->rq_arg.head[0].iov_len) {
1251 rqstp->rq_arg.head[0].iov_len = len;
1252 rqstp->rq_arg.page_len = 0;
1253 } else {
1254 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
1255 }
1256
1257 rqstp->rq_skbuff = NULL;
1258 rqstp->rq_prot = IPPROTO_TCP;
1259
1260 /* Reset TCP read info */
1261 svsk->sk_reclen = 0;
1262 svsk->sk_tcplen = 0;
1263
1264 svc_sock_received(svsk);
1265 if (serv->sv_stats)
1266 serv->sv_stats->nettcpcnt++;
1267
1268 return len;
1269
1270 err_delete:
1271 svc_delete_socket(svsk);
1272 return -EAGAIN;
1273
1274 error:
1275 if (len == -EAGAIN) {
1276 dprintk("RPC: TCP recvfrom got EAGAIN\n");
1277 svc_sock_received(svsk);
1278 } else {
1279 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
1280 svsk->sk_server->sv_name, -len);
Olaf Kirch93fbf1a2006-01-06 00:19:56 -08001281 goto err_delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283
1284 return len;
1285}
1286
1287/*
1288 * Send out data on TCP socket.
1289 */
1290static int
1291svc_tcp_sendto(struct svc_rqst *rqstp)
1292{
1293 struct xdr_buf *xbufp = &rqstp->rq_res;
1294 int sent;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001295 __be32 reclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 /* Set up the first element of the reply kvec.
1298 * Any other kvecs that may be in use have been taken
1299 * care of by the server implementation itself.
1300 */
1301 reclen = htonl(0x80000000|((xbufp->len ) - 4));
1302 memcpy(xbufp->head[0].iov_base, &reclen, 4);
1303
1304 if (test_bit(SK_DEAD, &rqstp->rq_sock->sk_flags))
1305 return -ENOTCONN;
1306
1307 sent = svc_sendto(rqstp, &rqstp->rq_res);
1308 if (sent != xbufp->len) {
1309 printk(KERN_NOTICE "rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n",
1310 rqstp->rq_sock->sk_server->sv_name,
1311 (sent<0)?"got error":"sent only",
1312 sent, xbufp->len);
NeilBrownaaf68cf2007-02-08 14:20:30 -08001313 set_bit(SK_CLOSE, &rqstp->rq_sock->sk_flags);
1314 svc_sock_enqueue(rqstp->rq_sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 sent = -EAGAIN;
1316 }
1317 return sent;
1318}
1319
1320static void
1321svc_tcp_init(struct svc_sock *svsk)
1322{
1323 struct sock *sk = svsk->sk_sk;
1324 struct tcp_sock *tp = tcp_sk(sk);
1325
1326 svsk->sk_recvfrom = svc_tcp_recvfrom;
1327 svsk->sk_sendto = svc_tcp_sendto;
1328
1329 if (sk->sk_state == TCP_LISTEN) {
1330 dprintk("setting up TCP socket for listening\n");
1331 sk->sk_data_ready = svc_tcp_listen_data_ready;
1332 set_bit(SK_CONN, &svsk->sk_flags);
1333 } else {
1334 dprintk("setting up TCP socket for reading\n");
1335 sk->sk_state_change = svc_tcp_state_change;
1336 sk->sk_data_ready = svc_tcp_data_ready;
1337 sk->sk_write_space = svc_write_space;
1338
1339 svsk->sk_reclen = 0;
1340 svsk->sk_tcplen = 0;
1341
1342 tp->nonagle = 1; /* disable Nagle's algorithm */
1343
1344 /* initialise setting must have enough space to
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001345 * receive and respond to one request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 * svc_tcp_recvfrom will re-adjust if necessary
1347 */
1348 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001349 3 * svsk->sk_server->sv_max_mesg,
1350 3 * svsk->sk_server->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1353 set_bit(SK_DATA, &svsk->sk_flags);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001354 if (sk->sk_state != TCP_ESTABLISHED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 set_bit(SK_CLOSE, &svsk->sk_flags);
1356 }
1357}
1358
1359void
1360svc_sock_update_bufs(struct svc_serv *serv)
1361{
1362 /*
1363 * The number of server threads has changed. Update
1364 * rcvbuf and sndbuf accordingly on all sockets
1365 */
1366 struct list_head *le;
1367
1368 spin_lock_bh(&serv->sv_lock);
1369 list_for_each(le, &serv->sv_permsocks) {
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001370 struct svc_sock *svsk =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 list_entry(le, struct svc_sock, sk_list);
1372 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1373 }
1374 list_for_each(le, &serv->sv_tempsocks) {
1375 struct svc_sock *svsk =
1376 list_entry(le, struct svc_sock, sk_list);
1377 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1378 }
1379 spin_unlock_bh(&serv->sv_lock);
1380}
1381
1382/*
Greg Banks3262c812006-10-02 02:17:58 -07001383 * Receive the next request on any socket. This code is carefully
1384 * organised not to touch any cachelines in the shared svc_serv
1385 * structure, only cachelines in the local svc_pool.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 */
1387int
NeilBrown6fb2b472006-10-02 02:17:50 -07001388svc_recv(struct svc_rqst *rqstp, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Chuck Lever27459f02007-02-12 00:53:34 -08001390 struct svc_sock *svsk = NULL;
NeilBrown6fb2b472006-10-02 02:17:50 -07001391 struct svc_serv *serv = rqstp->rq_server;
Greg Banks3262c812006-10-02 02:17:58 -07001392 struct svc_pool *pool = rqstp->rq_pool;
NeilBrown44524352006-10-04 02:15:46 -07001393 int len, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 int pages;
1395 struct xdr_buf *arg;
1396 DECLARE_WAITQUEUE(wait, current);
1397
1398 dprintk("svc: server %p waiting for data (to = %ld)\n",
1399 rqstp, timeout);
1400
1401 if (rqstp->rq_sock)
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001402 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 "svc_recv: service %p, socket not NULL!\n",
1404 rqstp);
1405 if (waitqueue_active(&rqstp->rq_wait))
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001406 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 "svc_recv: service %p, wait queue active!\n",
1408 rqstp);
1409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 /* now allocate needed pages. If we get a failure, sleep briefly */
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001412 pages = (serv->sv_max_mesg + PAGE_SIZE) / PAGE_SIZE;
NeilBrown44524352006-10-04 02:15:46 -07001413 for (i=0; i < pages ; i++)
1414 while (rqstp->rq_pages[i] == NULL) {
1415 struct page *p = alloc_page(GFP_KERNEL);
1416 if (!p)
1417 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
1418 rqstp->rq_pages[i] = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
NeilBrown250f3912007-01-26 00:56:59 -08001420 rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */
1421 BUG_ON(pages >= RPCSVC_MAXPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 /* Make arg->head point to first page and arg->pages point to rest */
1424 arg = &rqstp->rq_arg;
NeilBrown44524352006-10-04 02:15:46 -07001425 arg->head[0].iov_base = page_address(rqstp->rq_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 arg->head[0].iov_len = PAGE_SIZE;
NeilBrown44524352006-10-04 02:15:46 -07001427 arg->pages = rqstp->rq_pages + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 arg->page_base = 0;
1429 /* save at least one page for response */
1430 arg->page_len = (pages-2)*PAGE_SIZE;
1431 arg->len = (pages-1)*PAGE_SIZE;
1432 arg->tail[0].iov_len = 0;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001433
1434 try_to_freeze();
NeilBrown1887b932005-11-15 00:09:10 -08001435 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (signalled())
1437 return -EINTR;
1438
Greg Banks3262c812006-10-02 02:17:58 -07001439 spin_lock_bh(&pool->sp_lock);
1440 if ((svsk = svc_sock_dequeue(pool)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 rqstp->rq_sock = svsk;
Greg Banksc45c3572006-10-02 02:17:54 -07001442 atomic_inc(&svsk->sk_inuse);
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001443 rqstp->rq_reserved = serv->sv_max_mesg;
Greg Banks5685f0f2006-10-02 02:17:56 -07001444 atomic_add(rqstp->rq_reserved, &svsk->sk_reserved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 } else {
1446 /* No data pending. Go to sleep */
Greg Banks3262c812006-10-02 02:17:58 -07001447 svc_thread_enqueue(pool, rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 /*
1450 * We have to be able to interrupt this wait
1451 * to bring down the daemons ...
1452 */
1453 set_current_state(TASK_INTERRUPTIBLE);
1454 add_wait_queue(&rqstp->rq_wait, &wait);
Greg Banks3262c812006-10-02 02:17:58 -07001455 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 schedule_timeout(timeout);
1458
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001459 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Greg Banks3262c812006-10-02 02:17:58 -07001461 spin_lock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 remove_wait_queue(&rqstp->rq_wait, &wait);
1463
1464 if (!(svsk = rqstp->rq_sock)) {
Greg Banks3262c812006-10-02 02:17:58 -07001465 svc_thread_dequeue(pool, rqstp);
1466 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 dprintk("svc: server %p, no data yet\n", rqstp);
1468 return signalled()? -EINTR : -EAGAIN;
1469 }
1470 }
Greg Banks3262c812006-10-02 02:17:58 -07001471 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Greg Banks3262c812006-10-02 02:17:58 -07001473 dprintk("svc: server %p, pool %u, socket %p, inuse=%d\n",
1474 rqstp, pool->sp_id, svsk, atomic_read(&svsk->sk_inuse));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 len = svsk->sk_recvfrom(rqstp);
1476 dprintk("svc: got len=%d\n", len);
1477
1478 /* No data, incomplete (TCP) read, or accept() */
1479 if (len == 0 || len == -EAGAIN) {
1480 rqstp->rq_res.len = 0;
1481 svc_sock_release(rqstp);
1482 return -EAGAIN;
1483 }
1484 svsk->sk_lastrecv = get_seconds();
Greg Banks36bdfc82006-10-02 02:17:54 -07001485 clear_bit(SK_OLD, &svsk->sk_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Chuck Leverbcdb81a2007-02-12 00:53:37 -08001487 rqstp->rq_secure = svc_port_is_privileged(svc_addr(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 rqstp->rq_chandle.defer = svc_defer;
1489
1490 if (serv->sv_stats)
1491 serv->sv_stats->netcnt++;
1492 return len;
1493}
1494
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001495/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 * Drop request
1497 */
1498void
1499svc_drop(struct svc_rqst *rqstp)
1500{
1501 dprintk("svc: socket %p dropped request\n", rqstp->rq_sock);
1502 svc_sock_release(rqstp);
1503}
1504
1505/*
1506 * Return reply to client.
1507 */
1508int
1509svc_send(struct svc_rqst *rqstp)
1510{
1511 struct svc_sock *svsk;
1512 int len;
1513 struct xdr_buf *xb;
1514
1515 if ((svsk = rqstp->rq_sock) == NULL) {
1516 printk(KERN_WARNING "NULL socket pointer in %s:%d\n",
1517 __FILE__, __LINE__);
1518 return -EFAULT;
1519 }
1520
1521 /* release the receive skb before sending the reply */
1522 svc_release_skb(rqstp);
1523
1524 /* calculate over-all length */
1525 xb = & rqstp->rq_res;
1526 xb->len = xb->head[0].iov_len +
1527 xb->page_len +
1528 xb->tail[0].iov_len;
1529
Ingo Molnar57b47a52006-03-20 22:35:41 -08001530 /* Grab svsk->sk_mutex to serialize outgoing data. */
1531 mutex_lock(&svsk->sk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (test_bit(SK_DEAD, &svsk->sk_flags))
1533 len = -ENOTCONN;
1534 else
1535 len = svsk->sk_sendto(rqstp);
Ingo Molnar57b47a52006-03-20 22:35:41 -08001536 mutex_unlock(&svsk->sk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 svc_sock_release(rqstp);
1538
1539 if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
1540 return 0;
1541 return len;
1542}
1543
1544/*
Greg Banks36bdfc82006-10-02 02:17:54 -07001545 * Timer function to close old temporary sockets, using
1546 * a mark-and-sweep algorithm.
1547 */
1548static void
1549svc_age_temp_sockets(unsigned long closure)
1550{
1551 struct svc_serv *serv = (struct svc_serv *)closure;
1552 struct svc_sock *svsk;
1553 struct list_head *le, *next;
1554 LIST_HEAD(to_be_aged);
1555
1556 dprintk("svc_age_temp_sockets\n");
1557
1558 if (!spin_trylock_bh(&serv->sv_lock)) {
1559 /* busy, try again 1 sec later */
1560 dprintk("svc_age_temp_sockets: busy\n");
1561 mod_timer(&serv->sv_temptimer, jiffies + HZ);
1562 return;
1563 }
1564
1565 list_for_each_safe(le, next, &serv->sv_tempsocks) {
1566 svsk = list_entry(le, struct svc_sock, sk_list);
1567
1568 if (!test_and_set_bit(SK_OLD, &svsk->sk_flags))
1569 continue;
Greg Banksc45c3572006-10-02 02:17:54 -07001570 if (atomic_read(&svsk->sk_inuse) || test_bit(SK_BUSY, &svsk->sk_flags))
Greg Banks36bdfc82006-10-02 02:17:54 -07001571 continue;
Greg Banksc45c3572006-10-02 02:17:54 -07001572 atomic_inc(&svsk->sk_inuse);
Greg Banks36bdfc82006-10-02 02:17:54 -07001573 list_move(le, &to_be_aged);
1574 set_bit(SK_CLOSE, &svsk->sk_flags);
1575 set_bit(SK_DETACHED, &svsk->sk_flags);
1576 }
1577 spin_unlock_bh(&serv->sv_lock);
1578
1579 while (!list_empty(&to_be_aged)) {
1580 le = to_be_aged.next;
1581 /* fiddling the sk_list node is safe 'cos we're SK_DETACHED */
1582 list_del_init(le);
1583 svsk = list_entry(le, struct svc_sock, sk_list);
1584
1585 dprintk("queuing svsk %p for closing, %lu seconds old\n",
1586 svsk, get_seconds() - svsk->sk_lastrecv);
1587
1588 /* a thread will dequeue and close it soon */
1589 svc_sock_enqueue(svsk);
1590 svc_sock_put(svsk);
1591 }
1592
1593 mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
1594}
1595
1596/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 * Initialize socket for RPC use and create svc_sock struct
1598 * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
1599 */
Chuck Lever6b174332007-02-12 00:53:28 -08001600static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1601 struct socket *sock,
1602 int *errp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
1604 struct svc_sock *svsk;
1605 struct sock *inet;
Chuck Lever6b174332007-02-12 00:53:28 -08001606 int pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
1607 int is_temporary = flags & SVC_SOCK_TEMPORARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 dprintk("svc: svc_setup_socket %p\n", sock);
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001610 if (!(svsk = kzalloc(sizeof(*svsk), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 *errp = -ENOMEM;
1612 return NULL;
1613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615 inet = sock->sk;
1616
1617 /* Register socket with portmapper */
1618 if (*errp >= 0 && pmap_register)
1619 *errp = svc_register(serv, inet->sk_protocol,
1620 ntohs(inet_sk(inet)->sport));
1621
1622 if (*errp < 0) {
1623 kfree(svsk);
1624 return NULL;
1625 }
1626
1627 set_bit(SK_BUSY, &svsk->sk_flags);
1628 inet->sk_user_data = svsk;
1629 svsk->sk_sock = sock;
1630 svsk->sk_sk = inet;
1631 svsk->sk_ostate = inet->sk_state_change;
1632 svsk->sk_odata = inet->sk_data_ready;
1633 svsk->sk_owspace = inet->sk_write_space;
1634 svsk->sk_server = serv;
NeilBrownaaf68cf2007-02-08 14:20:30 -08001635 atomic_set(&svsk->sk_inuse, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 svsk->sk_lastrecv = get_seconds();
NeilBrown7ac1bea2007-05-09 02:34:48 -07001637 spin_lock_init(&svsk->sk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 INIT_LIST_HEAD(&svsk->sk_deferred);
1639 INIT_LIST_HEAD(&svsk->sk_ready);
Ingo Molnar57b47a52006-03-20 22:35:41 -08001640 mutex_init(&svsk->sk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642 /* Initialize the socket */
1643 if (sock->type == SOCK_DGRAM)
1644 svc_udp_init(svsk);
1645 else
1646 svc_tcp_init(svsk);
1647
1648 spin_lock_bh(&serv->sv_lock);
Chuck Lever6b174332007-02-12 00:53:28 -08001649 if (is_temporary) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 set_bit(SK_TEMP, &svsk->sk_flags);
1651 list_add(&svsk->sk_list, &serv->sv_tempsocks);
1652 serv->sv_tmpcnt++;
Greg Banks36bdfc82006-10-02 02:17:54 -07001653 if (serv->sv_temptimer.function == NULL) {
1654 /* setup timer to age temp sockets */
1655 setup_timer(&serv->sv_temptimer, svc_age_temp_sockets,
1656 (unsigned long)serv);
1657 mod_timer(&serv->sv_temptimer,
1658 jiffies + svc_conn_age_period * HZ);
1659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 } else {
1661 clear_bit(SK_TEMP, &svsk->sk_flags);
1662 list_add(&svsk->sk_list, &serv->sv_permsocks);
1663 }
1664 spin_unlock_bh(&serv->sv_lock);
1665
1666 dprintk("svc: svc_setup_socket created %p (inet %p)\n",
1667 svsk, svsk->sk_sk);
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 return svsk;
1670}
1671
NeilBrownb41b66d2006-10-02 02:17:48 -07001672int svc_addsock(struct svc_serv *serv,
1673 int fd,
1674 char *name_return,
1675 int *proto)
1676{
1677 int err = 0;
1678 struct socket *so = sockfd_lookup(fd, &err);
1679 struct svc_sock *svsk = NULL;
1680
1681 if (!so)
1682 return err;
1683 if (so->sk->sk_family != AF_INET)
1684 err = -EAFNOSUPPORT;
1685 else if (so->sk->sk_protocol != IPPROTO_TCP &&
1686 so->sk->sk_protocol != IPPROTO_UDP)
1687 err = -EPROTONOSUPPORT;
1688 else if (so->state > SS_UNCONNECTED)
1689 err = -EISCONN;
1690 else {
Chuck Lever6b174332007-02-12 00:53:28 -08001691 svsk = svc_setup_socket(serv, so, &err, SVC_SOCK_DEFAULTS);
NeilBrowne79eff12007-02-12 00:53:30 -08001692 if (svsk) {
1693 svc_sock_received(svsk);
NeilBrownb41b66d2006-10-02 02:17:48 -07001694 err = 0;
NeilBrowne79eff12007-02-12 00:53:30 -08001695 }
NeilBrownb41b66d2006-10-02 02:17:48 -07001696 }
1697 if (err) {
1698 sockfd_put(so);
1699 return err;
1700 }
1701 if (proto) *proto = so->sk->sk_protocol;
1702 return one_sock_name(name_return, svsk);
1703}
1704EXPORT_SYMBOL_GPL(svc_addsock);
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706/*
1707 * Create socket for RPC service.
1708 */
Chuck Lever6b174332007-02-12 00:53:28 -08001709static int svc_create_socket(struct svc_serv *serv, int protocol,
Chuck Lever77f1f672007-02-12 00:53:39 -08001710 struct sockaddr *sin, int len, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
1712 struct svc_sock *svsk;
1713 struct socket *sock;
1714 int error;
1715 int type;
Chuck Leverad06e4b2007-02-12 00:53:32 -08001716 char buf[RPC_MAX_ADDRBUFLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Chuck Leverad06e4b2007-02-12 00:53:32 -08001718 dprintk("svc: svc_create_socket(%s, %d, %s)\n",
1719 serv->sv_program->pg_name, protocol,
Chuck Lever77f1f672007-02-12 00:53:39 -08001720 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1723 printk(KERN_WARNING "svc: only UDP and TCP "
1724 "sockets supported\n");
1725 return -EINVAL;
1726 }
1727 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1728
Chuck Lever77f1f672007-02-12 00:53:39 -08001729 error = sock_create_kern(sin->sa_family, type, protocol, &sock);
1730 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 return error;
1732
Peter Zijlstraed075362006-12-06 20:35:24 -08001733 svc_reclassify_socket(sock);
1734
Eric Sesterhenn18114742006-09-28 14:37:07 -07001735 if (type == SOCK_STREAM)
Chuck Lever77f1f672007-02-12 00:53:39 -08001736 sock->sk->sk_reuse = 1; /* allow address reuse */
1737 error = kernel_bind(sock, sin, len);
Eric Sesterhenn18114742006-09-28 14:37:07 -07001738 if (error < 0)
1739 goto bummer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741 if (protocol == IPPROTO_TCP) {
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001742 if ((error = kernel_listen(sock, 64)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 goto bummer;
1744 }
1745
NeilBrowne79eff12007-02-12 00:53:30 -08001746 if ((svsk = svc_setup_socket(serv, sock, &error, flags)) != NULL) {
1747 svc_sock_received(svsk);
Chuck Lever6b174332007-02-12 00:53:28 -08001748 return ntohs(inet_sk(svsk->sk_sk)->sport);
NeilBrowne79eff12007-02-12 00:53:30 -08001749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751bummer:
1752 dprintk("svc: svc_create_socket error = %d\n", -error);
1753 sock_release(sock);
1754 return error;
1755}
1756
1757/*
1758 * Remove a dead socket
1759 */
NeilBrownaaf68cf2007-02-08 14:20:30 -08001760static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761svc_delete_socket(struct svc_sock *svsk)
1762{
1763 struct svc_serv *serv;
1764 struct sock *sk;
1765
1766 dprintk("svc: svc_delete_socket(%p)\n", svsk);
1767
1768 serv = svsk->sk_server;
1769 sk = svsk->sk_sk;
1770
1771 sk->sk_state_change = svsk->sk_ostate;
1772 sk->sk_data_ready = svsk->sk_odata;
1773 sk->sk_write_space = svsk->sk_owspace;
1774
1775 spin_lock_bh(&serv->sv_lock);
1776
Greg Banks36bdfc82006-10-02 02:17:54 -07001777 if (!test_and_set_bit(SK_DETACHED, &svsk->sk_flags))
1778 list_del_init(&svsk->sk_list);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001779 /*
Greg Banks3262c812006-10-02 02:17:58 -07001780 * We used to delete the svc_sock from whichever list
1781 * it's sk_ready node was on, but we don't actually
1782 * need to. This is because the only time we're called
1783 * while still attached to a queue, the queue itself
1784 * is about to be destroyed (in svc_destroy).
1785 */
NeilBrownaaf68cf2007-02-08 14:20:30 -08001786 if (!test_and_set_bit(SK_DEAD, &svsk->sk_flags)) {
1787 BUG_ON(atomic_read(&svsk->sk_inuse)<2);
1788 atomic_dec(&svsk->sk_inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 if (test_bit(SK_TEMP, &svsk->sk_flags))
1790 serv->sv_tmpcnt--;
NeilBrownaaf68cf2007-02-08 14:20:30 -08001791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Neil Brownd6740df2006-10-29 22:46:45 -08001793 spin_unlock_bh(&serv->sv_lock);
NeilBrownaaf68cf2007-02-08 14:20:30 -08001794}
1795
NeilBrowncda1fd42007-03-06 01:42:22 -08001796static void svc_close_socket(struct svc_sock *svsk)
NeilBrownaaf68cf2007-02-08 14:20:30 -08001797{
1798 set_bit(SK_CLOSE, &svsk->sk_flags);
1799 if (test_and_set_bit(SK_BUSY, &svsk->sk_flags))
1800 /* someone else will have to effect the close */
1801 return;
1802
1803 atomic_inc(&svsk->sk_inuse);
1804 svc_delete_socket(svsk);
1805 clear_bit(SK_BUSY, &svsk->sk_flags);
Neil Brownd6740df2006-10-29 22:46:45 -08001806 svc_sock_put(svsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807}
1808
NeilBrowncda1fd42007-03-06 01:42:22 -08001809void svc_force_close_socket(struct svc_sock *svsk)
1810{
1811 set_bit(SK_CLOSE, &svsk->sk_flags);
1812 if (test_bit(SK_BUSY, &svsk->sk_flags)) {
1813 /* Waiting to be processed, but no threads left,
1814 * So just remove it from the waiting list
1815 */
1816 list_del_init(&svsk->sk_ready);
1817 clear_bit(SK_BUSY, &svsk->sk_flags);
1818 }
1819 svc_close_socket(svsk);
1820}
1821
Chuck Lever6b174332007-02-12 00:53:28 -08001822/**
1823 * svc_makesock - Make a socket for nfsd and lockd
1824 * @serv: RPC server structure
1825 * @protocol: transport protocol to use
1826 * @port: port to use
Chuck Lever482fb942007-02-12 00:53:29 -08001827 * @flags: requested socket characteristics
Chuck Lever6b174332007-02-12 00:53:28 -08001828 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 */
Chuck Lever482fb942007-02-12 00:53:29 -08001830int svc_makesock(struct svc_serv *serv, int protocol, unsigned short port,
1831 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
Chuck Lever6b174332007-02-12 00:53:28 -08001833 struct sockaddr_in sin = {
1834 .sin_family = AF_INET,
1835 .sin_addr.s_addr = INADDR_ANY,
1836 .sin_port = htons(port),
1837 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839 dprintk("svc: creating socket proto = %d\n", protocol);
Chuck Lever77f1f672007-02-12 00:53:39 -08001840 return svc_create_socket(serv, protocol, (struct sockaddr *) &sin,
1841 sizeof(sin), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
1843
1844/*
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001845 * Handle defer and revisit of requests
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 */
1847
1848static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
1849{
1850 struct svc_deferred_req *dr = container_of(dreq, struct svc_deferred_req, handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 struct svc_sock *svsk;
1852
1853 if (too_many) {
1854 svc_sock_put(dr->svsk);
1855 kfree(dr);
1856 return;
1857 }
1858 dprintk("revisit queued\n");
1859 svsk = dr->svsk;
1860 dr->svsk = NULL;
NeilBrown7ac1bea2007-05-09 02:34:48 -07001861 spin_lock(&svsk->sk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 list_add(&dr->handle.recent, &svsk->sk_deferred);
NeilBrown7ac1bea2007-05-09 02:34:48 -07001863 spin_unlock(&svsk->sk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 set_bit(SK_DEFERRED, &svsk->sk_flags);
1865 svc_sock_enqueue(svsk);
1866 svc_sock_put(svsk);
1867}
1868
1869static struct cache_deferred_req *
1870svc_defer(struct cache_req *req)
1871{
1872 struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
1873 int size = sizeof(struct svc_deferred_req) + (rqstp->rq_arg.len);
1874 struct svc_deferred_req *dr;
1875
1876 if (rqstp->rq_arg.page_len)
1877 return NULL; /* if more than a page, give up FIXME */
1878 if (rqstp->rq_deferred) {
1879 dr = rqstp->rq_deferred;
1880 rqstp->rq_deferred = NULL;
1881 } else {
1882 int skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
1883 /* FIXME maybe discard if size too large */
1884 dr = kmalloc(size, GFP_KERNEL);
1885 if (dr == NULL)
1886 return NULL;
1887
1888 dr->handle.owner = rqstp->rq_server;
1889 dr->prot = rqstp->rq_prot;
Chuck Lever24422222007-02-12 00:53:33 -08001890 memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen);
1891 dr->addrlen = rqstp->rq_addrlen;
J. Bruce Fields1918e342006-01-18 17:43:16 -08001892 dr->daddr = rqstp->rq_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 dr->argslen = rqstp->rq_arg.len >> 2;
1894 memcpy(dr->args, rqstp->rq_arg.head[0].iov_base-skip, dr->argslen<<2);
1895 }
Greg Banksc45c3572006-10-02 02:17:54 -07001896 atomic_inc(&rqstp->rq_sock->sk_inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 dr->svsk = rqstp->rq_sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899 dr->handle.revisit = svc_revisit;
1900 return &dr->handle;
1901}
1902
1903/*
1904 * recv data from a deferred request into an active one
1905 */
1906static int svc_deferred_recv(struct svc_rqst *rqstp)
1907{
1908 struct svc_deferred_req *dr = rqstp->rq_deferred;
1909
1910 rqstp->rq_arg.head[0].iov_base = dr->args;
1911 rqstp->rq_arg.head[0].iov_len = dr->argslen<<2;
1912 rqstp->rq_arg.page_len = 0;
1913 rqstp->rq_arg.len = dr->argslen<<2;
1914 rqstp->rq_prot = dr->prot;
Chuck Lever24422222007-02-12 00:53:33 -08001915 memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen);
1916 rqstp->rq_addrlen = dr->addrlen;
J. Bruce Fields1918e342006-01-18 17:43:16 -08001917 rqstp->rq_daddr = dr->daddr;
NeilBrown44524352006-10-04 02:15:46 -07001918 rqstp->rq_respages = rqstp->rq_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 return dr->argslen<<2;
1920}
1921
1922
1923static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk)
1924{
1925 struct svc_deferred_req *dr = NULL;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 if (!test_bit(SK_DEFERRED, &svsk->sk_flags))
1928 return NULL;
NeilBrown7ac1bea2007-05-09 02:34:48 -07001929 spin_lock(&svsk->sk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 clear_bit(SK_DEFERRED, &svsk->sk_flags);
1931 if (!list_empty(&svsk->sk_deferred)) {
1932 dr = list_entry(svsk->sk_deferred.next,
1933 struct svc_deferred_req,
1934 handle.recent);
1935 list_del_init(&dr->handle.recent);
1936 set_bit(SK_DEFERRED, &svsk->sk_flags);
1937 }
NeilBrown7ac1bea2007-05-09 02:34:48 -07001938 spin_unlock(&svsk->sk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 return dr;
1940}