blob: fa57e9bc68e400571150ea44fe45fde0bd366423 [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
Tom Tucker8c7b0172007-12-30 21:08:10 -060092static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static 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",
Tom Tuckerdef13d72007-12-30 21:08:08 -0600116 &svc_slock_key[0],
117 "sk_xprt.xpt_lock-AF_INET-NFSD",
118 &svc_key[0]);
Peter Zijlstraed075362006-12-06 20:35:24 -0800119 break;
120
121 case AF_INET6:
122 sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
Tom Tuckerdef13d72007-12-30 21:08:08 -0600123 &svc_slock_key[1],
124 "sk_xprt.xpt_lock-AF_INET6-NFSD",
125 &svc_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -0800126 break;
127
128 default:
129 BUG();
130 }
131}
132#else
133static inline void svc_reclassify_socket(struct socket *sock)
134{
135}
136#endif
137
Chuck Leverad06e4b2007-02-12 00:53:32 -0800138static char *__svc_print_addr(struct sockaddr *addr, char *buf, size_t len)
139{
140 switch (addr->sa_family) {
141 case AF_INET:
142 snprintf(buf, len, "%u.%u.%u.%u, port=%u",
143 NIPQUAD(((struct sockaddr_in *) addr)->sin_addr),
Al Viro582ee432007-07-26 17:33:39 +0100144 ntohs(((struct sockaddr_in *) addr)->sin_port));
Chuck Leverad06e4b2007-02-12 00:53:32 -0800145 break;
NeilBrown5a05ed72007-03-06 01:42:22 -0800146
Chuck Leverad06e4b2007-02-12 00:53:32 -0800147 case AF_INET6:
148 snprintf(buf, len, "%x:%x:%x:%x:%x:%x:%x:%x, port=%u",
149 NIP6(((struct sockaddr_in6 *) addr)->sin6_addr),
Al Viro582ee432007-07-26 17:33:39 +0100150 ntohs(((struct sockaddr_in6 *) addr)->sin6_port));
Chuck Leverad06e4b2007-02-12 00:53:32 -0800151 break;
NeilBrown5a05ed72007-03-06 01:42:22 -0800152
Chuck Leverad06e4b2007-02-12 00:53:32 -0800153 default:
154 snprintf(buf, len, "unknown address type: %d", addr->sa_family);
155 break;
156 }
157 return buf;
158}
159
160/**
161 * svc_print_addr - Format rq_addr field for printing
162 * @rqstp: svc_rqst struct containing address to print
163 * @buf: target buffer for formatted address
164 * @len: length of target buffer
165 *
166 */
167char *svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len)
168{
Chuck Lever27459f02007-02-12 00:53:34 -0800169 return __svc_print_addr(svc_addr(rqstp), buf, len);
Chuck Leverad06e4b2007-02-12 00:53:32 -0800170}
171EXPORT_SYMBOL_GPL(svc_print_addr);
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/*
Greg Banks3262c812006-10-02 02:17:58 -0700174 * Queue up an idle server thread. Must have pool->sp_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 * Note: this is really a stack rather than a queue, so that we only
Greg Banks3262c812006-10-02 02:17:58 -0700176 * use as many different threads as we need, and the rest don't pollute
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 * the cache.
178 */
179static inline void
Greg Banks3262c812006-10-02 02:17:58 -0700180svc_thread_enqueue(struct svc_pool *pool, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Greg Banks3262c812006-10-02 02:17:58 -0700182 list_add(&rqstp->rq_list, &pool->sp_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
185/*
Greg Banks3262c812006-10-02 02:17:58 -0700186 * Dequeue an nfsd thread. Must have pool->sp_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 */
188static inline void
Greg Banks3262c812006-10-02 02:17:58 -0700189svc_thread_dequeue(struct svc_pool *pool, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 list_del(&rqstp->rq_list);
192}
193
194/*
195 * Release an skbuff after use
196 */
Tom Tucker5148bf42007-12-30 21:07:25 -0600197static void svc_release_skb(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Tom Tucker5148bf42007-12-30 21:07:25 -0600199 struct sk_buff *skb = rqstp->rq_xprt_ctxt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 struct svc_deferred_req *dr = rqstp->rq_deferred;
201
202 if (skb) {
Tom Tucker5148bf42007-12-30 21:07:25 -0600203 rqstp->rq_xprt_ctxt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
206 skb_free_datagram(rqstp->rq_sock->sk_sk, skb);
207 }
208 if (dr) {
209 rqstp->rq_deferred = NULL;
210 kfree(dr);
211 }
212}
213
214/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * Queue up a socket with data pending. If there are idle nfsd
216 * processes, wake 'em up.
217 *
218 */
Tom Tuckerf6150c32007-12-30 21:07:57 -0600219void svc_xprt_enqueue(struct svc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Tom Tuckerf6150c32007-12-30 21:07:57 -0600221 struct svc_serv *serv = xprt->xpt_server;
Greg Banksbfd24162006-10-02 02:18:01 -0700222 struct svc_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 struct svc_rqst *rqstp;
Greg Banksbfd24162006-10-02 02:18:01 -0700224 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Tom Tuckerf6150c32007-12-30 21:07:57 -0600226 if (!(xprt->xpt_flags &
Tom Tucker02fc6c32007-12-30 21:07:48 -0600227 ((1<<XPT_CONN)|(1<<XPT_DATA)|(1<<XPT_CLOSE)|(1<<XPT_DEFERRED))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return;
Tom Tuckerf6150c32007-12-30 21:07:57 -0600229 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return;
231
Greg Banksbfd24162006-10-02 02:18:01 -0700232 cpu = get_cpu();
Tom Tuckerf6150c32007-12-30 21:07:57 -0600233 pool = svc_pool_for_cpu(xprt->xpt_server, cpu);
Greg Banksbfd24162006-10-02 02:18:01 -0700234 put_cpu();
235
Greg Banks3262c812006-10-02 02:17:58 -0700236 spin_lock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Greg Banks3262c812006-10-02 02:17:58 -0700238 if (!list_empty(&pool->sp_threads) &&
239 !list_empty(&pool->sp_sockets))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 printk(KERN_ERR
Tom Tuckerf6150c32007-12-30 21:07:57 -0600241 "svc_xprt_enqueue: "
242 "threads and transports both waiting??\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Tom Tuckerf6150c32007-12-30 21:07:57 -0600244 if (test_bit(XPT_DEAD, &xprt->xpt_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 /* Don't enqueue dead sockets */
Tom Tuckerf6150c32007-12-30 21:07:57 -0600246 dprintk("svc: transport %p is dead, not enqueued\n", xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 goto out_unlock;
248 }
249
Greg Banksc081a0c2006-10-02 02:17:57 -0700250 /* Mark socket as busy. It will remain in this state until the
251 * server has processed all pending data and put the socket back
Tom Tucker02fc6c32007-12-30 21:07:48 -0600252 * on the idle list. We update XPT_BUSY atomically because
Greg Banksc081a0c2006-10-02 02:17:57 -0700253 * it also guards against trying to enqueue the svc_sock twice.
254 */
Tom Tuckerf6150c32007-12-30 21:07:57 -0600255 if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags)) {
Greg Banksc081a0c2006-10-02 02:17:57 -0700256 /* Don't enqueue socket while already enqueued */
Tom Tuckerf6150c32007-12-30 21:07:57 -0600257 dprintk("svc: transport %p busy, not enqueued\n", xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 goto out_unlock;
259 }
Tom Tuckerf6150c32007-12-30 21:07:57 -0600260 BUG_ON(xprt->xpt_pool != NULL);
261 xprt->xpt_pool = pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Tom Tucker323bee32007-12-30 21:07:31 -0600263 /* Handle pending connection */
Tom Tuckerf6150c32007-12-30 21:07:57 -0600264 if (test_bit(XPT_CONN, &xprt->xpt_flags))
Tom Tucker323bee32007-12-30 21:07:31 -0600265 goto process;
266
267 /* Handle close in-progress */
Tom Tuckerf6150c32007-12-30 21:07:57 -0600268 if (test_bit(XPT_CLOSE, &xprt->xpt_flags))
Tom Tucker323bee32007-12-30 21:07:31 -0600269 goto process;
270
271 /* Check if we have space to reply to a request */
Tom Tuckerf6150c32007-12-30 21:07:57 -0600272 if (!xprt->xpt_ops->xpo_has_wspace(xprt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* Don't enqueue while not enough space for reply */
Tom Tuckerf6150c32007-12-30 21:07:57 -0600274 dprintk("svc: no write space, transport %p not enqueued\n",
275 xprt);
276 xprt->xpt_pool = NULL;
277 clear_bit(XPT_BUSY, &xprt->xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 goto out_unlock;
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Tom Tucker323bee32007-12-30 21:07:31 -0600281 process:
Greg Banks3262c812006-10-02 02:17:58 -0700282 if (!list_empty(&pool->sp_threads)) {
283 rqstp = list_entry(pool->sp_threads.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 struct svc_rqst,
285 rq_list);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600286 dprintk("svc: transport %p served by daemon %p\n",
287 xprt, rqstp);
Greg Banks3262c812006-10-02 02:17:58 -0700288 svc_thread_dequeue(pool, rqstp);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600289 if (rqstp->rq_xprt)
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800290 printk(KERN_ERR
Tom Tuckerf6150c32007-12-30 21:07:57 -0600291 "svc_xprt_enqueue: server %p, rq_xprt=%p!\n",
292 rqstp, rqstp->rq_xprt);
293 rqstp->rq_xprt = xprt;
294 svc_xprt_get(xprt);
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700295 rqstp->rq_reserved = serv->sv_max_mesg;
Tom Tuckerf6150c32007-12-30 21:07:57 -0600296 atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved);
297 BUG_ON(xprt->xpt_pool != pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 wake_up(&rqstp->rq_wait);
299 } else {
Tom Tuckerf6150c32007-12-30 21:07:57 -0600300 dprintk("svc: transport %p put into queue\n", xprt);
301 list_add_tail(&xprt->xpt_ready, &pool->sp_sockets);
302 BUG_ON(xprt->xpt_pool != pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304
305out_unlock:
Greg Banks3262c812006-10-02 02:17:58 -0700306 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
Tom Tuckerf6150c32007-12-30 21:07:57 -0600308EXPORT_SYMBOL_GPL(svc_xprt_enqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310/*
Greg Banks3262c812006-10-02 02:17:58 -0700311 * Dequeue the first socket. Must be called with the pool->sp_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 */
Tom Tuckerc36adb2a2007-12-30 21:08:16 -0600313static struct svc_xprt *svc_xprt_dequeue(struct svc_pool *pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Tom Tuckerc36adb2a2007-12-30 21:08:16 -0600315 struct svc_xprt *xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Greg Banks3262c812006-10-02 02:17:58 -0700317 if (list_empty(&pool->sp_sockets))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return NULL;
319
Tom Tuckerc36adb2a2007-12-30 21:08:16 -0600320 xprt = list_entry(pool->sp_sockets.next,
321 struct svc_xprt, xpt_ready);
322 list_del_init(&xprt->xpt_ready);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Tom Tuckerc36adb2a2007-12-30 21:08:16 -0600324 dprintk("svc: transport %p dequeued, inuse=%d\n",
325 xprt, atomic_read(&xprt->xpt_ref.refcount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Tom Tuckerc36adb2a2007-12-30 21:08:16 -0600327 return xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
330/*
Tom Tuckera6046f72007-12-30 21:08:01 -0600331 * svc_xprt_received conditionally queues the transport for processing
332 * by another thread. The caller must hold the XPT_BUSY bit and must
333 * not thereafter touch transport data.
334 *
335 * Note: XPT_DATA only gets cleared when a read-attempt finds no (or
336 * insufficient) data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 */
Tom Tuckera6046f72007-12-30 21:08:01 -0600338void svc_xprt_received(struct svc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Tom Tuckera6046f72007-12-30 21:08:01 -0600340 BUG_ON(!test_bit(XPT_BUSY, &xprt->xpt_flags));
341 xprt->xpt_pool = NULL;
342 clear_bit(XPT_BUSY, &xprt->xpt_flags);
343 svc_xprt_enqueue(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
Tom Tuckera6046f72007-12-30 21:08:01 -0600345EXPORT_SYMBOL_GPL(svc_xprt_received);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347/**
348 * svc_reserve - change the space reserved for the reply to a request.
349 * @rqstp: The request in question
350 * @space: new max space to reserve
351 *
352 * Each request reserves some space on the output queue of the socket
353 * to make sure the reply fits. This function reduces that reserved
354 * space to be the amount of space used already, plus @space.
355 *
356 */
357void svc_reserve(struct svc_rqst *rqstp, int space)
358{
359 space += rqstp->rq_res.head[0].iov_len;
360
361 if (space < rqstp->rq_reserved) {
Tom Tuckerf6150c32007-12-30 21:07:57 -0600362 struct svc_xprt *xprt = rqstp->rq_xprt;
363 atomic_sub((rqstp->rq_reserved - space), &xprt->xpt_reserved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 rqstp->rq_reserved = space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Tom Tuckerf6150c32007-12-30 21:07:57 -0600366 svc_xprt_enqueue(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368}
369
Tom Tuckereab996d2007-12-30 21:08:14 -0600370static void svc_xprt_release(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Tom Tuckereab996d2007-12-30 21:08:14 -0600372 struct svc_xprt *xprt = rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Tom Tucker5148bf42007-12-30 21:07:25 -0600374 rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
NeilBrown44524352006-10-04 02:15:46 -0700376 svc_free_res_pages(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 rqstp->rq_res.page_len = 0;
378 rqstp->rq_res.page_base = 0;
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /* Reset response buffer and release
381 * the reservation.
382 * But first, check that enough space was reserved
383 * for the reply, otherwise we have a bug!
384 */
385 if ((rqstp->rq_res.len) > rqstp->rq_reserved)
386 printk(KERN_ERR "RPC request reserved %d but used %d\n",
387 rqstp->rq_reserved,
388 rqstp->rq_res.len);
389
390 rqstp->rq_res.head[0].iov_len = 0;
391 svc_reserve(rqstp, 0);
Tom Tuckereab996d2007-12-30 21:08:14 -0600392 rqstp->rq_xprt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Tom Tuckereab996d2007-12-30 21:08:14 -0600394 svc_xprt_put(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
397/*
398 * External function to wake up a server waiting for data
Greg Banks3262c812006-10-02 02:17:58 -0700399 * This really only makes sense for services like lockd
400 * which have exactly one thread anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 */
402void
403svc_wake_up(struct svc_serv *serv)
404{
405 struct svc_rqst *rqstp;
Greg Banks3262c812006-10-02 02:17:58 -0700406 unsigned int i;
407 struct svc_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Greg Banks3262c812006-10-02 02:17:58 -0700409 for (i = 0; i < serv->sv_nrpools; i++) {
410 pool = &serv->sv_pools[i];
411
412 spin_lock_bh(&pool->sp_lock);
413 if (!list_empty(&pool->sp_threads)) {
414 rqstp = list_entry(pool->sp_threads.next,
415 struct svc_rqst,
416 rq_list);
417 dprintk("svc: daemon %p woken up.\n", rqstp);
418 /*
419 svc_thread_dequeue(pool, rqstp);
420 rqstp->rq_sock = NULL;
421 */
422 wake_up(&rqstp->rq_wait);
423 }
424 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
Chuck Leverb92503b2007-02-12 00:53:36 -0800428union svc_pktinfo_u {
429 struct in_pktinfo pkti;
Chuck Leverb92503b2007-02-12 00:53:36 -0800430 struct in6_pktinfo pkti6;
Chuck Leverb92503b2007-02-12 00:53:36 -0800431};
David S. Millerbc375ea2007-04-12 13:35:59 -0700432#define SVC_PKTINFO_SPACE \
433 CMSG_SPACE(sizeof(union svc_pktinfo_u))
Chuck Leverb92503b2007-02-12 00:53:36 -0800434
435static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
436{
437 switch (rqstp->rq_sock->sk_sk->sk_family) {
438 case AF_INET: {
439 struct in_pktinfo *pki = CMSG_DATA(cmh);
440
441 cmh->cmsg_level = SOL_IP;
442 cmh->cmsg_type = IP_PKTINFO;
443 pki->ipi_ifindex = 0;
444 pki->ipi_spec_dst.s_addr = rqstp->rq_daddr.addr.s_addr;
445 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
446 }
447 break;
NeilBrown5a05ed72007-03-06 01:42:22 -0800448
Chuck Leverb92503b2007-02-12 00:53:36 -0800449 case AF_INET6: {
450 struct in6_pktinfo *pki = CMSG_DATA(cmh);
451
452 cmh->cmsg_level = SOL_IPV6;
453 cmh->cmsg_type = IPV6_PKTINFO;
454 pki->ipi6_ifindex = 0;
455 ipv6_addr_copy(&pki->ipi6_addr,
456 &rqstp->rq_daddr.addr6);
457 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
458 }
459 break;
Chuck Leverb92503b2007-02-12 00:53:36 -0800460 }
461 return;
462}
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464/*
465 * Generic sendto routine
466 */
467static int
468svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
469{
470 struct svc_sock *svsk = rqstp->rq_sock;
471 struct socket *sock = svsk->sk_sock;
472 int slen;
David S. Millerbc375ea2007-04-12 13:35:59 -0700473 union {
474 struct cmsghdr hdr;
475 long all[SVC_PKTINFO_SPACE / sizeof(long)];
476 } buffer;
477 struct cmsghdr *cmh = &buffer.hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 int len = 0;
479 int result;
480 int size;
481 struct page **ppage = xdr->pages;
482 size_t base = xdr->page_base;
483 unsigned int pglen = xdr->page_len;
484 unsigned int flags = MSG_MORE;
Chuck Leverad06e4b2007-02-12 00:53:32 -0800485 char buf[RPC_MAX_ADDRBUFLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 slen = xdr->len;
488
489 if (rqstp->rq_prot == IPPROTO_UDP) {
Chuck Leverb92503b2007-02-12 00:53:36 -0800490 struct msghdr msg = {
491 .msg_name = &rqstp->rq_addr,
492 .msg_namelen = rqstp->rq_addrlen,
493 .msg_control = cmh,
494 .msg_controllen = sizeof(buffer),
495 .msg_flags = MSG_MORE,
496 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Chuck Leverb92503b2007-02-12 00:53:36 -0800498 svc_set_cmsg_data(rqstp, cmh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 if (sock_sendmsg(sock, &msg, 0) < 0)
501 goto out;
502 }
503
504 /* send head */
505 if (slen == xdr->head[0].iov_len)
506 flags = 0;
NeilBrown44524352006-10-04 02:15:46 -0700507 len = kernel_sendpage(sock, rqstp->rq_respages[0], 0,
508 xdr->head[0].iov_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (len != xdr->head[0].iov_len)
510 goto out;
511 slen -= xdr->head[0].iov_len;
512 if (slen == 0)
513 goto out;
514
515 /* send page data */
516 size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
517 while (pglen > 0) {
518 if (slen == size)
519 flags = 0;
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700520 result = kernel_sendpage(sock, *ppage, base, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (result > 0)
522 len += result;
523 if (result != size)
524 goto out;
525 slen -= size;
526 pglen -= size;
527 size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
528 base = 0;
529 ppage++;
530 }
531 /* send tail */
532 if (xdr->tail[0].iov_len) {
NeilBrown44524352006-10-04 02:15:46 -0700533 result = kernel_sendpage(sock, rqstp->rq_respages[0],
534 ((unsigned long)xdr->tail[0].iov_base)
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800535 & (PAGE_SIZE-1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 xdr->tail[0].iov_len, 0);
537
538 if (result > 0)
539 len += result;
540 }
541out:
Chuck Leverad06e4b2007-02-12 00:53:32 -0800542 dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %s)\n",
543 rqstp->rq_sock, xdr->head[0].iov_base, xdr->head[0].iov_len,
544 xdr->len, len, svc_print_addr(rqstp, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 return len;
547}
548
549/*
NeilBrown80212d52006-10-02 02:17:47 -0700550 * Report socket names for nfsdfs
551 */
552static int one_sock_name(char *buf, struct svc_sock *svsk)
553{
554 int len;
555
556 switch(svsk->sk_sk->sk_family) {
557 case AF_INET:
558 len = sprintf(buf, "ipv4 %s %u.%u.%u.%u %d\n",
559 svsk->sk_sk->sk_protocol==IPPROTO_UDP?
560 "udp" : "tcp",
561 NIPQUAD(inet_sk(svsk->sk_sk)->rcv_saddr),
562 inet_sk(svsk->sk_sk)->num);
563 break;
564 default:
565 len = sprintf(buf, "*unknown-%d*\n",
566 svsk->sk_sk->sk_family);
567 }
568 return len;
569}
570
571int
NeilBrownb41b66d2006-10-02 02:17:48 -0700572svc_sock_names(char *buf, struct svc_serv *serv, char *toclose)
NeilBrown80212d52006-10-02 02:17:47 -0700573{
NeilBrownb41b66d2006-10-02 02:17:48 -0700574 struct svc_sock *svsk, *closesk = NULL;
NeilBrown80212d52006-10-02 02:17:47 -0700575 int len = 0;
576
577 if (!serv)
578 return 0;
NeilBrownaaf68cf2007-02-08 14:20:30 -0800579 spin_lock_bh(&serv->sv_lock);
Tom Tucker7a182082007-12-30 21:07:53 -0600580 list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list) {
NeilBrown80212d52006-10-02 02:17:47 -0700581 int onelen = one_sock_name(buf+len, svsk);
NeilBrownb41b66d2006-10-02 02:17:48 -0700582 if (toclose && strcmp(toclose, buf+len) == 0)
583 closesk = svsk;
584 else
585 len += onelen;
NeilBrown80212d52006-10-02 02:17:47 -0700586 }
NeilBrownaaf68cf2007-02-08 14:20:30 -0800587 spin_unlock_bh(&serv->sv_lock);
NeilBrownb41b66d2006-10-02 02:17:48 -0700588 if (closesk)
NeilBrown5680c442006-10-04 02:15:45 -0700589 /* Should unregister with portmap, but you cannot
590 * unregister just one protocol...
591 */
Tom Tucker7a182082007-12-30 21:07:53 -0600592 svc_close_xprt(&closesk->sk_xprt);
NeilBrown37a03472006-10-04 02:15:44 -0700593 else if (toclose)
594 return -ENOENT;
NeilBrown80212d52006-10-02 02:17:47 -0700595 return len;
596}
597EXPORT_SYMBOL(svc_sock_names);
598
599/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 * Check input queue length
601 */
602static int
603svc_recv_available(struct svc_sock *svsk)
604{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 struct socket *sock = svsk->sk_sock;
606 int avail, err;
607
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700608 err = kernel_sock_ioctl(sock, TIOCINQ, (unsigned long) &avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 return (err >= 0)? avail : err;
611}
612
613/*
614 * Generic recvfrom routine.
615 */
616static int
617svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr, int buflen)
618{
Chuck Lever067d7812007-02-12 00:53:30 -0800619 struct svc_sock *svsk = rqstp->rq_sock;
Chuck Lever1ba95102007-02-12 00:53:31 -0800620 struct msghdr msg = {
621 .msg_flags = MSG_DONTWAIT,
622 };
623 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
Chuck Lever1ba95102007-02-12 00:53:31 -0800629 svsk, iov[0].iov_base, iov[0].iov_len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return len;
631}
632
633/*
634 * Set socket snd and rcv buffer lengths
635 */
636static inline void
637svc_sock_setbufsize(struct socket *sock, unsigned int snd, unsigned int rcv)
638{
639#if 0
640 mm_segment_t oldfs;
641 oldfs = get_fs(); set_fs(KERNEL_DS);
642 sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
643 (char*)&snd, sizeof(snd));
644 sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
645 (char*)&rcv, sizeof(rcv));
646#else
647 /* sock_setsockopt limits use to sysctl_?mem_max,
648 * which isn't acceptable. Until that is made conditional
649 * on not having CAP_SYS_RESOURCE or similar, we go direct...
650 * DaveM said I could!
651 */
652 lock_sock(sock->sk);
653 sock->sk->sk_sndbuf = snd * 2;
654 sock->sk->sk_rcvbuf = rcv * 2;
655 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK|SOCK_RCVBUF_LOCK;
656 release_sock(sock->sk);
657#endif
658}
659/*
660 * INET callback when data has been received on the socket.
661 */
662static void
663svc_udp_data_ready(struct sock *sk, int count)
664{
Neil Brown939bb7e2005-09-13 01:25:39 -0700665 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Neil Brown939bb7e2005-09-13 01:25:39 -0700667 if (svsk) {
668 dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
Tom Tucker02fc6c32007-12-30 21:07:48 -0600669 svsk, sk, count,
670 test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
671 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600672 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -0700673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
675 wake_up_interruptible(sk->sk_sleep);
676}
677
678/*
679 * INET callback when space is newly available on the socket.
680 */
681static void
682svc_write_space(struct sock *sk)
683{
684 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
685
686 if (svsk) {
687 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
Tom Tucker02fc6c32007-12-30 21:07:48 -0600688 svsk, sk, test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
Tom Tuckerf6150c32007-12-30 21:07:57 -0600689 svc_xprt_enqueue(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691
692 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) {
Neil Brown939bb7e2005-09-13 01:25:39 -0700693 dprintk("RPC svc_write_space: someone sleeping on %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 svsk);
695 wake_up_interruptible(sk->sk_sleep);
696 }
697}
698
Tom Tucker9dbc2402007-12-30 21:08:12 -0600699/*
700 * Copy the UDP datagram's destination address to the rqstp structure.
701 * The 'destination' address in this case is the address to which the
702 * peer sent the datagram, i.e. our local address. For multihomed
703 * hosts, this can change from msg to msg. Note that only the IP
704 * address changes, the port number should remain the same.
705 */
706static void svc_udp_get_dest_address(struct svc_rqst *rqstp,
707 struct cmsghdr *cmh)
Chuck Lever95756482007-02-12 00:53:38 -0800708{
709 switch (rqstp->rq_sock->sk_sk->sk_family) {
710 case AF_INET: {
NeilBrown7a37f572007-03-06 01:42:21 -0800711 struct in_pktinfo *pki = CMSG_DATA(cmh);
712 rqstp->rq_daddr.addr.s_addr = pki->ipi_spec_dst.s_addr;
Chuck Lever95756482007-02-12 00:53:38 -0800713 break;
NeilBrown7a37f572007-03-06 01:42:21 -0800714 }
Chuck Lever95756482007-02-12 00:53:38 -0800715 case AF_INET6: {
NeilBrown7a37f572007-03-06 01:42:21 -0800716 struct in6_pktinfo *pki = CMSG_DATA(cmh);
717 ipv6_addr_copy(&rqstp->rq_daddr.addr6, &pki->ipi6_addr);
Chuck Lever95756482007-02-12 00:53:38 -0800718 break;
NeilBrown7a37f572007-03-06 01:42:21 -0800719 }
Chuck Lever95756482007-02-12 00:53:38 -0800720 }
Chuck Lever95756482007-02-12 00:53:38 -0800721}
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723/*
724 * Receive a datagram from a UDP socket.
725 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726static int
727svc_udp_recvfrom(struct svc_rqst *rqstp)
728{
729 struct svc_sock *svsk = rqstp->rq_sock;
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600730 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 struct sk_buff *skb;
David S. Millerbc375ea2007-04-12 13:35:59 -0700732 union {
733 struct cmsghdr hdr;
734 long all[SVC_PKTINFO_SPACE / sizeof(long)];
735 } buffer;
736 struct cmsghdr *cmh = &buffer.hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 int err, len;
NeilBrown7a37f572007-03-06 01:42:21 -0800738 struct msghdr msg = {
739 .msg_name = svc_addr(rqstp),
740 .msg_control = cmh,
741 .msg_controllen = sizeof(buffer),
742 .msg_flags = MSG_DONTWAIT,
743 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Tom Tucker02fc6c32007-12-30 21:07:48 -0600745 if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 /* udp sockets need large rcvbuf as all pending
747 * requests are still in that buffer. sndbuf must
748 * also be large enough that there is enough space
Greg Banks3262c812006-10-02 02:17:58 -0700749 * for one reply per thread. We count all threads
750 * rather than threads in a particular pool, which
751 * provides an upper bound on the number of threads
752 * which will access the socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 */
754 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700755 (serv->sv_nrthreads+3) * serv->sv_max_mesg,
756 (serv->sv_nrthreads+3) * serv->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Tom Tucker02fc6c32007-12-30 21:07:48 -0600758 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
NeilBrown05ed6902007-05-09 02:34:55 -0700759 skb = NULL;
760 err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
761 0, 0, MSG_PEEK | MSG_DONTWAIT);
762 if (err >= 0)
763 skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err);
764
765 if (skb == NULL) {
766 if (err != -EAGAIN) {
767 /* possibly an icmp error */
768 dprintk("svc: recvfrom returned error %d\n", -err);
Tom Tucker02fc6c32007-12-30 21:07:48 -0600769 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
Tom Tuckera6046f72007-12-30 21:08:01 -0600771 svc_xprt_received(&svsk->sk_xprt);
NeilBrown05ed6902007-05-09 02:34:55 -0700772 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
Tom Tucker9dbc2402007-12-30 21:08:12 -0600774 len = svc_addr_len(svc_addr(rqstp));
775 if (len < 0)
776 return len;
777 rqstp->rq_addrlen = len;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700778 if (skb->tstamp.tv64 == 0) {
779 skb->tstamp = ktime_get_real();
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800780 /* Don't enable netstamp, sunrpc doesn't
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 need that much accuracy */
782 }
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700783 svsk->sk_sk->sk_stamp = skb->tstamp;
Tom Tucker02fc6c32007-12-30 21:07:48 -0600784 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 /*
787 * Maybe more packets - kick another thread ASAP.
788 */
Tom Tuckera6046f72007-12-30 21:08:01 -0600789 svc_xprt_received(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 len = skb->len - sizeof(struct udphdr);
792 rqstp->rq_arg.len = len;
793
Chuck Lever95756482007-02-12 00:53:38 -0800794 rqstp->rq_prot = IPPROTO_UDP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
NeilBrown7a37f572007-03-06 01:42:21 -0800796 if (cmh->cmsg_level != IPPROTO_IP ||
797 cmh->cmsg_type != IP_PKTINFO) {
798 if (net_ratelimit())
799 printk("rpcsvc: received unknown control message:"
800 "%d/%d\n",
801 cmh->cmsg_level, cmh->cmsg_type);
802 skb_free_datagram(svsk->sk_sk, skb);
803 return 0;
804 }
805 svc_udp_get_dest_address(rqstp, cmh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 if (skb_is_nonlinear(skb)) {
808 /* we have to copy */
809 local_bh_disable();
810 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
811 local_bh_enable();
812 /* checksum error */
813 skb_free_datagram(svsk->sk_sk, skb);
814 return 0;
815 }
816 local_bh_enable();
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800817 skb_free_datagram(svsk->sk_sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 } else {
819 /* we can use it in-place */
820 rqstp->rq_arg.head[0].iov_base = skb->data + sizeof(struct udphdr);
821 rqstp->rq_arg.head[0].iov_len = len;
Herbert Xufb286bb2005-11-10 13:01:24 -0800822 if (skb_checksum_complete(skb)) {
823 skb_free_datagram(svsk->sk_sk, skb);
824 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
Tom Tucker5148bf42007-12-30 21:07:25 -0600826 rqstp->rq_xprt_ctxt = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828
829 rqstp->rq_arg.page_base = 0;
830 if (len <= rqstp->rq_arg.head[0].iov_len) {
831 rqstp->rq_arg.head[0].iov_len = len;
832 rqstp->rq_arg.page_len = 0;
NeilBrown44524352006-10-04 02:15:46 -0700833 rqstp->rq_respages = rqstp->rq_pages+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 } else {
835 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
NeilBrown44524352006-10-04 02:15:46 -0700836 rqstp->rq_respages = rqstp->rq_pages + 1 +
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700837 DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839
840 if (serv->sv_stats)
841 serv->sv_stats->netudpcnt++;
842
843 return len;
844}
845
846static int
847svc_udp_sendto(struct svc_rqst *rqstp)
848{
849 int error;
850
851 error = svc_sendto(rqstp, &rqstp->rq_res);
852 if (error == -ECONNREFUSED)
853 /* ICMP error on earlier request. */
854 error = svc_sendto(rqstp, &rqstp->rq_res);
855
856 return error;
857}
858
Tom Tuckere831fe62007-12-30 21:07:29 -0600859static void svc_udp_prep_reply_hdr(struct svc_rqst *rqstp)
860{
861}
862
Tom Tucker323bee32007-12-30 21:07:31 -0600863static int svc_udp_has_wspace(struct svc_xprt *xprt)
864{
865 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600866 struct svc_serv *serv = xprt->xpt_server;
Tom Tucker323bee32007-12-30 21:07:31 -0600867 unsigned long required;
868
869 /*
870 * Set the SOCK_NOSPACE flag before checking the available
871 * sock space.
872 */
873 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
Tom Tucker7a90e8c2007-12-30 21:07:55 -0600874 required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
Tom Tucker323bee32007-12-30 21:07:31 -0600875 if (required*2 > sock_wspace(svsk->sk_sk))
876 return 0;
877 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
878 return 1;
879}
880
Tom Tucker38a417c2007-12-30 21:07:36 -0600881static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt)
882{
883 BUG();
884 return NULL;
885}
886
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600887static struct svc_xprt *svc_udp_create(struct svc_serv *serv,
888 struct sockaddr *sa, int salen,
889 int flags)
890{
891 return svc_create_socket(serv, IPPROTO_UDP, sa, salen, flags);
892}
893
Tom Tucker360d87382007-12-30 21:07:17 -0600894static struct svc_xprt_ops svc_udp_ops = {
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600895 .xpo_create = svc_udp_create,
Tom Tucker5d137992007-12-30 21:07:23 -0600896 .xpo_recvfrom = svc_udp_recvfrom,
897 .xpo_sendto = svc_udp_sendto,
Tom Tucker5148bf42007-12-30 21:07:25 -0600898 .xpo_release_rqst = svc_release_skb,
Tom Tucker755ccea2007-12-30 21:07:27 -0600899 .xpo_detach = svc_sock_detach,
900 .xpo_free = svc_sock_free,
Tom Tuckere831fe62007-12-30 21:07:29 -0600901 .xpo_prep_reply_hdr = svc_udp_prep_reply_hdr,
Tom Tucker323bee32007-12-30 21:07:31 -0600902 .xpo_has_wspace = svc_udp_has_wspace,
Tom Tucker38a417c2007-12-30 21:07:36 -0600903 .xpo_accept = svc_udp_accept,
Tom Tucker360d87382007-12-30 21:07:17 -0600904};
905
906static struct svc_xprt_class svc_udp_class = {
907 .xcl_name = "udp",
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600908 .xcl_owner = THIS_MODULE,
Tom Tucker360d87382007-12-30 21:07:17 -0600909 .xcl_ops = &svc_udp_ops,
Tom Tucker49023152007-12-30 21:07:21 -0600910 .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP,
Tom Tucker360d87382007-12-30 21:07:17 -0600911};
912
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600913static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
NeilBrown7a37f572007-03-06 01:42:21 -0800915 int one = 1;
916 mm_segment_t oldfs;
917
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600918 svc_xprt_init(&svc_udp_class, &svsk->sk_xprt, serv);
Tom Tuckerdef13d72007-12-30 21:08:08 -0600919 clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 svsk->sk_sk->sk_data_ready = svc_udp_data_ready;
921 svsk->sk_sk->sk_write_space = svc_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 /* initialise setting must have enough space to
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800924 * receive and respond to one request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 * svc_udp_recvfrom will re-adjust if necessary
926 */
927 svc_sock_setbufsize(svsk->sk_sock,
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600928 3 * svsk->sk_xprt.xpt_server->sv_max_mesg,
929 3 * svsk->sk_xprt.xpt_server->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Tom Tucker02fc6c32007-12-30 21:07:48 -0600931 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* might have come in before data_ready set up */
932 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
NeilBrown7a37f572007-03-06 01:42:21 -0800933
934 oldfs = get_fs();
935 set_fs(KERNEL_DS);
936 /* make sure we get destination address info */
937 svsk->sk_sock->ops->setsockopt(svsk->sk_sock, IPPROTO_IP, IP_PKTINFO,
938 (char __user *)&one, sizeof(one));
939 set_fs(oldfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
942/*
943 * A data_ready event on a listening socket means there's a connection
944 * pending. Do not use state_change as a substitute for it.
945 */
946static void
947svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
948{
Neil Brown939bb7e2005-09-13 01:25:39 -0700949 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 dprintk("svc: socket %p TCP (listen) state change %d\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700952 sk, sk->sk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Neil Brown939bb7e2005-09-13 01:25:39 -0700954 /*
955 * This callback may called twice when a new connection
956 * is established as a child socket inherits everything
957 * from a parent LISTEN socket.
958 * 1) data_ready method of the parent socket will be called
959 * when one of child sockets become ESTABLISHED.
960 * 2) data_ready method of the child socket may be called
961 * when it receives data before the socket is accepted.
962 * In case of 2, we should ignore it silently.
963 */
964 if (sk->sk_state == TCP_LISTEN) {
965 if (svsk) {
Tom Tucker02fc6c32007-12-30 21:07:48 -0600966 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600967 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -0700968 } else
969 printk("svc: socket %p: no user data\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
Neil Brown939bb7e2005-09-13 01:25:39 -0700971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
973 wake_up_interruptible_all(sk->sk_sleep);
974}
975
976/*
977 * A state change on a connected socket means it's dying or dead.
978 */
979static void
980svc_tcp_state_change(struct sock *sk)
981{
Neil Brown939bb7e2005-09-13 01:25:39 -0700982 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700985 sk, sk->sk_state, sk->sk_user_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Neil Brown939bb7e2005-09-13 01:25:39 -0700987 if (!svsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 printk("svc: socket %p: no user data\n", sk);
Neil Brown939bb7e2005-09-13 01:25:39 -0700989 else {
Tom Tucker02fc6c32007-12-30 21:07:48 -0600990 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600991 svc_xprt_enqueue(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
994 wake_up_interruptible_all(sk->sk_sleep);
995}
996
997static void
998svc_tcp_data_ready(struct sock *sk, int count)
999{
Neil Brown939bb7e2005-09-13 01:25:39 -07001000 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 dprintk("svc: socket %p TCP data ready (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -07001003 sk, sk->sk_user_data);
1004 if (svsk) {
Tom Tucker02fc6c32007-12-30 21:07:48 -06001005 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -06001006 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -07001007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
1009 wake_up_interruptible(sk->sk_sleep);
1010}
1011
Chuck Leverbcdb81a2007-02-12 00:53:37 -08001012static inline int svc_port_is_privileged(struct sockaddr *sin)
1013{
1014 switch (sin->sa_family) {
1015 case AF_INET:
1016 return ntohs(((struct sockaddr_in *)sin)->sin_port)
1017 < PROT_SOCK;
Chuck Leverbcdb81a2007-02-12 00:53:37 -08001018 case AF_INET6:
1019 return ntohs(((struct sockaddr_in6 *)sin)->sin6_port)
1020 < PROT_SOCK;
Chuck Leverbcdb81a2007-02-12 00:53:37 -08001021 default:
1022 return 0;
1023 }
1024}
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026/*
1027 * Accept a TCP connection
1028 */
Tom Tucker38a417c2007-12-30 21:07:36 -06001029static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Tom Tucker38a417c2007-12-30 21:07:36 -06001031 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001032 struct sockaddr_storage addr;
1033 struct sockaddr *sin = (struct sockaddr *) &addr;
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001034 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 struct socket *sock = svsk->sk_sock;
1036 struct socket *newsock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 struct svc_sock *newsvsk;
1038 int err, slen;
Chuck Leverad06e4b2007-02-12 00:53:32 -08001039 char buf[RPC_MAX_ADDRBUFLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
1042 if (!sock)
Tom Tucker38a417c2007-12-30 21:07:36 -06001043 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Tom Tucker02fc6c32007-12-30 21:07:48 -06001045 clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001046 err = kernel_accept(sock, &newsock, O_NONBLOCK);
1047 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (err == -ENOMEM)
1049 printk(KERN_WARNING "%s: no more sockets!\n",
1050 serv->sv_name);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001051 else if (err != -EAGAIN && net_ratelimit())
1052 printk(KERN_WARNING "%s: accept failed (err %d)!\n",
1053 serv->sv_name, -err);
Tom Tucker38a417c2007-12-30 21:07:36 -06001054 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
Tom Tucker02fc6c32007-12-30 21:07:48 -06001056 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001058 err = kernel_getpeername(newsock, sin, &slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (err < 0) {
1060 if (net_ratelimit())
1061 printk(KERN_WARNING "%s: peername failed (err %d)!\n",
1062 serv->sv_name, -err);
1063 goto failed; /* aborted connection or whatever */
1064 }
1065
1066 /* Ideally, we would want to reject connections from unauthorized
Chuck Leverad06e4b2007-02-12 00:53:32 -08001067 * hosts here, but when we get encryption, the IP of the host won't
1068 * tell us anything. For now just warn about unpriv connections.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 */
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001070 if (!svc_port_is_privileged(sin)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 dprintk(KERN_WARNING
Chuck Leverad06e4b2007-02-12 00:53:32 -08001072 "%s: connect from unprivileged port: %s\n",
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001073 serv->sv_name,
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001074 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
Chuck Leverad06e4b2007-02-12 00:53:32 -08001076 dprintk("%s: connect from %s\n", serv->sv_name,
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001077 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 /* make sure that a write doesn't block forever when
1080 * low on memory
1081 */
1082 newsock->sk->sk_sndtimeo = HZ*30;
1083
Chuck Lever6b174332007-02-12 00:53:28 -08001084 if (!(newsvsk = svc_setup_socket(serv, newsock, &err,
1085 (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 goto failed;
Tom Tucker9dbc2402007-12-30 21:08:12 -06001087 svc_xprt_set_remote(&newsvsk->sk_xprt, sin, slen);
Frank van Maarseveena9747692007-07-09 22:21:39 +02001088 err = kernel_getsockname(newsock, sin, &slen);
1089 if (unlikely(err < 0)) {
1090 dprintk("svc_tcp_accept: kernel_getsockname error %d\n", -err);
1091 slen = offsetof(struct sockaddr, sa_data);
1092 }
Tom Tucker9dbc2402007-12-30 21:08:12 -06001093 svc_xprt_set_local(&newsvsk->sk_xprt, sin, slen);
Chuck Lever067d7812007-02-12 00:53:30 -08001094
Tom Tuckerf9f3cc42007-12-30 21:07:40 -06001095 if (serv->sv_stats)
1096 serv->sv_stats->nettcpconn++;
1097
1098 return &newsvsk->sk_xprt;
1099
1100failed:
1101 sock_release(newsock);
1102 return NULL;
1103}
1104
1105/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 * Receive data from a TCP socket.
1107 */
1108static int
1109svc_tcp_recvfrom(struct svc_rqst *rqstp)
1110{
1111 struct svc_sock *svsk = rqstp->rq_sock;
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001112 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 int len;
NeilBrown3cc03b12006-10-04 02:15:47 -07001114 struct kvec *vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 int pnum, vlen;
1116
1117 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
Tom Tucker02fc6c32007-12-30 21:07:48 -06001118 svsk, test_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags),
1119 test_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags),
1120 test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Tom Tucker02fc6c32007-12-30 21:07:48 -06001122 if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 /* sndbuf needs to have room for one request
1124 * per thread, otherwise we can stall even when the
1125 * network isn't a bottleneck.
Greg Banks3262c812006-10-02 02:17:58 -07001126 *
1127 * We count all threads rather than threads in a
1128 * particular pool, which provides an upper bound
1129 * on the number of threads which will access the socket.
1130 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 * rcvbuf just needs to be able to hold a few requests.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001132 * Normally they will be removed from the queue
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 * as soon a a complete request arrives.
1134 */
1135 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001136 (serv->sv_nrthreads+3) * serv->sv_max_mesg,
1137 3 * serv->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Tom Tucker02fc6c32007-12-30 21:07:48 -06001139 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 /* Receive data. If we haven't got the record length yet, get
1142 * the next four bytes. Otherwise try to gobble up as much as
1143 * possible up to the complete record length.
1144 */
1145 if (svsk->sk_tcplen < 4) {
1146 unsigned long want = 4 - svsk->sk_tcplen;
1147 struct kvec iov;
1148
1149 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
1150 iov.iov_len = want;
1151 if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
1152 goto error;
1153 svsk->sk_tcplen += len;
1154
1155 if (len < want) {
1156 dprintk("svc: short recvfrom while reading record length (%d of %lu)\n",
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001157 len, want);
Tom Tuckera6046f72007-12-30 21:08:01 -06001158 svc_xprt_received(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return -EAGAIN; /* record header not complete */
1160 }
1161
1162 svsk->sk_reclen = ntohl(svsk->sk_reclen);
1163 if (!(svsk->sk_reclen & 0x80000000)) {
1164 /* FIXME: technically, a record can be fragmented,
1165 * and non-terminal fragments will not have the top
1166 * bit set in the fragment length header.
1167 * But apparently no known nfs clients send fragmented
1168 * records. */
NeilBrown34e9a632007-01-29 13:19:52 -08001169 if (net_ratelimit())
1170 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx"
1171 " (non-terminal)\n",
1172 (unsigned long) svsk->sk_reclen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 goto err_delete;
1174 }
1175 svsk->sk_reclen &= 0x7fffffff;
1176 dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001177 if (svsk->sk_reclen > serv->sv_max_mesg) {
NeilBrown34e9a632007-01-29 13:19:52 -08001178 if (net_ratelimit())
1179 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx"
1180 " (large)\n",
1181 (unsigned long) svsk->sk_reclen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 goto err_delete;
1183 }
1184 }
1185
1186 /* Check whether enough data is available */
1187 len = svc_recv_available(svsk);
1188 if (len < 0)
1189 goto error;
1190
1191 if (len < svsk->sk_reclen) {
1192 dprintk("svc: incomplete TCP record (%d of %d)\n",
1193 len, svsk->sk_reclen);
Tom Tuckera6046f72007-12-30 21:08:01 -06001194 svc_xprt_received(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 return -EAGAIN; /* record not complete */
1196 }
1197 len = svsk->sk_reclen;
Tom Tucker02fc6c32007-12-30 21:07:48 -06001198 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
NeilBrown3cc03b12006-10-04 02:15:47 -07001200 vec = rqstp->rq_vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 vec[0] = rqstp->rq_arg.head[0];
1202 vlen = PAGE_SIZE;
1203 pnum = 1;
1204 while (vlen < len) {
NeilBrown44524352006-10-04 02:15:46 -07001205 vec[pnum].iov_base = page_address(rqstp->rq_pages[pnum]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 vec[pnum].iov_len = PAGE_SIZE;
1207 pnum++;
1208 vlen += PAGE_SIZE;
1209 }
NeilBrown44524352006-10-04 02:15:46 -07001210 rqstp->rq_respages = &rqstp->rq_pages[pnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 /* Now receive data */
1213 len = svc_recvfrom(rqstp, vec, pnum, len);
1214 if (len < 0)
1215 goto error;
1216
1217 dprintk("svc: TCP complete record (%d bytes)\n", len);
1218 rqstp->rq_arg.len = len;
1219 rqstp->rq_arg.page_base = 0;
1220 if (len <= rqstp->rq_arg.head[0].iov_len) {
1221 rqstp->rq_arg.head[0].iov_len = len;
1222 rqstp->rq_arg.page_len = 0;
1223 } else {
1224 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
1225 }
1226
Tom Tucker5148bf42007-12-30 21:07:25 -06001227 rqstp->rq_xprt_ctxt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 rqstp->rq_prot = IPPROTO_TCP;
1229
1230 /* Reset TCP read info */
1231 svsk->sk_reclen = 0;
1232 svsk->sk_tcplen = 0;
1233
Tom Tucker9dbc2402007-12-30 21:08:12 -06001234 svc_xprt_copy_addrs(rqstp, &svsk->sk_xprt);
Tom Tuckera6046f72007-12-30 21:08:01 -06001235 svc_xprt_received(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (serv->sv_stats)
1237 serv->sv_stats->nettcpcnt++;
1238
1239 return len;
1240
1241 err_delete:
Tom Tucker02fc6c32007-12-30 21:07:48 -06001242 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return -EAGAIN;
1244
1245 error:
1246 if (len == -EAGAIN) {
1247 dprintk("RPC: TCP recvfrom got EAGAIN\n");
Tom Tuckera6046f72007-12-30 21:08:01 -06001248 svc_xprt_received(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 } else {
1250 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001251 svsk->sk_xprt.xpt_server->sv_name, -len);
Olaf Kirch93fbf1a2006-01-06 00:19:56 -08001252 goto err_delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254
1255 return len;
1256}
1257
1258/*
1259 * Send out data on TCP socket.
1260 */
1261static int
1262svc_tcp_sendto(struct svc_rqst *rqstp)
1263{
1264 struct xdr_buf *xbufp = &rqstp->rq_res;
1265 int sent;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001266 __be32 reclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 /* Set up the first element of the reply kvec.
1269 * Any other kvecs that may be in use have been taken
1270 * care of by the server implementation itself.
1271 */
1272 reclen = htonl(0x80000000|((xbufp->len ) - 4));
1273 memcpy(xbufp->head[0].iov_base, &reclen, 4);
1274
Tom Tucker02fc6c32007-12-30 21:07:48 -06001275 if (test_bit(XPT_DEAD, &rqstp->rq_sock->sk_xprt.xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return -ENOTCONN;
1277
1278 sent = svc_sendto(rqstp, &rqstp->rq_res);
1279 if (sent != xbufp->len) {
1280 printk(KERN_NOTICE "rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n",
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001281 rqstp->rq_sock->sk_xprt.xpt_server->sv_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 (sent<0)?"got error":"sent only",
1283 sent, xbufp->len);
Tom Tucker02fc6c32007-12-30 21:07:48 -06001284 set_bit(XPT_CLOSE, &rqstp->rq_sock->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -06001285 svc_xprt_enqueue(rqstp->rq_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 sent = -EAGAIN;
1287 }
1288 return sent;
1289}
1290
Tom Tuckere831fe62007-12-30 21:07:29 -06001291/*
1292 * Setup response header. TCP has a 4B record length field.
1293 */
1294static void svc_tcp_prep_reply_hdr(struct svc_rqst *rqstp)
1295{
1296 struct kvec *resv = &rqstp->rq_res.head[0];
1297
1298 /* tcp needs a space for the record length... */
1299 svc_putnl(resv, 0);
1300}
1301
Tom Tucker323bee32007-12-30 21:07:31 -06001302static int svc_tcp_has_wspace(struct svc_xprt *xprt)
1303{
1304 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001305 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Tom Tucker323bee32007-12-30 21:07:31 -06001306 int required;
1307 int wspace;
1308
1309 /*
1310 * Set the SOCK_NOSPACE flag before checking the available
1311 * sock space.
1312 */
1313 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
Tom Tucker7a90e8c2007-12-30 21:07:55 -06001314 required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
Tom Tucker323bee32007-12-30 21:07:31 -06001315 wspace = sk_stream_wspace(svsk->sk_sk);
1316
1317 if (wspace < sk_stream_min_wspace(svsk->sk_sk))
1318 return 0;
1319 if (required * 2 > wspace)
1320 return 0;
1321
1322 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
1323 return 1;
1324}
1325
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001326static struct svc_xprt *svc_tcp_create(struct svc_serv *serv,
1327 struct sockaddr *sa, int salen,
1328 int flags)
1329{
1330 return svc_create_socket(serv, IPPROTO_TCP, sa, salen, flags);
1331}
1332
Tom Tucker360d87382007-12-30 21:07:17 -06001333static struct svc_xprt_ops svc_tcp_ops = {
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001334 .xpo_create = svc_tcp_create,
Tom Tucker5d137992007-12-30 21:07:23 -06001335 .xpo_recvfrom = svc_tcp_recvfrom,
1336 .xpo_sendto = svc_tcp_sendto,
Tom Tucker5148bf42007-12-30 21:07:25 -06001337 .xpo_release_rqst = svc_release_skb,
Tom Tucker755ccea2007-12-30 21:07:27 -06001338 .xpo_detach = svc_sock_detach,
1339 .xpo_free = svc_sock_free,
Tom Tuckere831fe62007-12-30 21:07:29 -06001340 .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr,
Tom Tucker323bee32007-12-30 21:07:31 -06001341 .xpo_has_wspace = svc_tcp_has_wspace,
Tom Tucker38a417c2007-12-30 21:07:36 -06001342 .xpo_accept = svc_tcp_accept,
Tom Tucker360d87382007-12-30 21:07:17 -06001343};
1344
1345static struct svc_xprt_class svc_tcp_class = {
1346 .xcl_name = "tcp",
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001347 .xcl_owner = THIS_MODULE,
Tom Tucker360d87382007-12-30 21:07:17 -06001348 .xcl_ops = &svc_tcp_ops,
Tom Tucker49023152007-12-30 21:07:21 -06001349 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
Tom Tucker360d87382007-12-30 21:07:17 -06001350};
1351
1352void svc_init_xprt_sock(void)
1353{
1354 svc_reg_xprt_class(&svc_tcp_class);
1355 svc_reg_xprt_class(&svc_udp_class);
1356}
1357
1358void svc_cleanup_xprt_sock(void)
1359{
1360 svc_unreg_xprt_class(&svc_tcp_class);
1361 svc_unreg_xprt_class(&svc_udp_class);
1362}
1363
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001364static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
1366 struct sock *sk = svsk->sk_sk;
1367 struct tcp_sock *tp = tcp_sk(sk);
1368
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001369 svc_xprt_init(&svc_tcp_class, &svsk->sk_xprt, serv);
Tom Tuckerdef13d72007-12-30 21:08:08 -06001370 set_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (sk->sk_state == TCP_LISTEN) {
1372 dprintk("setting up TCP socket for listening\n");
Tom Tucker02fc6c32007-12-30 21:07:48 -06001373 set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 sk->sk_data_ready = svc_tcp_listen_data_ready;
Tom Tucker02fc6c32007-12-30 21:07:48 -06001375 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 } else {
1377 dprintk("setting up TCP socket for reading\n");
1378 sk->sk_state_change = svc_tcp_state_change;
1379 sk->sk_data_ready = svc_tcp_data_ready;
1380 sk->sk_write_space = svc_write_space;
1381
1382 svsk->sk_reclen = 0;
1383 svsk->sk_tcplen = 0;
1384
1385 tp->nonagle = 1; /* disable Nagle's algorithm */
1386
1387 /* initialise setting must have enough space to
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001388 * receive and respond to one request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 * svc_tcp_recvfrom will re-adjust if necessary
1390 */
1391 svc_sock_setbufsize(svsk->sk_sock,
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001392 3 * svsk->sk_xprt.xpt_server->sv_max_mesg,
1393 3 * svsk->sk_xprt.xpt_server->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Tom Tucker02fc6c32007-12-30 21:07:48 -06001395 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
1396 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001397 if (sk->sk_state != TCP_ESTABLISHED)
Tom Tucker02fc6c32007-12-30 21:07:48 -06001398 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
1400}
1401
1402void
1403svc_sock_update_bufs(struct svc_serv *serv)
1404{
1405 /*
1406 * The number of server threads has changed. Update
1407 * rcvbuf and sndbuf accordingly on all sockets
1408 */
1409 struct list_head *le;
1410
1411 spin_lock_bh(&serv->sv_lock);
1412 list_for_each(le, &serv->sv_permsocks) {
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001413 struct svc_sock *svsk =
Tom Tucker7a182082007-12-30 21:07:53 -06001414 list_entry(le, struct svc_sock, sk_xprt.xpt_list);
Tom Tucker02fc6c32007-12-30 21:07:48 -06001415 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
1417 list_for_each(le, &serv->sv_tempsocks) {
1418 struct svc_sock *svsk =
Tom Tucker7a182082007-12-30 21:07:53 -06001419 list_entry(le, struct svc_sock, sk_xprt.xpt_list);
Tom Tucker02fc6c32007-12-30 21:07:48 -06001420 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
1422 spin_unlock_bh(&serv->sv_lock);
1423}
1424
1425/*
Tom Tuckere1b31572007-12-30 21:07:46 -06001426 * Make sure that we don't have too many active connections. If we
1427 * have, something must be dropped.
1428 *
1429 * There's no point in trying to do random drop here for DoS
1430 * prevention. The NFS clients does 1 reconnect in 15 seconds. An
1431 * attacker can easily beat that.
1432 *
1433 * The only somewhat efficient mechanism would be if drop old
1434 * connections from the same IP first. But right now we don't even
1435 * record the client IP in svc_sock.
1436 */
1437static void svc_check_conn_limits(struct svc_serv *serv)
1438{
1439 if (serv->sv_tmpcnt > (serv->sv_nrthreads+3)*20) {
1440 struct svc_sock *svsk = NULL;
1441 spin_lock_bh(&serv->sv_lock);
1442 if (!list_empty(&serv->sv_tempsocks)) {
1443 if (net_ratelimit()) {
1444 /* Try to help the admin */
1445 printk(KERN_NOTICE "%s: too many open TCP "
1446 "sockets, consider increasing the "
1447 "number of nfsd threads\n",
1448 serv->sv_name);
1449 }
1450 /*
1451 * Always select the oldest socket. It's not fair,
1452 * but so is life
1453 */
1454 svsk = list_entry(serv->sv_tempsocks.prev,
1455 struct svc_sock,
Tom Tucker7a182082007-12-30 21:07:53 -06001456 sk_xprt.xpt_list);
Tom Tucker02fc6c32007-12-30 21:07:48 -06001457 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Tom Tuckere1b31572007-12-30 21:07:46 -06001458 svc_xprt_get(&svsk->sk_xprt);
1459 }
1460 spin_unlock_bh(&serv->sv_lock);
1461
1462 if (svsk) {
Tom Tuckerf6150c32007-12-30 21:07:57 -06001463 svc_xprt_enqueue(&svsk->sk_xprt);
Tom Tuckere1b31572007-12-30 21:07:46 -06001464 svc_xprt_put(&svsk->sk_xprt);
1465 }
1466 }
1467}
1468
1469/*
Greg Banks3262c812006-10-02 02:17:58 -07001470 * Receive the next request on any socket. This code is carefully
1471 * organised not to touch any cachelines in the shared svc_serv
1472 * structure, only cachelines in the local svc_pool.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 */
1474int
NeilBrown6fb2b472006-10-02 02:17:50 -07001475svc_recv(struct svc_rqst *rqstp, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Tom Tuckerc36adb2a2007-12-30 21:08:16 -06001477 struct svc_xprt *xprt = NULL;
NeilBrown6fb2b472006-10-02 02:17:50 -07001478 struct svc_serv *serv = rqstp->rq_server;
Greg Banks3262c812006-10-02 02:17:58 -07001479 struct svc_pool *pool = rqstp->rq_pool;
NeilBrown44524352006-10-04 02:15:46 -07001480 int len, i;
Tom Tuckerc36adb2a2007-12-30 21:08:16 -06001481 int pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 struct xdr_buf *arg;
1483 DECLARE_WAITQUEUE(wait, current);
1484
1485 dprintk("svc: server %p waiting for data (to = %ld)\n",
1486 rqstp, timeout);
1487
Tom Tuckerc36adb2a2007-12-30 21:08:16 -06001488 if (rqstp->rq_xprt)
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001489 printk(KERN_ERR
Tom Tuckerc36adb2a2007-12-30 21:08:16 -06001490 "svc_recv: service %p, transport not NULL!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 rqstp);
1492 if (waitqueue_active(&rqstp->rq_wait))
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001493 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 "svc_recv: service %p, wait queue active!\n",
1495 rqstp);
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 /* now allocate needed pages. If we get a failure, sleep briefly */
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001499 pages = (serv->sv_max_mesg + PAGE_SIZE) / PAGE_SIZE;
NeilBrown44524352006-10-04 02:15:46 -07001500 for (i=0; i < pages ; i++)
1501 while (rqstp->rq_pages[i] == NULL) {
1502 struct page *p = alloc_page(GFP_KERNEL);
1503 if (!p)
1504 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
1505 rqstp->rq_pages[i] = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
NeilBrown250f3912007-01-26 00:56:59 -08001507 rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */
1508 BUG_ON(pages >= RPCSVC_MAXPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 /* Make arg->head point to first page and arg->pages point to rest */
1511 arg = &rqstp->rq_arg;
NeilBrown44524352006-10-04 02:15:46 -07001512 arg->head[0].iov_base = page_address(rqstp->rq_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 arg->head[0].iov_len = PAGE_SIZE;
NeilBrown44524352006-10-04 02:15:46 -07001514 arg->pages = rqstp->rq_pages + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 arg->page_base = 0;
1516 /* save at least one page for response */
1517 arg->page_len = (pages-2)*PAGE_SIZE;
1518 arg->len = (pages-1)*PAGE_SIZE;
1519 arg->tail[0].iov_len = 0;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001520
1521 try_to_freeze();
NeilBrown1887b932005-11-15 00:09:10 -08001522 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (signalled())
1524 return -EINTR;
1525
Greg Banks3262c812006-10-02 02:17:58 -07001526 spin_lock_bh(&pool->sp_lock);
Tom Tuckerc36adb2a2007-12-30 21:08:16 -06001527 xprt = svc_xprt_dequeue(pool);
1528 if (xprt) {
1529 rqstp->rq_xprt = xprt;
1530 svc_xprt_get(xprt);
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001531 rqstp->rq_reserved = serv->sv_max_mesg;
Tom Tuckerc36adb2a2007-12-30 21:08:16 -06001532 atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 } else {
1534 /* No data pending. Go to sleep */
Greg Banks3262c812006-10-02 02:17:58 -07001535 svc_thread_enqueue(pool, rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 /*
1538 * We have to be able to interrupt this wait
1539 * to bring down the daemons ...
1540 */
1541 set_current_state(TASK_INTERRUPTIBLE);
1542 add_wait_queue(&rqstp->rq_wait, &wait);
Greg Banks3262c812006-10-02 02:17:58 -07001543 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545 schedule_timeout(timeout);
1546
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001547 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Greg Banks3262c812006-10-02 02:17:58 -07001549 spin_lock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 remove_wait_queue(&rqstp->rq_wait, &wait);
1551
Tom Tuckerc36adb2a2007-12-30 21:08:16 -06001552 xprt = rqstp->rq_xprt;
1553 if (!xprt) {
Greg Banks3262c812006-10-02 02:17:58 -07001554 svc_thread_dequeue(pool, rqstp);
1555 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 dprintk("svc: server %p, no data yet\n", rqstp);
1557 return signalled()? -EINTR : -EAGAIN;
1558 }
1559 }
Greg Banks3262c812006-10-02 02:17:58 -07001560 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Tom Tuckerd7979ae2007-12-30 21:07:34 -06001562 len = 0;
Tom Tuckerc36adb2a2007-12-30 21:08:16 -06001563 if (test_bit(XPT_CLOSE, &xprt->xpt_flags)) {
Tom Tucker02fc6c32007-12-30 21:07:48 -06001564 dprintk("svc_recv: found XPT_CLOSE\n");
Tom Tuckerc36adb2a2007-12-30 21:08:16 -06001565 svc_delete_xprt(xprt);
1566 } else if (test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
Tom Tucker38a417c2007-12-30 21:07:36 -06001567 struct svc_xprt *newxpt;
Tom Tuckerc36adb2a2007-12-30 21:08:16 -06001568 newxpt = xprt->xpt_ops->xpo_accept(xprt);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001569 if (newxpt) {
1570 /*
1571 * We know this module_get will succeed because the
1572 * listener holds a reference too
1573 */
1574 __module_get(newxpt->xpt_class->xcl_owner);
Tom Tuckerc36adb2a2007-12-30 21:08:16 -06001575 svc_check_conn_limits(xprt->xpt_server);
Tom Tucker6bc5ab12007-12-30 21:08:03 -06001576 svc_xprt_received(newxpt);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001577 }
Tom Tuckerc36adb2a2007-12-30 21:08:16 -06001578 svc_xprt_received(xprt);
Tom Tuckerd7979ae2007-12-30 21:07:34 -06001579 } else {
Tom Tuckerc36adb2a2007-12-30 21:08:16 -06001580 dprintk("svc: server %p, pool %u, transport %p, inuse=%d\n",
1581 rqstp, pool->sp_id, xprt,
1582 atomic_read(&xprt->xpt_ref.refcount));
1583 rqstp->rq_deferred = svc_deferred_dequeue(xprt);
Tom Tucker8c7b0172007-12-30 21:08:10 -06001584 if (rqstp->rq_deferred) {
Tom Tuckerc36adb2a2007-12-30 21:08:16 -06001585 svc_xprt_received(xprt);
Tom Tucker8c7b0172007-12-30 21:08:10 -06001586 len = svc_deferred_recv(rqstp);
1587 } else
Tom Tuckerc36adb2a2007-12-30 21:08:16 -06001588 len = xprt->xpt_ops->xpo_recvfrom(rqstp);
Tom Tuckerd7979ae2007-12-30 21:07:34 -06001589 dprintk("svc: got len=%d\n", len);
1590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 /* No data, incomplete (TCP) read, or accept() */
1593 if (len == 0 || len == -EAGAIN) {
1594 rqstp->rq_res.len = 0;
Tom Tuckereab996d2007-12-30 21:08:14 -06001595 svc_xprt_release(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 return -EAGAIN;
1597 }
Tom Tuckerc36adb2a2007-12-30 21:08:16 -06001598 clear_bit(XPT_OLD, &xprt->xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Chuck Leverbcdb81a2007-02-12 00:53:37 -08001600 rqstp->rq_secure = svc_port_is_privileged(svc_addr(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 rqstp->rq_chandle.defer = svc_defer;
1602
1603 if (serv->sv_stats)
1604 serv->sv_stats->netcnt++;
1605 return len;
1606}
1607
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001608/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 * Drop request
1610 */
1611void
1612svc_drop(struct svc_rqst *rqstp)
1613{
1614 dprintk("svc: socket %p dropped request\n", rqstp->rq_sock);
Tom Tuckereab996d2007-12-30 21:08:14 -06001615 svc_xprt_release(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616}
1617
1618/*
1619 * Return reply to client.
1620 */
1621int
1622svc_send(struct svc_rqst *rqstp)
1623{
Tom Tuckera50fea22007-12-30 21:07:59 -06001624 struct svc_xprt *xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 int len;
1626 struct xdr_buf *xb;
1627
Tom Tuckera50fea22007-12-30 21:07:59 -06001628 xprt = rqstp->rq_xprt;
1629 if (!xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632 /* release the receive skb before sending the reply */
Tom Tucker5148bf42007-12-30 21:07:25 -06001633 rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 /* calculate over-all length */
1636 xb = & rqstp->rq_res;
1637 xb->len = xb->head[0].iov_len +
1638 xb->page_len +
1639 xb->tail[0].iov_len;
1640
Tom Tuckera50fea22007-12-30 21:07:59 -06001641 /* Grab mutex to serialize outgoing data. */
1642 mutex_lock(&xprt->xpt_mutex);
1643 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 len = -ENOTCONN;
1645 else
Tom Tuckera50fea22007-12-30 21:07:59 -06001646 len = xprt->xpt_ops->xpo_sendto(rqstp);
1647 mutex_unlock(&xprt->xpt_mutex);
Tom Tuckereab996d2007-12-30 21:08:14 -06001648 svc_xprt_release(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
1651 return 0;
1652 return len;
1653}
1654
1655/*
Greg Banks36bdfc82006-10-02 02:17:54 -07001656 * Timer function to close old temporary sockets, using
1657 * a mark-and-sweep algorithm.
1658 */
1659static void
1660svc_age_temp_sockets(unsigned long closure)
1661{
1662 struct svc_serv *serv = (struct svc_serv *)closure;
1663 struct svc_sock *svsk;
1664 struct list_head *le, *next;
1665 LIST_HEAD(to_be_aged);
1666
1667 dprintk("svc_age_temp_sockets\n");
1668
1669 if (!spin_trylock_bh(&serv->sv_lock)) {
1670 /* busy, try again 1 sec later */
1671 dprintk("svc_age_temp_sockets: busy\n");
1672 mod_timer(&serv->sv_temptimer, jiffies + HZ);
1673 return;
1674 }
1675
1676 list_for_each_safe(le, next, &serv->sv_tempsocks) {
Tom Tucker7a182082007-12-30 21:07:53 -06001677 svsk = list_entry(le, struct svc_sock, sk_xprt.xpt_list);
Greg Banks36bdfc82006-10-02 02:17:54 -07001678
Tom Tucker02fc6c32007-12-30 21:07:48 -06001679 if (!test_and_set_bit(XPT_OLD, &svsk->sk_xprt.xpt_flags))
Greg Banks36bdfc82006-10-02 02:17:54 -07001680 continue;
Tom Tuckere1b31572007-12-30 21:07:46 -06001681 if (atomic_read(&svsk->sk_xprt.xpt_ref.refcount) > 1
Tom Tucker02fc6c32007-12-30 21:07:48 -06001682 || test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags))
Greg Banks36bdfc82006-10-02 02:17:54 -07001683 continue;
Tom Tuckere1b31572007-12-30 21:07:46 -06001684 svc_xprt_get(&svsk->sk_xprt);
Greg Banks36bdfc82006-10-02 02:17:54 -07001685 list_move(le, &to_be_aged);
Tom Tucker02fc6c32007-12-30 21:07:48 -06001686 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1687 set_bit(XPT_DETACHED, &svsk->sk_xprt.xpt_flags);
Greg Banks36bdfc82006-10-02 02:17:54 -07001688 }
1689 spin_unlock_bh(&serv->sv_lock);
1690
1691 while (!list_empty(&to_be_aged)) {
1692 le = to_be_aged.next;
Tom Tucker7a182082007-12-30 21:07:53 -06001693 /* fiddling the sk_xprt.xpt_list node is safe 'cos we're XPT_DETACHED */
Greg Banks36bdfc82006-10-02 02:17:54 -07001694 list_del_init(le);
Tom Tucker7a182082007-12-30 21:07:53 -06001695 svsk = list_entry(le, struct svc_sock, sk_xprt.xpt_list);
Greg Banks36bdfc82006-10-02 02:17:54 -07001696
Tom Tucker4bc6c492007-12-30 21:08:05 -06001697 dprintk("queuing svsk %p for closing\n", svsk);
Greg Banks36bdfc82006-10-02 02:17:54 -07001698
1699 /* a thread will dequeue and close it soon */
Tom Tuckerf6150c32007-12-30 21:07:57 -06001700 svc_xprt_enqueue(&svsk->sk_xprt);
Tom Tuckere1b31572007-12-30 21:07:46 -06001701 svc_xprt_put(&svsk->sk_xprt);
Greg Banks36bdfc82006-10-02 02:17:54 -07001702 }
1703
1704 mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
1705}
1706
1707/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 * Initialize socket for RPC use and create svc_sock struct
1709 * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
1710 */
Chuck Lever6b174332007-02-12 00:53:28 -08001711static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1712 struct socket *sock,
1713 int *errp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
1715 struct svc_sock *svsk;
1716 struct sock *inet;
Chuck Lever6b174332007-02-12 00:53:28 -08001717 int pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
1718 int is_temporary = flags & SVC_SOCK_TEMPORARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 dprintk("svc: svc_setup_socket %p\n", sock);
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001721 if (!(svsk = kzalloc(sizeof(*svsk), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 *errp = -ENOMEM;
1723 return NULL;
1724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 inet = sock->sk;
1727
1728 /* Register socket with portmapper */
1729 if (*errp >= 0 && pmap_register)
1730 *errp = svc_register(serv, inet->sk_protocol,
1731 ntohs(inet_sk(inet)->sport));
1732
1733 if (*errp < 0) {
1734 kfree(svsk);
1735 return NULL;
1736 }
1737
Tom Tucker02fc6c32007-12-30 21:07:48 -06001738 set_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 inet->sk_user_data = svsk;
1740 svsk->sk_sock = sock;
1741 svsk->sk_sk = inet;
1742 svsk->sk_ostate = inet->sk_state_change;
1743 svsk->sk_odata = inet->sk_data_ready;
1744 svsk->sk_owspace = inet->sk_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746 /* Initialize the socket */
1747 if (sock->type == SOCK_DGRAM)
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001748 svc_udp_init(svsk, serv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 else
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001750 svc_tcp_init(svsk, serv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
1752 spin_lock_bh(&serv->sv_lock);
Chuck Lever6b174332007-02-12 00:53:28 -08001753 if (is_temporary) {
Tom Tucker02fc6c32007-12-30 21:07:48 -06001754 set_bit(XPT_TEMP, &svsk->sk_xprt.xpt_flags);
Tom Tucker7a182082007-12-30 21:07:53 -06001755 list_add(&svsk->sk_xprt.xpt_list, &serv->sv_tempsocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 serv->sv_tmpcnt++;
Greg Banks36bdfc82006-10-02 02:17:54 -07001757 if (serv->sv_temptimer.function == NULL) {
1758 /* setup timer to age temp sockets */
1759 setup_timer(&serv->sv_temptimer, svc_age_temp_sockets,
1760 (unsigned long)serv);
1761 mod_timer(&serv->sv_temptimer,
1762 jiffies + svc_conn_age_period * HZ);
1763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 } else {
Tom Tucker02fc6c32007-12-30 21:07:48 -06001765 clear_bit(XPT_TEMP, &svsk->sk_xprt.xpt_flags);
Tom Tucker7a182082007-12-30 21:07:53 -06001766 list_add(&svsk->sk_xprt.xpt_list, &serv->sv_permsocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 }
1768 spin_unlock_bh(&serv->sv_lock);
1769
1770 dprintk("svc: svc_setup_socket created %p (inet %p)\n",
1771 svsk, svsk->sk_sk);
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 return svsk;
1774}
1775
NeilBrownb41b66d2006-10-02 02:17:48 -07001776int svc_addsock(struct svc_serv *serv,
1777 int fd,
1778 char *name_return,
1779 int *proto)
1780{
1781 int err = 0;
1782 struct socket *so = sockfd_lookup(fd, &err);
1783 struct svc_sock *svsk = NULL;
1784
1785 if (!so)
1786 return err;
1787 if (so->sk->sk_family != AF_INET)
1788 err = -EAFNOSUPPORT;
1789 else if (so->sk->sk_protocol != IPPROTO_TCP &&
1790 so->sk->sk_protocol != IPPROTO_UDP)
1791 err = -EPROTONOSUPPORT;
1792 else if (so->state > SS_UNCONNECTED)
1793 err = -EISCONN;
1794 else {
Chuck Lever6b174332007-02-12 00:53:28 -08001795 svsk = svc_setup_socket(serv, so, &err, SVC_SOCK_DEFAULTS);
NeilBrowne79eff12007-02-12 00:53:30 -08001796 if (svsk) {
Tom Tucker9dbc2402007-12-30 21:08:12 -06001797 struct sockaddr_storage addr;
1798 struct sockaddr *sin = (struct sockaddr *)&addr;
1799 int salen;
1800 if (kernel_getsockname(svsk->sk_sock, sin, &salen) == 0)
1801 svc_xprt_set_local(&svsk->sk_xprt, sin, salen);
Tom Tuckera6046f72007-12-30 21:08:01 -06001802 svc_xprt_received(&svsk->sk_xprt);
NeilBrownb41b66d2006-10-02 02:17:48 -07001803 err = 0;
NeilBrowne79eff12007-02-12 00:53:30 -08001804 }
NeilBrownb41b66d2006-10-02 02:17:48 -07001805 }
1806 if (err) {
1807 sockfd_put(so);
1808 return err;
1809 }
1810 if (proto) *proto = so->sk->sk_protocol;
1811 return one_sock_name(name_return, svsk);
1812}
1813EXPORT_SYMBOL_GPL(svc_addsock);
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815/*
1816 * Create socket for RPC service.
1817 */
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001818static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
1819 int protocol,
1820 struct sockaddr *sin, int len,
1821 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
1823 struct svc_sock *svsk;
1824 struct socket *sock;
1825 int error;
1826 int type;
Chuck Leverad06e4b2007-02-12 00:53:32 -08001827 char buf[RPC_MAX_ADDRBUFLEN];
Tom Tucker9dbc2402007-12-30 21:08:12 -06001828 struct sockaddr_storage addr;
1829 struct sockaddr *newsin = (struct sockaddr *)&addr;
1830 int newlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Chuck Leverad06e4b2007-02-12 00:53:32 -08001832 dprintk("svc: svc_create_socket(%s, %d, %s)\n",
1833 serv->sv_program->pg_name, protocol,
Chuck Lever77f1f672007-02-12 00:53:39 -08001834 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
1836 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1837 printk(KERN_WARNING "svc: only UDP and TCP "
1838 "sockets supported\n");
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001839 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 }
1841 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1842
Chuck Lever77f1f672007-02-12 00:53:39 -08001843 error = sock_create_kern(sin->sa_family, type, protocol, &sock);
1844 if (error < 0)
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001845 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Peter Zijlstraed075362006-12-06 20:35:24 -08001847 svc_reclassify_socket(sock);
1848
Eric Sesterhenn18114742006-09-28 14:37:07 -07001849 if (type == SOCK_STREAM)
Chuck Lever77f1f672007-02-12 00:53:39 -08001850 sock->sk->sk_reuse = 1; /* allow address reuse */
1851 error = kernel_bind(sock, sin, len);
Eric Sesterhenn18114742006-09-28 14:37:07 -07001852 if (error < 0)
1853 goto bummer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Tom Tucker9dbc2402007-12-30 21:08:12 -06001855 newlen = len;
1856 error = kernel_getsockname(sock, newsin, &newlen);
1857 if (error < 0)
1858 goto bummer;
1859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (protocol == IPPROTO_TCP) {
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001861 if ((error = kernel_listen(sock, 64)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 goto bummer;
1863 }
1864
NeilBrowne79eff12007-02-12 00:53:30 -08001865 if ((svsk = svc_setup_socket(serv, sock, &error, flags)) != NULL) {
Tom Tucker9dbc2402007-12-30 21:08:12 -06001866 svc_xprt_set_local(&svsk->sk_xprt, newsin, newlen);
Tom Tuckera6046f72007-12-30 21:08:01 -06001867 svc_xprt_received(&svsk->sk_xprt);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001868 return (struct svc_xprt *)svsk;
NeilBrowne79eff12007-02-12 00:53:30 -08001869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871bummer:
1872 dprintk("svc: svc_create_socket error = %d\n", -error);
1873 sock_release(sock);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001874 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875}
1876
1877/*
Tom Tucker755ccea2007-12-30 21:07:27 -06001878 * Detach the svc_sock from the socket so that no
1879 * more callbacks occur.
1880 */
1881static void svc_sock_detach(struct svc_xprt *xprt)
1882{
1883 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1884 struct sock *sk = svsk->sk_sk;
1885
1886 dprintk("svc: svc_sock_detach(%p)\n", svsk);
1887
1888 /* put back the old socket callbacks */
1889 sk->sk_state_change = svsk->sk_ostate;
1890 sk->sk_data_ready = svsk->sk_odata;
1891 sk->sk_write_space = svsk->sk_owspace;
1892}
1893
1894/*
1895 * Free the svc_sock's socket resources and the svc_sock itself.
1896 */
1897static void svc_sock_free(struct svc_xprt *xprt)
1898{
1899 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1900 dprintk("svc: svc_sock_free(%p)\n", svsk);
1901
Tom Tucker755ccea2007-12-30 21:07:27 -06001902 if (svsk->sk_sock->file)
1903 sockfd_put(svsk->sk_sock);
1904 else
1905 sock_release(svsk->sk_sock);
1906 kfree(svsk);
1907}
1908
1909/*
Tom Tucker7a182082007-12-30 21:07:53 -06001910 * Remove a dead transport
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 */
Tom Tucker7a182082007-12-30 21:07:53 -06001912static void svc_delete_xprt(struct svc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
Tom Tucker7a182082007-12-30 21:07:53 -06001914 struct svc_serv *serv = xprt->xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Tom Tucker7a182082007-12-30 21:07:53 -06001916 dprintk("svc: svc_delete_xprt(%p)\n", xprt);
1917 xprt->xpt_ops->xpo_detach(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 spin_lock_bh(&serv->sv_lock);
Tom Tucker7a182082007-12-30 21:07:53 -06001920 if (!test_and_set_bit(XPT_DETACHED, &xprt->xpt_flags))
1921 list_del_init(&xprt->xpt_list);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001922 /*
Tom Tucker7a182082007-12-30 21:07:53 -06001923 * We used to delete the transport from whichever list
1924 * it's sk_xprt.xpt_ready node was on, but we don't actually
Greg Banks3262c812006-10-02 02:17:58 -07001925 * need to. This is because the only time we're called
1926 * while still attached to a queue, the queue itself
1927 * is about to be destroyed (in svc_destroy).
1928 */
Tom Tucker7a182082007-12-30 21:07:53 -06001929 if (!test_and_set_bit(XPT_DEAD, &xprt->xpt_flags)) {
1930 BUG_ON(atomic_read(&xprt->xpt_ref.refcount) < 2);
1931 if (test_bit(XPT_TEMP, &xprt->xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 serv->sv_tmpcnt--;
Tom Tucker7a182082007-12-30 21:07:53 -06001933 svc_xprt_put(xprt);
NeilBrownaaf68cf2007-02-08 14:20:30 -08001934 }
Neil Brownd6740df2006-10-29 22:46:45 -08001935 spin_unlock_bh(&serv->sv_lock);
NeilBrownaaf68cf2007-02-08 14:20:30 -08001936}
1937
Tom Tucker7a182082007-12-30 21:07:53 -06001938static void svc_close_xprt(struct svc_xprt *xprt)
NeilBrownaaf68cf2007-02-08 14:20:30 -08001939{
Tom Tucker7a182082007-12-30 21:07:53 -06001940 set_bit(XPT_CLOSE, &xprt->xpt_flags);
1941 if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags))
NeilBrownaaf68cf2007-02-08 14:20:30 -08001942 /* someone else will have to effect the close */
1943 return;
1944
Tom Tucker7a182082007-12-30 21:07:53 -06001945 svc_xprt_get(xprt);
1946 svc_delete_xprt(xprt);
1947 clear_bit(XPT_BUSY, &xprt->xpt_flags);
1948 svc_xprt_put(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949}
1950
Tom Tucker7a182082007-12-30 21:07:53 -06001951void svc_close_all(struct list_head *xprt_list)
NeilBrowncda1fd42007-03-06 01:42:22 -08001952{
Tom Tucker7a182082007-12-30 21:07:53 -06001953 struct svc_xprt *xprt;
1954 struct svc_xprt *tmp;
1955
1956 list_for_each_entry_safe(xprt, tmp, xprt_list, xpt_list) {
1957 set_bit(XPT_CLOSE, &xprt->xpt_flags);
1958 if (test_bit(XPT_BUSY, &xprt->xpt_flags)) {
1959 /* Waiting to be processed, but no threads left,
1960 * So just remove it from the waiting list
1961 */
1962 list_del_init(&xprt->xpt_ready);
1963 clear_bit(XPT_BUSY, &xprt->xpt_flags);
1964 }
1965 svc_close_xprt(xprt);
NeilBrowncda1fd42007-03-06 01:42:22 -08001966 }
NeilBrowncda1fd42007-03-06 01:42:22 -08001967}
1968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969/*
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001970 * Handle defer and revisit of requests
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 */
1972
1973static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
1974{
1975 struct svc_deferred_req *dr = container_of(dreq, struct svc_deferred_req, handle);
Tom Tucker8c7b0172007-12-30 21:08:10 -06001976 struct svc_xprt *xprt = dr->xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978 if (too_many) {
Tom Tucker8c7b0172007-12-30 21:08:10 -06001979 svc_xprt_put(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 kfree(dr);
1981 return;
1982 }
1983 dprintk("revisit queued\n");
Tom Tucker8c7b0172007-12-30 21:08:10 -06001984 dr->xprt = NULL;
1985 spin_lock(&xprt->xpt_lock);
1986 list_add(&dr->handle.recent, &xprt->xpt_deferred);
1987 spin_unlock(&xprt->xpt_lock);
1988 set_bit(XPT_DEFERRED, &xprt->xpt_flags);
1989 svc_xprt_enqueue(xprt);
1990 svc_xprt_put(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991}
1992
1993static struct cache_deferred_req *
1994svc_defer(struct cache_req *req)
1995{
1996 struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
1997 int size = sizeof(struct svc_deferred_req) + (rqstp->rq_arg.len);
1998 struct svc_deferred_req *dr;
1999
2000 if (rqstp->rq_arg.page_len)
2001 return NULL; /* if more than a page, give up FIXME */
2002 if (rqstp->rq_deferred) {
2003 dr = rqstp->rq_deferred;
2004 rqstp->rq_deferred = NULL;
2005 } else {
2006 int skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
2007 /* FIXME maybe discard if size too large */
2008 dr = kmalloc(size, GFP_KERNEL);
2009 if (dr == NULL)
2010 return NULL;
2011
2012 dr->handle.owner = rqstp->rq_server;
2013 dr->prot = rqstp->rq_prot;
Chuck Lever24422222007-02-12 00:53:33 -08002014 memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen);
2015 dr->addrlen = rqstp->rq_addrlen;
J. Bruce Fields1918e342006-01-18 17:43:16 -08002016 dr->daddr = rqstp->rq_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 dr->argslen = rqstp->rq_arg.len >> 2;
2018 memcpy(dr->args, rqstp->rq_arg.head[0].iov_base-skip, dr->argslen<<2);
2019 }
Tom Tuckere1b31572007-12-30 21:07:46 -06002020 svc_xprt_get(rqstp->rq_xprt);
Tom Tucker8c7b0172007-12-30 21:08:10 -06002021 dr->xprt = rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
2023 dr->handle.revisit = svc_revisit;
2024 return &dr->handle;
2025}
2026
2027/*
2028 * recv data from a deferred request into an active one
2029 */
2030static int svc_deferred_recv(struct svc_rqst *rqstp)
2031{
2032 struct svc_deferred_req *dr = rqstp->rq_deferred;
2033
2034 rqstp->rq_arg.head[0].iov_base = dr->args;
2035 rqstp->rq_arg.head[0].iov_len = dr->argslen<<2;
2036 rqstp->rq_arg.page_len = 0;
2037 rqstp->rq_arg.len = dr->argslen<<2;
2038 rqstp->rq_prot = dr->prot;
Chuck Lever24422222007-02-12 00:53:33 -08002039 memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen);
2040 rqstp->rq_addrlen = dr->addrlen;
J. Bruce Fields1918e342006-01-18 17:43:16 -08002041 rqstp->rq_daddr = dr->daddr;
NeilBrown44524352006-10-04 02:15:46 -07002042 rqstp->rq_respages = rqstp->rq_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 return dr->argslen<<2;
2044}
2045
2046
Tom Tucker8c7b0172007-12-30 21:08:10 -06002047static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
2049 struct svc_deferred_req *dr = NULL;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08002050
Tom Tucker8c7b0172007-12-30 21:08:10 -06002051 if (!test_bit(XPT_DEFERRED, &xprt->xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 return NULL;
Tom Tucker8c7b0172007-12-30 21:08:10 -06002053 spin_lock(&xprt->xpt_lock);
2054 clear_bit(XPT_DEFERRED, &xprt->xpt_flags);
2055 if (!list_empty(&xprt->xpt_deferred)) {
2056 dr = list_entry(xprt->xpt_deferred.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 struct svc_deferred_req,
2058 handle.recent);
2059 list_del_init(&dr->handle.recent);
Tom Tucker8c7b0172007-12-30 21:08:10 -06002060 set_bit(XPT_DEFERRED, &xprt->xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 }
Tom Tucker8c7b0172007-12-30 21:08:10 -06002062 spin_unlock(&xprt->xpt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return dr;
2064}