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/stats.h>
33 #include <linux/sunrpc/svc_xprt.h>
35 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
37 static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt);
38 static int svc_deferred_recv(struct svc_rqst *rqstp);
39 static struct cache_deferred_req *svc_defer(struct cache_req *req);
40 static void svc_age_temp_xprts(unsigned long closure);
42 /* apparently the "standard" is that clients close
43 * idle connections after 5 minutes, servers after
45 * http://www.connectathon.org/talks96/nfstcp.pdf
47 static int svc_conn_age_period = 6*60;
49 /* List of registered transport classes */
50 static DEFINE_SPINLOCK(svc_xprt_class_lock);
51 static LIST_HEAD(svc_xprt_class_list);
53 /* SMP locking strategy:
55 * svc_pool->sp_lock protects most of the fields of that pool.
56 * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt.
57 * when both need to be taken (rare), svc_serv->sv_lock is first.
58 * BKL protects svc_serv->sv_nrthread.
59 * svc_sock->sk_lock protects the svc_sock->sk_deferred list
60 * and the ->sk_info_authunix cache.
62 * The XPT_BUSY bit in xprt->xpt_flags prevents a transport being
63 * enqueued multiply. During normal transport processing this bit
64 * is set by svc_xprt_enqueue and cleared by svc_xprt_received.
65 * Providers should not manipulate this bit directly.
67 * Some flags can be set to certain values at any time
68 * providing that certain rules are followed:
71 * - Can be set or cleared at any time.
72 * - After a set, svc_xprt_enqueue must be called to enqueue
73 * the transport for processing.
74 * - After a clear, the transport must be read/accepted.
75 * If this succeeds, it must be set again.
77 * - Can set at any time. It is never cleared.
79 * - Can only be set while XPT_BUSY is held which ensures
80 * that no other thread will be using the transport or will
81 * try to set XPT_DEAD.
84 int svc_reg_xprt_class(struct svc_xprt_class *xcl)
86 struct svc_xprt_class *cl;
89 dprintk("svc: Adding svc transport class '%s'\n", xcl->xcl_name);
91 INIT_LIST_HEAD(&xcl->xcl_list);
92 spin_lock(&svc_xprt_class_lock);
93 /* Make sure there isn't already a class with the same name */
94 list_for_each_entry(cl, &svc_xprt_class_list, xcl_list) {
95 if (strcmp(xcl->xcl_name, cl->xcl_name) == 0)
98 list_add_tail(&xcl->xcl_list, &svc_xprt_class_list);
101 spin_unlock(&svc_xprt_class_lock);
104 EXPORT_SYMBOL_GPL(svc_reg_xprt_class);
106 void svc_unreg_xprt_class(struct svc_xprt_class *xcl)
108 dprintk("svc: Removing svc transport class '%s'\n", xcl->xcl_name);
109 spin_lock(&svc_xprt_class_lock);
110 list_del_init(&xcl->xcl_list);
111 spin_unlock(&svc_xprt_class_lock);
113 EXPORT_SYMBOL_GPL(svc_unreg_xprt_class);
116 * Format the transport list for printing
118 int svc_print_xprts(char *buf, int maxlen)
120 struct list_head *le;
125 spin_lock(&svc_xprt_class_lock);
126 list_for_each(le, &svc_xprt_class_list) {
128 struct svc_xprt_class *xcl =
129 list_entry(le, struct svc_xprt_class, xcl_list);
131 sprintf(tmpstr, "%s %d\n", xcl->xcl_name, xcl->xcl_max_payload);
132 slen = strlen(tmpstr);
133 if (len + slen > maxlen)
138 spin_unlock(&svc_xprt_class_lock);
143 static void svc_xprt_free(struct kref *kref)
145 struct svc_xprt *xprt =
146 container_of(kref, struct svc_xprt, xpt_ref);
147 struct module *owner = xprt->xpt_class->xcl_owner;
148 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)
149 && xprt->xpt_auth_cache != NULL)
150 svcauth_unix_info_release(xprt->xpt_auth_cache);
151 xprt->xpt_ops->xpo_free(xprt);
155 void svc_xprt_put(struct svc_xprt *xprt)
157 kref_put(&xprt->xpt_ref, svc_xprt_free);
159 EXPORT_SYMBOL_GPL(svc_xprt_put);
162 * Called by transport drivers to initialize the transport independent
163 * portion of the transport instance.
165 void svc_xprt_init(struct svc_xprt_class *xcl, struct svc_xprt *xprt,
166 struct svc_serv *serv)
168 memset(xprt, 0, sizeof(*xprt));
169 xprt->xpt_class = xcl;
170 xprt->xpt_ops = xcl->xcl_ops;
171 kref_init(&xprt->xpt_ref);
172 xprt->xpt_server = serv;
173 INIT_LIST_HEAD(&xprt->xpt_list);
174 INIT_LIST_HEAD(&xprt->xpt_ready);
175 INIT_LIST_HEAD(&xprt->xpt_deferred);
176 mutex_init(&xprt->xpt_mutex);
177 spin_lock_init(&xprt->xpt_lock);
178 set_bit(XPT_BUSY, &xprt->xpt_flags);
180 EXPORT_SYMBOL_GPL(svc_xprt_init);
182 int svc_create_xprt(struct svc_serv *serv, char *xprt_name, unsigned short port,
185 struct svc_xprt_class *xcl;
186 struct sockaddr_in sin = {
187 .sin_family = AF_INET,
188 .sin_addr.s_addr = htonl(INADDR_ANY),
189 .sin_port = htons(port),
191 dprintk("svc: creating transport %s[%d]\n", xprt_name, port);
192 spin_lock(&svc_xprt_class_lock);
193 list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) {
194 struct svc_xprt *newxprt;
196 if (strcmp(xprt_name, xcl->xcl_name))
199 if (!try_module_get(xcl->xcl_owner))
202 spin_unlock(&svc_xprt_class_lock);
203 newxprt = xcl->xcl_ops->
204 xpo_create(serv, (struct sockaddr *)&sin, sizeof(sin),
206 if (IS_ERR(newxprt)) {
207 module_put(xcl->xcl_owner);
208 return PTR_ERR(newxprt);
211 clear_bit(XPT_TEMP, &newxprt->xpt_flags);
212 spin_lock_bh(&serv->sv_lock);
213 list_add(&newxprt->xpt_list, &serv->sv_permsocks);
214 spin_unlock_bh(&serv->sv_lock);
215 clear_bit(XPT_BUSY, &newxprt->xpt_flags);
216 return svc_xprt_local_port(newxprt);
219 spin_unlock(&svc_xprt_class_lock);
220 dprintk("svc: transport %s not found\n", xprt_name);
223 EXPORT_SYMBOL_GPL(svc_create_xprt);
226 * Copy the local and remote xprt addresses to the rqstp structure
228 void svc_xprt_copy_addrs(struct svc_rqst *rqstp, struct svc_xprt *xprt)
230 struct sockaddr *sin;
232 memcpy(&rqstp->rq_addr, &xprt->xpt_remote, xprt->xpt_remotelen);
233 rqstp->rq_addrlen = xprt->xpt_remotelen;
236 * Destination address in request is needed for binding the
237 * source address in RPC replies/callbacks later.
239 sin = (struct sockaddr *)&xprt->xpt_local;
240 switch (sin->sa_family) {
242 rqstp->rq_daddr.addr = ((struct sockaddr_in *)sin)->sin_addr;
245 rqstp->rq_daddr.addr6 = ((struct sockaddr_in6 *)sin)->sin6_addr;
249 EXPORT_SYMBOL_GPL(svc_xprt_copy_addrs);
252 * svc_print_addr - Format rq_addr field for printing
253 * @rqstp: svc_rqst struct containing address to print
254 * @buf: target buffer for formatted address
255 * @len: length of target buffer
258 char *svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len)
260 return __svc_print_addr(svc_addr(rqstp), buf, len);
262 EXPORT_SYMBOL_GPL(svc_print_addr);
265 * Queue up an idle server thread. Must have pool->sp_lock held.
266 * Note: this is really a stack rather than a queue, so that we only
267 * use as many different threads as we need, and the rest don't pollute
270 static void svc_thread_enqueue(struct svc_pool *pool, struct svc_rqst *rqstp)
272 list_add(&rqstp->rq_list, &pool->sp_threads);
276 * Dequeue an nfsd thread. Must have pool->sp_lock held.
278 static void svc_thread_dequeue(struct svc_pool *pool, struct svc_rqst *rqstp)
280 list_del(&rqstp->rq_list);
284 * Queue up a transport with data pending. If there are idle nfsd
285 * processes, wake 'em up.
288 void svc_xprt_enqueue(struct svc_xprt *xprt)
290 struct svc_serv *serv = xprt->xpt_server;
291 struct svc_pool *pool;
292 struct svc_rqst *rqstp;
295 if (!(xprt->xpt_flags &
296 ((1<<XPT_CONN)|(1<<XPT_DATA)|(1<<XPT_CLOSE)|(1<<XPT_DEFERRED))))
298 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
302 pool = svc_pool_for_cpu(xprt->xpt_server, cpu);
305 spin_lock_bh(&pool->sp_lock);
307 if (!list_empty(&pool->sp_threads) &&
308 !list_empty(&pool->sp_sockets))
311 "threads and transports both waiting??\n");
313 if (test_bit(XPT_DEAD, &xprt->xpt_flags)) {
314 /* Don't enqueue dead transports */
315 dprintk("svc: transport %p is dead, not enqueued\n", xprt);
319 /* Mark transport as busy. It will remain in this state until
320 * the provider calls svc_xprt_received. We update XPT_BUSY
321 * atomically because it also guards against trying to enqueue
322 * the transport twice.
324 if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags)) {
325 /* Don't enqueue transport while already enqueued */
326 dprintk("svc: transport %p busy, not enqueued\n", xprt);
329 BUG_ON(xprt->xpt_pool != NULL);
330 xprt->xpt_pool = pool;
332 /* Handle pending connection */
333 if (test_bit(XPT_CONN, &xprt->xpt_flags))
336 /* Handle close in-progress */
337 if (test_bit(XPT_CLOSE, &xprt->xpt_flags))
340 /* Check if we have space to reply to a request */
341 if (!xprt->xpt_ops->xpo_has_wspace(xprt)) {
342 /* Don't enqueue while not enough space for reply */
343 dprintk("svc: no write space, transport %p not enqueued\n",
345 xprt->xpt_pool = NULL;
346 clear_bit(XPT_BUSY, &xprt->xpt_flags);
351 if (!list_empty(&pool->sp_threads)) {
352 rqstp = list_entry(pool->sp_threads.next,
355 dprintk("svc: transport %p served by daemon %p\n",
357 svc_thread_dequeue(pool, rqstp);
360 "svc_xprt_enqueue: server %p, rq_xprt=%p!\n",
361 rqstp, rqstp->rq_xprt);
362 rqstp->rq_xprt = xprt;
364 rqstp->rq_reserved = serv->sv_max_mesg;
365 atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved);
366 BUG_ON(xprt->xpt_pool != pool);
367 wake_up(&rqstp->rq_wait);
369 dprintk("svc: transport %p put into queue\n", xprt);
370 list_add_tail(&xprt->xpt_ready, &pool->sp_sockets);
371 BUG_ON(xprt->xpt_pool != pool);
375 spin_unlock_bh(&pool->sp_lock);
377 EXPORT_SYMBOL_GPL(svc_xprt_enqueue);
380 * Dequeue the first transport. Must be called with the pool->sp_lock held.
382 static struct svc_xprt *svc_xprt_dequeue(struct svc_pool *pool)
384 struct svc_xprt *xprt;
386 if (list_empty(&pool->sp_sockets))
389 xprt = list_entry(pool->sp_sockets.next,
390 struct svc_xprt, xpt_ready);
391 list_del_init(&xprt->xpt_ready);
393 dprintk("svc: transport %p dequeued, inuse=%d\n",
394 xprt, atomic_read(&xprt->xpt_ref.refcount));
400 * svc_xprt_received conditionally queues the transport for processing
401 * by another thread. The caller must hold the XPT_BUSY bit and must
402 * not thereafter touch transport data.
404 * Note: XPT_DATA only gets cleared when a read-attempt finds no (or
405 * insufficient) data.
407 void svc_xprt_received(struct svc_xprt *xprt)
409 BUG_ON(!test_bit(XPT_BUSY, &xprt->xpt_flags));
410 xprt->xpt_pool = NULL;
411 clear_bit(XPT_BUSY, &xprt->xpt_flags);
412 svc_xprt_enqueue(xprt);
414 EXPORT_SYMBOL_GPL(svc_xprt_received);
417 * svc_reserve - change the space reserved for the reply to a request.
418 * @rqstp: The request in question
419 * @space: new max space to reserve
421 * Each request reserves some space on the output queue of the transport
422 * to make sure the reply fits. This function reduces that reserved
423 * space to be the amount of space used already, plus @space.
426 void svc_reserve(struct svc_rqst *rqstp, int space)
428 space += rqstp->rq_res.head[0].iov_len;
430 if (space < rqstp->rq_reserved) {
431 struct svc_xprt *xprt = rqstp->rq_xprt;
432 atomic_sub((rqstp->rq_reserved - space), &xprt->xpt_reserved);
433 rqstp->rq_reserved = space;
435 svc_xprt_enqueue(xprt);
438 EXPORT_SYMBOL(svc_reserve);
440 static void svc_xprt_release(struct svc_rqst *rqstp)
442 struct svc_xprt *xprt = rqstp->rq_xprt;
444 rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
446 svc_free_res_pages(rqstp);
447 rqstp->rq_res.page_len = 0;
448 rqstp->rq_res.page_base = 0;
450 /* Reset response buffer and release
452 * But first, check that enough space was reserved
453 * for the reply, otherwise we have a bug!
455 if ((rqstp->rq_res.len) > rqstp->rq_reserved)
456 printk(KERN_ERR "RPC request reserved %d but used %d\n",
460 rqstp->rq_res.head[0].iov_len = 0;
461 svc_reserve(rqstp, 0);
462 rqstp->rq_xprt = NULL;
468 * External function to wake up a server waiting for data
469 * This really only makes sense for services like lockd
470 * which have exactly one thread anyway.
472 void svc_wake_up(struct svc_serv *serv)
474 struct svc_rqst *rqstp;
476 struct svc_pool *pool;
478 for (i = 0; i < serv->sv_nrpools; i++) {
479 pool = &serv->sv_pools[i];
481 spin_lock_bh(&pool->sp_lock);
482 if (!list_empty(&pool->sp_threads)) {
483 rqstp = list_entry(pool->sp_threads.next,
486 dprintk("svc: daemon %p woken up.\n", rqstp);
488 svc_thread_dequeue(pool, rqstp);
489 rqstp->rq_xprt = NULL;
491 wake_up(&rqstp->rq_wait);
493 spin_unlock_bh(&pool->sp_lock);
496 EXPORT_SYMBOL(svc_wake_up);
498 int svc_port_is_privileged(struct sockaddr *sin)
500 switch (sin->sa_family) {
502 return ntohs(((struct sockaddr_in *)sin)->sin_port)
505 return ntohs(((struct sockaddr_in6 *)sin)->sin6_port)
513 * Make sure that we don't have too many active connections. If we
514 * have, something must be dropped.
516 * There's no point in trying to do random drop here for DoS
517 * prevention. The NFS clients does 1 reconnect in 15 seconds. An
518 * attacker can easily beat that.
520 * The only somewhat efficient mechanism would be if drop old
521 * connections from the same IP first. But right now we don't even
522 * record the client IP in svc_sock.
524 static void svc_check_conn_limits(struct svc_serv *serv)
526 if (serv->sv_tmpcnt > (serv->sv_nrthreads+3)*20) {
527 struct svc_xprt *xprt = NULL;
528 spin_lock_bh(&serv->sv_lock);
529 if (!list_empty(&serv->sv_tempsocks)) {
530 if (net_ratelimit()) {
531 /* Try to help the admin */
532 printk(KERN_NOTICE "%s: too many open "
533 "connections, consider increasing the "
534 "number of nfsd threads\n",
538 * Always select the oldest connection. It's not fair,
541 xprt = list_entry(serv->sv_tempsocks.prev,
544 set_bit(XPT_CLOSE, &xprt->xpt_flags);
547 spin_unlock_bh(&serv->sv_lock);
550 svc_xprt_enqueue(xprt);
557 * Receive the next request on any transport. This code is carefully
558 * organised not to touch any cachelines in the shared svc_serv
559 * structure, only cachelines in the local svc_pool.
561 int svc_recv(struct svc_rqst *rqstp, long timeout)
563 struct svc_xprt *xprt = NULL;
564 struct svc_serv *serv = rqstp->rq_server;
565 struct svc_pool *pool = rqstp->rq_pool;
569 DECLARE_WAITQUEUE(wait, current);
571 dprintk("svc: server %p waiting for data (to = %ld)\n",
576 "svc_recv: service %p, transport not NULL!\n",
578 if (waitqueue_active(&rqstp->rq_wait))
580 "svc_recv: service %p, wait queue active!\n",
583 /* now allocate needed pages. If we get a failure, sleep briefly */
584 pages = (serv->sv_max_mesg + PAGE_SIZE) / PAGE_SIZE;
585 for (i = 0; i < pages ; i++)
586 while (rqstp->rq_pages[i] == NULL) {
587 struct page *p = alloc_page(GFP_KERNEL);
589 int j = msecs_to_jiffies(500);
590 schedule_timeout_uninterruptible(j);
592 rqstp->rq_pages[i] = p;
594 rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */
595 BUG_ON(pages >= RPCSVC_MAXPAGES);
597 /* Make arg->head point to first page and arg->pages point to rest */
598 arg = &rqstp->rq_arg;
599 arg->head[0].iov_base = page_address(rqstp->rq_pages[0]);
600 arg->head[0].iov_len = PAGE_SIZE;
601 arg->pages = rqstp->rq_pages + 1;
603 /* save at least one page for response */
604 arg->page_len = (pages-2)*PAGE_SIZE;
605 arg->len = (pages-1)*PAGE_SIZE;
606 arg->tail[0].iov_len = 0;
613 spin_lock_bh(&pool->sp_lock);
614 xprt = svc_xprt_dequeue(pool);
616 rqstp->rq_xprt = xprt;
618 rqstp->rq_reserved = serv->sv_max_mesg;
619 atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved);
621 /* No data pending. Go to sleep */
622 svc_thread_enqueue(pool, rqstp);
625 * We have to be able to interrupt this wait
626 * to bring down the daemons ...
628 set_current_state(TASK_INTERRUPTIBLE);
629 add_wait_queue(&rqstp->rq_wait, &wait);
630 spin_unlock_bh(&pool->sp_lock);
632 schedule_timeout(timeout);
636 spin_lock_bh(&pool->sp_lock);
637 remove_wait_queue(&rqstp->rq_wait, &wait);
639 xprt = rqstp->rq_xprt;
641 svc_thread_dequeue(pool, rqstp);
642 spin_unlock_bh(&pool->sp_lock);
643 dprintk("svc: server %p, no data yet\n", rqstp);
644 return signalled()? -EINTR : -EAGAIN;
647 spin_unlock_bh(&pool->sp_lock);
650 if (test_bit(XPT_CLOSE, &xprt->xpt_flags)) {
651 dprintk("svc_recv: found XPT_CLOSE\n");
652 svc_delete_xprt(xprt);
653 } else if (test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
654 struct svc_xprt *newxpt;
655 newxpt = xprt->xpt_ops->xpo_accept(xprt);
658 * We know this module_get will succeed because the
659 * listener holds a reference too
661 __module_get(newxpt->xpt_class->xcl_owner);
662 svc_check_conn_limits(xprt->xpt_server);
663 spin_lock_bh(&serv->sv_lock);
664 set_bit(XPT_TEMP, &newxpt->xpt_flags);
665 list_add(&newxpt->xpt_list, &serv->sv_tempsocks);
667 if (serv->sv_temptimer.function == NULL) {
668 /* setup timer to age temp transports */
669 setup_timer(&serv->sv_temptimer,
671 (unsigned long)serv);
672 mod_timer(&serv->sv_temptimer,
673 jiffies + svc_conn_age_period * HZ);
675 spin_unlock_bh(&serv->sv_lock);
676 svc_xprt_received(newxpt);
678 svc_xprt_received(xprt);
680 dprintk("svc: server %p, pool %u, transport %p, inuse=%d\n",
681 rqstp, pool->sp_id, xprt,
682 atomic_read(&xprt->xpt_ref.refcount));
683 rqstp->rq_deferred = svc_deferred_dequeue(xprt);
684 if (rqstp->rq_deferred) {
685 svc_xprt_received(xprt);
686 len = svc_deferred_recv(rqstp);
688 len = xprt->xpt_ops->xpo_recvfrom(rqstp);
689 dprintk("svc: got len=%d\n", len);
692 /* No data, incomplete (TCP) read, or accept() */
693 if (len == 0 || len == -EAGAIN) {
694 rqstp->rq_res.len = 0;
695 svc_xprt_release(rqstp);
698 clear_bit(XPT_OLD, &xprt->xpt_flags);
700 rqstp->rq_secure = svc_port_is_privileged(svc_addr(rqstp));
701 rqstp->rq_chandle.defer = svc_defer;
704 serv->sv_stats->netcnt++;
707 EXPORT_SYMBOL(svc_recv);
712 void svc_drop(struct svc_rqst *rqstp)
714 dprintk("svc: xprt %p dropped request\n", rqstp->rq_xprt);
715 svc_xprt_release(rqstp);
717 EXPORT_SYMBOL(svc_drop);
720 * Return reply to client.
722 int svc_send(struct svc_rqst *rqstp)
724 struct svc_xprt *xprt;
728 xprt = rqstp->rq_xprt;
732 /* release the receive skb before sending the reply */
733 rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
735 /* calculate over-all length */
737 xb->len = xb->head[0].iov_len +
741 /* Grab mutex to serialize outgoing data. */
742 mutex_lock(&xprt->xpt_mutex);
743 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
746 len = xprt->xpt_ops->xpo_sendto(rqstp);
747 mutex_unlock(&xprt->xpt_mutex);
748 svc_xprt_release(rqstp);
750 if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
756 * Timer function to close old temporary transports, using
757 * a mark-and-sweep algorithm.
759 static void svc_age_temp_xprts(unsigned long closure)
761 struct svc_serv *serv = (struct svc_serv *)closure;
762 struct svc_xprt *xprt;
763 struct list_head *le, *next;
764 LIST_HEAD(to_be_aged);
766 dprintk("svc_age_temp_xprts\n");
768 if (!spin_trylock_bh(&serv->sv_lock)) {
769 /* busy, try again 1 sec later */
770 dprintk("svc_age_temp_xprts: busy\n");
771 mod_timer(&serv->sv_temptimer, jiffies + HZ);
775 list_for_each_safe(le, next, &serv->sv_tempsocks) {
776 xprt = list_entry(le, struct svc_xprt, xpt_list);
778 /* First time through, just mark it OLD. Second time
779 * through, close it. */
780 if (!test_and_set_bit(XPT_OLD, &xprt->xpt_flags))
782 if (atomic_read(&xprt->xpt_ref.refcount) > 1
783 || test_bit(XPT_BUSY, &xprt->xpt_flags))
786 list_move(le, &to_be_aged);
787 set_bit(XPT_CLOSE, &xprt->xpt_flags);
788 set_bit(XPT_DETACHED, &xprt->xpt_flags);
790 spin_unlock_bh(&serv->sv_lock);
792 while (!list_empty(&to_be_aged)) {
793 le = to_be_aged.next;
794 /* fiddling the xpt_list node is safe 'cos we're XPT_DETACHED */
796 xprt = list_entry(le, struct svc_xprt, xpt_list);
798 dprintk("queuing xprt %p for closing\n", xprt);
800 /* a thread will dequeue and close it soon */
801 svc_xprt_enqueue(xprt);
805 mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
809 * Remove a dead transport
811 void svc_delete_xprt(struct svc_xprt *xprt)
813 struct svc_serv *serv = xprt->xpt_server;
815 dprintk("svc: svc_delete_xprt(%p)\n", xprt);
816 xprt->xpt_ops->xpo_detach(xprt);
818 spin_lock_bh(&serv->sv_lock);
819 if (!test_and_set_bit(XPT_DETACHED, &xprt->xpt_flags))
820 list_del_init(&xprt->xpt_list);
822 * We used to delete the transport from whichever list
823 * it's sk_xprt.xpt_ready node was on, but we don't actually
824 * need to. This is because the only time we're called
825 * while still attached to a queue, the queue itself
826 * is about to be destroyed (in svc_destroy).
828 if (!test_and_set_bit(XPT_DEAD, &xprt->xpt_flags)) {
829 BUG_ON(atomic_read(&xprt->xpt_ref.refcount) < 2);
830 if (test_bit(XPT_TEMP, &xprt->xpt_flags))
834 spin_unlock_bh(&serv->sv_lock);
837 void svc_close_xprt(struct svc_xprt *xprt)
839 set_bit(XPT_CLOSE, &xprt->xpt_flags);
840 if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags))
841 /* someone else will have to effect the close */
845 svc_delete_xprt(xprt);
846 clear_bit(XPT_BUSY, &xprt->xpt_flags);
849 EXPORT_SYMBOL_GPL(svc_close_xprt);
851 void svc_close_all(struct list_head *xprt_list)
853 struct svc_xprt *xprt;
854 struct svc_xprt *tmp;
856 list_for_each_entry_safe(xprt, tmp, xprt_list, xpt_list) {
857 set_bit(XPT_CLOSE, &xprt->xpt_flags);
858 if (test_bit(XPT_BUSY, &xprt->xpt_flags)) {
859 /* Waiting to be processed, but no threads left,
860 * So just remove it from the waiting list
862 list_del_init(&xprt->xpt_ready);
863 clear_bit(XPT_BUSY, &xprt->xpt_flags);
865 svc_close_xprt(xprt);
870 * Handle defer and revisit of requests
873 static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
875 struct svc_deferred_req *dr =
876 container_of(dreq, struct svc_deferred_req, handle);
877 struct svc_xprt *xprt = dr->xprt;
884 dprintk("revisit queued\n");
886 spin_lock(&xprt->xpt_lock);
887 list_add(&dr->handle.recent, &xprt->xpt_deferred);
888 spin_unlock(&xprt->xpt_lock);
889 set_bit(XPT_DEFERRED, &xprt->xpt_flags);
890 svc_xprt_enqueue(xprt);
895 * Save the request off for later processing. The request buffer looks
898 * <xprt-header><rpc-header><rpc-pagelist><rpc-tail>
900 * This code can only handle requests that consist of an xprt-header
903 static struct cache_deferred_req *svc_defer(struct cache_req *req)
905 struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
906 struct svc_deferred_req *dr;
908 if (rqstp->rq_arg.page_len)
909 return NULL; /* if more than a page, give up FIXME */
910 if (rqstp->rq_deferred) {
911 dr = rqstp->rq_deferred;
912 rqstp->rq_deferred = NULL;
916 /* FIXME maybe discard if size too large */
917 size = sizeof(struct svc_deferred_req) + rqstp->rq_arg.len;
918 dr = kmalloc(size, GFP_KERNEL);
922 dr->handle.owner = rqstp->rq_server;
923 dr->prot = rqstp->rq_prot;
924 memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen);
925 dr->addrlen = rqstp->rq_addrlen;
926 dr->daddr = rqstp->rq_daddr;
927 dr->argslen = rqstp->rq_arg.len >> 2;
928 dr->xprt_hlen = rqstp->rq_xprt_hlen;
930 /* back up head to the start of the buffer and copy */
931 skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
932 memcpy(dr->args, rqstp->rq_arg.head[0].iov_base - skip,
935 svc_xprt_get(rqstp->rq_xprt);
936 dr->xprt = rqstp->rq_xprt;
938 dr->handle.revisit = svc_revisit;
943 * recv data from a deferred request into an active one
945 static int svc_deferred_recv(struct svc_rqst *rqstp)
947 struct svc_deferred_req *dr = rqstp->rq_deferred;
949 /* setup iov_base past transport header */
950 rqstp->rq_arg.head[0].iov_base = dr->args + (dr->xprt_hlen>>2);
951 /* The iov_len does not include the transport header bytes */
952 rqstp->rq_arg.head[0].iov_len = (dr->argslen<<2) - dr->xprt_hlen;
953 rqstp->rq_arg.page_len = 0;
954 /* The rq_arg.len includes the transport header bytes */
955 rqstp->rq_arg.len = dr->argslen<<2;
956 rqstp->rq_prot = dr->prot;
957 memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen);
958 rqstp->rq_addrlen = dr->addrlen;
959 /* Save off transport header len in case we get deferred again */
960 rqstp->rq_xprt_hlen = dr->xprt_hlen;
961 rqstp->rq_daddr = dr->daddr;
962 rqstp->rq_respages = rqstp->rq_pages;
963 return (dr->argslen<<2) - dr->xprt_hlen;
967 static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt)
969 struct svc_deferred_req *dr = NULL;
971 if (!test_bit(XPT_DEFERRED, &xprt->xpt_flags))
973 spin_lock(&xprt->xpt_lock);
974 clear_bit(XPT_DEFERRED, &xprt->xpt_flags);
975 if (!list_empty(&xprt->xpt_deferred)) {
976 dr = list_entry(xprt->xpt_deferred.next,
977 struct svc_deferred_req,
979 list_del_init(&dr->handle.recent);
980 set_bit(XPT_DEFERRED, &xprt->xpt_flags);
982 spin_unlock(&xprt->xpt_lock);
987 * Return the transport instance pointer for the endpoint accepting
988 * connections/peer traffic from the specified transport class,
989 * address family and port.
991 * Specifying 0 for the address family or port is effectively a
992 * wild-card, and will result in matching the first transport in the
993 * service's list that has a matching class name.
995 struct svc_xprt *svc_find_xprt(struct svc_serv *serv, char *xcl_name,
998 struct svc_xprt *xprt;
999 struct svc_xprt *found = NULL;
1001 /* Sanity check the args */
1002 if (!serv || !xcl_name)
1005 spin_lock_bh(&serv->sv_lock);
1006 list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
1007 if (strcmp(xprt->xpt_class->xcl_name, xcl_name))
1009 if (af != AF_UNSPEC && af != xprt->xpt_local.ss_family)
1011 if (port && port != svc_xprt_local_port(xprt))
1017 spin_unlock_bh(&serv->sv_lock);
1020 EXPORT_SYMBOL_GPL(svc_find_xprt);
1023 * Format a buffer with a list of the active transports. A zero for
1024 * the buflen parameter disables target buffer overflow checking.
1026 int svc_xprt_names(struct svc_serv *serv, char *buf, int buflen)
1028 struct svc_xprt *xprt;
1033 /* Sanity check args */
1037 spin_lock_bh(&serv->sv_lock);
1038 list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
1039 len = snprintf(xprt_str, sizeof(xprt_str),
1040 "%s %d\n", xprt->xpt_class->xcl_name,
1041 svc_xprt_local_port(xprt));
1042 /* If the string was truncated, replace with error string */
1043 if (len >= sizeof(xprt_str))
1044 strcpy(xprt_str, "name-too-long\n");
1045 /* Don't overflow buffer */
1046 len = strlen(xprt_str);
1047 if (buflen && (len + totlen >= buflen))
1049 strcpy(buf+totlen, xprt_str);
1052 spin_unlock_bh(&serv->sv_lock);
1055 EXPORT_SYMBOL_GPL(svc_xprt_names);