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