nfsd: provide callbacks on svc_xprt deletion
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / sunrpc / svc_xprt.c
blob12025eedc781bd95aa2e1bc972f134cb162746a5
1 /*
2 * linux/net/sunrpc/svc_xprt.c
4 * Author: Tom Tucker <tom@opengridcomputing.com>
5 */
7 #include <linux/sched.h>
8 #include <linux/smp_lock.h>
9 #include <linux/errno.h>
10 #include <linux/freezer.h>
11 #include <linux/kthread.h>
12 #include <linux/slab.h>
13 #include <net/sock.h>
14 #include <linux/sunrpc/stats.h>
15 #include <linux/sunrpc/svc_xprt.h>
16 #include <linux/sunrpc/svcsock.h>
18 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
20 static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt);
21 static int svc_deferred_recv(struct svc_rqst *rqstp);
22 static struct cache_deferred_req *svc_defer(struct cache_req *req);
23 static void svc_age_temp_xprts(unsigned long closure);
25 /* apparently the "standard" is that clients close
26 * idle connections after 5 minutes, servers after
27 * 6 minutes
28 * http://www.connectathon.org/talks96/nfstcp.pdf
30 static int svc_conn_age_period = 6*60;
32 /* List of registered transport classes */
33 static DEFINE_SPINLOCK(svc_xprt_class_lock);
34 static LIST_HEAD(svc_xprt_class_list);
36 /* SMP locking strategy:
38 * svc_pool->sp_lock protects most of the fields of that pool.
39 * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt.
40 * when both need to be taken (rare), svc_serv->sv_lock is first.
41 * BKL protects svc_serv->sv_nrthread.
42 * svc_sock->sk_lock protects the svc_sock->sk_deferred list
43 * and the ->sk_info_authunix cache.
45 * The XPT_BUSY bit in xprt->xpt_flags prevents a transport being
46 * enqueued multiply. During normal transport processing this bit
47 * is set by svc_xprt_enqueue and cleared by svc_xprt_received.
48 * Providers should not manipulate this bit directly.
50 * Some flags can be set to certain values at any time
51 * providing that certain rules are followed:
53 * XPT_CONN, XPT_DATA:
54 * - Can be set or cleared at any time.
55 * - After a set, svc_xprt_enqueue must be called to enqueue
56 * the transport for processing.
57 * - After a clear, the transport must be read/accepted.
58 * If this succeeds, it must be set again.
59 * XPT_CLOSE:
60 * - Can set at any time. It is never cleared.
61 * XPT_DEAD:
62 * - Can only be set while XPT_BUSY is held which ensures
63 * that no other thread will be using the transport or will
64 * try to set XPT_DEAD.
67 int svc_reg_xprt_class(struct svc_xprt_class *xcl)
69 struct svc_xprt_class *cl;
70 int res = -EEXIST;
72 dprintk("svc: Adding svc transport class '%s'\n", xcl->xcl_name);
74 INIT_LIST_HEAD(&xcl->xcl_list);
75 spin_lock(&svc_xprt_class_lock);
76 /* Make sure there isn't already a class with the same name */
77 list_for_each_entry(cl, &svc_xprt_class_list, xcl_list) {
78 if (strcmp(xcl->xcl_name, cl->xcl_name) == 0)
79 goto out;
81 list_add_tail(&xcl->xcl_list, &svc_xprt_class_list);
82 res = 0;
83 out:
84 spin_unlock(&svc_xprt_class_lock);
85 return res;
87 EXPORT_SYMBOL_GPL(svc_reg_xprt_class);
89 void svc_unreg_xprt_class(struct svc_xprt_class *xcl)
91 dprintk("svc: Removing svc transport class '%s'\n", xcl->xcl_name);
92 spin_lock(&svc_xprt_class_lock);
93 list_del_init(&xcl->xcl_list);
94 spin_unlock(&svc_xprt_class_lock);
96 EXPORT_SYMBOL_GPL(svc_unreg_xprt_class);
99 * Format the transport list for printing
101 int svc_print_xprts(char *buf, int maxlen)
103 struct list_head *le;
104 char tmpstr[80];
105 int len = 0;
106 buf[0] = '\0';
108 spin_lock(&svc_xprt_class_lock);
109 list_for_each(le, &svc_xprt_class_list) {
110 int slen;
111 struct svc_xprt_class *xcl =
112 list_entry(le, struct svc_xprt_class, xcl_list);
114 sprintf(tmpstr, "%s %d\n", xcl->xcl_name, xcl->xcl_max_payload);
115 slen = strlen(tmpstr);
116 if (len + slen > maxlen)
117 break;
118 len += slen;
119 strcat(buf, tmpstr);
121 spin_unlock(&svc_xprt_class_lock);
123 return len;
126 static void svc_xprt_free(struct kref *kref)
128 struct svc_xprt *xprt =
129 container_of(kref, struct svc_xprt, xpt_ref);
130 struct module *owner = xprt->xpt_class->xcl_owner;
131 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags))
132 svcauth_unix_info_release(xprt);
133 put_net(xprt->xpt_net);
134 xprt->xpt_ops->xpo_free(xprt);
135 module_put(owner);
138 void svc_xprt_put(struct svc_xprt *xprt)
140 kref_put(&xprt->xpt_ref, svc_xprt_free);
142 EXPORT_SYMBOL_GPL(svc_xprt_put);
145 * Called by transport drivers to initialize the transport independent
146 * portion of the transport instance.
148 void svc_xprt_init(struct svc_xprt_class *xcl, struct svc_xprt *xprt,
149 struct svc_serv *serv)
151 memset(xprt, 0, sizeof(*xprt));
152 xprt->xpt_class = xcl;
153 xprt->xpt_ops = xcl->xcl_ops;
154 kref_init(&xprt->xpt_ref);
155 xprt->xpt_server = serv;
156 INIT_LIST_HEAD(&xprt->xpt_list);
157 INIT_LIST_HEAD(&xprt->xpt_ready);
158 INIT_LIST_HEAD(&xprt->xpt_deferred);
159 INIT_LIST_HEAD(&xprt->xpt_users);
160 mutex_init(&xprt->xpt_mutex);
161 spin_lock_init(&xprt->xpt_lock);
162 set_bit(XPT_BUSY, &xprt->xpt_flags);
163 rpc_init_wait_queue(&xprt->xpt_bc_pending, "xpt_bc_pending");
164 xprt->xpt_net = get_net(&init_net);
166 EXPORT_SYMBOL_GPL(svc_xprt_init);
168 static struct svc_xprt *__svc_xpo_create(struct svc_xprt_class *xcl,
169 struct svc_serv *serv,
170 struct net *net,
171 const int family,
172 const unsigned short port,
173 int flags)
175 struct sockaddr_in sin = {
176 .sin_family = AF_INET,
177 .sin_addr.s_addr = htonl(INADDR_ANY),
178 .sin_port = htons(port),
180 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
181 struct sockaddr_in6 sin6 = {
182 .sin6_family = AF_INET6,
183 .sin6_addr = IN6ADDR_ANY_INIT,
184 .sin6_port = htons(port),
186 #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */
187 struct sockaddr *sap;
188 size_t len;
190 switch (family) {
191 case PF_INET:
192 sap = (struct sockaddr *)&sin;
193 len = sizeof(sin);
194 break;
195 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
196 case PF_INET6:
197 sap = (struct sockaddr *)&sin6;
198 len = sizeof(sin6);
199 break;
200 #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */
201 default:
202 return ERR_PTR(-EAFNOSUPPORT);
205 return xcl->xcl_ops->xpo_create(serv, net, sap, len, flags);
208 int svc_create_xprt(struct svc_serv *serv, const char *xprt_name,
209 struct net *net, const int family,
210 const unsigned short port, int flags)
212 struct svc_xprt_class *xcl;
214 dprintk("svc: creating transport %s[%d]\n", xprt_name, port);
215 spin_lock(&svc_xprt_class_lock);
216 list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) {
217 struct svc_xprt *newxprt;
219 if (strcmp(xprt_name, xcl->xcl_name))
220 continue;
222 if (!try_module_get(xcl->xcl_owner))
223 goto err;
225 spin_unlock(&svc_xprt_class_lock);
226 newxprt = __svc_xpo_create(xcl, serv, net, family, port, flags);
227 if (IS_ERR(newxprt)) {
228 module_put(xcl->xcl_owner);
229 return PTR_ERR(newxprt);
232 clear_bit(XPT_TEMP, &newxprt->xpt_flags);
233 spin_lock_bh(&serv->sv_lock);
234 list_add(&newxprt->xpt_list, &serv->sv_permsocks);
235 spin_unlock_bh(&serv->sv_lock);
236 clear_bit(XPT_BUSY, &newxprt->xpt_flags);
237 return svc_xprt_local_port(newxprt);
239 err:
240 spin_unlock(&svc_xprt_class_lock);
241 dprintk("svc: transport %s not found\n", xprt_name);
243 /* This errno is exposed to user space. Provide a reasonable
244 * perror msg for a bad transport. */
245 return -EPROTONOSUPPORT;
247 EXPORT_SYMBOL_GPL(svc_create_xprt);
250 * Copy the local and remote xprt addresses to the rqstp structure
252 void svc_xprt_copy_addrs(struct svc_rqst *rqstp, struct svc_xprt *xprt)
254 struct sockaddr *sin;
256 memcpy(&rqstp->rq_addr, &xprt->xpt_remote, xprt->xpt_remotelen);
257 rqstp->rq_addrlen = xprt->xpt_remotelen;
260 * Destination address in request is needed for binding the
261 * source address in RPC replies/callbacks later.
263 sin = (struct sockaddr *)&xprt->xpt_local;
264 switch (sin->sa_family) {
265 case AF_INET:
266 rqstp->rq_daddr.addr = ((struct sockaddr_in *)sin)->sin_addr;
267 break;
268 case AF_INET6:
269 rqstp->rq_daddr.addr6 = ((struct sockaddr_in6 *)sin)->sin6_addr;
270 break;
273 EXPORT_SYMBOL_GPL(svc_xprt_copy_addrs);
276 * svc_print_addr - Format rq_addr field for printing
277 * @rqstp: svc_rqst struct containing address to print
278 * @buf: target buffer for formatted address
279 * @len: length of target buffer
282 char *svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len)
284 return __svc_print_addr(svc_addr(rqstp), buf, len);
286 EXPORT_SYMBOL_GPL(svc_print_addr);
289 * Queue up an idle server thread. Must have pool->sp_lock held.
290 * Note: this is really a stack rather than a queue, so that we only
291 * use as many different threads as we need, and the rest don't pollute
292 * the cache.
294 static void svc_thread_enqueue(struct svc_pool *pool, struct svc_rqst *rqstp)
296 list_add(&rqstp->rq_list, &pool->sp_threads);
300 * Dequeue an nfsd thread. Must have pool->sp_lock held.
302 static void svc_thread_dequeue(struct svc_pool *pool, struct svc_rqst *rqstp)
304 list_del(&rqstp->rq_list);
308 * Queue up a transport with data pending. If there are idle nfsd
309 * processes, wake 'em up.
312 void svc_xprt_enqueue(struct svc_xprt *xprt)
314 struct svc_serv *serv = xprt->xpt_server;
315 struct svc_pool *pool;
316 struct svc_rqst *rqstp;
317 int cpu;
319 if (!(xprt->xpt_flags &
320 ((1<<XPT_CONN)|(1<<XPT_DATA)|(1<<XPT_CLOSE)|(1<<XPT_DEFERRED))))
321 return;
323 cpu = get_cpu();
324 pool = svc_pool_for_cpu(xprt->xpt_server, cpu);
325 put_cpu();
327 spin_lock_bh(&pool->sp_lock);
329 if (!list_empty(&pool->sp_threads) &&
330 !list_empty(&pool->sp_sockets))
331 printk(KERN_ERR
332 "svc_xprt_enqueue: "
333 "threads and transports both waiting??\n");
335 if (test_bit(XPT_DEAD, &xprt->xpt_flags)) {
336 /* Don't enqueue dead transports */
337 dprintk("svc: transport %p is dead, not enqueued\n", xprt);
338 goto out_unlock;
341 pool->sp_stats.packets++;
343 /* Mark transport as busy. It will remain in this state until
344 * the provider calls svc_xprt_received. We update XPT_BUSY
345 * atomically because it also guards against trying to enqueue
346 * the transport twice.
348 if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags)) {
349 /* Don't enqueue transport while already enqueued */
350 dprintk("svc: transport %p busy, not enqueued\n", xprt);
351 goto out_unlock;
353 BUG_ON(xprt->xpt_pool != NULL);
354 xprt->xpt_pool = pool;
356 /* Handle pending connection */
357 if (test_bit(XPT_CONN, &xprt->xpt_flags))
358 goto process;
360 /* Handle close in-progress */
361 if (test_bit(XPT_CLOSE, &xprt->xpt_flags))
362 goto process;
364 /* Check if we have space to reply to a request */
365 if (!xprt->xpt_ops->xpo_has_wspace(xprt)) {
366 /* Don't enqueue while not enough space for reply */
367 dprintk("svc: no write space, transport %p not enqueued\n",
368 xprt);
369 xprt->xpt_pool = NULL;
370 clear_bit(XPT_BUSY, &xprt->xpt_flags);
371 goto out_unlock;
374 process:
375 if (!list_empty(&pool->sp_threads)) {
376 rqstp = list_entry(pool->sp_threads.next,
377 struct svc_rqst,
378 rq_list);
379 dprintk("svc: transport %p served by daemon %p\n",
380 xprt, rqstp);
381 svc_thread_dequeue(pool, rqstp);
382 if (rqstp->rq_xprt)
383 printk(KERN_ERR
384 "svc_xprt_enqueue: server %p, rq_xprt=%p!\n",
385 rqstp, rqstp->rq_xprt);
386 rqstp->rq_xprt = xprt;
387 svc_xprt_get(xprt);
388 rqstp->rq_reserved = serv->sv_max_mesg;
389 atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved);
390 pool->sp_stats.threads_woken++;
391 BUG_ON(xprt->xpt_pool != pool);
392 wake_up(&rqstp->rq_wait);
393 } else {
394 dprintk("svc: transport %p put into queue\n", xprt);
395 list_add_tail(&xprt->xpt_ready, &pool->sp_sockets);
396 pool->sp_stats.sockets_queued++;
397 BUG_ON(xprt->xpt_pool != pool);
400 out_unlock:
401 spin_unlock_bh(&pool->sp_lock);
403 EXPORT_SYMBOL_GPL(svc_xprt_enqueue);
406 * Dequeue the first transport. Must be called with the pool->sp_lock held.
408 static struct svc_xprt *svc_xprt_dequeue(struct svc_pool *pool)
410 struct svc_xprt *xprt;
412 if (list_empty(&pool->sp_sockets))
413 return NULL;
415 xprt = list_entry(pool->sp_sockets.next,
416 struct svc_xprt, xpt_ready);
417 list_del_init(&xprt->xpt_ready);
419 dprintk("svc: transport %p dequeued, inuse=%d\n",
420 xprt, atomic_read(&xprt->xpt_ref.refcount));
422 return xprt;
426 * svc_xprt_received conditionally queues the transport for processing
427 * by another thread. The caller must hold the XPT_BUSY bit and must
428 * not thereafter touch transport data.
430 * Note: XPT_DATA only gets cleared when a read-attempt finds no (or
431 * insufficient) data.
433 void svc_xprt_received(struct svc_xprt *xprt)
435 BUG_ON(!test_bit(XPT_BUSY, &xprt->xpt_flags));
436 xprt->xpt_pool = NULL;
437 clear_bit(XPT_BUSY, &xprt->xpt_flags);
438 svc_xprt_enqueue(xprt);
440 EXPORT_SYMBOL_GPL(svc_xprt_received);
443 * svc_reserve - change the space reserved for the reply to a request.
444 * @rqstp: The request in question
445 * @space: new max space to reserve
447 * Each request reserves some space on the output queue of the transport
448 * to make sure the reply fits. This function reduces that reserved
449 * space to be the amount of space used already, plus @space.
452 void svc_reserve(struct svc_rqst *rqstp, int space)
454 space += rqstp->rq_res.head[0].iov_len;
456 if (space < rqstp->rq_reserved) {
457 struct svc_xprt *xprt = rqstp->rq_xprt;
458 atomic_sub((rqstp->rq_reserved - space), &xprt->xpt_reserved);
459 rqstp->rq_reserved = space;
461 svc_xprt_enqueue(xprt);
464 EXPORT_SYMBOL_GPL(svc_reserve);
466 static void svc_xprt_release(struct svc_rqst *rqstp)
468 struct svc_xprt *xprt = rqstp->rq_xprt;
470 rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
472 kfree(rqstp->rq_deferred);
473 rqstp->rq_deferred = NULL;
475 svc_free_res_pages(rqstp);
476 rqstp->rq_res.page_len = 0;
477 rqstp->rq_res.page_base = 0;
479 /* Reset response buffer and release
480 * the reservation.
481 * But first, check that enough space was reserved
482 * for the reply, otherwise we have a bug!
484 if ((rqstp->rq_res.len) > rqstp->rq_reserved)
485 printk(KERN_ERR "RPC request reserved %d but used %d\n",
486 rqstp->rq_reserved,
487 rqstp->rq_res.len);
489 rqstp->rq_res.head[0].iov_len = 0;
490 svc_reserve(rqstp, 0);
491 rqstp->rq_xprt = NULL;
493 svc_xprt_put(xprt);
497 * External function to wake up a server waiting for data
498 * This really only makes sense for services like lockd
499 * which have exactly one thread anyway.
501 void svc_wake_up(struct svc_serv *serv)
503 struct svc_rqst *rqstp;
504 unsigned int i;
505 struct svc_pool *pool;
507 for (i = 0; i < serv->sv_nrpools; i++) {
508 pool = &serv->sv_pools[i];
510 spin_lock_bh(&pool->sp_lock);
511 if (!list_empty(&pool->sp_threads)) {
512 rqstp = list_entry(pool->sp_threads.next,
513 struct svc_rqst,
514 rq_list);
515 dprintk("svc: daemon %p woken up.\n", rqstp);
517 svc_thread_dequeue(pool, rqstp);
518 rqstp->rq_xprt = NULL;
520 wake_up(&rqstp->rq_wait);
522 spin_unlock_bh(&pool->sp_lock);
525 EXPORT_SYMBOL_GPL(svc_wake_up);
527 int svc_port_is_privileged(struct sockaddr *sin)
529 switch (sin->sa_family) {
530 case AF_INET:
531 return ntohs(((struct sockaddr_in *)sin)->sin_port)
532 < PROT_SOCK;
533 case AF_INET6:
534 return ntohs(((struct sockaddr_in6 *)sin)->sin6_port)
535 < PROT_SOCK;
536 default:
537 return 0;
542 * Make sure that we don't have too many active connections. If we have,
543 * something must be dropped. It's not clear what will happen if we allow
544 * "too many" connections, but when dealing with network-facing software,
545 * we have to code defensively. Here we do that by imposing hard limits.
547 * There's no point in trying to do random drop here for DoS
548 * prevention. The NFS clients does 1 reconnect in 15 seconds. An
549 * attacker can easily beat that.
551 * The only somewhat efficient mechanism would be if drop old
552 * connections from the same IP first. But right now we don't even
553 * record the client IP in svc_sock.
555 * single-threaded services that expect a lot of clients will probably
556 * need to set sv_maxconn to override the default value which is based
557 * on the number of threads
559 static void svc_check_conn_limits(struct svc_serv *serv)
561 unsigned int limit = serv->sv_maxconn ? serv->sv_maxconn :
562 (serv->sv_nrthreads+3) * 20;
564 if (serv->sv_tmpcnt > limit) {
565 struct svc_xprt *xprt = NULL;
566 spin_lock_bh(&serv->sv_lock);
567 if (!list_empty(&serv->sv_tempsocks)) {
568 if (net_ratelimit()) {
569 /* Try to help the admin */
570 printk(KERN_NOTICE "%s: too many open "
571 "connections, consider increasing %s\n",
572 serv->sv_name, serv->sv_maxconn ?
573 "the max number of connections." :
574 "the number of threads.");
577 * Always select the oldest connection. It's not fair,
578 * but so is life
580 xprt = list_entry(serv->sv_tempsocks.prev,
581 struct svc_xprt,
582 xpt_list);
583 set_bit(XPT_CLOSE, &xprt->xpt_flags);
584 svc_xprt_get(xprt);
586 spin_unlock_bh(&serv->sv_lock);
588 if (xprt) {
589 svc_xprt_enqueue(xprt);
590 svc_xprt_put(xprt);
596 * Receive the next request on any transport. This code is carefully
597 * organised not to touch any cachelines in the shared svc_serv
598 * structure, only cachelines in the local svc_pool.
600 int svc_recv(struct svc_rqst *rqstp, long timeout)
602 struct svc_xprt *xprt = NULL;
603 struct svc_serv *serv = rqstp->rq_server;
604 struct svc_pool *pool = rqstp->rq_pool;
605 int len, i;
606 int pages;
607 struct xdr_buf *arg;
608 DECLARE_WAITQUEUE(wait, current);
609 long time_left;
611 dprintk("svc: server %p waiting for data (to = %ld)\n",
612 rqstp, timeout);
614 if (rqstp->rq_xprt)
615 printk(KERN_ERR
616 "svc_recv: service %p, transport not NULL!\n",
617 rqstp);
618 if (waitqueue_active(&rqstp->rq_wait))
619 printk(KERN_ERR
620 "svc_recv: service %p, wait queue active!\n",
621 rqstp);
623 /* now allocate needed pages. If we get a failure, sleep briefly */
624 pages = (serv->sv_max_mesg + PAGE_SIZE) / PAGE_SIZE;
625 for (i = 0; i < pages ; i++)
626 while (rqstp->rq_pages[i] == NULL) {
627 struct page *p = alloc_page(GFP_KERNEL);
628 if (!p) {
629 set_current_state(TASK_INTERRUPTIBLE);
630 if (signalled() || kthread_should_stop()) {
631 set_current_state(TASK_RUNNING);
632 return -EINTR;
634 schedule_timeout(msecs_to_jiffies(500));
636 rqstp->rq_pages[i] = p;
638 rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */
639 BUG_ON(pages >= RPCSVC_MAXPAGES);
641 /* Make arg->head point to first page and arg->pages point to rest */
642 arg = &rqstp->rq_arg;
643 arg->head[0].iov_base = page_address(rqstp->rq_pages[0]);
644 arg->head[0].iov_len = PAGE_SIZE;
645 arg->pages = rqstp->rq_pages + 1;
646 arg->page_base = 0;
647 /* save at least one page for response */
648 arg->page_len = (pages-2)*PAGE_SIZE;
649 arg->len = (pages-1)*PAGE_SIZE;
650 arg->tail[0].iov_len = 0;
652 try_to_freeze();
653 cond_resched();
654 if (signalled() || kthread_should_stop())
655 return -EINTR;
657 /* Normally we will wait up to 5 seconds for any required
658 * cache information to be provided.
660 rqstp->rq_chandle.thread_wait = 5*HZ;
662 spin_lock_bh(&pool->sp_lock);
663 xprt = svc_xprt_dequeue(pool);
664 if (xprt) {
665 rqstp->rq_xprt = xprt;
666 svc_xprt_get(xprt);
667 rqstp->rq_reserved = serv->sv_max_mesg;
668 atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved);
670 /* As there is a shortage of threads and this request
671 * had to be queued, don't allow the thread to wait so
672 * long for cache updates.
674 rqstp->rq_chandle.thread_wait = 1*HZ;
675 } else {
676 /* No data pending. Go to sleep */
677 svc_thread_enqueue(pool, rqstp);
680 * We have to be able to interrupt this wait
681 * to bring down the daemons ...
683 set_current_state(TASK_INTERRUPTIBLE);
686 * checking kthread_should_stop() here allows us to avoid
687 * locking and signalling when stopping kthreads that call
688 * svc_recv. If the thread has already been woken up, then
689 * we can exit here without sleeping. If not, then it
690 * it'll be woken up quickly during the schedule_timeout
692 if (kthread_should_stop()) {
693 set_current_state(TASK_RUNNING);
694 spin_unlock_bh(&pool->sp_lock);
695 return -EINTR;
698 add_wait_queue(&rqstp->rq_wait, &wait);
699 spin_unlock_bh(&pool->sp_lock);
701 time_left = schedule_timeout(timeout);
703 try_to_freeze();
705 spin_lock_bh(&pool->sp_lock);
706 remove_wait_queue(&rqstp->rq_wait, &wait);
707 if (!time_left)
708 pool->sp_stats.threads_timedout++;
710 xprt = rqstp->rq_xprt;
711 if (!xprt) {
712 svc_thread_dequeue(pool, rqstp);
713 spin_unlock_bh(&pool->sp_lock);
714 dprintk("svc: server %p, no data yet\n", rqstp);
715 if (signalled() || kthread_should_stop())
716 return -EINTR;
717 else
718 return -EAGAIN;
721 spin_unlock_bh(&pool->sp_lock);
723 len = 0;
724 if (test_bit(XPT_CLOSE, &xprt->xpt_flags)) {
725 dprintk("svc_recv: found XPT_CLOSE\n");
726 svc_delete_xprt(xprt);
727 } else if (test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
728 struct svc_xprt *newxpt;
729 newxpt = xprt->xpt_ops->xpo_accept(xprt);
730 if (newxpt) {
732 * We know this module_get will succeed because the
733 * listener holds a reference too
735 __module_get(newxpt->xpt_class->xcl_owner);
736 svc_check_conn_limits(xprt->xpt_server);
737 spin_lock_bh(&serv->sv_lock);
738 set_bit(XPT_TEMP, &newxpt->xpt_flags);
739 list_add(&newxpt->xpt_list, &serv->sv_tempsocks);
740 serv->sv_tmpcnt++;
741 if (serv->sv_temptimer.function == NULL) {
742 /* setup timer to age temp transports */
743 setup_timer(&serv->sv_temptimer,
744 svc_age_temp_xprts,
745 (unsigned long)serv);
746 mod_timer(&serv->sv_temptimer,
747 jiffies + svc_conn_age_period * HZ);
749 spin_unlock_bh(&serv->sv_lock);
750 svc_xprt_received(newxpt);
752 svc_xprt_received(xprt);
753 } else {
754 dprintk("svc: server %p, pool %u, transport %p, inuse=%d\n",
755 rqstp, pool->sp_id, xprt,
756 atomic_read(&xprt->xpt_ref.refcount));
757 rqstp->rq_deferred = svc_deferred_dequeue(xprt);
758 if (rqstp->rq_deferred) {
759 svc_xprt_received(xprt);
760 len = svc_deferred_recv(rqstp);
761 } else {
762 len = xprt->xpt_ops->xpo_recvfrom(rqstp);
763 svc_xprt_received(xprt);
765 dprintk("svc: got len=%d\n", len);
768 /* No data, incomplete (TCP) read, or accept() */
769 if (len == 0 || len == -EAGAIN) {
770 rqstp->rq_res.len = 0;
771 svc_xprt_release(rqstp);
772 return -EAGAIN;
774 clear_bit(XPT_OLD, &xprt->xpt_flags);
776 rqstp->rq_secure = svc_port_is_privileged(svc_addr(rqstp));
777 rqstp->rq_chandle.defer = svc_defer;
779 if (serv->sv_stats)
780 serv->sv_stats->netcnt++;
781 return len;
783 EXPORT_SYMBOL_GPL(svc_recv);
786 * Drop request
788 void svc_drop(struct svc_rqst *rqstp)
790 dprintk("svc: xprt %p dropped request\n", rqstp->rq_xprt);
791 svc_xprt_release(rqstp);
793 EXPORT_SYMBOL_GPL(svc_drop);
796 * Return reply to client.
798 int svc_send(struct svc_rqst *rqstp)
800 struct svc_xprt *xprt;
801 int len;
802 struct xdr_buf *xb;
804 xprt = rqstp->rq_xprt;
805 if (!xprt)
806 return -EFAULT;
808 /* release the receive skb before sending the reply */
809 rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
811 /* calculate over-all length */
812 xb = &rqstp->rq_res;
813 xb->len = xb->head[0].iov_len +
814 xb->page_len +
815 xb->tail[0].iov_len;
817 /* Grab mutex to serialize outgoing data. */
818 mutex_lock(&xprt->xpt_mutex);
819 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
820 len = -ENOTCONN;
821 else
822 len = xprt->xpt_ops->xpo_sendto(rqstp);
823 mutex_unlock(&xprt->xpt_mutex);
824 rpc_wake_up(&xprt->xpt_bc_pending);
825 svc_xprt_release(rqstp);
827 if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
828 return 0;
829 return len;
833 * Timer function to close old temporary transports, using
834 * a mark-and-sweep algorithm.
836 static void svc_age_temp_xprts(unsigned long closure)
838 struct svc_serv *serv = (struct svc_serv *)closure;
839 struct svc_xprt *xprt;
840 struct list_head *le, *next;
841 LIST_HEAD(to_be_aged);
843 dprintk("svc_age_temp_xprts\n");
845 if (!spin_trylock_bh(&serv->sv_lock)) {
846 /* busy, try again 1 sec later */
847 dprintk("svc_age_temp_xprts: busy\n");
848 mod_timer(&serv->sv_temptimer, jiffies + HZ);
849 return;
852 list_for_each_safe(le, next, &serv->sv_tempsocks) {
853 xprt = list_entry(le, struct svc_xprt, xpt_list);
855 /* First time through, just mark it OLD. Second time
856 * through, close it. */
857 if (!test_and_set_bit(XPT_OLD, &xprt->xpt_flags))
858 continue;
859 if (atomic_read(&xprt->xpt_ref.refcount) > 1 ||
860 test_bit(XPT_BUSY, &xprt->xpt_flags))
861 continue;
862 svc_xprt_get(xprt);
863 list_move(le, &to_be_aged);
864 set_bit(XPT_CLOSE, &xprt->xpt_flags);
865 set_bit(XPT_DETACHED, &xprt->xpt_flags);
867 spin_unlock_bh(&serv->sv_lock);
869 while (!list_empty(&to_be_aged)) {
870 le = to_be_aged.next;
871 /* fiddling the xpt_list node is safe 'cos we're XPT_DETACHED */
872 list_del_init(le);
873 xprt = list_entry(le, struct svc_xprt, xpt_list);
875 dprintk("queuing xprt %p for closing\n", xprt);
877 /* a thread will dequeue and close it soon */
878 svc_xprt_enqueue(xprt);
879 svc_xprt_put(xprt);
882 mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
885 static void call_xpt_users(struct svc_xprt *xprt)
887 struct svc_xpt_user *u;
889 spin_lock(&xprt->xpt_lock);
890 while (!list_empty(&xprt->xpt_users)) {
891 u = list_first_entry(&xprt->xpt_users, struct svc_xpt_user, list);
892 list_del(&u->list);
893 u->callback(u);
895 spin_unlock(&xprt->xpt_lock);
899 * Remove a dead transport
901 void svc_delete_xprt(struct svc_xprt *xprt)
903 struct svc_serv *serv = xprt->xpt_server;
904 struct svc_deferred_req *dr;
906 /* Only do this once */
907 if (test_and_set_bit(XPT_DEAD, &xprt->xpt_flags))
908 return;
910 dprintk("svc: svc_delete_xprt(%p)\n", xprt);
911 xprt->xpt_ops->xpo_detach(xprt);
913 spin_lock_bh(&serv->sv_lock);
914 if (!test_and_set_bit(XPT_DETACHED, &xprt->xpt_flags))
915 list_del_init(&xprt->xpt_list);
917 * We used to delete the transport from whichever list
918 * it's sk_xprt.xpt_ready node was on, but we don't actually
919 * need to. This is because the only time we're called
920 * while still attached to a queue, the queue itself
921 * is about to be destroyed (in svc_destroy).
923 if (test_bit(XPT_TEMP, &xprt->xpt_flags))
924 serv->sv_tmpcnt--;
925 spin_unlock_bh(&serv->sv_lock);
927 while ((dr = svc_deferred_dequeue(xprt)) != NULL)
928 kfree(dr);
930 call_xpt_users(xprt);
931 svc_xprt_put(xprt);
934 void svc_close_xprt(struct svc_xprt *xprt)
936 set_bit(XPT_CLOSE, &xprt->xpt_flags);
937 if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags))
938 /* someone else will have to effect the close */
939 return;
941 svc_xprt_get(xprt);
942 svc_delete_xprt(xprt);
943 clear_bit(XPT_BUSY, &xprt->xpt_flags);
944 svc_xprt_put(xprt);
946 EXPORT_SYMBOL_GPL(svc_close_xprt);
948 void svc_close_all(struct list_head *xprt_list)
950 struct svc_xprt *xprt;
951 struct svc_xprt *tmp;
953 list_for_each_entry_safe(xprt, tmp, xprt_list, xpt_list) {
954 set_bit(XPT_CLOSE, &xprt->xpt_flags);
955 if (test_bit(XPT_BUSY, &xprt->xpt_flags)) {
956 /* Waiting to be processed, but no threads left,
957 * So just remove it from the waiting list
959 list_del_init(&xprt->xpt_ready);
960 clear_bit(XPT_BUSY, &xprt->xpt_flags);
962 svc_close_xprt(xprt);
967 * Handle defer and revisit of requests
970 static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
972 struct svc_deferred_req *dr =
973 container_of(dreq, struct svc_deferred_req, handle);
974 struct svc_xprt *xprt = dr->xprt;
976 spin_lock(&xprt->xpt_lock);
977 set_bit(XPT_DEFERRED, &xprt->xpt_flags);
978 if (too_many || test_bit(XPT_DEAD, &xprt->xpt_flags)) {
979 spin_unlock(&xprt->xpt_lock);
980 dprintk("revisit canceled\n");
981 svc_xprt_put(xprt);
982 kfree(dr);
983 return;
985 dprintk("revisit queued\n");
986 dr->xprt = NULL;
987 list_add(&dr->handle.recent, &xprt->xpt_deferred);
988 spin_unlock(&xprt->xpt_lock);
989 svc_xprt_enqueue(xprt);
990 svc_xprt_put(xprt);
994 * Save the request off for later processing. The request buffer looks
995 * like this:
997 * <xprt-header><rpc-header><rpc-pagelist><rpc-tail>
999 * This code can only handle requests that consist of an xprt-header
1000 * and rpc-header.
1002 static struct cache_deferred_req *svc_defer(struct cache_req *req)
1004 struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
1005 struct svc_deferred_req *dr;
1007 if (rqstp->rq_arg.page_len || !rqstp->rq_usedeferral)
1008 return NULL; /* if more than a page, give up FIXME */
1009 if (rqstp->rq_deferred) {
1010 dr = rqstp->rq_deferred;
1011 rqstp->rq_deferred = NULL;
1012 } else {
1013 size_t skip;
1014 size_t size;
1015 /* FIXME maybe discard if size too large */
1016 size = sizeof(struct svc_deferred_req) + rqstp->rq_arg.len;
1017 dr = kmalloc(size, GFP_KERNEL);
1018 if (dr == NULL)
1019 return NULL;
1021 dr->handle.owner = rqstp->rq_server;
1022 dr->prot = rqstp->rq_prot;
1023 memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen);
1024 dr->addrlen = rqstp->rq_addrlen;
1025 dr->daddr = rqstp->rq_daddr;
1026 dr->argslen = rqstp->rq_arg.len >> 2;
1027 dr->xprt_hlen = rqstp->rq_xprt_hlen;
1029 /* back up head to the start of the buffer and copy */
1030 skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
1031 memcpy(dr->args, rqstp->rq_arg.head[0].iov_base - skip,
1032 dr->argslen << 2);
1034 svc_xprt_get(rqstp->rq_xprt);
1035 dr->xprt = rqstp->rq_xprt;
1037 dr->handle.revisit = svc_revisit;
1038 return &dr->handle;
1042 * recv data from a deferred request into an active one
1044 static int svc_deferred_recv(struct svc_rqst *rqstp)
1046 struct svc_deferred_req *dr = rqstp->rq_deferred;
1048 /* setup iov_base past transport header */
1049 rqstp->rq_arg.head[0].iov_base = dr->args + (dr->xprt_hlen>>2);
1050 /* The iov_len does not include the transport header bytes */
1051 rqstp->rq_arg.head[0].iov_len = (dr->argslen<<2) - dr->xprt_hlen;
1052 rqstp->rq_arg.page_len = 0;
1053 /* The rq_arg.len includes the transport header bytes */
1054 rqstp->rq_arg.len = dr->argslen<<2;
1055 rqstp->rq_prot = dr->prot;
1056 memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen);
1057 rqstp->rq_addrlen = dr->addrlen;
1058 /* Save off transport header len in case we get deferred again */
1059 rqstp->rq_xprt_hlen = dr->xprt_hlen;
1060 rqstp->rq_daddr = dr->daddr;
1061 rqstp->rq_respages = rqstp->rq_pages;
1062 return (dr->argslen<<2) - dr->xprt_hlen;
1066 static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt)
1068 struct svc_deferred_req *dr = NULL;
1070 if (!test_bit(XPT_DEFERRED, &xprt->xpt_flags))
1071 return NULL;
1072 spin_lock(&xprt->xpt_lock);
1073 clear_bit(XPT_DEFERRED, &xprt->xpt_flags);
1074 if (!list_empty(&xprt->xpt_deferred)) {
1075 dr = list_entry(xprt->xpt_deferred.next,
1076 struct svc_deferred_req,
1077 handle.recent);
1078 list_del_init(&dr->handle.recent);
1079 set_bit(XPT_DEFERRED, &xprt->xpt_flags);
1081 spin_unlock(&xprt->xpt_lock);
1082 return dr;
1086 * svc_find_xprt - find an RPC transport instance
1087 * @serv: pointer to svc_serv to search
1088 * @xcl_name: C string containing transport's class name
1089 * @af: Address family of transport's local address
1090 * @port: transport's IP port number
1092 * Return the transport instance pointer for the endpoint accepting
1093 * connections/peer traffic from the specified transport class,
1094 * address family and port.
1096 * Specifying 0 for the address family or port is effectively a
1097 * wild-card, and will result in matching the first transport in the
1098 * service's list that has a matching class name.
1100 struct svc_xprt *svc_find_xprt(struct svc_serv *serv, const char *xcl_name,
1101 const sa_family_t af, const unsigned short port)
1103 struct svc_xprt *xprt;
1104 struct svc_xprt *found = NULL;
1106 /* Sanity check the args */
1107 if (serv == NULL || xcl_name == NULL)
1108 return found;
1110 spin_lock_bh(&serv->sv_lock);
1111 list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
1112 if (strcmp(xprt->xpt_class->xcl_name, xcl_name))
1113 continue;
1114 if (af != AF_UNSPEC && af != xprt->xpt_local.ss_family)
1115 continue;
1116 if (port != 0 && port != svc_xprt_local_port(xprt))
1117 continue;
1118 found = xprt;
1119 svc_xprt_get(xprt);
1120 break;
1122 spin_unlock_bh(&serv->sv_lock);
1123 return found;
1125 EXPORT_SYMBOL_GPL(svc_find_xprt);
1127 static int svc_one_xprt_name(const struct svc_xprt *xprt,
1128 char *pos, int remaining)
1130 int len;
1132 len = snprintf(pos, remaining, "%s %u\n",
1133 xprt->xpt_class->xcl_name,
1134 svc_xprt_local_port(xprt));
1135 if (len >= remaining)
1136 return -ENAMETOOLONG;
1137 return len;
1141 * svc_xprt_names - format a buffer with a list of transport names
1142 * @serv: pointer to an RPC service
1143 * @buf: pointer to a buffer to be filled in
1144 * @buflen: length of buffer to be filled in
1146 * Fills in @buf with a string containing a list of transport names,
1147 * each name terminated with '\n'.
1149 * Returns positive length of the filled-in string on success; otherwise
1150 * a negative errno value is returned if an error occurs.
1152 int svc_xprt_names(struct svc_serv *serv, char *buf, const int buflen)
1154 struct svc_xprt *xprt;
1155 int len, totlen;
1156 char *pos;
1158 /* Sanity check args */
1159 if (!serv)
1160 return 0;
1162 spin_lock_bh(&serv->sv_lock);
1164 pos = buf;
1165 totlen = 0;
1166 list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
1167 len = svc_one_xprt_name(xprt, pos, buflen - totlen);
1168 if (len < 0) {
1169 *buf = '\0';
1170 totlen = len;
1172 if (len <= 0)
1173 break;
1175 pos += len;
1176 totlen += len;
1179 spin_unlock_bh(&serv->sv_lock);
1180 return totlen;
1182 EXPORT_SYMBOL_GPL(svc_xprt_names);
1185 /*----------------------------------------------------------------------------*/
1187 static void *svc_pool_stats_start(struct seq_file *m, loff_t *pos)
1189 unsigned int pidx = (unsigned int)*pos;
1190 struct svc_serv *serv = m->private;
1192 dprintk("svc_pool_stats_start, *pidx=%u\n", pidx);
1194 if (!pidx)
1195 return SEQ_START_TOKEN;
1196 return (pidx > serv->sv_nrpools ? NULL : &serv->sv_pools[pidx-1]);
1199 static void *svc_pool_stats_next(struct seq_file *m, void *p, loff_t *pos)
1201 struct svc_pool *pool = p;
1202 struct svc_serv *serv = m->private;
1204 dprintk("svc_pool_stats_next, *pos=%llu\n", *pos);
1206 if (p == SEQ_START_TOKEN) {
1207 pool = &serv->sv_pools[0];
1208 } else {
1209 unsigned int pidx = (pool - &serv->sv_pools[0]);
1210 if (pidx < serv->sv_nrpools-1)
1211 pool = &serv->sv_pools[pidx+1];
1212 else
1213 pool = NULL;
1215 ++*pos;
1216 return pool;
1219 static void svc_pool_stats_stop(struct seq_file *m, void *p)
1223 static int svc_pool_stats_show(struct seq_file *m, void *p)
1225 struct svc_pool *pool = p;
1227 if (p == SEQ_START_TOKEN) {
1228 seq_puts(m, "# pool packets-arrived sockets-enqueued threads-woken threads-timedout\n");
1229 return 0;
1232 seq_printf(m, "%u %lu %lu %lu %lu\n",
1233 pool->sp_id,
1234 pool->sp_stats.packets,
1235 pool->sp_stats.sockets_queued,
1236 pool->sp_stats.threads_woken,
1237 pool->sp_stats.threads_timedout);
1239 return 0;
1242 static const struct seq_operations svc_pool_stats_seq_ops = {
1243 .start = svc_pool_stats_start,
1244 .next = svc_pool_stats_next,
1245 .stop = svc_pool_stats_stop,
1246 .show = svc_pool_stats_show,
1249 int svc_pool_stats_open(struct svc_serv *serv, struct file *file)
1251 int err;
1253 err = seq_open(file, &svc_pool_stats_seq_ops);
1254 if (!err)
1255 ((struct seq_file *) file->private_data)->private = serv;
1256 return err;
1258 EXPORT_SYMBOL(svc_pool_stats_open);
1260 /*----------------------------------------------------------------------------*/