blob: 2e5b92ae24ed74cf63b836e803ff2462d8377676 [file] [log] [blame]
Tom Tucker1d8206b92007-12-30 21:07:15 -06001/*
2 * linux/net/sunrpc/svc_xprt.c
3 *
4 * Author: Tom Tucker <tom@opengridcomputing.com>
5 */
6
7#include <linux/sched.h>
8#include <linux/errno.h>
9#include <linux/fcntl.h>
10#include <linux/net.h>
11#include <linux/in.h>
12#include <linux/inet.h>
13#include <linux/udp.h>
14#include <linux/tcp.h>
15#include <linux/unistd.h>
16#include <linux/slab.h>
17#include <linux/netdevice.h>
18#include <linux/skbuff.h>
19#include <linux/file.h>
20#include <linux/freezer.h>
21#include <net/sock.h>
22#include <net/checksum.h>
23#include <net/ip.h>
24#include <net/ipv6.h>
25#include <net/tcp_states.h>
26#include <linux/uaccess.h>
27#include <asm/ioctls.h>
28
29#include <linux/sunrpc/types.h>
30#include <linux/sunrpc/clnt.h>
31#include <linux/sunrpc/xdr.h>
Tom Tucker1d8206b92007-12-30 21:07:15 -060032#include <linux/sunrpc/stats.h>
33#include <linux/sunrpc/svc_xprt.h>
34
35#define RPCDBG_FACILITY RPCDBG_SVCXPRT
36
Tom Tucker0f0257e2007-12-30 21:08:27 -060037static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt);
38static int svc_deferred_recv(struct svc_rqst *rqstp);
39static struct cache_deferred_req *svc_defer(struct cache_req *req);
40static void svc_age_temp_xprts(unsigned long closure);
41
42/* apparently the "standard" is that clients close
43 * idle connections after 5 minutes, servers after
44 * 6 minutes
45 * http://www.connectathon.org/talks96/nfstcp.pdf
46 */
47static int svc_conn_age_period = 6*60;
48
Tom Tucker1d8206b92007-12-30 21:07:15 -060049/* List of registered transport classes */
50static DEFINE_SPINLOCK(svc_xprt_class_lock);
51static LIST_HEAD(svc_xprt_class_list);
52
Tom Tucker0f0257e2007-12-30 21:08:27 -060053/* SMP locking strategy:
54 *
55 * svc_pool->sp_lock protects most of the fields of that pool.
56 * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt.
57 * when both need to be taken (rare), svc_serv->sv_lock is first.
58 * BKL protects svc_serv->sv_nrthread.
59 * svc_sock->sk_lock protects the svc_sock->sk_deferred list
60 * and the ->sk_info_authunix cache.
61 *
62 * The XPT_BUSY bit in xprt->xpt_flags prevents a transport being
63 * enqueued multiply. During normal transport processing this bit
64 * is set by svc_xprt_enqueue and cleared by svc_xprt_received.
65 * Providers should not manipulate this bit directly.
66 *
67 * Some flags can be set to certain values at any time
68 * providing that certain rules are followed:
69 *
70 * XPT_CONN, XPT_DATA:
71 * - Can be set or cleared at any time.
72 * - After a set, svc_xprt_enqueue must be called to enqueue
73 * the transport for processing.
74 * - After a clear, the transport must be read/accepted.
75 * If this succeeds, it must be set again.
76 * XPT_CLOSE:
77 * - Can set at any time. It is never cleared.
78 * XPT_DEAD:
79 * - Can only be set while XPT_BUSY is held which ensures
80 * that no other thread will be using the transport or will
81 * try to set XPT_DEAD.
82 */
83
Tom Tucker1d8206b92007-12-30 21:07:15 -060084int svc_reg_xprt_class(struct svc_xprt_class *xcl)
85{
86 struct svc_xprt_class *cl;
87 int res = -EEXIST;
88
89 dprintk("svc: Adding svc transport class '%s'\n", xcl->xcl_name);
90
91 INIT_LIST_HEAD(&xcl->xcl_list);
92 spin_lock(&svc_xprt_class_lock);
93 /* Make sure there isn't already a class with the same name */
94 list_for_each_entry(cl, &svc_xprt_class_list, xcl_list) {
95 if (strcmp(xcl->xcl_name, cl->xcl_name) == 0)
96 goto out;
97 }
98 list_add_tail(&xcl->xcl_list, &svc_xprt_class_list);
99 res = 0;
100out:
101 spin_unlock(&svc_xprt_class_lock);
102 return res;
103}
104EXPORT_SYMBOL_GPL(svc_reg_xprt_class);
105
106void svc_unreg_xprt_class(struct svc_xprt_class *xcl)
107{
108 dprintk("svc: Removing svc transport class '%s'\n", xcl->xcl_name);
109 spin_lock(&svc_xprt_class_lock);
110 list_del_init(&xcl->xcl_list);
111 spin_unlock(&svc_xprt_class_lock);
112}
113EXPORT_SYMBOL_GPL(svc_unreg_xprt_class);
114
Tom Tuckerdc9a16e2007-12-30 21:08:31 -0600115/*
116 * Format the transport list for printing
117 */
118int svc_print_xprts(char *buf, int maxlen)
119{
120 struct list_head *le;
121 char tmpstr[80];
122 int len = 0;
123 buf[0] = '\0';
124
125 spin_lock(&svc_xprt_class_lock);
126 list_for_each(le, &svc_xprt_class_list) {
127 int slen;
128 struct svc_xprt_class *xcl =
129 list_entry(le, struct svc_xprt_class, xcl_list);
130
131 sprintf(tmpstr, "%s %d\n", xcl->xcl_name, xcl->xcl_max_payload);
132 slen = strlen(tmpstr);
133 if (len + slen > maxlen)
134 break;
135 len += slen;
136 strcat(buf, tmpstr);
137 }
138 spin_unlock(&svc_xprt_class_lock);
139
140 return len;
141}
142
Tom Tuckere1b31572007-12-30 21:07:46 -0600143static void svc_xprt_free(struct kref *kref)
144{
145 struct svc_xprt *xprt =
146 container_of(kref, struct svc_xprt, xpt_ref);
147 struct module *owner = xprt->xpt_class->xcl_owner;
Tom Tuckerdef13d72007-12-30 21:08:08 -0600148 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)
149 && xprt->xpt_auth_cache != NULL)
150 svcauth_unix_info_release(xprt->xpt_auth_cache);
Tom Tuckere1b31572007-12-30 21:07:46 -0600151 xprt->xpt_ops->xpo_free(xprt);
152 module_put(owner);
153}
154
155void svc_xprt_put(struct svc_xprt *xprt)
156{
157 kref_put(&xprt->xpt_ref, svc_xprt_free);
158}
159EXPORT_SYMBOL_GPL(svc_xprt_put);
160
Tom Tucker1d8206b92007-12-30 21:07:15 -0600161/*
162 * Called by transport drivers to initialize the transport independent
163 * portion of the transport instance.
164 */
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600165void svc_xprt_init(struct svc_xprt_class *xcl, struct svc_xprt *xprt,
166 struct svc_serv *serv)
Tom Tucker1d8206b92007-12-30 21:07:15 -0600167{
168 memset(xprt, 0, sizeof(*xprt));
169 xprt->xpt_class = xcl;
170 xprt->xpt_ops = xcl->xcl_ops;
Tom Tuckere1b31572007-12-30 21:07:46 -0600171 kref_init(&xprt->xpt_ref);
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600172 xprt->xpt_server = serv;
Tom Tucker7a182082007-12-30 21:07:53 -0600173 INIT_LIST_HEAD(&xprt->xpt_list);
174 INIT_LIST_HEAD(&xprt->xpt_ready);
Tom Tucker8c7b0172007-12-30 21:08:10 -0600175 INIT_LIST_HEAD(&xprt->xpt_deferred);
Tom Tuckera50fea22007-12-30 21:07:59 -0600176 mutex_init(&xprt->xpt_mutex);
Tom Tuckerdef13d72007-12-30 21:08:08 -0600177 spin_lock_init(&xprt->xpt_lock);
Tom Tucker4e5caaa2007-12-30 21:08:20 -0600178 set_bit(XPT_BUSY, &xprt->xpt_flags);
Tom Tucker1d8206b92007-12-30 21:07:15 -0600179}
180EXPORT_SYMBOL_GPL(svc_xprt_init);
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600181
182int svc_create_xprt(struct svc_serv *serv, char *xprt_name, unsigned short port,
183 int flags)
184{
185 struct svc_xprt_class *xcl;
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600186 struct sockaddr_in sin = {
187 .sin_family = AF_INET,
188 .sin_addr.s_addr = INADDR_ANY,
189 .sin_port = htons(port),
190 };
191 dprintk("svc: creating transport %s[%d]\n", xprt_name, port);
192 spin_lock(&svc_xprt_class_lock);
193 list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) {
Tom Tucker4e5caaa2007-12-30 21:08:20 -0600194 struct svc_xprt *newxprt;
195
196 if (strcmp(xprt_name, xcl->xcl_name))
197 continue;
198
199 if (!try_module_get(xcl->xcl_owner))
200 goto err;
201
202 spin_unlock(&svc_xprt_class_lock);
203 newxprt = xcl->xcl_ops->
204 xpo_create(serv, (struct sockaddr *)&sin, sizeof(sin),
205 flags);
206 if (IS_ERR(newxprt)) {
207 module_put(xcl->xcl_owner);
208 return PTR_ERR(newxprt);
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600209 }
Tom Tucker4e5caaa2007-12-30 21:08:20 -0600210
211 clear_bit(XPT_TEMP, &newxprt->xpt_flags);
212 spin_lock_bh(&serv->sv_lock);
213 list_add(&newxprt->xpt_list, &serv->sv_permsocks);
214 spin_unlock_bh(&serv->sv_lock);
215 clear_bit(XPT_BUSY, &newxprt->xpt_flags);
216 return svc_xprt_local_port(newxprt);
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600217 }
Tom Tucker4e5caaa2007-12-30 21:08:20 -0600218 err:
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600219 spin_unlock(&svc_xprt_class_lock);
220 dprintk("svc: transport %s not found\n", xprt_name);
Tom Tucker4e5caaa2007-12-30 21:08:20 -0600221 return -ENOENT;
Tom Tuckerb700cbb2007-12-30 21:07:42 -0600222}
223EXPORT_SYMBOL_GPL(svc_create_xprt);
Tom Tucker9dbc2402007-12-30 21:08:12 -0600224
225/*
226 * Copy the local and remote xprt addresses to the rqstp structure
227 */
228void svc_xprt_copy_addrs(struct svc_rqst *rqstp, struct svc_xprt *xprt)
229{
230 struct sockaddr *sin;
231
232 memcpy(&rqstp->rq_addr, &xprt->xpt_remote, xprt->xpt_remotelen);
233 rqstp->rq_addrlen = xprt->xpt_remotelen;
234
235 /*
236 * Destination address in request is needed for binding the
237 * source address in RPC replies/callbacks later.
238 */
239 sin = (struct sockaddr *)&xprt->xpt_local;
240 switch (sin->sa_family) {
241 case AF_INET:
242 rqstp->rq_daddr.addr = ((struct sockaddr_in *)sin)->sin_addr;
243 break;
244 case AF_INET6:
245 rqstp->rq_daddr.addr6 = ((struct sockaddr_in6 *)sin)->sin6_addr;
246 break;
247 }
248}
249EXPORT_SYMBOL_GPL(svc_xprt_copy_addrs);
250
Tom Tucker0f0257e2007-12-30 21:08:27 -0600251/**
252 * svc_print_addr - Format rq_addr field for printing
253 * @rqstp: svc_rqst struct containing address to print
254 * @buf: target buffer for formatted address
255 * @len: length of target buffer
256 *
257 */
258char *svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len)
259{
260 return __svc_print_addr(svc_addr(rqstp), buf, len);
261}
262EXPORT_SYMBOL_GPL(svc_print_addr);
263
264/*
265 * Queue up an idle server thread. Must have pool->sp_lock held.
266 * Note: this is really a stack rather than a queue, so that we only
267 * use as many different threads as we need, and the rest don't pollute
268 * the cache.
269 */
270static void svc_thread_enqueue(struct svc_pool *pool, struct svc_rqst *rqstp)
271{
272 list_add(&rqstp->rq_list, &pool->sp_threads);
273}
274
275/*
276 * Dequeue an nfsd thread. Must have pool->sp_lock held.
277 */
278static void svc_thread_dequeue(struct svc_pool *pool, struct svc_rqst *rqstp)
279{
280 list_del(&rqstp->rq_list);
281}
282
283/*
284 * Queue up a transport with data pending. If there are idle nfsd
285 * processes, wake 'em up.
286 *
287 */
288void svc_xprt_enqueue(struct svc_xprt *xprt)
289{
290 struct svc_serv *serv = xprt->xpt_server;
291 struct svc_pool *pool;
292 struct svc_rqst *rqstp;
293 int cpu;
294
295 if (!(xprt->xpt_flags &
296 ((1<<XPT_CONN)|(1<<XPT_DATA)|(1<<XPT_CLOSE)|(1<<XPT_DEFERRED))))
297 return;
298 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
299 return;
300
301 cpu = get_cpu();
302 pool = svc_pool_for_cpu(xprt->xpt_server, cpu);
303 put_cpu();
304
305 spin_lock_bh(&pool->sp_lock);
306
307 if (!list_empty(&pool->sp_threads) &&
308 !list_empty(&pool->sp_sockets))
309 printk(KERN_ERR
310 "svc_xprt_enqueue: "
311 "threads and transports both waiting??\n");
312
313 if (test_bit(XPT_DEAD, &xprt->xpt_flags)) {
314 /* Don't enqueue dead transports */
315 dprintk("svc: transport %p is dead, not enqueued\n", xprt);
316 goto out_unlock;
317 }
318
319 /* Mark transport as busy. It will remain in this state until
320 * the provider calls svc_xprt_received. We update XPT_BUSY
321 * atomically because it also guards against trying to enqueue
322 * the transport twice.
323 */
324 if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags)) {
325 /* Don't enqueue transport while already enqueued */
326 dprintk("svc: transport %p busy, not enqueued\n", xprt);
327 goto out_unlock;
328 }
329 BUG_ON(xprt->xpt_pool != NULL);
330 xprt->xpt_pool = pool;
331
332 /* Handle pending connection */
333 if (test_bit(XPT_CONN, &xprt->xpt_flags))
334 goto process;
335
336 /* Handle close in-progress */
337 if (test_bit(XPT_CLOSE, &xprt->xpt_flags))
338 goto process;
339
340 /* Check if we have space to reply to a request */
341 if (!xprt->xpt_ops->xpo_has_wspace(xprt)) {
342 /* Don't enqueue while not enough space for reply */
343 dprintk("svc: no write space, transport %p not enqueued\n",
344 xprt);
345 xprt->xpt_pool = NULL;
346 clear_bit(XPT_BUSY, &xprt->xpt_flags);
347 goto out_unlock;
348 }
349
350 process:
351 if (!list_empty(&pool->sp_threads)) {
352 rqstp = list_entry(pool->sp_threads.next,
353 struct svc_rqst,
354 rq_list);
355 dprintk("svc: transport %p served by daemon %p\n",
356 xprt, rqstp);
357 svc_thread_dequeue(pool, rqstp);
358 if (rqstp->rq_xprt)
359 printk(KERN_ERR
360 "svc_xprt_enqueue: server %p, rq_xprt=%p!\n",
361 rqstp, rqstp->rq_xprt);
362 rqstp->rq_xprt = xprt;
363 svc_xprt_get(xprt);
364 rqstp->rq_reserved = serv->sv_max_mesg;
365 atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved);
366 BUG_ON(xprt->xpt_pool != pool);
367 wake_up(&rqstp->rq_wait);
368 } else {
369 dprintk("svc: transport %p put into queue\n", xprt);
370 list_add_tail(&xprt->xpt_ready, &pool->sp_sockets);
371 BUG_ON(xprt->xpt_pool != pool);
372 }
373
374out_unlock:
375 spin_unlock_bh(&pool->sp_lock);
376}
377EXPORT_SYMBOL_GPL(svc_xprt_enqueue);
378
379/*
380 * Dequeue the first transport. Must be called with the pool->sp_lock held.
381 */
382static struct svc_xprt *svc_xprt_dequeue(struct svc_pool *pool)
383{
384 struct svc_xprt *xprt;
385
386 if (list_empty(&pool->sp_sockets))
387 return NULL;
388
389 xprt = list_entry(pool->sp_sockets.next,
390 struct svc_xprt, xpt_ready);
391 list_del_init(&xprt->xpt_ready);
392
393 dprintk("svc: transport %p dequeued, inuse=%d\n",
394 xprt, atomic_read(&xprt->xpt_ref.refcount));
395
396 return xprt;
397}
398
399/*
400 * svc_xprt_received conditionally queues the transport for processing
401 * by another thread. The caller must hold the XPT_BUSY bit and must
402 * not thereafter touch transport data.
403 *
404 * Note: XPT_DATA only gets cleared when a read-attempt finds no (or
405 * insufficient) data.
406 */
407void svc_xprt_received(struct svc_xprt *xprt)
408{
409 BUG_ON(!test_bit(XPT_BUSY, &xprt->xpt_flags));
410 xprt->xpt_pool = NULL;
411 clear_bit(XPT_BUSY, &xprt->xpt_flags);
412 svc_xprt_enqueue(xprt);
413}
414EXPORT_SYMBOL_GPL(svc_xprt_received);
415
416/**
417 * svc_reserve - change the space reserved for the reply to a request.
418 * @rqstp: The request in question
419 * @space: new max space to reserve
420 *
421 * Each request reserves some space on the output queue of the transport
422 * to make sure the reply fits. This function reduces that reserved
423 * space to be the amount of space used already, plus @space.
424 *
425 */
426void svc_reserve(struct svc_rqst *rqstp, int space)
427{
428 space += rqstp->rq_res.head[0].iov_len;
429
430 if (space < rqstp->rq_reserved) {
431 struct svc_xprt *xprt = rqstp->rq_xprt;
432 atomic_sub((rqstp->rq_reserved - space), &xprt->xpt_reserved);
433 rqstp->rq_reserved = space;
434
435 svc_xprt_enqueue(xprt);
436 }
437}
438
439static void svc_xprt_release(struct svc_rqst *rqstp)
440{
441 struct svc_xprt *xprt = rqstp->rq_xprt;
442
443 rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
444
445 svc_free_res_pages(rqstp);
446 rqstp->rq_res.page_len = 0;
447 rqstp->rq_res.page_base = 0;
448
449 /* Reset response buffer and release
450 * the reservation.
451 * But first, check that enough space was reserved
452 * for the reply, otherwise we have a bug!
453 */
454 if ((rqstp->rq_res.len) > rqstp->rq_reserved)
455 printk(KERN_ERR "RPC request reserved %d but used %d\n",
456 rqstp->rq_reserved,
457 rqstp->rq_res.len);
458
459 rqstp->rq_res.head[0].iov_len = 0;
460 svc_reserve(rqstp, 0);
461 rqstp->rq_xprt = NULL;
462
463 svc_xprt_put(xprt);
464}
465
466/*
467 * External function to wake up a server waiting for data
468 * This really only makes sense for services like lockd
469 * which have exactly one thread anyway.
470 */
471void svc_wake_up(struct svc_serv *serv)
472{
473 struct svc_rqst *rqstp;
474 unsigned int i;
475 struct svc_pool *pool;
476
477 for (i = 0; i < serv->sv_nrpools; i++) {
478 pool = &serv->sv_pools[i];
479
480 spin_lock_bh(&pool->sp_lock);
481 if (!list_empty(&pool->sp_threads)) {
482 rqstp = list_entry(pool->sp_threads.next,
483 struct svc_rqst,
484 rq_list);
485 dprintk("svc: daemon %p woken up.\n", rqstp);
486 /*
487 svc_thread_dequeue(pool, rqstp);
488 rqstp->rq_xprt = NULL;
489 */
490 wake_up(&rqstp->rq_wait);
491 }
492 spin_unlock_bh(&pool->sp_lock);
493 }
494}
495
496int svc_port_is_privileged(struct sockaddr *sin)
497{
498 switch (sin->sa_family) {
499 case AF_INET:
500 return ntohs(((struct sockaddr_in *)sin)->sin_port)
501 < PROT_SOCK;
502 case AF_INET6:
503 return ntohs(((struct sockaddr_in6 *)sin)->sin6_port)
504 < PROT_SOCK;
505 default:
506 return 0;
507 }
508}
509
510/*
511 * Make sure that we don't have too many active connections. If we
512 * have, something must be dropped.
513 *
514 * There's no point in trying to do random drop here for DoS
515 * prevention. The NFS clients does 1 reconnect in 15 seconds. An
516 * attacker can easily beat that.
517 *
518 * The only somewhat efficient mechanism would be if drop old
519 * connections from the same IP first. But right now we don't even
520 * record the client IP in svc_sock.
521 */
522static void svc_check_conn_limits(struct svc_serv *serv)
523{
524 if (serv->sv_tmpcnt > (serv->sv_nrthreads+3)*20) {
525 struct svc_xprt *xprt = NULL;
526 spin_lock_bh(&serv->sv_lock);
527 if (!list_empty(&serv->sv_tempsocks)) {
528 if (net_ratelimit()) {
529 /* Try to help the admin */
530 printk(KERN_NOTICE "%s: too many open "
531 "connections, consider increasing the "
532 "number of nfsd threads\n",
533 serv->sv_name);
534 }
535 /*
536 * Always select the oldest connection. It's not fair,
537 * but so is life
538 */
539 xprt = list_entry(serv->sv_tempsocks.prev,
540 struct svc_xprt,
541 xpt_list);
542 set_bit(XPT_CLOSE, &xprt->xpt_flags);
543 svc_xprt_get(xprt);
544 }
545 spin_unlock_bh(&serv->sv_lock);
546
547 if (xprt) {
548 svc_xprt_enqueue(xprt);
549 svc_xprt_put(xprt);
550 }
551 }
552}
553
554/*
555 * Receive the next request on any transport. This code is carefully
556 * organised not to touch any cachelines in the shared svc_serv
557 * structure, only cachelines in the local svc_pool.
558 */
559int svc_recv(struct svc_rqst *rqstp, long timeout)
560{
561 struct svc_xprt *xprt = NULL;
562 struct svc_serv *serv = rqstp->rq_server;
563 struct svc_pool *pool = rqstp->rq_pool;
564 int len, i;
565 int pages;
566 struct xdr_buf *arg;
567 DECLARE_WAITQUEUE(wait, current);
568
569 dprintk("svc: server %p waiting for data (to = %ld)\n",
570 rqstp, timeout);
571
572 if (rqstp->rq_xprt)
573 printk(KERN_ERR
574 "svc_recv: service %p, transport not NULL!\n",
575 rqstp);
576 if (waitqueue_active(&rqstp->rq_wait))
577 printk(KERN_ERR
578 "svc_recv: service %p, wait queue active!\n",
579 rqstp);
580
581 /* now allocate needed pages. If we get a failure, sleep briefly */
582 pages = (serv->sv_max_mesg + PAGE_SIZE) / PAGE_SIZE;
583 for (i = 0; i < pages ; i++)
584 while (rqstp->rq_pages[i] == NULL) {
585 struct page *p = alloc_page(GFP_KERNEL);
586 if (!p) {
587 int j = msecs_to_jiffies(500);
588 schedule_timeout_uninterruptible(j);
589 }
590 rqstp->rq_pages[i] = p;
591 }
592 rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */
593 BUG_ON(pages >= RPCSVC_MAXPAGES);
594
595 /* Make arg->head point to first page and arg->pages point to rest */
596 arg = &rqstp->rq_arg;
597 arg->head[0].iov_base = page_address(rqstp->rq_pages[0]);
598 arg->head[0].iov_len = PAGE_SIZE;
599 arg->pages = rqstp->rq_pages + 1;
600 arg->page_base = 0;
601 /* save at least one page for response */
602 arg->page_len = (pages-2)*PAGE_SIZE;
603 arg->len = (pages-1)*PAGE_SIZE;
604 arg->tail[0].iov_len = 0;
605
606 try_to_freeze();
607 cond_resched();
608 if (signalled())
609 return -EINTR;
610
611 spin_lock_bh(&pool->sp_lock);
612 xprt = svc_xprt_dequeue(pool);
613 if (xprt) {
614 rqstp->rq_xprt = xprt;
615 svc_xprt_get(xprt);
616 rqstp->rq_reserved = serv->sv_max_mesg;
617 atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved);
618 } else {
619 /* No data pending. Go to sleep */
620 svc_thread_enqueue(pool, rqstp);
621
622 /*
623 * We have to be able to interrupt this wait
624 * to bring down the daemons ...
625 */
626 set_current_state(TASK_INTERRUPTIBLE);
627 add_wait_queue(&rqstp->rq_wait, &wait);
628 spin_unlock_bh(&pool->sp_lock);
629
630 schedule_timeout(timeout);
631
632 try_to_freeze();
633
634 spin_lock_bh(&pool->sp_lock);
635 remove_wait_queue(&rqstp->rq_wait, &wait);
636
637 xprt = rqstp->rq_xprt;
638 if (!xprt) {
639 svc_thread_dequeue(pool, rqstp);
640 spin_unlock_bh(&pool->sp_lock);
641 dprintk("svc: server %p, no data yet\n", rqstp);
642 return signalled()? -EINTR : -EAGAIN;
643 }
644 }
645 spin_unlock_bh(&pool->sp_lock);
646
647 len = 0;
648 if (test_bit(XPT_CLOSE, &xprt->xpt_flags)) {
649 dprintk("svc_recv: found XPT_CLOSE\n");
650 svc_delete_xprt(xprt);
651 } else if (test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
652 struct svc_xprt *newxpt;
653 newxpt = xprt->xpt_ops->xpo_accept(xprt);
654 if (newxpt) {
655 /*
656 * We know this module_get will succeed because the
657 * listener holds a reference too
658 */
659 __module_get(newxpt->xpt_class->xcl_owner);
660 svc_check_conn_limits(xprt->xpt_server);
661 spin_lock_bh(&serv->sv_lock);
662 set_bit(XPT_TEMP, &newxpt->xpt_flags);
663 list_add(&newxpt->xpt_list, &serv->sv_tempsocks);
664 serv->sv_tmpcnt++;
665 if (serv->sv_temptimer.function == NULL) {
666 /* setup timer to age temp transports */
667 setup_timer(&serv->sv_temptimer,
668 svc_age_temp_xprts,
669 (unsigned long)serv);
670 mod_timer(&serv->sv_temptimer,
671 jiffies + svc_conn_age_period * HZ);
672 }
673 spin_unlock_bh(&serv->sv_lock);
674 svc_xprt_received(newxpt);
675 }
676 svc_xprt_received(xprt);
677 } else {
678 dprintk("svc: server %p, pool %u, transport %p, inuse=%d\n",
679 rqstp, pool->sp_id, xprt,
680 atomic_read(&xprt->xpt_ref.refcount));
681 rqstp->rq_deferred = svc_deferred_dequeue(xprt);
682 if (rqstp->rq_deferred) {
683 svc_xprt_received(xprt);
684 len = svc_deferred_recv(rqstp);
685 } else
686 len = xprt->xpt_ops->xpo_recvfrom(rqstp);
687 dprintk("svc: got len=%d\n", len);
688 }
689
690 /* No data, incomplete (TCP) read, or accept() */
691 if (len == 0 || len == -EAGAIN) {
692 rqstp->rq_res.len = 0;
693 svc_xprt_release(rqstp);
694 return -EAGAIN;
695 }
696 clear_bit(XPT_OLD, &xprt->xpt_flags);
697
698 rqstp->rq_secure = svc_port_is_privileged(svc_addr(rqstp));
699 rqstp->rq_chandle.defer = svc_defer;
700
701 if (serv->sv_stats)
702 serv->sv_stats->netcnt++;
703 return len;
704}
705
706/*
707 * Drop request
708 */
709void svc_drop(struct svc_rqst *rqstp)
710{
711 dprintk("svc: xprt %p dropped request\n", rqstp->rq_xprt);
712 svc_xprt_release(rqstp);
713}
714
715/*
716 * Return reply to client.
717 */
718int svc_send(struct svc_rqst *rqstp)
719{
720 struct svc_xprt *xprt;
721 int len;
722 struct xdr_buf *xb;
723
724 xprt = rqstp->rq_xprt;
725 if (!xprt)
726 return -EFAULT;
727
728 /* release the receive skb before sending the reply */
729 rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
730
731 /* calculate over-all length */
732 xb = &rqstp->rq_res;
733 xb->len = xb->head[0].iov_len +
734 xb->page_len +
735 xb->tail[0].iov_len;
736
737 /* Grab mutex to serialize outgoing data. */
738 mutex_lock(&xprt->xpt_mutex);
739 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
740 len = -ENOTCONN;
741 else
742 len = xprt->xpt_ops->xpo_sendto(rqstp);
743 mutex_unlock(&xprt->xpt_mutex);
744 svc_xprt_release(rqstp);
745
746 if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
747 return 0;
748 return len;
749}
750
751/*
752 * Timer function to close old temporary transports, using
753 * a mark-and-sweep algorithm.
754 */
755static void svc_age_temp_xprts(unsigned long closure)
756{
757 struct svc_serv *serv = (struct svc_serv *)closure;
758 struct svc_xprt *xprt;
759 struct list_head *le, *next;
760 LIST_HEAD(to_be_aged);
761
762 dprintk("svc_age_temp_xprts\n");
763
764 if (!spin_trylock_bh(&serv->sv_lock)) {
765 /* busy, try again 1 sec later */
766 dprintk("svc_age_temp_xprts: busy\n");
767 mod_timer(&serv->sv_temptimer, jiffies + HZ);
768 return;
769 }
770
771 list_for_each_safe(le, next, &serv->sv_tempsocks) {
772 xprt = list_entry(le, struct svc_xprt, xpt_list);
773
774 /* First time through, just mark it OLD. Second time
775 * through, close it. */
776 if (!test_and_set_bit(XPT_OLD, &xprt->xpt_flags))
777 continue;
778 if (atomic_read(&xprt->xpt_ref.refcount) > 1
779 || test_bit(XPT_BUSY, &xprt->xpt_flags))
780 continue;
781 svc_xprt_get(xprt);
782 list_move(le, &to_be_aged);
783 set_bit(XPT_CLOSE, &xprt->xpt_flags);
784 set_bit(XPT_DETACHED, &xprt->xpt_flags);
785 }
786 spin_unlock_bh(&serv->sv_lock);
787
788 while (!list_empty(&to_be_aged)) {
789 le = to_be_aged.next;
790 /* fiddling the xpt_list node is safe 'cos we're XPT_DETACHED */
791 list_del_init(le);
792 xprt = list_entry(le, struct svc_xprt, xpt_list);
793
794 dprintk("queuing xprt %p for closing\n", xprt);
795
796 /* a thread will dequeue and close it soon */
797 svc_xprt_enqueue(xprt);
798 svc_xprt_put(xprt);
799 }
800
801 mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
802}
803
804/*
805 * Remove a dead transport
806 */
807void svc_delete_xprt(struct svc_xprt *xprt)
808{
809 struct svc_serv *serv = xprt->xpt_server;
810
811 dprintk("svc: svc_delete_xprt(%p)\n", xprt);
812 xprt->xpt_ops->xpo_detach(xprt);
813
814 spin_lock_bh(&serv->sv_lock);
815 if (!test_and_set_bit(XPT_DETACHED, &xprt->xpt_flags))
816 list_del_init(&xprt->xpt_list);
817 /*
818 * We used to delete the transport from whichever list
819 * it's sk_xprt.xpt_ready node was on, but we don't actually
820 * need to. This is because the only time we're called
821 * while still attached to a queue, the queue itself
822 * is about to be destroyed (in svc_destroy).
823 */
824 if (!test_and_set_bit(XPT_DEAD, &xprt->xpt_flags)) {
825 BUG_ON(atomic_read(&xprt->xpt_ref.refcount) < 2);
826 if (test_bit(XPT_TEMP, &xprt->xpt_flags))
827 serv->sv_tmpcnt--;
828 svc_xprt_put(xprt);
829 }
830 spin_unlock_bh(&serv->sv_lock);
831}
832
833void svc_close_xprt(struct svc_xprt *xprt)
834{
835 set_bit(XPT_CLOSE, &xprt->xpt_flags);
836 if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags))
837 /* someone else will have to effect the close */
838 return;
839
840 svc_xprt_get(xprt);
841 svc_delete_xprt(xprt);
842 clear_bit(XPT_BUSY, &xprt->xpt_flags);
843 svc_xprt_put(xprt);
844}
845
846void svc_close_all(struct list_head *xprt_list)
847{
848 struct svc_xprt *xprt;
849 struct svc_xprt *tmp;
850
851 list_for_each_entry_safe(xprt, tmp, xprt_list, xpt_list) {
852 set_bit(XPT_CLOSE, &xprt->xpt_flags);
853 if (test_bit(XPT_BUSY, &xprt->xpt_flags)) {
854 /* Waiting to be processed, but no threads left,
855 * So just remove it from the waiting list
856 */
857 list_del_init(&xprt->xpt_ready);
858 clear_bit(XPT_BUSY, &xprt->xpt_flags);
859 }
860 svc_close_xprt(xprt);
861 }
862}
863
864/*
865 * Handle defer and revisit of requests
866 */
867
868static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
869{
870 struct svc_deferred_req *dr =
871 container_of(dreq, struct svc_deferred_req, handle);
872 struct svc_xprt *xprt = dr->xprt;
873
874 if (too_many) {
875 svc_xprt_put(xprt);
876 kfree(dr);
877 return;
878 }
879 dprintk("revisit queued\n");
880 dr->xprt = NULL;
881 spin_lock(&xprt->xpt_lock);
882 list_add(&dr->handle.recent, &xprt->xpt_deferred);
883 spin_unlock(&xprt->xpt_lock);
884 set_bit(XPT_DEFERRED, &xprt->xpt_flags);
885 svc_xprt_enqueue(xprt);
886 svc_xprt_put(xprt);
887}
888
Tom Tucker260c1d12007-12-30 21:08:29 -0600889/*
890 * Save the request off for later processing. The request buffer looks
891 * like this:
892 *
893 * <xprt-header><rpc-header><rpc-pagelist><rpc-tail>
894 *
895 * This code can only handle requests that consist of an xprt-header
896 * and rpc-header.
897 */
Tom Tucker0f0257e2007-12-30 21:08:27 -0600898static struct cache_deferred_req *svc_defer(struct cache_req *req)
899{
900 struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
Tom Tucker0f0257e2007-12-30 21:08:27 -0600901 struct svc_deferred_req *dr;
902
903 if (rqstp->rq_arg.page_len)
904 return NULL; /* if more than a page, give up FIXME */
905 if (rqstp->rq_deferred) {
906 dr = rqstp->rq_deferred;
907 rqstp->rq_deferred = NULL;
908 } else {
Tom Tucker260c1d12007-12-30 21:08:29 -0600909 size_t skip;
910 size_t size;
Tom Tucker0f0257e2007-12-30 21:08:27 -0600911 /* FIXME maybe discard if size too large */
Tom Tucker260c1d12007-12-30 21:08:29 -0600912 size = sizeof(struct svc_deferred_req) + rqstp->rq_arg.len;
Tom Tucker0f0257e2007-12-30 21:08:27 -0600913 dr = kmalloc(size, GFP_KERNEL);
914 if (dr == NULL)
915 return NULL;
916
917 dr->handle.owner = rqstp->rq_server;
918 dr->prot = rqstp->rq_prot;
919 memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen);
920 dr->addrlen = rqstp->rq_addrlen;
921 dr->daddr = rqstp->rq_daddr;
922 dr->argslen = rqstp->rq_arg.len >> 2;
Tom Tucker260c1d12007-12-30 21:08:29 -0600923 dr->xprt_hlen = rqstp->rq_xprt_hlen;
924
925 /* back up head to the start of the buffer and copy */
926 skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
927 memcpy(dr->args, rqstp->rq_arg.head[0].iov_base - skip,
928 dr->argslen << 2);
Tom Tucker0f0257e2007-12-30 21:08:27 -0600929 }
930 svc_xprt_get(rqstp->rq_xprt);
931 dr->xprt = rqstp->rq_xprt;
932
933 dr->handle.revisit = svc_revisit;
934 return &dr->handle;
935}
936
937/*
938 * recv data from a deferred request into an active one
939 */
940static int svc_deferred_recv(struct svc_rqst *rqstp)
941{
942 struct svc_deferred_req *dr = rqstp->rq_deferred;
943
Tom Tucker260c1d12007-12-30 21:08:29 -0600944 /* setup iov_base past transport header */
945 rqstp->rq_arg.head[0].iov_base = dr->args + (dr->xprt_hlen>>2);
946 /* The iov_len does not include the transport header bytes */
947 rqstp->rq_arg.head[0].iov_len = (dr->argslen<<2) - dr->xprt_hlen;
Tom Tucker0f0257e2007-12-30 21:08:27 -0600948 rqstp->rq_arg.page_len = 0;
Tom Tucker260c1d12007-12-30 21:08:29 -0600949 /* The rq_arg.len includes the transport header bytes */
950 rqstp->rq_arg.len = dr->argslen<<2;
Tom Tucker0f0257e2007-12-30 21:08:27 -0600951 rqstp->rq_prot = dr->prot;
952 memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen);
953 rqstp->rq_addrlen = dr->addrlen;
Tom Tucker260c1d12007-12-30 21:08:29 -0600954 /* Save off transport header len in case we get deferred again */
955 rqstp->rq_xprt_hlen = dr->xprt_hlen;
Tom Tucker0f0257e2007-12-30 21:08:27 -0600956 rqstp->rq_daddr = dr->daddr;
957 rqstp->rq_respages = rqstp->rq_pages;
Tom Tucker260c1d12007-12-30 21:08:29 -0600958 return (dr->argslen<<2) - dr->xprt_hlen;
Tom Tucker0f0257e2007-12-30 21:08:27 -0600959}
960
961
962static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt)
963{
964 struct svc_deferred_req *dr = NULL;
965
966 if (!test_bit(XPT_DEFERRED, &xprt->xpt_flags))
967 return NULL;
968 spin_lock(&xprt->xpt_lock);
969 clear_bit(XPT_DEFERRED, &xprt->xpt_flags);
970 if (!list_empty(&xprt->xpt_deferred)) {
971 dr = list_entry(xprt->xpt_deferred.next,
972 struct svc_deferred_req,
973 handle.recent);
974 list_del_init(&dr->handle.recent);
975 set_bit(XPT_DEFERRED, &xprt->xpt_flags);
976 }
977 spin_unlock(&xprt->xpt_lock);
978 return dr;
979}