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
= 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
);
439 static void svc_xprt_release(struct svc_rqst
*rqstp
)
441 struct svc_xprt
*xprt
= rqstp
->rq_xprt
;
443 rqstp
->rq_xprt
->xpt_ops
->xpo_release_rqst(rqstp
);
445 svc_free_res_pages(rqstp
);
446 rqstp
->rq_res
.page_len
= 0;
447 rqstp
->rq_res
.page_base
= 0;
449 /* Reset response buffer and release
451 * But first, check that enough space was reserved
452 * for the reply, otherwise we have a bug!
454 if ((rqstp
->rq_res
.len
) > rqstp
->rq_reserved
)
455 printk(KERN_ERR
"RPC request reserved %d but used %d\n",
459 rqstp
->rq_res
.head
[0].iov_len
= 0;
460 svc_reserve(rqstp
, 0);
461 rqstp
->rq_xprt
= NULL
;
467 * External function to wake up a server waiting for data
468 * This really only makes sense for services like lockd
469 * which have exactly one thread anyway.
471 void svc_wake_up(struct svc_serv
*serv
)
473 struct svc_rqst
*rqstp
;
475 struct svc_pool
*pool
;
477 for (i
= 0; i
< serv
->sv_nrpools
; i
++) {
478 pool
= &serv
->sv_pools
[i
];
480 spin_lock_bh(&pool
->sp_lock
);
481 if (!list_empty(&pool
->sp_threads
)) {
482 rqstp
= list_entry(pool
->sp_threads
.next
,
485 dprintk("svc: daemon %p woken up.\n", rqstp
);
487 svc_thread_dequeue(pool, rqstp);
488 rqstp->rq_xprt = NULL;
490 wake_up(&rqstp
->rq_wait
);
492 spin_unlock_bh(&pool
->sp_lock
);
496 int svc_port_is_privileged(struct sockaddr
*sin
)
498 switch (sin
->sa_family
) {
500 return ntohs(((struct sockaddr_in
*)sin
)->sin_port
)
503 return ntohs(((struct sockaddr_in6
*)sin
)->sin6_port
)
511 * Make sure that we don't have too many active connections. If we
512 * have, something must be dropped.
514 * There's no point in trying to do random drop here for DoS
515 * prevention. The NFS clients does 1 reconnect in 15 seconds. An
516 * attacker can easily beat that.
518 * The only somewhat efficient mechanism would be if drop old
519 * connections from the same IP first. But right now we don't even
520 * record the client IP in svc_sock.
522 static void svc_check_conn_limits(struct svc_serv
*serv
)
524 if (serv
->sv_tmpcnt
> (serv
->sv_nrthreads
+3)*20) {
525 struct svc_xprt
*xprt
= NULL
;
526 spin_lock_bh(&serv
->sv_lock
);
527 if (!list_empty(&serv
->sv_tempsocks
)) {
528 if (net_ratelimit()) {
529 /* Try to help the admin */
530 printk(KERN_NOTICE
"%s: too many open "
531 "connections, consider increasing the "
532 "number of nfsd threads\n",
536 * Always select the oldest connection. It's not fair,
539 xprt
= list_entry(serv
->sv_tempsocks
.prev
,
542 set_bit(XPT_CLOSE
, &xprt
->xpt_flags
);
545 spin_unlock_bh(&serv
->sv_lock
);
548 svc_xprt_enqueue(xprt
);
555 * Receive the next request on any transport. This code is carefully
556 * organised not to touch any cachelines in the shared svc_serv
557 * structure, only cachelines in the local svc_pool.
559 int svc_recv(struct svc_rqst
*rqstp
, long timeout
)
561 struct svc_xprt
*xprt
= NULL
;
562 struct svc_serv
*serv
= rqstp
->rq_server
;
563 struct svc_pool
*pool
= rqstp
->rq_pool
;
567 DECLARE_WAITQUEUE(wait
, current
);
569 dprintk("svc: server %p waiting for data (to = %ld)\n",
574 "svc_recv: service %p, transport not NULL!\n",
576 if (waitqueue_active(&rqstp
->rq_wait
))
578 "svc_recv: service %p, wait queue active!\n",
581 /* now allocate needed pages. If we get a failure, sleep briefly */
582 pages
= (serv
->sv_max_mesg
+ PAGE_SIZE
) / PAGE_SIZE
;
583 for (i
= 0; i
< pages
; i
++)
584 while (rqstp
->rq_pages
[i
] == NULL
) {
585 struct page
*p
= alloc_page(GFP_KERNEL
);
587 int j
= msecs_to_jiffies(500);
588 schedule_timeout_uninterruptible(j
);
590 rqstp
->rq_pages
[i
] = p
;
592 rqstp
->rq_pages
[i
++] = NULL
; /* this might be seen in nfs_read_actor */
593 BUG_ON(pages
>= RPCSVC_MAXPAGES
);
595 /* Make arg->head point to first page and arg->pages point to rest */
596 arg
= &rqstp
->rq_arg
;
597 arg
->head
[0].iov_base
= page_address(rqstp
->rq_pages
[0]);
598 arg
->head
[0].iov_len
= PAGE_SIZE
;
599 arg
->pages
= rqstp
->rq_pages
+ 1;
601 /* save at least one page for response */
602 arg
->page_len
= (pages
-2)*PAGE_SIZE
;
603 arg
->len
= (pages
-1)*PAGE_SIZE
;
604 arg
->tail
[0].iov_len
= 0;
611 spin_lock_bh(&pool
->sp_lock
);
612 xprt
= svc_xprt_dequeue(pool
);
614 rqstp
->rq_xprt
= xprt
;
616 rqstp
->rq_reserved
= serv
->sv_max_mesg
;
617 atomic_add(rqstp
->rq_reserved
, &xprt
->xpt_reserved
);
619 /* No data pending. Go to sleep */
620 svc_thread_enqueue(pool
, rqstp
);
623 * We have to be able to interrupt this wait
624 * to bring down the daemons ...
626 set_current_state(TASK_INTERRUPTIBLE
);
627 add_wait_queue(&rqstp
->rq_wait
, &wait
);
628 spin_unlock_bh(&pool
->sp_lock
);
630 schedule_timeout(timeout
);
634 spin_lock_bh(&pool
->sp_lock
);
635 remove_wait_queue(&rqstp
->rq_wait
, &wait
);
637 xprt
= rqstp
->rq_xprt
;
639 svc_thread_dequeue(pool
, rqstp
);
640 spin_unlock_bh(&pool
->sp_lock
);
641 dprintk("svc: server %p, no data yet\n", rqstp
);
642 return signalled()? -EINTR
: -EAGAIN
;
645 spin_unlock_bh(&pool
->sp_lock
);
648 if (test_bit(XPT_CLOSE
, &xprt
->xpt_flags
)) {
649 dprintk("svc_recv: found XPT_CLOSE\n");
650 svc_delete_xprt(xprt
);
651 } else if (test_bit(XPT_LISTENER
, &xprt
->xpt_flags
)) {
652 struct svc_xprt
*newxpt
;
653 newxpt
= xprt
->xpt_ops
->xpo_accept(xprt
);
656 * We know this module_get will succeed because the
657 * listener holds a reference too
659 __module_get(newxpt
->xpt_class
->xcl_owner
);
660 svc_check_conn_limits(xprt
->xpt_server
);
661 spin_lock_bh(&serv
->sv_lock
);
662 set_bit(XPT_TEMP
, &newxpt
->xpt_flags
);
663 list_add(&newxpt
->xpt_list
, &serv
->sv_tempsocks
);
665 if (serv
->sv_temptimer
.function
== NULL
) {
666 /* setup timer to age temp transports */
667 setup_timer(&serv
->sv_temptimer
,
669 (unsigned long)serv
);
670 mod_timer(&serv
->sv_temptimer
,
671 jiffies
+ svc_conn_age_period
* HZ
);
673 spin_unlock_bh(&serv
->sv_lock
);
674 svc_xprt_received(newxpt
);
676 svc_xprt_received(xprt
);
678 dprintk("svc: server %p, pool %u, transport %p, inuse=%d\n",
679 rqstp
, pool
->sp_id
, xprt
,
680 atomic_read(&xprt
->xpt_ref
.refcount
));
681 rqstp
->rq_deferred
= svc_deferred_dequeue(xprt
);
682 if (rqstp
->rq_deferred
) {
683 svc_xprt_received(xprt
);
684 len
= svc_deferred_recv(rqstp
);
686 len
= xprt
->xpt_ops
->xpo_recvfrom(rqstp
);
687 dprintk("svc: got len=%d\n", len
);
690 /* No data, incomplete (TCP) read, or accept() */
691 if (len
== 0 || len
== -EAGAIN
) {
692 rqstp
->rq_res
.len
= 0;
693 svc_xprt_release(rqstp
);
696 clear_bit(XPT_OLD
, &xprt
->xpt_flags
);
698 rqstp
->rq_secure
= svc_port_is_privileged(svc_addr(rqstp
));
699 rqstp
->rq_chandle
.defer
= svc_defer
;
702 serv
->sv_stats
->netcnt
++;
709 void svc_drop(struct svc_rqst
*rqstp
)
711 dprintk("svc: xprt %p dropped request\n", rqstp
->rq_xprt
);
712 svc_xprt_release(rqstp
);
716 * Return reply to client.
718 int svc_send(struct svc_rqst
*rqstp
)
720 struct svc_xprt
*xprt
;
724 xprt
= rqstp
->rq_xprt
;
728 /* release the receive skb before sending the reply */
729 rqstp
->rq_xprt
->xpt_ops
->xpo_release_rqst(rqstp
);
731 /* calculate over-all length */
733 xb
->len
= xb
->head
[0].iov_len
+
737 /* Grab mutex to serialize outgoing data. */
738 mutex_lock(&xprt
->xpt_mutex
);
739 if (test_bit(XPT_DEAD
, &xprt
->xpt_flags
))
742 len
= xprt
->xpt_ops
->xpo_sendto(rqstp
);
743 mutex_unlock(&xprt
->xpt_mutex
);
744 svc_xprt_release(rqstp
);
746 if (len
== -ECONNREFUSED
|| len
== -ENOTCONN
|| len
== -EAGAIN
)
752 * Timer function to close old temporary transports, using
753 * a mark-and-sweep algorithm.
755 static void svc_age_temp_xprts(unsigned long closure
)
757 struct svc_serv
*serv
= (struct svc_serv
*)closure
;
758 struct svc_xprt
*xprt
;
759 struct list_head
*le
, *next
;
760 LIST_HEAD(to_be_aged
);
762 dprintk("svc_age_temp_xprts\n");
764 if (!spin_trylock_bh(&serv
->sv_lock
)) {
765 /* busy, try again 1 sec later */
766 dprintk("svc_age_temp_xprts: busy\n");
767 mod_timer(&serv
->sv_temptimer
, jiffies
+ HZ
);
771 list_for_each_safe(le
, next
, &serv
->sv_tempsocks
) {
772 xprt
= list_entry(le
, struct svc_xprt
, xpt_list
);
774 /* First time through, just mark it OLD. Second time
775 * through, close it. */
776 if (!test_and_set_bit(XPT_OLD
, &xprt
->xpt_flags
))
778 if (atomic_read(&xprt
->xpt_ref
.refcount
) > 1
779 || test_bit(XPT_BUSY
, &xprt
->xpt_flags
))
782 list_move(le
, &to_be_aged
);
783 set_bit(XPT_CLOSE
, &xprt
->xpt_flags
);
784 set_bit(XPT_DETACHED
, &xprt
->xpt_flags
);
786 spin_unlock_bh(&serv
->sv_lock
);
788 while (!list_empty(&to_be_aged
)) {
789 le
= to_be_aged
.next
;
790 /* fiddling the xpt_list node is safe 'cos we're XPT_DETACHED */
792 xprt
= list_entry(le
, struct svc_xprt
, xpt_list
);
794 dprintk("queuing xprt %p for closing\n", xprt
);
796 /* a thread will dequeue and close it soon */
797 svc_xprt_enqueue(xprt
);
801 mod_timer(&serv
->sv_temptimer
, jiffies
+ svc_conn_age_period
* HZ
);
805 * Remove a dead transport
807 void svc_delete_xprt(struct svc_xprt
*xprt
)
809 struct svc_serv
*serv
= xprt
->xpt_server
;
811 dprintk("svc: svc_delete_xprt(%p)\n", xprt
);
812 xprt
->xpt_ops
->xpo_detach(xprt
);
814 spin_lock_bh(&serv
->sv_lock
);
815 if (!test_and_set_bit(XPT_DETACHED
, &xprt
->xpt_flags
))
816 list_del_init(&xprt
->xpt_list
);
818 * We used to delete the transport from whichever list
819 * it's sk_xprt.xpt_ready node was on, but we don't actually
820 * need to. This is because the only time we're called
821 * while still attached to a queue, the queue itself
822 * is about to be destroyed (in svc_destroy).
824 if (!test_and_set_bit(XPT_DEAD
, &xprt
->xpt_flags
)) {
825 BUG_ON(atomic_read(&xprt
->xpt_ref
.refcount
) < 2);
826 if (test_bit(XPT_TEMP
, &xprt
->xpt_flags
))
830 spin_unlock_bh(&serv
->sv_lock
);
833 void svc_close_xprt(struct svc_xprt
*xprt
)
835 set_bit(XPT_CLOSE
, &xprt
->xpt_flags
);
836 if (test_and_set_bit(XPT_BUSY
, &xprt
->xpt_flags
))
837 /* someone else will have to effect the close */
841 svc_delete_xprt(xprt
);
842 clear_bit(XPT_BUSY
, &xprt
->xpt_flags
);
846 void svc_close_all(struct list_head
*xprt_list
)
848 struct svc_xprt
*xprt
;
849 struct svc_xprt
*tmp
;
851 list_for_each_entry_safe(xprt
, tmp
, xprt_list
, xpt_list
) {
852 set_bit(XPT_CLOSE
, &xprt
->xpt_flags
);
853 if (test_bit(XPT_BUSY
, &xprt
->xpt_flags
)) {
854 /* Waiting to be processed, but no threads left,
855 * So just remove it from the waiting list
857 list_del_init(&xprt
->xpt_ready
);
858 clear_bit(XPT_BUSY
, &xprt
->xpt_flags
);
860 svc_close_xprt(xprt
);
865 * Handle defer and revisit of requests
868 static void svc_revisit(struct cache_deferred_req
*dreq
, int too_many
)
870 struct svc_deferred_req
*dr
=
871 container_of(dreq
, struct svc_deferred_req
, handle
);
872 struct svc_xprt
*xprt
= dr
->xprt
;
879 dprintk("revisit queued\n");
881 spin_lock(&xprt
->xpt_lock
);
882 list_add(&dr
->handle
.recent
, &xprt
->xpt_deferred
);
883 spin_unlock(&xprt
->xpt_lock
);
884 set_bit(XPT_DEFERRED
, &xprt
->xpt_flags
);
885 svc_xprt_enqueue(xprt
);
890 * Save the request off for later processing. The request buffer looks
893 * <xprt-header><rpc-header><rpc-pagelist><rpc-tail>
895 * This code can only handle requests that consist of an xprt-header
898 static struct cache_deferred_req
*svc_defer(struct cache_req
*req
)
900 struct svc_rqst
*rqstp
= container_of(req
, struct svc_rqst
, rq_chandle
);
901 struct svc_deferred_req
*dr
;
903 if (rqstp
->rq_arg
.page_len
)
904 return NULL
; /* if more than a page, give up FIXME */
905 if (rqstp
->rq_deferred
) {
906 dr
= rqstp
->rq_deferred
;
907 rqstp
->rq_deferred
= NULL
;
911 /* FIXME maybe discard if size too large */
912 size
= sizeof(struct svc_deferred_req
) + rqstp
->rq_arg
.len
;
913 dr
= kmalloc(size
, GFP_KERNEL
);
917 dr
->handle
.owner
= rqstp
->rq_server
;
918 dr
->prot
= rqstp
->rq_prot
;
919 memcpy(&dr
->addr
, &rqstp
->rq_addr
, rqstp
->rq_addrlen
);
920 dr
->addrlen
= rqstp
->rq_addrlen
;
921 dr
->daddr
= rqstp
->rq_daddr
;
922 dr
->argslen
= rqstp
->rq_arg
.len
>> 2;
923 dr
->xprt_hlen
= rqstp
->rq_xprt_hlen
;
925 /* back up head to the start of the buffer and copy */
926 skip
= rqstp
->rq_arg
.len
- rqstp
->rq_arg
.head
[0].iov_len
;
927 memcpy(dr
->args
, rqstp
->rq_arg
.head
[0].iov_base
- skip
,
930 svc_xprt_get(rqstp
->rq_xprt
);
931 dr
->xprt
= rqstp
->rq_xprt
;
933 dr
->handle
.revisit
= svc_revisit
;
938 * recv data from a deferred request into an active one
940 static int svc_deferred_recv(struct svc_rqst
*rqstp
)
942 struct svc_deferred_req
*dr
= rqstp
->rq_deferred
;
944 /* setup iov_base past transport header */
945 rqstp
->rq_arg
.head
[0].iov_base
= dr
->args
+ (dr
->xprt_hlen
>>2);
946 /* The iov_len does not include the transport header bytes */
947 rqstp
->rq_arg
.head
[0].iov_len
= (dr
->argslen
<<2) - dr
->xprt_hlen
;
948 rqstp
->rq_arg
.page_len
= 0;
949 /* The rq_arg.len includes the transport header bytes */
950 rqstp
->rq_arg
.len
= dr
->argslen
<<2;
951 rqstp
->rq_prot
= dr
->prot
;
952 memcpy(&rqstp
->rq_addr
, &dr
->addr
, dr
->addrlen
);
953 rqstp
->rq_addrlen
= dr
->addrlen
;
954 /* Save off transport header len in case we get deferred again */
955 rqstp
->rq_xprt_hlen
= dr
->xprt_hlen
;
956 rqstp
->rq_daddr
= dr
->daddr
;
957 rqstp
->rq_respages
= rqstp
->rq_pages
;
958 return (dr
->argslen
<<2) - dr
->xprt_hlen
;
962 static struct svc_deferred_req
*svc_deferred_dequeue(struct svc_xprt
*xprt
)
964 struct svc_deferred_req
*dr
= NULL
;
966 if (!test_bit(XPT_DEFERRED
, &xprt
->xpt_flags
))
968 spin_lock(&xprt
->xpt_lock
);
969 clear_bit(XPT_DEFERRED
, &xprt
->xpt_flags
);
970 if (!list_empty(&xprt
->xpt_deferred
)) {
971 dr
= list_entry(xprt
->xpt_deferred
.next
,
972 struct svc_deferred_req
,
974 list_del_init(&dr
->handle
.recent
);
975 set_bit(XPT_DEFERRED
, &xprt
->xpt_flags
);
977 spin_unlock(&xprt
->xpt_lock
);
982 * Return the transport instance pointer for the endpoint accepting
983 * connections/peer traffic from the specified transport class,
984 * address family and port.
986 * Specifying 0 for the address family or port is effectively a
987 * wild-card, and will result in matching the first transport in the
988 * service's list that has a matching class name.
990 struct svc_xprt
*svc_find_xprt(struct svc_serv
*serv
, char *xcl_name
,
993 struct svc_xprt
*xprt
;
994 struct svc_xprt
*found
= NULL
;
996 /* Sanity check the args */
997 if (!serv
|| !xcl_name
)
1000 spin_lock_bh(&serv
->sv_lock
);
1001 list_for_each_entry(xprt
, &serv
->sv_permsocks
, xpt_list
) {
1002 if (strcmp(xprt
->xpt_class
->xcl_name
, xcl_name
))
1004 if (af
!= AF_UNSPEC
&& af
!= xprt
->xpt_local
.ss_family
)
1006 if (port
&& port
!= svc_xprt_local_port(xprt
))
1011 spin_unlock_bh(&serv
->sv_lock
);
1014 EXPORT_SYMBOL_GPL(svc_find_xprt
);
This page took 0.049655 seconds and 6 git commands to generate.