2 * linux/net/sunrpc/svc_xprt.c
4 * Author: Tom Tucker <tom@opengridcomputing.com>
7 #include <linux/sched.h>
8 #include <linux/errno.h>
9 #include <linux/fcntl.h>
10 #include <linux/net.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>
22 #include <net/checksum.h>
25 #include <net/tcp_states.h>
26 #include <linux/uaccess.h>
27 #include <asm/ioctls.h>
29 #include <linux/sunrpc/types.h>
30 #include <linux/sunrpc/clnt.h>
31 #include <linux/sunrpc/xdr.h>
32 #include <linux/sunrpc/svcsock.h>
33 #include <linux/sunrpc/stats.h>
34 #include <linux/sunrpc/svc_xprt.h>
36 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
38 static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt);
39 static int svc_deferred_recv(struct svc_rqst *rqstp);
40 static struct cache_deferred_req *svc_defer(struct cache_req *req);
41 static void svc_age_temp_xprts(unsigned long closure);
43 /* apparently the "standard" is that clients close
44 * idle connections after 5 minutes, servers after
46 * http://www.connectathon.org/talks96/nfstcp.pdf
48 static int svc_conn_age_period = 6*60;
50 /* List of registered transport classes */
51 static DEFINE_SPINLOCK(svc_xprt_class_lock);
52 static LIST_HEAD(svc_xprt_class_list);
54 /* SMP locking strategy:
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.
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.
68 * Some flags can be set to certain values at any time
69 * providing that certain rules are followed:
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.
78 * - Can set at any time. It is never cleared.
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.
85 int svc_reg_xprt_class(struct svc_xprt_class *xcl)
87 struct svc_xprt_class *cl;
90 dprintk("svc: Adding svc transport class '%s'\n", xcl->xcl_name);
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)
99 list_add_tail(&xcl->xcl_list, &svc_xprt_class_list);
102 spin_unlock(&svc_xprt_class_lock);
105 EXPORT_SYMBOL_GPL(svc_reg_xprt_class);
107 void svc_unreg_xprt_class(struct svc_xprt_class *xcl)
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);
114 EXPORT_SYMBOL_GPL(svc_unreg_xprt_class);
116 static void svc_xprt_free(struct kref *kref)
118 struct svc_xprt *xprt =
119 container_of(kref, struct svc_xprt, xpt_ref);
120 struct module *owner = xprt->xpt_class->xcl_owner;
121 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)
122 && xprt->xpt_auth_cache != NULL)
123 svcauth_unix_info_release(xprt->xpt_auth_cache);
124 xprt->xpt_ops->xpo_free(xprt);
128 void svc_xprt_put(struct svc_xprt *xprt)
130 kref_put(&xprt->xpt_ref, svc_xprt_free);
132 EXPORT_SYMBOL_GPL(svc_xprt_put);
135 * Called by transport drivers to initialize the transport independent
136 * portion of the transport instance.
138 void svc_xprt_init(struct svc_xprt_class *xcl, struct svc_xprt *xprt,
139 struct svc_serv *serv)
141 memset(xprt, 0, sizeof(*xprt));
142 xprt->xpt_class = xcl;
143 xprt->xpt_ops = xcl->xcl_ops;
144 kref_init(&xprt->xpt_ref);
145 xprt->xpt_server = serv;
146 INIT_LIST_HEAD(&xprt->xpt_list);
147 INIT_LIST_HEAD(&xprt->xpt_ready);
148 INIT_LIST_HEAD(&xprt->xpt_deferred);
149 mutex_init(&xprt->xpt_mutex);
150 spin_lock_init(&xprt->xpt_lock);
151 set_bit(XPT_BUSY, &xprt->xpt_flags);
153 EXPORT_SYMBOL_GPL(svc_xprt_init);
155 int svc_create_xprt(struct svc_serv *serv, char *xprt_name, unsigned short port,
158 struct svc_xprt_class *xcl;
159 struct sockaddr_in sin = {
160 .sin_family = AF_INET,
161 .sin_addr.s_addr = INADDR_ANY,
162 .sin_port = htons(port),
164 dprintk("svc: creating transport %s[%d]\n", xprt_name, port);
165 spin_lock(&svc_xprt_class_lock);
166 list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) {
167 struct svc_xprt *newxprt;
169 if (strcmp(xprt_name, xcl->xcl_name))
172 if (!try_module_get(xcl->xcl_owner))
175 spin_unlock(&svc_xprt_class_lock);
176 newxprt = xcl->xcl_ops->
177 xpo_create(serv, (struct sockaddr *)&sin, sizeof(sin),
179 if (IS_ERR(newxprt)) {
180 module_put(xcl->xcl_owner);
181 return PTR_ERR(newxprt);
184 clear_bit(XPT_TEMP, &newxprt->xpt_flags);
185 spin_lock_bh(&serv->sv_lock);
186 list_add(&newxprt->xpt_list, &serv->sv_permsocks);
187 spin_unlock_bh(&serv->sv_lock);
188 clear_bit(XPT_BUSY, &newxprt->xpt_flags);
189 return svc_xprt_local_port(newxprt);
192 spin_unlock(&svc_xprt_class_lock);
193 dprintk("svc: transport %s not found\n", xprt_name);
196 EXPORT_SYMBOL_GPL(svc_create_xprt);
199 * Copy the local and remote xprt addresses to the rqstp structure
201 void svc_xprt_copy_addrs(struct svc_rqst *rqstp, struct svc_xprt *xprt)
203 struct sockaddr *sin;
205 memcpy(&rqstp->rq_addr, &xprt->xpt_remote, xprt->xpt_remotelen);
206 rqstp->rq_addrlen = xprt->xpt_remotelen;
209 * Destination address in request is needed for binding the
210 * source address in RPC replies/callbacks later.
212 sin = (struct sockaddr *)&xprt->xpt_local;
213 switch (sin->sa_family) {
215 rqstp->rq_daddr.addr = ((struct sockaddr_in *)sin)->sin_addr;
218 rqstp->rq_daddr.addr6 = ((struct sockaddr_in6 *)sin)->sin6_addr;
222 EXPORT_SYMBOL_GPL(svc_xprt_copy_addrs);
225 * svc_print_addr - Format rq_addr field for printing
226 * @rqstp: svc_rqst struct containing address to print
227 * @buf: target buffer for formatted address
228 * @len: length of target buffer
231 char *svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len)
233 return __svc_print_addr(svc_addr(rqstp), buf, len);
235 EXPORT_SYMBOL_GPL(svc_print_addr);
238 * Queue up an idle server thread. Must have pool->sp_lock held.
239 * Note: this is really a stack rather than a queue, so that we only
240 * use as many different threads as we need, and the rest don't pollute
243 static void svc_thread_enqueue(struct svc_pool *pool, struct svc_rqst *rqstp)
245 list_add(&rqstp->rq_list, &pool->sp_threads);
249 * Dequeue an nfsd thread. Must have pool->sp_lock held.
251 static void svc_thread_dequeue(struct svc_pool *pool, struct svc_rqst *rqstp)
253 list_del(&rqstp->rq_list);
257 * Queue up a transport with data pending. If there are idle nfsd
258 * processes, wake 'em up.
261 void svc_xprt_enqueue(struct svc_xprt *xprt)
263 struct svc_serv *serv = xprt->xpt_server;
264 struct svc_pool *pool;
265 struct svc_rqst *rqstp;
268 if (!(xprt->xpt_flags &
269 ((1<<XPT_CONN)|(1<<XPT_DATA)|(1<<XPT_CLOSE)|(1<<XPT_DEFERRED))))
271 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
275 pool = svc_pool_for_cpu(xprt->xpt_server, cpu);
278 spin_lock_bh(&pool->sp_lock);
280 if (!list_empty(&pool->sp_threads) &&
281 !list_empty(&pool->sp_sockets))
284 "threads and transports both waiting??\n");
286 if (test_bit(XPT_DEAD, &xprt->xpt_flags)) {
287 /* Don't enqueue dead transports */
288 dprintk("svc: transport %p is dead, not enqueued\n", xprt);
292 /* Mark transport as busy. It will remain in this state until
293 * the provider calls svc_xprt_received. We update XPT_BUSY
294 * atomically because it also guards against trying to enqueue
295 * the transport twice.
297 if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags)) {
298 /* Don't enqueue transport while already enqueued */
299 dprintk("svc: transport %p busy, not enqueued\n", xprt);
302 BUG_ON(xprt->xpt_pool != NULL);
303 xprt->xpt_pool = pool;
305 /* Handle pending connection */
306 if (test_bit(XPT_CONN, &xprt->xpt_flags))
309 /* Handle close in-progress */
310 if (test_bit(XPT_CLOSE, &xprt->xpt_flags))
313 /* Check if we have space to reply to a request */
314 if (!xprt->xpt_ops->xpo_has_wspace(xprt)) {
315 /* Don't enqueue while not enough space for reply */
316 dprintk("svc: no write space, transport %p not enqueued\n",
318 xprt->xpt_pool = NULL;
319 clear_bit(XPT_BUSY, &xprt->xpt_flags);
324 if (!list_empty(&pool->sp_threads)) {
325 rqstp = list_entry(pool->sp_threads.next,
328 dprintk("svc: transport %p served by daemon %p\n",
330 svc_thread_dequeue(pool, rqstp);
333 "svc_xprt_enqueue: server %p, rq_xprt=%p!\n",
334 rqstp, rqstp->rq_xprt);
335 rqstp->rq_xprt = xprt;
337 rqstp->rq_reserved = serv->sv_max_mesg;
338 atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved);
339 BUG_ON(xprt->xpt_pool != pool);
340 wake_up(&rqstp->rq_wait);
342 dprintk("svc: transport %p put into queue\n", xprt);
343 list_add_tail(&xprt->xpt_ready, &pool->sp_sockets);
344 BUG_ON(xprt->xpt_pool != pool);
348 spin_unlock_bh(&pool->sp_lock);
350 EXPORT_SYMBOL_GPL(svc_xprt_enqueue);
353 * Dequeue the first transport. Must be called with the pool->sp_lock held.
355 static struct svc_xprt *svc_xprt_dequeue(struct svc_pool *pool)
357 struct svc_xprt *xprt;
359 if (list_empty(&pool->sp_sockets))
362 xprt = list_entry(pool->sp_sockets.next,
363 struct svc_xprt, xpt_ready);
364 list_del_init(&xprt->xpt_ready);
366 dprintk("svc: transport %p dequeued, inuse=%d\n",
367 xprt, atomic_read(&xprt->xpt_ref.refcount));
373 * svc_xprt_received conditionally queues the transport for processing
374 * by another thread. The caller must hold the XPT_BUSY bit and must
375 * not thereafter touch transport data.
377 * Note: XPT_DATA only gets cleared when a read-attempt finds no (or
378 * insufficient) data.
380 void svc_xprt_received(struct svc_xprt *xprt)
382 BUG_ON(!test_bit(XPT_BUSY, &xprt->xpt_flags));
383 xprt->xpt_pool = NULL;
384 clear_bit(XPT_BUSY, &xprt->xpt_flags);
385 svc_xprt_enqueue(xprt);
387 EXPORT_SYMBOL_GPL(svc_xprt_received);
390 * svc_reserve - change the space reserved for the reply to a request.
391 * @rqstp: The request in question
392 * @space: new max space to reserve
394 * Each request reserves some space on the output queue of the transport
395 * to make sure the reply fits. This function reduces that reserved
396 * space to be the amount of space used already, plus @space.
399 void svc_reserve(struct svc_rqst *rqstp, int space)
401 space += rqstp->rq_res.head[0].iov_len;
403 if (space < rqstp->rq_reserved) {
404 struct svc_xprt *xprt = rqstp->rq_xprt;
405 atomic_sub((rqstp->rq_reserved - space), &xprt->xpt_reserved);
406 rqstp->rq_reserved = space;
408 svc_xprt_enqueue(xprt);
412 static void svc_xprt_release(struct svc_rqst *rqstp)
414 struct svc_xprt *xprt = rqstp->rq_xprt;
416 rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
418 svc_free_res_pages(rqstp);
419 rqstp->rq_res.page_len = 0;
420 rqstp->rq_res.page_base = 0;
422 /* Reset response buffer and release
424 * But first, check that enough space was reserved
425 * for the reply, otherwise we have a bug!
427 if ((rqstp->rq_res.len) > rqstp->rq_reserved)
428 printk(KERN_ERR "RPC request reserved %d but used %d\n",
432 rqstp->rq_res.head[0].iov_len = 0;
433 svc_reserve(rqstp, 0);
434 rqstp->rq_xprt = NULL;
440 * External function to wake up a server waiting for data
441 * This really only makes sense for services like lockd
442 * which have exactly one thread anyway.
444 void svc_wake_up(struct svc_serv *serv)
446 struct svc_rqst *rqstp;
448 struct svc_pool *pool;
450 for (i = 0; i < serv->sv_nrpools; i++) {
451 pool = &serv->sv_pools[i];
453 spin_lock_bh(&pool->sp_lock);
454 if (!list_empty(&pool->sp_threads)) {
455 rqstp = list_entry(pool->sp_threads.next,
458 dprintk("svc: daemon %p woken up.\n", rqstp);
460 svc_thread_dequeue(pool, rqstp);
461 rqstp->rq_xprt = NULL;
463 wake_up(&rqstp->rq_wait);
465 spin_unlock_bh(&pool->sp_lock);
469 int svc_port_is_privileged(struct sockaddr *sin)
471 switch (sin->sa_family) {
473 return ntohs(((struct sockaddr_in *)sin)->sin_port)
476 return ntohs(((struct sockaddr_in6 *)sin)->sin6_port)
484 * Make sure that we don't have too many active connections. If we
485 * have, something must be dropped.
487 * There's no point in trying to do random drop here for DoS
488 * prevention. The NFS clients does 1 reconnect in 15 seconds. An
489 * attacker can easily beat that.
491 * The only somewhat efficient mechanism would be if drop old
492 * connections from the same IP first. But right now we don't even
493 * record the client IP in svc_sock.
495 static void svc_check_conn_limits(struct svc_serv *serv)
497 if (serv->sv_tmpcnt > (serv->sv_nrthreads+3)*20) {
498 struct svc_xprt *xprt = NULL;
499 spin_lock_bh(&serv->sv_lock);
500 if (!list_empty(&serv->sv_tempsocks)) {
501 if (net_ratelimit()) {
502 /* Try to help the admin */
503 printk(KERN_NOTICE "%s: too many open "
504 "connections, consider increasing the "
505 "number of nfsd threads\n",
509 * Always select the oldest connection. It's not fair,
512 xprt = list_entry(serv->sv_tempsocks.prev,
515 set_bit(XPT_CLOSE, &xprt->xpt_flags);
518 spin_unlock_bh(&serv->sv_lock);
521 svc_xprt_enqueue(xprt);
528 * Receive the next request on any transport. This code is carefully
529 * organised not to touch any cachelines in the shared svc_serv
530 * structure, only cachelines in the local svc_pool.
532 int svc_recv(struct svc_rqst *rqstp, long timeout)
534 struct svc_xprt *xprt = NULL;
535 struct svc_serv *serv = rqstp->rq_server;
536 struct svc_pool *pool = rqstp->rq_pool;
540 DECLARE_WAITQUEUE(wait, current);
542 dprintk("svc: server %p waiting for data (to = %ld)\n",
547 "svc_recv: service %p, transport not NULL!\n",
549 if (waitqueue_active(&rqstp->rq_wait))
551 "svc_recv: service %p, wait queue active!\n",
554 /* now allocate needed pages. If we get a failure, sleep briefly */
555 pages = (serv->sv_max_mesg + PAGE_SIZE) / PAGE_SIZE;
556 for (i = 0; i < pages ; i++)
557 while (rqstp->rq_pages[i] == NULL) {
558 struct page *p = alloc_page(GFP_KERNEL);
560 int j = msecs_to_jiffies(500);
561 schedule_timeout_uninterruptible(j);
563 rqstp->rq_pages[i] = p;
565 rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */
566 BUG_ON(pages >= RPCSVC_MAXPAGES);
568 /* Make arg->head point to first page and arg->pages point to rest */
569 arg = &rqstp->rq_arg;
570 arg->head[0].iov_base = page_address(rqstp->rq_pages[0]);
571 arg->head[0].iov_len = PAGE_SIZE;
572 arg->pages = rqstp->rq_pages + 1;
574 /* save at least one page for response */
575 arg->page_len = (pages-2)*PAGE_SIZE;
576 arg->len = (pages-1)*PAGE_SIZE;
577 arg->tail[0].iov_len = 0;
584 spin_lock_bh(&pool->sp_lock);
585 xprt = svc_xprt_dequeue(pool);
587 rqstp->rq_xprt = xprt;
589 rqstp->rq_reserved = serv->sv_max_mesg;
590 atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved);
592 /* No data pending. Go to sleep */
593 svc_thread_enqueue(pool, rqstp);
596 * We have to be able to interrupt this wait
597 * to bring down the daemons ...
599 set_current_state(TASK_INTERRUPTIBLE);
600 add_wait_queue(&rqstp->rq_wait, &wait);
601 spin_unlock_bh(&pool->sp_lock);
603 schedule_timeout(timeout);
607 spin_lock_bh(&pool->sp_lock);
608 remove_wait_queue(&rqstp->rq_wait, &wait);
610 xprt = rqstp->rq_xprt;
612 svc_thread_dequeue(pool, rqstp);
613 spin_unlock_bh(&pool->sp_lock);
614 dprintk("svc: server %p, no data yet\n", rqstp);
615 return signalled()? -EINTR : -EAGAIN;
618 spin_unlock_bh(&pool->sp_lock);
621 if (test_bit(XPT_CLOSE, &xprt->xpt_flags)) {
622 dprintk("svc_recv: found XPT_CLOSE\n");
623 svc_delete_xprt(xprt);
624 } else if (test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
625 struct svc_xprt *newxpt;
626 newxpt = xprt->xpt_ops->xpo_accept(xprt);
629 * We know this module_get will succeed because the
630 * listener holds a reference too
632 __module_get(newxpt->xpt_class->xcl_owner);
633 svc_check_conn_limits(xprt->xpt_server);
634 spin_lock_bh(&serv->sv_lock);
635 set_bit(XPT_TEMP, &newxpt->xpt_flags);
636 list_add(&newxpt->xpt_list, &serv->sv_tempsocks);
638 if (serv->sv_temptimer.function == NULL) {
639 /* setup timer to age temp transports */
640 setup_timer(&serv->sv_temptimer,
642 (unsigned long)serv);
643 mod_timer(&serv->sv_temptimer,
644 jiffies + svc_conn_age_period * HZ);
646 spin_unlock_bh(&serv->sv_lock);
647 svc_xprt_received(newxpt);
649 svc_xprt_received(xprt);
651 dprintk("svc: server %p, pool %u, transport %p, inuse=%d\n",
652 rqstp, pool->sp_id, xprt,
653 atomic_read(&xprt->xpt_ref.refcount));
654 rqstp->rq_deferred = svc_deferred_dequeue(xprt);
655 if (rqstp->rq_deferred) {
656 svc_xprt_received(xprt);
657 len = svc_deferred_recv(rqstp);
659 len = xprt->xpt_ops->xpo_recvfrom(rqstp);
660 dprintk("svc: got len=%d\n", len);
663 /* No data, incomplete (TCP) read, or accept() */
664 if (len == 0 || len == -EAGAIN) {
665 rqstp->rq_res.len = 0;
666 svc_xprt_release(rqstp);
669 clear_bit(XPT_OLD, &xprt->xpt_flags);
671 rqstp->rq_secure = svc_port_is_privileged(svc_addr(rqstp));
672 rqstp->rq_chandle.defer = svc_defer;
675 serv->sv_stats->netcnt++;
682 void svc_drop(struct svc_rqst *rqstp)
684 dprintk("svc: xprt %p dropped request\n", rqstp->rq_xprt);
685 svc_xprt_release(rqstp);
689 * Return reply to client.
691 int svc_send(struct svc_rqst *rqstp)
693 struct svc_xprt *xprt;
697 xprt = rqstp->rq_xprt;
701 /* release the receive skb before sending the reply */
702 rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
704 /* calculate over-all length */
706 xb->len = xb->head[0].iov_len +
710 /* Grab mutex to serialize outgoing data. */
711 mutex_lock(&xprt->xpt_mutex);
712 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
715 len = xprt->xpt_ops->xpo_sendto(rqstp);
716 mutex_unlock(&xprt->xpt_mutex);
717 svc_xprt_release(rqstp);
719 if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
725 * Timer function to close old temporary transports, using
726 * a mark-and-sweep algorithm.
728 static void svc_age_temp_xprts(unsigned long closure)
730 struct svc_serv *serv = (struct svc_serv *)closure;
731 struct svc_xprt *xprt;
732 struct list_head *le, *next;
733 LIST_HEAD(to_be_aged);
735 dprintk("svc_age_temp_xprts\n");
737 if (!spin_trylock_bh(&serv->sv_lock)) {
738 /* busy, try again 1 sec later */
739 dprintk("svc_age_temp_xprts: busy\n");
740 mod_timer(&serv->sv_temptimer, jiffies + HZ);
744 list_for_each_safe(le, next, &serv->sv_tempsocks) {
745 xprt = list_entry(le, struct svc_xprt, xpt_list);
747 /* First time through, just mark it OLD. Second time
748 * through, close it. */
749 if (!test_and_set_bit(XPT_OLD, &xprt->xpt_flags))
751 if (atomic_read(&xprt->xpt_ref.refcount) > 1
752 || test_bit(XPT_BUSY, &xprt->xpt_flags))
755 list_move(le, &to_be_aged);
756 set_bit(XPT_CLOSE, &xprt->xpt_flags);
757 set_bit(XPT_DETACHED, &xprt->xpt_flags);
759 spin_unlock_bh(&serv->sv_lock);
761 while (!list_empty(&to_be_aged)) {
762 le = to_be_aged.next;
763 /* fiddling the xpt_list node is safe 'cos we're XPT_DETACHED */
765 xprt = list_entry(le, struct svc_xprt, xpt_list);
767 dprintk("queuing xprt %p for closing\n", xprt);
769 /* a thread will dequeue and close it soon */
770 svc_xprt_enqueue(xprt);
774 mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
778 * Remove a dead transport
780 void svc_delete_xprt(struct svc_xprt *xprt)
782 struct svc_serv *serv = xprt->xpt_server;
784 dprintk("svc: svc_delete_xprt(%p)\n", xprt);
785 xprt->xpt_ops->xpo_detach(xprt);
787 spin_lock_bh(&serv->sv_lock);
788 if (!test_and_set_bit(XPT_DETACHED, &xprt->xpt_flags))
789 list_del_init(&xprt->xpt_list);
791 * We used to delete the transport from whichever list
792 * it's sk_xprt.xpt_ready node was on, but we don't actually
793 * need to. This is because the only time we're called
794 * while still attached to a queue, the queue itself
795 * is about to be destroyed (in svc_destroy).
797 if (!test_and_set_bit(XPT_DEAD, &xprt->xpt_flags)) {
798 BUG_ON(atomic_read(&xprt->xpt_ref.refcount) < 2);
799 if (test_bit(XPT_TEMP, &xprt->xpt_flags))
803 spin_unlock_bh(&serv->sv_lock);
806 void svc_close_xprt(struct svc_xprt *xprt)
808 set_bit(XPT_CLOSE, &xprt->xpt_flags);
809 if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags))
810 /* someone else will have to effect the close */
814 svc_delete_xprt(xprt);
815 clear_bit(XPT_BUSY, &xprt->xpt_flags);
819 void svc_close_all(struct list_head *xprt_list)
821 struct svc_xprt *xprt;
822 struct svc_xprt *tmp;
824 list_for_each_entry_safe(xprt, tmp, xprt_list, xpt_list) {
825 set_bit(XPT_CLOSE, &xprt->xpt_flags);
826 if (test_bit(XPT_BUSY, &xprt->xpt_flags)) {
827 /* Waiting to be processed, but no threads left,
828 * So just remove it from the waiting list
830 list_del_init(&xprt->xpt_ready);
831 clear_bit(XPT_BUSY, &xprt->xpt_flags);
833 svc_close_xprt(xprt);
838 * Handle defer and revisit of requests
841 static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
843 struct svc_deferred_req *dr =
844 container_of(dreq, struct svc_deferred_req, handle);
845 struct svc_xprt *xprt = dr->xprt;
852 dprintk("revisit queued\n");
854 spin_lock(&xprt->xpt_lock);
855 list_add(&dr->handle.recent, &xprt->xpt_deferred);
856 spin_unlock(&xprt->xpt_lock);
857 set_bit(XPT_DEFERRED, &xprt->xpt_flags);
858 svc_xprt_enqueue(xprt);
862 static struct cache_deferred_req *svc_defer(struct cache_req *req)
864 struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
865 int size = sizeof(struct svc_deferred_req) + (rqstp->rq_arg.len);
866 struct svc_deferred_req *dr;
868 if (rqstp->rq_arg.page_len)
869 return NULL; /* if more than a page, give up FIXME */
870 if (rqstp->rq_deferred) {
871 dr = rqstp->rq_deferred;
872 rqstp->rq_deferred = NULL;
874 int skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
875 /* FIXME maybe discard if size too large */
876 dr = kmalloc(size, GFP_KERNEL);
880 dr->handle.owner = rqstp->rq_server;
881 dr->prot = rqstp->rq_prot;
882 memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen);
883 dr->addrlen = rqstp->rq_addrlen;
884 dr->daddr = rqstp->rq_daddr;
885 dr->argslen = rqstp->rq_arg.len >> 2;
886 memcpy(dr->args, rqstp->rq_arg.head[0].iov_base-skip,
889 svc_xprt_get(rqstp->rq_xprt);
890 dr->xprt = rqstp->rq_xprt;
892 dr->handle.revisit = svc_revisit;
897 * recv data from a deferred request into an active one
899 static int svc_deferred_recv(struct svc_rqst *rqstp)
901 struct svc_deferred_req *dr = rqstp->rq_deferred;
903 rqstp->rq_arg.head[0].iov_base = dr->args;
904 rqstp->rq_arg.head[0].iov_len = dr->argslen<<2;
905 rqstp->rq_arg.page_len = 0;
906 rqstp->rq_arg.len = dr->argslen<<2;
907 rqstp->rq_prot = dr->prot;
908 memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen);
909 rqstp->rq_addrlen = dr->addrlen;
910 rqstp->rq_daddr = dr->daddr;
911 rqstp->rq_respages = rqstp->rq_pages;
912 return dr->argslen<<2;
916 static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt)
918 struct svc_deferred_req *dr = NULL;
920 if (!test_bit(XPT_DEFERRED, &xprt->xpt_flags))
922 spin_lock(&xprt->xpt_lock);
923 clear_bit(XPT_DEFERRED, &xprt->xpt_flags);
924 if (!list_empty(&xprt->xpt_deferred)) {
925 dr = list_entry(xprt->xpt_deferred.next,
926 struct svc_deferred_req,
928 list_del_init(&dr->handle.recent);
929 set_bit(XPT_DEFERRED, &xprt->xpt_flags);
931 spin_unlock(&xprt->xpt_lock);