blob: d2d412d4382778a0a4d2186e553597160e0dca2d [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.
Chuck Lever4ff923c2021-01-03 14:37:23 -0500562 * We allocate pages and place them in rq_pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 */
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
Chuck Lever2f0f88f2021-07-01 10:03:10 -0400841/**
842 * svc_rqst_replace_page - Replace one page in rq_pages[]
843 * @rqstp: svc_rqst with pages to replace
844 * @page: replacement page
845 *
846 * When replacing a page in rq_pages, batch the release of the
847 * replaced pages to avoid hammering the page allocator.
848 */
849void svc_rqst_replace_page(struct svc_rqst *rqstp, struct page *page)
850{
851 if (*rqstp->rq_next_page) {
852 if (!pagevec_space(&rqstp->rq_pvec))
853 __pagevec_release(&rqstp->rq_pvec);
854 pagevec_add(&rqstp->rq_pvec, *rqstp->rq_next_page);
855 }
856
857 get_page(page);
858 *(rqstp->rq_next_page++) = page;
859}
860EXPORT_SYMBOL_GPL(svc_rqst_replace_page);
861
Greg Banksa7455442006-10-02 02:17:59 -0700862/*
Jeff Layton3c519912015-01-22 08:19:32 -0500863 * Called from a server thread as it's exiting. Caller must hold the "service
864 * mutex" for the service.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 */
866void
Jeff Layton1b6dc1d2015-06-08 12:11:10 -0700867svc_rqst_free(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 svc_release_buffer(rqstp);
Yunjian Wangb9f83ff2021-04-23 17:42:58 +0800870 if (rqstp->rq_scratch_page)
871 put_page(rqstp->rq_scratch_page);
Jesper Juhla51482b2005-11-08 09:41:34 -0800872 kfree(rqstp->rq_resp);
873 kfree(rqstp->rq_argp);
874 kfree(rqstp->rq_auth_data);
Jeff Layton1b6dc1d2015-06-08 12:11:10 -0700875 kfree_rcu(rqstp, rq_rcu_head);
876}
877EXPORT_SYMBOL_GPL(svc_rqst_free);
878
879void
880svc_exit_thread(struct svc_rqst *rqstp)
881{
882 struct svc_serv *serv = rqstp->rq_server;
883 struct svc_pool *pool = rqstp->rq_pool;
Greg Banks3262c812006-10-02 02:17:58 -0700884
885 spin_lock_bh(&pool->sp_lock);
886 pool->sp_nrthreads--;
Jeff Layton81244382014-11-21 14:19:28 -0500887 if (!test_and_set_bit(RQ_VICTIM, &rqstp->rq_flags))
888 list_del_rcu(&rqstp->rq_all);
Greg Banks3262c812006-10-02 02:17:58 -0700889 spin_unlock_bh(&pool->sp_lock);
890
Jeff Layton1b6dc1d2015-06-08 12:11:10 -0700891 svc_rqst_free(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 /* Release the server */
894 if (serv)
895 svc_destroy(serv);
896}
Trond Myklebust24c37672008-12-23 16:30:12 -0500897EXPORT_SYMBOL_GPL(svc_exit_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899/*
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500900 * Register an "inet" protocol family netid with the local
901 * rpcbind daemon via an rpcbind v4 SET request.
Chuck Levera26cfad2008-08-18 19:34:16 -0400902 *
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500903 * No netconfig infrastructure is available in the kernel, so
904 * we map IP_ protocol numbers to netids by hand.
Chuck Levera26cfad2008-08-18 19:34:16 -0400905 *
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500906 * Returns zero on success; a negative errno value is returned
907 * if any error occurs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 */
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +0400909static int __svc_rpcb_register4(struct net *net, const u32 program,
910 const u32 version,
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500911 const unsigned short protocol,
912 const unsigned short port)
Chuck Levera26cfad2008-08-18 19:34:16 -0400913{
Chuck Levercadc0fa2009-03-18 20:47:36 -0400914 const struct sockaddr_in sin = {
Chuck Levera26cfad2008-08-18 19:34:16 -0400915 .sin_family = AF_INET,
916 .sin_addr.s_addr = htonl(INADDR_ANY),
917 .sin_port = htons(port),
918 };
Chuck Levercadc0fa2009-03-18 20:47:36 -0400919 const char *netid;
920 int error;
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500921
922 switch (protocol) {
923 case IPPROTO_UDP:
924 netid = RPCBIND_NETID_UDP;
925 break;
926 case IPPROTO_TCP:
927 netid = RPCBIND_NETID_TCP;
928 break;
929 default:
Chuck Leverba5c35e2009-03-18 20:46:59 -0400930 return -ENOPROTOOPT;
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500931 }
932
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +0400933 error = rpcb_v4_register(net, program, version,
Chuck Levercadc0fa2009-03-18 20:47:36 -0400934 (const struct sockaddr *)&sin, netid);
935
936 /*
937 * User space didn't support rpcbind v4, so retry this
938 * registration request with the legacy rpcbind v2 protocol.
939 */
940 if (error == -EPROTONOSUPPORT)
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +0400941 error = rpcb_register(net, program, version, protocol, port);
Chuck Levercadc0fa2009-03-18 20:47:36 -0400942
943 return error;
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500944}
945
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000946#if IS_ENABLED(CONFIG_IPV6)
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500947/*
948 * Register an "inet6" protocol family netid with the local
949 * rpcbind daemon via an rpcbind v4 SET request.
950 *
951 * No netconfig infrastructure is available in the kernel, so
952 * we map IP_ protocol numbers to netids by hand.
953 *
954 * Returns zero on success; a negative errno value is returned
955 * if any error occurs.
956 */
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +0400957static int __svc_rpcb_register6(struct net *net, const u32 program,
958 const u32 version,
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500959 const unsigned short protocol,
960 const unsigned short port)
961{
Chuck Levercadc0fa2009-03-18 20:47:36 -0400962 const struct sockaddr_in6 sin6 = {
Chuck Levera26cfad2008-08-18 19:34:16 -0400963 .sin6_family = AF_INET6,
964 .sin6_addr = IN6ADDR_ANY_INIT,
965 .sin6_port = htons(port),
966 };
Chuck Levercadc0fa2009-03-18 20:47:36 -0400967 const char *netid;
968 int error;
Chuck Levera26cfad2008-08-18 19:34:16 -0400969
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500970 switch (protocol) {
971 case IPPROTO_UDP:
972 netid = RPCBIND_NETID_UDP6;
973 break;
974 case IPPROTO_TCP:
975 netid = RPCBIND_NETID_TCP6;
976 break;
977 default:
Chuck Leverba5c35e2009-03-18 20:46:59 -0400978 return -ENOPROTOOPT;
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500979 }
980
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +0400981 error = rpcb_v4_register(net, program, version,
Chuck Levercadc0fa2009-03-18 20:47:36 -0400982 (const struct sockaddr *)&sin6, netid);
983
984 /*
985 * User space didn't support rpcbind version 4, so we won't
986 * use a PF_INET6 listener.
987 */
988 if (error == -EPROTONOSUPPORT)
989 error = -EAFNOSUPPORT;
990
991 return error;
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500992}
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000993#endif /* IS_ENABLED(CONFIG_IPV6) */
Chuck Lever2c7eb0b2008-09-15 16:27:23 -0500994
995/*
996 * Register a kernel RPC service via rpcbind version 4.
997 *
998 * Returns zero on success; a negative errno value is returned
999 * if any error occurs.
1000 */
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001001static int __svc_register(struct net *net, const char *progname,
Chuck Lever363f7242009-03-18 20:47:44 -04001002 const u32 program, const u32 version,
Chuck Lever4b62e582009-03-18 20:46:06 -04001003 const int family,
Chuck Lever2c7eb0b2008-09-15 16:27:23 -05001004 const unsigned short protocol,
1005 const unsigned short port)
1006{
Chuck Lever363f7242009-03-18 20:47:44 -04001007 int error = -EAFNOSUPPORT;
Chuck Lever2c7eb0b2008-09-15 16:27:23 -05001008
Chuck Levera26cfad2008-08-18 19:34:16 -04001009 switch (family) {
Chuck Lever4b62e582009-03-18 20:46:06 -04001010 case PF_INET:
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001011 error = __svc_rpcb_register4(net, program, version,
Chuck Lever2c7eb0b2008-09-15 16:27:23 -05001012 protocol, port);
Chuck Levercadc0fa2009-03-18 20:47:36 -04001013 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001014#if IS_ENABLED(CONFIG_IPV6)
Chuck Lever4b62e582009-03-18 20:46:06 -04001015 case PF_INET6:
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001016 error = __svc_rpcb_register6(net, program, version,
Chuck Lever2c7eb0b2008-09-15 16:27:23 -05001017 protocol, port);
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001018#endif
Chuck Levera26cfad2008-08-18 19:34:16 -04001019 }
1020
Chuck Leverb4af5932020-05-02 11:59:37 -04001021 trace_svc_register(progname, version, protocol, port, family, error);
Chuck Lever363f7242009-03-18 20:47:44 -04001022 return error;
Chuck Levera26cfad2008-08-18 19:34:16 -04001023}
Chuck Lever2c7eb0b2008-09-15 16:27:23 -05001024
Trond Myklebust642ee6b2019-04-09 11:46:17 -04001025int svc_rpcbind_set_version(struct net *net,
1026 const struct svc_program *progp,
1027 u32 version, int family,
1028 unsigned short proto,
1029 unsigned short port)
1030{
Trond Myklebust642ee6b2019-04-09 11:46:17 -04001031 return __svc_register(net, progp->pg_name, progp->pg_prog,
1032 version, family, proto, port);
1033
1034}
1035EXPORT_SYMBOL_GPL(svc_rpcbind_set_version);
1036
1037int svc_generic_rpcbind_set(struct net *net,
1038 const struct svc_program *progp,
1039 u32 version, int family,
1040 unsigned short proto,
1041 unsigned short port)
1042{
1043 const struct svc_version *vers = progp->pg_vers[version];
1044 int error;
1045
1046 if (vers == NULL)
1047 return 0;
1048
1049 if (vers->vs_hidden) {
Chuck Leverb4af5932020-05-02 11:59:37 -04001050 trace_svc_noregister(progp->pg_name, version, proto,
1051 port, family, 0);
Trond Myklebust642ee6b2019-04-09 11:46:17 -04001052 return 0;
1053 }
1054
1055 /*
1056 * Don't register a UDP port if we need congestion
1057 * control.
1058 */
1059 if (vers->vs_need_cong_ctrl && proto == IPPROTO_UDP)
1060 return 0;
1061
1062 error = svc_rpcbind_set_version(net, progp, version,
1063 family, proto, port);
1064
1065 return (vers->vs_rpcb_optnl) ? 0 : error;
1066}
1067EXPORT_SYMBOL_GPL(svc_generic_rpcbind_set);
1068
Chuck Levera26cfad2008-08-18 19:34:16 -04001069/**
1070 * svc_register - register an RPC service with the local portmapper
1071 * @serv: svc_serv struct for the service to register
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001072 * @net: net namespace for the service to register
Chuck Lever4b62e582009-03-18 20:46:06 -04001073 * @family: protocol family of service's listener socket
Chuck Levera26cfad2008-08-18 19:34:16 -04001074 * @proto: transport protocol number to advertise
1075 * @port: port to advertise
1076 *
Chuck Lever4b62e582009-03-18 20:46:06 -04001077 * Service is registered for any address in the passed-in protocol family
Chuck Levera26cfad2008-08-18 19:34:16 -04001078 */
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001079int svc_register(const struct svc_serv *serv, struct net *net,
1080 const int family, const unsigned short proto,
1081 const unsigned short port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 struct svc_program *progp;
Chuck Leverea339d42007-10-26 13:32:56 -04001084 unsigned int i;
Chuck Lever14aeb212008-08-18 19:34:00 -04001085 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Weston Andros Adamson0af39502012-10-23 10:43:45 -04001087 WARN_ON_ONCE(proto == 0 && port == 0);
1088 if (proto == 0 && port == 0)
1089 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Olaf Kirchbc5fea42006-10-04 02:16:05 -07001091 for (progp = serv->sv_program; progp; progp = progp->pg_next) {
1092 for (i = 0; i < progp->pg_nvers; i++) {
Olaf Kirchbc5fea42006-10-04 02:16:05 -07001093
Trond Myklebust642ee6b2019-04-09 11:46:17 -04001094 error = progp->pg_rpcbind_set(net, progp, i,
1095 family, proto, port);
Kinglong Mee7e55b592013-12-31 13:17:20 +08001096 if (error < 0) {
1097 printk(KERN_WARNING "svc: failed to register "
1098 "%sv%u RPC service (errno %d).\n",
1099 progp->pg_name, i, -error);
Olaf Kirchbc5fea42006-10-04 02:16:05 -07001100 break;
Kinglong Mee7e55b592013-12-31 13:17:20 +08001101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
1103 }
1104
Chuck Lever7252d572008-08-18 19:34:08 -04001105 return error;
1106}
1107
Chuck Leverd5a86202009-03-18 20:47:29 -04001108/*
1109 * If user space is running rpcbind, it should take the v4 UNSET
1110 * and clear everything for this [program, version]. If user space
1111 * is running portmap, it will reject the v4 UNSET, but won't have
1112 * any "inet6" entries anyway. So a PMAP_UNSET should be sufficient
1113 * in this case to clear all existing entries for [program, version].
1114 */
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001115static void __svc_unregister(struct net *net, const u32 program, const u32 version,
Chuck Leverf6fb3f62008-09-25 11:56:57 -04001116 const char *progname)
1117{
1118 int error;
1119
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001120 error = rpcb_v4_register(net, program, version, NULL, "");
Chuck Leverd5a86202009-03-18 20:47:29 -04001121
1122 /*
1123 * User space didn't support rpcbind v4, so retry this
1124 * request with the legacy rpcbind v2 protocol.
1125 */
1126 if (error == -EPROTONOSUPPORT)
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001127 error = rpcb_register(net, program, version, 0, 0);
Chuck Leverd5a86202009-03-18 20:47:29 -04001128
Chuck Leverb4af5932020-05-02 11:59:37 -04001129 trace_svc_unregister(progname, version, error);
Chuck Leverf6fb3f62008-09-25 11:56:57 -04001130}
1131
Chuck Lever7252d572008-08-18 19:34:08 -04001132/*
Chuck Leverf6fb3f62008-09-25 11:56:57 -04001133 * All netids, bind addresses and ports registered for [program, version]
1134 * are removed from the local rpcbind database (if the service is not
1135 * hidden) to make way for a new instance of the service.
Chuck Lever7252d572008-08-18 19:34:08 -04001136 *
Chuck Leverf6fb3f62008-09-25 11:56:57 -04001137 * The result of unregistration is reported via dprintk for those who want
1138 * verification of the result, but is otherwise not important.
Chuck Lever7252d572008-08-18 19:34:08 -04001139 */
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001140static void svc_unregister(const struct svc_serv *serv, struct net *net)
Chuck Lever7252d572008-08-18 19:34:08 -04001141{
1142 struct svc_program *progp;
1143 unsigned long flags;
1144 unsigned int i;
Chuck Lever7252d572008-08-18 19:34:08 -04001145
1146 clear_thread_flag(TIF_SIGPENDING);
1147
1148 for (progp = serv->sv_program; progp; progp = progp->pg_next) {
1149 for (i = 0; i < progp->pg_nvers; i++) {
1150 if (progp->pg_vers[i] == NULL)
1151 continue;
1152 if (progp->pg_vers[i]->vs_hidden)
1153 continue;
Stanislav Kinsbursky5247fab2012-01-13 14:02:48 +04001154 __svc_unregister(net, progp->pg_prog, i, progp->pg_name);
Chuck Lever7252d572008-08-18 19:34:08 -04001155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
1157
Chuck Lever7252d572008-08-18 19:34:08 -04001158 spin_lock_irqsave(&current->sighand->siglock, flags);
1159 recalc_sigpending();
1160 spin_unlock_irqrestore(&current->sighand->siglock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
1163/*
J. Bruce Fields7032a3d2012-10-09 18:33:38 -04001164 * dprintk the given error with the address of the client that caused it.
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001165 */
Jeff Laytonf895b252014-11-17 16:58:04 -05001166#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Joe Perchesb9075fa2011-10-31 17:11:33 -07001167static __printf(2, 3)
Joe Perchese87cc472012-05-13 21:56:26 +00001168void svc_printk(struct svc_rqst *rqstp, const char *fmt, ...)
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001169{
Joe Perchese87cc472012-05-13 21:56:26 +00001170 struct va_format vaf;
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001171 va_list args;
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001172 char buf[RPC_MAX_ADDRBUFLEN];
1173
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001174 va_start(args, fmt);
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001175
Joe Perchese87cc472012-05-13 21:56:26 +00001176 vaf.fmt = fmt;
1177 vaf.va = &args;
1178
J. Bruce Fields7032a3d2012-10-09 18:33:38 -04001179 dprintk("svc: %s: %pV", svc_print_addr(rqstp, buf, sizeof(buf)), &vaf);
Joe Perchese87cc472012-05-13 21:56:26 +00001180
1181 va_end(args);
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001182}
J. Bruce Fields624ab462013-01-08 16:22:15 -05001183#else
1184static __printf(2,3) void svc_printk(struct svc_rqst *rqstp, const char *fmt, ...) {}
1185#endif
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001186
Trond Myklebust83dd59a2019-04-09 11:46:14 -04001187__be32
1188svc_return_autherr(struct svc_rqst *rqstp, __be32 auth_err)
1189{
1190 set_bit(RQ_AUTHERR, &rqstp->rq_flags);
1191 return auth_err;
1192}
1193EXPORT_SYMBOL_GPL(svc_return_autherr);
1194
1195static __be32
1196svc_get_autherr(struct svc_rqst *rqstp, __be32 *statp)
1197{
1198 if (test_and_clear_bit(RQ_AUTHERR, &rqstp->rq_flags))
1199 return *statp;
1200 return rpc_auth_ok;
1201}
1202
Trond Myklebust45326082019-04-09 11:46:16 -04001203static int
1204svc_generic_dispatch(struct svc_rqst *rqstp, __be32 *statp)
1205{
1206 struct kvec *argv = &rqstp->rq_arg.head[0];
1207 struct kvec *resv = &rqstp->rq_res.head[0];
1208 const struct svc_procedure *procp = rqstp->rq_procinfo;
1209
1210 /*
1211 * Decode arguments
1212 * XXX: why do we ignore the return value?
1213 */
1214 if (procp->pc_decode &&
1215 !procp->pc_decode(rqstp, argv->iov_base)) {
1216 *statp = rpc_garbage_args;
1217 return 1;
1218 }
1219
1220 *statp = procp->pc_func(rqstp);
1221
1222 if (*statp == rpc_drop_reply ||
1223 test_bit(RQ_DROPME, &rqstp->rq_flags))
1224 return 0;
1225
1226 if (test_bit(RQ_AUTHERR, &rqstp->rq_flags))
1227 return 1;
1228
1229 if (*statp != rpc_success)
1230 return 1;
1231
1232 /* Encode reply */
1233 if (procp->pc_encode &&
1234 !procp->pc_encode(rqstp, resv->iov_base + resv->iov_len)) {
1235 dprintk("svc: failed to encode reply\n");
1236 /* serv->sv_stats->rpcsystemerr++; */
1237 *statp = rpc_system_err;
1238 }
1239 return 1;
1240}
1241
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001242__be32
1243svc_generic_init_request(struct svc_rqst *rqstp,
1244 const struct svc_program *progp,
1245 struct svc_process_info *ret)
1246{
1247 const struct svc_version *versp = NULL; /* compiler food */
1248 const struct svc_procedure *procp = NULL;
1249
1250 if (rqstp->rq_vers >= progp->pg_nvers )
1251 goto err_bad_vers;
Colin Ian Kinge41f9ef2019-09-25 14:09:30 +01001252 versp = progp->pg_vers[rqstp->rq_vers];
1253 if (!versp)
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001254 goto err_bad_vers;
1255
1256 /*
1257 * Some protocol versions (namely NFSv4) require some form of
1258 * congestion control. (See RFC 7530 section 3.1 paragraph 2)
1259 * In other words, UDP is not allowed. We mark those when setting
1260 * up the svc_xprt, and verify that here.
1261 *
1262 * The spec is not very clear about what error should be returned
1263 * when someone tries to access a server that is listening on UDP
1264 * for lower versions. RPC_PROG_MISMATCH seems to be the closest
1265 * fit.
1266 */
1267 if (versp->vs_need_cong_ctrl && rqstp->rq_xprt &&
1268 !test_bit(XPT_CONG_CTRL, &rqstp->rq_xprt->xpt_flags))
1269 goto err_bad_vers;
1270
1271 if (rqstp->rq_proc >= versp->vs_nproc)
1272 goto err_bad_proc;
1273 rqstp->rq_procinfo = procp = &versp->vs_proc[rqstp->rq_proc];
1274 if (!procp)
1275 goto err_bad_proc;
1276
1277 /* Initialize storage for argp and resp */
1278 memset(rqstp->rq_argp, 0, procp->pc_argsize);
1279 memset(rqstp->rq_resp, 0, procp->pc_ressize);
1280
1281 /* Bump per-procedure stats counter */
1282 versp->vs_count[rqstp->rq_proc]++;
1283
1284 ret->dispatch = versp->vs_dispatch;
1285 return rpc_success;
1286err_bad_vers:
1287 ret->mismatch.lovers = progp->pg_lovers;
1288 ret->mismatch.hivers = progp->pg_hivers;
1289 return rpc_prog_mismatch;
1290err_bad_proc:
1291 return rpc_proc_unavail;
1292}
1293EXPORT_SYMBOL_GPL(svc_generic_init_request);
1294
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001295/*
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001296 * Common routine for processing the RPC request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 */
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001298static int
1299svc_process_common(struct svc_rqst *rqstp, struct kvec *argv, struct kvec *resv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 struct svc_program *progp;
Christoph Hellwig860bda22017-05-12 16:11:49 +02001302 const struct svc_procedure *procp = NULL;
NeilBrown6fb2b472006-10-02 02:17:50 -07001303 struct svc_serv *serv = rqstp->rq_server;
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001304 struct svc_process_info process;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001305 __be32 *statp;
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001306 u32 prog, vers;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001307 __be32 auth_stat, rpc_stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 int auth_res;
J.Bruce Fields8f8e05c2006-10-04 02:16:08 -07001309 __be32 *reply_statp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 rpc_stat = rpc_success;
1312
1313 if (argv->iov_len < 6*4)
1314 goto err_short_len;
1315
Chuck Lever06eb8a52017-06-19 14:00:40 -04001316 /* Will be turned off by GSS integrity and privacy services */
Jeff Layton779fb0f2014-11-19 07:51:18 -05001317 set_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
Andy Adamson2f425872009-04-03 08:27:32 +03001318 /* Will be turned off only when NFSv4 Sessions are used */
Jeff Layton30660e04b2014-11-19 07:51:16 -05001319 set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags);
Jeff Layton78b65eb2014-11-19 07:51:17 -05001320 clear_bit(RQ_DROPME, &rqstp->rq_flags);
Tom Tuckere831fe62007-12-30 21:07:29 -06001321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 svc_putu32(resv, rqstp->rq_xid);
1323
Alexey Dobriyan76994312006-09-26 22:28:46 -07001324 vers = svc_getnl(argv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 /* First words of reply: */
Alexey Dobriyan76994312006-09-26 22:28:46 -07001327 svc_putnl(resv, 1); /* REPLY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (vers != 2) /* RPC version number */
1330 goto err_bad_rpc;
1331
1332 /* Save position in case we later decide to reject: */
J.Bruce Fields8f8e05c2006-10-04 02:16:08 -07001333 reply_statp = resv->iov_base + resv->iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Alexey Dobriyan76994312006-09-26 22:28:46 -07001335 svc_putnl(resv, 0); /* ACCEPT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Alexey Dobriyan76994312006-09-26 22:28:46 -07001337 rqstp->rq_prog = prog = svc_getnl(argv); /* program number */
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001338 rqstp->rq_vers = svc_getnl(argv); /* version number */
1339 rqstp->rq_proc = svc_getnl(argv); /* procedure number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
NeilBrown80d188a2005-11-07 01:00:27 -08001341 for (progp = serv->sv_program; progp; progp = progp->pg_next)
1342 if (prog == progp->pg_prog)
1343 break;
1344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 /*
1346 * Decode auth data, and add verifier to reply buffer.
1347 * We do this before anything else in order to get a decent
1348 * auth verifier.
1349 */
1350 auth_res = svc_authenticate(rqstp, &auth_stat);
1351 /* Also give the program a chance to reject this call: */
NeilBrown80d188a2005-11-07 01:00:27 -08001352 if (auth_res == SVC_OK && progp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 auth_stat = rpc_autherr_badcred;
1354 auth_res = progp->pg_authenticate(rqstp);
1355 }
Chuck Leverff27e9f2019-10-24 09:34:10 -04001356 if (auth_res != SVC_OK)
1357 trace_svc_authenticate(rqstp, auth_res, auth_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 switch (auth_res) {
1359 case SVC_OK:
1360 break;
1361 case SVC_GARBAGE:
Harshula Jayasuriyadd352102008-02-20 10:56:56 +11001362 goto err_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 case SVC_SYSERR:
1364 rpc_stat = rpc_system_err;
1365 goto err_bad;
1366 case SVC_DENIED:
1367 goto err_bad_auth;
NeilBrown1ebede82010-08-12 17:04:07 +10001368 case SVC_CLOSE:
Chuck Lever4d712ef2016-11-29 11:04:34 -05001369 goto close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 case SVC_DROP:
1371 goto dropit;
1372 case SVC_COMPLETE:
1373 goto sendit;
1374 }
NeilBrown80d188a2005-11-07 01:00:27 -08001375
Andreas Gruenbacher9ba02632005-06-22 17:16:24 +00001376 if (progp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 goto err_bad_prog;
1378
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001379 rpc_stat = progp->pg_init_request(rqstp, progp, &process);
1380 switch (rpc_stat) {
1381 case rpc_success:
1382 break;
1383 case rpc_prog_unavail:
1384 goto err_bad_prog;
1385 case rpc_prog_mismatch:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 goto err_bad_vers;
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001387 case rpc_proc_unavail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 goto err_bad_proc;
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001389 }
1390
1391 procp = rqstp->rq_procinfo;
1392 /* Should this check go into the dispatcher? */
1393 if (!procp || !procp->pc_func)
1394 goto err_bad_proc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 /* Syntactic check complete */
1397 serv->sv_stats->rpccnt++;
Chuck Lever0b9547b2018-03-27 10:51:22 -04001398 trace_svc_process(rqstp, progp->pg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 /* Build the reply header. */
1401 statp = resv->iov_base +resv->iov_len;
Alexey Dobriyan76994312006-09-26 22:28:46 -07001402 svc_putnl(resv, RPC_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001404 /* un-reserve some of the out-queue now that we have a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 * better idea of reply size
1406 */
1407 if (procp->pc_xdrressize)
Jeff Laytoncd1230122007-05-09 02:34:50 -07001408 svc_reserve_auth(rqstp, procp->pc_xdrressize<<2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 /* Call the function that processes the request. */
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001411 if (!process.dispatch) {
Trond Myklebust45326082019-04-09 11:46:16 -04001412 if (!svc_generic_dispatch(rqstp, statp))
1413 goto release_dropit;
1414 if (*statp == rpc_garbage_args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 goto err_garbage;
Trond Myklebust83dd59a2019-04-09 11:46:14 -04001416 auth_stat = svc_get_autherr(rqstp, statp);
1417 if (auth_stat != rpc_auth_ok)
1418 goto err_release_bad_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 } else {
1420 dprintk("svc: calling dispatcher\n");
Trond Myklebust45326082019-04-09 11:46:16 -04001421 if (!process.dispatch(rqstp, statp))
1422 goto release_dropit; /* Release reply info */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
1424
1425 /* Check RPC status result */
1426 if (*statp != rpc_success)
1427 resv->iov_len = ((void*)statp) - resv->iov_base + 4;
1428
1429 /* Release reply info */
1430 if (procp->pc_release)
Christoph Hellwig85374882017-05-08 18:48:24 +02001431 procp->pc_release(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 if (procp->pc_encode == NULL)
1434 goto dropit;
1435
1436 sendit:
1437 if (svc_authorise(rqstp))
Daniel Kobrasf1442d632021-02-27 00:04:37 +01001438 goto close_xprt;
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001439 return 1; /* Caller can now send it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Trond Myklebust45326082019-04-09 11:46:16 -04001441release_dropit:
1442 if (procp->pc_release)
1443 procp->pc_release(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 dropit:
1445 svc_authorise(rqstp); /* doesn't hurt to call this twice */
1446 dprintk("svc: svc_process dropit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return 0;
1448
Chuck Lever4d712ef2016-11-29 11:04:34 -05001449 close:
Daniel Kobrasf1442d632021-02-27 00:04:37 +01001450 svc_authorise(rqstp);
1451close_xprt:
Vasily Averind4b09ac2018-12-24 14:44:52 +03001452 if (rqstp->rq_xprt && test_bit(XPT_TEMP, &rqstp->rq_xprt->xpt_flags))
Chuck Lever4d712ef2016-11-29 11:04:34 -05001453 svc_close_xprt(rqstp->rq_xprt);
1454 dprintk("svc: svc_process close\n");
1455 return 0;
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457err_short_len:
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001458 svc_printk(rqstp, "short len %zd, dropping request\n",
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001459 argv->iov_len);
Daniel Kobrasf1442d632021-02-27 00:04:37 +01001460 goto close_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462err_bad_rpc:
1463 serv->sv_stats->rpcbadfmt++;
Alexey Dobriyan76994312006-09-26 22:28:46 -07001464 svc_putnl(resv, 1); /* REJECT */
1465 svc_putnl(resv, 0); /* RPC_MISMATCH */
1466 svc_putnl(resv, 2); /* Only RPCv2 supported */
1467 svc_putnl(resv, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 goto sendit;
1469
Trond Myklebust83dd59a2019-04-09 11:46:14 -04001470err_release_bad_auth:
1471 if (procp->pc_release)
1472 procp->pc_release(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473err_bad_auth:
1474 dprintk("svc: authentication failed (%d)\n", ntohl(auth_stat));
1475 serv->sv_stats->rpcbadauth++;
1476 /* Restore write pointer to location of accept status: */
J.Bruce Fields8f8e05c2006-10-04 02:16:08 -07001477 xdr_ressize_check(rqstp, reply_statp);
Alexey Dobriyan76994312006-09-26 22:28:46 -07001478 svc_putnl(resv, 1); /* REJECT */
1479 svc_putnl(resv, 1); /* AUTH_ERROR */
1480 svc_putnl(resv, ntohl(auth_stat)); /* status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 goto sendit;
1482
1483err_bad_prog:
Andreas Gruenbacher9ba02632005-06-22 17:16:24 +00001484 dprintk("svc: unknown program %d\n", prog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 serv->sv_stats->rpcbadfmt++;
Alexey Dobriyan76994312006-09-26 22:28:46 -07001486 svc_putnl(resv, RPC_PROG_UNAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 goto sendit;
1488
1489err_bad_vers:
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001490 svc_printk(rqstp, "unknown version (%d for prog %d, %s)\n",
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001491 rqstp->rq_vers, rqstp->rq_prog, progp->pg_name);
NeilBrown34e9a632007-01-29 13:19:52 -08001492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 serv->sv_stats->rpcbadfmt++;
Alexey Dobriyan76994312006-09-26 22:28:46 -07001494 svc_putnl(resv, RPC_PROG_MISMATCH);
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001495 svc_putnl(resv, process.mismatch.lovers);
1496 svc_putnl(resv, process.mismatch.hivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 goto sendit;
1498
1499err_bad_proc:
Trond Myklebust8e5b6772019-04-09 11:46:15 -04001500 svc_printk(rqstp, "unknown procedure (%d)\n", rqstp->rq_proc);
NeilBrown34e9a632007-01-29 13:19:52 -08001501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 serv->sv_stats->rpcbadfmt++;
Alexey Dobriyan76994312006-09-26 22:28:46 -07001503 svc_putnl(resv, RPC_PROC_UNAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 goto sendit;
1505
1506err_garbage:
Dr. David Alan Gilbert354ecbb2007-08-25 16:09:27 +01001507 svc_printk(rqstp, "failed to decode args\n");
NeilBrown34e9a632007-01-29 13:19:52 -08001508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 rpc_stat = rpc_garbage_args;
1510err_bad:
1511 serv->sv_stats->rpcbadfmt++;
Alexey Dobriyan76994312006-09-26 22:28:46 -07001512 svc_putnl(resv, ntohl(rpc_stat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 goto sendit;
1514}
Greg Banks7adae482006-10-04 02:15:47 -07001515
1516/*
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001517 * Process the RPC request.
1518 */
1519int
1520svc_process(struct svc_rqst *rqstp)
1521{
1522 struct kvec *argv = &rqstp->rq_arg.head[0];
1523 struct kvec *resv = &rqstp->rq_res.head[0];
1524 struct svc_serv *serv = rqstp->rq_server;
1525 u32 dir;
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001526
1527 /*
1528 * Setup response xdr_buf.
1529 * Initially it has just one page
1530 */
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05001531 rqstp->rq_next_page = &rqstp->rq_respages[1];
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001532 resv->iov_base = page_address(rqstp->rq_respages[0]);
1533 resv->iov_len = 0;
1534 rqstp->rq_res.pages = rqstp->rq_respages + 1;
1535 rqstp->rq_res.len = 0;
1536 rqstp->rq_res.page_base = 0;
1537 rqstp->rq_res.page_len = 0;
1538 rqstp->rq_res.buflen = PAGE_SIZE;
1539 rqstp->rq_res.tail[0].iov_base = NULL;
1540 rqstp->rq_res.tail[0].iov_len = 0;
1541
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001542 dir = svc_getnl(argv);
1543 if (dir != 0) {
1544 /* direction != CALL */
1545 svc_printk(rqstp, "bad direction %d, dropping request\n", dir);
1546 serv->sv_stats->rpcbadfmt++;
Jeff Layton860a0d92014-10-28 14:24:12 -04001547 goto out_drop;
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001548 }
1549
Andy Adamson4b5b3ba2011-01-06 02:04:24 +00001550 /* Returns 1 for send, 0 for drop */
Chuck Lever0b9547b2018-03-27 10:51:22 -04001551 if (likely(svc_process_common(rqstp, argv, resv)))
1552 return svc_send(rqstp);
Jeff Layton860a0d92014-10-28 14:24:12 -04001553
Jeff Layton860a0d92014-10-28 14:24:12 -04001554out_drop:
Jeff Layton860a0d92014-10-28 14:24:12 -04001555 svc_drop(rqstp);
1556 return 0;
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001557}
Chuck Lever3f87d5d2015-05-26 13:48:57 -04001558EXPORT_SYMBOL_GPL(svc_process);
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001559
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001560#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga4d6bbb62009-04-01 09:23:07 -04001561/*
1562 * Process a backchannel RPC request that arrived over an existing
1563 * outbound connection
1564 */
1565int
1566bc_svc_process(struct svc_serv *serv, struct rpc_rqst *req,
1567 struct svc_rqst *rqstp)
1568{
1569 struct kvec *argv = &rqstp->rq_arg.head[0];
1570 struct kvec *resv = &rqstp->rq_res.head[0];
Chuck Lever632dda82015-05-11 14:04:50 -04001571 struct rpc_task *task;
Trond Myklebust0d2a9702015-06-04 15:37:10 -04001572 int proc_error;
Chuck Lever632dda82015-05-11 14:04:50 -04001573 int error;
1574
1575 dprintk("svc: %s(%p)\n", __func__, req);
Ricardo Labiaga4d6bbb62009-04-01 09:23:07 -04001576
1577 /* Build the svc_rqst used by the common processing routine */
1578 rqstp->rq_xid = req->rq_xid;
1579 rqstp->rq_prot = req->rq_xprt->prot;
1580 rqstp->rq_server = serv;
Vasily Averind4b09ac2018-12-24 14:44:52 +03001581 rqstp->rq_bc_net = req->rq_xprt->xprt_net;
Ricardo Labiaga4d6bbb62009-04-01 09:23:07 -04001582
1583 rqstp->rq_addrlen = sizeof(req->rq_xprt->addr);
1584 memcpy(&rqstp->rq_addr, &req->rq_xprt->addr, rqstp->rq_addrlen);
1585 memcpy(&rqstp->rq_arg, &req->rq_rcv_buf, sizeof(rqstp->rq_arg));
1586 memcpy(&rqstp->rq_res, &req->rq_snd_buf, sizeof(rqstp->rq_res));
Trond Myklebust756b9b32015-12-07 12:52:23 -08001587
1588 /* Adjust the argument buffer length */
Benjamin Coddington38b76312015-11-20 09:55:30 -05001589 rqstp->rq_arg.len = req->rq_private_buf.len;
Trond Myklebust756b9b32015-12-07 12:52:23 -08001590 if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) {
1591 rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len;
1592 rqstp->rq_arg.page_len = 0;
1593 } else if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len +
1594 rqstp->rq_arg.page_len)
1595 rqstp->rq_arg.page_len = rqstp->rq_arg.len -
1596 rqstp->rq_arg.head[0].iov_len;
1597 else
1598 rqstp->rq_arg.len = rqstp->rq_arg.head[0].iov_len +
1599 rqstp->rq_arg.page_len;
Ricardo Labiaga4d6bbb62009-04-01 09:23:07 -04001600
1601 /* reset result send buffer "put" position */
1602 resv->iov_len = 0;
1603
Ricardo Labiaga4d6bbb62009-04-01 09:23:07 -04001604 /*
1605 * Skip the next two words because they've already been
Chuck Lever632dda82015-05-11 14:04:50 -04001606 * processed in the transport
Ricardo Labiaga4d6bbb62009-04-01 09:23:07 -04001607 */
1608 svc_getu32(argv); /* XID */
1609 svc_getnl(argv); /* CALLDIR */
1610
Chuck Lever632dda82015-05-11 14:04:50 -04001611 /* Parse and execute the bc call */
Trond Myklebust0d2a9702015-06-04 15:37:10 -04001612 proc_error = svc_process_common(rqstp, argv, resv);
1613
Trond Myklebust7402a4f2019-07-16 13:51:29 -04001614 atomic_dec(&req->rq_xprt->bc_slot_count);
Trond Myklebust0d2a9702015-06-04 15:37:10 -04001615 if (!proc_error) {
Chuck Lever632dda82015-05-11 14:04:50 -04001616 /* Processing error: drop the request */
Trond Myklebustb3b02ae2012-05-31 15:26:38 -04001617 xprt_free_bc_request(req);
Vasily Averin8f7766c2018-12-24 14:46:08 +03001618 error = -EINVAL;
1619 goto out;
Andy Adamson4b5b3ba2011-01-06 02:04:24 +00001620 }
Chuck Lever632dda82015-05-11 14:04:50 -04001621 /* Finally, send the reply synchronously */
1622 memcpy(&req->rq_snd_buf, &rqstp->rq_res, sizeof(req->rq_snd_buf));
Trond Myklebust0f419792015-06-01 22:59:08 -04001623 task = rpc_run_bc_task(req);
Chuck Lever632dda82015-05-11 14:04:50 -04001624 if (IS_ERR(task)) {
1625 error = PTR_ERR(task);
1626 goto out;
1627 }
1628
1629 WARN_ON_ONCE(atomic_read(&task->tk_count) != 1);
1630 error = task->tk_status;
1631 rpc_put_task(task);
1632
1633out:
1634 dprintk("svc: %s(), error=%d\n", __func__, error);
1635 return error;
Ricardo Labiaga4d6bbb62009-04-01 09:23:07 -04001636}
Trond Myklebust0d961aa2011-07-13 19:24:15 -04001637EXPORT_SYMBOL_GPL(bc_svc_process);
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001638#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga4d6bbb62009-04-01 09:23:07 -04001639
Ricardo Labiaga1cad7ea2009-04-01 09:23:06 -04001640/*
Greg Banks7adae482006-10-04 02:15:47 -07001641 * Return (transport-specific) limit on the rpc payload.
1642 */
1643u32 svc_max_payload(const struct svc_rqst *rqstp)
1644{
Tom Tucker49023152007-12-30 21:07:21 -06001645 u32 max = rqstp->rq_xprt->xpt_class->xcl_max_payload;
Greg Banks7adae482006-10-04 02:15:47 -07001646
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001647 if (rqstp->rq_server->sv_max_payload < max)
1648 max = rqstp->rq_server->sv_max_payload;
Greg Banks7adae482006-10-04 02:15:47 -07001649 return max;
1650}
1651EXPORT_SYMBOL_GPL(svc_max_payload);
Chuck Lever8154ef22018-03-27 10:54:07 -04001652
1653/**
Chuck Lever03493bc2020-06-10 10:36:42 -04001654 * svc_encode_result_payload - mark a range of bytes as a result payload
Chuck Lever41205532020-03-02 14:45:53 -05001655 * @rqstp: svc_rqst to operate on
1656 * @offset: payload's byte offset in rqstp->rq_res
1657 * @length: size of payload, in bytes
1658 *
1659 * Returns zero on success, or a negative errno if a permanent
1660 * error occurred.
1661 */
Chuck Lever03493bc2020-06-10 10:36:42 -04001662int svc_encode_result_payload(struct svc_rqst *rqstp, unsigned int offset,
1663 unsigned int length)
Chuck Lever41205532020-03-02 14:45:53 -05001664{
Chuck Lever03493bc2020-06-10 10:36:42 -04001665 return rqstp->rq_xprt->xpt_ops->xpo_result_payload(rqstp, offset,
1666 length);
Chuck Lever41205532020-03-02 14:45:53 -05001667}
Chuck Lever03493bc2020-06-10 10:36:42 -04001668EXPORT_SYMBOL_GPL(svc_encode_result_payload);
Chuck Lever41205532020-03-02 14:45:53 -05001669
1670/**
Chuck Lever8154ef22018-03-27 10:54:07 -04001671 * svc_fill_write_vector - Construct data argument for VFS write call
1672 * @rqstp: svc_rqst to operate on
Chuck Lever3fd95572018-07-27 11:19:05 -04001673 * @pages: list of pages containing data payload
Chuck Lever8154ef22018-03-27 10:54:07 -04001674 * @first: buffer containing first section of write payload
1675 * @total: total number of bytes of write payload
1676 *
Chuck Lever3fd95572018-07-27 11:19:05 -04001677 * Fills in rqstp::rq_vec, and returns the number of elements.
Chuck Lever8154ef22018-03-27 10:54:07 -04001678 */
Chuck Lever3fd95572018-07-27 11:19:05 -04001679unsigned int svc_fill_write_vector(struct svc_rqst *rqstp, struct page **pages,
1680 struct kvec *first, size_t total)
Chuck Lever8154ef22018-03-27 10:54:07 -04001681{
1682 struct kvec *vec = rqstp->rq_vec;
Chuck Lever8154ef22018-03-27 10:54:07 -04001683 unsigned int i;
1684
1685 /* Some types of transport can present the write payload
1686 * entirely in rq_arg.pages. In this case, @first is empty.
1687 */
1688 i = 0;
1689 if (first->iov_len) {
1690 vec[i].iov_base = first->iov_base;
1691 vec[i].iov_len = min_t(size_t, total, first->iov_len);
1692 total -= vec[i].iov_len;
1693 ++i;
1694 }
1695
Chuck Lever8154ef22018-03-27 10:54:07 -04001696 while (total) {
1697 vec[i].iov_base = page_address(*pages);
1698 vec[i].iov_len = min_t(size_t, total, PAGE_SIZE);
1699 total -= vec[i].iov_len;
1700 ++i;
Chuck Lever8154ef22018-03-27 10:54:07 -04001701 ++pages;
1702 }
1703
1704 WARN_ON_ONCE(i > ARRAY_SIZE(rqstp->rq_vec));
1705 return i;
1706}
1707EXPORT_SYMBOL_GPL(svc_fill_write_vector);
Chuck Lever38a70312018-03-27 10:54:21 -04001708
1709/**
1710 * svc_fill_symlink_pathname - Construct pathname argument for VFS symlink call
1711 * @rqstp: svc_rqst to operate on
1712 * @first: buffer containing first section of pathname
Chuck Lever11b4d662018-07-27 11:19:10 -04001713 * @p: buffer containing remaining section of pathname
Chuck Lever38a70312018-03-27 10:54:21 -04001714 * @total: total length of the pathname argument
1715 *
Chuck Lever11b4d662018-07-27 11:19:10 -04001716 * The VFS symlink API demands a NUL-terminated pathname in mapped memory.
1717 * Returns pointer to a NUL-terminated string, or an ERR_PTR. Caller must free
1718 * the returned string.
Chuck Lever38a70312018-03-27 10:54:21 -04001719 */
1720char *svc_fill_symlink_pathname(struct svc_rqst *rqstp, struct kvec *first,
Chuck Lever11b4d662018-07-27 11:19:10 -04001721 void *p, size_t total)
Chuck Lever38a70312018-03-27 10:54:21 -04001722{
Chuck Lever11b4d662018-07-27 11:19:10 -04001723 size_t len, remaining;
1724 char *result, *dst;
Chuck Lever38a70312018-03-27 10:54:21 -04001725
Chuck Lever11b4d662018-07-27 11:19:10 -04001726 result = kmalloc(total + 1, GFP_KERNEL);
1727 if (!result)
1728 return ERR_PTR(-ESERVERFAULT);
Chuck Lever38a70312018-03-27 10:54:21 -04001729
Chuck Lever11b4d662018-07-27 11:19:10 -04001730 dst = result;
1731 remaining = total;
Chuck Lever38a70312018-03-27 10:54:21 -04001732
Chuck Lever11b4d662018-07-27 11:19:10 -04001733 len = min_t(size_t, total, first->iov_len);
1734 if (len) {
Chuck Lever38a70312018-03-27 10:54:21 -04001735 memcpy(dst, first->iov_base, len);
1736 dst += len;
1737 remaining -= len;
Chuck Lever38a70312018-03-27 10:54:21 -04001738 }
1739
Chuck Lever11b4d662018-07-27 11:19:10 -04001740 if (remaining) {
1741 len = min_t(size_t, remaining, PAGE_SIZE);
1742 memcpy(dst, p, len);
1743 dst += len;
1744 }
1745
1746 *dst = '\0';
1747
1748 /* Sanity check: Linux doesn't allow the pathname argument to
Chuck Lever38a70312018-03-27 10:54:21 -04001749 * contain a NUL byte.
1750 */
Chuck Lever11b4d662018-07-27 11:19:10 -04001751 if (strlen(result) != total) {
1752 kfree(result);
Chuck Lever38a70312018-03-27 10:54:21 -04001753 return ERR_PTR(-EINVAL);
Chuck Lever11b4d662018-07-27 11:19:10 -04001754 }
Chuck Lever38a70312018-03-27 10:54:21 -04001755 return result;
1756}
1757EXPORT_SYMBOL_GPL(svc_fill_symlink_pathname);