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/freezer.h>
10 #include <linux/kthread.h>
12 #include <linux/sunrpc/stats.h>
13 #include <linux/sunrpc/svc_xprt.h>
15 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
17 static struct svc_deferred_req
*svc_deferred_dequeue(struct svc_xprt
*xprt
);
18 static int svc_deferred_recv(struct svc_rqst
*rqstp
);
19 static struct cache_deferred_req
*svc_defer(struct cache_req
*req
);
20 static void svc_age_temp_xprts(unsigned long closure
);
22 /* apparently the "standard" is that clients close
23 * idle connections after 5 minutes, servers after
25 * http://www.connectathon.org/talks96/nfstcp.pdf
27 static int svc_conn_age_period
= 6*60;
29 /* List of registered transport classes */
30 static DEFINE_SPINLOCK(svc_xprt_class_lock
);
31 static LIST_HEAD(svc_xprt_class_list
);
33 /* SMP locking strategy:
35 * svc_pool->sp_lock protects most of the fields of that pool.
36 * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt.
37 * when both need to be taken (rare), svc_serv->sv_lock is first.
38 * BKL protects svc_serv->sv_nrthread.
39 * svc_sock->sk_lock protects the svc_sock->sk_deferred list
40 * and the ->sk_info_authunix cache.
42 * The XPT_BUSY bit in xprt->xpt_flags prevents a transport being
43 * enqueued multiply. During normal transport processing this bit
44 * is set by svc_xprt_enqueue and cleared by svc_xprt_received.
45 * Providers should not manipulate this bit directly.
47 * Some flags can be set to certain values at any time
48 * providing that certain rules are followed:
51 * - Can be set or cleared at any time.
52 * - After a set, svc_xprt_enqueue must be called to enqueue
53 * the transport for processing.
54 * - After a clear, the transport must be read/accepted.
55 * If this succeeds, it must be set again.
57 * - Can set at any time. It is never cleared.
59 * - Can only be set while XPT_BUSY is held which ensures
60 * that no other thread will be using the transport or will
61 * try to set XPT_DEAD.
64 int svc_reg_xprt_class(struct svc_xprt_class
*xcl
)
66 struct svc_xprt_class
*cl
;
69 dprintk("svc: Adding svc transport class '%s'\n", xcl
->xcl_name
);
71 INIT_LIST_HEAD(&xcl
->xcl_list
);
72 spin_lock(&svc_xprt_class_lock
);
73 /* Make sure there isn't already a class with the same name */
74 list_for_each_entry(cl
, &svc_xprt_class_list
, xcl_list
) {
75 if (strcmp(xcl
->xcl_name
, cl
->xcl_name
) == 0)
78 list_add_tail(&xcl
->xcl_list
, &svc_xprt_class_list
);
81 spin_unlock(&svc_xprt_class_lock
);
84 EXPORT_SYMBOL_GPL(svc_reg_xprt_class
);
86 void svc_unreg_xprt_class(struct svc_xprt_class
*xcl
)
88 dprintk("svc: Removing svc transport class '%s'\n", xcl
->xcl_name
);
89 spin_lock(&svc_xprt_class_lock
);
90 list_del_init(&xcl
->xcl_list
);
91 spin_unlock(&svc_xprt_class_lock
);
93 EXPORT_SYMBOL_GPL(svc_unreg_xprt_class
);
96 * Format the transport list for printing
98 int svc_print_xprts(char *buf
, int maxlen
)
100 struct list_head
*le
;
105 spin_lock(&svc_xprt_class_lock
);
106 list_for_each(le
, &svc_xprt_class_list
) {
108 struct svc_xprt_class
*xcl
=
109 list_entry(le
, struct svc_xprt_class
, xcl_list
);
111 sprintf(tmpstr
, "%s %d\n", xcl
->xcl_name
, xcl
->xcl_max_payload
);
112 slen
= strlen(tmpstr
);
113 if (len
+ slen
> maxlen
)
118 spin_unlock(&svc_xprt_class_lock
);
123 static void svc_xprt_free(struct kref
*kref
)
125 struct svc_xprt
*xprt
=
126 container_of(kref
, struct svc_xprt
, xpt_ref
);
127 struct module
*owner
= xprt
->xpt_class
->xcl_owner
;
128 if (test_bit(XPT_CACHE_AUTH
, &xprt
->xpt_flags
)
129 && xprt
->xpt_auth_cache
!= NULL
)
130 svcauth_unix_info_release(xprt
->xpt_auth_cache
);
131 xprt
->xpt_ops
->xpo_free(xprt
);
135 void svc_xprt_put(struct svc_xprt
*xprt
)
137 kref_put(&xprt
->xpt_ref
, svc_xprt_free
);
139 EXPORT_SYMBOL_GPL(svc_xprt_put
);
142 * Called by transport drivers to initialize the transport independent
143 * portion of the transport instance.
145 void svc_xprt_init(struct svc_xprt_class
*xcl
, struct svc_xprt
*xprt
,
146 struct svc_serv
*serv
)
148 memset(xprt
, 0, sizeof(*xprt
));
149 xprt
->xpt_class
= xcl
;
150 xprt
->xpt_ops
= xcl
->xcl_ops
;
151 kref_init(&xprt
->xpt_ref
);
152 xprt
->xpt_server
= serv
;
153 INIT_LIST_HEAD(&xprt
->xpt_list
);
154 INIT_LIST_HEAD(&xprt
->xpt_ready
);
155 INIT_LIST_HEAD(&xprt
->xpt_deferred
);
156 mutex_init(&xprt
->xpt_mutex
);
157 spin_lock_init(&xprt
->xpt_lock
);
158 set_bit(XPT_BUSY
, &xprt
->xpt_flags
);
160 EXPORT_SYMBOL_GPL(svc_xprt_init
);
162 static struct svc_xprt
*__svc_xpo_create(struct svc_xprt_class
*xcl
,
163 struct svc_serv
*serv
,
165 const unsigned short port
,
168 struct sockaddr_in sin
= {
169 .sin_family
= AF_INET
,
170 .sin_addr
.s_addr
= htonl(INADDR_ANY
),
171 .sin_port
= htons(port
),
173 struct sockaddr_in6 sin6
= {
174 .sin6_family
= AF_INET6
,
175 .sin6_addr
= IN6ADDR_ANY_INIT
,
176 .sin6_port
= htons(port
),
178 struct sockaddr
*sap
;
183 sap
= (struct sockaddr
*)&sin
;
187 sap
= (struct sockaddr
*)&sin6
;
191 return ERR_PTR(-EAFNOSUPPORT
);
194 return xcl
->xcl_ops
->xpo_create(serv
, sap
, len
, flags
);
197 int svc_create_xprt(struct svc_serv
*serv
, const char *xprt_name
,
198 const int family
, const unsigned short port
,
201 struct svc_xprt_class
*xcl
;
203 dprintk("svc: creating transport %s[%d]\n", xprt_name
, port
);
204 spin_lock(&svc_xprt_class_lock
);
205 list_for_each_entry(xcl
, &svc_xprt_class_list
, xcl_list
) {
206 struct svc_xprt
*newxprt
;
208 if (strcmp(xprt_name
, xcl
->xcl_name
))
211 if (!try_module_get(xcl
->xcl_owner
))
214 spin_unlock(&svc_xprt_class_lock
);
215 newxprt
= __svc_xpo_create(xcl
, serv
, family
, port
, flags
);
216 if (IS_ERR(newxprt
)) {
217 module_put(xcl
->xcl_owner
);
218 return PTR_ERR(newxprt
);
221 clear_bit(XPT_TEMP
, &newxprt
->xpt_flags
);
222 spin_lock_bh(&serv
->sv_lock
);
223 list_add(&newxprt
->xpt_list
, &serv
->sv_permsocks
);
224 spin_unlock_bh(&serv
->sv_lock
);
225 clear_bit(XPT_BUSY
, &newxprt
->xpt_flags
);
226 return svc_xprt_local_port(newxprt
);
229 spin_unlock(&svc_xprt_class_lock
);
230 dprintk("svc: transport %s not found\n", xprt_name
);
233 EXPORT_SYMBOL_GPL(svc_create_xprt
);
236 * Copy the local and remote xprt addresses to the rqstp structure
238 void svc_xprt_copy_addrs(struct svc_rqst
*rqstp
, struct svc_xprt
*xprt
)
240 struct sockaddr
*sin
;
242 memcpy(&rqstp
->rq_addr
, &xprt
->xpt_remote
, xprt
->xpt_remotelen
);
243 rqstp
->rq_addrlen
= xprt
->xpt_remotelen
;
246 * Destination address in request is needed for binding the
247 * source address in RPC replies/callbacks later.
249 sin
= (struct sockaddr
*)&xprt
->xpt_local
;
250 switch (sin
->sa_family
) {
252 rqstp
->rq_daddr
.addr
= ((struct sockaddr_in
*)sin
)->sin_addr
;
255 rqstp
->rq_daddr
.addr6
= ((struct sockaddr_in6
*)sin
)->sin6_addr
;
259 EXPORT_SYMBOL_GPL(svc_xprt_copy_addrs
);
262 * svc_print_addr - Format rq_addr field for printing
263 * @rqstp: svc_rqst struct containing address to print
264 * @buf: target buffer for formatted address
265 * @len: length of target buffer
268 char *svc_print_addr(struct svc_rqst
*rqstp
, char *buf
, size_t len
)
270 return __svc_print_addr(svc_addr(rqstp
), buf
, len
);
272 EXPORT_SYMBOL_GPL(svc_print_addr
);
275 * Queue up an idle server thread. Must have pool->sp_lock held.
276 * Note: this is really a stack rather than a queue, so that we only
277 * use as many different threads as we need, and the rest don't pollute
280 static void svc_thread_enqueue(struct svc_pool
*pool
, struct svc_rqst
*rqstp
)
282 list_add(&rqstp
->rq_list
, &pool
->sp_threads
);
286 * Dequeue an nfsd thread. Must have pool->sp_lock held.
288 static void svc_thread_dequeue(struct svc_pool
*pool
, struct svc_rqst
*rqstp
)
290 list_del(&rqstp
->rq_list
);
294 * Queue up a transport with data pending. If there are idle nfsd
295 * processes, wake 'em up.
298 void svc_xprt_enqueue(struct svc_xprt
*xprt
)
300 struct svc_serv
*serv
= xprt
->xpt_server
;
301 struct svc_pool
*pool
;
302 struct svc_rqst
*rqstp
;
305 if (!(xprt
->xpt_flags
&
306 ((1<<XPT_CONN
)|(1<<XPT_DATA
)|(1<<XPT_CLOSE
)|(1<<XPT_DEFERRED
))))
310 pool
= svc_pool_for_cpu(xprt
->xpt_server
, cpu
);
313 spin_lock_bh(&pool
->sp_lock
);
315 if (!list_empty(&pool
->sp_threads
) &&
316 !list_empty(&pool
->sp_sockets
))
319 "threads and transports both waiting??\n");
321 if (test_bit(XPT_DEAD
, &xprt
->xpt_flags
)) {
322 /* Don't enqueue dead transports */
323 dprintk("svc: transport %p is dead, not enqueued\n", xprt
);
327 /* Mark transport as busy. It will remain in this state until
328 * the provider calls svc_xprt_received. We update XPT_BUSY
329 * atomically because it also guards against trying to enqueue
330 * the transport twice.
332 if (test_and_set_bit(XPT_BUSY
, &xprt
->xpt_flags
)) {
333 /* Don't enqueue transport while already enqueued */
334 dprintk("svc: transport %p busy, not enqueued\n", xprt
);
337 BUG_ON(xprt
->xpt_pool
!= NULL
);
338 xprt
->xpt_pool
= pool
;
340 /* Handle pending connection */
341 if (test_bit(XPT_CONN
, &xprt
->xpt_flags
))
344 /* Handle close in-progress */
345 if (test_bit(XPT_CLOSE
, &xprt
->xpt_flags
))
348 /* Check if we have space to reply to a request */
349 if (!xprt
->xpt_ops
->xpo_has_wspace(xprt
)) {
350 /* Don't enqueue while not enough space for reply */
351 dprintk("svc: no write space, transport %p not enqueued\n",
353 xprt
->xpt_pool
= NULL
;
354 clear_bit(XPT_BUSY
, &xprt
->xpt_flags
);
359 if (!list_empty(&pool
->sp_threads
)) {
360 rqstp
= list_entry(pool
->sp_threads
.next
,
363 dprintk("svc: transport %p served by daemon %p\n",
365 svc_thread_dequeue(pool
, rqstp
);
368 "svc_xprt_enqueue: server %p, rq_xprt=%p!\n",
369 rqstp
, rqstp
->rq_xprt
);
370 rqstp
->rq_xprt
= xprt
;
372 rqstp
->rq_reserved
= serv
->sv_max_mesg
;
373 atomic_add(rqstp
->rq_reserved
, &xprt
->xpt_reserved
);
374 BUG_ON(xprt
->xpt_pool
!= pool
);
375 wake_up(&rqstp
->rq_wait
);
377 dprintk("svc: transport %p put into queue\n", xprt
);
378 list_add_tail(&xprt
->xpt_ready
, &pool
->sp_sockets
);
379 BUG_ON(xprt
->xpt_pool
!= pool
);
383 spin_unlock_bh(&pool
->sp_lock
);
385 EXPORT_SYMBOL_GPL(svc_xprt_enqueue
);
388 * Dequeue the first transport. Must be called with the pool->sp_lock held.
390 static struct svc_xprt
*svc_xprt_dequeue(struct svc_pool
*pool
)
392 struct svc_xprt
*xprt
;
394 if (list_empty(&pool
->sp_sockets
))
397 xprt
= list_entry(pool
->sp_sockets
.next
,
398 struct svc_xprt
, xpt_ready
);
399 list_del_init(&xprt
->xpt_ready
);
401 dprintk("svc: transport %p dequeued, inuse=%d\n",
402 xprt
, atomic_read(&xprt
->xpt_ref
.refcount
));
408 * svc_xprt_received conditionally queues the transport for processing
409 * by another thread. The caller must hold the XPT_BUSY bit and must
410 * not thereafter touch transport data.
412 * Note: XPT_DATA only gets cleared when a read-attempt finds no (or
413 * insufficient) data.
415 void svc_xprt_received(struct svc_xprt
*xprt
)
417 BUG_ON(!test_bit(XPT_BUSY
, &xprt
->xpt_flags
));
418 xprt
->xpt_pool
= NULL
;
419 clear_bit(XPT_BUSY
, &xprt
->xpt_flags
);
420 svc_xprt_enqueue(xprt
);
422 EXPORT_SYMBOL_GPL(svc_xprt_received
);
425 * svc_reserve - change the space reserved for the reply to a request.
426 * @rqstp: The request in question
427 * @space: new max space to reserve
429 * Each request reserves some space on the output queue of the transport
430 * to make sure the reply fits. This function reduces that reserved
431 * space to be the amount of space used already, plus @space.
434 void svc_reserve(struct svc_rqst
*rqstp
, int space
)
436 space
+= rqstp
->rq_res
.head
[0].iov_len
;
438 if (space
< rqstp
->rq_reserved
) {
439 struct svc_xprt
*xprt
= rqstp
->rq_xprt
;
440 atomic_sub((rqstp
->rq_reserved
- space
), &xprt
->xpt_reserved
);
441 rqstp
->rq_reserved
= space
;
443 svc_xprt_enqueue(xprt
);
446 EXPORT_SYMBOL_GPL(svc_reserve
);
448 static void svc_xprt_release(struct svc_rqst
*rqstp
)
450 struct svc_xprt
*xprt
= rqstp
->rq_xprt
;
452 rqstp
->rq_xprt
->xpt_ops
->xpo_release_rqst(rqstp
);
454 kfree(rqstp
->rq_deferred
);
455 rqstp
->rq_deferred
= NULL
;
457 svc_free_res_pages(rqstp
);
458 rqstp
->rq_res
.page_len
= 0;
459 rqstp
->rq_res
.page_base
= 0;
461 /* Reset response buffer and release
463 * But first, check that enough space was reserved
464 * for the reply, otherwise we have a bug!
466 if ((rqstp
->rq_res
.len
) > rqstp
->rq_reserved
)
467 printk(KERN_ERR
"RPC request reserved %d but used %d\n",
471 rqstp
->rq_res
.head
[0].iov_len
= 0;
472 svc_reserve(rqstp
, 0);
473 rqstp
->rq_xprt
= NULL
;
479 * External function to wake up a server waiting for data
480 * This really only makes sense for services like lockd
481 * which have exactly one thread anyway.
483 void svc_wake_up(struct svc_serv
*serv
)
485 struct svc_rqst
*rqstp
;
487 struct svc_pool
*pool
;
489 for (i
= 0; i
< serv
->sv_nrpools
; i
++) {
490 pool
= &serv
->sv_pools
[i
];
492 spin_lock_bh(&pool
->sp_lock
);
493 if (!list_empty(&pool
->sp_threads
)) {
494 rqstp
= list_entry(pool
->sp_threads
.next
,
497 dprintk("svc: daemon %p woken up.\n", rqstp
);
499 svc_thread_dequeue(pool, rqstp);
500 rqstp->rq_xprt = NULL;
502 wake_up(&rqstp
->rq_wait
);
504 spin_unlock_bh(&pool
->sp_lock
);
507 EXPORT_SYMBOL_GPL(svc_wake_up
);
509 int svc_port_is_privileged(struct sockaddr
*sin
)
511 switch (sin
->sa_family
) {
513 return ntohs(((struct sockaddr_in
*)sin
)->sin_port
)
516 return ntohs(((struct sockaddr_in6
*)sin
)->sin6_port
)
524 * Make sure that we don't have too many active connections. If we have,
525 * something must be dropped. It's not clear what will happen if we allow
526 * "too many" connections, but when dealing with network-facing software,
527 * we have to code defensively. Here we do that by imposing hard limits.
529 * There's no point in trying to do random drop here for DoS
530 * prevention. The NFS clients does 1 reconnect in 15 seconds. An
531 * attacker can easily beat that.
533 * The only somewhat efficient mechanism would be if drop old
534 * connections from the same IP first. But right now we don't even
535 * record the client IP in svc_sock.
537 * single-threaded services that expect a lot of clients will probably
538 * need to set sv_maxconn to override the default value which is based
539 * on the number of threads
541 static void svc_check_conn_limits(struct svc_serv
*serv
)
543 unsigned int limit
= serv
->sv_maxconn
? serv
->sv_maxconn
:
544 (serv
->sv_nrthreads
+3) * 20;
546 if (serv
->sv_tmpcnt
> limit
) {
547 struct svc_xprt
*xprt
= NULL
;
548 spin_lock_bh(&serv
->sv_lock
);
549 if (!list_empty(&serv
->sv_tempsocks
)) {
550 if (net_ratelimit()) {
551 /* Try to help the admin */
552 printk(KERN_NOTICE
"%s: too many open "
553 "connections, consider increasing %s\n",
554 serv
->sv_name
, serv
->sv_maxconn
?
555 "the max number of connections." :
556 "the number of threads.");
559 * Always select the oldest connection. It's not fair,
562 xprt
= list_entry(serv
->sv_tempsocks
.prev
,
565 set_bit(XPT_CLOSE
, &xprt
->xpt_flags
);
568 spin_unlock_bh(&serv
->sv_lock
);
571 svc_xprt_enqueue(xprt
);
578 * Receive the next request on any transport. This code is carefully
579 * organised not to touch any cachelines in the shared svc_serv
580 * structure, only cachelines in the local svc_pool.
582 int svc_recv(struct svc_rqst
*rqstp
, long timeout
)
584 struct svc_xprt
*xprt
= NULL
;
585 struct svc_serv
*serv
= rqstp
->rq_server
;
586 struct svc_pool
*pool
= rqstp
->rq_pool
;
590 DECLARE_WAITQUEUE(wait
, current
);
592 dprintk("svc: server %p waiting for data (to = %ld)\n",
597 "svc_recv: service %p, transport not NULL!\n",
599 if (waitqueue_active(&rqstp
->rq_wait
))
601 "svc_recv: service %p, wait queue active!\n",
604 /* now allocate needed pages. If we get a failure, sleep briefly */
605 pages
= (serv
->sv_max_mesg
+ PAGE_SIZE
) / PAGE_SIZE
;
606 for (i
= 0; i
< pages
; i
++)
607 while (rqstp
->rq_pages
[i
] == NULL
) {
608 struct page
*p
= alloc_page(GFP_KERNEL
);
610 set_current_state(TASK_INTERRUPTIBLE
);
611 if (signalled() || kthread_should_stop()) {
612 set_current_state(TASK_RUNNING
);
615 schedule_timeout(msecs_to_jiffies(500));
617 rqstp
->rq_pages
[i
] = p
;
619 rqstp
->rq_pages
[i
++] = NULL
; /* this might be seen in nfs_read_actor */
620 BUG_ON(pages
>= RPCSVC_MAXPAGES
);
622 /* Make arg->head point to first page and arg->pages point to rest */
623 arg
= &rqstp
->rq_arg
;
624 arg
->head
[0].iov_base
= page_address(rqstp
->rq_pages
[0]);
625 arg
->head
[0].iov_len
= PAGE_SIZE
;
626 arg
->pages
= rqstp
->rq_pages
+ 1;
628 /* save at least one page for response */
629 arg
->page_len
= (pages
-2)*PAGE_SIZE
;
630 arg
->len
= (pages
-1)*PAGE_SIZE
;
631 arg
->tail
[0].iov_len
= 0;
635 if (signalled() || kthread_should_stop())
638 spin_lock_bh(&pool
->sp_lock
);
639 xprt
= svc_xprt_dequeue(pool
);
641 rqstp
->rq_xprt
= xprt
;
643 rqstp
->rq_reserved
= serv
->sv_max_mesg
;
644 atomic_add(rqstp
->rq_reserved
, &xprt
->xpt_reserved
);
646 /* No data pending. Go to sleep */
647 svc_thread_enqueue(pool
, rqstp
);
650 * We have to be able to interrupt this wait
651 * to bring down the daemons ...
653 set_current_state(TASK_INTERRUPTIBLE
);
656 * checking kthread_should_stop() here allows us to avoid
657 * locking and signalling when stopping kthreads that call
658 * svc_recv. If the thread has already been woken up, then
659 * we can exit here without sleeping. If not, then it
660 * it'll be woken up quickly during the schedule_timeout
662 if (kthread_should_stop()) {
663 set_current_state(TASK_RUNNING
);
664 spin_unlock_bh(&pool
->sp_lock
);
668 add_wait_queue(&rqstp
->rq_wait
, &wait
);
669 spin_unlock_bh(&pool
->sp_lock
);
671 schedule_timeout(timeout
);
675 spin_lock_bh(&pool
->sp_lock
);
676 remove_wait_queue(&rqstp
->rq_wait
, &wait
);
678 xprt
= rqstp
->rq_xprt
;
680 svc_thread_dequeue(pool
, rqstp
);
681 spin_unlock_bh(&pool
->sp_lock
);
682 dprintk("svc: server %p, no data yet\n", rqstp
);
683 if (signalled() || kthread_should_stop())
689 spin_unlock_bh(&pool
->sp_lock
);
692 if (test_bit(XPT_CLOSE
, &xprt
->xpt_flags
)) {
693 dprintk("svc_recv: found XPT_CLOSE\n");
694 svc_delete_xprt(xprt
);
695 } else if (test_bit(XPT_LISTENER
, &xprt
->xpt_flags
)) {
696 struct svc_xprt
*newxpt
;
697 newxpt
= xprt
->xpt_ops
->xpo_accept(xprt
);
700 * We know this module_get will succeed because the
701 * listener holds a reference too
703 __module_get(newxpt
->xpt_class
->xcl_owner
);
704 svc_check_conn_limits(xprt
->xpt_server
);
705 spin_lock_bh(&serv
->sv_lock
);
706 set_bit(XPT_TEMP
, &newxpt
->xpt_flags
);
707 list_add(&newxpt
->xpt_list
, &serv
->sv_tempsocks
);
709 if (serv
->sv_temptimer
.function
== NULL
) {
710 /* setup timer to age temp transports */
711 setup_timer(&serv
->sv_temptimer
,
713 (unsigned long)serv
);
714 mod_timer(&serv
->sv_temptimer
,
715 jiffies
+ svc_conn_age_period
* HZ
);
717 spin_unlock_bh(&serv
->sv_lock
);
718 svc_xprt_received(newxpt
);
720 svc_xprt_received(xprt
);
722 dprintk("svc: server %p, pool %u, transport %p, inuse=%d\n",
723 rqstp
, pool
->sp_id
, xprt
,
724 atomic_read(&xprt
->xpt_ref
.refcount
));
725 rqstp
->rq_deferred
= svc_deferred_dequeue(xprt
);
726 if (rqstp
->rq_deferred
) {
727 svc_xprt_received(xprt
);
728 len
= svc_deferred_recv(rqstp
);
730 len
= xprt
->xpt_ops
->xpo_recvfrom(rqstp
);
731 dprintk("svc: got len=%d\n", len
);
734 /* No data, incomplete (TCP) read, or accept() */
735 if (len
== 0 || len
== -EAGAIN
) {
736 rqstp
->rq_res
.len
= 0;
737 svc_xprt_release(rqstp
);
740 clear_bit(XPT_OLD
, &xprt
->xpt_flags
);
742 rqstp
->rq_secure
= svc_port_is_privileged(svc_addr(rqstp
));
743 rqstp
->rq_chandle
.defer
= svc_defer
;
746 serv
->sv_stats
->netcnt
++;
749 EXPORT_SYMBOL_GPL(svc_recv
);
754 void svc_drop(struct svc_rqst
*rqstp
)
756 dprintk("svc: xprt %p dropped request\n", rqstp
->rq_xprt
);
757 svc_xprt_release(rqstp
);
759 EXPORT_SYMBOL_GPL(svc_drop
);
762 * Return reply to client.
764 int svc_send(struct svc_rqst
*rqstp
)
766 struct svc_xprt
*xprt
;
770 xprt
= rqstp
->rq_xprt
;
774 /* release the receive skb before sending the reply */
775 rqstp
->rq_xprt
->xpt_ops
->xpo_release_rqst(rqstp
);
777 /* calculate over-all length */
779 xb
->len
= xb
->head
[0].iov_len
+
783 /* Grab mutex to serialize outgoing data. */
784 mutex_lock(&xprt
->xpt_mutex
);
785 if (test_bit(XPT_DEAD
, &xprt
->xpt_flags
))
788 len
= xprt
->xpt_ops
->xpo_sendto(rqstp
);
789 mutex_unlock(&xprt
->xpt_mutex
);
790 svc_xprt_release(rqstp
);
792 if (len
== -ECONNREFUSED
|| len
== -ENOTCONN
|| len
== -EAGAIN
)
798 * Timer function to close old temporary transports, using
799 * a mark-and-sweep algorithm.
801 static void svc_age_temp_xprts(unsigned long closure
)
803 struct svc_serv
*serv
= (struct svc_serv
*)closure
;
804 struct svc_xprt
*xprt
;
805 struct list_head
*le
, *next
;
806 LIST_HEAD(to_be_aged
);
808 dprintk("svc_age_temp_xprts\n");
810 if (!spin_trylock_bh(&serv
->sv_lock
)) {
811 /* busy, try again 1 sec later */
812 dprintk("svc_age_temp_xprts: busy\n");
813 mod_timer(&serv
->sv_temptimer
, jiffies
+ HZ
);
817 list_for_each_safe(le
, next
, &serv
->sv_tempsocks
) {
818 xprt
= list_entry(le
, struct svc_xprt
, xpt_list
);
820 /* First time through, just mark it OLD. Second time
821 * through, close it. */
822 if (!test_and_set_bit(XPT_OLD
, &xprt
->xpt_flags
))
824 if (atomic_read(&xprt
->xpt_ref
.refcount
) > 1
825 || test_bit(XPT_BUSY
, &xprt
->xpt_flags
))
828 list_move(le
, &to_be_aged
);
829 set_bit(XPT_CLOSE
, &xprt
->xpt_flags
);
830 set_bit(XPT_DETACHED
, &xprt
->xpt_flags
);
832 spin_unlock_bh(&serv
->sv_lock
);
834 while (!list_empty(&to_be_aged
)) {
835 le
= to_be_aged
.next
;
836 /* fiddling the xpt_list node is safe 'cos we're XPT_DETACHED */
838 xprt
= list_entry(le
, struct svc_xprt
, xpt_list
);
840 dprintk("queuing xprt %p for closing\n", xprt
);
842 /* a thread will dequeue and close it soon */
843 svc_xprt_enqueue(xprt
);
847 mod_timer(&serv
->sv_temptimer
, jiffies
+ svc_conn_age_period
* HZ
);
851 * Remove a dead transport
853 void svc_delete_xprt(struct svc_xprt
*xprt
)
855 struct svc_serv
*serv
= xprt
->xpt_server
;
856 struct svc_deferred_req
*dr
;
858 /* Only do this once */
859 if (test_and_set_bit(XPT_DEAD
, &xprt
->xpt_flags
))
862 dprintk("svc: svc_delete_xprt(%p)\n", xprt
);
863 xprt
->xpt_ops
->xpo_detach(xprt
);
865 spin_lock_bh(&serv
->sv_lock
);
866 if (!test_and_set_bit(XPT_DETACHED
, &xprt
->xpt_flags
))
867 list_del_init(&xprt
->xpt_list
);
869 * We used to delete the transport from whichever list
870 * it's sk_xprt.xpt_ready node was on, but we don't actually
871 * need to. This is because the only time we're called
872 * while still attached to a queue, the queue itself
873 * is about to be destroyed (in svc_destroy).
875 if (test_bit(XPT_TEMP
, &xprt
->xpt_flags
))
878 for (dr
= svc_deferred_dequeue(xprt
); dr
;
879 dr
= svc_deferred_dequeue(xprt
)) {
885 spin_unlock_bh(&serv
->sv_lock
);
888 void svc_close_xprt(struct svc_xprt
*xprt
)
890 set_bit(XPT_CLOSE
, &xprt
->xpt_flags
);
891 if (test_and_set_bit(XPT_BUSY
, &xprt
->xpt_flags
))
892 /* someone else will have to effect the close */
896 svc_delete_xprt(xprt
);
897 clear_bit(XPT_BUSY
, &xprt
->xpt_flags
);
900 EXPORT_SYMBOL_GPL(svc_close_xprt
);
902 void svc_close_all(struct list_head
*xprt_list
)
904 struct svc_xprt
*xprt
;
905 struct svc_xprt
*tmp
;
907 list_for_each_entry_safe(xprt
, tmp
, xprt_list
, xpt_list
) {
908 set_bit(XPT_CLOSE
, &xprt
->xpt_flags
);
909 if (test_bit(XPT_BUSY
, &xprt
->xpt_flags
)) {
910 /* Waiting to be processed, but no threads left,
911 * So just remove it from the waiting list
913 list_del_init(&xprt
->xpt_ready
);
914 clear_bit(XPT_BUSY
, &xprt
->xpt_flags
);
916 svc_close_xprt(xprt
);
921 * Handle defer and revisit of requests
924 static void svc_revisit(struct cache_deferred_req
*dreq
, int too_many
)
926 struct svc_deferred_req
*dr
=
927 container_of(dreq
, struct svc_deferred_req
, handle
);
928 struct svc_xprt
*xprt
= dr
->xprt
;
930 spin_lock(&xprt
->xpt_lock
);
931 set_bit(XPT_DEFERRED
, &xprt
->xpt_flags
);
932 if (too_many
|| test_bit(XPT_DEAD
, &xprt
->xpt_flags
)) {
933 spin_unlock(&xprt
->xpt_lock
);
934 dprintk("revisit canceled\n");
939 dprintk("revisit queued\n");
941 list_add(&dr
->handle
.recent
, &xprt
->xpt_deferred
);
942 spin_unlock(&xprt
->xpt_lock
);
943 svc_xprt_enqueue(xprt
);
948 * Save the request off for later processing. The request buffer looks
951 * <xprt-header><rpc-header><rpc-pagelist><rpc-tail>
953 * This code can only handle requests that consist of an xprt-header
956 static struct cache_deferred_req
*svc_defer(struct cache_req
*req
)
958 struct svc_rqst
*rqstp
= container_of(req
, struct svc_rqst
, rq_chandle
);
959 struct svc_deferred_req
*dr
;
961 if (rqstp
->rq_arg
.page_len
)
962 return NULL
; /* if more than a page, give up FIXME */
963 if (rqstp
->rq_deferred
) {
964 dr
= rqstp
->rq_deferred
;
965 rqstp
->rq_deferred
= NULL
;
969 /* FIXME maybe discard if size too large */
970 size
= sizeof(struct svc_deferred_req
) + rqstp
->rq_arg
.len
;
971 dr
= kmalloc(size
, GFP_KERNEL
);
975 dr
->handle
.owner
= rqstp
->rq_server
;
976 dr
->prot
= rqstp
->rq_prot
;
977 memcpy(&dr
->addr
, &rqstp
->rq_addr
, rqstp
->rq_addrlen
);
978 dr
->addrlen
= rqstp
->rq_addrlen
;
979 dr
->daddr
= rqstp
->rq_daddr
;
980 dr
->argslen
= rqstp
->rq_arg
.len
>> 2;
981 dr
->xprt_hlen
= rqstp
->rq_xprt_hlen
;
983 /* back up head to the start of the buffer and copy */
984 skip
= rqstp
->rq_arg
.len
- rqstp
->rq_arg
.head
[0].iov_len
;
985 memcpy(dr
->args
, rqstp
->rq_arg
.head
[0].iov_base
- skip
,
988 svc_xprt_get(rqstp
->rq_xprt
);
989 dr
->xprt
= rqstp
->rq_xprt
;
991 dr
->handle
.revisit
= svc_revisit
;
996 * recv data from a deferred request into an active one
998 static int svc_deferred_recv(struct svc_rqst
*rqstp
)
1000 struct svc_deferred_req
*dr
= rqstp
->rq_deferred
;
1002 /* setup iov_base past transport header */
1003 rqstp
->rq_arg
.head
[0].iov_base
= dr
->args
+ (dr
->xprt_hlen
>>2);
1004 /* The iov_len does not include the transport header bytes */
1005 rqstp
->rq_arg
.head
[0].iov_len
= (dr
->argslen
<<2) - dr
->xprt_hlen
;
1006 rqstp
->rq_arg
.page_len
= 0;
1007 /* The rq_arg.len includes the transport header bytes */
1008 rqstp
->rq_arg
.len
= dr
->argslen
<<2;
1009 rqstp
->rq_prot
= dr
->prot
;
1010 memcpy(&rqstp
->rq_addr
, &dr
->addr
, dr
->addrlen
);
1011 rqstp
->rq_addrlen
= dr
->addrlen
;
1012 /* Save off transport header len in case we get deferred again */
1013 rqstp
->rq_xprt_hlen
= dr
->xprt_hlen
;
1014 rqstp
->rq_daddr
= dr
->daddr
;
1015 rqstp
->rq_respages
= rqstp
->rq_pages
;
1016 return (dr
->argslen
<<2) - dr
->xprt_hlen
;
1020 static struct svc_deferred_req
*svc_deferred_dequeue(struct svc_xprt
*xprt
)
1022 struct svc_deferred_req
*dr
= NULL
;
1024 if (!test_bit(XPT_DEFERRED
, &xprt
->xpt_flags
))
1026 spin_lock(&xprt
->xpt_lock
);
1027 clear_bit(XPT_DEFERRED
, &xprt
->xpt_flags
);
1028 if (!list_empty(&xprt
->xpt_deferred
)) {
1029 dr
= list_entry(xprt
->xpt_deferred
.next
,
1030 struct svc_deferred_req
,
1032 list_del_init(&dr
->handle
.recent
);
1033 set_bit(XPT_DEFERRED
, &xprt
->xpt_flags
);
1035 spin_unlock(&xprt
->xpt_lock
);
1040 * svc_find_xprt - find an RPC transport instance
1041 * @serv: pointer to svc_serv to search
1042 * @xcl_name: C string containing transport's class name
1043 * @af: Address family of transport's local address
1044 * @port: transport's IP port number
1046 * Return the transport instance pointer for the endpoint accepting
1047 * connections/peer traffic from the specified transport class,
1048 * address family and port.
1050 * Specifying 0 for the address family or port is effectively a
1051 * wild-card, and will result in matching the first transport in the
1052 * service's list that has a matching class name.
1054 struct svc_xprt
*svc_find_xprt(struct svc_serv
*serv
, const char *xcl_name
,
1055 const sa_family_t af
, const unsigned short port
)
1057 struct svc_xprt
*xprt
;
1058 struct svc_xprt
*found
= NULL
;
1060 /* Sanity check the args */
1061 if (serv
== NULL
|| xcl_name
== NULL
)
1064 spin_lock_bh(&serv
->sv_lock
);
1065 list_for_each_entry(xprt
, &serv
->sv_permsocks
, xpt_list
) {
1066 if (strcmp(xprt
->xpt_class
->xcl_name
, xcl_name
))
1068 if (af
!= AF_UNSPEC
&& af
!= xprt
->xpt_local
.ss_family
)
1070 if (port
!= 0 && port
!= svc_xprt_local_port(xprt
))
1076 spin_unlock_bh(&serv
->sv_lock
);
1079 EXPORT_SYMBOL_GPL(svc_find_xprt
);
1082 * Format a buffer with a list of the active transports. A zero for
1083 * the buflen parameter disables target buffer overflow checking.
1085 int svc_xprt_names(struct svc_serv
*serv
, char *buf
, int buflen
)
1087 struct svc_xprt
*xprt
;
1092 /* Sanity check args */
1096 spin_lock_bh(&serv
->sv_lock
);
1097 list_for_each_entry(xprt
, &serv
->sv_permsocks
, xpt_list
) {
1098 len
= snprintf(xprt_str
, sizeof(xprt_str
),
1099 "%s %d\n", xprt
->xpt_class
->xcl_name
,
1100 svc_xprt_local_port(xprt
));
1101 /* If the string was truncated, replace with error string */
1102 if (len
>= sizeof(xprt_str
))
1103 strcpy(xprt_str
, "name-too-long\n");
1104 /* Don't overflow buffer */
1105 len
= strlen(xprt_str
);
1106 if (buflen
&& (len
+ totlen
>= buflen
))
1108 strcpy(buf
+totlen
, xprt_str
);
1111 spin_unlock_bh(&serv
->sv_lock
);
1114 EXPORT_SYMBOL_GPL(svc_xprt_names
);