blob: 5fb537e4d63bc4cbc0a02811fbf2767645b61af9 [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
Tom Tuckerf6150c32007-12-30 21:07:57 -06008 * svc_xprt_enqueue procedure...
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
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
Ilpo Järvinen172589c2007-08-28 15:50:33 -070022#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/sched.h>
24#include <linux/errno.h>
25#include <linux/fcntl.h>
26#include <linux/net.h>
27#include <linux/in.h>
28#include <linux/inet.h>
29#include <linux/udp.h>
Andrew Morton91483c42005-08-09 20:20:07 -070030#include <linux/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/unistd.h>
32#include <linux/slab.h>
33#include <linux/netdevice.h>
34#include <linux/skbuff.h>
NeilBrownb41b66d2006-10-02 02:17:48 -070035#include <linux/file.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080036#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <net/sock.h>
38#include <net/checksum.h>
39#include <net/ip.h>
Chuck Leverb92503b2007-02-12 00:53:36 -080040#include <net/ipv6.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070041#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/uaccess.h>
43#include <asm/ioctls.h>
44
45#include <linux/sunrpc/types.h>
Chuck Leverad06e4b2007-02-12 00:53:32 -080046#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/sunrpc/xdr.h>
48#include <linux/sunrpc/svcsock.h>
49#include <linux/sunrpc/stats.h>
50
51/* SMP locking strategy:
52 *
Greg Banks3262c812006-10-02 02:17:58 -070053 * svc_pool->sp_lock protects most of the fields of that pool.
54 * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt.
55 * when both need to be taken (rare), svc_serv->sv_lock is first.
56 * BKL protects svc_serv->sv_nrthread.
NeilBrown7ac1bea2007-05-09 02:34:48 -070057 * svc_sock->sk_lock protects the svc_sock->sk_deferred list
58 * and the ->sk_info_authunix cache.
Tom Tucker02fc6c32007-12-30 21:07:48 -060059 * svc_sock->sk_xprt.xpt_flags.XPT_BUSY prevents a svc_sock being
60 * enqueued multiply.
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 *
62 * Some flags can be set to certain values at any time
63 * providing that certain rules are followed:
64 *
Tom Tucker02fc6c32007-12-30 21:07:48 -060065 * XPT_CONN, XPT_DATA, can be set or cleared at any time.
Tom Tuckerf6150c32007-12-30 21:07:57 -060066 * after a set, svc_xprt_enqueue must be called.
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 * after a clear, the socket must be read/accepted
68 * if this succeeds, it must be set again.
Tom Tucker02fc6c32007-12-30 21:07:48 -060069 * XPT_CLOSE can set at any time. It is never cleared.
70 * xpt_ref contains a bias of '1' until XPT_DEAD is set.
Tom Tuckere1b31572007-12-30 21:07:46 -060071 * so when xprt_ref hits zero, we know the transport is dead
NeilBrownaaf68cf2007-02-08 14:20:30 -080072 * and no-one is using it.
Tom Tucker02fc6c32007-12-30 21:07:48 -060073 * XPT_DEAD can only be set while XPT_BUSY is held which ensures
NeilBrownaaf68cf2007-02-08 14:20:30 -080074 * no other thread will be using the socket or will try to
Tom Tucker02fc6c32007-12-30 21:07:48 -060075 * set XPT_DEAD.
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 *
77 */
78
Tom Tucker360d87382007-12-30 21:07:17 -060079#define RPCDBG_FACILITY RPCDBG_SVCXPRT
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81
82static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
Chuck Lever6b174332007-02-12 00:53:28 -080083 int *errp, int flags);
Tom Tucker7a182082007-12-30 21:07:53 -060084static void svc_delete_xprt(struct svc_xprt *xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static void svc_udp_data_ready(struct sock *, int);
86static int svc_udp_recvfrom(struct svc_rqst *);
87static int svc_udp_sendto(struct svc_rqst *);
Tom Tucker7a182082007-12-30 21:07:53 -060088static void svc_close_xprt(struct svc_xprt *xprt);
Tom Tucker755ccea2007-12-30 21:07:27 -060089static void svc_sock_detach(struct svc_xprt *);
90static void svc_sock_free(struct svc_xprt *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk);
93static int svc_deferred_recv(struct svc_rqst *rqstp);
94static struct cache_deferred_req *svc_defer(struct cache_req *req);
Tom Tuckerb700cbb2007-12-30 21:07:42 -060095static struct svc_xprt *svc_create_socket(struct svc_serv *, int,
96 struct sockaddr *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Greg Banks36bdfc82006-10-02 02:17:54 -070098/* apparently the "standard" is that clients close
99 * idle connections after 5 minutes, servers after
100 * 6 minutes
101 * http://www.connectathon.org/talks96/nfstcp.pdf
102 */
103static int svc_conn_age_period = 6*60;
104
Peter Zijlstraed075362006-12-06 20:35:24 -0800105#ifdef CONFIG_DEBUG_LOCK_ALLOC
106static struct lock_class_key svc_key[2];
107static struct lock_class_key svc_slock_key[2];
108
109static inline void svc_reclassify_socket(struct socket *sock)
110{
111 struct sock *sk = sock->sk;
John Heffner02b3d342007-09-12 10:42:12 +0200112 BUG_ON(sock_owned_by_user(sk));
Peter Zijlstraed075362006-12-06 20:35:24 -0800113 switch (sk->sk_family) {
114 case AF_INET:
115 sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
116 &svc_slock_key[0], "sk_lock-AF_INET-NFSD", &svc_key[0]);
117 break;
118
119 case AF_INET6:
120 sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
121 &svc_slock_key[1], "sk_lock-AF_INET6-NFSD", &svc_key[1]);
122 break;
123
124 default:
125 BUG();
126 }
127}
128#else
129static inline void svc_reclassify_socket(struct socket *sock)
130{
131}
132#endif
133
Chuck Leverad06e4b2007-02-12 00:53:32 -0800134static char *__svc_print_addr(struct sockaddr *addr, char *buf, size_t len)
135{
136 switch (addr->sa_family) {
137 case AF_INET:
138 snprintf(buf, len, "%u.%u.%u.%u, port=%u",
139 NIPQUAD(((struct sockaddr_in *) addr)->sin_addr),
Al Viro582ee432007-07-26 17:33:39 +0100140 ntohs(((struct sockaddr_in *) addr)->sin_port));
Chuck Leverad06e4b2007-02-12 00:53:32 -0800141 break;
NeilBrown5a05ed72007-03-06 01:42:22 -0800142
Chuck Leverad06e4b2007-02-12 00:53:32 -0800143 case AF_INET6:
144 snprintf(buf, len, "%x:%x:%x:%x:%x:%x:%x:%x, port=%u",
145 NIP6(((struct sockaddr_in6 *) addr)->sin6_addr),
Al Viro582ee432007-07-26 17:33:39 +0100146 ntohs(((struct sockaddr_in6 *) addr)->sin6_port));
Chuck Leverad06e4b2007-02-12 00:53:32 -0800147 break;
NeilBrown5a05ed72007-03-06 01:42:22 -0800148
Chuck Leverad06e4b2007-02-12 00:53:32 -0800149 default:
150 snprintf(buf, len, "unknown address type: %d", addr->sa_family);
151 break;
152 }
153 return buf;
154}
155
156/**
157 * svc_print_addr - Format rq_addr field for printing
158 * @rqstp: svc_rqst struct containing address to print
159 * @buf: target buffer for formatted address
160 * @len: length of target buffer
161 *
162 */
163char *svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len)
164{
Chuck Lever27459f02007-02-12 00:53:34 -0800165 return __svc_print_addr(svc_addr(rqstp), buf, len);
Chuck Leverad06e4b2007-02-12 00:53:32 -0800166}
167EXPORT_SYMBOL_GPL(svc_print_addr);
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169/*
Greg Banks3262c812006-10-02 02:17:58 -0700170 * Queue up an idle server thread. Must have pool->sp_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * Note: this is really a stack rather than a queue, so that we only
Greg Banks3262c812006-10-02 02:17:58 -0700172 * use as many different threads as we need, and the rest don't pollute
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * the cache.
174 */
175static inline void
Greg Banks3262c812006-10-02 02:17:58 -0700176svc_thread_enqueue(struct svc_pool *pool, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Greg Banks3262c812006-10-02 02:17:58 -0700178 list_add(&rqstp->rq_list, &pool->sp_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181/*
Greg Banks3262c812006-10-02 02:17:58 -0700182 * Dequeue an nfsd thread. Must have pool->sp_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 */
184static inline void
Greg Banks3262c812006-10-02 02:17:58 -0700185svc_thread_dequeue(struct svc_pool *pool, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 list_del(&rqstp->rq_list);
188}
189
190/*
191 * Release an skbuff after use
192 */
Tom Tucker5148bf42007-12-30 21:07:25 -0600193static void svc_release_skb(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Tom Tucker5148bf42007-12-30 21:07:25 -0600195 struct sk_buff *skb = rqstp->rq_xprt_ctxt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 struct svc_deferred_req *dr = rqstp->rq_deferred;
197
198 if (skb) {
Tom Tucker5148bf42007-12-30 21:07:25 -0600199 rqstp->rq_xprt_ctxt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
202 skb_free_datagram(rqstp->rq_sock->sk_sk, skb);
203 }
204 if (dr) {
205 rqstp->rq_deferred = NULL;
206 kfree(dr);
207 }
208}
209
210/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * Queue up a socket with data pending. If there are idle nfsd
212 * processes, wake 'em up.
213 *
214 */
Tom Tuckerf6150c32007-12-30 21:07:57 -0600215void svc_xprt_enqueue(struct svc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Tom Tuckerf6150c32007-12-30 21:07:57 -0600217 struct svc_serv *serv = xprt->xpt_server;
Greg Banksbfd24162006-10-02 02:18:01 -0700218 struct svc_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 struct svc_rqst *rqstp;
Greg Banksbfd24162006-10-02 02:18:01 -0700220 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Tom Tuckerf6150c32007-12-30 21:07:57 -0600222 if (!(xprt->xpt_flags &
Tom Tucker02fc6c32007-12-30 21:07:48 -0600223 ((1<<XPT_CONN)|(1<<XPT_DATA)|(1<<XPT_CLOSE)|(1<<XPT_DEFERRED))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return;
Tom Tuckerf6150c32007-12-30 21:07:57 -0600225 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return;
227
Greg Banksbfd24162006-10-02 02:18:01 -0700228 cpu = get_cpu();
Tom Tuckerf6150c32007-12-30 21:07:57 -0600229 pool = svc_pool_for_cpu(xprt->xpt_server, cpu);
Greg Banksbfd24162006-10-02 02:18:01 -0700230 put_cpu();
231
Greg Banks3262c812006-10-02 02:17:58 -0700232 spin_lock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Greg Banks3262c812006-10-02 02:17:58 -0700234 if (!list_empty(&pool->sp_threads) &&
235 !list_empty(&pool->sp_sockets))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 printk(KERN_ERR
Tom Tuckerf6150c32007-12-30 21:07:57 -0600237 "svc_xprt_enqueue: "
238 "threads and transports both waiting??\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Tom Tuckerf6150c32007-12-30 21:07:57 -0600240 if (test_bit(XPT_DEAD, &xprt->xpt_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /* Don't enqueue dead sockets */
Tom Tuckerf6150c32007-12-30 21:07:57 -0600242 dprintk("svc: transport %p is dead, not enqueued\n", xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 goto out_unlock;
244 }
245
Greg Banksc081a0c2006-10-02 02:17:57 -0700246 /* Mark socket as busy. It will remain in this state until the
247 * server has processed all pending data and put the socket back
Tom Tucker02fc6c32007-12-30 21:07:48 -0600248 * on the idle list. We update XPT_BUSY atomically because
Greg Banksc081a0c2006-10-02 02:17:57 -0700249 * it also guards against trying to enqueue the svc_sock twice.
250 */
Tom Tuckerf6150c32007-12-30 21:07:57 -0600251 if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags)) {
Greg Banksc081a0c2006-10-02 02:17:57 -0700252 /* Don't enqueue socket while already enqueued */
Tom Tuckerf6150c32007-12-30 21:07:57 -0600253 dprintk("svc: transport %p busy, not enqueued\n", xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 goto out_unlock;
255 }
Tom Tuckerf6150c32007-12-30 21:07:57 -0600256 BUG_ON(xprt->xpt_pool != NULL);
257 xprt->xpt_pool = pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Tom Tucker323bee32007-12-30 21:07:31 -0600259 /* Handle pending connection */
Tom Tuckerf6150c32007-12-30 21:07:57 -0600260 if (test_bit(XPT_CONN, &xprt->xpt_flags))
Tom Tucker323bee32007-12-30 21:07:31 -0600261 goto process;
262
263 /* Handle close in-progress */
Tom Tuckerf6150c32007-12-30 21:07:57 -0600264 if (test_bit(XPT_CLOSE, &xprt->xpt_flags))
Tom Tucker323bee32007-12-30 21:07:31 -0600265 goto process;
266
267 /* Check if we have space to reply to a request */
Tom Tuckerf6150c32007-12-30 21:07:57 -0600268 if (!xprt->xpt_ops->xpo_has_wspace(xprt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 /* Don't enqueue while not enough space for reply */
Tom Tuckerf6150c32007-12-30 21:07:57 -0600270 dprintk("svc: no write space, transport %p not enqueued\n",
271 xprt);
272 xprt->xpt_pool = NULL;
273 clear_bit(XPT_BUSY, &xprt->xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 goto out_unlock;
275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Tom Tucker323bee32007-12-30 21:07:31 -0600277 process:
Greg Banks3262c812006-10-02 02:17:58 -0700278 if (!list_empty(&pool->sp_threads)) {
279 rqstp = list_entry(pool->sp_threads.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 struct svc_rqst,
281 rq_list);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600282 dprintk("svc: transport %p served by daemon %p\n",
283 xprt, rqstp);
Greg Banks3262c812006-10-02 02:17:58 -0700284 svc_thread_dequeue(pool, rqstp);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600285 if (rqstp->rq_xprt)
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800286 printk(KERN_ERR
Tom Tuckerf6150c32007-12-30 21:07:57 -0600287 "svc_xprt_enqueue: server %p, rq_xprt=%p!\n",
288 rqstp, rqstp->rq_xprt);
289 rqstp->rq_xprt = xprt;
290 svc_xprt_get(xprt);
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700291 rqstp->rq_reserved = serv->sv_max_mesg;
Tom Tuckerf6150c32007-12-30 21:07:57 -0600292 atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved);
293 BUG_ON(xprt->xpt_pool != pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 wake_up(&rqstp->rq_wait);
295 } else {
Tom Tuckerf6150c32007-12-30 21:07:57 -0600296 dprintk("svc: transport %p put into queue\n", xprt);
297 list_add_tail(&xprt->xpt_ready, &pool->sp_sockets);
298 BUG_ON(xprt->xpt_pool != pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
301out_unlock:
Greg Banks3262c812006-10-02 02:17:58 -0700302 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
Tom Tuckerf6150c32007-12-30 21:07:57 -0600304EXPORT_SYMBOL_GPL(svc_xprt_enqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306/*
Greg Banks3262c812006-10-02 02:17:58 -0700307 * Dequeue the first socket. Must be called with the pool->sp_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 */
309static inline struct svc_sock *
Greg Banks3262c812006-10-02 02:17:58 -0700310svc_sock_dequeue(struct svc_pool *pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 struct svc_sock *svsk;
313
Greg Banks3262c812006-10-02 02:17:58 -0700314 if (list_empty(&pool->sp_sockets))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return NULL;
316
Greg Banks3262c812006-10-02 02:17:58 -0700317 svsk = list_entry(pool->sp_sockets.next,
Tom Tucker7a182082007-12-30 21:07:53 -0600318 struct svc_sock, sk_xprt.xpt_ready);
319 list_del_init(&svsk->sk_xprt.xpt_ready);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 dprintk("svc: socket %p dequeued, inuse=%d\n",
Tom Tuckere1b31572007-12-30 21:07:46 -0600322 svsk->sk_sk, atomic_read(&svsk->sk_xprt.xpt_ref.refcount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 return svsk;
325}
326
327/*
Tom Tuckera6046f72007-12-30 21:08:01 -0600328 * svc_xprt_received conditionally queues the transport for processing
329 * by another thread. The caller must hold the XPT_BUSY bit and must
330 * not thereafter touch transport data.
331 *
332 * Note: XPT_DATA only gets cleared when a read-attempt finds no (or
333 * insufficient) data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 */
Tom Tuckera6046f72007-12-30 21:08:01 -0600335void svc_xprt_received(struct svc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Tom Tuckera6046f72007-12-30 21:08:01 -0600337 BUG_ON(!test_bit(XPT_BUSY, &xprt->xpt_flags));
338 xprt->xpt_pool = NULL;
339 clear_bit(XPT_BUSY, &xprt->xpt_flags);
340 svc_xprt_enqueue(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
Tom Tuckera6046f72007-12-30 21:08:01 -0600342EXPORT_SYMBOL_GPL(svc_xprt_received);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344/**
345 * svc_reserve - change the space reserved for the reply to a request.
346 * @rqstp: The request in question
347 * @space: new max space to reserve
348 *
349 * Each request reserves some space on the output queue of the socket
350 * to make sure the reply fits. This function reduces that reserved
351 * space to be the amount of space used already, plus @space.
352 *
353 */
354void svc_reserve(struct svc_rqst *rqstp, int space)
355{
356 space += rqstp->rq_res.head[0].iov_len;
357
358 if (space < rqstp->rq_reserved) {
Tom Tuckerf6150c32007-12-30 21:07:57 -0600359 struct svc_xprt *xprt = rqstp->rq_xprt;
360 atomic_sub((rqstp->rq_reserved - space), &xprt->xpt_reserved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 rqstp->rq_reserved = space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Tom Tuckerf6150c32007-12-30 21:07:57 -0600363 svc_xprt_enqueue(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367static void
368svc_sock_release(struct svc_rqst *rqstp)
369{
370 struct svc_sock *svsk = rqstp->rq_sock;
371
Tom Tucker5148bf42007-12-30 21:07:25 -0600372 rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
NeilBrown44524352006-10-04 02:15:46 -0700374 svc_free_res_pages(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 rqstp->rq_res.page_len = 0;
376 rqstp->rq_res.page_base = 0;
377
378
379 /* Reset response buffer and release
380 * the reservation.
381 * But first, check that enough space was reserved
382 * for the reply, otherwise we have a bug!
383 */
384 if ((rqstp->rq_res.len) > rqstp->rq_reserved)
385 printk(KERN_ERR "RPC request reserved %d but used %d\n",
386 rqstp->rq_reserved,
387 rqstp->rq_res.len);
388
389 rqstp->rq_res.head[0].iov_len = 0;
390 svc_reserve(rqstp, 0);
391 rqstp->rq_sock = NULL;
392
Tom Tuckere1b31572007-12-30 21:07:46 -0600393 svc_xprt_put(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
396/*
397 * External function to wake up a server waiting for data
Greg Banks3262c812006-10-02 02:17:58 -0700398 * This really only makes sense for services like lockd
399 * which have exactly one thread anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 */
401void
402svc_wake_up(struct svc_serv *serv)
403{
404 struct svc_rqst *rqstp;
Greg Banks3262c812006-10-02 02:17:58 -0700405 unsigned int i;
406 struct svc_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Greg Banks3262c812006-10-02 02:17:58 -0700408 for (i = 0; i < serv->sv_nrpools; i++) {
409 pool = &serv->sv_pools[i];
410
411 spin_lock_bh(&pool->sp_lock);
412 if (!list_empty(&pool->sp_threads)) {
413 rqstp = list_entry(pool->sp_threads.next,
414 struct svc_rqst,
415 rq_list);
416 dprintk("svc: daemon %p woken up.\n", rqstp);
417 /*
418 svc_thread_dequeue(pool, rqstp);
419 rqstp->rq_sock = NULL;
420 */
421 wake_up(&rqstp->rq_wait);
422 }
423 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
Chuck Leverb92503b2007-02-12 00:53:36 -0800427union svc_pktinfo_u {
428 struct in_pktinfo pkti;
Chuck Leverb92503b2007-02-12 00:53:36 -0800429 struct in6_pktinfo pkti6;
Chuck Leverb92503b2007-02-12 00:53:36 -0800430};
David S. Millerbc375ea2007-04-12 13:35:59 -0700431#define SVC_PKTINFO_SPACE \
432 CMSG_SPACE(sizeof(union svc_pktinfo_u))
Chuck Leverb92503b2007-02-12 00:53:36 -0800433
434static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
435{
436 switch (rqstp->rq_sock->sk_sk->sk_family) {
437 case AF_INET: {
438 struct in_pktinfo *pki = CMSG_DATA(cmh);
439
440 cmh->cmsg_level = SOL_IP;
441 cmh->cmsg_type = IP_PKTINFO;
442 pki->ipi_ifindex = 0;
443 pki->ipi_spec_dst.s_addr = rqstp->rq_daddr.addr.s_addr;
444 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
445 }
446 break;
NeilBrown5a05ed72007-03-06 01:42:22 -0800447
Chuck Leverb92503b2007-02-12 00:53:36 -0800448 case AF_INET6: {
449 struct in6_pktinfo *pki = CMSG_DATA(cmh);
450
451 cmh->cmsg_level = SOL_IPV6;
452 cmh->cmsg_type = IPV6_PKTINFO;
453 pki->ipi6_ifindex = 0;
454 ipv6_addr_copy(&pki->ipi6_addr,
455 &rqstp->rq_daddr.addr6);
456 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
457 }
458 break;
Chuck Leverb92503b2007-02-12 00:53:36 -0800459 }
460 return;
461}
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463/*
464 * Generic sendto routine
465 */
466static int
467svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
468{
469 struct svc_sock *svsk = rqstp->rq_sock;
470 struct socket *sock = svsk->sk_sock;
471 int slen;
David S. Millerbc375ea2007-04-12 13:35:59 -0700472 union {
473 struct cmsghdr hdr;
474 long all[SVC_PKTINFO_SPACE / sizeof(long)];
475 } buffer;
476 struct cmsghdr *cmh = &buffer.hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 int len = 0;
478 int result;
479 int size;
480 struct page **ppage = xdr->pages;
481 size_t base = xdr->page_base;
482 unsigned int pglen = xdr->page_len;
483 unsigned int flags = MSG_MORE;
Chuck Leverad06e4b2007-02-12 00:53:32 -0800484 char buf[RPC_MAX_ADDRBUFLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 slen = xdr->len;
487
488 if (rqstp->rq_prot == IPPROTO_UDP) {
Chuck Leverb92503b2007-02-12 00:53:36 -0800489 struct msghdr msg = {
490 .msg_name = &rqstp->rq_addr,
491 .msg_namelen = rqstp->rq_addrlen,
492 .msg_control = cmh,
493 .msg_controllen = sizeof(buffer),
494 .msg_flags = MSG_MORE,
495 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Chuck Leverb92503b2007-02-12 00:53:36 -0800497 svc_set_cmsg_data(rqstp, cmh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 if (sock_sendmsg(sock, &msg, 0) < 0)
500 goto out;
501 }
502
503 /* send head */
504 if (slen == xdr->head[0].iov_len)
505 flags = 0;
NeilBrown44524352006-10-04 02:15:46 -0700506 len = kernel_sendpage(sock, rqstp->rq_respages[0], 0,
507 xdr->head[0].iov_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (len != xdr->head[0].iov_len)
509 goto out;
510 slen -= xdr->head[0].iov_len;
511 if (slen == 0)
512 goto out;
513
514 /* send page data */
515 size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
516 while (pglen > 0) {
517 if (slen == size)
518 flags = 0;
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700519 result = kernel_sendpage(sock, *ppage, base, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (result > 0)
521 len += result;
522 if (result != size)
523 goto out;
524 slen -= size;
525 pglen -= size;
526 size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
527 base = 0;
528 ppage++;
529 }
530 /* send tail */
531 if (xdr->tail[0].iov_len) {
NeilBrown44524352006-10-04 02:15:46 -0700532 result = kernel_sendpage(sock, rqstp->rq_respages[0],
533 ((unsigned long)xdr->tail[0].iov_base)
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800534 & (PAGE_SIZE-1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 xdr->tail[0].iov_len, 0);
536
537 if (result > 0)
538 len += result;
539 }
540out:
Chuck Leverad06e4b2007-02-12 00:53:32 -0800541 dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %s)\n",
542 rqstp->rq_sock, xdr->head[0].iov_base, xdr->head[0].iov_len,
543 xdr->len, len, svc_print_addr(rqstp, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 return len;
546}
547
548/*
NeilBrown80212d52006-10-02 02:17:47 -0700549 * Report socket names for nfsdfs
550 */
551static int one_sock_name(char *buf, struct svc_sock *svsk)
552{
553 int len;
554
555 switch(svsk->sk_sk->sk_family) {
556 case AF_INET:
557 len = sprintf(buf, "ipv4 %s %u.%u.%u.%u %d\n",
558 svsk->sk_sk->sk_protocol==IPPROTO_UDP?
559 "udp" : "tcp",
560 NIPQUAD(inet_sk(svsk->sk_sk)->rcv_saddr),
561 inet_sk(svsk->sk_sk)->num);
562 break;
563 default:
564 len = sprintf(buf, "*unknown-%d*\n",
565 svsk->sk_sk->sk_family);
566 }
567 return len;
568}
569
570int
NeilBrownb41b66d2006-10-02 02:17:48 -0700571svc_sock_names(char *buf, struct svc_serv *serv, char *toclose)
NeilBrown80212d52006-10-02 02:17:47 -0700572{
NeilBrownb41b66d2006-10-02 02:17:48 -0700573 struct svc_sock *svsk, *closesk = NULL;
NeilBrown80212d52006-10-02 02:17:47 -0700574 int len = 0;
575
576 if (!serv)
577 return 0;
NeilBrownaaf68cf2007-02-08 14:20:30 -0800578 spin_lock_bh(&serv->sv_lock);
Tom Tucker7a182082007-12-30 21:07:53 -0600579 list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list) {
NeilBrown80212d52006-10-02 02:17:47 -0700580 int onelen = one_sock_name(buf+len, svsk);
NeilBrownb41b66d2006-10-02 02:17:48 -0700581 if (toclose && strcmp(toclose, buf+len) == 0)
582 closesk = svsk;
583 else
584 len += onelen;
NeilBrown80212d52006-10-02 02:17:47 -0700585 }
NeilBrownaaf68cf2007-02-08 14:20:30 -0800586 spin_unlock_bh(&serv->sv_lock);
NeilBrownb41b66d2006-10-02 02:17:48 -0700587 if (closesk)
NeilBrown5680c442006-10-04 02:15:45 -0700588 /* Should unregister with portmap, but you cannot
589 * unregister just one protocol...
590 */
Tom Tucker7a182082007-12-30 21:07:53 -0600591 svc_close_xprt(&closesk->sk_xprt);
NeilBrown37a03472006-10-04 02:15:44 -0700592 else if (toclose)
593 return -ENOENT;
NeilBrown80212d52006-10-02 02:17:47 -0700594 return len;
595}
596EXPORT_SYMBOL(svc_sock_names);
597
598/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 * Check input queue length
600 */
601static int
602svc_recv_available(struct svc_sock *svsk)
603{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 struct socket *sock = svsk->sk_sock;
605 int avail, err;
606
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700607 err = kernel_sock_ioctl(sock, TIOCINQ, (unsigned long) &avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 return (err >= 0)? avail : err;
610}
611
612/*
613 * Generic recvfrom routine.
614 */
615static int
616svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr, int buflen)
617{
Chuck Lever067d7812007-02-12 00:53:30 -0800618 struct svc_sock *svsk = rqstp->rq_sock;
Chuck Lever1ba95102007-02-12 00:53:31 -0800619 struct msghdr msg = {
620 .msg_flags = MSG_DONTWAIT,
621 };
Frank van Maarseveena9747692007-07-09 22:21:39 +0200622 struct sockaddr *sin;
Chuck Lever1ba95102007-02-12 00:53:31 -0800623 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Chuck Lever1ba95102007-02-12 00:53:31 -0800625 len = kernel_recvmsg(svsk->sk_sock, &msg, iov, nr, buflen,
626 msg.msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 /* sock_recvmsg doesn't fill in the name/namelen, so we must..
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 */
Chuck Lever067d7812007-02-12 00:53:30 -0800630 memcpy(&rqstp->rq_addr, &svsk->sk_remote, svsk->sk_remotelen);
631 rqstp->rq_addrlen = svsk->sk_remotelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Frank van Maarseveena9747692007-07-09 22:21:39 +0200633 /* Destination address in request is needed for binding the
634 * source address in RPC callbacks later.
635 */
636 sin = (struct sockaddr *)&svsk->sk_local;
637 switch (sin->sa_family) {
638 case AF_INET:
639 rqstp->rq_daddr.addr = ((struct sockaddr_in *)sin)->sin_addr;
640 break;
641 case AF_INET6:
642 rqstp->rq_daddr.addr6 = ((struct sockaddr_in6 *)sin)->sin6_addr;
643 break;
644 }
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
Chuck Lever1ba95102007-02-12 00:53:31 -0800647 svsk, iov[0].iov_base, iov[0].iov_len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 return len;
650}
651
652/*
653 * Set socket snd and rcv buffer lengths
654 */
655static inline void
656svc_sock_setbufsize(struct socket *sock, unsigned int snd, unsigned int rcv)
657{
658#if 0
659 mm_segment_t oldfs;
660 oldfs = get_fs(); set_fs(KERNEL_DS);
661 sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
662 (char*)&snd, sizeof(snd));
663 sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
664 (char*)&rcv, sizeof(rcv));
665#else
666 /* sock_setsockopt limits use to sysctl_?mem_max,
667 * which isn't acceptable. Until that is made conditional
668 * on not having CAP_SYS_RESOURCE or similar, we go direct...
669 * DaveM said I could!
670 */
671 lock_sock(sock->sk);
672 sock->sk->sk_sndbuf = snd * 2;
673 sock->sk->sk_rcvbuf = rcv * 2;
674 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK|SOCK_RCVBUF_LOCK;
675 release_sock(sock->sk);
676#endif
677}
678/*
679 * INET callback when data has been received on the socket.
680 */
681static void
682svc_udp_data_ready(struct sock *sk, int count)
683{
Neil Brown939bb7e2005-09-13 01:25:39 -0700684 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Neil Brown939bb7e2005-09-13 01:25:39 -0700686 if (svsk) {
687 dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
Tom Tucker02fc6c32007-12-30 21:07:48 -0600688 svsk, sk, count,
689 test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
690 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600691 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -0700692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
694 wake_up_interruptible(sk->sk_sleep);
695}
696
697/*
698 * INET callback when space is newly available on the socket.
699 */
700static void
701svc_write_space(struct sock *sk)
702{
703 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
704
705 if (svsk) {
706 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
Tom Tucker02fc6c32007-12-30 21:07:48 -0600707 svsk, sk, test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
Tom Tuckerf6150c32007-12-30 21:07:57 -0600708 svc_xprt_enqueue(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710
711 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) {
Neil Brown939bb7e2005-09-13 01:25:39 -0700712 dprintk("RPC svc_write_space: someone sleeping on %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 svsk);
714 wake_up_interruptible(sk->sk_sleep);
715 }
716}
717
NeilBrown7a37f572007-03-06 01:42:21 -0800718static inline void svc_udp_get_dest_address(struct svc_rqst *rqstp,
719 struct cmsghdr *cmh)
Chuck Lever95756482007-02-12 00:53:38 -0800720{
721 switch (rqstp->rq_sock->sk_sk->sk_family) {
722 case AF_INET: {
NeilBrown7a37f572007-03-06 01:42:21 -0800723 struct in_pktinfo *pki = CMSG_DATA(cmh);
724 rqstp->rq_daddr.addr.s_addr = pki->ipi_spec_dst.s_addr;
Chuck Lever95756482007-02-12 00:53:38 -0800725 break;
NeilBrown7a37f572007-03-06 01:42:21 -0800726 }
Chuck Lever95756482007-02-12 00:53:38 -0800727 case AF_INET6: {
NeilBrown7a37f572007-03-06 01:42:21 -0800728 struct in6_pktinfo *pki = CMSG_DATA(cmh);
729 ipv6_addr_copy(&rqstp->rq_daddr.addr6, &pki->ipi6_addr);
Chuck Lever95756482007-02-12 00:53:38 -0800730 break;
NeilBrown7a37f572007-03-06 01:42:21 -0800731 }
Chuck Lever95756482007-02-12 00:53:38 -0800732 }
Chuck Lever95756482007-02-12 00:53:38 -0800733}
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735/*
736 * Receive a datagram from a UDP socket.
737 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738static int
739svc_udp_recvfrom(struct svc_rqst *rqstp)
740{
741 struct svc_sock *svsk = rqstp->rq_sock;
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600742 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 struct sk_buff *skb;
David S. Millerbc375ea2007-04-12 13:35:59 -0700744 union {
745 struct cmsghdr hdr;
746 long all[SVC_PKTINFO_SPACE / sizeof(long)];
747 } buffer;
748 struct cmsghdr *cmh = &buffer.hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 int err, len;
NeilBrown7a37f572007-03-06 01:42:21 -0800750 struct msghdr msg = {
751 .msg_name = svc_addr(rqstp),
752 .msg_control = cmh,
753 .msg_controllen = sizeof(buffer),
754 .msg_flags = MSG_DONTWAIT,
755 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Tom Tucker02fc6c32007-12-30 21:07:48 -0600757 if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 /* udp sockets need large rcvbuf as all pending
759 * requests are still in that buffer. sndbuf must
760 * also be large enough that there is enough space
Greg Banks3262c812006-10-02 02:17:58 -0700761 * for one reply per thread. We count all threads
762 * rather than threads in a particular pool, which
763 * provides an upper bound on the number of threads
764 * which will access the socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 */
766 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700767 (serv->sv_nrthreads+3) * serv->sv_max_mesg,
768 (serv->sv_nrthreads+3) * serv->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
Tom Tuckera6046f72007-12-30 21:08:01 -0600771 svc_xprt_received(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return svc_deferred_recv(rqstp);
773 }
774
Tom Tucker02fc6c32007-12-30 21:07:48 -0600775 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
NeilBrown05ed6902007-05-09 02:34:55 -0700776 skb = NULL;
777 err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
778 0, 0, MSG_PEEK | MSG_DONTWAIT);
779 if (err >= 0)
780 skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err);
781
782 if (skb == NULL) {
783 if (err != -EAGAIN) {
784 /* possibly an icmp error */
785 dprintk("svc: recvfrom returned error %d\n", -err);
Tom Tucker02fc6c32007-12-30 21:07:48 -0600786 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
Tom Tuckera6046f72007-12-30 21:08:01 -0600788 svc_xprt_received(&svsk->sk_xprt);
NeilBrown05ed6902007-05-09 02:34:55 -0700789 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
NeilBrown7a37f572007-03-06 01:42:21 -0800791 rqstp->rq_addrlen = sizeof(rqstp->rq_addr);
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700792 if (skb->tstamp.tv64 == 0) {
793 skb->tstamp = ktime_get_real();
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800794 /* Don't enable netstamp, sunrpc doesn't
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 need that much accuracy */
796 }
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700797 svsk->sk_sk->sk_stamp = skb->tstamp;
Tom Tucker02fc6c32007-12-30 21:07:48 -0600798 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 /*
801 * Maybe more packets - kick another thread ASAP.
802 */
Tom Tuckera6046f72007-12-30 21:08:01 -0600803 svc_xprt_received(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 len = skb->len - sizeof(struct udphdr);
806 rqstp->rq_arg.len = len;
807
Chuck Lever95756482007-02-12 00:53:38 -0800808 rqstp->rq_prot = IPPROTO_UDP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
NeilBrown7a37f572007-03-06 01:42:21 -0800810 if (cmh->cmsg_level != IPPROTO_IP ||
811 cmh->cmsg_type != IP_PKTINFO) {
812 if (net_ratelimit())
813 printk("rpcsvc: received unknown control message:"
814 "%d/%d\n",
815 cmh->cmsg_level, cmh->cmsg_type);
816 skb_free_datagram(svsk->sk_sk, skb);
817 return 0;
818 }
819 svc_udp_get_dest_address(rqstp, cmh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 if (skb_is_nonlinear(skb)) {
822 /* we have to copy */
823 local_bh_disable();
824 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
825 local_bh_enable();
826 /* checksum error */
827 skb_free_datagram(svsk->sk_sk, skb);
828 return 0;
829 }
830 local_bh_enable();
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800831 skb_free_datagram(svsk->sk_sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 } else {
833 /* we can use it in-place */
834 rqstp->rq_arg.head[0].iov_base = skb->data + sizeof(struct udphdr);
835 rqstp->rq_arg.head[0].iov_len = len;
Herbert Xufb286bb2005-11-10 13:01:24 -0800836 if (skb_checksum_complete(skb)) {
837 skb_free_datagram(svsk->sk_sk, skb);
838 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
Tom Tucker5148bf42007-12-30 21:07:25 -0600840 rqstp->rq_xprt_ctxt = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
842
843 rqstp->rq_arg.page_base = 0;
844 if (len <= rqstp->rq_arg.head[0].iov_len) {
845 rqstp->rq_arg.head[0].iov_len = len;
846 rqstp->rq_arg.page_len = 0;
NeilBrown44524352006-10-04 02:15:46 -0700847 rqstp->rq_respages = rqstp->rq_pages+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 } else {
849 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
NeilBrown44524352006-10-04 02:15:46 -0700850 rqstp->rq_respages = rqstp->rq_pages + 1 +
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700851 DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
853
854 if (serv->sv_stats)
855 serv->sv_stats->netudpcnt++;
856
857 return len;
858}
859
860static int
861svc_udp_sendto(struct svc_rqst *rqstp)
862{
863 int error;
864
865 error = svc_sendto(rqstp, &rqstp->rq_res);
866 if (error == -ECONNREFUSED)
867 /* ICMP error on earlier request. */
868 error = svc_sendto(rqstp, &rqstp->rq_res);
869
870 return error;
871}
872
Tom Tuckere831fe62007-12-30 21:07:29 -0600873static void svc_udp_prep_reply_hdr(struct svc_rqst *rqstp)
874{
875}
876
Tom Tucker323bee32007-12-30 21:07:31 -0600877static int svc_udp_has_wspace(struct svc_xprt *xprt)
878{
879 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600880 struct svc_serv *serv = xprt->xpt_server;
Tom Tucker323bee32007-12-30 21:07:31 -0600881 unsigned long required;
882
883 /*
884 * Set the SOCK_NOSPACE flag before checking the available
885 * sock space.
886 */
887 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
Tom Tucker7a90e8c2007-12-30 21:07:55 -0600888 required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
Tom Tucker323bee32007-12-30 21:07:31 -0600889 if (required*2 > sock_wspace(svsk->sk_sk))
890 return 0;
891 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
892 return 1;
893}
894
Tom Tucker38a417c2007-12-30 21:07:36 -0600895static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt)
896{
897 BUG();
898 return NULL;
899}
900
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600901static struct svc_xprt *svc_udp_create(struct svc_serv *serv,
902 struct sockaddr *sa, int salen,
903 int flags)
904{
905 return svc_create_socket(serv, IPPROTO_UDP, sa, salen, flags);
906}
907
Tom Tucker360d87382007-12-30 21:07:17 -0600908static struct svc_xprt_ops svc_udp_ops = {
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600909 .xpo_create = svc_udp_create,
Tom Tucker5d137992007-12-30 21:07:23 -0600910 .xpo_recvfrom = svc_udp_recvfrom,
911 .xpo_sendto = svc_udp_sendto,
Tom Tucker5148bf42007-12-30 21:07:25 -0600912 .xpo_release_rqst = svc_release_skb,
Tom Tucker755ccea2007-12-30 21:07:27 -0600913 .xpo_detach = svc_sock_detach,
914 .xpo_free = svc_sock_free,
Tom Tuckere831fe62007-12-30 21:07:29 -0600915 .xpo_prep_reply_hdr = svc_udp_prep_reply_hdr,
Tom Tucker323bee32007-12-30 21:07:31 -0600916 .xpo_has_wspace = svc_udp_has_wspace,
Tom Tucker38a417c2007-12-30 21:07:36 -0600917 .xpo_accept = svc_udp_accept,
Tom Tucker360d87382007-12-30 21:07:17 -0600918};
919
920static struct svc_xprt_class svc_udp_class = {
921 .xcl_name = "udp",
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600922 .xcl_owner = THIS_MODULE,
Tom Tucker360d87382007-12-30 21:07:17 -0600923 .xcl_ops = &svc_udp_ops,
Tom Tucker49023152007-12-30 21:07:21 -0600924 .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP,
Tom Tucker360d87382007-12-30 21:07:17 -0600925};
926
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600927static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
NeilBrown7a37f572007-03-06 01:42:21 -0800929 int one = 1;
930 mm_segment_t oldfs;
931
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600932 svc_xprt_init(&svc_udp_class, &svsk->sk_xprt, serv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 svsk->sk_sk->sk_data_ready = svc_udp_data_ready;
934 svsk->sk_sk->sk_write_space = svc_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 /* initialise setting must have enough space to
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800937 * receive and respond to one request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 * svc_udp_recvfrom will re-adjust if necessary
939 */
940 svc_sock_setbufsize(svsk->sk_sock,
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600941 3 * svsk->sk_xprt.xpt_server->sv_max_mesg,
942 3 * svsk->sk_xprt.xpt_server->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Tom Tucker02fc6c32007-12-30 21:07:48 -0600944 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* might have come in before data_ready set up */
945 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
NeilBrown7a37f572007-03-06 01:42:21 -0800946
947 oldfs = get_fs();
948 set_fs(KERNEL_DS);
949 /* make sure we get destination address info */
950 svsk->sk_sock->ops->setsockopt(svsk->sk_sock, IPPROTO_IP, IP_PKTINFO,
951 (char __user *)&one, sizeof(one));
952 set_fs(oldfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
955/*
956 * A data_ready event on a listening socket means there's a connection
957 * pending. Do not use state_change as a substitute for it.
958 */
959static void
960svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
961{
Neil Brown939bb7e2005-09-13 01:25:39 -0700962 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 dprintk("svc: socket %p TCP (listen) state change %d\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700965 sk, sk->sk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Neil Brown939bb7e2005-09-13 01:25:39 -0700967 /*
968 * This callback may called twice when a new connection
969 * is established as a child socket inherits everything
970 * from a parent LISTEN socket.
971 * 1) data_ready method of the parent socket will be called
972 * when one of child sockets become ESTABLISHED.
973 * 2) data_ready method of the child socket may be called
974 * when it receives data before the socket is accepted.
975 * In case of 2, we should ignore it silently.
976 */
977 if (sk->sk_state == TCP_LISTEN) {
978 if (svsk) {
Tom Tucker02fc6c32007-12-30 21:07:48 -0600979 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600980 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -0700981 } else
982 printk("svc: socket %p: no user data\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
Neil Brown939bb7e2005-09-13 01:25:39 -0700984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
986 wake_up_interruptible_all(sk->sk_sleep);
987}
988
989/*
990 * A state change on a connected socket means it's dying or dead.
991 */
992static void
993svc_tcp_state_change(struct sock *sk)
994{
Neil Brown939bb7e2005-09-13 01:25:39 -0700995 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700998 sk, sk->sk_state, sk->sk_user_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Neil Brown939bb7e2005-09-13 01:25:39 -07001000 if (!svsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 printk("svc: socket %p: no user data\n", sk);
Neil Brown939bb7e2005-09-13 01:25:39 -07001002 else {
Tom Tucker02fc6c32007-12-30 21:07:48 -06001003 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -06001004 svc_xprt_enqueue(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
1007 wake_up_interruptible_all(sk->sk_sleep);
1008}
1009
1010static void
1011svc_tcp_data_ready(struct sock *sk, int count)
1012{
Neil Brown939bb7e2005-09-13 01:25:39 -07001013 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 dprintk("svc: socket %p TCP data ready (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -07001016 sk, sk->sk_user_data);
1017 if (svsk) {
Tom Tucker02fc6c32007-12-30 21:07:48 -06001018 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -06001019 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -07001020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
1022 wake_up_interruptible(sk->sk_sleep);
1023}
1024
Chuck Leverbcdb81a2007-02-12 00:53:37 -08001025static inline int svc_port_is_privileged(struct sockaddr *sin)
1026{
1027 switch (sin->sa_family) {
1028 case AF_INET:
1029 return ntohs(((struct sockaddr_in *)sin)->sin_port)
1030 < PROT_SOCK;
Chuck Leverbcdb81a2007-02-12 00:53:37 -08001031 case AF_INET6:
1032 return ntohs(((struct sockaddr_in6 *)sin)->sin6_port)
1033 < PROT_SOCK;
Chuck Leverbcdb81a2007-02-12 00:53:37 -08001034 default:
1035 return 0;
1036 }
1037}
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039/*
1040 * Accept a TCP connection
1041 */
Tom Tucker38a417c2007-12-30 21:07:36 -06001042static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
Tom Tucker38a417c2007-12-30 21:07:36 -06001044 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001045 struct sockaddr_storage addr;
1046 struct sockaddr *sin = (struct sockaddr *) &addr;
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001047 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 struct socket *sock = svsk->sk_sock;
1049 struct socket *newsock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 struct svc_sock *newsvsk;
1051 int err, slen;
Chuck Leverad06e4b2007-02-12 00:53:32 -08001052 char buf[RPC_MAX_ADDRBUFLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
1055 if (!sock)
Tom Tucker38a417c2007-12-30 21:07:36 -06001056 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Tom Tucker02fc6c32007-12-30 21:07:48 -06001058 clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001059 err = kernel_accept(sock, &newsock, O_NONBLOCK);
1060 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (err == -ENOMEM)
1062 printk(KERN_WARNING "%s: no more sockets!\n",
1063 serv->sv_name);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001064 else if (err != -EAGAIN && net_ratelimit())
1065 printk(KERN_WARNING "%s: accept failed (err %d)!\n",
1066 serv->sv_name, -err);
Tom Tucker38a417c2007-12-30 21:07:36 -06001067 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
Tom Tucker02fc6c32007-12-30 21:07:48 -06001069 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001071 err = kernel_getpeername(newsock, sin, &slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (err < 0) {
1073 if (net_ratelimit())
1074 printk(KERN_WARNING "%s: peername failed (err %d)!\n",
1075 serv->sv_name, -err);
1076 goto failed; /* aborted connection or whatever */
1077 }
1078
1079 /* Ideally, we would want to reject connections from unauthorized
Chuck Leverad06e4b2007-02-12 00:53:32 -08001080 * hosts here, but when we get encryption, the IP of the host won't
1081 * tell us anything. For now just warn about unpriv connections.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 */
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001083 if (!svc_port_is_privileged(sin)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 dprintk(KERN_WARNING
Chuck Leverad06e4b2007-02-12 00:53:32 -08001085 "%s: connect from unprivileged port: %s\n",
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001086 serv->sv_name,
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001087 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
Chuck Leverad06e4b2007-02-12 00:53:32 -08001089 dprintk("%s: connect from %s\n", serv->sv_name,
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001090 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 /* make sure that a write doesn't block forever when
1093 * low on memory
1094 */
1095 newsock->sk->sk_sndtimeo = HZ*30;
1096
Chuck Lever6b174332007-02-12 00:53:28 -08001097 if (!(newsvsk = svc_setup_socket(serv, newsock, &err,
1098 (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 goto failed;
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001100 memcpy(&newsvsk->sk_remote, sin, slen);
Chuck Lever067d7812007-02-12 00:53:30 -08001101 newsvsk->sk_remotelen = slen;
Frank van Maarseveena9747692007-07-09 22:21:39 +02001102 err = kernel_getsockname(newsock, sin, &slen);
1103 if (unlikely(err < 0)) {
1104 dprintk("svc_tcp_accept: kernel_getsockname error %d\n", -err);
1105 slen = offsetof(struct sockaddr, sa_data);
1106 }
1107 memcpy(&newsvsk->sk_local, sin, slen);
Chuck Lever067d7812007-02-12 00:53:30 -08001108
Tom Tuckera6046f72007-12-30 21:08:01 -06001109 svc_xprt_received(&newsvsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Tom Tuckerf9f3cc42007-12-30 21:07:40 -06001111 if (serv->sv_stats)
1112 serv->sv_stats->nettcpconn++;
1113
1114 return &newsvsk->sk_xprt;
1115
1116failed:
1117 sock_release(newsock);
1118 return NULL;
1119}
1120
1121/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 * Receive data from a TCP socket.
1123 */
1124static int
1125svc_tcp_recvfrom(struct svc_rqst *rqstp)
1126{
1127 struct svc_sock *svsk = rqstp->rq_sock;
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001128 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 int len;
NeilBrown3cc03b12006-10-04 02:15:47 -07001130 struct kvec *vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 int pnum, vlen;
1132
1133 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
Tom Tucker02fc6c32007-12-30 21:07:48 -06001134 svsk, test_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags),
1135 test_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags),
1136 test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
Tom Tuckera6046f72007-12-30 21:08:01 -06001139 svc_xprt_received(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return svc_deferred_recv(rqstp);
1141 }
1142
Tom Tucker02fc6c32007-12-30 21:07:48 -06001143 if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 /* sndbuf needs to have room for one request
1145 * per thread, otherwise we can stall even when the
1146 * network isn't a bottleneck.
Greg Banks3262c812006-10-02 02:17:58 -07001147 *
1148 * We count all threads rather than threads in a
1149 * particular pool, which provides an upper bound
1150 * on the number of threads which will access the socket.
1151 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 * rcvbuf just needs to be able to hold a few requests.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001153 * Normally they will be removed from the queue
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 * as soon a a complete request arrives.
1155 */
1156 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001157 (serv->sv_nrthreads+3) * serv->sv_max_mesg,
1158 3 * serv->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Tom Tucker02fc6c32007-12-30 21:07:48 -06001160 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 /* Receive data. If we haven't got the record length yet, get
1163 * the next four bytes. Otherwise try to gobble up as much as
1164 * possible up to the complete record length.
1165 */
1166 if (svsk->sk_tcplen < 4) {
1167 unsigned long want = 4 - svsk->sk_tcplen;
1168 struct kvec iov;
1169
1170 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
1171 iov.iov_len = want;
1172 if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
1173 goto error;
1174 svsk->sk_tcplen += len;
1175
1176 if (len < want) {
1177 dprintk("svc: short recvfrom while reading record length (%d of %lu)\n",
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001178 len, want);
Tom Tuckera6046f72007-12-30 21:08:01 -06001179 svc_xprt_received(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 return -EAGAIN; /* record header not complete */
1181 }
1182
1183 svsk->sk_reclen = ntohl(svsk->sk_reclen);
1184 if (!(svsk->sk_reclen & 0x80000000)) {
1185 /* FIXME: technically, a record can be fragmented,
1186 * and non-terminal fragments will not have the top
1187 * bit set in the fragment length header.
1188 * But apparently no known nfs clients send fragmented
1189 * records. */
NeilBrown34e9a632007-01-29 13:19:52 -08001190 if (net_ratelimit())
1191 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx"
1192 " (non-terminal)\n",
1193 (unsigned long) svsk->sk_reclen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 goto err_delete;
1195 }
1196 svsk->sk_reclen &= 0x7fffffff;
1197 dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001198 if (svsk->sk_reclen > serv->sv_max_mesg) {
NeilBrown34e9a632007-01-29 13:19:52 -08001199 if (net_ratelimit())
1200 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx"
1201 " (large)\n",
1202 (unsigned long) svsk->sk_reclen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 goto err_delete;
1204 }
1205 }
1206
1207 /* Check whether enough data is available */
1208 len = svc_recv_available(svsk);
1209 if (len < 0)
1210 goto error;
1211
1212 if (len < svsk->sk_reclen) {
1213 dprintk("svc: incomplete TCP record (%d of %d)\n",
1214 len, svsk->sk_reclen);
Tom Tuckera6046f72007-12-30 21:08:01 -06001215 svc_xprt_received(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return -EAGAIN; /* record not complete */
1217 }
1218 len = svsk->sk_reclen;
Tom Tucker02fc6c32007-12-30 21:07:48 -06001219 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
NeilBrown3cc03b12006-10-04 02:15:47 -07001221 vec = rqstp->rq_vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 vec[0] = rqstp->rq_arg.head[0];
1223 vlen = PAGE_SIZE;
1224 pnum = 1;
1225 while (vlen < len) {
NeilBrown44524352006-10-04 02:15:46 -07001226 vec[pnum].iov_base = page_address(rqstp->rq_pages[pnum]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 vec[pnum].iov_len = PAGE_SIZE;
1228 pnum++;
1229 vlen += PAGE_SIZE;
1230 }
NeilBrown44524352006-10-04 02:15:46 -07001231 rqstp->rq_respages = &rqstp->rq_pages[pnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 /* Now receive data */
1234 len = svc_recvfrom(rqstp, vec, pnum, len);
1235 if (len < 0)
1236 goto error;
1237
1238 dprintk("svc: TCP complete record (%d bytes)\n", len);
1239 rqstp->rq_arg.len = len;
1240 rqstp->rq_arg.page_base = 0;
1241 if (len <= rqstp->rq_arg.head[0].iov_len) {
1242 rqstp->rq_arg.head[0].iov_len = len;
1243 rqstp->rq_arg.page_len = 0;
1244 } else {
1245 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
1246 }
1247
Tom Tucker5148bf42007-12-30 21:07:25 -06001248 rqstp->rq_xprt_ctxt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 rqstp->rq_prot = IPPROTO_TCP;
1250
1251 /* Reset TCP read info */
1252 svsk->sk_reclen = 0;
1253 svsk->sk_tcplen = 0;
1254
Tom Tuckera6046f72007-12-30 21:08:01 -06001255 svc_xprt_received(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (serv->sv_stats)
1257 serv->sv_stats->nettcpcnt++;
1258
1259 return len;
1260
1261 err_delete:
Tom Tucker02fc6c32007-12-30 21:07:48 -06001262 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return -EAGAIN;
1264
1265 error:
1266 if (len == -EAGAIN) {
1267 dprintk("RPC: TCP recvfrom got EAGAIN\n");
Tom Tuckera6046f72007-12-30 21:08:01 -06001268 svc_xprt_received(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 } else {
1270 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001271 svsk->sk_xprt.xpt_server->sv_name, -len);
Olaf Kirch93fbf1a2006-01-06 00:19:56 -08001272 goto err_delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
1274
1275 return len;
1276}
1277
1278/*
1279 * Send out data on TCP socket.
1280 */
1281static int
1282svc_tcp_sendto(struct svc_rqst *rqstp)
1283{
1284 struct xdr_buf *xbufp = &rqstp->rq_res;
1285 int sent;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001286 __be32 reclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 /* Set up the first element of the reply kvec.
1289 * Any other kvecs that may be in use have been taken
1290 * care of by the server implementation itself.
1291 */
1292 reclen = htonl(0x80000000|((xbufp->len ) - 4));
1293 memcpy(xbufp->head[0].iov_base, &reclen, 4);
1294
Tom Tucker02fc6c32007-12-30 21:07:48 -06001295 if (test_bit(XPT_DEAD, &rqstp->rq_sock->sk_xprt.xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return -ENOTCONN;
1297
1298 sent = svc_sendto(rqstp, &rqstp->rq_res);
1299 if (sent != xbufp->len) {
1300 printk(KERN_NOTICE "rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n",
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001301 rqstp->rq_sock->sk_xprt.xpt_server->sv_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 (sent<0)?"got error":"sent only",
1303 sent, xbufp->len);
Tom Tucker02fc6c32007-12-30 21:07:48 -06001304 set_bit(XPT_CLOSE, &rqstp->rq_sock->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -06001305 svc_xprt_enqueue(rqstp->rq_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 sent = -EAGAIN;
1307 }
1308 return sent;
1309}
1310
Tom Tuckere831fe62007-12-30 21:07:29 -06001311/*
1312 * Setup response header. TCP has a 4B record length field.
1313 */
1314static void svc_tcp_prep_reply_hdr(struct svc_rqst *rqstp)
1315{
1316 struct kvec *resv = &rqstp->rq_res.head[0];
1317
1318 /* tcp needs a space for the record length... */
1319 svc_putnl(resv, 0);
1320}
1321
Tom Tucker323bee32007-12-30 21:07:31 -06001322static int svc_tcp_has_wspace(struct svc_xprt *xprt)
1323{
1324 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001325 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Tom Tucker323bee32007-12-30 21:07:31 -06001326 int required;
1327 int wspace;
1328
1329 /*
1330 * Set the SOCK_NOSPACE flag before checking the available
1331 * sock space.
1332 */
1333 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
Tom Tucker7a90e8c2007-12-30 21:07:55 -06001334 required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
Tom Tucker323bee32007-12-30 21:07:31 -06001335 wspace = sk_stream_wspace(svsk->sk_sk);
1336
1337 if (wspace < sk_stream_min_wspace(svsk->sk_sk))
1338 return 0;
1339 if (required * 2 > wspace)
1340 return 0;
1341
1342 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
1343 return 1;
1344}
1345
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001346static struct svc_xprt *svc_tcp_create(struct svc_serv *serv,
1347 struct sockaddr *sa, int salen,
1348 int flags)
1349{
1350 return svc_create_socket(serv, IPPROTO_TCP, sa, salen, flags);
1351}
1352
Tom Tucker360d87382007-12-30 21:07:17 -06001353static struct svc_xprt_ops svc_tcp_ops = {
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001354 .xpo_create = svc_tcp_create,
Tom Tucker5d137992007-12-30 21:07:23 -06001355 .xpo_recvfrom = svc_tcp_recvfrom,
1356 .xpo_sendto = svc_tcp_sendto,
Tom Tucker5148bf42007-12-30 21:07:25 -06001357 .xpo_release_rqst = svc_release_skb,
Tom Tucker755ccea2007-12-30 21:07:27 -06001358 .xpo_detach = svc_sock_detach,
1359 .xpo_free = svc_sock_free,
Tom Tuckere831fe62007-12-30 21:07:29 -06001360 .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr,
Tom Tucker323bee32007-12-30 21:07:31 -06001361 .xpo_has_wspace = svc_tcp_has_wspace,
Tom Tucker38a417c2007-12-30 21:07:36 -06001362 .xpo_accept = svc_tcp_accept,
Tom Tucker360d87382007-12-30 21:07:17 -06001363};
1364
1365static struct svc_xprt_class svc_tcp_class = {
1366 .xcl_name = "tcp",
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001367 .xcl_owner = THIS_MODULE,
Tom Tucker360d87382007-12-30 21:07:17 -06001368 .xcl_ops = &svc_tcp_ops,
Tom Tucker49023152007-12-30 21:07:21 -06001369 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
Tom Tucker360d87382007-12-30 21:07:17 -06001370};
1371
1372void svc_init_xprt_sock(void)
1373{
1374 svc_reg_xprt_class(&svc_tcp_class);
1375 svc_reg_xprt_class(&svc_udp_class);
1376}
1377
1378void svc_cleanup_xprt_sock(void)
1379{
1380 svc_unreg_xprt_class(&svc_tcp_class);
1381 svc_unreg_xprt_class(&svc_udp_class);
1382}
1383
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001384static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
1386 struct sock *sk = svsk->sk_sk;
1387 struct tcp_sock *tp = tcp_sk(sk);
1388
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001389 svc_xprt_init(&svc_tcp_class, &svsk->sk_xprt, serv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 if (sk->sk_state == TCP_LISTEN) {
1392 dprintk("setting up TCP socket for listening\n");
Tom Tucker02fc6c32007-12-30 21:07:48 -06001393 set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 sk->sk_data_ready = svc_tcp_listen_data_ready;
Tom Tucker02fc6c32007-12-30 21:07:48 -06001395 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 } else {
1397 dprintk("setting up TCP socket for reading\n");
1398 sk->sk_state_change = svc_tcp_state_change;
1399 sk->sk_data_ready = svc_tcp_data_ready;
1400 sk->sk_write_space = svc_write_space;
1401
1402 svsk->sk_reclen = 0;
1403 svsk->sk_tcplen = 0;
1404
1405 tp->nonagle = 1; /* disable Nagle's algorithm */
1406
1407 /* initialise setting must have enough space to
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001408 * receive and respond to one request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 * svc_tcp_recvfrom will re-adjust if necessary
1410 */
1411 svc_sock_setbufsize(svsk->sk_sock,
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001412 3 * svsk->sk_xprt.xpt_server->sv_max_mesg,
1413 3 * svsk->sk_xprt.xpt_server->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Tom Tucker02fc6c32007-12-30 21:07:48 -06001415 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
1416 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001417 if (sk->sk_state != TCP_ESTABLISHED)
Tom Tucker02fc6c32007-12-30 21:07:48 -06001418 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
1420}
1421
1422void
1423svc_sock_update_bufs(struct svc_serv *serv)
1424{
1425 /*
1426 * The number of server threads has changed. Update
1427 * rcvbuf and sndbuf accordingly on all sockets
1428 */
1429 struct list_head *le;
1430
1431 spin_lock_bh(&serv->sv_lock);
1432 list_for_each(le, &serv->sv_permsocks) {
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001433 struct svc_sock *svsk =
Tom Tucker7a182082007-12-30 21:07:53 -06001434 list_entry(le, struct svc_sock, sk_xprt.xpt_list);
Tom Tucker02fc6c32007-12-30 21:07:48 -06001435 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 }
1437 list_for_each(le, &serv->sv_tempsocks) {
1438 struct svc_sock *svsk =
Tom Tucker7a182082007-12-30 21:07:53 -06001439 list_entry(le, struct svc_sock, sk_xprt.xpt_list);
Tom Tucker02fc6c32007-12-30 21:07:48 -06001440 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
1442 spin_unlock_bh(&serv->sv_lock);
1443}
1444
1445/*
Tom Tuckere1b31572007-12-30 21:07:46 -06001446 * Make sure that we don't have too many active connections. If we
1447 * have, something must be dropped.
1448 *
1449 * There's no point in trying to do random drop here for DoS
1450 * prevention. The NFS clients does 1 reconnect in 15 seconds. An
1451 * attacker can easily beat that.
1452 *
1453 * The only somewhat efficient mechanism would be if drop old
1454 * connections from the same IP first. But right now we don't even
1455 * record the client IP in svc_sock.
1456 */
1457static void svc_check_conn_limits(struct svc_serv *serv)
1458{
1459 if (serv->sv_tmpcnt > (serv->sv_nrthreads+3)*20) {
1460 struct svc_sock *svsk = NULL;
1461 spin_lock_bh(&serv->sv_lock);
1462 if (!list_empty(&serv->sv_tempsocks)) {
1463 if (net_ratelimit()) {
1464 /* Try to help the admin */
1465 printk(KERN_NOTICE "%s: too many open TCP "
1466 "sockets, consider increasing the "
1467 "number of nfsd threads\n",
1468 serv->sv_name);
1469 }
1470 /*
1471 * Always select the oldest socket. It's not fair,
1472 * but so is life
1473 */
1474 svsk = list_entry(serv->sv_tempsocks.prev,
1475 struct svc_sock,
Tom Tucker7a182082007-12-30 21:07:53 -06001476 sk_xprt.xpt_list);
Tom Tucker02fc6c32007-12-30 21:07:48 -06001477 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Tom Tuckere1b31572007-12-30 21:07:46 -06001478 svc_xprt_get(&svsk->sk_xprt);
1479 }
1480 spin_unlock_bh(&serv->sv_lock);
1481
1482 if (svsk) {
Tom Tuckerf6150c32007-12-30 21:07:57 -06001483 svc_xprt_enqueue(&svsk->sk_xprt);
Tom Tuckere1b31572007-12-30 21:07:46 -06001484 svc_xprt_put(&svsk->sk_xprt);
1485 }
1486 }
1487}
1488
1489/*
Greg Banks3262c812006-10-02 02:17:58 -07001490 * Receive the next request on any socket. This code is carefully
1491 * organised not to touch any cachelines in the shared svc_serv
1492 * structure, only cachelines in the local svc_pool.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 */
1494int
NeilBrown6fb2b472006-10-02 02:17:50 -07001495svc_recv(struct svc_rqst *rqstp, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
Chuck Lever27459f02007-02-12 00:53:34 -08001497 struct svc_sock *svsk = NULL;
NeilBrown6fb2b472006-10-02 02:17:50 -07001498 struct svc_serv *serv = rqstp->rq_server;
Greg Banks3262c812006-10-02 02:17:58 -07001499 struct svc_pool *pool = rqstp->rq_pool;
NeilBrown44524352006-10-04 02:15:46 -07001500 int len, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 int pages;
1502 struct xdr_buf *arg;
1503 DECLARE_WAITQUEUE(wait, current);
1504
1505 dprintk("svc: server %p waiting for data (to = %ld)\n",
1506 rqstp, timeout);
1507
1508 if (rqstp->rq_sock)
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001509 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 "svc_recv: service %p, socket not NULL!\n",
1511 rqstp);
1512 if (waitqueue_active(&rqstp->rq_wait))
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001513 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 "svc_recv: service %p, wait queue active!\n",
1515 rqstp);
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 /* now allocate needed pages. If we get a failure, sleep briefly */
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001519 pages = (serv->sv_max_mesg + PAGE_SIZE) / PAGE_SIZE;
NeilBrown44524352006-10-04 02:15:46 -07001520 for (i=0; i < pages ; i++)
1521 while (rqstp->rq_pages[i] == NULL) {
1522 struct page *p = alloc_page(GFP_KERNEL);
1523 if (!p)
1524 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
1525 rqstp->rq_pages[i] = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
NeilBrown250f3912007-01-26 00:56:59 -08001527 rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */
1528 BUG_ON(pages >= RPCSVC_MAXPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 /* Make arg->head point to first page and arg->pages point to rest */
1531 arg = &rqstp->rq_arg;
NeilBrown44524352006-10-04 02:15:46 -07001532 arg->head[0].iov_base = page_address(rqstp->rq_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 arg->head[0].iov_len = PAGE_SIZE;
NeilBrown44524352006-10-04 02:15:46 -07001534 arg->pages = rqstp->rq_pages + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 arg->page_base = 0;
1536 /* save at least one page for response */
1537 arg->page_len = (pages-2)*PAGE_SIZE;
1538 arg->len = (pages-1)*PAGE_SIZE;
1539 arg->tail[0].iov_len = 0;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001540
1541 try_to_freeze();
NeilBrown1887b932005-11-15 00:09:10 -08001542 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 if (signalled())
1544 return -EINTR;
1545
Greg Banks3262c812006-10-02 02:17:58 -07001546 spin_lock_bh(&pool->sp_lock);
1547 if ((svsk = svc_sock_dequeue(pool)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 rqstp->rq_sock = svsk;
Tom Tuckere1b31572007-12-30 21:07:46 -06001549 svc_xprt_get(&svsk->sk_xprt);
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001550 rqstp->rq_reserved = serv->sv_max_mesg;
Tom Tucker7a90e8c2007-12-30 21:07:55 -06001551 atomic_add(rqstp->rq_reserved, &svsk->sk_xprt.xpt_reserved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 } else {
1553 /* No data pending. Go to sleep */
Greg Banks3262c812006-10-02 02:17:58 -07001554 svc_thread_enqueue(pool, rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
1556 /*
1557 * We have to be able to interrupt this wait
1558 * to bring down the daemons ...
1559 */
1560 set_current_state(TASK_INTERRUPTIBLE);
1561 add_wait_queue(&rqstp->rq_wait, &wait);
Greg Banks3262c812006-10-02 02:17:58 -07001562 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 schedule_timeout(timeout);
1565
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001566 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Greg Banks3262c812006-10-02 02:17:58 -07001568 spin_lock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 remove_wait_queue(&rqstp->rq_wait, &wait);
1570
1571 if (!(svsk = rqstp->rq_sock)) {
Greg Banks3262c812006-10-02 02:17:58 -07001572 svc_thread_dequeue(pool, rqstp);
1573 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 dprintk("svc: server %p, no data yet\n", rqstp);
1575 return signalled()? -EINTR : -EAGAIN;
1576 }
1577 }
Greg Banks3262c812006-10-02 02:17:58 -07001578 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Tom Tuckerd7979ae2007-12-30 21:07:34 -06001580 len = 0;
Tom Tucker02fc6c32007-12-30 21:07:48 -06001581 if (test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags)) {
1582 dprintk("svc_recv: found XPT_CLOSE\n");
Tom Tucker7a182082007-12-30 21:07:53 -06001583 svc_delete_xprt(&svsk->sk_xprt);
Tom Tucker02fc6c32007-12-30 21:07:48 -06001584 } else if (test_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags)) {
Tom Tucker38a417c2007-12-30 21:07:36 -06001585 struct svc_xprt *newxpt;
1586 newxpt = svsk->sk_xprt.xpt_ops->xpo_accept(&svsk->sk_xprt);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001587 if (newxpt) {
1588 /*
1589 * We know this module_get will succeed because the
1590 * listener holds a reference too
1591 */
1592 __module_get(newxpt->xpt_class->xcl_owner);
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001593 svc_check_conn_limits(svsk->sk_xprt.xpt_server);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001594 }
Tom Tuckera6046f72007-12-30 21:08:01 -06001595 svc_xprt_received(&svsk->sk_xprt);
Tom Tuckerd7979ae2007-12-30 21:07:34 -06001596 } else {
1597 dprintk("svc: server %p, pool %u, socket %p, inuse=%d\n",
Tom Tuckere1b31572007-12-30 21:07:46 -06001598 rqstp, pool->sp_id, svsk,
1599 atomic_read(&svsk->sk_xprt.xpt_ref.refcount));
Tom Tuckerd7979ae2007-12-30 21:07:34 -06001600 len = svsk->sk_xprt.xpt_ops->xpo_recvfrom(rqstp);
1601 dprintk("svc: got len=%d\n", len);
1602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 /* No data, incomplete (TCP) read, or accept() */
1605 if (len == 0 || len == -EAGAIN) {
1606 rqstp->rq_res.len = 0;
1607 svc_sock_release(rqstp);
1608 return -EAGAIN;
1609 }
1610 svsk->sk_lastrecv = get_seconds();
Tom Tucker02fc6c32007-12-30 21:07:48 -06001611 clear_bit(XPT_OLD, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Chuck Leverbcdb81a2007-02-12 00:53:37 -08001613 rqstp->rq_secure = svc_port_is_privileged(svc_addr(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 rqstp->rq_chandle.defer = svc_defer;
1615
1616 if (serv->sv_stats)
1617 serv->sv_stats->netcnt++;
1618 return len;
1619}
1620
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001621/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 * Drop request
1623 */
1624void
1625svc_drop(struct svc_rqst *rqstp)
1626{
1627 dprintk("svc: socket %p dropped request\n", rqstp->rq_sock);
1628 svc_sock_release(rqstp);
1629}
1630
1631/*
1632 * Return reply to client.
1633 */
1634int
1635svc_send(struct svc_rqst *rqstp)
1636{
Tom Tuckera50fea22007-12-30 21:07:59 -06001637 struct svc_xprt *xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 int len;
1639 struct xdr_buf *xb;
1640
Tom Tuckera50fea22007-12-30 21:07:59 -06001641 xprt = rqstp->rq_xprt;
1642 if (!xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 /* release the receive skb before sending the reply */
Tom Tucker5148bf42007-12-30 21:07:25 -06001646 rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648 /* calculate over-all length */
1649 xb = & rqstp->rq_res;
1650 xb->len = xb->head[0].iov_len +
1651 xb->page_len +
1652 xb->tail[0].iov_len;
1653
Tom Tuckera50fea22007-12-30 21:07:59 -06001654 /* Grab mutex to serialize outgoing data. */
1655 mutex_lock(&xprt->xpt_mutex);
1656 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 len = -ENOTCONN;
1658 else
Tom Tuckera50fea22007-12-30 21:07:59 -06001659 len = xprt->xpt_ops->xpo_sendto(rqstp);
1660 mutex_unlock(&xprt->xpt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 svc_sock_release(rqstp);
1662
1663 if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
1664 return 0;
1665 return len;
1666}
1667
1668/*
Greg Banks36bdfc82006-10-02 02:17:54 -07001669 * Timer function to close old temporary sockets, using
1670 * a mark-and-sweep algorithm.
1671 */
1672static void
1673svc_age_temp_sockets(unsigned long closure)
1674{
1675 struct svc_serv *serv = (struct svc_serv *)closure;
1676 struct svc_sock *svsk;
1677 struct list_head *le, *next;
1678 LIST_HEAD(to_be_aged);
1679
1680 dprintk("svc_age_temp_sockets\n");
1681
1682 if (!spin_trylock_bh(&serv->sv_lock)) {
1683 /* busy, try again 1 sec later */
1684 dprintk("svc_age_temp_sockets: busy\n");
1685 mod_timer(&serv->sv_temptimer, jiffies + HZ);
1686 return;
1687 }
1688
1689 list_for_each_safe(le, next, &serv->sv_tempsocks) {
Tom Tucker7a182082007-12-30 21:07:53 -06001690 svsk = list_entry(le, struct svc_sock, sk_xprt.xpt_list);
Greg Banks36bdfc82006-10-02 02:17:54 -07001691
Tom Tucker02fc6c32007-12-30 21:07:48 -06001692 if (!test_and_set_bit(XPT_OLD, &svsk->sk_xprt.xpt_flags))
Greg Banks36bdfc82006-10-02 02:17:54 -07001693 continue;
Tom Tuckere1b31572007-12-30 21:07:46 -06001694 if (atomic_read(&svsk->sk_xprt.xpt_ref.refcount) > 1
Tom Tucker02fc6c32007-12-30 21:07:48 -06001695 || test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags))
Greg Banks36bdfc82006-10-02 02:17:54 -07001696 continue;
Tom Tuckere1b31572007-12-30 21:07:46 -06001697 svc_xprt_get(&svsk->sk_xprt);
Greg Banks36bdfc82006-10-02 02:17:54 -07001698 list_move(le, &to_be_aged);
Tom Tucker02fc6c32007-12-30 21:07:48 -06001699 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1700 set_bit(XPT_DETACHED, &svsk->sk_xprt.xpt_flags);
Greg Banks36bdfc82006-10-02 02:17:54 -07001701 }
1702 spin_unlock_bh(&serv->sv_lock);
1703
1704 while (!list_empty(&to_be_aged)) {
1705 le = to_be_aged.next;
Tom Tucker7a182082007-12-30 21:07:53 -06001706 /* fiddling the sk_xprt.xpt_list node is safe 'cos we're XPT_DETACHED */
Greg Banks36bdfc82006-10-02 02:17:54 -07001707 list_del_init(le);
Tom Tucker7a182082007-12-30 21:07:53 -06001708 svsk = list_entry(le, struct svc_sock, sk_xprt.xpt_list);
Greg Banks36bdfc82006-10-02 02:17:54 -07001709
1710 dprintk("queuing svsk %p for closing, %lu seconds old\n",
1711 svsk, get_seconds() - svsk->sk_lastrecv);
1712
1713 /* a thread will dequeue and close it soon */
Tom Tuckerf6150c32007-12-30 21:07:57 -06001714 svc_xprt_enqueue(&svsk->sk_xprt);
Tom Tuckere1b31572007-12-30 21:07:46 -06001715 svc_xprt_put(&svsk->sk_xprt);
Greg Banks36bdfc82006-10-02 02:17:54 -07001716 }
1717
1718 mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
1719}
1720
1721/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 * Initialize socket for RPC use and create svc_sock struct
1723 * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
1724 */
Chuck Lever6b174332007-02-12 00:53:28 -08001725static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1726 struct socket *sock,
1727 int *errp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
1729 struct svc_sock *svsk;
1730 struct sock *inet;
Chuck Lever6b174332007-02-12 00:53:28 -08001731 int pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
1732 int is_temporary = flags & SVC_SOCK_TEMPORARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 dprintk("svc: svc_setup_socket %p\n", sock);
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001735 if (!(svsk = kzalloc(sizeof(*svsk), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 *errp = -ENOMEM;
1737 return NULL;
1738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740 inet = sock->sk;
1741
1742 /* Register socket with portmapper */
1743 if (*errp >= 0 && pmap_register)
1744 *errp = svc_register(serv, inet->sk_protocol,
1745 ntohs(inet_sk(inet)->sport));
1746
1747 if (*errp < 0) {
1748 kfree(svsk);
1749 return NULL;
1750 }
1751
Tom Tucker02fc6c32007-12-30 21:07:48 -06001752 set_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 inet->sk_user_data = svsk;
1754 svsk->sk_sock = sock;
1755 svsk->sk_sk = inet;
1756 svsk->sk_ostate = inet->sk_state_change;
1757 svsk->sk_odata = inet->sk_data_ready;
1758 svsk->sk_owspace = inet->sk_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 svsk->sk_lastrecv = get_seconds();
NeilBrown7ac1bea2007-05-09 02:34:48 -07001760 spin_lock_init(&svsk->sk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 INIT_LIST_HEAD(&svsk->sk_deferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 /* Initialize the socket */
1764 if (sock->type == SOCK_DGRAM)
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001765 svc_udp_init(svsk, serv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 else
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001767 svc_tcp_init(svsk, serv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 spin_lock_bh(&serv->sv_lock);
Chuck Lever6b174332007-02-12 00:53:28 -08001770 if (is_temporary) {
Tom Tucker02fc6c32007-12-30 21:07:48 -06001771 set_bit(XPT_TEMP, &svsk->sk_xprt.xpt_flags);
Tom Tucker7a182082007-12-30 21:07:53 -06001772 list_add(&svsk->sk_xprt.xpt_list, &serv->sv_tempsocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 serv->sv_tmpcnt++;
Greg Banks36bdfc82006-10-02 02:17:54 -07001774 if (serv->sv_temptimer.function == NULL) {
1775 /* setup timer to age temp sockets */
1776 setup_timer(&serv->sv_temptimer, svc_age_temp_sockets,
1777 (unsigned long)serv);
1778 mod_timer(&serv->sv_temptimer,
1779 jiffies + svc_conn_age_period * HZ);
1780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 } else {
Tom Tucker02fc6c32007-12-30 21:07:48 -06001782 clear_bit(XPT_TEMP, &svsk->sk_xprt.xpt_flags);
Tom Tucker7a182082007-12-30 21:07:53 -06001783 list_add(&svsk->sk_xprt.xpt_list, &serv->sv_permsocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
1785 spin_unlock_bh(&serv->sv_lock);
1786
1787 dprintk("svc: svc_setup_socket created %p (inet %p)\n",
1788 svsk, svsk->sk_sk);
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return svsk;
1791}
1792
NeilBrownb41b66d2006-10-02 02:17:48 -07001793int svc_addsock(struct svc_serv *serv,
1794 int fd,
1795 char *name_return,
1796 int *proto)
1797{
1798 int err = 0;
1799 struct socket *so = sockfd_lookup(fd, &err);
1800 struct svc_sock *svsk = NULL;
1801
1802 if (!so)
1803 return err;
1804 if (so->sk->sk_family != AF_INET)
1805 err = -EAFNOSUPPORT;
1806 else if (so->sk->sk_protocol != IPPROTO_TCP &&
1807 so->sk->sk_protocol != IPPROTO_UDP)
1808 err = -EPROTONOSUPPORT;
1809 else if (so->state > SS_UNCONNECTED)
1810 err = -EISCONN;
1811 else {
Chuck Lever6b174332007-02-12 00:53:28 -08001812 svsk = svc_setup_socket(serv, so, &err, SVC_SOCK_DEFAULTS);
NeilBrowne79eff12007-02-12 00:53:30 -08001813 if (svsk) {
Tom Tuckera6046f72007-12-30 21:08:01 -06001814 svc_xprt_received(&svsk->sk_xprt);
NeilBrownb41b66d2006-10-02 02:17:48 -07001815 err = 0;
NeilBrowne79eff12007-02-12 00:53:30 -08001816 }
NeilBrownb41b66d2006-10-02 02:17:48 -07001817 }
1818 if (err) {
1819 sockfd_put(so);
1820 return err;
1821 }
1822 if (proto) *proto = so->sk->sk_protocol;
1823 return one_sock_name(name_return, svsk);
1824}
1825EXPORT_SYMBOL_GPL(svc_addsock);
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827/*
1828 * Create socket for RPC service.
1829 */
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001830static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
1831 int protocol,
1832 struct sockaddr *sin, int len,
1833 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
1835 struct svc_sock *svsk;
1836 struct socket *sock;
1837 int error;
1838 int type;
Chuck Leverad06e4b2007-02-12 00:53:32 -08001839 char buf[RPC_MAX_ADDRBUFLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Chuck Leverad06e4b2007-02-12 00:53:32 -08001841 dprintk("svc: svc_create_socket(%s, %d, %s)\n",
1842 serv->sv_program->pg_name, protocol,
Chuck Lever77f1f672007-02-12 00:53:39 -08001843 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1846 printk(KERN_WARNING "svc: only UDP and TCP "
1847 "sockets supported\n");
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001848 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 }
1850 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1851
Chuck Lever77f1f672007-02-12 00:53:39 -08001852 error = sock_create_kern(sin->sa_family, type, protocol, &sock);
1853 if (error < 0)
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001854 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Peter Zijlstraed075362006-12-06 20:35:24 -08001856 svc_reclassify_socket(sock);
1857
Eric Sesterhenn18114742006-09-28 14:37:07 -07001858 if (type == SOCK_STREAM)
Chuck Lever77f1f672007-02-12 00:53:39 -08001859 sock->sk->sk_reuse = 1; /* allow address reuse */
1860 error = kernel_bind(sock, sin, len);
Eric Sesterhenn18114742006-09-28 14:37:07 -07001861 if (error < 0)
1862 goto bummer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864 if (protocol == IPPROTO_TCP) {
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001865 if ((error = kernel_listen(sock, 64)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 goto bummer;
1867 }
1868
NeilBrowne79eff12007-02-12 00:53:30 -08001869 if ((svsk = svc_setup_socket(serv, sock, &error, flags)) != NULL) {
Tom Tuckera6046f72007-12-30 21:08:01 -06001870 svc_xprt_received(&svsk->sk_xprt);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001871 return (struct svc_xprt *)svsk;
NeilBrowne79eff12007-02-12 00:53:30 -08001872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874bummer:
1875 dprintk("svc: svc_create_socket error = %d\n", -error);
1876 sock_release(sock);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001877 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878}
1879
1880/*
Tom Tucker755ccea2007-12-30 21:07:27 -06001881 * Detach the svc_sock from the socket so that no
1882 * more callbacks occur.
1883 */
1884static void svc_sock_detach(struct svc_xprt *xprt)
1885{
1886 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1887 struct sock *sk = svsk->sk_sk;
1888
1889 dprintk("svc: svc_sock_detach(%p)\n", svsk);
1890
1891 /* put back the old socket callbacks */
1892 sk->sk_state_change = svsk->sk_ostate;
1893 sk->sk_data_ready = svsk->sk_odata;
1894 sk->sk_write_space = svsk->sk_owspace;
1895}
1896
1897/*
1898 * Free the svc_sock's socket resources and the svc_sock itself.
1899 */
1900static void svc_sock_free(struct svc_xprt *xprt)
1901{
1902 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1903 dprintk("svc: svc_sock_free(%p)\n", svsk);
1904
1905 if (svsk->sk_info_authunix != NULL)
1906 svcauth_unix_info_release(svsk->sk_info_authunix);
1907 if (svsk->sk_sock->file)
1908 sockfd_put(svsk->sk_sock);
1909 else
1910 sock_release(svsk->sk_sock);
1911 kfree(svsk);
1912}
1913
1914/*
Tom Tucker7a182082007-12-30 21:07:53 -06001915 * Remove a dead transport
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 */
Tom Tucker7a182082007-12-30 21:07:53 -06001917static void svc_delete_xprt(struct svc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
Tom Tucker7a182082007-12-30 21:07:53 -06001919 struct svc_serv *serv = xprt->xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Tom Tucker7a182082007-12-30 21:07:53 -06001921 dprintk("svc: svc_delete_xprt(%p)\n", xprt);
1922 xprt->xpt_ops->xpo_detach(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924 spin_lock_bh(&serv->sv_lock);
Tom Tucker7a182082007-12-30 21:07:53 -06001925 if (!test_and_set_bit(XPT_DETACHED, &xprt->xpt_flags))
1926 list_del_init(&xprt->xpt_list);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001927 /*
Tom Tucker7a182082007-12-30 21:07:53 -06001928 * We used to delete the transport from whichever list
1929 * it's sk_xprt.xpt_ready node was on, but we don't actually
Greg Banks3262c812006-10-02 02:17:58 -07001930 * need to. This is because the only time we're called
1931 * while still attached to a queue, the queue itself
1932 * is about to be destroyed (in svc_destroy).
1933 */
Tom Tucker7a182082007-12-30 21:07:53 -06001934 if (!test_and_set_bit(XPT_DEAD, &xprt->xpt_flags)) {
1935 BUG_ON(atomic_read(&xprt->xpt_ref.refcount) < 2);
1936 if (test_bit(XPT_TEMP, &xprt->xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 serv->sv_tmpcnt--;
Tom Tucker7a182082007-12-30 21:07:53 -06001938 svc_xprt_put(xprt);
NeilBrownaaf68cf2007-02-08 14:20:30 -08001939 }
Neil Brownd6740df2006-10-29 22:46:45 -08001940 spin_unlock_bh(&serv->sv_lock);
NeilBrownaaf68cf2007-02-08 14:20:30 -08001941}
1942
Tom Tucker7a182082007-12-30 21:07:53 -06001943static void svc_close_xprt(struct svc_xprt *xprt)
NeilBrownaaf68cf2007-02-08 14:20:30 -08001944{
Tom Tucker7a182082007-12-30 21:07:53 -06001945 set_bit(XPT_CLOSE, &xprt->xpt_flags);
1946 if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags))
NeilBrownaaf68cf2007-02-08 14:20:30 -08001947 /* someone else will have to effect the close */
1948 return;
1949
Tom Tucker7a182082007-12-30 21:07:53 -06001950 svc_xprt_get(xprt);
1951 svc_delete_xprt(xprt);
1952 clear_bit(XPT_BUSY, &xprt->xpt_flags);
1953 svc_xprt_put(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
1955
Tom Tucker7a182082007-12-30 21:07:53 -06001956void svc_close_all(struct list_head *xprt_list)
NeilBrowncda1fd42007-03-06 01:42:22 -08001957{
Tom Tucker7a182082007-12-30 21:07:53 -06001958 struct svc_xprt *xprt;
1959 struct svc_xprt *tmp;
1960
1961 list_for_each_entry_safe(xprt, tmp, xprt_list, xpt_list) {
1962 set_bit(XPT_CLOSE, &xprt->xpt_flags);
1963 if (test_bit(XPT_BUSY, &xprt->xpt_flags)) {
1964 /* Waiting to be processed, but no threads left,
1965 * So just remove it from the waiting list
1966 */
1967 list_del_init(&xprt->xpt_ready);
1968 clear_bit(XPT_BUSY, &xprt->xpt_flags);
1969 }
1970 svc_close_xprt(xprt);
NeilBrowncda1fd42007-03-06 01:42:22 -08001971 }
NeilBrowncda1fd42007-03-06 01:42:22 -08001972}
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974/*
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001975 * Handle defer and revisit of requests
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 */
1977
1978static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
1979{
1980 struct svc_deferred_req *dr = container_of(dreq, struct svc_deferred_req, handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 struct svc_sock *svsk;
1982
1983 if (too_many) {
Tom Tuckere1b31572007-12-30 21:07:46 -06001984 svc_xprt_put(&dr->svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 kfree(dr);
1986 return;
1987 }
1988 dprintk("revisit queued\n");
1989 svsk = dr->svsk;
1990 dr->svsk = NULL;
NeilBrown7ac1bea2007-05-09 02:34:48 -07001991 spin_lock(&svsk->sk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 list_add(&dr->handle.recent, &svsk->sk_deferred);
NeilBrown7ac1bea2007-05-09 02:34:48 -07001993 spin_unlock(&svsk->sk_lock);
Tom Tucker02fc6c32007-12-30 21:07:48 -06001994 set_bit(XPT_DEFERRED, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -06001995 svc_xprt_enqueue(&svsk->sk_xprt);
Tom Tuckere1b31572007-12-30 21:07:46 -06001996 svc_xprt_put(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
1999static struct cache_deferred_req *
2000svc_defer(struct cache_req *req)
2001{
2002 struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
2003 int size = sizeof(struct svc_deferred_req) + (rqstp->rq_arg.len);
2004 struct svc_deferred_req *dr;
2005
2006 if (rqstp->rq_arg.page_len)
2007 return NULL; /* if more than a page, give up FIXME */
2008 if (rqstp->rq_deferred) {
2009 dr = rqstp->rq_deferred;
2010 rqstp->rq_deferred = NULL;
2011 } else {
2012 int skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
2013 /* FIXME maybe discard if size too large */
2014 dr = kmalloc(size, GFP_KERNEL);
2015 if (dr == NULL)
2016 return NULL;
2017
2018 dr->handle.owner = rqstp->rq_server;
2019 dr->prot = rqstp->rq_prot;
Chuck Lever24422222007-02-12 00:53:33 -08002020 memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen);
2021 dr->addrlen = rqstp->rq_addrlen;
J. Bruce Fields1918e342006-01-18 17:43:16 -08002022 dr->daddr = rqstp->rq_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 dr->argslen = rqstp->rq_arg.len >> 2;
2024 memcpy(dr->args, rqstp->rq_arg.head[0].iov_base-skip, dr->argslen<<2);
2025 }
Tom Tuckere1b31572007-12-30 21:07:46 -06002026 svc_xprt_get(rqstp->rq_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 dr->svsk = rqstp->rq_sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029 dr->handle.revisit = svc_revisit;
2030 return &dr->handle;
2031}
2032
2033/*
2034 * recv data from a deferred request into an active one
2035 */
2036static int svc_deferred_recv(struct svc_rqst *rqstp)
2037{
2038 struct svc_deferred_req *dr = rqstp->rq_deferred;
2039
2040 rqstp->rq_arg.head[0].iov_base = dr->args;
2041 rqstp->rq_arg.head[0].iov_len = dr->argslen<<2;
2042 rqstp->rq_arg.page_len = 0;
2043 rqstp->rq_arg.len = dr->argslen<<2;
2044 rqstp->rq_prot = dr->prot;
Chuck Lever24422222007-02-12 00:53:33 -08002045 memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen);
2046 rqstp->rq_addrlen = dr->addrlen;
J. Bruce Fields1918e342006-01-18 17:43:16 -08002047 rqstp->rq_daddr = dr->daddr;
NeilBrown44524352006-10-04 02:15:46 -07002048 rqstp->rq_respages = rqstp->rq_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 return dr->argslen<<2;
2050}
2051
2052
2053static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk)
2054{
2055 struct svc_deferred_req *dr = NULL;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08002056
Tom Tucker02fc6c32007-12-30 21:07:48 -06002057 if (!test_bit(XPT_DEFERRED, &svsk->sk_xprt.xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 return NULL;
NeilBrown7ac1bea2007-05-09 02:34:48 -07002059 spin_lock(&svsk->sk_lock);
Tom Tucker02fc6c32007-12-30 21:07:48 -06002060 clear_bit(XPT_DEFERRED, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 if (!list_empty(&svsk->sk_deferred)) {
2062 dr = list_entry(svsk->sk_deferred.next,
2063 struct svc_deferred_req,
2064 handle.recent);
2065 list_del_init(&dr->handle.recent);
Tom Tucker02fc6c32007-12-30 21:07:48 -06002066 set_bit(XPT_DEFERRED, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
NeilBrown7ac1bea2007-05-09 02:34:48 -07002068 spin_unlock(&svsk->sk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 return dr;
2070}