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