blob: 9564d2e9520e8276951645448d9f53985fe03f58 [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 */
313static inline struct svc_sock *
Greg Banks3262c812006-10-02 02:17:58 -0700314svc_sock_dequeue(struct svc_pool *pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 struct svc_sock *svsk;
317
Greg Banks3262c812006-10-02 02:17:58 -0700318 if (list_empty(&pool->sp_sockets))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return NULL;
320
Greg Banks3262c812006-10-02 02:17:58 -0700321 svsk = list_entry(pool->sp_sockets.next,
Tom Tucker7a182082007-12-30 21:07:53 -0600322 struct svc_sock, sk_xprt.xpt_ready);
323 list_del_init(&svsk->sk_xprt.xpt_ready);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 dprintk("svc: socket %p dequeued, inuse=%d\n",
Tom Tuckere1b31572007-12-30 21:07:46 -0600326 svsk->sk_sk, atomic_read(&svsk->sk_xprt.xpt_ref.refcount));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 return svsk;
329}
330
331/*
Tom Tuckera6046f72007-12-30 21:08:01 -0600332 * svc_xprt_received conditionally queues the transport for processing
333 * by another thread. The caller must hold the XPT_BUSY bit and must
334 * not thereafter touch transport data.
335 *
336 * Note: XPT_DATA only gets cleared when a read-attempt finds no (or
337 * insufficient) data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 */
Tom Tuckera6046f72007-12-30 21:08:01 -0600339void svc_xprt_received(struct svc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Tom Tuckera6046f72007-12-30 21:08:01 -0600341 BUG_ON(!test_bit(XPT_BUSY, &xprt->xpt_flags));
342 xprt->xpt_pool = NULL;
343 clear_bit(XPT_BUSY, &xprt->xpt_flags);
344 svc_xprt_enqueue(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
Tom Tuckera6046f72007-12-30 21:08:01 -0600346EXPORT_SYMBOL_GPL(svc_xprt_received);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348/**
349 * svc_reserve - change the space reserved for the reply to a request.
350 * @rqstp: The request in question
351 * @space: new max space to reserve
352 *
353 * Each request reserves some space on the output queue of the socket
354 * to make sure the reply fits. This function reduces that reserved
355 * space to be the amount of space used already, plus @space.
356 *
357 */
358void svc_reserve(struct svc_rqst *rqstp, int space)
359{
360 space += rqstp->rq_res.head[0].iov_len;
361
362 if (space < rqstp->rq_reserved) {
Tom Tuckerf6150c32007-12-30 21:07:57 -0600363 struct svc_xprt *xprt = rqstp->rq_xprt;
364 atomic_sub((rqstp->rq_reserved - space), &xprt->xpt_reserved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 rqstp->rq_reserved = space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Tom Tuckerf6150c32007-12-30 21:07:57 -0600367 svc_xprt_enqueue(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369}
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371static void
372svc_sock_release(struct svc_rqst *rqstp)
373{
374 struct svc_sock *svsk = rqstp->rq_sock;
375
Tom Tucker5148bf42007-12-30 21:07:25 -0600376 rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
NeilBrown44524352006-10-04 02:15:46 -0700378 svc_free_res_pages(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 rqstp->rq_res.page_len = 0;
380 rqstp->rq_res.page_base = 0;
381
382
383 /* Reset response buffer and release
384 * the reservation.
385 * But first, check that enough space was reserved
386 * for the reply, otherwise we have a bug!
387 */
388 if ((rqstp->rq_res.len) > rqstp->rq_reserved)
389 printk(KERN_ERR "RPC request reserved %d but used %d\n",
390 rqstp->rq_reserved,
391 rqstp->rq_res.len);
392
393 rqstp->rq_res.head[0].iov_len = 0;
394 svc_reserve(rqstp, 0);
395 rqstp->rq_sock = NULL;
396
Tom Tuckere1b31572007-12-30 21:07:46 -0600397 svc_xprt_put(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400/*
401 * External function to wake up a server waiting for data
Greg Banks3262c812006-10-02 02:17:58 -0700402 * This really only makes sense for services like lockd
403 * which have exactly one thread anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 */
405void
406svc_wake_up(struct svc_serv *serv)
407{
408 struct svc_rqst *rqstp;
Greg Banks3262c812006-10-02 02:17:58 -0700409 unsigned int i;
410 struct svc_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Greg Banks3262c812006-10-02 02:17:58 -0700412 for (i = 0; i < serv->sv_nrpools; i++) {
413 pool = &serv->sv_pools[i];
414
415 spin_lock_bh(&pool->sp_lock);
416 if (!list_empty(&pool->sp_threads)) {
417 rqstp = list_entry(pool->sp_threads.next,
418 struct svc_rqst,
419 rq_list);
420 dprintk("svc: daemon %p woken up.\n", rqstp);
421 /*
422 svc_thread_dequeue(pool, rqstp);
423 rqstp->rq_sock = NULL;
424 */
425 wake_up(&rqstp->rq_wait);
426 }
427 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
Chuck Leverb92503b2007-02-12 00:53:36 -0800431union svc_pktinfo_u {
432 struct in_pktinfo pkti;
Chuck Leverb92503b2007-02-12 00:53:36 -0800433 struct in6_pktinfo pkti6;
Chuck Leverb92503b2007-02-12 00:53:36 -0800434};
David S. Millerbc375ea2007-04-12 13:35:59 -0700435#define SVC_PKTINFO_SPACE \
436 CMSG_SPACE(sizeof(union svc_pktinfo_u))
Chuck Leverb92503b2007-02-12 00:53:36 -0800437
438static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
439{
440 switch (rqstp->rq_sock->sk_sk->sk_family) {
441 case AF_INET: {
442 struct in_pktinfo *pki = CMSG_DATA(cmh);
443
444 cmh->cmsg_level = SOL_IP;
445 cmh->cmsg_type = IP_PKTINFO;
446 pki->ipi_ifindex = 0;
447 pki->ipi_spec_dst.s_addr = rqstp->rq_daddr.addr.s_addr;
448 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
449 }
450 break;
NeilBrown5a05ed72007-03-06 01:42:22 -0800451
Chuck Leverb92503b2007-02-12 00:53:36 -0800452 case AF_INET6: {
453 struct in6_pktinfo *pki = CMSG_DATA(cmh);
454
455 cmh->cmsg_level = SOL_IPV6;
456 cmh->cmsg_type = IPV6_PKTINFO;
457 pki->ipi6_ifindex = 0;
458 ipv6_addr_copy(&pki->ipi6_addr,
459 &rqstp->rq_daddr.addr6);
460 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
461 }
462 break;
Chuck Leverb92503b2007-02-12 00:53:36 -0800463 }
464 return;
465}
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467/*
468 * Generic sendto routine
469 */
470static int
471svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
472{
473 struct svc_sock *svsk = rqstp->rq_sock;
474 struct socket *sock = svsk->sk_sock;
475 int slen;
David S. Millerbc375ea2007-04-12 13:35:59 -0700476 union {
477 struct cmsghdr hdr;
478 long all[SVC_PKTINFO_SPACE / sizeof(long)];
479 } buffer;
480 struct cmsghdr *cmh = &buffer.hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 int len = 0;
482 int result;
483 int size;
484 struct page **ppage = xdr->pages;
485 size_t base = xdr->page_base;
486 unsigned int pglen = xdr->page_len;
487 unsigned int flags = MSG_MORE;
Chuck Leverad06e4b2007-02-12 00:53:32 -0800488 char buf[RPC_MAX_ADDRBUFLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 slen = xdr->len;
491
492 if (rqstp->rq_prot == IPPROTO_UDP) {
Chuck Leverb92503b2007-02-12 00:53:36 -0800493 struct msghdr msg = {
494 .msg_name = &rqstp->rq_addr,
495 .msg_namelen = rqstp->rq_addrlen,
496 .msg_control = cmh,
497 .msg_controllen = sizeof(buffer),
498 .msg_flags = MSG_MORE,
499 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Chuck Leverb92503b2007-02-12 00:53:36 -0800501 svc_set_cmsg_data(rqstp, cmh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 if (sock_sendmsg(sock, &msg, 0) < 0)
504 goto out;
505 }
506
507 /* send head */
508 if (slen == xdr->head[0].iov_len)
509 flags = 0;
NeilBrown44524352006-10-04 02:15:46 -0700510 len = kernel_sendpage(sock, rqstp->rq_respages[0], 0,
511 xdr->head[0].iov_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (len != xdr->head[0].iov_len)
513 goto out;
514 slen -= xdr->head[0].iov_len;
515 if (slen == 0)
516 goto out;
517
518 /* send page data */
519 size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
520 while (pglen > 0) {
521 if (slen == size)
522 flags = 0;
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700523 result = kernel_sendpage(sock, *ppage, base, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (result > 0)
525 len += result;
526 if (result != size)
527 goto out;
528 slen -= size;
529 pglen -= size;
530 size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
531 base = 0;
532 ppage++;
533 }
534 /* send tail */
535 if (xdr->tail[0].iov_len) {
NeilBrown44524352006-10-04 02:15:46 -0700536 result = kernel_sendpage(sock, rqstp->rq_respages[0],
537 ((unsigned long)xdr->tail[0].iov_base)
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800538 & (PAGE_SIZE-1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 xdr->tail[0].iov_len, 0);
540
541 if (result > 0)
542 len += result;
543 }
544out:
Chuck Leverad06e4b2007-02-12 00:53:32 -0800545 dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %s)\n",
546 rqstp->rq_sock, xdr->head[0].iov_base, xdr->head[0].iov_len,
547 xdr->len, len, svc_print_addr(rqstp, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 return len;
550}
551
552/*
NeilBrown80212d52006-10-02 02:17:47 -0700553 * Report socket names for nfsdfs
554 */
555static int one_sock_name(char *buf, struct svc_sock *svsk)
556{
557 int len;
558
559 switch(svsk->sk_sk->sk_family) {
560 case AF_INET:
561 len = sprintf(buf, "ipv4 %s %u.%u.%u.%u %d\n",
562 svsk->sk_sk->sk_protocol==IPPROTO_UDP?
563 "udp" : "tcp",
564 NIPQUAD(inet_sk(svsk->sk_sk)->rcv_saddr),
565 inet_sk(svsk->sk_sk)->num);
566 break;
567 default:
568 len = sprintf(buf, "*unknown-%d*\n",
569 svsk->sk_sk->sk_family);
570 }
571 return len;
572}
573
574int
NeilBrownb41b66d2006-10-02 02:17:48 -0700575svc_sock_names(char *buf, struct svc_serv *serv, char *toclose)
NeilBrown80212d52006-10-02 02:17:47 -0700576{
NeilBrownb41b66d2006-10-02 02:17:48 -0700577 struct svc_sock *svsk, *closesk = NULL;
NeilBrown80212d52006-10-02 02:17:47 -0700578 int len = 0;
579
580 if (!serv)
581 return 0;
NeilBrownaaf68cf2007-02-08 14:20:30 -0800582 spin_lock_bh(&serv->sv_lock);
Tom Tucker7a182082007-12-30 21:07:53 -0600583 list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list) {
NeilBrown80212d52006-10-02 02:17:47 -0700584 int onelen = one_sock_name(buf+len, svsk);
NeilBrownb41b66d2006-10-02 02:17:48 -0700585 if (toclose && strcmp(toclose, buf+len) == 0)
586 closesk = svsk;
587 else
588 len += onelen;
NeilBrown80212d52006-10-02 02:17:47 -0700589 }
NeilBrownaaf68cf2007-02-08 14:20:30 -0800590 spin_unlock_bh(&serv->sv_lock);
NeilBrownb41b66d2006-10-02 02:17:48 -0700591 if (closesk)
NeilBrown5680c442006-10-04 02:15:45 -0700592 /* Should unregister with portmap, but you cannot
593 * unregister just one protocol...
594 */
Tom Tucker7a182082007-12-30 21:07:53 -0600595 svc_close_xprt(&closesk->sk_xprt);
NeilBrown37a03472006-10-04 02:15:44 -0700596 else if (toclose)
597 return -ENOENT;
NeilBrown80212d52006-10-02 02:17:47 -0700598 return len;
599}
600EXPORT_SYMBOL(svc_sock_names);
601
602/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 * Check input queue length
604 */
605static int
606svc_recv_available(struct svc_sock *svsk)
607{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 struct socket *sock = svsk->sk_sock;
609 int avail, err;
610
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700611 err = kernel_sock_ioctl(sock, TIOCINQ, (unsigned long) &avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 return (err >= 0)? avail : err;
614}
615
616/*
617 * Generic recvfrom routine.
618 */
619static int
620svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr, int buflen)
621{
Chuck Lever067d7812007-02-12 00:53:30 -0800622 struct svc_sock *svsk = rqstp->rq_sock;
Chuck Lever1ba95102007-02-12 00:53:31 -0800623 struct msghdr msg = {
624 .msg_flags = MSG_DONTWAIT,
625 };
626 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Chuck Lever1ba95102007-02-12 00:53:31 -0800628 len = kernel_recvmsg(svsk->sk_sock, &msg, iov, nr, buflen,
629 msg.msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
Chuck Lever1ba95102007-02-12 00:53:31 -0800632 svsk, iov[0].iov_base, iov[0].iov_len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return len;
634}
635
636/*
637 * Set socket snd and rcv buffer lengths
638 */
639static inline void
640svc_sock_setbufsize(struct socket *sock, unsigned int snd, unsigned int rcv)
641{
642#if 0
643 mm_segment_t oldfs;
644 oldfs = get_fs(); set_fs(KERNEL_DS);
645 sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
646 (char*)&snd, sizeof(snd));
647 sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
648 (char*)&rcv, sizeof(rcv));
649#else
650 /* sock_setsockopt limits use to sysctl_?mem_max,
651 * which isn't acceptable. Until that is made conditional
652 * on not having CAP_SYS_RESOURCE or similar, we go direct...
653 * DaveM said I could!
654 */
655 lock_sock(sock->sk);
656 sock->sk->sk_sndbuf = snd * 2;
657 sock->sk->sk_rcvbuf = rcv * 2;
658 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK|SOCK_RCVBUF_LOCK;
659 release_sock(sock->sk);
660#endif
661}
662/*
663 * INET callback when data has been received on the socket.
664 */
665static void
666svc_udp_data_ready(struct sock *sk, int count)
667{
Neil Brown939bb7e2005-09-13 01:25:39 -0700668 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Neil Brown939bb7e2005-09-13 01:25:39 -0700670 if (svsk) {
671 dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
Tom Tucker02fc6c32007-12-30 21:07:48 -0600672 svsk, sk, count,
673 test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
674 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600675 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -0700676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
678 wake_up_interruptible(sk->sk_sleep);
679}
680
681/*
682 * INET callback when space is newly available on the socket.
683 */
684static void
685svc_write_space(struct sock *sk)
686{
687 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
688
689 if (svsk) {
690 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
Tom Tucker02fc6c32007-12-30 21:07:48 -0600691 svsk, sk, test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags));
Tom Tuckerf6150c32007-12-30 21:07:57 -0600692 svc_xprt_enqueue(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694
695 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) {
Neil Brown939bb7e2005-09-13 01:25:39 -0700696 dprintk("RPC svc_write_space: someone sleeping on %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 svsk);
698 wake_up_interruptible(sk->sk_sleep);
699 }
700}
701
Tom Tucker9dbc2402007-12-30 21:08:12 -0600702/*
703 * Copy the UDP datagram's destination address to the rqstp structure.
704 * The 'destination' address in this case is the address to which the
705 * peer sent the datagram, i.e. our local address. For multihomed
706 * hosts, this can change from msg to msg. Note that only the IP
707 * address changes, the port number should remain the same.
708 */
709static void svc_udp_get_dest_address(struct svc_rqst *rqstp,
710 struct cmsghdr *cmh)
Chuck Lever95756482007-02-12 00:53:38 -0800711{
712 switch (rqstp->rq_sock->sk_sk->sk_family) {
713 case AF_INET: {
NeilBrown7a37f572007-03-06 01:42:21 -0800714 struct in_pktinfo *pki = CMSG_DATA(cmh);
715 rqstp->rq_daddr.addr.s_addr = pki->ipi_spec_dst.s_addr;
Chuck Lever95756482007-02-12 00:53:38 -0800716 break;
NeilBrown7a37f572007-03-06 01:42:21 -0800717 }
Chuck Lever95756482007-02-12 00:53:38 -0800718 case AF_INET6: {
NeilBrown7a37f572007-03-06 01:42:21 -0800719 struct in6_pktinfo *pki = CMSG_DATA(cmh);
720 ipv6_addr_copy(&rqstp->rq_daddr.addr6, &pki->ipi6_addr);
Chuck Lever95756482007-02-12 00:53:38 -0800721 break;
NeilBrown7a37f572007-03-06 01:42:21 -0800722 }
Chuck Lever95756482007-02-12 00:53:38 -0800723 }
Chuck Lever95756482007-02-12 00:53:38 -0800724}
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726/*
727 * Receive a datagram from a UDP socket.
728 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729static int
730svc_udp_recvfrom(struct svc_rqst *rqstp)
731{
732 struct svc_sock *svsk = rqstp->rq_sock;
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600733 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 struct sk_buff *skb;
David S. Millerbc375ea2007-04-12 13:35:59 -0700735 union {
736 struct cmsghdr hdr;
737 long all[SVC_PKTINFO_SPACE / sizeof(long)];
738 } buffer;
739 struct cmsghdr *cmh = &buffer.hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 int err, len;
NeilBrown7a37f572007-03-06 01:42:21 -0800741 struct msghdr msg = {
742 .msg_name = svc_addr(rqstp),
743 .msg_control = cmh,
744 .msg_controllen = sizeof(buffer),
745 .msg_flags = MSG_DONTWAIT,
746 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Tom Tucker02fc6c32007-12-30 21:07:48 -0600748 if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 /* udp sockets need large rcvbuf as all pending
750 * requests are still in that buffer. sndbuf must
751 * also be large enough that there is enough space
Greg Banks3262c812006-10-02 02:17:58 -0700752 * for one reply per thread. We count all threads
753 * rather than threads in a particular pool, which
754 * provides an upper bound on the number of threads
755 * which will access the socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 */
757 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700758 (serv->sv_nrthreads+3) * serv->sv_max_mesg,
759 (serv->sv_nrthreads+3) * serv->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Tom Tucker02fc6c32007-12-30 21:07:48 -0600761 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
NeilBrown05ed6902007-05-09 02:34:55 -0700762 skb = NULL;
763 err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
764 0, 0, MSG_PEEK | MSG_DONTWAIT);
765 if (err >= 0)
766 skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err);
767
768 if (skb == NULL) {
769 if (err != -EAGAIN) {
770 /* possibly an icmp error */
771 dprintk("svc: recvfrom returned error %d\n", -err);
Tom Tucker02fc6c32007-12-30 21:07:48 -0600772 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
Tom Tuckera6046f72007-12-30 21:08:01 -0600774 svc_xprt_received(&svsk->sk_xprt);
NeilBrown05ed6902007-05-09 02:34:55 -0700775 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
Tom Tucker9dbc2402007-12-30 21:08:12 -0600777 len = svc_addr_len(svc_addr(rqstp));
778 if (len < 0)
779 return len;
780 rqstp->rq_addrlen = len;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700781 if (skb->tstamp.tv64 == 0) {
782 skb->tstamp = ktime_get_real();
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800783 /* Don't enable netstamp, sunrpc doesn't
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 need that much accuracy */
785 }
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700786 svsk->sk_sk->sk_stamp = skb->tstamp;
Tom Tucker02fc6c32007-12-30 21:07:48 -0600787 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 /*
790 * Maybe more packets - kick another thread ASAP.
791 */
Tom Tuckera6046f72007-12-30 21:08:01 -0600792 svc_xprt_received(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 len = skb->len - sizeof(struct udphdr);
795 rqstp->rq_arg.len = len;
796
Chuck Lever95756482007-02-12 00:53:38 -0800797 rqstp->rq_prot = IPPROTO_UDP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
NeilBrown7a37f572007-03-06 01:42:21 -0800799 if (cmh->cmsg_level != IPPROTO_IP ||
800 cmh->cmsg_type != IP_PKTINFO) {
801 if (net_ratelimit())
802 printk("rpcsvc: received unknown control message:"
803 "%d/%d\n",
804 cmh->cmsg_level, cmh->cmsg_type);
805 skb_free_datagram(svsk->sk_sk, skb);
806 return 0;
807 }
808 svc_udp_get_dest_address(rqstp, cmh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 if (skb_is_nonlinear(skb)) {
811 /* we have to copy */
812 local_bh_disable();
813 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
814 local_bh_enable();
815 /* checksum error */
816 skb_free_datagram(svsk->sk_sk, skb);
817 return 0;
818 }
819 local_bh_enable();
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800820 skb_free_datagram(svsk->sk_sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 } else {
822 /* we can use it in-place */
823 rqstp->rq_arg.head[0].iov_base = skb->data + sizeof(struct udphdr);
824 rqstp->rq_arg.head[0].iov_len = len;
Herbert Xufb286bb2005-11-10 13:01:24 -0800825 if (skb_checksum_complete(skb)) {
826 skb_free_datagram(svsk->sk_sk, skb);
827 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
Tom Tucker5148bf42007-12-30 21:07:25 -0600829 rqstp->rq_xprt_ctxt = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831
832 rqstp->rq_arg.page_base = 0;
833 if (len <= rqstp->rq_arg.head[0].iov_len) {
834 rqstp->rq_arg.head[0].iov_len = len;
835 rqstp->rq_arg.page_len = 0;
NeilBrown44524352006-10-04 02:15:46 -0700836 rqstp->rq_respages = rqstp->rq_pages+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 } else {
838 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
NeilBrown44524352006-10-04 02:15:46 -0700839 rqstp->rq_respages = rqstp->rq_pages + 1 +
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700840 DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
842
843 if (serv->sv_stats)
844 serv->sv_stats->netudpcnt++;
845
846 return len;
847}
848
849static int
850svc_udp_sendto(struct svc_rqst *rqstp)
851{
852 int error;
853
854 error = svc_sendto(rqstp, &rqstp->rq_res);
855 if (error == -ECONNREFUSED)
856 /* ICMP error on earlier request. */
857 error = svc_sendto(rqstp, &rqstp->rq_res);
858
859 return error;
860}
861
Tom Tuckere831fe62007-12-30 21:07:29 -0600862static void svc_udp_prep_reply_hdr(struct svc_rqst *rqstp)
863{
864}
865
Tom Tucker323bee32007-12-30 21:07:31 -0600866static int svc_udp_has_wspace(struct svc_xprt *xprt)
867{
868 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600869 struct svc_serv *serv = xprt->xpt_server;
Tom Tucker323bee32007-12-30 21:07:31 -0600870 unsigned long required;
871
872 /*
873 * Set the SOCK_NOSPACE flag before checking the available
874 * sock space.
875 */
876 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
Tom Tucker7a90e8c2007-12-30 21:07:55 -0600877 required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
Tom Tucker323bee32007-12-30 21:07:31 -0600878 if (required*2 > sock_wspace(svsk->sk_sk))
879 return 0;
880 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
881 return 1;
882}
883
Tom Tucker38a417c2007-12-30 21:07:36 -0600884static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt)
885{
886 BUG();
887 return NULL;
888}
889
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600890static struct svc_xprt *svc_udp_create(struct svc_serv *serv,
891 struct sockaddr *sa, int salen,
892 int flags)
893{
894 return svc_create_socket(serv, IPPROTO_UDP, sa, salen, flags);
895}
896
Tom Tucker360d87382007-12-30 21:07:17 -0600897static struct svc_xprt_ops svc_udp_ops = {
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600898 .xpo_create = svc_udp_create,
Tom Tucker5d137992007-12-30 21:07:23 -0600899 .xpo_recvfrom = svc_udp_recvfrom,
900 .xpo_sendto = svc_udp_sendto,
Tom Tucker5148bf42007-12-30 21:07:25 -0600901 .xpo_release_rqst = svc_release_skb,
Tom Tucker755ccea2007-12-30 21:07:27 -0600902 .xpo_detach = svc_sock_detach,
903 .xpo_free = svc_sock_free,
Tom Tuckere831fe62007-12-30 21:07:29 -0600904 .xpo_prep_reply_hdr = svc_udp_prep_reply_hdr,
Tom Tucker323bee32007-12-30 21:07:31 -0600905 .xpo_has_wspace = svc_udp_has_wspace,
Tom Tucker38a417c2007-12-30 21:07:36 -0600906 .xpo_accept = svc_udp_accept,
Tom Tucker360d87382007-12-30 21:07:17 -0600907};
908
909static struct svc_xprt_class svc_udp_class = {
910 .xcl_name = "udp",
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600911 .xcl_owner = THIS_MODULE,
Tom Tucker360d87382007-12-30 21:07:17 -0600912 .xcl_ops = &svc_udp_ops,
Tom Tucker49023152007-12-30 21:07:21 -0600913 .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP,
Tom Tucker360d87382007-12-30 21:07:17 -0600914};
915
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600916static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
NeilBrown7a37f572007-03-06 01:42:21 -0800918 int one = 1;
919 mm_segment_t oldfs;
920
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600921 svc_xprt_init(&svc_udp_class, &svsk->sk_xprt, serv);
Tom Tuckerdef13d72007-12-30 21:08:08 -0600922 clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 svsk->sk_sk->sk_data_ready = svc_udp_data_ready;
924 svsk->sk_sk->sk_write_space = svc_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 /* initialise setting must have enough space to
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800927 * receive and respond to one request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 * svc_udp_recvfrom will re-adjust if necessary
929 */
930 svc_sock_setbufsize(svsk->sk_sock,
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600931 3 * svsk->sk_xprt.xpt_server->sv_max_mesg,
932 3 * svsk->sk_xprt.xpt_server->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Tom Tucker02fc6c32007-12-30 21:07:48 -0600934 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* might have come in before data_ready set up */
935 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
NeilBrown7a37f572007-03-06 01:42:21 -0800936
937 oldfs = get_fs();
938 set_fs(KERNEL_DS);
939 /* make sure we get destination address info */
940 svsk->sk_sock->ops->setsockopt(svsk->sk_sock, IPPROTO_IP, IP_PKTINFO,
941 (char __user *)&one, sizeof(one));
942 set_fs(oldfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
945/*
946 * A data_ready event on a listening socket means there's a connection
947 * pending. Do not use state_change as a substitute for it.
948 */
949static void
950svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
951{
Neil Brown939bb7e2005-09-13 01:25:39 -0700952 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 dprintk("svc: socket %p TCP (listen) state change %d\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700955 sk, sk->sk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Neil Brown939bb7e2005-09-13 01:25:39 -0700957 /*
958 * This callback may called twice when a new connection
959 * is established as a child socket inherits everything
960 * from a parent LISTEN socket.
961 * 1) data_ready method of the parent socket will be called
962 * when one of child sockets become ESTABLISHED.
963 * 2) data_ready method of the child socket may be called
964 * when it receives data before the socket is accepted.
965 * In case of 2, we should ignore it silently.
966 */
967 if (sk->sk_state == TCP_LISTEN) {
968 if (svsk) {
Tom Tucker02fc6c32007-12-30 21:07:48 -0600969 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600970 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -0700971 } else
972 printk("svc: socket %p: no user data\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
Neil Brown939bb7e2005-09-13 01:25:39 -0700974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
976 wake_up_interruptible_all(sk->sk_sleep);
977}
978
979/*
980 * A state change on a connected socket means it's dying or dead.
981 */
982static void
983svc_tcp_state_change(struct sock *sk)
984{
Neil Brown939bb7e2005-09-13 01:25:39 -0700985 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700988 sk, sk->sk_state, sk->sk_user_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Neil Brown939bb7e2005-09-13 01:25:39 -0700990 if (!svsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 printk("svc: socket %p: no user data\n", sk);
Neil Brown939bb7e2005-09-13 01:25:39 -0700992 else {
Tom Tucker02fc6c32007-12-30 21:07:48 -0600993 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -0600994 svc_xprt_enqueue(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
997 wake_up_interruptible_all(sk->sk_sleep);
998}
999
1000static void
1001svc_tcp_data_ready(struct sock *sk, int count)
1002{
Neil Brown939bb7e2005-09-13 01:25:39 -07001003 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 dprintk("svc: socket %p TCP data ready (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -07001006 sk, sk->sk_user_data);
1007 if (svsk) {
Tom Tucker02fc6c32007-12-30 21:07:48 -06001008 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -06001009 svc_xprt_enqueue(&svsk->sk_xprt);
Neil Brown939bb7e2005-09-13 01:25:39 -07001010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
1012 wake_up_interruptible(sk->sk_sleep);
1013}
1014
Chuck Leverbcdb81a2007-02-12 00:53:37 -08001015static inline int svc_port_is_privileged(struct sockaddr *sin)
1016{
1017 switch (sin->sa_family) {
1018 case AF_INET:
1019 return ntohs(((struct sockaddr_in *)sin)->sin_port)
1020 < PROT_SOCK;
Chuck Leverbcdb81a2007-02-12 00:53:37 -08001021 case AF_INET6:
1022 return ntohs(((struct sockaddr_in6 *)sin)->sin6_port)
1023 < PROT_SOCK;
Chuck Leverbcdb81a2007-02-12 00:53:37 -08001024 default:
1025 return 0;
1026 }
1027}
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029/*
1030 * Accept a TCP connection
1031 */
Tom Tucker38a417c2007-12-30 21:07:36 -06001032static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Tom Tucker38a417c2007-12-30 21:07:36 -06001034 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001035 struct sockaddr_storage addr;
1036 struct sockaddr *sin = (struct sockaddr *) &addr;
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001037 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 struct socket *sock = svsk->sk_sock;
1039 struct socket *newsock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 struct svc_sock *newsvsk;
1041 int err, slen;
Chuck Leverad06e4b2007-02-12 00:53:32 -08001042 char buf[RPC_MAX_ADDRBUFLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
1045 if (!sock)
Tom Tucker38a417c2007-12-30 21:07:36 -06001046 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Tom Tucker02fc6c32007-12-30 21:07:48 -06001048 clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001049 err = kernel_accept(sock, &newsock, O_NONBLOCK);
1050 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (err == -ENOMEM)
1052 printk(KERN_WARNING "%s: no more sockets!\n",
1053 serv->sv_name);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001054 else if (err != -EAGAIN && net_ratelimit())
1055 printk(KERN_WARNING "%s: accept failed (err %d)!\n",
1056 serv->sv_name, -err);
Tom Tucker38a417c2007-12-30 21:07:36 -06001057 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
Tom Tucker02fc6c32007-12-30 21:07:48 -06001059 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001061 err = kernel_getpeername(newsock, sin, &slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (err < 0) {
1063 if (net_ratelimit())
1064 printk(KERN_WARNING "%s: peername failed (err %d)!\n",
1065 serv->sv_name, -err);
1066 goto failed; /* aborted connection or whatever */
1067 }
1068
1069 /* Ideally, we would want to reject connections from unauthorized
Chuck Leverad06e4b2007-02-12 00:53:32 -08001070 * hosts here, but when we get encryption, the IP of the host won't
1071 * tell us anything. For now just warn about unpriv connections.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 */
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001073 if (!svc_port_is_privileged(sin)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 dprintk(KERN_WARNING
Chuck Leverad06e4b2007-02-12 00:53:32 -08001075 "%s: connect from unprivileged port: %s\n",
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001076 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 }
Chuck Leverad06e4b2007-02-12 00:53:32 -08001079 dprintk("%s: connect from %s\n", serv->sv_name,
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001080 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 /* make sure that a write doesn't block forever when
1083 * low on memory
1084 */
1085 newsock->sk->sk_sndtimeo = HZ*30;
1086
Chuck Lever6b174332007-02-12 00:53:28 -08001087 if (!(newsvsk = svc_setup_socket(serv, newsock, &err,
1088 (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 goto failed;
Tom Tucker9dbc2402007-12-30 21:08:12 -06001090 svc_xprt_set_remote(&newsvsk->sk_xprt, sin, slen);
Frank van Maarseveena9747692007-07-09 22:21:39 +02001091 err = kernel_getsockname(newsock, sin, &slen);
1092 if (unlikely(err < 0)) {
1093 dprintk("svc_tcp_accept: kernel_getsockname error %d\n", -err);
1094 slen = offsetof(struct sockaddr, sa_data);
1095 }
Tom Tucker9dbc2402007-12-30 21:08:12 -06001096 svc_xprt_set_local(&newsvsk->sk_xprt, sin, slen);
Chuck Lever067d7812007-02-12 00:53:30 -08001097
Tom Tuckerf9f3cc42007-12-30 21:07:40 -06001098 if (serv->sv_stats)
1099 serv->sv_stats->nettcpconn++;
1100
1101 return &newsvsk->sk_xprt;
1102
1103failed:
1104 sock_release(newsock);
1105 return NULL;
1106}
1107
1108/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 * Receive data from a TCP socket.
1110 */
1111static int
1112svc_tcp_recvfrom(struct svc_rqst *rqstp)
1113{
1114 struct svc_sock *svsk = rqstp->rq_sock;
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001115 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 int len;
NeilBrown3cc03b12006-10-04 02:15:47 -07001117 struct kvec *vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 int pnum, vlen;
1119
1120 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
Tom Tucker02fc6c32007-12-30 21:07:48 -06001121 svsk, test_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags),
1122 test_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags),
1123 test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Tom Tucker02fc6c32007-12-30 21:07:48 -06001125 if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 /* sndbuf needs to have room for one request
1127 * per thread, otherwise we can stall even when the
1128 * network isn't a bottleneck.
Greg Banks3262c812006-10-02 02:17:58 -07001129 *
1130 * We count all threads rather than threads in a
1131 * particular pool, which provides an upper bound
1132 * on the number of threads which will access the socket.
1133 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 * rcvbuf just needs to be able to hold a few requests.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001135 * Normally they will be removed from the queue
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 * as soon a a complete request arrives.
1137 */
1138 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001139 (serv->sv_nrthreads+3) * serv->sv_max_mesg,
1140 3 * serv->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Tom Tucker02fc6c32007-12-30 21:07:48 -06001142 clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 /* Receive data. If we haven't got the record length yet, get
1145 * the next four bytes. Otherwise try to gobble up as much as
1146 * possible up to the complete record length.
1147 */
1148 if (svsk->sk_tcplen < 4) {
1149 unsigned long want = 4 - svsk->sk_tcplen;
1150 struct kvec iov;
1151
1152 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
1153 iov.iov_len = want;
1154 if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
1155 goto error;
1156 svsk->sk_tcplen += len;
1157
1158 if (len < want) {
1159 dprintk("svc: short recvfrom while reading record length (%d of %lu)\n",
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001160 len, want);
Tom Tuckera6046f72007-12-30 21:08:01 -06001161 svc_xprt_received(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return -EAGAIN; /* record header not complete */
1163 }
1164
1165 svsk->sk_reclen = ntohl(svsk->sk_reclen);
1166 if (!(svsk->sk_reclen & 0x80000000)) {
1167 /* FIXME: technically, a record can be fragmented,
1168 * and non-terminal fragments will not have the top
1169 * bit set in the fragment length header.
1170 * But apparently no known nfs clients send fragmented
1171 * records. */
NeilBrown34e9a632007-01-29 13:19:52 -08001172 if (net_ratelimit())
1173 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx"
1174 " (non-terminal)\n",
1175 (unsigned long) svsk->sk_reclen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 goto err_delete;
1177 }
1178 svsk->sk_reclen &= 0x7fffffff;
1179 dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001180 if (svsk->sk_reclen > serv->sv_max_mesg) {
NeilBrown34e9a632007-01-29 13:19:52 -08001181 if (net_ratelimit())
1182 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx"
1183 " (large)\n",
1184 (unsigned long) svsk->sk_reclen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 goto err_delete;
1186 }
1187 }
1188
1189 /* Check whether enough data is available */
1190 len = svc_recv_available(svsk);
1191 if (len < 0)
1192 goto error;
1193
1194 if (len < svsk->sk_reclen) {
1195 dprintk("svc: incomplete TCP record (%d of %d)\n",
1196 len, svsk->sk_reclen);
Tom Tuckera6046f72007-12-30 21:08:01 -06001197 svc_xprt_received(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return -EAGAIN; /* record not complete */
1199 }
1200 len = svsk->sk_reclen;
Tom Tucker02fc6c32007-12-30 21:07:48 -06001201 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
NeilBrown3cc03b12006-10-04 02:15:47 -07001203 vec = rqstp->rq_vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 vec[0] = rqstp->rq_arg.head[0];
1205 vlen = PAGE_SIZE;
1206 pnum = 1;
1207 while (vlen < len) {
NeilBrown44524352006-10-04 02:15:46 -07001208 vec[pnum].iov_base = page_address(rqstp->rq_pages[pnum]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 vec[pnum].iov_len = PAGE_SIZE;
1210 pnum++;
1211 vlen += PAGE_SIZE;
1212 }
NeilBrown44524352006-10-04 02:15:46 -07001213 rqstp->rq_respages = &rqstp->rq_pages[pnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 /* Now receive data */
1216 len = svc_recvfrom(rqstp, vec, pnum, len);
1217 if (len < 0)
1218 goto error;
1219
1220 dprintk("svc: TCP complete record (%d bytes)\n", len);
1221 rqstp->rq_arg.len = len;
1222 rqstp->rq_arg.page_base = 0;
1223 if (len <= rqstp->rq_arg.head[0].iov_len) {
1224 rqstp->rq_arg.head[0].iov_len = len;
1225 rqstp->rq_arg.page_len = 0;
1226 } else {
1227 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
1228 }
1229
Tom Tucker5148bf42007-12-30 21:07:25 -06001230 rqstp->rq_xprt_ctxt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 rqstp->rq_prot = IPPROTO_TCP;
1232
1233 /* Reset TCP read info */
1234 svsk->sk_reclen = 0;
1235 svsk->sk_tcplen = 0;
1236
Tom Tucker9dbc2402007-12-30 21:08:12 -06001237 svc_xprt_copy_addrs(rqstp, &svsk->sk_xprt);
Tom Tuckera6046f72007-12-30 21:08:01 -06001238 svc_xprt_received(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (serv->sv_stats)
1240 serv->sv_stats->nettcpcnt++;
1241
1242 return len;
1243
1244 err_delete:
Tom Tucker02fc6c32007-12-30 21:07:48 -06001245 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return -EAGAIN;
1247
1248 error:
1249 if (len == -EAGAIN) {
1250 dprintk("RPC: TCP recvfrom got EAGAIN\n");
Tom Tuckera6046f72007-12-30 21:08:01 -06001251 svc_xprt_received(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 } else {
1253 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001254 svsk->sk_xprt.xpt_server->sv_name, -len);
Olaf Kirch93fbf1a2006-01-06 00:19:56 -08001255 goto err_delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257
1258 return len;
1259}
1260
1261/*
1262 * Send out data on TCP socket.
1263 */
1264static int
1265svc_tcp_sendto(struct svc_rqst *rqstp)
1266{
1267 struct xdr_buf *xbufp = &rqstp->rq_res;
1268 int sent;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001269 __be32 reclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 /* Set up the first element of the reply kvec.
1272 * Any other kvecs that may be in use have been taken
1273 * care of by the server implementation itself.
1274 */
1275 reclen = htonl(0x80000000|((xbufp->len ) - 4));
1276 memcpy(xbufp->head[0].iov_base, &reclen, 4);
1277
Tom Tucker02fc6c32007-12-30 21:07:48 -06001278 if (test_bit(XPT_DEAD, &rqstp->rq_sock->sk_xprt.xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 return -ENOTCONN;
1280
1281 sent = svc_sendto(rqstp, &rqstp->rq_res);
1282 if (sent != xbufp->len) {
1283 printk(KERN_NOTICE "rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n",
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001284 rqstp->rq_sock->sk_xprt.xpt_server->sv_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 (sent<0)?"got error":"sent only",
1286 sent, xbufp->len);
Tom Tucker02fc6c32007-12-30 21:07:48 -06001287 set_bit(XPT_CLOSE, &rqstp->rq_sock->sk_xprt.xpt_flags);
Tom Tuckerf6150c32007-12-30 21:07:57 -06001288 svc_xprt_enqueue(rqstp->rq_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 sent = -EAGAIN;
1290 }
1291 return sent;
1292}
1293
Tom Tuckere831fe62007-12-30 21:07:29 -06001294/*
1295 * Setup response header. TCP has a 4B record length field.
1296 */
1297static void svc_tcp_prep_reply_hdr(struct svc_rqst *rqstp)
1298{
1299 struct kvec *resv = &rqstp->rq_res.head[0];
1300
1301 /* tcp needs a space for the record length... */
1302 svc_putnl(resv, 0);
1303}
1304
Tom Tucker323bee32007-12-30 21:07:31 -06001305static int svc_tcp_has_wspace(struct svc_xprt *xprt)
1306{
1307 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001308 struct svc_serv *serv = svsk->sk_xprt.xpt_server;
Tom Tucker323bee32007-12-30 21:07:31 -06001309 int required;
1310 int wspace;
1311
1312 /*
1313 * Set the SOCK_NOSPACE flag before checking the available
1314 * sock space.
1315 */
1316 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
Tom Tucker7a90e8c2007-12-30 21:07:55 -06001317 required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg;
Tom Tucker323bee32007-12-30 21:07:31 -06001318 wspace = sk_stream_wspace(svsk->sk_sk);
1319
1320 if (wspace < sk_stream_min_wspace(svsk->sk_sk))
1321 return 0;
1322 if (required * 2 > wspace)
1323 return 0;
1324
1325 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
1326 return 1;
1327}
1328
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001329static struct svc_xprt *svc_tcp_create(struct svc_serv *serv,
1330 struct sockaddr *sa, int salen,
1331 int flags)
1332{
1333 return svc_create_socket(serv, IPPROTO_TCP, sa, salen, flags);
1334}
1335
Tom Tucker360d87382007-12-30 21:07:17 -06001336static struct svc_xprt_ops svc_tcp_ops = {
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001337 .xpo_create = svc_tcp_create,
Tom Tucker5d137992007-12-30 21:07:23 -06001338 .xpo_recvfrom = svc_tcp_recvfrom,
1339 .xpo_sendto = svc_tcp_sendto,
Tom Tucker5148bf42007-12-30 21:07:25 -06001340 .xpo_release_rqst = svc_release_skb,
Tom Tucker755ccea2007-12-30 21:07:27 -06001341 .xpo_detach = svc_sock_detach,
1342 .xpo_free = svc_sock_free,
Tom Tuckere831fe62007-12-30 21:07:29 -06001343 .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr,
Tom Tucker323bee32007-12-30 21:07:31 -06001344 .xpo_has_wspace = svc_tcp_has_wspace,
Tom Tucker38a417c2007-12-30 21:07:36 -06001345 .xpo_accept = svc_tcp_accept,
Tom Tucker360d87382007-12-30 21:07:17 -06001346};
1347
1348static struct svc_xprt_class svc_tcp_class = {
1349 .xcl_name = "tcp",
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001350 .xcl_owner = THIS_MODULE,
Tom Tucker360d87382007-12-30 21:07:17 -06001351 .xcl_ops = &svc_tcp_ops,
Tom Tucker49023152007-12-30 21:07:21 -06001352 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
Tom Tucker360d87382007-12-30 21:07:17 -06001353};
1354
1355void svc_init_xprt_sock(void)
1356{
1357 svc_reg_xprt_class(&svc_tcp_class);
1358 svc_reg_xprt_class(&svc_udp_class);
1359}
1360
1361void svc_cleanup_xprt_sock(void)
1362{
1363 svc_unreg_xprt_class(&svc_tcp_class);
1364 svc_unreg_xprt_class(&svc_udp_class);
1365}
1366
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001367static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
1369 struct sock *sk = svsk->sk_sk;
1370 struct tcp_sock *tp = tcp_sk(sk);
1371
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001372 svc_xprt_init(&svc_tcp_class, &svsk->sk_xprt, serv);
Tom Tuckerdef13d72007-12-30 21:08:08 -06001373 set_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if (sk->sk_state == TCP_LISTEN) {
1375 dprintk("setting up TCP socket for listening\n");
Tom Tucker02fc6c32007-12-30 21:07:48 -06001376 set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 sk->sk_data_ready = svc_tcp_listen_data_ready;
Tom Tucker02fc6c32007-12-30 21:07:48 -06001378 set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 } else {
1380 dprintk("setting up TCP socket for reading\n");
1381 sk->sk_state_change = svc_tcp_state_change;
1382 sk->sk_data_ready = svc_tcp_data_ready;
1383 sk->sk_write_space = svc_write_space;
1384
1385 svsk->sk_reclen = 0;
1386 svsk->sk_tcplen = 0;
1387
1388 tp->nonagle = 1; /* disable Nagle's algorithm */
1389
1390 /* initialise setting must have enough space to
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001391 * receive and respond to one request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 * svc_tcp_recvfrom will re-adjust if necessary
1393 */
1394 svc_sock_setbufsize(svsk->sk_sock,
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001395 3 * svsk->sk_xprt.xpt_server->sv_max_mesg,
1396 3 * svsk->sk_xprt.xpt_server->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Tom Tucker02fc6c32007-12-30 21:07:48 -06001398 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
1399 set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001400 if (sk->sk_state != TCP_ESTABLISHED)
Tom Tucker02fc6c32007-12-30 21:07:48 -06001401 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403}
1404
1405void
1406svc_sock_update_bufs(struct svc_serv *serv)
1407{
1408 /*
1409 * The number of server threads has changed. Update
1410 * rcvbuf and sndbuf accordingly on all sockets
1411 */
1412 struct list_head *le;
1413
1414 spin_lock_bh(&serv->sv_lock);
1415 list_for_each(le, &serv->sv_permsocks) {
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001416 struct svc_sock *svsk =
Tom Tucker7a182082007-12-30 21:07:53 -06001417 list_entry(le, struct svc_sock, sk_xprt.xpt_list);
Tom Tucker02fc6c32007-12-30 21:07:48 -06001418 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
1420 list_for_each(le, &serv->sv_tempsocks) {
1421 struct svc_sock *svsk =
Tom Tucker7a182082007-12-30 21:07:53 -06001422 list_entry(le, struct svc_sock, sk_xprt.xpt_list);
Tom Tucker02fc6c32007-12-30 21:07:48 -06001423 set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
1425 spin_unlock_bh(&serv->sv_lock);
1426}
1427
1428/*
Tom Tuckere1b31572007-12-30 21:07:46 -06001429 * Make sure that we don't have too many active connections. If we
1430 * have, something must be dropped.
1431 *
1432 * There's no point in trying to do random drop here for DoS
1433 * prevention. The NFS clients does 1 reconnect in 15 seconds. An
1434 * attacker can easily beat that.
1435 *
1436 * The only somewhat efficient mechanism would be if drop old
1437 * connections from the same IP first. But right now we don't even
1438 * record the client IP in svc_sock.
1439 */
1440static void svc_check_conn_limits(struct svc_serv *serv)
1441{
1442 if (serv->sv_tmpcnt > (serv->sv_nrthreads+3)*20) {
1443 struct svc_sock *svsk = NULL;
1444 spin_lock_bh(&serv->sv_lock);
1445 if (!list_empty(&serv->sv_tempsocks)) {
1446 if (net_ratelimit()) {
1447 /* Try to help the admin */
1448 printk(KERN_NOTICE "%s: too many open TCP "
1449 "sockets, consider increasing the "
1450 "number of nfsd threads\n",
1451 serv->sv_name);
1452 }
1453 /*
1454 * Always select the oldest socket. It's not fair,
1455 * but so is life
1456 */
1457 svsk = list_entry(serv->sv_tempsocks.prev,
1458 struct svc_sock,
Tom Tucker7a182082007-12-30 21:07:53 -06001459 sk_xprt.xpt_list);
Tom Tucker02fc6c32007-12-30 21:07:48 -06001460 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
Tom Tuckere1b31572007-12-30 21:07:46 -06001461 svc_xprt_get(&svsk->sk_xprt);
1462 }
1463 spin_unlock_bh(&serv->sv_lock);
1464
1465 if (svsk) {
Tom Tuckerf6150c32007-12-30 21:07:57 -06001466 svc_xprt_enqueue(&svsk->sk_xprt);
Tom Tuckere1b31572007-12-30 21:07:46 -06001467 svc_xprt_put(&svsk->sk_xprt);
1468 }
1469 }
1470}
1471
1472/*
Greg Banks3262c812006-10-02 02:17:58 -07001473 * Receive the next request on any socket. This code is carefully
1474 * organised not to touch any cachelines in the shared svc_serv
1475 * structure, only cachelines in the local svc_pool.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 */
1477int
NeilBrown6fb2b472006-10-02 02:17:50 -07001478svc_recv(struct svc_rqst *rqstp, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
Chuck Lever27459f02007-02-12 00:53:34 -08001480 struct svc_sock *svsk = NULL;
NeilBrown6fb2b472006-10-02 02:17:50 -07001481 struct svc_serv *serv = rqstp->rq_server;
Greg Banks3262c812006-10-02 02:17:58 -07001482 struct svc_pool *pool = rqstp->rq_pool;
NeilBrown44524352006-10-04 02:15:46 -07001483 int len, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 int pages;
1485 struct xdr_buf *arg;
1486 DECLARE_WAITQUEUE(wait, current);
1487
1488 dprintk("svc: server %p waiting for data (to = %ld)\n",
1489 rqstp, timeout);
1490
1491 if (rqstp->rq_sock)
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001492 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 "svc_recv: service %p, socket not NULL!\n",
1494 rqstp);
1495 if (waitqueue_active(&rqstp->rq_wait))
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001496 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 "svc_recv: service %p, wait queue active!\n",
1498 rqstp);
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 /* now allocate needed pages. If we get a failure, sleep briefly */
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001502 pages = (serv->sv_max_mesg + PAGE_SIZE) / PAGE_SIZE;
NeilBrown44524352006-10-04 02:15:46 -07001503 for (i=0; i < pages ; i++)
1504 while (rqstp->rq_pages[i] == NULL) {
1505 struct page *p = alloc_page(GFP_KERNEL);
1506 if (!p)
1507 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
1508 rqstp->rq_pages[i] = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 }
NeilBrown250f3912007-01-26 00:56:59 -08001510 rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */
1511 BUG_ON(pages >= RPCSVC_MAXPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 /* Make arg->head point to first page and arg->pages point to rest */
1514 arg = &rqstp->rq_arg;
NeilBrown44524352006-10-04 02:15:46 -07001515 arg->head[0].iov_base = page_address(rqstp->rq_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 arg->head[0].iov_len = PAGE_SIZE;
NeilBrown44524352006-10-04 02:15:46 -07001517 arg->pages = rqstp->rq_pages + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 arg->page_base = 0;
1519 /* save at least one page for response */
1520 arg->page_len = (pages-2)*PAGE_SIZE;
1521 arg->len = (pages-1)*PAGE_SIZE;
1522 arg->tail[0].iov_len = 0;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001523
1524 try_to_freeze();
NeilBrown1887b932005-11-15 00:09:10 -08001525 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (signalled())
1527 return -EINTR;
1528
Greg Banks3262c812006-10-02 02:17:58 -07001529 spin_lock_bh(&pool->sp_lock);
1530 if ((svsk = svc_sock_dequeue(pool)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 rqstp->rq_sock = svsk;
Tom Tuckere1b31572007-12-30 21:07:46 -06001532 svc_xprt_get(&svsk->sk_xprt);
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001533 rqstp->rq_reserved = serv->sv_max_mesg;
Tom Tucker7a90e8c2007-12-30 21:07:55 -06001534 atomic_add(rqstp->rq_reserved, &svsk->sk_xprt.xpt_reserved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 } else {
1536 /* No data pending. Go to sleep */
Greg Banks3262c812006-10-02 02:17:58 -07001537 svc_thread_enqueue(pool, rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 /*
1540 * We have to be able to interrupt this wait
1541 * to bring down the daemons ...
1542 */
1543 set_current_state(TASK_INTERRUPTIBLE);
1544 add_wait_queue(&rqstp->rq_wait, &wait);
Greg Banks3262c812006-10-02 02:17:58 -07001545 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547 schedule_timeout(timeout);
1548
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001549 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Greg Banks3262c812006-10-02 02:17:58 -07001551 spin_lock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 remove_wait_queue(&rqstp->rq_wait, &wait);
1553
1554 if (!(svsk = rqstp->rq_sock)) {
Greg Banks3262c812006-10-02 02:17:58 -07001555 svc_thread_dequeue(pool, rqstp);
1556 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 dprintk("svc: server %p, no data yet\n", rqstp);
1558 return signalled()? -EINTR : -EAGAIN;
1559 }
1560 }
Greg Banks3262c812006-10-02 02:17:58 -07001561 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Tom Tuckerd7979ae2007-12-30 21:07:34 -06001563 len = 0;
Tom Tucker02fc6c32007-12-30 21:07:48 -06001564 if (test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags)) {
1565 dprintk("svc_recv: found XPT_CLOSE\n");
Tom Tucker7a182082007-12-30 21:07:53 -06001566 svc_delete_xprt(&svsk->sk_xprt);
Tom Tucker02fc6c32007-12-30 21:07:48 -06001567 } else if (test_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags)) {
Tom Tucker38a417c2007-12-30 21:07:36 -06001568 struct svc_xprt *newxpt;
1569 newxpt = svsk->sk_xprt.xpt_ops->xpo_accept(&svsk->sk_xprt);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001570 if (newxpt) {
1571 /*
1572 * We know this module_get will succeed because the
1573 * listener holds a reference too
1574 */
1575 __module_get(newxpt->xpt_class->xcl_owner);
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001576 svc_check_conn_limits(svsk->sk_xprt.xpt_server);
Tom Tucker6bc5ab12007-12-30 21:08:03 -06001577 svc_xprt_received(newxpt);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001578 }
Tom Tuckera6046f72007-12-30 21:08:01 -06001579 svc_xprt_received(&svsk->sk_xprt);
Tom Tuckerd7979ae2007-12-30 21:07:34 -06001580 } else {
1581 dprintk("svc: server %p, pool %u, socket %p, inuse=%d\n",
Tom Tuckere1b31572007-12-30 21:07:46 -06001582 rqstp, pool->sp_id, svsk,
1583 atomic_read(&svsk->sk_xprt.xpt_ref.refcount));
Tom Tucker8c7b0172007-12-30 21:08:10 -06001584 rqstp->rq_deferred = svc_deferred_dequeue(&svsk->sk_xprt);
1585 if (rqstp->rq_deferred) {
1586 svc_xprt_received(&svsk->sk_xprt);
1587 len = svc_deferred_recv(rqstp);
1588 } else
1589 len = svsk->sk_xprt.xpt_ops->xpo_recvfrom(rqstp);
Tom Tuckerd7979ae2007-12-30 21:07:34 -06001590 dprintk("svc: got len=%d\n", len);
1591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
1593 /* No data, incomplete (TCP) read, or accept() */
1594 if (len == 0 || len == -EAGAIN) {
1595 rqstp->rq_res.len = 0;
1596 svc_sock_release(rqstp);
1597 return -EAGAIN;
1598 }
Tom Tucker02fc6c32007-12-30 21:07:48 -06001599 clear_bit(XPT_OLD, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Chuck Leverbcdb81a2007-02-12 00:53:37 -08001601 rqstp->rq_secure = svc_port_is_privileged(svc_addr(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 rqstp->rq_chandle.defer = svc_defer;
1603
1604 if (serv->sv_stats)
1605 serv->sv_stats->netcnt++;
1606 return len;
1607}
1608
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001609/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 * Drop request
1611 */
1612void
1613svc_drop(struct svc_rqst *rqstp)
1614{
1615 dprintk("svc: socket %p dropped request\n", rqstp->rq_sock);
1616 svc_sock_release(rqstp);
1617}
1618
1619/*
1620 * Return reply to client.
1621 */
1622int
1623svc_send(struct svc_rqst *rqstp)
1624{
Tom Tuckera50fea22007-12-30 21:07:59 -06001625 struct svc_xprt *xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 int len;
1627 struct xdr_buf *xb;
1628
Tom Tuckera50fea22007-12-30 21:07:59 -06001629 xprt = rqstp->rq_xprt;
1630 if (!xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 /* release the receive skb before sending the reply */
Tom Tucker5148bf42007-12-30 21:07:25 -06001634 rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 /* calculate over-all length */
1637 xb = & rqstp->rq_res;
1638 xb->len = xb->head[0].iov_len +
1639 xb->page_len +
1640 xb->tail[0].iov_len;
1641
Tom Tuckera50fea22007-12-30 21:07:59 -06001642 /* Grab mutex to serialize outgoing data. */
1643 mutex_lock(&xprt->xpt_mutex);
1644 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 len = -ENOTCONN;
1646 else
Tom Tuckera50fea22007-12-30 21:07:59 -06001647 len = xprt->xpt_ops->xpo_sendto(rqstp);
1648 mutex_unlock(&xprt->xpt_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 svc_sock_release(rqstp);
1650
1651 if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
1652 return 0;
1653 return len;
1654}
1655
1656/*
Greg Banks36bdfc82006-10-02 02:17:54 -07001657 * Timer function to close old temporary sockets, using
1658 * a mark-and-sweep algorithm.
1659 */
1660static void
1661svc_age_temp_sockets(unsigned long closure)
1662{
1663 struct svc_serv *serv = (struct svc_serv *)closure;
1664 struct svc_sock *svsk;
1665 struct list_head *le, *next;
1666 LIST_HEAD(to_be_aged);
1667
1668 dprintk("svc_age_temp_sockets\n");
1669
1670 if (!spin_trylock_bh(&serv->sv_lock)) {
1671 /* busy, try again 1 sec later */
1672 dprintk("svc_age_temp_sockets: busy\n");
1673 mod_timer(&serv->sv_temptimer, jiffies + HZ);
1674 return;
1675 }
1676
1677 list_for_each_safe(le, next, &serv->sv_tempsocks) {
Tom Tucker7a182082007-12-30 21:07:53 -06001678 svsk = list_entry(le, struct svc_sock, sk_xprt.xpt_list);
Greg Banks36bdfc82006-10-02 02:17:54 -07001679
Tom Tucker02fc6c32007-12-30 21:07:48 -06001680 if (!test_and_set_bit(XPT_OLD, &svsk->sk_xprt.xpt_flags))
Greg Banks36bdfc82006-10-02 02:17:54 -07001681 continue;
Tom Tuckere1b31572007-12-30 21:07:46 -06001682 if (atomic_read(&svsk->sk_xprt.xpt_ref.refcount) > 1
Tom Tucker02fc6c32007-12-30 21:07:48 -06001683 || test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags))
Greg Banks36bdfc82006-10-02 02:17:54 -07001684 continue;
Tom Tuckere1b31572007-12-30 21:07:46 -06001685 svc_xprt_get(&svsk->sk_xprt);
Greg Banks36bdfc82006-10-02 02:17:54 -07001686 list_move(le, &to_be_aged);
Tom Tucker02fc6c32007-12-30 21:07:48 -06001687 set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags);
1688 set_bit(XPT_DETACHED, &svsk->sk_xprt.xpt_flags);
Greg Banks36bdfc82006-10-02 02:17:54 -07001689 }
1690 spin_unlock_bh(&serv->sv_lock);
1691
1692 while (!list_empty(&to_be_aged)) {
1693 le = to_be_aged.next;
Tom Tucker7a182082007-12-30 21:07:53 -06001694 /* fiddling the sk_xprt.xpt_list node is safe 'cos we're XPT_DETACHED */
Greg Banks36bdfc82006-10-02 02:17:54 -07001695 list_del_init(le);
Tom Tucker7a182082007-12-30 21:07:53 -06001696 svsk = list_entry(le, struct svc_sock, sk_xprt.xpt_list);
Greg Banks36bdfc82006-10-02 02:17:54 -07001697
Tom Tucker4bc6c492007-12-30 21:08:05 -06001698 dprintk("queuing svsk %p for closing\n", svsk);
Greg Banks36bdfc82006-10-02 02:17:54 -07001699
1700 /* a thread will dequeue and close it soon */
Tom Tuckerf6150c32007-12-30 21:07:57 -06001701 svc_xprt_enqueue(&svsk->sk_xprt);
Tom Tuckere1b31572007-12-30 21:07:46 -06001702 svc_xprt_put(&svsk->sk_xprt);
Greg Banks36bdfc82006-10-02 02:17:54 -07001703 }
1704
1705 mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
1706}
1707
1708/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 * Initialize socket for RPC use and create svc_sock struct
1710 * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
1711 */
Chuck Lever6b174332007-02-12 00:53:28 -08001712static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1713 struct socket *sock,
1714 int *errp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
1716 struct svc_sock *svsk;
1717 struct sock *inet;
Chuck Lever6b174332007-02-12 00:53:28 -08001718 int pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
1719 int is_temporary = flags & SVC_SOCK_TEMPORARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721 dprintk("svc: svc_setup_socket %p\n", sock);
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001722 if (!(svsk = kzalloc(sizeof(*svsk), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 *errp = -ENOMEM;
1724 return NULL;
1725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 inet = sock->sk;
1728
1729 /* Register socket with portmapper */
1730 if (*errp >= 0 && pmap_register)
1731 *errp = svc_register(serv, inet->sk_protocol,
1732 ntohs(inet_sk(inet)->sport));
1733
1734 if (*errp < 0) {
1735 kfree(svsk);
1736 return NULL;
1737 }
1738
Tom Tucker02fc6c32007-12-30 21:07:48 -06001739 set_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 inet->sk_user_data = svsk;
1741 svsk->sk_sock = sock;
1742 svsk->sk_sk = inet;
1743 svsk->sk_ostate = inet->sk_state_change;
1744 svsk->sk_odata = inet->sk_data_ready;
1745 svsk->sk_owspace = inet->sk_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 /* Initialize the socket */
1748 if (sock->type == SOCK_DGRAM)
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001749 svc_udp_init(svsk, serv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 else
Tom Tuckerbb5cf162007-12-30 21:07:50 -06001751 svc_tcp_init(svsk, serv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 spin_lock_bh(&serv->sv_lock);
Chuck Lever6b174332007-02-12 00:53:28 -08001754 if (is_temporary) {
Tom Tucker02fc6c32007-12-30 21:07:48 -06001755 set_bit(XPT_TEMP, &svsk->sk_xprt.xpt_flags);
Tom Tucker7a182082007-12-30 21:07:53 -06001756 list_add(&svsk->sk_xprt.xpt_list, &serv->sv_tempsocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 serv->sv_tmpcnt++;
Greg Banks36bdfc82006-10-02 02:17:54 -07001758 if (serv->sv_temptimer.function == NULL) {
1759 /* setup timer to age temp sockets */
1760 setup_timer(&serv->sv_temptimer, svc_age_temp_sockets,
1761 (unsigned long)serv);
1762 mod_timer(&serv->sv_temptimer,
1763 jiffies + svc_conn_age_period * HZ);
1764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 } else {
Tom Tucker02fc6c32007-12-30 21:07:48 -06001766 clear_bit(XPT_TEMP, &svsk->sk_xprt.xpt_flags);
Tom Tucker7a182082007-12-30 21:07:53 -06001767 list_add(&svsk->sk_xprt.xpt_list, &serv->sv_permsocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 }
1769 spin_unlock_bh(&serv->sv_lock);
1770
1771 dprintk("svc: svc_setup_socket created %p (inet %p)\n",
1772 svsk, svsk->sk_sk);
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 return svsk;
1775}
1776
NeilBrownb41b66d2006-10-02 02:17:48 -07001777int svc_addsock(struct svc_serv *serv,
1778 int fd,
1779 char *name_return,
1780 int *proto)
1781{
1782 int err = 0;
1783 struct socket *so = sockfd_lookup(fd, &err);
1784 struct svc_sock *svsk = NULL;
1785
1786 if (!so)
1787 return err;
1788 if (so->sk->sk_family != AF_INET)
1789 err = -EAFNOSUPPORT;
1790 else if (so->sk->sk_protocol != IPPROTO_TCP &&
1791 so->sk->sk_protocol != IPPROTO_UDP)
1792 err = -EPROTONOSUPPORT;
1793 else if (so->state > SS_UNCONNECTED)
1794 err = -EISCONN;
1795 else {
Chuck Lever6b174332007-02-12 00:53:28 -08001796 svsk = svc_setup_socket(serv, so, &err, SVC_SOCK_DEFAULTS);
NeilBrowne79eff12007-02-12 00:53:30 -08001797 if (svsk) {
Tom Tucker9dbc2402007-12-30 21:08:12 -06001798 struct sockaddr_storage addr;
1799 struct sockaddr *sin = (struct sockaddr *)&addr;
1800 int salen;
1801 if (kernel_getsockname(svsk->sk_sock, sin, &salen) == 0)
1802 svc_xprt_set_local(&svsk->sk_xprt, sin, salen);
Tom Tuckera6046f72007-12-30 21:08:01 -06001803 svc_xprt_received(&svsk->sk_xprt);
NeilBrownb41b66d2006-10-02 02:17:48 -07001804 err = 0;
NeilBrowne79eff12007-02-12 00:53:30 -08001805 }
NeilBrownb41b66d2006-10-02 02:17:48 -07001806 }
1807 if (err) {
1808 sockfd_put(so);
1809 return err;
1810 }
1811 if (proto) *proto = so->sk->sk_protocol;
1812 return one_sock_name(name_return, svsk);
1813}
1814EXPORT_SYMBOL_GPL(svc_addsock);
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816/*
1817 * Create socket for RPC service.
1818 */
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001819static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
1820 int protocol,
1821 struct sockaddr *sin, int len,
1822 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
1824 struct svc_sock *svsk;
1825 struct socket *sock;
1826 int error;
1827 int type;
Chuck Leverad06e4b2007-02-12 00:53:32 -08001828 char buf[RPC_MAX_ADDRBUFLEN];
Tom Tucker9dbc2402007-12-30 21:08:12 -06001829 struct sockaddr_storage addr;
1830 struct sockaddr *newsin = (struct sockaddr *)&addr;
1831 int newlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Chuck Leverad06e4b2007-02-12 00:53:32 -08001833 dprintk("svc: svc_create_socket(%s, %d, %s)\n",
1834 serv->sv_program->pg_name, protocol,
Chuck Lever77f1f672007-02-12 00:53:39 -08001835 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
1837 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1838 printk(KERN_WARNING "svc: only UDP and TCP "
1839 "sockets supported\n");
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001840 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 }
1842 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1843
Chuck Lever77f1f672007-02-12 00:53:39 -08001844 error = sock_create_kern(sin->sa_family, type, protocol, &sock);
1845 if (error < 0)
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001846 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Peter Zijlstraed075362006-12-06 20:35:24 -08001848 svc_reclassify_socket(sock);
1849
Eric Sesterhenn18114742006-09-28 14:37:07 -07001850 if (type == SOCK_STREAM)
Chuck Lever77f1f672007-02-12 00:53:39 -08001851 sock->sk->sk_reuse = 1; /* allow address reuse */
1852 error = kernel_bind(sock, sin, len);
Eric Sesterhenn18114742006-09-28 14:37:07 -07001853 if (error < 0)
1854 goto bummer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Tom Tucker9dbc2402007-12-30 21:08:12 -06001856 newlen = len;
1857 error = kernel_getsockname(sock, newsin, &newlen);
1858 if (error < 0)
1859 goto bummer;
1860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (protocol == IPPROTO_TCP) {
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001862 if ((error = kernel_listen(sock, 64)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 goto bummer;
1864 }
1865
NeilBrowne79eff12007-02-12 00:53:30 -08001866 if ((svsk = svc_setup_socket(serv, sock, &error, flags)) != NULL) {
Tom Tucker9dbc2402007-12-30 21:08:12 -06001867 svc_xprt_set_local(&svsk->sk_xprt, newsin, newlen);
Tom Tuckera6046f72007-12-30 21:08:01 -06001868 svc_xprt_received(&svsk->sk_xprt);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001869 return (struct svc_xprt *)svsk;
NeilBrowne79eff12007-02-12 00:53:30 -08001870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872bummer:
1873 dprintk("svc: svc_create_socket error = %d\n", -error);
1874 sock_release(sock);
Tom Tuckerb700cbb2007-12-30 21:07:42 -06001875 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876}
1877
1878/*
Tom Tucker755ccea2007-12-30 21:07:27 -06001879 * Detach the svc_sock from the socket so that no
1880 * more callbacks occur.
1881 */
1882static void svc_sock_detach(struct svc_xprt *xprt)
1883{
1884 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1885 struct sock *sk = svsk->sk_sk;
1886
1887 dprintk("svc: svc_sock_detach(%p)\n", svsk);
1888
1889 /* put back the old socket callbacks */
1890 sk->sk_state_change = svsk->sk_ostate;
1891 sk->sk_data_ready = svsk->sk_odata;
1892 sk->sk_write_space = svsk->sk_owspace;
1893}
1894
1895/*
1896 * Free the svc_sock's socket resources and the svc_sock itself.
1897 */
1898static void svc_sock_free(struct svc_xprt *xprt)
1899{
1900 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1901 dprintk("svc: svc_sock_free(%p)\n", svsk);
1902
Tom Tucker755ccea2007-12-30 21:07:27 -06001903 if (svsk->sk_sock->file)
1904 sockfd_put(svsk->sk_sock);
1905 else
1906 sock_release(svsk->sk_sock);
1907 kfree(svsk);
1908}
1909
1910/*
Tom Tucker7a182082007-12-30 21:07:53 -06001911 * Remove a dead transport
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 */
Tom Tucker7a182082007-12-30 21:07:53 -06001913static void svc_delete_xprt(struct svc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
Tom Tucker7a182082007-12-30 21:07:53 -06001915 struct svc_serv *serv = xprt->xpt_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Tom Tucker7a182082007-12-30 21:07:53 -06001917 dprintk("svc: svc_delete_xprt(%p)\n", xprt);
1918 xprt->xpt_ops->xpo_detach(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920 spin_lock_bh(&serv->sv_lock);
Tom Tucker7a182082007-12-30 21:07:53 -06001921 if (!test_and_set_bit(XPT_DETACHED, &xprt->xpt_flags))
1922 list_del_init(&xprt->xpt_list);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001923 /*
Tom Tucker7a182082007-12-30 21:07:53 -06001924 * We used to delete the transport from whichever list
1925 * it's sk_xprt.xpt_ready node was on, but we don't actually
Greg Banks3262c812006-10-02 02:17:58 -07001926 * need to. This is because the only time we're called
1927 * while still attached to a queue, the queue itself
1928 * is about to be destroyed (in svc_destroy).
1929 */
Tom Tucker7a182082007-12-30 21:07:53 -06001930 if (!test_and_set_bit(XPT_DEAD, &xprt->xpt_flags)) {
1931 BUG_ON(atomic_read(&xprt->xpt_ref.refcount) < 2);
1932 if (test_bit(XPT_TEMP, &xprt->xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 serv->sv_tmpcnt--;
Tom Tucker7a182082007-12-30 21:07:53 -06001934 svc_xprt_put(xprt);
NeilBrownaaf68cf2007-02-08 14:20:30 -08001935 }
Neil Brownd6740df2006-10-29 22:46:45 -08001936 spin_unlock_bh(&serv->sv_lock);
NeilBrownaaf68cf2007-02-08 14:20:30 -08001937}
1938
Tom Tucker7a182082007-12-30 21:07:53 -06001939static void svc_close_xprt(struct svc_xprt *xprt)
NeilBrownaaf68cf2007-02-08 14:20:30 -08001940{
Tom Tucker7a182082007-12-30 21:07:53 -06001941 set_bit(XPT_CLOSE, &xprt->xpt_flags);
1942 if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags))
NeilBrownaaf68cf2007-02-08 14:20:30 -08001943 /* someone else will have to effect the close */
1944 return;
1945
Tom Tucker7a182082007-12-30 21:07:53 -06001946 svc_xprt_get(xprt);
1947 svc_delete_xprt(xprt);
1948 clear_bit(XPT_BUSY, &xprt->xpt_flags);
1949 svc_xprt_put(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950}
1951
Tom Tucker7a182082007-12-30 21:07:53 -06001952void svc_close_all(struct list_head *xprt_list)
NeilBrowncda1fd42007-03-06 01:42:22 -08001953{
Tom Tucker7a182082007-12-30 21:07:53 -06001954 struct svc_xprt *xprt;
1955 struct svc_xprt *tmp;
1956
1957 list_for_each_entry_safe(xprt, tmp, xprt_list, xpt_list) {
1958 set_bit(XPT_CLOSE, &xprt->xpt_flags);
1959 if (test_bit(XPT_BUSY, &xprt->xpt_flags)) {
1960 /* Waiting to be processed, but no threads left,
1961 * So just remove it from the waiting list
1962 */
1963 list_del_init(&xprt->xpt_ready);
1964 clear_bit(XPT_BUSY, &xprt->xpt_flags);
1965 }
1966 svc_close_xprt(xprt);
NeilBrowncda1fd42007-03-06 01:42:22 -08001967 }
NeilBrowncda1fd42007-03-06 01:42:22 -08001968}
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970/*
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001971 * Handle defer and revisit of requests
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 */
1973
1974static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
1975{
1976 struct svc_deferred_req *dr = container_of(dreq, struct svc_deferred_req, handle);
Tom Tucker8c7b0172007-12-30 21:08:10 -06001977 struct svc_xprt *xprt = dr->xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 if (too_many) {
Tom Tucker8c7b0172007-12-30 21:08:10 -06001980 svc_xprt_put(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 kfree(dr);
1982 return;
1983 }
1984 dprintk("revisit queued\n");
Tom Tucker8c7b0172007-12-30 21:08:10 -06001985 dr->xprt = NULL;
1986 spin_lock(&xprt->xpt_lock);
1987 list_add(&dr->handle.recent, &xprt->xpt_deferred);
1988 spin_unlock(&xprt->xpt_lock);
1989 set_bit(XPT_DEFERRED, &xprt->xpt_flags);
1990 svc_xprt_enqueue(xprt);
1991 svc_xprt_put(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992}
1993
1994static struct cache_deferred_req *
1995svc_defer(struct cache_req *req)
1996{
1997 struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
1998 int size = sizeof(struct svc_deferred_req) + (rqstp->rq_arg.len);
1999 struct svc_deferred_req *dr;
2000
2001 if (rqstp->rq_arg.page_len)
2002 return NULL; /* if more than a page, give up FIXME */
2003 if (rqstp->rq_deferred) {
2004 dr = rqstp->rq_deferred;
2005 rqstp->rq_deferred = NULL;
2006 } else {
2007 int skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
2008 /* FIXME maybe discard if size too large */
2009 dr = kmalloc(size, GFP_KERNEL);
2010 if (dr == NULL)
2011 return NULL;
2012
2013 dr->handle.owner = rqstp->rq_server;
2014 dr->prot = rqstp->rq_prot;
Chuck Lever24422222007-02-12 00:53:33 -08002015 memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen);
2016 dr->addrlen = rqstp->rq_addrlen;
J. Bruce Fields1918e342006-01-18 17:43:16 -08002017 dr->daddr = rqstp->rq_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 dr->argslen = rqstp->rq_arg.len >> 2;
2019 memcpy(dr->args, rqstp->rq_arg.head[0].iov_base-skip, dr->argslen<<2);
2020 }
Tom Tuckere1b31572007-12-30 21:07:46 -06002021 svc_xprt_get(rqstp->rq_xprt);
Tom Tucker8c7b0172007-12-30 21:08:10 -06002022 dr->xprt = rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
2024 dr->handle.revisit = svc_revisit;
2025 return &dr->handle;
2026}
2027
2028/*
2029 * recv data from a deferred request into an active one
2030 */
2031static int svc_deferred_recv(struct svc_rqst *rqstp)
2032{
2033 struct svc_deferred_req *dr = rqstp->rq_deferred;
2034
2035 rqstp->rq_arg.head[0].iov_base = dr->args;
2036 rqstp->rq_arg.head[0].iov_len = dr->argslen<<2;
2037 rqstp->rq_arg.page_len = 0;
2038 rqstp->rq_arg.len = dr->argslen<<2;
2039 rqstp->rq_prot = dr->prot;
Chuck Lever24422222007-02-12 00:53:33 -08002040 memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen);
2041 rqstp->rq_addrlen = dr->addrlen;
J. Bruce Fields1918e342006-01-18 17:43:16 -08002042 rqstp->rq_daddr = dr->daddr;
NeilBrown44524352006-10-04 02:15:46 -07002043 rqstp->rq_respages = rqstp->rq_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 return dr->argslen<<2;
2045}
2046
2047
Tom Tucker8c7b0172007-12-30 21:08:10 -06002048static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
2050 struct svc_deferred_req *dr = NULL;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08002051
Tom Tucker8c7b0172007-12-30 21:08:10 -06002052 if (!test_bit(XPT_DEFERRED, &xprt->xpt_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 return NULL;
Tom Tucker8c7b0172007-12-30 21:08:10 -06002054 spin_lock(&xprt->xpt_lock);
2055 clear_bit(XPT_DEFERRED, &xprt->xpt_flags);
2056 if (!list_empty(&xprt->xpt_deferred)) {
2057 dr = list_entry(xprt->xpt_deferred.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 struct svc_deferred_req,
2059 handle.recent);
2060 list_del_init(&dr->handle.recent);
Tom Tucker8c7b0172007-12-30 21:08:10 -06002061 set_bit(XPT_DEFERRED, &xprt->xpt_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
Tom Tucker8c7b0172007-12-30 21:08:10 -06002063 spin_unlock(&xprt->xpt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 return dr;
2065}