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