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