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