blob: 4187745887f0f946017853924df0f6f2054f88ec [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:
Xiongfeng Wang746c6232020-05-08 09:32:59 +080091 return strlcpy(buf, "auto\n", 20);
Greg Banks42a7fc42007-03-06 01:42:23 -080092 case SVC_POOL_GLOBAL:
Xiongfeng Wang746c6232020-05-08 09:32:59 +080093 return strlcpy(buf, "global\n", 20);
Greg Banks42a7fc42007-03-06 01:42:23 -080094 case SVC_POOL_PERCPU:
Xiongfeng Wang746c6232020-05-08 09:32:59 +080095 return strlcpy(buf, "percpu\n", 20);
Greg Banks42a7fc42007-03-06 01:42:23 -080096 case SVC_POOL_PERNODE:
Xiongfeng Wang746c6232020-05-08 09:32:59 +080097 return strlcpy(buf, "pernode\n", 20);
Greg Banks42a7fc42007-03-06 01:42:23 -080098 default:
Xiongfeng Wang746c6232020-05-08 09:32:59 +080099 return sprintf(buf, "%d\n", *ip);
Greg Banks42a7fc42007-03-06 01:42:23 -0800100 }
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
Chuck Lever51919552020-11-05 11:19:42 -0500617 rqstp->rq_scratch_page = alloc_pages_node(node, GFP_KERNEL, 0);
618 if (!rqstp->rq_scratch_page)
619 goto out_enomem;
620
Jeff Layton1b6dc1d2015-06-08 12:11:10 -0700621 rqstp->rq_argp = kmalloc_node(serv->sv_xdrsize, GFP_KERNEL, node);
622 if (!rqstp->rq_argp)
623 goto out_enomem;
624
625 rqstp->rq_resp = kmalloc_node(serv->sv_xdrsize, GFP_KERNEL, node);
626 if (!rqstp->rq_resp)
627 goto out_enomem;
628
629 if (!svc_init_buffer(rqstp, serv->sv_max_mesg, node))
630 goto out_enomem;
631
632 return rqstp;
633out_enomem:
634 svc_rqst_free(rqstp);
635 return NULL;
636}
637EXPORT_SYMBOL_GPL(svc_rqst_alloc);
638
639struct svc_rqst *
640svc_prepare_thread(struct svc_serv *serv, struct svc_pool *pool, int node)
641{
642 struct svc_rqst *rqstp;
643
644 rqstp = svc_rqst_alloc(serv, pool, node);
645 if (!rqstp)
646 return ERR_PTR(-ENOMEM);
647
648 serv->sv_nrthreads++;
Jeff Layton0113ab32008-01-29 10:30:54 -0500649 spin_lock_bh(&pool->sp_lock);
650 pool->sp_nrthreads++;
Jeff Layton81244382014-11-21 14:19:28 -0500651 list_add_rcu(&rqstp->rq_all, &pool->sp_all_threads);
Jeff Layton0113ab32008-01-29 10:30:54 -0500652 spin_unlock_bh(&pool->sp_lock);
Jeff Layton0113ab32008-01-29 10:30:54 -0500653 return rqstp;
Jeff Layton0113ab32008-01-29 10:30:54 -0500654}
Trond Myklebust24c37672008-12-23 16:30:12 -0500655EXPORT_SYMBOL_GPL(svc_prepare_thread);
Jeff Layton0113ab32008-01-29 10:30:54 -0500656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657/*
Greg Banksa7455442006-10-02 02:17:59 -0700658 * Choose a pool in which to create a new thread, for svc_set_num_threads
659 */
660static inline struct svc_pool *
661choose_pool(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state)
662{
663 if (pool != NULL)
664 return pool;
665
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800666 return &serv->sv_pools[(*state)++ % serv->sv_nrpools];
Greg Banksa7455442006-10-02 02:17:59 -0700667}
668
669/*
670 * Choose a thread to kill, for svc_set_num_threads
671 */
672static inline struct task_struct *
673choose_victim(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state)
674{
675 unsigned int i;
676 struct task_struct *task = NULL;
677
678 if (pool != NULL) {
679 spin_lock_bh(&pool->sp_lock);
680 } else {
681 /* choose a pool in round-robin fashion */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800682 for (i = 0; i < serv->sv_nrpools; i++) {
683 pool = &serv->sv_pools[--(*state) % serv->sv_nrpools];
Greg Banksa7455442006-10-02 02:17:59 -0700684 spin_lock_bh(&pool->sp_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800685 if (!list_empty(&pool->sp_all_threads))
686 goto found_pool;
Greg Banksa7455442006-10-02 02:17:59 -0700687 spin_unlock_bh(&pool->sp_lock);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800688 }
Greg Banksa7455442006-10-02 02:17:59 -0700689 return NULL;
690 }
691
692found_pool:
693 if (!list_empty(&pool->sp_all_threads)) {
694 struct svc_rqst *rqstp;
695
696 /*
697 * Remove from the pool->sp_all_threads list
698 * so we don't try to kill it again.
699 */
700 rqstp = list_entry(pool->sp_all_threads.next, struct svc_rqst, rq_all);
Jeff Layton81244382014-11-21 14:19:28 -0500701 set_bit(RQ_VICTIM, &rqstp->rq_flags);
702 list_del_rcu(&rqstp->rq_all);
Greg Banksa7455442006-10-02 02:17:59 -0700703 task = rqstp->rq_task;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800704 }
Greg Banksa7455442006-10-02 02:17:59 -0700705 spin_unlock_bh(&pool->sp_lock);
706
707 return task;
708}
709
Trond Myklebust9e0d8762017-04-26 11:55:26 -0400710/* create new threads */
711static int
712svc_start_kthreads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
713{
714 struct svc_rqst *rqstp;
715 struct task_struct *task;
716 struct svc_pool *chosen_pool;
717 unsigned int state = serv->sv_nrthreads-1;
718 int node;
719
720 do {
721 nrservs--;
722 chosen_pool = choose_pool(serv, pool, &state);
723
724 node = svc_pool_map_get_node(chosen_pool->sp_id);
725 rqstp = svc_prepare_thread(serv, chosen_pool, node);
726 if (IS_ERR(rqstp))
727 return PTR_ERR(rqstp);
728
729 __module_get(serv->sv_ops->svo_module);
730 task = kthread_create_on_node(serv->sv_ops->svo_function, rqstp,
731 node, "%s", serv->sv_name);
732 if (IS_ERR(task)) {
733 module_put(serv->sv_ops->svo_module);
734 svc_exit_thread(rqstp);
735 return PTR_ERR(task);
736 }
737
738 rqstp->rq_task = task;
739 if (serv->sv_nrpools > 1)
740 svc_pool_map_set_cpumask(task, chosen_pool->sp_id);
741
742 svc_sock_update_bufs(serv);
743 wake_up_process(task);
744 } while (nrservs > 0);
745
746 return 0;
747}
748
749
750/* destroy old threads */
751static int
752svc_signal_kthreads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
753{
754 struct task_struct *task;
755 unsigned int state = serv->sv_nrthreads-1;
756
757 /* destroy old threads */
758 do {
759 task = choose_victim(serv, pool, &state);
760 if (task == NULL)
761 break;
762 send_sig(SIGINT, task, 1);
763 nrservs++;
764 } while (nrservs < 0);
765
766 return 0;
767}
768
Greg Banksa7455442006-10-02 02:17:59 -0700769/*
770 * Create or destroy enough new threads to make the number
771 * of threads the given number. If `pool' is non-NULL, applies
772 * only to threads in that pool, otherwise round-robins between
J. Bruce Fields94cf3172011-12-01 17:51:21 -0500773 * all pools. Caller must ensure that mutual exclusion between this and
774 * server startup or shutdown.
Greg Banksa7455442006-10-02 02:17:59 -0700775 *
776 * Destroying threads relies on the service threads filling in
777 * rqstp->rq_task, which only the nfs ones do. Assumes the serv
778 * has been created using svc_create_pooled().
779 *
780 * Based on code that used to be in nfsd_svc() but tweaked
781 * to be pool-aware.
782 */
783int
784svc_set_num_threads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
785{
Greg Banksa7455442006-10-02 02:17:59 -0700786 if (pool == NULL) {
787 /* The -1 assumes caller has done a svc_get() */
788 nrservs -= (serv->sv_nrthreads-1);
789 } else {
790 spin_lock_bh(&pool->sp_lock);
791 nrservs -= pool->sp_nrthreads;
792 spin_unlock_bh(&pool->sp_lock);
793 }
794
Trond Myklebust9e0d8762017-04-26 11:55:26 -0400795 if (nrservs > 0)
796 return svc_start_kthreads(serv, pool, nrservs);
797 if (nrservs < 0)
798 return svc_signal_kthreads(serv, pool, nrservs);
799 return 0;
Greg Banksa7455442006-10-02 02:17:59 -0700800}
Trond Myklebust24c37672008-12-23 16:30:12 -0500801EXPORT_SYMBOL_GPL(svc_set_num_threads);
Greg Banksa7455442006-10-02 02:17:59 -0700802
Trond Myklebusted6473d2017-04-26 11:55:27 -0400803/* destroy old threads */
804static int
805svc_stop_kthreads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
806{
807 struct task_struct *task;
808 unsigned int state = serv->sv_nrthreads-1;
809
810 /* destroy old threads */
811 do {
812 task = choose_victim(serv, pool, &state);
813 if (task == NULL)
814 break;
815 kthread_stop(task);
816 nrservs++;
817 } while (nrservs < 0);
818 return 0;
819}
820
821int
822svc_set_num_threads_sync(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
823{
824 if (pool == NULL) {
825 /* The -1 assumes caller has done a svc_get() */
826 nrservs -= (serv->sv_nrthreads-1);
827 } else {
828 spin_lock_bh(&pool->sp_lock);
829 nrservs -= pool->sp_nrthreads;
830 spin_unlock_bh(&pool->sp_lock);
831 }
832
833 if (nrservs > 0)
834 return svc_start_kthreads(serv, pool, nrservs);
835 if (nrservs < 0)
836 return svc_stop_kthreads(serv, pool, nrservs);
837 return 0;
838}
839EXPORT_SYMBOL_GPL(svc_set_num_threads_sync);
840
Greg Banksa7455442006-10-02 02:17:59 -0700841/*
Jeff Layton3c519912015-01-22 08:19:32 -0500842 * Called from a server thread as it's exiting. Caller must hold the "service
843 * mutex" for the service.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 */
845void
Jeff Layton1b6dc1d2015-06-08 12:11:10 -0700846svc_rqst_free(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 svc_release_buffer(rqstp);
Chuck Lever51919552020-11-05 11:19:42 -0500849 put_page(rqstp->rq_scratch_page);
Jesper Juhla51482b2005-11-08 09:41:34 -0800850 kfree(rqstp->rq_resp);
851 kfree(rqstp->rq_argp);
852 kfree(rqstp->rq_auth_data);
Jeff Layton1b6dc1d2015-06-08 12:11:10 -0700853 kfree_rcu(rqstp, rq_rcu_head);
854}
855EXPORT_SYMBOL_GPL(svc_rqst_free);
856
857void
858svc_exit_thread(struct svc_rqst *rqstp)
859{
860 struct svc_serv *serv = rqstp->rq_server;
861 struct svc_pool *pool = rqstp->rq_pool;
Greg Banks3262c812006-10-02 02:17:58 -0700862
863 spin_lock_bh(&pool->sp_lock);
864 pool->sp_nrthreads--;
Jeff Layton81244382014-11-21 14:19:28 -0500865 if (!test_and_set_bit(RQ_VICTIM, &rqstp->rq_flags))
866 list_del_rcu(&rqstp->rq_all);
Greg Banks3262c812006-10-02 02:17:58 -0700867 spin_unlock_bh(&pool->sp_lock);
868
Jeff Layton1b6dc1d2015-06-08 12:11:10 -0700869 svc_rqst_free(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 /* Release the server */
872 if (serv)
873 svc_destroy(serv);
874}
Trond Myklebust24c37672008-12-23 16:30:12 -0500875EXPORT_SYMBOL_GPL(svc_exit_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877/*
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500878 * Register an "inet" protocol family netid with the local
879 * rpcbind daemon via an rpcbind v4 SET request.
Chuck Levera26cfad2008-08-18 19:34:16 -0400880 *
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500881 * No netconfig infrastructure is available in the kernel, so
882 * we map IP_ protocol numbers to netids by hand.
Chuck Levera26cfad2008-08-18 19:34:16 -0400883 *
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500884 * Returns zero on success; a negative errno value is returned
885 * if any error occurs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 */
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +0400887static int __svc_rpcb_register4(struct net *net, const u32 program,
888 const u32 version,
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500889 const unsigned short protocol,
890 const unsigned short port)
Chuck Levera26cfad2008-08-18 19:34:16 -0400891{
Chuck Levercadc0fa2009-03-18 20:47:36 -0400892 const struct sockaddr_in sin = {
Chuck Levera26cfad2008-08-18 19:34:16 -0400893 .sin_family = AF_INET,
894 .sin_addr.s_addr = htonl(INADDR_ANY),
895 .sin_port = htons(port),
896 };
Chuck Levercadc0fa2009-03-18 20:47:36 -0400897 const char *netid;
898 int error;
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500899
900 switch (protocol) {
901 case IPPROTO_UDP:
902 netid = RPCBIND_NETID_UDP;
903 break;
904 case IPPROTO_TCP:
905 netid = RPCBIND_NETID_TCP;
906 break;
907 default:
Chuck Leverba5c35e2009-03-18 20:46:59 -0400908 return -ENOPROTOOPT;
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500909 }
910
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +0400911 error = rpcb_v4_register(net, program, version,
Chuck Levercadc0fa2009-03-18 20:47:36 -0400912 (const struct sockaddr *)&sin, netid);
913
914 /*
915 * User space didn't support rpcbind v4, so retry this
916 * registration request with the legacy rpcbind v2 protocol.
917 */
918 if (error == -EPROTONOSUPPORT)
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +0400919 error = rpcb_register(net, program, version, protocol, port);
Chuck Levercadc0fa2009-03-18 20:47:36 -0400920
921 return error;
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500922}
923
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000924#if IS_ENABLED(CONFIG_IPV6)
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500925/*
926 * Register an "inet6" protocol family netid with the local
927 * rpcbind daemon via an rpcbind v4 SET request.
928 *
929 * No netconfig infrastructure is available in the kernel, so
930 * we map IP_ protocol numbers to netids by hand.
931 *
932 * Returns zero on success; a negative errno value is returned
933 * if any error occurs.
934 */
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +0400935static int __svc_rpcb_register6(struct net *net, const u32 program,
936 const u32 version,
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500937 const unsigned short protocol,
938 const unsigned short port)
939{
Chuck Levercadc0fa2009-03-18 20:47:36 -0400940 const struct sockaddr_in6 sin6 = {
Chuck Levera26cfad2008-08-18 19:34:16 -0400941 .sin6_family = AF_INET6,
942 .sin6_addr = IN6ADDR_ANY_INIT,
943 .sin6_port = htons(port),
944 };
Chuck Levercadc0fa2009-03-18 20:47:36 -0400945 const char *netid;
946 int error;
Chuck Levera26cfad2008-08-18 19:34:16 -0400947
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500948 switch (protocol) {
949 case IPPROTO_UDP:
950 netid = RPCBIND_NETID_UDP6;
951 break;
952 case IPPROTO_TCP:
953 netid = RPCBIND_NETID_TCP6;
954 break;
955 default:
Chuck Leverba5c35e2009-03-18 20:46:59 -0400956 return -ENOPROTOOPT;
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500957 }
958
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +0400959 error = rpcb_v4_register(net, program, version,
Chuck Levercadc0fa2009-03-18 20:47:36 -0400960 (const struct sockaddr *)&sin6, netid);
961
962 /*
963 * User space didn't support rpcbind version 4, so we won't
964 * use a PF_INET6 listener.
965 */
966 if (error == -EPROTONOSUPPORT)
967 error = -EAFNOSUPPORT;
968
969 return error;
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500970}
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000971#endif /* IS_ENABLED(CONFIG_IPV6) */
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500972
973/*
974 * Register a kernel RPC service via rpcbind version 4.
975 *
976 * Returns zero on success; a negative errno value is returned
977 * if any error occurs.
978 */
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +0400979static int __svc_register(struct net *net, const char *progname,
Chuck Lever363f7242009-03-18 20:47:44 -0400980 const u32 program, const u32 version,
Chuck Lever4b62e582009-03-18 20:46:06 -0400981 const int family,
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500982 const unsigned short protocol,
983 const unsigned short port)
984{
Chuck Lever363f7242009-03-18 20:47:44 -0400985 int error = -EAFNOSUPPORT;
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500986
Chuck Levera26cfad2008-08-18 19:34:16 -0400987 switch (family) {
Chuck Lever4b62e582009-03-18 20:46:06 -0400988 case PF_INET:
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +0400989 error = __svc_rpcb_register4(net, program, version,
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500990 protocol, port);
Chuck Levercadc0fa2009-03-18 20:47:36 -0400991 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000992#if IS_ENABLED(CONFIG_IPV6)
Chuck Lever4b62e582009-03-18 20:46:06 -0400993 case PF_INET6:
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +0400994 error = __svc_rpcb_register6(net, program, version,
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500995 protocol, port);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000996#endif
Chuck Levera26cfad2008-08-18 19:34:16 -0400997 }
998
Chuck Leverb4af5932020-05-02 11:59:37 -0400999 trace_svc_register(progname, version, protocol, port, family, error);
Chuck Lever363f7242009-03-18 20:47:44 -04001000 return error;
Chuck Levera26cfad2008-08-18 19:34:16 -04001001}
Chuck Lever2c7eb0b2008-09-15 16:27:23 -05001002
Trond Myklebust642ee6b2019-04-09 11:46:17 -04001003int svc_rpcbind_set_version(struct net *net,
1004 const struct svc_program *progp,
1005 u32 version, int family,
1006 unsigned short proto,
1007 unsigned short port)
1008{
Trond Myklebust642ee6b2019-04-09 11:46:17 -04001009 return __svc_register(net, progp->pg_name, progp->pg_prog,
1010 version, family, proto, port);
1011
1012}
1013EXPORT_SYMBOL_GPL(svc_rpcbind_set_version);
1014
1015int svc_generic_rpcbind_set(struct net *net,
1016 const struct svc_program *progp,
1017 u32 version, int family,
1018 unsigned short proto,
1019 unsigned short port)
1020{
1021 const struct svc_version *vers = progp->pg_vers[version];
1022 int error;
1023
1024 if (vers == NULL)
1025 return 0;
1026
1027 if (vers->vs_hidden) {
Chuck Leverb4af5932020-05-02 11:59:37 -04001028 trace_svc_noregister(progp->pg_name, version, proto,
1029 port, family, 0);
Trond Myklebust642ee6b2019-04-09 11:46:17 -04001030 return 0;
1031 }
1032
1033 /*
1034 * Don't register a UDP port if we need congestion
1035 * control.
1036 */
1037 if (vers->vs_need_cong_ctrl && proto == IPPROTO_UDP)
1038 return 0;
1039
1040 error = svc_rpcbind_set_version(net, progp, version,
1041 family, proto, port);
1042
1043 return (vers->vs_rpcb_optnl) ? 0 : error;
1044}
1045EXPORT_SYMBOL_GPL(svc_generic_rpcbind_set);
1046
Chuck Levera26cfad2008-08-18 19:34:16 -04001047/**
1048 * svc_register - register an RPC service with the local portmapper
1049 * @serv: svc_serv struct for the service to register
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001050 * @net: net namespace for the service to register
Chuck Lever4b62e582009-03-18 20:46:06 -04001051 * @family: protocol family of service's listener socket
Chuck Levera26cfad2008-08-18 19:34:16 -04001052 * @proto: transport protocol number to advertise
1053 * @port: port to advertise
1054 *
Chuck Lever4b62e582009-03-18 20:46:06 -04001055 * Service is registered for any address in the passed-in protocol family
Chuck Levera26cfad2008-08-18 19:34:16 -04001056 */
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001057int svc_register(const struct svc_serv *serv, struct net *net,
1058 const int family, const unsigned short proto,
1059 const unsigned short port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 struct svc_program *progp;
Chuck Leverea339d42007-10-26 13:32:56 -04001062 unsigned int i;
Chuck Lever14aeb212008-08-18 19:34:00 -04001063 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Weston Andros Adamson0af39502012-10-23 10:43:45 -04001065 WARN_ON_ONCE(proto == 0 && port == 0);
1066 if (proto == 0 && port == 0)
1067 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Olaf Kirchbc5fea42006-10-04 02:16:05 -07001069 for (progp = serv->sv_program; progp; progp = progp->pg_next) {
1070 for (i = 0; i < progp->pg_nvers; i++) {
Olaf Kirchbc5fea42006-10-04 02:16:05 -07001071
Trond Myklebust642ee6b2019-04-09 11:46:17 -04001072 error = progp->pg_rpcbind_set(net, progp, i,
1073 family, proto, port);
Kinglong Mee7e55b592013-12-31 13:17:20 +08001074 if (error < 0) {
1075 printk(KERN_WARNING "svc: failed to register "
1076 "%sv%u RPC service (errno %d).\n",
1077 progp->pg_name, i, -error);
Olaf Kirchbc5fea42006-10-04 02:16:05 -07001078 break;
Kinglong Mee7e55b592013-12-31 13:17:20 +08001079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081 }
1082
Chuck Lever7252d572008-08-18 19:34:08 -04001083 return error;
1084}
1085
Chuck Leverd5a86202009-03-18 20:47:29 -04001086/*
1087 * If user space is running rpcbind, it should take the v4 UNSET
1088 * and clear everything for this [program, version]. If user space
1089 * is running portmap, it will reject the v4 UNSET, but won't have
1090 * any "inet6" entries anyway. So a PMAP_UNSET should be sufficient
1091 * in this case to clear all existing entries for [program, version].
1092 */
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001093static void __svc_unregister(struct net *net, const u32 program, const u32 version,
Chuck Leverf6fb3f62008-09-25 11:56:57 -04001094 const char *progname)
1095{
1096 int error;
1097
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001098 error = rpcb_v4_register(net, program, version, NULL, "");
Chuck Leverd5a86202009-03-18 20:47:29 -04001099
1100 /*
1101 * User space didn't support rpcbind v4, so retry this
1102 * request with the legacy rpcbind v2 protocol.
1103 */
1104 if (error == -EPROTONOSUPPORT)
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001105 error = rpcb_register(net, program, version, 0, 0);
Chuck Leverd5a86202009-03-18 20:47:29 -04001106
Chuck Leverb4af5932020-05-02 11:59:37 -04001107 trace_svc_unregister(progname, version, error);
Chuck Leverf6fb3f62008-09-25 11:56:57 -04001108}
1109
Chuck Lever7252d572008-08-18 19:34:08 -04001110/*
Chuck Leverf6fb3f62008-09-25 11:56:57 -04001111 * All netids, bind addresses and ports registered for [program, version]
1112 * are removed from the local rpcbind database (if the service is not
1113 * hidden) to make way for a new instance of the service.
Chuck Lever7252d572008-08-18 19:34:08 -04001114 *
Chuck Leverf6fb3f62008-09-25 11:56:57 -04001115 * The result of unregistration is reported via dprintk for those who want
1116 * verification of the result, but is otherwise not important.
Chuck Lever7252d572008-08-18 19:34:08 -04001117 */
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001118static void svc_unregister(const struct svc_serv *serv, struct net *net)
Chuck Lever7252d572008-08-18 19:34:08 -04001119{
1120 struct svc_program *progp;
1121 unsigned long flags;
1122 unsigned int i;
Chuck Lever7252d572008-08-18 19:34:08 -04001123
1124 clear_thread_flag(TIF_SIGPENDING);
1125
1126 for (progp = serv->sv_program; progp; progp = progp->pg_next) {
1127 for (i = 0; i < progp->pg_nvers; i++) {
1128 if (progp->pg_vers[i] == NULL)
1129 continue;
1130 if (progp->pg_vers[i]->vs_hidden)
1131 continue;
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001132 __svc_unregister(net, progp->pg_prog, i, progp->pg_name);
Chuck Lever7252d572008-08-18 19:34:08 -04001133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135
Chuck Lever7252d572008-08-18 19:34:08 -04001136 spin_lock_irqsave(&current->sighand->siglock, flags);
1137 recalc_sigpending();
1138 spin_unlock_irqrestore(&current->sighand->siglock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
1141/*
J. Bruce Fields7032a3d2012-10-09 18:33:38 -04001142 * dprintk the given error with the address of the client that caused it.
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001143 */
Jeff Laytonf895b252014-11-17 16:58:04 -05001144#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Joe Perchesb9075fa2011-10-31 17:11:33 -07001145static __printf(2, 3)
Joe Perchese87cc472012-05-13 21:56:26 +00001146void svc_printk(struct svc_rqst *rqstp, const char *fmt, ...)
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001147{
Joe Perchese87cc472012-05-13 21:56:26 +00001148 struct va_format vaf;
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001149 va_list args;
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001150 char buf[RPC_MAX_ADDRBUFLEN];
1151
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001152 va_start(args, fmt);
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001153
Joe Perchese87cc472012-05-13 21:56:26 +00001154 vaf.fmt = fmt;
1155 vaf.va = &args;
1156
J. Bruce Fields7032a3d2012-10-09 18:33:38 -04001157 dprintk("svc: %s: %pV", svc_print_addr(rqstp, buf, sizeof(buf)), &vaf);
Joe Perchese87cc472012-05-13 21:56:26 +00001158
1159 va_end(args);
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001160}
J. Bruce Fields624ab462013-01-08 16:22:15 -05001161#else
1162static __printf(2,3) void svc_printk(struct svc_rqst *rqstp, const char *fmt, ...) {}
1163#endif
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001164
Trond Myklebust83dd59a2019-04-09 11:46:14 -04001165__be32
1166svc_return_autherr(struct svc_rqst *rqstp, __be32 auth_err)
1167{
1168 set_bit(RQ_AUTHERR, &rqstp->rq_flags);
1169 return auth_err;
1170}
1171EXPORT_SYMBOL_GPL(svc_return_autherr);
1172
1173static __be32
1174svc_get_autherr(struct svc_rqst *rqstp, __be32 *statp)
1175{
1176 if (test_and_clear_bit(RQ_AUTHERR, &rqstp->rq_flags))
1177 return *statp;
1178 return rpc_auth_ok;
1179}
1180
Trond Myklebust45326082019-04-09 11:46:16 -04001181static int
1182svc_generic_dispatch(struct svc_rqst *rqstp, __be32 *statp)
1183{
1184 struct kvec *argv = &rqstp->rq_arg.head[0];
1185 struct kvec *resv = &rqstp->rq_res.head[0];
1186 const struct svc_procedure *procp = rqstp->rq_procinfo;
1187
1188 /*
1189 * Decode arguments
1190 * XXX: why do we ignore the return value?
1191 */
1192 if (procp->pc_decode &&
1193 !procp->pc_decode(rqstp, argv->iov_base)) {
1194 *statp = rpc_garbage_args;
1195 return 1;
1196 }
1197
1198 *statp = procp->pc_func(rqstp);
1199
1200 if (*statp == rpc_drop_reply ||
1201 test_bit(RQ_DROPME, &rqstp->rq_flags))
1202 return 0;
1203
1204 if (test_bit(RQ_AUTHERR, &rqstp->rq_flags))
1205 return 1;
1206
1207 if (*statp != rpc_success)
1208 return 1;
1209
1210 /* Encode reply */
1211 if (procp->pc_encode &&
1212 !procp->pc_encode(rqstp, resv->iov_base + resv->iov_len)) {
1213 dprintk("svc: failed to encode reply\n");
1214 /* serv->sv_stats->rpcsystemerr++; */
1215 *statp = rpc_system_err;
1216 }
1217 return 1;
1218}
1219
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001220__be32
1221svc_generic_init_request(struct svc_rqst *rqstp,
1222 const struct svc_program *progp,
1223 struct svc_process_info *ret)
1224{
1225 const struct svc_version *versp = NULL; /* compiler food */
1226 const struct svc_procedure *procp = NULL;
1227
1228 if (rqstp->rq_vers >= progp->pg_nvers )
1229 goto err_bad_vers;
Colin Ian Kinge41f9ef2019-09-25 14:09:30 +01001230 versp = progp->pg_vers[rqstp->rq_vers];
1231 if (!versp)
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001232 goto err_bad_vers;
1233
1234 /*
1235 * Some protocol versions (namely NFSv4) require some form of
1236 * congestion control. (See RFC 7530 section 3.1 paragraph 2)
1237 * In other words, UDP is not allowed. We mark those when setting
1238 * up the svc_xprt, and verify that here.
1239 *
1240 * The spec is not very clear about what error should be returned
1241 * when someone tries to access a server that is listening on UDP
1242 * for lower versions. RPC_PROG_MISMATCH seems to be the closest
1243 * fit.
1244 */
1245 if (versp->vs_need_cong_ctrl && rqstp->rq_xprt &&
1246 !test_bit(XPT_CONG_CTRL, &rqstp->rq_xprt->xpt_flags))
1247 goto err_bad_vers;
1248
1249 if (rqstp->rq_proc >= versp->vs_nproc)
1250 goto err_bad_proc;
1251 rqstp->rq_procinfo = procp = &versp->vs_proc[rqstp->rq_proc];
1252 if (!procp)
1253 goto err_bad_proc;
1254
1255 /* Initialize storage for argp and resp */
1256 memset(rqstp->rq_argp, 0, procp->pc_argsize);
1257 memset(rqstp->rq_resp, 0, procp->pc_ressize);
1258
1259 /* Bump per-procedure stats counter */
1260 versp->vs_count[rqstp->rq_proc]++;
1261
1262 ret->dispatch = versp->vs_dispatch;
1263 return rpc_success;
1264err_bad_vers:
1265 ret->mismatch.lovers = progp->pg_lovers;
1266 ret->mismatch.hivers = progp->pg_hivers;
1267 return rpc_prog_mismatch;
1268err_bad_proc:
1269 return rpc_proc_unavail;
1270}
1271EXPORT_SYMBOL_GPL(svc_generic_init_request);
1272
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001273/*
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001274 * Common routine for processing the RPC request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 */
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001276static int
1277svc_process_common(struct svc_rqst *rqstp, struct kvec *argv, struct kvec *resv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
1279 struct svc_program *progp;
Christoph Hellwig860bda22017-05-12 16:11:49 +02001280 const struct svc_procedure *procp = NULL;
NeilBrown6fb2b472006-10-02 02:17:50 -07001281 struct svc_serv *serv = rqstp->rq_server;
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001282 struct svc_process_info process;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001283 __be32 *statp;
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001284 u32 prog, vers;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001285 __be32 auth_stat, rpc_stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 int auth_res;
J.Bruce Fields8f8e05c2006-10-04 02:16:08 -07001287 __be32 *reply_statp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 rpc_stat = rpc_success;
1290
1291 if (argv->iov_len < 6*4)
1292 goto err_short_len;
1293
Chuck Lever06eb8a52017-06-19 14:00:40 -04001294 /* Will be turned off by GSS integrity and privacy services */
Jeff Layton779fb0f2014-11-19 07:51:18 -05001295 set_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
Andy Adamson2f425872009-04-03 08:27:32 +03001296 /* Will be turned off only when NFSv4 Sessions are used */
Jeff Layton30660e04b2014-11-19 07:51:16 -05001297 set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
Jeff Layton78b65eb2014-11-19 07:51:17 -05001298 clear_bit(RQ_DROPME, &rqstp->rq_flags);
Tom Tuckere831fe62007-12-30 21:07:29 -06001299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 svc_putu32(resv, rqstp->rq_xid);
1301
Alexey Dobriyan76994312006-09-26 22:28:46 -07001302 vers = svc_getnl(argv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 /* First words of reply: */
Alexey Dobriyan76994312006-09-26 22:28:46 -07001305 svc_putnl(resv, 1); /* REPLY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (vers != 2) /* RPC version number */
1308 goto err_bad_rpc;
1309
1310 /* Save position in case we later decide to reject: */
J.Bruce Fields8f8e05c2006-10-04 02:16:08 -07001311 reply_statp = resv->iov_base + resv->iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Alexey Dobriyan76994312006-09-26 22:28:46 -07001313 svc_putnl(resv, 0); /* ACCEPT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Alexey Dobriyan76994312006-09-26 22:28:46 -07001315 rqstp->rq_prog = prog = svc_getnl(argv); /* program number */
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001316 rqstp->rq_vers = svc_getnl(argv); /* version number */
1317 rqstp->rq_proc = svc_getnl(argv); /* procedure number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
NeilBrown80d188a2005-11-07 01:00:27 -08001319 for (progp = serv->sv_program; progp; progp = progp->pg_next)
1320 if (prog == progp->pg_prog)
1321 break;
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 /*
1324 * Decode auth data, and add verifier to reply buffer.
1325 * We do this before anything else in order to get a decent
1326 * auth verifier.
1327 */
1328 auth_res = svc_authenticate(rqstp, &auth_stat);
1329 /* Also give the program a chance to reject this call: */
NeilBrown80d188a2005-11-07 01:00:27 -08001330 if (auth_res == SVC_OK && progp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 auth_stat = rpc_autherr_badcred;
1332 auth_res = progp->pg_authenticate(rqstp);
1333 }
Chuck Leverff27e9f2019-10-24 09:34:10 -04001334 if (auth_res != SVC_OK)
1335 trace_svc_authenticate(rqstp, auth_res, auth_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 switch (auth_res) {
1337 case SVC_OK:
1338 break;
1339 case SVC_GARBAGE:
Harshula Jayasuriyadd352102008-02-20 10:56:56 +11001340 goto err_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 case SVC_SYSERR:
1342 rpc_stat = rpc_system_err;
1343 goto err_bad;
1344 case SVC_DENIED:
1345 goto err_bad_auth;
NeilBrown1ebede82010-08-12 17:04:07 +10001346 case SVC_CLOSE:
Chuck Lever4d712ef2016-11-29 11:04:34 -05001347 goto close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 case SVC_DROP:
1349 goto dropit;
1350 case SVC_COMPLETE:
1351 goto sendit;
1352 }
NeilBrown80d188a2005-11-07 01:00:27 -08001353
Andreas Gruenbacher9ba02632005-06-22 17:16:24 +00001354 if (progp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 goto err_bad_prog;
1356
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001357 rpc_stat = progp->pg_init_request(rqstp, progp, &process);
1358 switch (rpc_stat) {
1359 case rpc_success:
1360 break;
1361 case rpc_prog_unavail:
1362 goto err_bad_prog;
1363 case rpc_prog_mismatch:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 goto err_bad_vers;
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001365 case rpc_proc_unavail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 goto err_bad_proc;
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001367 }
1368
1369 procp = rqstp->rq_procinfo;
1370 /* Should this check go into the dispatcher? */
1371 if (!procp || !procp->pc_func)
1372 goto err_bad_proc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 /* Syntactic check complete */
1375 serv->sv_stats->rpccnt++;
Chuck Lever0b9547b2018-03-27 10:51:22 -04001376 trace_svc_process(rqstp, progp->pg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 /* Build the reply header. */
1379 statp = resv->iov_base +resv->iov_len;
Alexey Dobriyan76994312006-09-26 22:28:46 -07001380 svc_putnl(resv, RPC_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001382 /* un-reserve some of the out-queue now that we have a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 * better idea of reply size
1384 */
1385 if (procp->pc_xdrressize)
Jeff Laytoncd1230122007-05-09 02:34:50 -07001386 svc_reserve_auth(rqstp, procp->pc_xdrressize<<2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 /* Call the function that processes the request. */
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001389 if (!process.dispatch) {
Trond Myklebust45326082019-04-09 11:46:16 -04001390 if (!svc_generic_dispatch(rqstp, statp))
1391 goto release_dropit;
1392 if (*statp == rpc_garbage_args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 goto err_garbage;
Trond Myklebust83dd59a2019-04-09 11:46:14 -04001394 auth_stat = svc_get_autherr(rqstp, statp);
1395 if (auth_stat != rpc_auth_ok)
1396 goto err_release_bad_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 } else {
1398 dprintk("svc: calling dispatcher\n");
Trond Myklebust45326082019-04-09 11:46:16 -04001399 if (!process.dispatch(rqstp, statp))
1400 goto release_dropit; /* Release reply info */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
1402
1403 /* Check RPC status result */
1404 if (*statp != rpc_success)
1405 resv->iov_len = ((void*)statp) - resv->iov_base + 4;
1406
1407 /* Release reply info */
1408 if (procp->pc_release)
Christoph Hellwig85374882017-05-08 18:48:24 +02001409 procp->pc_release(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 if (procp->pc_encode == NULL)
1412 goto dropit;
1413
1414 sendit:
1415 if (svc_authorise(rqstp))
Chuck Lever4d712ef2016-11-29 11:04:34 -05001416 goto close;
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001417 return 1; /* Caller can now send it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Trond Myklebust45326082019-04-09 11:46:16 -04001419release_dropit:
1420 if (procp->pc_release)
1421 procp->pc_release(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 dropit:
1423 svc_authorise(rqstp); /* doesn't hurt to call this twice */
1424 dprintk("svc: svc_process dropit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return 0;
1426
Chuck Lever4d712ef2016-11-29 11:04:34 -05001427 close:
Vasily Averind4b09ac2018-12-24 14:44:52 +03001428 if (rqstp->rq_xprt && test_bit(XPT_TEMP, &rqstp->rq_xprt->xpt_flags))
Chuck Lever4d712ef2016-11-29 11:04:34 -05001429 svc_close_xprt(rqstp->rq_xprt);
1430 dprintk("svc: svc_process close\n");
1431 return 0;
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433err_short_len:
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001434 svc_printk(rqstp, "short len %zd, dropping request\n",
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001435 argv->iov_len);
Chuck Lever4d712ef2016-11-29 11:04:34 -05001436 goto close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438err_bad_rpc:
1439 serv->sv_stats->rpcbadfmt++;
Alexey Dobriyan76994312006-09-26 22:28:46 -07001440 svc_putnl(resv, 1); /* REJECT */
1441 svc_putnl(resv, 0); /* RPC_MISMATCH */
1442 svc_putnl(resv, 2); /* Only RPCv2 supported */
1443 svc_putnl(resv, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 goto sendit;
1445
Trond Myklebust83dd59a2019-04-09 11:46:14 -04001446err_release_bad_auth:
1447 if (procp->pc_release)
1448 procp->pc_release(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449err_bad_auth:
1450 dprintk("svc: authentication failed (%d)\n", ntohl(auth_stat));
1451 serv->sv_stats->rpcbadauth++;
1452 /* Restore write pointer to location of accept status: */
J.Bruce Fields8f8e05c2006-10-04 02:16:08 -07001453 xdr_ressize_check(rqstp, reply_statp);
Alexey Dobriyan76994312006-09-26 22:28:46 -07001454 svc_putnl(resv, 1); /* REJECT */
1455 svc_putnl(resv, 1); /* AUTH_ERROR */
1456 svc_putnl(resv, ntohl(auth_stat)); /* status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 goto sendit;
1458
1459err_bad_prog:
Andreas Gruenbacher9ba02632005-06-22 17:16:24 +00001460 dprintk("svc: unknown program %d\n", prog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 serv->sv_stats->rpcbadfmt++;
Alexey Dobriyan76994312006-09-26 22:28:46 -07001462 svc_putnl(resv, RPC_PROG_UNAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 goto sendit;
1464
1465err_bad_vers:
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001466 svc_printk(rqstp, "unknown version (%d for prog %d, %s)\n",
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001467 rqstp->rq_vers, rqstp->rq_prog, progp->pg_name);
NeilBrown34e9a632007-01-29 13:19:52 -08001468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 serv->sv_stats->rpcbadfmt++;
Alexey Dobriyan76994312006-09-26 22:28:46 -07001470 svc_putnl(resv, RPC_PROG_MISMATCH);
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001471 svc_putnl(resv, process.mismatch.lovers);
1472 svc_putnl(resv, process.mismatch.hivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 goto sendit;
1474
1475err_bad_proc:
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001476 svc_printk(rqstp, "unknown procedure (%d)\n", rqstp->rq_proc);
NeilBrown34e9a632007-01-29 13:19:52 -08001477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 serv->sv_stats->rpcbadfmt++;
Alexey Dobriyan76994312006-09-26 22:28:46 -07001479 svc_putnl(resv, RPC_PROC_UNAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 goto sendit;
1481
1482err_garbage:
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001483 svc_printk(rqstp, "failed to decode args\n");
NeilBrown34e9a632007-01-29 13:19:52 -08001484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 rpc_stat = rpc_garbage_args;
1486err_bad:
1487 serv->sv_stats->rpcbadfmt++;
Alexey Dobriyan76994312006-09-26 22:28:46 -07001488 svc_putnl(resv, ntohl(rpc_stat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 goto sendit;
1490}
Greg Banks7adae482006-10-04 02:15:47 -07001491
1492/*
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001493 * Process the RPC request.
1494 */
1495int
1496svc_process(struct svc_rqst *rqstp)
1497{
1498 struct kvec *argv = &rqstp->rq_arg.head[0];
1499 struct kvec *resv = &rqstp->rq_res.head[0];
1500 struct svc_serv *serv = rqstp->rq_server;
1501 u32 dir;
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001502
1503 /*
1504 * Setup response xdr_buf.
1505 * Initially it has just one page
1506 */
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05001507 rqstp->rq_next_page = &rqstp->rq_respages[1];
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001508 resv->iov_base = page_address(rqstp->rq_respages[0]);
1509 resv->iov_len = 0;
1510 rqstp->rq_res.pages = rqstp->rq_respages + 1;
1511 rqstp->rq_res.len = 0;
1512 rqstp->rq_res.page_base = 0;
1513 rqstp->rq_res.page_len = 0;
1514 rqstp->rq_res.buflen = PAGE_SIZE;
1515 rqstp->rq_res.tail[0].iov_base = NULL;
1516 rqstp->rq_res.tail[0].iov_len = 0;
1517
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001518 dir = svc_getnl(argv);
1519 if (dir != 0) {
1520 /* direction != CALL */
1521 svc_printk(rqstp, "bad direction %d, dropping request\n", dir);
1522 serv->sv_stats->rpcbadfmt++;
Jeff Layton860a0d92014-10-28 14:24:12 -04001523 goto out_drop;
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001524 }
1525
Andy Adamson4b5b3ba2011-01-06 02:04:24 +00001526 /* Returns 1 for send, 0 for drop */
Chuck Lever0b9547b2018-03-27 10:51:22 -04001527 if (likely(svc_process_common(rqstp, argv, resv)))
1528 return svc_send(rqstp);
Jeff Layton860a0d92014-10-28 14:24:12 -04001529
Jeff Layton860a0d92014-10-28 14:24:12 -04001530out_drop:
Jeff Layton860a0d92014-10-28 14:24:12 -04001531 svc_drop(rqstp);
1532 return 0;
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001533}
Chuck Lever3f87d5d2015-05-26 13:48:57 -04001534EXPORT_SYMBOL_GPL(svc_process);
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001535
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001536#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga4d6bbb62009-04-01 09:23:07 -04001537/*
1538 * Process a backchannel RPC request that arrived over an existing
1539 * outbound connection
1540 */
1541int
1542bc_svc_process(struct svc_serv *serv, struct rpc_rqst *req,
1543 struct svc_rqst *rqstp)
1544{
1545 struct kvec *argv = &rqstp->rq_arg.head[0];
1546 struct kvec *resv = &rqstp->rq_res.head[0];
Chuck Lever632dda82015-05-11 14:04:50 -04001547 struct rpc_task *task;
Trond Myklebust0d2a9702015-06-04 15:37:10 -04001548 int proc_error;
Chuck Lever632dda82015-05-11 14:04:50 -04001549 int error;
1550
1551 dprintk("svc: %s(%p)\n", __func__, req);
Ricardo Labiaga4d6bbb62009-04-01 09:23:07 -04001552
1553 /* Build the svc_rqst used by the common processing routine */
1554 rqstp->rq_xid = req->rq_xid;
1555 rqstp->rq_prot = req->rq_xprt->prot;
1556 rqstp->rq_server = serv;
Vasily Averind4b09ac2018-12-24 14:44:52 +03001557 rqstp->rq_bc_net = req->rq_xprt->xprt_net;
Ricardo Labiaga4d6bbb62009-04-01 09:23:07 -04001558
1559 rqstp->rq_addrlen = sizeof(req->rq_xprt->addr);
1560 memcpy(&rqstp->rq_addr, &req->rq_xprt->addr, rqstp->rq_addrlen);
1561 memcpy(&rqstp->rq_arg, &req->rq_rcv_buf, sizeof(rqstp->rq_arg));
1562 memcpy(&rqstp->rq_res, &req->rq_snd_buf, sizeof(rqstp->rq_res));
Trond Myklebust756b9b32015-12-07 12:52:23 -08001563
1564 /* Adjust the argument buffer length */
Benjamin Coddington38b76312015-11-20 09:55:30 -05001565 rqstp->rq_arg.len = req->rq_private_buf.len;
Trond Myklebust756b9b32015-12-07 12:52:23 -08001566 if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
1567 rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
1568 rqstp->rq_arg.page_len = 0;
1569 } else if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len +
1570 rqstp->rq_arg.page_len)
1571 rqstp->rq_arg.page_len = rqstp->rq_arg.len -
1572 rqstp->rq_arg.head[0].iov_len;
1573 else
1574 rqstp->rq_arg.len = rqstp->rq_arg.head[0].iov_len +
1575 rqstp->rq_arg.page_len;
Ricardo Labiaga4d6bbb62009-04-01 09:23:07 -04001576
1577 /* reset result send buffer "put" position */
1578 resv->iov_len = 0;
1579
Ricardo Labiaga4d6bbb62009-04-01 09:23:07 -04001580 /*
1581 * Skip the next two words because they've already been
Chuck Lever632dda82015-05-11 14:04:50 -04001582 * processed in the transport
Ricardo Labiaga4d6bbb62009-04-01 09:23:07 -04001583 */
1584 svc_getu32(argv); /* XID */
1585 svc_getnl(argv); /* CALLDIR */
1586
Chuck Lever632dda82015-05-11 14:04:50 -04001587 /* Parse and execute the bc call */
Trond Myklebust0d2a9702015-06-04 15:37:10 -04001588 proc_error = svc_process_common(rqstp, argv, resv);
1589
Trond Myklebust7402a4f2019-07-16 13:51:29 -04001590 atomic_dec(&req->rq_xprt->bc_slot_count);
Trond Myklebust0d2a9702015-06-04 15:37:10 -04001591 if (!proc_error) {
Chuck Lever632dda82015-05-11 14:04:50 -04001592 /* Processing error: drop the request */
Trond Myklebustb3b02ae2012-05-31 15:26:38 -04001593 xprt_free_bc_request(req);
Vasily Averin8f7766c2018-12-24 14:46:08 +03001594 error = -EINVAL;
1595 goto out;
Andy Adamson4b5b3ba2011-01-06 02:04:24 +00001596 }
Chuck Lever632dda82015-05-11 14:04:50 -04001597 /* Finally, send the reply synchronously */
1598 memcpy(&req->rq_snd_buf, &rqstp->rq_res, sizeof(req->rq_snd_buf));
Trond Myklebust0f419792015-06-01 22:59:08 -04001599 task = rpc_run_bc_task(req);
Chuck Lever632dda82015-05-11 14:04:50 -04001600 if (IS_ERR(task)) {
1601 error = PTR_ERR(task);
1602 goto out;
1603 }
1604
1605 WARN_ON_ONCE(atomic_read(&task->tk_count) != 1);
1606 error = task->tk_status;
1607 rpc_put_task(task);
1608
1609out:
1610 dprintk("svc: %s(), error=%d\n", __func__, error);
1611 return error;
Ricardo Labiaga4d6bbb62009-04-01 09:23:07 -04001612}
Trond Myklebust0d961aa2011-07-13 19:24:15 -04001613EXPORT_SYMBOL_GPL(bc_svc_process);
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001614#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga4d6bbb62009-04-01 09:23:07 -04001615
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001616/*
Greg Banks7adae482006-10-04 02:15:47 -07001617 * Return (transport-specific) limit on the rpc payload.
1618 */
1619u32 svc_max_payload(const struct svc_rqst *rqstp)
1620{
Tom Tucker49023152007-12-30 21:07:21 -06001621 u32 max = rqstp->rq_xprt->xpt_class->xcl_max_payload;
Greg Banks7adae482006-10-04 02:15:47 -07001622
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001623 if (rqstp->rq_server->sv_max_payload < max)
1624 max = rqstp->rq_server->sv_max_payload;
Greg Banks7adae482006-10-04 02:15:47 -07001625 return max;
1626}
1627EXPORT_SYMBOL_GPL(svc_max_payload);
Chuck Lever8154ef22018-03-27 10:54:07 -04001628
1629/**
Chuck Lever03493bc2020-06-10 10:36:42 -04001630 * svc_encode_result_payload - mark a range of bytes as a result payload
Chuck Lever41205532020-03-02 14:45:53 -05001631 * @rqstp: svc_rqst to operate on
1632 * @offset: payload's byte offset in rqstp->rq_res
1633 * @length: size of payload, in bytes
1634 *
1635 * Returns zero on success, or a negative errno if a permanent
1636 * error occurred.
1637 */
Chuck Lever03493bc2020-06-10 10:36:42 -04001638int svc_encode_result_payload(struct svc_rqst *rqstp, unsigned int offset,
1639 unsigned int length)
Chuck Lever41205532020-03-02 14:45:53 -05001640{
Chuck Lever03493bc2020-06-10 10:36:42 -04001641 return rqstp->rq_xprt->xpt_ops->xpo_result_payload(rqstp, offset,
1642 length);
Chuck Lever41205532020-03-02 14:45:53 -05001643}
Chuck Lever03493bc2020-06-10 10:36:42 -04001644EXPORT_SYMBOL_GPL(svc_encode_result_payload);
Chuck Lever41205532020-03-02 14:45:53 -05001645
1646/**
Chuck Lever8154ef22018-03-27 10:54:07 -04001647 * svc_fill_write_vector - Construct data argument for VFS write call
1648 * @rqstp: svc_rqst to operate on
Chuck Lever3fd95572018-07-27 11:19:05 -04001649 * @pages: list of pages containing data payload
Chuck Lever8154ef22018-03-27 10:54:07 -04001650 * @first: buffer containing first section of write payload
1651 * @total: total number of bytes of write payload
1652 *
Chuck Lever3fd95572018-07-27 11:19:05 -04001653 * Fills in rqstp::rq_vec, and returns the number of elements.
Chuck Lever8154ef22018-03-27 10:54:07 -04001654 */
Chuck Lever3fd95572018-07-27 11:19:05 -04001655unsigned int svc_fill_write_vector(struct svc_rqst *rqstp, struct page **pages,
1656 struct kvec *first, size_t total)
Chuck Lever8154ef22018-03-27 10:54:07 -04001657{
1658 struct kvec *vec = rqstp->rq_vec;
Chuck Lever8154ef22018-03-27 10:54:07 -04001659 unsigned int i;
1660
1661 /* Some types of transport can present the write payload
1662 * entirely in rq_arg.pages. In this case, @first is empty.
1663 */
1664 i = 0;
1665 if (first->iov_len) {
1666 vec[i].iov_base = first->iov_base;
1667 vec[i].iov_len = min_t(size_t, total, first->iov_len);
1668 total -= vec[i].iov_len;
1669 ++i;
1670 }
1671
Chuck Lever8154ef22018-03-27 10:54:07 -04001672 while (total) {
1673 vec[i].iov_base = page_address(*pages);
1674 vec[i].iov_len = min_t(size_t, total, PAGE_SIZE);
1675 total -= vec[i].iov_len;
1676 ++i;
Chuck Lever8154ef22018-03-27 10:54:07 -04001677 ++pages;
1678 }
1679
1680 WARN_ON_ONCE(i > ARRAY_SIZE(rqstp->rq_vec));
1681 return i;
1682}
1683EXPORT_SYMBOL_GPL(svc_fill_write_vector);
Chuck Lever38a70312018-03-27 10:54:21 -04001684
1685/**
1686 * svc_fill_symlink_pathname - Construct pathname argument for VFS symlink call
1687 * @rqstp: svc_rqst to operate on
1688 * @first: buffer containing first section of pathname
Chuck Lever11b4d662018-07-27 11:19:10 -04001689 * @p: buffer containing remaining section of pathname
Chuck Lever38a70312018-03-27 10:54:21 -04001690 * @total: total length of the pathname argument
1691 *
Chuck Lever11b4d662018-07-27 11:19:10 -04001692 * The VFS symlink API demands a NUL-terminated pathname in mapped memory.
1693 * Returns pointer to a NUL-terminated string, or an ERR_PTR. Caller must free
1694 * the returned string.
Chuck Lever38a70312018-03-27 10:54:21 -04001695 */
1696char *svc_fill_symlink_pathname(struct svc_rqst *rqstp, struct kvec *first,
Chuck Lever11b4d662018-07-27 11:19:10 -04001697 void *p, size_t total)
Chuck Lever38a70312018-03-27 10:54:21 -04001698{
Chuck Lever11b4d662018-07-27 11:19:10 -04001699 size_t len, remaining;
1700 char *result, *dst;
Chuck Lever38a70312018-03-27 10:54:21 -04001701
Chuck Lever11b4d662018-07-27 11:19:10 -04001702 result = kmalloc(total + 1, GFP_KERNEL);
1703 if (!result)
1704 return ERR_PTR(-ESERVERFAULT);
Chuck Lever38a70312018-03-27 10:54:21 -04001705
Chuck Lever11b4d662018-07-27 11:19:10 -04001706 dst = result;
1707 remaining = total;
Chuck Lever38a70312018-03-27 10:54:21 -04001708
Chuck Lever11b4d662018-07-27 11:19:10 -04001709 len = min_t(size_t, total, first->iov_len);
1710 if (len) {
Chuck Lever38a70312018-03-27 10:54:21 -04001711 memcpy(dst, first->iov_base, len);
1712 dst += len;
1713 remaining -= len;
Chuck Lever38a70312018-03-27 10:54:21 -04001714 }
1715
Chuck Lever11b4d662018-07-27 11:19:10 -04001716 if (remaining) {
1717 len = min_t(size_t, remaining, PAGE_SIZE);
1718 memcpy(dst, p, len);
1719 dst += len;
1720 }
1721
1722 *dst = '\0';
1723
1724 /* Sanity check: Linux doesn't allow the pathname argument to
Chuck Lever38a70312018-03-27 10:54:21 -04001725 * contain a NUL byte.
1726 */
Chuck Lever11b4d662018-07-27 11:19:10 -04001727 if (strlen(result) != total) {
1728 kfree(result);
Chuck Lever38a70312018-03-27 10:54:21 -04001729 return ERR_PTR(-EINVAL);
Chuck Lever11b4d662018-07-27 11:19:10 -04001730 }
Chuck Lever38a70312018-03-27 10:54:21 -04001731 return result;
1732}
1733EXPORT_SYMBOL_GPL(svc_fill_symlink_pathname);