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