blob: 220b79988000ed11e9ddf2abf10b5acb521e366f [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/net/sunrpc/svc.c
4 *
5 * High-level RPC service routines
6 *
7 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
Greg Banksbfd24162006-10-02 02:18:01 -07008 *
9 * Multiple threads pools and NUMAisation
10 * Copyright (c) 2006 Silicon Graphics, Inc.
11 * by Greg Banks <gnb@melbourne.sgi.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
14#include <linux/linkage.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010015#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/errno.h>
17#include <linux/net.h>
18#include <linux/in.h>
19#include <linux/mm.h>
Greg Banksa7455442006-10-02 02:17:59 -070020#include <linux/interrupt.h>
21#include <linux/module.h>
Jeff Layton9867d762008-06-10 08:40:38 -040022#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <linux/sunrpc/types.h>
26#include <linux/sunrpc/xdr.h>
27#include <linux/sunrpc/stats.h>
28#include <linux/sunrpc/svcsock.h>
29#include <linux/sunrpc/clnt.h>
Ricardo Labiaga4d6bbb62009-04-01 09:23:07 -040030#include <linux/sunrpc/bc_xprt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Jeff Layton860a0d92014-10-28 14:24:12 -040032#include <trace/events/sunrpc.h>
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define RPCDBG_FACILITY RPCDBG_SVCDSP
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +040036static void svc_unregister(const struct svc_serv *serv, struct net *net);
Chuck Lever7252d572008-08-18 19:34:08 -040037
Jeff Laytonc3690142015-06-08 12:04:46 -070038#define svc_serv_is_pooled(serv) ((serv)->sv_ops->svo_function)
Greg Banks42a7fc42007-03-06 01:42:23 -080039
Greg Banks42a7fc42007-03-06 01:42:23 -080040#define SVC_POOL_DEFAULT SVC_POOL_GLOBAL
Greg Banksbfd24162006-10-02 02:18:01 -070041
42/*
43 * Structure for mapping cpus to pools and vice versa.
44 * Setup once during sunrpc initialisation.
45 */
Jeff Laytond70bc0c2015-06-08 12:09:54 -070046struct svc_pool_map svc_pool_map = {
Greg Banks42a7fc42007-03-06 01:42:23 -080047 .mode = SVC_POOL_DEFAULT
Greg Banksbfd24162006-10-02 02:18:01 -070048};
Jeff Laytond70bc0c2015-06-08 12:09:54 -070049EXPORT_SYMBOL_GPL(svc_pool_map);
50
Greg Banks42a7fc42007-03-06 01:42:23 -080051static DEFINE_MUTEX(svc_pool_map_mutex);/* protects svc_pool_map.count only */
Greg Banksbfd24162006-10-02 02:18:01 -070052
Greg Banks42a7fc42007-03-06 01:42:23 -080053static int
Kees Cooke4dca7b2017-10-17 19:04:42 -070054param_set_pool_mode(const char *val, const struct kernel_param *kp)
Greg Banks42a7fc42007-03-06 01:42:23 -080055{
56 int *ip = (int *)kp->arg;
57 struct svc_pool_map *m = &svc_pool_map;
58 int err;
59
60 mutex_lock(&svc_pool_map_mutex);
61
62 err = -EBUSY;
63 if (m->count)
64 goto out;
65
66 err = 0;
67 if (!strncmp(val, "auto", 4))
68 *ip = SVC_POOL_AUTO;
69 else if (!strncmp(val, "global", 6))
70 *ip = SVC_POOL_GLOBAL;
71 else if (!strncmp(val, "percpu", 6))
72 *ip = SVC_POOL_PERCPU;
73 else if (!strncmp(val, "pernode", 7))
74 *ip = SVC_POOL_PERNODE;
75 else
76 err = -EINVAL;
77
78out:
79 mutex_unlock(&svc_pool_map_mutex);
80 return err;
81}
82
83static int
Kees Cooke4dca7b2017-10-17 19:04:42 -070084param_get_pool_mode(char *buf, const struct kernel_param *kp)
Greg Banks42a7fc42007-03-06 01:42:23 -080085{
86 int *ip = (int *)kp->arg;
87
88 switch (*ip)
89 {
90 case SVC_POOL_AUTO:
91 return strlcpy(buf, "auto", 20);
92 case SVC_POOL_GLOBAL:
93 return strlcpy(buf, "global", 20);
94 case SVC_POOL_PERCPU:
95 return strlcpy(buf, "percpu", 20);
96 case SVC_POOL_PERNODE:
97 return strlcpy(buf, "pernode", 20);
98 default:
99 return sprintf(buf, "%d", *ip);
100 }
101}
102
103module_param_call(pool_mode, param_set_pool_mode, param_get_pool_mode,
104 &svc_pool_map.mode, 0644);
Greg Banksbfd24162006-10-02 02:18:01 -0700105
106/*
107 * Detect best pool mapping mode heuristically,
108 * according to the machine's topology.
109 */
110static int
111svc_pool_map_choose_mode(void)
112{
113 unsigned int node;
114
Christoph Lameter62bc62a2009-06-16 15:32:15 -0700115 if (nr_online_nodes > 1) {
Greg Banksbfd24162006-10-02 02:18:01 -0700116 /*
117 * Actually have multiple NUMA nodes,
118 * so split pools on NUMA node boundaries
119 */
120 return SVC_POOL_PERNODE;
121 }
122
H Hartley Sweeten72c33682010-03-05 13:42:43 -0800123 node = first_online_node;
Greg Banksbfd24162006-10-02 02:18:01 -0700124 if (nr_cpus_node(node) > 2) {
125 /*
126 * Non-trivial SMP, or CONFIG_NUMA on
127 * non-NUMA hardware, e.g. with a generic
128 * x86_64 kernel on Xeons. In this case we
129 * want to divide the pools on cpu boundaries.
130 */
131 return SVC_POOL_PERCPU;
132 }
133
134 /* default: one global pool */
135 return SVC_POOL_GLOBAL;
136}
137
138/*
139 * Allocate the to_pool[] and pool_to[] arrays.
140 * Returns 0 on success or an errno.
141 */
142static int
143svc_pool_map_alloc_arrays(struct svc_pool_map *m, unsigned int maxpools)
144{
145 m->to_pool = kcalloc(maxpools, sizeof(unsigned int), GFP_KERNEL);
146 if (!m->to_pool)
147 goto fail;
148 m->pool_to = kcalloc(maxpools, sizeof(unsigned int), GFP_KERNEL);
149 if (!m->pool_to)
150 goto fail_free;
151
152 return 0;
153
154fail_free:
155 kfree(m->to_pool);
J. Bruce Fields61c85042011-12-22 18:22:49 -0700156 m->to_pool = NULL;
Greg Banksbfd24162006-10-02 02:18:01 -0700157fail:
158 return -ENOMEM;
159}
160
161/*
162 * Initialise the pool map for SVC_POOL_PERCPU mode.
163 * Returns number of pools or <0 on error.
164 */
165static int
166svc_pool_map_init_percpu(struct svc_pool_map *m)
167{
Christoph Lameter53b8a312007-02-20 13:57:51 -0800168 unsigned int maxpools = nr_cpu_ids;
Greg Banksbfd24162006-10-02 02:18:01 -0700169 unsigned int pidx = 0;
170 unsigned int cpu;
171 int err;
172
173 err = svc_pool_map_alloc_arrays(m, maxpools);
174 if (err)
175 return err;
176
177 for_each_online_cpu(cpu) {
Dan Carpentereb631922014-10-29 11:44:16 +0300178 BUG_ON(pidx >= maxpools);
Greg Banksbfd24162006-10-02 02:18:01 -0700179 m->to_pool[cpu] = pidx;
180 m->pool_to[pidx] = cpu;
181 pidx++;
182 }
183 /* cpus brought online later all get mapped to pool0, sorry */
184
185 return pidx;
186};
187
188
189/*
190 * Initialise the pool map for SVC_POOL_PERNODE mode.
191 * Returns number of pools or <0 on error.
192 */
193static int
194svc_pool_map_init_pernode(struct svc_pool_map *m)
195{
Christoph Lameter74c7aa82007-02-20 13:57:51 -0800196 unsigned int maxpools = nr_node_ids;
Greg Banksbfd24162006-10-02 02:18:01 -0700197 unsigned int pidx = 0;
198 unsigned int node;
199 int err;
200
201 err = svc_pool_map_alloc_arrays(m, maxpools);
202 if (err)
203 return err;
204
205 for_each_node_with_cpus(node) {
206 /* some architectures (e.g. SN2) have cpuless nodes */
207 BUG_ON(pidx > maxpools);
208 m->to_pool[node] = pidx;
209 m->pool_to[pidx] = node;
210 pidx++;
211 }
212 /* nodes brought online later all get mapped to pool0, sorry */
213
214 return pidx;
215}
216
217
218/*
Greg Banks42a7fc42007-03-06 01:42:23 -0800219 * Add a reference to the global map of cpus to pools (and
220 * vice versa). Initialise the map if we're the first user.
221 * Returns the number of pools.
Greg Banksbfd24162006-10-02 02:18:01 -0700222 */
Jeff Laytond70bc0c2015-06-08 12:09:54 -0700223unsigned int
Greg Banks42a7fc42007-03-06 01:42:23 -0800224svc_pool_map_get(void)
Greg Banksbfd24162006-10-02 02:18:01 -0700225{
226 struct svc_pool_map *m = &svc_pool_map;
227 int npools = -1;
228
Greg Banks42a7fc42007-03-06 01:42:23 -0800229 mutex_lock(&svc_pool_map_mutex);
Greg Banksbfd24162006-10-02 02:18:01 -0700230
Greg Banks42a7fc42007-03-06 01:42:23 -0800231 if (m->count++) {
232 mutex_unlock(&svc_pool_map_mutex);
233 return m->npools;
234 }
235
236 if (m->mode == SVC_POOL_AUTO)
237 m->mode = svc_pool_map_choose_mode();
Greg Banksbfd24162006-10-02 02:18:01 -0700238
239 switch (m->mode) {
240 case SVC_POOL_PERCPU:
241 npools = svc_pool_map_init_percpu(m);
242 break;
243 case SVC_POOL_PERNODE:
244 npools = svc_pool_map_init_pernode(m);
245 break;
246 }
247
248 if (npools < 0) {
249 /* default, or memory allocation failure */
250 npools = 1;
251 m->mode = SVC_POOL_GLOBAL;
252 }
253 m->npools = npools;
254
Greg Banks42a7fc42007-03-06 01:42:23 -0800255 mutex_unlock(&svc_pool_map_mutex);
Greg Banksbfd24162006-10-02 02:18:01 -0700256 return m->npools;
257}
Jeff Laytond70bc0c2015-06-08 12:09:54 -0700258EXPORT_SYMBOL_GPL(svc_pool_map_get);
Greg Banks42a7fc42007-03-06 01:42:23 -0800259
260/*
261 * Drop a reference to the global map of cpus to pools.
262 * When the last reference is dropped, the map data is
263 * freed; this allows the sysadmin to change the pool
264 * mode using the pool_mode module option without
265 * rebooting or re-loading sunrpc.ko.
266 */
Jeff Laytond70bc0c2015-06-08 12:09:54 -0700267void
Greg Banks42a7fc42007-03-06 01:42:23 -0800268svc_pool_map_put(void)
269{
270 struct svc_pool_map *m = &svc_pool_map;
271
272 mutex_lock(&svc_pool_map_mutex);
273
274 if (!--m->count) {
Greg Banks42a7fc42007-03-06 01:42:23 -0800275 kfree(m->to_pool);
J. Bruce Fields61c85042011-12-22 18:22:49 -0700276 m->to_pool = NULL;
Greg Banks42a7fc42007-03-06 01:42:23 -0800277 kfree(m->pool_to);
J. Bruce Fields61c85042011-12-22 18:22:49 -0700278 m->pool_to = NULL;
Greg Banks42a7fc42007-03-06 01:42:23 -0800279 m->npools = 0;
280 }
281
282 mutex_unlock(&svc_pool_map_mutex);
283}
Jeff Laytond70bc0c2015-06-08 12:09:54 -0700284EXPORT_SYMBOL_GPL(svc_pool_map_put);
Greg Banks42a7fc42007-03-06 01:42:23 -0800285
Eric Dumazet11fd1652011-07-28 20:04:09 +0200286static int svc_pool_map_get_node(unsigned int pidx)
287{
288 const struct svc_pool_map *m = &svc_pool_map;
289
290 if (m->count) {
291 if (m->mode == SVC_POOL_PERCPU)
292 return cpu_to_node(m->pool_to[pidx]);
293 if (m->mode == SVC_POOL_PERNODE)
294 return m->pool_to[pidx];
295 }
296 return NUMA_NO_NODE;
297}
Greg Banksbfd24162006-10-02 02:18:01 -0700298/*
Jeff Layton9867d762008-06-10 08:40:38 -0400299 * Set the given thread's cpus_allowed mask so that it
Greg Banksbfd24162006-10-02 02:18:01 -0700300 * will only run on cpus in the given pool.
Greg Banksbfd24162006-10-02 02:18:01 -0700301 */
Jeff Layton9867d762008-06-10 08:40:38 -0400302static inline void
303svc_pool_map_set_cpumask(struct task_struct *task, unsigned int pidx)
Greg Banksbfd24162006-10-02 02:18:01 -0700304{
305 struct svc_pool_map *m = &svc_pool_map;
Jeff Layton9867d762008-06-10 08:40:38 -0400306 unsigned int node = m->pool_to[pidx];
Greg Banksbfd24162006-10-02 02:18:01 -0700307
308 /*
309 * The caller checks for sv_nrpools > 1, which
Greg Banks42a7fc42007-03-06 01:42:23 -0800310 * implies that we've been initialized.
Greg Banksbfd24162006-10-02 02:18:01 -0700311 */
Weston Andros Adamson1bd58aa2012-10-23 10:43:38 -0400312 WARN_ON_ONCE(m->count == 0);
313 if (m->count == 0)
314 return;
Greg Banksbfd24162006-10-02 02:18:01 -0700315
Jeff Layton9867d762008-06-10 08:40:38 -0400316 switch (m->mode) {
Greg Banksbfd24162006-10-02 02:18:01 -0700317 case SVC_POOL_PERCPU:
Mike Travisc5f59f02008-04-04 18:11:10 -0700318 {
Rusty Russellaa85ea52009-03-30 22:05:15 -0600319 set_cpus_allowed_ptr(task, cpumask_of(node));
Jeff Layton9867d762008-06-10 08:40:38 -0400320 break;
Mike Travisc5f59f02008-04-04 18:11:10 -0700321 }
Greg Banksbfd24162006-10-02 02:18:01 -0700322 case SVC_POOL_PERNODE:
Mike Travisc5f59f02008-04-04 18:11:10 -0700323 {
Rusty Russella70f7302009-03-13 14:49:46 +1030324 set_cpus_allowed_ptr(task, cpumask_of_node(node));
Jeff Layton9867d762008-06-10 08:40:38 -0400325 break;
Greg Banksbfd24162006-10-02 02:18:01 -0700326 }
Mike Travisc5f59f02008-04-04 18:11:10 -0700327 }
Greg Banksbfd24162006-10-02 02:18:01 -0700328}
329
330/*
331 * Use the mapping mode to choose a pool for a given CPU.
332 * Used when enqueueing an incoming RPC. Always returns
333 * a non-NULL pool pointer.
334 */
335struct svc_pool *
336svc_pool_for_cpu(struct svc_serv *serv, int cpu)
337{
338 struct svc_pool_map *m = &svc_pool_map;
339 unsigned int pidx = 0;
340
341 /*
Greg Banks42a7fc42007-03-06 01:42:23 -0800342 * An uninitialised map happens in a pure client when
Greg Banksbfd24162006-10-02 02:18:01 -0700343 * lockd is brought up, so silently treat it the
344 * same as SVC_POOL_GLOBAL.
345 */
Greg Banks42a7fc42007-03-06 01:42:23 -0800346 if (svc_serv_is_pooled(serv)) {
347 switch (m->mode) {
348 case SVC_POOL_PERCPU:
349 pidx = m->to_pool[cpu];
350 break;
351 case SVC_POOL_PERNODE:
352 pidx = m->to_pool[cpu_to_node(cpu)];
353 break;
354 }
Greg Banksbfd24162006-10-02 02:18:01 -0700355 }
356 return &serv->sv_pools[pidx % serv->sv_nrpools];
357}
358
Stanislav Kinsburskybb2224d2012-01-31 15:08:05 +0400359int svc_rpcb_setup(struct svc_serv *serv, struct net *net)
Stanislav Kinsburskyd9908562011-10-25 14:16:58 +0300360{
361 int err;
362
Stanislav Kinsburskybee42f682012-01-13 14:02:56 +0400363 err = rpcb_create_local(net);
Stanislav Kinsburskyd9908562011-10-25 14:16:58 +0300364 if (err)
365 return err;
366
367 /* Remove any stale portmap registrations */
Stanislav Kinsburskybee42f682012-01-13 14:02:56 +0400368 svc_unregister(serv, net);
Stanislav Kinsburskyd9908562011-10-25 14:16:58 +0300369 return 0;
370}
Stanislav Kinsburskybb2224d2012-01-31 15:08:05 +0400371EXPORT_SYMBOL_GPL(svc_rpcb_setup);
Stanislav Kinsburskyd9908562011-10-25 14:16:58 +0300372
Stanislav Kinsbursky5ecebb72012-01-13 14:03:04 +0400373void svc_rpcb_cleanup(struct svc_serv *serv, struct net *net)
Stanislav Kinsburskyd9908562011-10-25 14:16:58 +0300374{
Stanislav Kinsbursky5ecebb72012-01-13 14:03:04 +0400375 svc_unregister(serv, net);
376 rpcb_put_local(net);
Stanislav Kinsburskyd9908562011-10-25 14:16:58 +0300377}
Stanislav Kinsbursky16d05872011-10-25 14:17:28 +0300378EXPORT_SYMBOL_GPL(svc_rpcb_cleanup);
Stanislav Kinsburskyd9908562011-10-25 14:16:58 +0300379
380static int svc_uses_rpcbind(struct svc_serv *serv)
381{
382 struct svc_program *progp;
383 unsigned int i;
384
385 for (progp = serv->sv_program; progp; progp = progp->pg_next) {
386 for (i = 0; i < progp->pg_nvers; i++) {
387 if (progp->pg_vers[i] == NULL)
388 continue;
Jeff Layton05a45a22017-02-24 13:25:22 -0500389 if (!progp->pg_vers[i]->vs_hidden)
Stanislav Kinsburskyd9908562011-10-25 14:16:58 +0300390 return 1;
391 }
392 }
393
394 return 0;
395}
Greg Banksbfd24162006-10-02 02:18:01 -0700396
Stanislav Kinsbursky9793f7c2012-05-02 16:08:38 +0400397int svc_bind(struct svc_serv *serv, struct net *net)
398{
399 if (!svc_uses_rpcbind(serv))
400 return 0;
401 return svc_rpcb_setup(serv, net);
402}
403EXPORT_SYMBOL_GPL(svc_bind);
404
Trond Myklebustd0025262016-08-29 20:03:49 -0400405#if defined(CONFIG_SUNRPC_BACKCHANNEL)
406static void
407__svc_init_bc(struct svc_serv *serv)
408{
409 INIT_LIST_HEAD(&serv->sv_cb_list);
410 spin_lock_init(&serv->sv_cb_lock);
411 init_waitqueue_head(&serv->sv_cb_waitq);
412}
413#else
414static void
415__svc_init_bc(struct svc_serv *serv)
416{
417}
418#endif
419
Greg Banksbfd24162006-10-02 02:18:01 -0700420/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 * Create an RPC service
422 */
Greg Banksa7455442006-10-02 02:17:59 -0700423static struct svc_serv *
424__svc_create(struct svc_program *prog, unsigned int bufsize, int npools,
Chuck Leverafea5652017-08-01 12:00:06 -0400425 const struct svc_serv_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 struct svc_serv *serv;
Chuck Leverea339d42007-10-26 13:32:56 -0400428 unsigned int vers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 unsigned int xdrsize;
Greg Banks3262c812006-10-02 02:17:58 -0700430 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700432 if (!(serv = kzalloc(sizeof(*serv), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return NULL;
Andreas Gruenbacher9ba02632005-06-22 17:16:24 +0000434 serv->sv_name = prog->pg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 serv->sv_program = prog;
436 serv->sv_nrthreads = 1;
437 serv->sv_stats = prog->pg_stats;
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700438 if (bufsize > RPCSVC_MAXPAYLOAD)
439 bufsize = RPCSVC_MAXPAYLOAD;
440 serv->sv_max_payload = bufsize? bufsize : 4096;
441 serv->sv_max_mesg = roundup(serv->sv_max_payload + PAGE_SIZE, PAGE_SIZE);
Jeff Laytonea126e72015-06-08 12:03:32 -0700442 serv->sv_ops = ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 xdrsize = 0;
Andreas Gruenbacher9ba02632005-06-22 17:16:24 +0000444 while (prog) {
445 prog->pg_lovers = prog->pg_nvers-1;
446 for (vers=0; vers<prog->pg_nvers ; vers++)
447 if (prog->pg_vers[vers]) {
448 prog->pg_hivers = vers;
449 if (prog->pg_lovers > vers)
450 prog->pg_lovers = vers;
451 if (prog->pg_vers[vers]->vs_xdrsize > xdrsize)
452 xdrsize = prog->pg_vers[vers]->vs_xdrsize;
453 }
454 prog = prog->pg_next;
455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 serv->sv_xdrsize = xdrsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 INIT_LIST_HEAD(&serv->sv_tempsocks);
458 INIT_LIST_HEAD(&serv->sv_permsocks);
Kees Cookff861c42017-10-16 17:29:42 -0700459 timer_setup(&serv->sv_temptimer, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 spin_lock_init(&serv->sv_lock);
461
Trond Myklebustd0025262016-08-29 20:03:49 -0400462 __svc_init_bc(serv);
463
Greg Banksa7455442006-10-02 02:17:59 -0700464 serv->sv_nrpools = npools;
Greg Banks3262c812006-10-02 02:17:58 -0700465 serv->sv_pools =
Robert P. J. Daycd861282006-12-13 00:34:52 -0800466 kcalloc(serv->sv_nrpools, sizeof(struct svc_pool),
Greg Banks3262c812006-10-02 02:17:58 -0700467 GFP_KERNEL);
468 if (!serv->sv_pools) {
469 kfree(serv);
470 return NULL;
471 }
472
473 for (i = 0; i < serv->sv_nrpools; i++) {
474 struct svc_pool *pool = &serv->sv_pools[i];
475
Chuck Lever46121cf2007-01-31 12:14:08 -0500476 dprintk("svc: initialising pool %u for %s\n",
Greg Banks3262c812006-10-02 02:17:58 -0700477 i, serv->sv_name);
478
479 pool->sp_id = i;
Greg Banks3262c812006-10-02 02:17:58 -0700480 INIT_LIST_HEAD(&pool->sp_sockets);
Greg Banksa7455442006-10-02 02:17:59 -0700481 INIT_LIST_HEAD(&pool->sp_all_threads);
Greg Banks3262c812006-10-02 02:17:58 -0700482 spin_lock_init(&pool->sp_lock);
483 }
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return serv;
486}
487
Greg Banksa7455442006-10-02 02:17:59 -0700488struct svc_serv *
489svc_create(struct svc_program *prog, unsigned int bufsize,
Chuck Leverafea5652017-08-01 12:00:06 -0400490 const struct svc_serv_ops *ops)
Greg Banksa7455442006-10-02 02:17:59 -0700491{
Jeff Laytonea126e72015-06-08 12:03:32 -0700492 return __svc_create(prog, bufsize, /*npools*/1, ops);
Greg Banksa7455442006-10-02 02:17:59 -0700493}
Trond Myklebust24c37672008-12-23 16:30:12 -0500494EXPORT_SYMBOL_GPL(svc_create);
Greg Banksa7455442006-10-02 02:17:59 -0700495
496struct svc_serv *
497svc_create_pooled(struct svc_program *prog, unsigned int bufsize,
Chuck Leverafea5652017-08-01 12:00:06 -0400498 const struct svc_serv_ops *ops)
Greg Banksa7455442006-10-02 02:17:59 -0700499{
500 struct svc_serv *serv;
Greg Banks42a7fc42007-03-06 01:42:23 -0800501 unsigned int npools = svc_pool_map_get();
Greg Banksa7455442006-10-02 02:17:59 -0700502
Jeff Laytonea126e72015-06-08 12:03:32 -0700503 serv = __svc_create(prog, bufsize, npools, ops);
Jeff Layton067f96e2014-11-19 07:51:13 -0500504 if (!serv)
505 goto out_err;
Greg Banksa7455442006-10-02 02:17:59 -0700506 return serv;
Jeff Layton067f96e2014-11-19 07:51:13 -0500507out_err:
508 svc_pool_map_put();
509 return NULL;
Greg Banksa7455442006-10-02 02:17:59 -0700510}
Trond Myklebust24c37672008-12-23 16:30:12 -0500511EXPORT_SYMBOL_GPL(svc_create_pooled);
Greg Banksa7455442006-10-02 02:17:59 -0700512
Stanislav Kinsbursky074d0f62012-01-31 14:09:25 +0400513void svc_shutdown_net(struct svc_serv *serv, struct net *net)
514{
Stanislav Kinsbursky074d0f62012-01-31 14:09:25 +0400515 svc_close_net(serv, net);
516
Jeff Laytonea126e72015-06-08 12:03:32 -0700517 if (serv->sv_ops->svo_shutdown)
518 serv->sv_ops->svo_shutdown(serv, net);
Stanislav Kinsbursky074d0f62012-01-31 14:09:25 +0400519}
520EXPORT_SYMBOL_GPL(svc_shutdown_net);
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/*
Neil Brownbedbdd82008-06-10 08:40:35 -0400523 * Destroy an RPC service. Should be called with appropriate locking to
524 * protect the sv_nrthreads, sv_permsocks and sv_tempsocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 */
526void
527svc_destroy(struct svc_serv *serv)
528{
Chuck Lever46121cf2007-01-31 12:14:08 -0500529 dprintk("svc: svc_destroy(%s, %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 serv->sv_program->pg_name,
531 serv->sv_nrthreads);
532
533 if (serv->sv_nrthreads) {
534 if (--(serv->sv_nrthreads) != 0) {
535 svc_sock_update_bufs(serv);
536 return;
537 }
538 } else
539 printk("svc_destroy: no threads for serv=%p!\n", serv);
540
Greg Banks36bdfc82006-10-02 02:17:54 -0700541 del_timer_sync(&serv->sv_temptimer);
Stanislav Kinsbursky074d0f62012-01-31 14:09:25 +0400542
Stanislav Kinsbursky7b147f12012-01-31 14:09:17 +0400543 /*
544 * The last user is gone and thus all sockets have to be destroyed to
545 * the point. Check this.
546 */
547 BUG_ON(!list_empty(&serv->sv_permsocks));
548 BUG_ON(!list_empty(&serv->sv_tempsocks));
NeilBrowncda1fd42007-03-06 01:42:22 -0800549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 cache_clean_deferred(serv);
551
Greg Banks42a7fc42007-03-06 01:42:23 -0800552 if (svc_serv_is_pooled(serv))
553 svc_pool_map_put();
554
Greg Banks3262c812006-10-02 02:17:58 -0700555 kfree(serv->sv_pools);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 kfree(serv);
557}
Trond Myklebust24c37672008-12-23 16:30:12 -0500558EXPORT_SYMBOL_GPL(svc_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560/*
561 * Allocate an RPC server's buffer space.
562 * We allocate pages and place them in rq_argpages.
563 */
564static int
Eric Dumazet11fd1652011-07-28 20:04:09 +0200565svc_init_buffer(struct svc_rqst *rqstp, unsigned int size, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Chuck Lever0dc220f2008-04-14 12:27:52 -0400567 unsigned int pages, arghi;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800568
Andy Adamsonba176862010-01-26 21:24:04 -0500569 /* bc_xprt uses fore channel allocated buffers */
570 if (svc_is_backchannel(rqstp))
571 return 1;
572
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700573 pages = size / PAGE_SIZE + 1; /* extra page as we hold both request and reply.
574 * We assume one is at most one page
575 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 arghi = 0;
Weston Andros Adamsonb25cd052012-10-23 10:43:41 -0400577 WARN_ON_ONCE(pages > RPCSVC_MAXPAGES);
578 if (pages > RPCSVC_MAXPAGES)
579 pages = RPCSVC_MAXPAGES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 while (pages) {
Eric Dumazet11fd1652011-07-28 20:04:09 +0200581 struct page *p = alloc_pages_node(node, GFP_KERNEL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (!p)
583 break;
NeilBrown44524352006-10-04 02:15:46 -0700584 rqstp->rq_pages[arghi++] = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 pages--;
586 }
Chuck Lever0dc220f2008-04-14 12:27:52 -0400587 return pages == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
590/*
591 * Release an RPC server buffer
592 */
593static void
594svc_release_buffer(struct svc_rqst *rqstp)
595{
Chuck Lever50c8bb12008-04-14 12:27:45 -0400596 unsigned int i;
597
598 for (i = 0; i < ARRAY_SIZE(rqstp->rq_pages); i++)
NeilBrown44524352006-10-04 02:15:46 -0700599 if (rqstp->rq_pages[i])
600 put_page(rqstp->rq_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Jeff Layton0113ab32008-01-29 10:30:54 -0500603struct svc_rqst *
Jeff Layton1b6dc1d2015-06-08 12:11:10 -0700604svc_rqst_alloc(struct svc_serv *serv, struct svc_pool *pool, int node)
Jeff Layton0113ab32008-01-29 10:30:54 -0500605{
606 struct svc_rqst *rqstp;
607
Eric Dumazet11fd1652011-07-28 20:04:09 +0200608 rqstp = kzalloc_node(sizeof(*rqstp), GFP_KERNEL, node);
Jeff Layton0113ab32008-01-29 10:30:54 -0500609 if (!rqstp)
Jeff Layton1b6dc1d2015-06-08 12:11:10 -0700610 return rqstp;
Jeff Layton0113ab32008-01-29 10:30:54 -0500611
Jeff Laytonb1691bc2014-11-21 14:19:30 -0500612 __set_bit(RQ_BUSY, &rqstp->rq_flags);
613 spin_lock_init(&rqstp->rq_lock);
614 rqstp->rq_server = serv;
615 rqstp->rq_pool = pool;
Jeff Layton1b6dc1d2015-06-08 12:11:10 -0700616
617 rqstp->rq_argp = kmalloc_node(serv->sv_xdrsize, GFP_KERNEL, node);
618 if (!rqstp->rq_argp)
619 goto out_enomem;
620
621 rqstp->rq_resp = kmalloc_node(serv->sv_xdrsize, GFP_KERNEL, node);
622 if (!rqstp->rq_resp)
623 goto out_enomem;
624
625 if (!svc_init_buffer(rqstp, serv->sv_max_mesg, node))
626 goto out_enomem;
627
628 return rqstp;
629out_enomem:
630 svc_rqst_free(rqstp);
631 return NULL;
632}
633EXPORT_SYMBOL_GPL(svc_rqst_alloc);
634
635struct svc_rqst *
636svc_prepare_thread(struct svc_serv *serv, struct svc_pool *pool, int node)
637{
638 struct svc_rqst *rqstp;
639
640 rqstp = svc_rqst_alloc(serv, pool, node);
641 if (!rqstp)
642 return ERR_PTR(-ENOMEM);
643
644 serv->sv_nrthreads++;
Jeff Layton0113ab32008-01-29 10:30:54 -0500645 spin_lock_bh(&pool->sp_lock);
646 pool->sp_nrthreads++;
Jeff Layton81244382014-11-21 14:19:28 -0500647 list_add_rcu(&rqstp->rq_all, &pool->sp_all_threads);
Jeff Layton0113ab32008-01-29 10:30:54 -0500648 spin_unlock_bh(&pool->sp_lock);
Jeff Layton0113ab32008-01-29 10:30:54 -0500649 return rqstp;
Jeff Layton0113ab32008-01-29 10:30:54 -0500650}
Trond Myklebust24c37672008-12-23 16:30:12 -0500651EXPORT_SYMBOL_GPL(svc_prepare_thread);
Jeff Layton0113ab32008-01-29 10:30:54 -0500652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653/*
Greg Banksa7455442006-10-02 02:17:59 -0700654 * Choose a pool in which to create a new thread, for svc_set_num_threads
655 */
656static inline struct svc_pool *
657choose_pool(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state)
658{
659 if (pool != NULL)
660 return pool;
661
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800662 return &serv->sv_pools[(*state)++ % serv->sv_nrpools];
Greg Banksa7455442006-10-02 02:17:59 -0700663}
664
665/*
666 * Choose a thread to kill, for svc_set_num_threads
667 */
668static inline struct task_struct *
669choose_victim(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state)
670{
671 unsigned int i;
672 struct task_struct *task = NULL;
673
674 if (pool != NULL) {
675 spin_lock_bh(&pool->sp_lock);
676 } else {
677 /* choose a pool in round-robin fashion */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800678 for (i = 0; i < serv->sv_nrpools; i++) {
679 pool = &serv->sv_pools[--(*state) % serv->sv_nrpools];
Greg Banksa7455442006-10-02 02:17:59 -0700680 spin_lock_bh(&pool->sp_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800681 if (!list_empty(&pool->sp_all_threads))
682 goto found_pool;
Greg Banksa7455442006-10-02 02:17:59 -0700683 spin_unlock_bh(&pool->sp_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800684 }
Greg Banksa7455442006-10-02 02:17:59 -0700685 return NULL;
686 }
687
688found_pool:
689 if (!list_empty(&pool->sp_all_threads)) {
690 struct svc_rqst *rqstp;
691
692 /*
693 * Remove from the pool->sp_all_threads list
694 * so we don't try to kill it again.
695 */
696 rqstp = list_entry(pool->sp_all_threads.next, struct svc_rqst, rq_all);
Jeff Layton81244382014-11-21 14:19:28 -0500697 set_bit(RQ_VICTIM, &rqstp->rq_flags);
698 list_del_rcu(&rqstp->rq_all);
Greg Banksa7455442006-10-02 02:17:59 -0700699 task = rqstp->rq_task;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800700 }
Greg Banksa7455442006-10-02 02:17:59 -0700701 spin_unlock_bh(&pool->sp_lock);
702
703 return task;
704}
705
Trond Myklebust9e0d8762017-04-26 11:55:26 -0400706/* create new threads */
707static int
708svc_start_kthreads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
709{
710 struct svc_rqst *rqstp;
711 struct task_struct *task;
712 struct svc_pool *chosen_pool;
713 unsigned int state = serv->sv_nrthreads-1;
714 int node;
715
716 do {
717 nrservs--;
718 chosen_pool = choose_pool(serv, pool, &state);
719
720 node = svc_pool_map_get_node(chosen_pool->sp_id);
721 rqstp = svc_prepare_thread(serv, chosen_pool, node);
722 if (IS_ERR(rqstp))
723 return PTR_ERR(rqstp);
724
725 __module_get(serv->sv_ops->svo_module);
726 task = kthread_create_on_node(serv->sv_ops->svo_function, rqstp,
727 node, "%s", serv->sv_name);
728 if (IS_ERR(task)) {
729 module_put(serv->sv_ops->svo_module);
730 svc_exit_thread(rqstp);
731 return PTR_ERR(task);
732 }
733
734 rqstp->rq_task = task;
735 if (serv->sv_nrpools > 1)
736 svc_pool_map_set_cpumask(task, chosen_pool->sp_id);
737
738 svc_sock_update_bufs(serv);
739 wake_up_process(task);
740 } while (nrservs > 0);
741
742 return 0;
743}
744
745
746/* destroy old threads */
747static int
748svc_signal_kthreads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
749{
750 struct task_struct *task;
751 unsigned int state = serv->sv_nrthreads-1;
752
753 /* destroy old threads */
754 do {
755 task = choose_victim(serv, pool, &state);
756 if (task == NULL)
757 break;
758 send_sig(SIGINT, task, 1);
759 nrservs++;
760 } while (nrservs < 0);
761
762 return 0;
763}
764
Greg Banksa7455442006-10-02 02:17:59 -0700765/*
766 * Create or destroy enough new threads to make the number
767 * of threads the given number. If `pool' is non-NULL, applies
768 * only to threads in that pool, otherwise round-robins between
J. Bruce Fields94cf3172011-12-01 17:51:21 -0500769 * all pools. Caller must ensure that mutual exclusion between this and
770 * server startup or shutdown.
Greg Banksa7455442006-10-02 02:17:59 -0700771 *
772 * Destroying threads relies on the service threads filling in
773 * rqstp->rq_task, which only the nfs ones do. Assumes the serv
774 * has been created using svc_create_pooled().
775 *
776 * Based on code that used to be in nfsd_svc() but tweaked
777 * to be pool-aware.
778 */
779int
780svc_set_num_threads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
781{
Greg Banksa7455442006-10-02 02:17:59 -0700782 if (pool == NULL) {
783 /* The -1 assumes caller has done a svc_get() */
784 nrservs -= (serv->sv_nrthreads-1);
785 } else {
786 spin_lock_bh(&pool->sp_lock);
787 nrservs -= pool->sp_nrthreads;
788 spin_unlock_bh(&pool->sp_lock);
789 }
790
Trond Myklebust9e0d8762017-04-26 11:55:26 -0400791 if (nrservs > 0)
792 return svc_start_kthreads(serv, pool, nrservs);
793 if (nrservs < 0)
794 return svc_signal_kthreads(serv, pool, nrservs);
795 return 0;
Greg Banksa7455442006-10-02 02:17:59 -0700796}
Trond Myklebust24c37672008-12-23 16:30:12 -0500797EXPORT_SYMBOL_GPL(svc_set_num_threads);
Greg Banksa7455442006-10-02 02:17:59 -0700798
Trond Myklebusted6473d2017-04-26 11:55:27 -0400799/* destroy old threads */
800static int
801svc_stop_kthreads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
802{
803 struct task_struct *task;
804 unsigned int state = serv->sv_nrthreads-1;
805
806 /* destroy old threads */
807 do {
808 task = choose_victim(serv, pool, &state);
809 if (task == NULL)
810 break;
811 kthread_stop(task);
812 nrservs++;
813 } while (nrservs < 0);
814 return 0;
815}
816
817int
818svc_set_num_threads_sync(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
819{
820 if (pool == NULL) {
821 /* The -1 assumes caller has done a svc_get() */
822 nrservs -= (serv->sv_nrthreads-1);
823 } else {
824 spin_lock_bh(&pool->sp_lock);
825 nrservs -= pool->sp_nrthreads;
826 spin_unlock_bh(&pool->sp_lock);
827 }
828
829 if (nrservs > 0)
830 return svc_start_kthreads(serv, pool, nrservs);
831 if (nrservs < 0)
832 return svc_stop_kthreads(serv, pool, nrservs);
833 return 0;
834}
835EXPORT_SYMBOL_GPL(svc_set_num_threads_sync);
836
Greg Banksa7455442006-10-02 02:17:59 -0700837/*
Jeff Layton3c519912015-01-22 08:19:32 -0500838 * Called from a server thread as it's exiting. Caller must hold the "service
839 * mutex" for the service.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 */
841void
Jeff Layton1b6dc1d2015-06-08 12:11:10 -0700842svc_rqst_free(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 svc_release_buffer(rqstp);
Jesper Juhla51482b2005-11-08 09:41:34 -0800845 kfree(rqstp->rq_resp);
846 kfree(rqstp->rq_argp);
847 kfree(rqstp->rq_auth_data);
Jeff Layton1b6dc1d2015-06-08 12:11:10 -0700848 kfree_rcu(rqstp, rq_rcu_head);
849}
850EXPORT_SYMBOL_GPL(svc_rqst_free);
851
852void
853svc_exit_thread(struct svc_rqst *rqstp)
854{
855 struct svc_serv *serv = rqstp->rq_server;
856 struct svc_pool *pool = rqstp->rq_pool;
Greg Banks3262c812006-10-02 02:17:58 -0700857
858 spin_lock_bh(&pool->sp_lock);
859 pool->sp_nrthreads--;
Jeff Layton81244382014-11-21 14:19:28 -0500860 if (!test_and_set_bit(RQ_VICTIM, &rqstp->rq_flags))
861 list_del_rcu(&rqstp->rq_all);
Greg Banks3262c812006-10-02 02:17:58 -0700862 spin_unlock_bh(&pool->sp_lock);
863
Jeff Layton1b6dc1d2015-06-08 12:11:10 -0700864 svc_rqst_free(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 /* Release the server */
867 if (serv)
868 svc_destroy(serv);
869}
Trond Myklebust24c37672008-12-23 16:30:12 -0500870EXPORT_SYMBOL_GPL(svc_exit_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872/*
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500873 * Register an "inet" protocol family netid with the local
874 * rpcbind daemon via an rpcbind v4 SET request.
Chuck Levera26cfad2008-08-18 19:34:16 -0400875 *
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500876 * No netconfig infrastructure is available in the kernel, so
877 * we map IP_ protocol numbers to netids by hand.
Chuck Levera26cfad2008-08-18 19:34:16 -0400878 *
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500879 * Returns zero on success; a negative errno value is returned
880 * if any error occurs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 */
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +0400882static int __svc_rpcb_register4(struct net *net, const u32 program,
883 const u32 version,
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500884 const unsigned short protocol,
885 const unsigned short port)
Chuck Levera26cfad2008-08-18 19:34:16 -0400886{
Chuck Levercadc0fa2009-03-18 20:47:36 -0400887 const struct sockaddr_in sin = {
Chuck Levera26cfad2008-08-18 19:34:16 -0400888 .sin_family = AF_INET,
889 .sin_addr.s_addr = htonl(INADDR_ANY),
890 .sin_port = htons(port),
891 };
Chuck Levercadc0fa2009-03-18 20:47:36 -0400892 const char *netid;
893 int error;
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500894
895 switch (protocol) {
896 case IPPROTO_UDP:
897 netid = RPCBIND_NETID_UDP;
898 break;
899 case IPPROTO_TCP:
900 netid = RPCBIND_NETID_TCP;
901 break;
902 default:
Chuck Leverba5c35e2009-03-18 20:46:59 -0400903 return -ENOPROTOOPT;
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500904 }
905
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +0400906 error = rpcb_v4_register(net, program, version,
Chuck Levercadc0fa2009-03-18 20:47:36 -0400907 (const struct sockaddr *)&sin, netid);
908
909 /*
910 * User space didn't support rpcbind v4, so retry this
911 * registration request with the legacy rpcbind v2 protocol.
912 */
913 if (error == -EPROTONOSUPPORT)
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +0400914 error = rpcb_register(net, program, version, protocol, port);
Chuck Levercadc0fa2009-03-18 20:47:36 -0400915
916 return error;
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500917}
918
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000919#if IS_ENABLED(CONFIG_IPV6)
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500920/*
921 * Register an "inet6" protocol family netid with the local
922 * rpcbind daemon via an rpcbind v4 SET request.
923 *
924 * No netconfig infrastructure is available in the kernel, so
925 * we map IP_ protocol numbers to netids by hand.
926 *
927 * Returns zero on success; a negative errno value is returned
928 * if any error occurs.
929 */
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +0400930static int __svc_rpcb_register6(struct net *net, const u32 program,
931 const u32 version,
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500932 const unsigned short protocol,
933 const unsigned short port)
934{
Chuck Levercadc0fa2009-03-18 20:47:36 -0400935 const struct sockaddr_in6 sin6 = {
Chuck Levera26cfad2008-08-18 19:34:16 -0400936 .sin6_family = AF_INET6,
937 .sin6_addr = IN6ADDR_ANY_INIT,
938 .sin6_port = htons(port),
939 };
Chuck Levercadc0fa2009-03-18 20:47:36 -0400940 const char *netid;
941 int error;
Chuck Levera26cfad2008-08-18 19:34:16 -0400942
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500943 switch (protocol) {
944 case IPPROTO_UDP:
945 netid = RPCBIND_NETID_UDP6;
946 break;
947 case IPPROTO_TCP:
948 netid = RPCBIND_NETID_TCP6;
949 break;
950 default:
Chuck Leverba5c35e2009-03-18 20:46:59 -0400951 return -ENOPROTOOPT;
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500952 }
953
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +0400954 error = rpcb_v4_register(net, program, version,
Chuck Levercadc0fa2009-03-18 20:47:36 -0400955 (const struct sockaddr *)&sin6, netid);
956
957 /*
958 * User space didn't support rpcbind version 4, so we won't
959 * use a PF_INET6 listener.
960 */
961 if (error == -EPROTONOSUPPORT)
962 error = -EAFNOSUPPORT;
963
964 return error;
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500965}
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000966#endif /* IS_ENABLED(CONFIG_IPV6) */
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500967
968/*
969 * Register a kernel RPC service via rpcbind version 4.
970 *
971 * Returns zero on success; a negative errno value is returned
972 * if any error occurs.
973 */
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +0400974static int __svc_register(struct net *net, const char *progname,
Chuck Lever363f7242009-03-18 20:47:44 -0400975 const u32 program, const u32 version,
Chuck Lever4b62e582009-03-18 20:46:06 -0400976 const int family,
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500977 const unsigned short protocol,
978 const unsigned short port)
979{
Chuck Lever363f7242009-03-18 20:47:44 -0400980 int error = -EAFNOSUPPORT;
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500981
Chuck Levera26cfad2008-08-18 19:34:16 -0400982 switch (family) {
Chuck Lever4b62e582009-03-18 20:46:06 -0400983 case PF_INET:
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +0400984 error = __svc_rpcb_register4(net, program, version,
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500985 protocol, port);
Chuck Levercadc0fa2009-03-18 20:47:36 -0400986 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000987#if IS_ENABLED(CONFIG_IPV6)
Chuck Lever4b62e582009-03-18 20:46:06 -0400988 case PF_INET6:
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +0400989 error = __svc_rpcb_register6(net, program, version,
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500990 protocol, port);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000991#endif
Chuck Levera26cfad2008-08-18 19:34:16 -0400992 }
993
Chuck Lever363f7242009-03-18 20:47:44 -0400994 return error;
Chuck Levera26cfad2008-08-18 19:34:16 -0400995}
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500996
Trond Myklebust642ee6b2019-04-09 11:46:17 -0400997int svc_rpcbind_set_version(struct net *net,
998 const struct svc_program *progp,
999 u32 version, int family,
1000 unsigned short proto,
1001 unsigned short port)
1002{
1003 dprintk("svc: svc_register(%sv%d, %s, %u, %u)\n",
1004 progp->pg_name, version,
1005 proto == IPPROTO_UDP? "udp" : "tcp",
1006 port, family);
1007
1008 return __svc_register(net, progp->pg_name, progp->pg_prog,
1009 version, family, proto, port);
1010
1011}
1012EXPORT_SYMBOL_GPL(svc_rpcbind_set_version);
1013
1014int svc_generic_rpcbind_set(struct net *net,
1015 const struct svc_program *progp,
1016 u32 version, int family,
1017 unsigned short proto,
1018 unsigned short port)
1019{
1020 const struct svc_version *vers = progp->pg_vers[version];
1021 int error;
1022
1023 if (vers == NULL)
1024 return 0;
1025
1026 if (vers->vs_hidden) {
1027 dprintk("svc: svc_register(%sv%d, %s, %u, %u)"
1028 " (but not telling portmap)\n",
1029 progp->pg_name, version,
1030 proto == IPPROTO_UDP? "udp" : "tcp",
1031 port, family);
1032 return 0;
1033 }
1034
1035 /*
1036 * Don't register a UDP port if we need congestion
1037 * control.
1038 */
1039 if (vers->vs_need_cong_ctrl && proto == IPPROTO_UDP)
1040 return 0;
1041
1042 error = svc_rpcbind_set_version(net, progp, version,
1043 family, proto, port);
1044
1045 return (vers->vs_rpcb_optnl) ? 0 : error;
1046}
1047EXPORT_SYMBOL_GPL(svc_generic_rpcbind_set);
1048
Chuck Levera26cfad2008-08-18 19:34:16 -04001049/**
1050 * svc_register - register an RPC service with the local portmapper
1051 * @serv: svc_serv struct for the service to register
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001052 * @net: net namespace for the service to register
Chuck Lever4b62e582009-03-18 20:46:06 -04001053 * @family: protocol family of service's listener socket
Chuck Levera26cfad2008-08-18 19:34:16 -04001054 * @proto: transport protocol number to advertise
1055 * @port: port to advertise
1056 *
Chuck Lever4b62e582009-03-18 20:46:06 -04001057 * Service is registered for any address in the passed-in protocol family
Chuck Levera26cfad2008-08-18 19:34:16 -04001058 */
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001059int svc_register(const struct svc_serv *serv, struct net *net,
1060 const int family, const unsigned short proto,
1061 const unsigned short port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
1063 struct svc_program *progp;
Chuck Leverea339d42007-10-26 13:32:56 -04001064 unsigned int i;
Chuck Lever14aeb212008-08-18 19:34:00 -04001065 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Weston Andros Adamson0af39502012-10-23 10:43:45 -04001067 WARN_ON_ONCE(proto == 0 && port == 0);
1068 if (proto == 0 && port == 0)
1069 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Olaf Kirchbc5fea42006-10-04 02:16:05 -07001071 for (progp = serv->sv_program; progp; progp = progp->pg_next) {
1072 for (i = 0; i < progp->pg_nvers; i++) {
Olaf Kirchbc5fea42006-10-04 02:16:05 -07001073
Trond Myklebust642ee6b2019-04-09 11:46:17 -04001074 error = progp->pg_rpcbind_set(net, progp, i,
1075 family, proto, port);
Kinglong Mee7e55b592013-12-31 13:17:20 +08001076 if (error < 0) {
1077 printk(KERN_WARNING "svc: failed to register "
1078 "%sv%u RPC service (errno %d).\n",
1079 progp->pg_name, i, -error);
Olaf Kirchbc5fea42006-10-04 02:16:05 -07001080 break;
Kinglong Mee7e55b592013-12-31 13:17:20 +08001081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083 }
1084
Chuck Lever7252d572008-08-18 19:34:08 -04001085 return error;
1086}
1087
Chuck Leverd5a86202009-03-18 20:47:29 -04001088/*
1089 * If user space is running rpcbind, it should take the v4 UNSET
1090 * and clear everything for this [program, version]. If user space
1091 * is running portmap, it will reject the v4 UNSET, but won't have
1092 * any "inet6" entries anyway. So a PMAP_UNSET should be sufficient
1093 * in this case to clear all existing entries for [program, version].
1094 */
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001095static void __svc_unregister(struct net *net, const u32 program, const u32 version,
Chuck Leverf6fb3f62008-09-25 11:56:57 -04001096 const char *progname)
1097{
1098 int error;
1099
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001100 error = rpcb_v4_register(net, program, version, NULL, "");
Chuck Leverd5a86202009-03-18 20:47:29 -04001101
1102 /*
1103 * User space didn't support rpcbind v4, so retry this
1104 * request with the legacy rpcbind v2 protocol.
1105 */
1106 if (error == -EPROTONOSUPPORT)
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001107 error = rpcb_register(net, program, version, 0, 0);
Chuck Leverd5a86202009-03-18 20:47:29 -04001108
Chuck Leverf6fb3f62008-09-25 11:56:57 -04001109 dprintk("svc: %s(%sv%u), error %d\n",
1110 __func__, progname, version, error);
1111}
1112
Chuck Lever7252d572008-08-18 19:34:08 -04001113/*
Chuck Leverf6fb3f62008-09-25 11:56:57 -04001114 * All netids, bind addresses and ports registered for [program, version]
1115 * are removed from the local rpcbind database (if the service is not
1116 * hidden) to make way for a new instance of the service.
Chuck Lever7252d572008-08-18 19:34:08 -04001117 *
Chuck Leverf6fb3f62008-09-25 11:56:57 -04001118 * The result of unregistration is reported via dprintk for those who want
1119 * verification of the result, but is otherwise not important.
Chuck Lever7252d572008-08-18 19:34:08 -04001120 */
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001121static void svc_unregister(const struct svc_serv *serv, struct net *net)
Chuck Lever7252d572008-08-18 19:34:08 -04001122{
1123 struct svc_program *progp;
1124 unsigned long flags;
1125 unsigned int i;
Chuck Lever7252d572008-08-18 19:34:08 -04001126
1127 clear_thread_flag(TIF_SIGPENDING);
1128
1129 for (progp = serv->sv_program; progp; progp = progp->pg_next) {
1130 for (i = 0; i < progp->pg_nvers; i++) {
1131 if (progp->pg_vers[i] == NULL)
1132 continue;
1133 if (progp->pg_vers[i]->vs_hidden)
1134 continue;
1135
Chuck Lever7402ab192011-05-09 15:22:55 -04001136 dprintk("svc: attempting to unregister %sv%u\n",
1137 progp->pg_name, i);
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001138 __svc_unregister(net, progp->pg_prog, i, progp->pg_name);
Chuck Lever7252d572008-08-18 19:34:08 -04001139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141
Chuck Lever7252d572008-08-18 19:34:08 -04001142 spin_lock_irqsave(&current->sighand->siglock, flags);
1143 recalc_sigpending();
1144 spin_unlock_irqrestore(&current->sighand->siglock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
1146
1147/*
J. Bruce Fields7032a3d2012-10-09 18:33:38 -04001148 * dprintk the given error with the address of the client that caused it.
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001149 */
Jeff Laytonf895b252014-11-17 16:58:04 -05001150#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Joe Perchesb9075fa2011-10-31 17:11:33 -07001151static __printf(2, 3)
Joe Perchese87cc472012-05-13 21:56:26 +00001152void svc_printk(struct svc_rqst *rqstp, const char *fmt, ...)
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001153{
Joe Perchese87cc472012-05-13 21:56:26 +00001154 struct va_format vaf;
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001155 va_list args;
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001156 char buf[RPC_MAX_ADDRBUFLEN];
1157
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001158 va_start(args, fmt);
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001159
Joe Perchese87cc472012-05-13 21:56:26 +00001160 vaf.fmt = fmt;
1161 vaf.va = &args;
1162
J. Bruce Fields7032a3d2012-10-09 18:33:38 -04001163 dprintk("svc: %s: %pV", svc_print_addr(rqstp, buf, sizeof(buf)), &vaf);
Joe Perchese87cc472012-05-13 21:56:26 +00001164
1165 va_end(args);
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001166}
J. Bruce Fields624ab462013-01-08 16:22:15 -05001167#else
1168static __printf(2,3) void svc_printk(struct svc_rqst *rqstp, const char *fmt, ...) {}
1169#endif
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001170
Trond Myklebust83dd59a2019-04-09 11:46:14 -04001171__be32
1172svc_return_autherr(struct svc_rqst *rqstp, __be32 auth_err)
1173{
1174 set_bit(RQ_AUTHERR, &rqstp->rq_flags);
1175 return auth_err;
1176}
1177EXPORT_SYMBOL_GPL(svc_return_autherr);
1178
1179static __be32
1180svc_get_autherr(struct svc_rqst *rqstp, __be32 *statp)
1181{
1182 if (test_and_clear_bit(RQ_AUTHERR, &rqstp->rq_flags))
1183 return *statp;
1184 return rpc_auth_ok;
1185}
1186
Trond Myklebust45326082019-04-09 11:46:16 -04001187static int
1188svc_generic_dispatch(struct svc_rqst *rqstp, __be32 *statp)
1189{
1190 struct kvec *argv = &rqstp->rq_arg.head[0];
1191 struct kvec *resv = &rqstp->rq_res.head[0];
1192 const struct svc_procedure *procp = rqstp->rq_procinfo;
1193
1194 /*
1195 * Decode arguments
1196 * XXX: why do we ignore the return value?
1197 */
1198 if (procp->pc_decode &&
1199 !procp->pc_decode(rqstp, argv->iov_base)) {
1200 *statp = rpc_garbage_args;
1201 return 1;
1202 }
1203
1204 *statp = procp->pc_func(rqstp);
1205
1206 if (*statp == rpc_drop_reply ||
1207 test_bit(RQ_DROPME, &rqstp->rq_flags))
1208 return 0;
1209
1210 if (test_bit(RQ_AUTHERR, &rqstp->rq_flags))
1211 return 1;
1212
1213 if (*statp != rpc_success)
1214 return 1;
1215
1216 /* Encode reply */
1217 if (procp->pc_encode &&
1218 !procp->pc_encode(rqstp, resv->iov_base + resv->iov_len)) {
1219 dprintk("svc: failed to encode reply\n");
1220 /* serv->sv_stats->rpcsystemerr++; */
1221 *statp = rpc_system_err;
1222 }
1223 return 1;
1224}
1225
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001226__be32
1227svc_generic_init_request(struct svc_rqst *rqstp,
1228 const struct svc_program *progp,
1229 struct svc_process_info *ret)
1230{
1231 const struct svc_version *versp = NULL; /* compiler food */
1232 const struct svc_procedure *procp = NULL;
1233
1234 if (rqstp->rq_vers >= progp->pg_nvers )
1235 goto err_bad_vers;
1236 versp = progp->pg_vers[rqstp->rq_vers];
1237 if (!versp)
1238 goto err_bad_vers;
1239
1240 /*
1241 * Some protocol versions (namely NFSv4) require some form of
1242 * congestion control. (See RFC 7530 section 3.1 paragraph 2)
1243 * In other words, UDP is not allowed. We mark those when setting
1244 * up the svc_xprt, and verify that here.
1245 *
1246 * The spec is not very clear about what error should be returned
1247 * when someone tries to access a server that is listening on UDP
1248 * for lower versions. RPC_PROG_MISMATCH seems to be the closest
1249 * fit.
1250 */
1251 if (versp->vs_need_cong_ctrl && rqstp->rq_xprt &&
1252 !test_bit(XPT_CONG_CTRL, &rqstp->rq_xprt->xpt_flags))
1253 goto err_bad_vers;
1254
1255 if (rqstp->rq_proc >= versp->vs_nproc)
1256 goto err_bad_proc;
1257 rqstp->rq_procinfo = procp = &versp->vs_proc[rqstp->rq_proc];
1258 if (!procp)
1259 goto err_bad_proc;
1260
1261 /* Initialize storage for argp and resp */
1262 memset(rqstp->rq_argp, 0, procp->pc_argsize);
1263 memset(rqstp->rq_resp, 0, procp->pc_ressize);
1264
1265 /* Bump per-procedure stats counter */
1266 versp->vs_count[rqstp->rq_proc]++;
1267
1268 ret->dispatch = versp->vs_dispatch;
1269 return rpc_success;
1270err_bad_vers:
1271 ret->mismatch.lovers = progp->pg_lovers;
1272 ret->mismatch.hivers = progp->pg_hivers;
1273 return rpc_prog_mismatch;
1274err_bad_proc:
1275 return rpc_proc_unavail;
1276}
1277EXPORT_SYMBOL_GPL(svc_generic_init_request);
1278
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001279/*
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001280 * Common routine for processing the RPC request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 */
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001282static int
1283svc_process_common(struct svc_rqst *rqstp, struct kvec *argv, struct kvec *resv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
1285 struct svc_program *progp;
Christoph Hellwig860bda22017-05-12 16:11:49 +02001286 const struct svc_procedure *procp = NULL;
NeilBrown6fb2b472006-10-02 02:17:50 -07001287 struct svc_serv *serv = rqstp->rq_server;
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001288 struct svc_process_info process;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001289 __be32 *statp;
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001290 u32 prog, vers;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001291 __be32 auth_stat, rpc_stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 int auth_res;
J.Bruce Fields8f8e05c2006-10-04 02:16:08 -07001293 __be32 *reply_statp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 rpc_stat = rpc_success;
1296
1297 if (argv->iov_len < 6*4)
1298 goto err_short_len;
1299
Chuck Lever06eb8a52017-06-19 14:00:40 -04001300 /* Will be turned off by GSS integrity and privacy services */
Jeff Layton779fb0f2014-11-19 07:51:18 -05001301 set_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
Andy Adamson2f425872009-04-03 08:27:32 +03001302 /* Will be turned off only when NFSv4 Sessions are used */
Jeff Layton30660e04b2014-11-19 07:51:16 -05001303 set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
Jeff Layton78b65eb2014-11-19 07:51:17 -05001304 clear_bit(RQ_DROPME, &rqstp->rq_flags);
Tom Tuckere831fe62007-12-30 21:07:29 -06001305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 svc_putu32(resv, rqstp->rq_xid);
1307
Alexey Dobriyan76994312006-09-26 22:28:46 -07001308 vers = svc_getnl(argv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 /* First words of reply: */
Alexey Dobriyan76994312006-09-26 22:28:46 -07001311 svc_putnl(resv, 1); /* REPLY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (vers != 2) /* RPC version number */
1314 goto err_bad_rpc;
1315
1316 /* Save position in case we later decide to reject: */
J.Bruce Fields8f8e05c2006-10-04 02:16:08 -07001317 reply_statp = resv->iov_base + resv->iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Alexey Dobriyan76994312006-09-26 22:28:46 -07001319 svc_putnl(resv, 0); /* ACCEPT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Alexey Dobriyan76994312006-09-26 22:28:46 -07001321 rqstp->rq_prog = prog = svc_getnl(argv); /* program number */
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001322 rqstp->rq_vers = svc_getnl(argv); /* version number */
1323 rqstp->rq_proc = svc_getnl(argv); /* procedure number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
NeilBrown80d188a2005-11-07 01:00:27 -08001325 for (progp = serv->sv_program; progp; progp = progp->pg_next)
1326 if (prog == progp->pg_prog)
1327 break;
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 /*
1330 * Decode auth data, and add verifier to reply buffer.
1331 * We do this before anything else in order to get a decent
1332 * auth verifier.
1333 */
1334 auth_res = svc_authenticate(rqstp, &auth_stat);
1335 /* Also give the program a chance to reject this call: */
NeilBrown80d188a2005-11-07 01:00:27 -08001336 if (auth_res == SVC_OK && progp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 auth_stat = rpc_autherr_badcred;
1338 auth_res = progp->pg_authenticate(rqstp);
1339 }
1340 switch (auth_res) {
1341 case SVC_OK:
1342 break;
1343 case SVC_GARBAGE:
Harshula Jayasuriyadd352102008-02-20 10:56:56 +11001344 goto err_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 case SVC_SYSERR:
1346 rpc_stat = rpc_system_err;
1347 goto err_bad;
1348 case SVC_DENIED:
1349 goto err_bad_auth;
NeilBrown1ebede82010-08-12 17:04:07 +10001350 case SVC_CLOSE:
Chuck Lever4d712ef2016-11-29 11:04:34 -05001351 goto close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 case SVC_DROP:
1353 goto dropit;
1354 case SVC_COMPLETE:
1355 goto sendit;
1356 }
NeilBrown80d188a2005-11-07 01:00:27 -08001357
Andreas Gruenbacher9ba02632005-06-22 17:16:24 +00001358 if (progp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 goto err_bad_prog;
1360
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001361 rpc_stat = progp->pg_init_request(rqstp, progp, &process);
1362 switch (rpc_stat) {
1363 case rpc_success:
1364 break;
1365 case rpc_prog_unavail:
1366 goto err_bad_prog;
1367 case rpc_prog_mismatch:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 goto err_bad_vers;
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001369 case rpc_proc_unavail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 goto err_bad_proc;
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001371 }
1372
1373 procp = rqstp->rq_procinfo;
1374 /* Should this check go into the dispatcher? */
1375 if (!procp || !procp->pc_func)
1376 goto err_bad_proc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 /* Syntactic check complete */
1379 serv->sv_stats->rpccnt++;
Chuck Lever0b9547b2018-03-27 10:51:22 -04001380 trace_svc_process(rqstp, progp->pg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 /* Build the reply header. */
1383 statp = resv->iov_base +resv->iov_len;
Alexey Dobriyan76994312006-09-26 22:28:46 -07001384 svc_putnl(resv, RPC_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001386 /* un-reserve some of the out-queue now that we have a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 * better idea of reply size
1388 */
1389 if (procp->pc_xdrressize)
Jeff Laytoncd1230122007-05-09 02:34:50 -07001390 svc_reserve_auth(rqstp, procp->pc_xdrressize<<2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 /* Call the function that processes the request. */
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001393 if (!process.dispatch) {
Trond Myklebust45326082019-04-09 11:46:16 -04001394 if (!svc_generic_dispatch(rqstp, statp))
1395 goto release_dropit;
1396 if (*statp == rpc_garbage_args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 goto err_garbage;
Trond Myklebust83dd59a2019-04-09 11:46:14 -04001398 auth_stat = svc_get_autherr(rqstp, statp);
1399 if (auth_stat != rpc_auth_ok)
1400 goto err_release_bad_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 } else {
1402 dprintk("svc: calling dispatcher\n");
Trond Myklebust45326082019-04-09 11:46:16 -04001403 if (!process.dispatch(rqstp, statp))
1404 goto release_dropit; /* Release reply info */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
1406
1407 /* Check RPC status result */
1408 if (*statp != rpc_success)
1409 resv->iov_len = ((void*)statp) - resv->iov_base + 4;
1410
1411 /* Release reply info */
1412 if (procp->pc_release)
Christoph Hellwig85374882017-05-08 18:48:24 +02001413 procp->pc_release(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 if (procp->pc_encode == NULL)
1416 goto dropit;
1417
1418 sendit:
1419 if (svc_authorise(rqstp))
Chuck Lever4d712ef2016-11-29 11:04:34 -05001420 goto close;
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001421 return 1; /* Caller can now send it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Trond Myklebust45326082019-04-09 11:46:16 -04001423release_dropit:
1424 if (procp->pc_release)
1425 procp->pc_release(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 dropit:
1427 svc_authorise(rqstp); /* doesn't hurt to call this twice */
1428 dprintk("svc: svc_process dropit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return 0;
1430
Chuck Lever4d712ef2016-11-29 11:04:34 -05001431 close:
Vasily Averind4b09ac2018-12-24 14:44:52 +03001432 if (rqstp->rq_xprt && test_bit(XPT_TEMP, &rqstp->rq_xprt->xpt_flags))
Chuck Lever4d712ef2016-11-29 11:04:34 -05001433 svc_close_xprt(rqstp->rq_xprt);
1434 dprintk("svc: svc_process close\n");
1435 return 0;
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437err_short_len:
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001438 svc_printk(rqstp, "short len %zd, dropping request\n",
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001439 argv->iov_len);
Chuck Lever4d712ef2016-11-29 11:04:34 -05001440 goto close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442err_bad_rpc:
1443 serv->sv_stats->rpcbadfmt++;
Alexey Dobriyan76994312006-09-26 22:28:46 -07001444 svc_putnl(resv, 1); /* REJECT */
1445 svc_putnl(resv, 0); /* RPC_MISMATCH */
1446 svc_putnl(resv, 2); /* Only RPCv2 supported */
1447 svc_putnl(resv, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 goto sendit;
1449
Trond Myklebust83dd59a2019-04-09 11:46:14 -04001450err_release_bad_auth:
1451 if (procp->pc_release)
1452 procp->pc_release(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453err_bad_auth:
1454 dprintk("svc: authentication failed (%d)\n", ntohl(auth_stat));
1455 serv->sv_stats->rpcbadauth++;
1456 /* Restore write pointer to location of accept status: */
J.Bruce Fields8f8e05c2006-10-04 02:16:08 -07001457 xdr_ressize_check(rqstp, reply_statp);
Alexey Dobriyan76994312006-09-26 22:28:46 -07001458 svc_putnl(resv, 1); /* REJECT */
1459 svc_putnl(resv, 1); /* AUTH_ERROR */
1460 svc_putnl(resv, ntohl(auth_stat)); /* status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 goto sendit;
1462
1463err_bad_prog:
Andreas Gruenbacher9ba02632005-06-22 17:16:24 +00001464 dprintk("svc: unknown program %d\n", prog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 serv->sv_stats->rpcbadfmt++;
Alexey Dobriyan76994312006-09-26 22:28:46 -07001466 svc_putnl(resv, RPC_PROG_UNAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 goto sendit;
1468
1469err_bad_vers:
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001470 svc_printk(rqstp, "unknown version (%d for prog %d, %s)\n",
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001471 rqstp->rq_vers, rqstp->rq_prog, progp->pg_name);
NeilBrown34e9a632007-01-29 13:19:52 -08001472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 serv->sv_stats->rpcbadfmt++;
Alexey Dobriyan76994312006-09-26 22:28:46 -07001474 svc_putnl(resv, RPC_PROG_MISMATCH);
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001475 svc_putnl(resv, process.mismatch.lovers);
1476 svc_putnl(resv, process.mismatch.hivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 goto sendit;
1478
1479err_bad_proc:
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001480 svc_printk(rqstp, "unknown procedure (%d)\n", rqstp->rq_proc);
NeilBrown34e9a632007-01-29 13:19:52 -08001481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 serv->sv_stats->rpcbadfmt++;
Alexey Dobriyan76994312006-09-26 22:28:46 -07001483 svc_putnl(resv, RPC_PROC_UNAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 goto sendit;
1485
1486err_garbage:
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001487 svc_printk(rqstp, "failed to decode args\n");
NeilBrown34e9a632007-01-29 13:19:52 -08001488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 rpc_stat = rpc_garbage_args;
1490err_bad:
1491 serv->sv_stats->rpcbadfmt++;
Alexey Dobriyan76994312006-09-26 22:28:46 -07001492 svc_putnl(resv, ntohl(rpc_stat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 goto sendit;
1494}
Greg Banks7adae482006-10-04 02:15:47 -07001495
1496/*
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001497 * Process the RPC request.
1498 */
1499int
1500svc_process(struct svc_rqst *rqstp)
1501{
1502 struct kvec *argv = &rqstp->rq_arg.head[0];
1503 struct kvec *resv = &rqstp->rq_res.head[0];
1504 struct svc_serv *serv = rqstp->rq_server;
1505 u32 dir;
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001506
1507 /*
1508 * Setup response xdr_buf.
1509 * Initially it has just one page
1510 */
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05001511 rqstp->rq_next_page = &rqstp->rq_respages[1];
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001512 resv->iov_base = page_address(rqstp->rq_respages[0]);
1513 resv->iov_len = 0;
1514 rqstp->rq_res.pages = rqstp->rq_respages + 1;
1515 rqstp->rq_res.len = 0;
1516 rqstp->rq_res.page_base = 0;
1517 rqstp->rq_res.page_len = 0;
1518 rqstp->rq_res.buflen = PAGE_SIZE;
1519 rqstp->rq_res.tail[0].iov_base = NULL;
1520 rqstp->rq_res.tail[0].iov_len = 0;
1521
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001522 dir = svc_getnl(argv);
1523 if (dir != 0) {
1524 /* direction != CALL */
1525 svc_printk(rqstp, "bad direction %d, dropping request\n", dir);
1526 serv->sv_stats->rpcbadfmt++;
Jeff Layton860a0d92014-10-28 14:24:12 -04001527 goto out_drop;
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001528 }
1529
Chuck Lever067fb112019-02-11 11:24:37 -05001530 /* Reserve space for the record marker */
1531 if (rqstp->rq_prot == IPPROTO_TCP)
1532 svc_putnl(resv, 0);
1533
Andy Adamson4b5b3ba2011-01-06 02:04:24 +00001534 /* Returns 1 for send, 0 for drop */
Chuck Lever0b9547b2018-03-27 10:51:22 -04001535 if (likely(svc_process_common(rqstp, argv, resv)))
1536 return svc_send(rqstp);
Jeff Layton860a0d92014-10-28 14:24:12 -04001537
Jeff Layton860a0d92014-10-28 14:24:12 -04001538out_drop:
Jeff Layton860a0d92014-10-28 14:24:12 -04001539 svc_drop(rqstp);
1540 return 0;
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001541}
Chuck Lever3f87d5d2015-05-26 13:48:57 -04001542EXPORT_SYMBOL_GPL(svc_process);
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001543
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001544#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga4d6bbb62009-04-01 09:23:07 -04001545/*
1546 * Process a backchannel RPC request that arrived over an existing
1547 * outbound connection
1548 */
1549int
1550bc_svc_process(struct svc_serv *serv, struct rpc_rqst *req,
1551 struct svc_rqst *rqstp)
1552{
1553 struct kvec *argv = &rqstp->rq_arg.head[0];
1554 struct kvec *resv = &rqstp->rq_res.head[0];
Chuck Lever632dda82015-05-11 14:04:50 -04001555 struct rpc_task *task;
Trond Myklebust0d2a9702015-06-04 15:37:10 -04001556 int proc_error;
Chuck Lever632dda82015-05-11 14:04:50 -04001557 int error;
1558
1559 dprintk("svc: %s(%p)\n", __func__, req);
Ricardo Labiaga4d6bbb62009-04-01 09:23:07 -04001560
1561 /* Build the svc_rqst used by the common processing routine */
1562 rqstp->rq_xid = req->rq_xid;
1563 rqstp->rq_prot = req->rq_xprt->prot;
1564 rqstp->rq_server = serv;
Vasily Averind4b09ac2018-12-24 14:44:52 +03001565 rqstp->rq_bc_net = req->rq_xprt->xprt_net;
Ricardo Labiaga4d6bbb62009-04-01 09:23:07 -04001566
1567 rqstp->rq_addrlen = sizeof(req->rq_xprt->addr);
1568 memcpy(&rqstp->rq_addr, &req->rq_xprt->addr, rqstp->rq_addrlen);
1569 memcpy(&rqstp->rq_arg, &req->rq_rcv_buf, sizeof(rqstp->rq_arg));
1570 memcpy(&rqstp->rq_res, &req->rq_snd_buf, sizeof(rqstp->rq_res));
Trond Myklebust756b9b32015-12-07 12:52:23 -08001571
1572 /* Adjust the argument buffer length */
Benjamin Coddington38b76312015-11-20 09:55:30 -05001573 rqstp->rq_arg.len = req->rq_private_buf.len;
Trond Myklebust756b9b32015-12-07 12:52:23 -08001574 if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
1575 rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
1576 rqstp->rq_arg.page_len = 0;
1577 } else if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len +
1578 rqstp->rq_arg.page_len)
1579 rqstp->rq_arg.page_len = rqstp->rq_arg.len -
1580 rqstp->rq_arg.head[0].iov_len;
1581 else
1582 rqstp->rq_arg.len = rqstp->rq_arg.head[0].iov_len +
1583 rqstp->rq_arg.page_len;
Ricardo Labiaga4d6bbb62009-04-01 09:23:07 -04001584
1585 /* reset result send buffer "put" position */
1586 resv->iov_len = 0;
1587
Ricardo Labiaga4d6bbb62009-04-01 09:23:07 -04001588 /*
1589 * Skip the next two words because they've already been
Chuck Lever632dda82015-05-11 14:04:50 -04001590 * processed in the transport
Ricardo Labiaga4d6bbb62009-04-01 09:23:07 -04001591 */
1592 svc_getu32(argv); /* XID */
1593 svc_getnl(argv); /* CALLDIR */
1594
Chuck Lever632dda82015-05-11 14:04:50 -04001595 /* Parse and execute the bc call */
Trond Myklebust0d2a9702015-06-04 15:37:10 -04001596 proc_error = svc_process_common(rqstp, argv, resv);
1597
Trond Myklebust7402a4f2019-07-16 13:51:29 -04001598 atomic_dec(&req->rq_xprt->bc_slot_count);
Trond Myklebust0d2a9702015-06-04 15:37:10 -04001599 if (!proc_error) {
Chuck Lever632dda82015-05-11 14:04:50 -04001600 /* Processing error: drop the request */
Trond Myklebustb3b02ae2012-05-31 15:26:38 -04001601 xprt_free_bc_request(req);
Vasily Averin8f7766c2018-12-24 14:46:08 +03001602 error = -EINVAL;
1603 goto out;
Andy Adamson4b5b3ba2011-01-06 02:04:24 +00001604 }
Chuck Lever632dda82015-05-11 14:04:50 -04001605 /* Finally, send the reply synchronously */
1606 memcpy(&req->rq_snd_buf, &rqstp->rq_res, sizeof(req->rq_snd_buf));
Trond Myklebust0f419792015-06-01 22:59:08 -04001607 task = rpc_run_bc_task(req);
Chuck Lever632dda82015-05-11 14:04:50 -04001608 if (IS_ERR(task)) {
1609 error = PTR_ERR(task);
1610 goto out;
1611 }
1612
1613 WARN_ON_ONCE(atomic_read(&task->tk_count) != 1);
1614 error = task->tk_status;
1615 rpc_put_task(task);
1616
1617out:
1618 dprintk("svc: %s(), error=%d\n", __func__, error);
1619 return error;
Ricardo Labiaga4d6bbb62009-04-01 09:23:07 -04001620}
Trond Myklebust0d961aa2011-07-13 19:24:15 -04001621EXPORT_SYMBOL_GPL(bc_svc_process);
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001622#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga4d6bbb62009-04-01 09:23:07 -04001623
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001624/*
Greg Banks7adae482006-10-04 02:15:47 -07001625 * Return (transport-specific) limit on the rpc payload.
1626 */
1627u32 svc_max_payload(const struct svc_rqst *rqstp)
1628{
Tom Tucker49023152007-12-30 21:07:21 -06001629 u32 max = rqstp->rq_xprt->xpt_class->xcl_max_payload;
Greg Banks7adae482006-10-04 02:15:47 -07001630
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001631 if (rqstp->rq_server->sv_max_payload < max)
1632 max = rqstp->rq_server->sv_max_payload;
Greg Banks7adae482006-10-04 02:15:47 -07001633 return max;
1634}
1635EXPORT_SYMBOL_GPL(svc_max_payload);
Chuck Lever8154ef22018-03-27 10:54:07 -04001636
1637/**
1638 * svc_fill_write_vector - Construct data argument for VFS write call
1639 * @rqstp: svc_rqst to operate on
Chuck Lever3fd95572018-07-27 11:19:05 -04001640 * @pages: list of pages containing data payload
Chuck Lever8154ef22018-03-27 10:54:07 -04001641 * @first: buffer containing first section of write payload
1642 * @total: total number of bytes of write payload
1643 *
Chuck Lever3fd95572018-07-27 11:19:05 -04001644 * Fills in rqstp::rq_vec, and returns the number of elements.
Chuck Lever8154ef22018-03-27 10:54:07 -04001645 */
Chuck Lever3fd95572018-07-27 11:19:05 -04001646unsigned int svc_fill_write_vector(struct svc_rqst *rqstp, struct page **pages,
1647 struct kvec *first, size_t total)
Chuck Lever8154ef22018-03-27 10:54:07 -04001648{
1649 struct kvec *vec = rqstp->rq_vec;
Chuck Lever8154ef22018-03-27 10:54:07 -04001650 unsigned int i;
1651
1652 /* Some types of transport can present the write payload
1653 * entirely in rq_arg.pages. In this case, @first is empty.
1654 */
1655 i = 0;
1656 if (first->iov_len) {
1657 vec[i].iov_base = first->iov_base;
1658 vec[i].iov_len = min_t(size_t, total, first->iov_len);
1659 total -= vec[i].iov_len;
1660 ++i;
1661 }
1662
Chuck Lever8154ef22018-03-27 10:54:07 -04001663 while (total) {
1664 vec[i].iov_base = page_address(*pages);
1665 vec[i].iov_len = min_t(size_t, total, PAGE_SIZE);
1666 total -= vec[i].iov_len;
1667 ++i;
Chuck Lever8154ef22018-03-27 10:54:07 -04001668 ++pages;
1669 }
1670
1671 WARN_ON_ONCE(i > ARRAY_SIZE(rqstp->rq_vec));
1672 return i;
1673}
1674EXPORT_SYMBOL_GPL(svc_fill_write_vector);
Chuck Lever38a70312018-03-27 10:54:21 -04001675
1676/**
1677 * svc_fill_symlink_pathname - Construct pathname argument for VFS symlink call
1678 * @rqstp: svc_rqst to operate on
1679 * @first: buffer containing first section of pathname
Chuck Lever11b4d662018-07-27 11:19:10 -04001680 * @p: buffer containing remaining section of pathname
Chuck Lever38a70312018-03-27 10:54:21 -04001681 * @total: total length of the pathname argument
1682 *
Chuck Lever11b4d662018-07-27 11:19:10 -04001683 * The VFS symlink API demands a NUL-terminated pathname in mapped memory.
1684 * Returns pointer to a NUL-terminated string, or an ERR_PTR. Caller must free
1685 * the returned string.
Chuck Lever38a70312018-03-27 10:54:21 -04001686 */
1687char *svc_fill_symlink_pathname(struct svc_rqst *rqstp, struct kvec *first,
Chuck Lever11b4d662018-07-27 11:19:10 -04001688 void *p, size_t total)
Chuck Lever38a70312018-03-27 10:54:21 -04001689{
Chuck Lever11b4d662018-07-27 11:19:10 -04001690 size_t len, remaining;
1691 char *result, *dst;
Chuck Lever38a70312018-03-27 10:54:21 -04001692
Chuck Lever11b4d662018-07-27 11:19:10 -04001693 result = kmalloc(total + 1, GFP_KERNEL);
1694 if (!result)
1695 return ERR_PTR(-ESERVERFAULT);
Chuck Lever38a70312018-03-27 10:54:21 -04001696
Chuck Lever11b4d662018-07-27 11:19:10 -04001697 dst = result;
1698 remaining = total;
Chuck Lever38a70312018-03-27 10:54:21 -04001699
Chuck Lever11b4d662018-07-27 11:19:10 -04001700 len = min_t(size_t, total, first->iov_len);
1701 if (len) {
Chuck Lever38a70312018-03-27 10:54:21 -04001702 memcpy(dst, first->iov_base, len);
1703 dst += len;
1704 remaining -= len;
Chuck Lever38a70312018-03-27 10:54:21 -04001705 }
1706
Chuck Lever11b4d662018-07-27 11:19:10 -04001707 if (remaining) {
1708 len = min_t(size_t, remaining, PAGE_SIZE);
1709 memcpy(dst, p, len);
1710 dst += len;
1711 }
1712
1713 *dst = '\0';
1714
1715 /* Sanity check: Linux doesn't allow the pathname argument to
Chuck Lever38a70312018-03-27 10:54:21 -04001716 * contain a NUL byte.
1717 */
Chuck Lever11b4d662018-07-27 11:19:10 -04001718 if (strlen(result) != total) {
1719 kfree(result);
Chuck Lever38a70312018-03-27 10:54:21 -04001720 return ERR_PTR(-EINVAL);
Chuck Lever11b4d662018-07-27 11:19:10 -04001721 }
Chuck Lever38a70312018-03-27 10:54:21 -04001722 return result;
1723}
1724EXPORT_SYMBOL_GPL(svc_fill_symlink_pathname);