Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 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 Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 8 | * |
| 9 | * Multiple threads pools and NUMAisation |
| 10 | * Copyright (c) 2006 Silicon Graphics, Inc. |
| 11 | * by Greg Banks <gnb@melbourne.sgi.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <linux/linkage.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 15 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/errno.h> |
| 17 | #include <linux/net.h> |
| 18 | #include <linux/in.h> |
| 19 | #include <linux/mm.h> |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/module.h> |
Jeff Layton | 9867d76 | 2008-06-10 08:40:38 -0400 | [diff] [blame] | 22 | #include <linux/kthread.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 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 Labiaga | 4d6bbb6 | 2009-04-01 09:23:07 -0400 | [diff] [blame] | 30 | #include <linux/sunrpc/bc_xprt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Jeff Layton | 860a0d9 | 2014-10-28 14:24:12 -0400 | [diff] [blame] | 32 | #include <trace/events/sunrpc.h> |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #define RPCDBG_FACILITY RPCDBG_SVCDSP |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Stanislav Kinsbursky | 5247fab | 2012-01-13 14:02:48 +0400 | [diff] [blame] | 36 | static void svc_unregister(const struct svc_serv *serv, struct net *net); |
Chuck Lever | 7252d57 | 2008-08-18 19:34:08 -0400 | [diff] [blame] | 37 | |
Jeff Layton | c369014 | 2015-06-08 12:04:46 -0700 | [diff] [blame] | 38 | #define svc_serv_is_pooled(serv) ((serv)->sv_ops->svo_function) |
Greg Banks | 42a7fc4 | 2007-03-06 01:42:23 -0800 | [diff] [blame] | 39 | |
Greg Banks | 42a7fc4 | 2007-03-06 01:42:23 -0800 | [diff] [blame] | 40 | #define SVC_POOL_DEFAULT SVC_POOL_GLOBAL |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 41 | |
| 42 | /* |
| 43 | * Structure for mapping cpus to pools and vice versa. |
| 44 | * Setup once during sunrpc initialisation. |
| 45 | */ |
Jeff Layton | d70bc0c | 2015-06-08 12:09:54 -0700 | [diff] [blame] | 46 | struct svc_pool_map svc_pool_map = { |
Greg Banks | 42a7fc4 | 2007-03-06 01:42:23 -0800 | [diff] [blame] | 47 | .mode = SVC_POOL_DEFAULT |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 48 | }; |
Jeff Layton | d70bc0c | 2015-06-08 12:09:54 -0700 | [diff] [blame] | 49 | EXPORT_SYMBOL_GPL(svc_pool_map); |
| 50 | |
Greg Banks | 42a7fc4 | 2007-03-06 01:42:23 -0800 | [diff] [blame] | 51 | static DEFINE_MUTEX(svc_pool_map_mutex);/* protects svc_pool_map.count only */ |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 52 | |
Greg Banks | 42a7fc4 | 2007-03-06 01:42:23 -0800 | [diff] [blame] | 53 | static int |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 54 | param_set_pool_mode(const char *val, const struct kernel_param *kp) |
Greg Banks | 42a7fc4 | 2007-03-06 01:42:23 -0800 | [diff] [blame] | 55 | { |
| 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 | |
| 78 | out: |
| 79 | mutex_unlock(&svc_pool_map_mutex); |
| 80 | return err; |
| 81 | } |
| 82 | |
| 83 | static int |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 84 | param_get_pool_mode(char *buf, const struct kernel_param *kp) |
Greg Banks | 42a7fc4 | 2007-03-06 01:42:23 -0800 | [diff] [blame] | 85 | { |
| 86 | int *ip = (int *)kp->arg; |
| 87 | |
| 88 | switch (*ip) |
| 89 | { |
| 90 | case SVC_POOL_AUTO: |
| 91 | return strlcpy(buf, "auto", 20); |
| 92 | case SVC_POOL_GLOBAL: |
| 93 | return strlcpy(buf, "global", 20); |
| 94 | case SVC_POOL_PERCPU: |
| 95 | return strlcpy(buf, "percpu", 20); |
| 96 | case SVC_POOL_PERNODE: |
| 97 | return strlcpy(buf, "pernode", 20); |
| 98 | default: |
| 99 | return sprintf(buf, "%d", *ip); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | module_param_call(pool_mode, param_set_pool_mode, param_get_pool_mode, |
| 104 | &svc_pool_map.mode, 0644); |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 105 | |
| 106 | /* |
| 107 | * Detect best pool mapping mode heuristically, |
| 108 | * according to the machine's topology. |
| 109 | */ |
| 110 | static int |
| 111 | svc_pool_map_choose_mode(void) |
| 112 | { |
| 113 | unsigned int node; |
| 114 | |
Christoph Lameter | 62bc62a | 2009-06-16 15:32:15 -0700 | [diff] [blame] | 115 | if (nr_online_nodes > 1) { |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 116 | /* |
| 117 | * Actually have multiple NUMA nodes, |
| 118 | * so split pools on NUMA node boundaries |
| 119 | */ |
| 120 | return SVC_POOL_PERNODE; |
| 121 | } |
| 122 | |
H Hartley Sweeten | 72c3368 | 2010-03-05 13:42:43 -0800 | [diff] [blame] | 123 | node = first_online_node; |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 124 | 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 | */ |
| 142 | static int |
| 143 | svc_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 | |
| 154 | fail_free: |
| 155 | kfree(m->to_pool); |
J. Bruce Fields | 61c8504 | 2011-12-22 18:22:49 -0700 | [diff] [blame] | 156 | m->to_pool = NULL; |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 157 | fail: |
| 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 | */ |
| 165 | static int |
| 166 | svc_pool_map_init_percpu(struct svc_pool_map *m) |
| 167 | { |
Christoph Lameter | 53b8a31 | 2007-02-20 13:57:51 -0800 | [diff] [blame] | 168 | unsigned int maxpools = nr_cpu_ids; |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 169 | 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 Carpenter | eb63192 | 2014-10-29 11:44:16 +0300 | [diff] [blame] | 178 | BUG_ON(pidx >= maxpools); |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 179 | 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 | */ |
| 193 | static int |
| 194 | svc_pool_map_init_pernode(struct svc_pool_map *m) |
| 195 | { |
Christoph Lameter | 74c7aa8 | 2007-02-20 13:57:51 -0800 | [diff] [blame] | 196 | unsigned int maxpools = nr_node_ids; |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 197 | 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 Banks | 42a7fc4 | 2007-03-06 01:42:23 -0800 | [diff] [blame] | 219 | * 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 Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 222 | */ |
Jeff Layton | d70bc0c | 2015-06-08 12:09:54 -0700 | [diff] [blame] | 223 | unsigned int |
Greg Banks | 42a7fc4 | 2007-03-06 01:42:23 -0800 | [diff] [blame] | 224 | svc_pool_map_get(void) |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 225 | { |
| 226 | struct svc_pool_map *m = &svc_pool_map; |
| 227 | int npools = -1; |
| 228 | |
Greg Banks | 42a7fc4 | 2007-03-06 01:42:23 -0800 | [diff] [blame] | 229 | mutex_lock(&svc_pool_map_mutex); |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 230 | |
Greg Banks | 42a7fc4 | 2007-03-06 01:42:23 -0800 | [diff] [blame] | 231 | 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 Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 238 | |
| 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 Banks | 42a7fc4 | 2007-03-06 01:42:23 -0800 | [diff] [blame] | 255 | mutex_unlock(&svc_pool_map_mutex); |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 256 | return m->npools; |
| 257 | } |
Jeff Layton | d70bc0c | 2015-06-08 12:09:54 -0700 | [diff] [blame] | 258 | EXPORT_SYMBOL_GPL(svc_pool_map_get); |
Greg Banks | 42a7fc4 | 2007-03-06 01:42:23 -0800 | [diff] [blame] | 259 | |
| 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 Layton | d70bc0c | 2015-06-08 12:09:54 -0700 | [diff] [blame] | 267 | void |
Greg Banks | 42a7fc4 | 2007-03-06 01:42:23 -0800 | [diff] [blame] | 268 | svc_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 Banks | 42a7fc4 | 2007-03-06 01:42:23 -0800 | [diff] [blame] | 275 | kfree(m->to_pool); |
J. Bruce Fields | 61c8504 | 2011-12-22 18:22:49 -0700 | [diff] [blame] | 276 | m->to_pool = NULL; |
Greg Banks | 42a7fc4 | 2007-03-06 01:42:23 -0800 | [diff] [blame] | 277 | kfree(m->pool_to); |
J. Bruce Fields | 61c8504 | 2011-12-22 18:22:49 -0700 | [diff] [blame] | 278 | m->pool_to = NULL; |
Greg Banks | 42a7fc4 | 2007-03-06 01:42:23 -0800 | [diff] [blame] | 279 | m->npools = 0; |
| 280 | } |
| 281 | |
| 282 | mutex_unlock(&svc_pool_map_mutex); |
| 283 | } |
Jeff Layton | d70bc0c | 2015-06-08 12:09:54 -0700 | [diff] [blame] | 284 | EXPORT_SYMBOL_GPL(svc_pool_map_put); |
Greg Banks | 42a7fc4 | 2007-03-06 01:42:23 -0800 | [diff] [blame] | 285 | |
Eric Dumazet | 11fd165 | 2011-07-28 20:04:09 +0200 | [diff] [blame] | 286 | static 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 Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 298 | /* |
Jeff Layton | 9867d76 | 2008-06-10 08:40:38 -0400 | [diff] [blame] | 299 | * Set the given thread's cpus_allowed mask so that it |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 300 | * will only run on cpus in the given pool. |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 301 | */ |
Jeff Layton | 9867d76 | 2008-06-10 08:40:38 -0400 | [diff] [blame] | 302 | static inline void |
| 303 | svc_pool_map_set_cpumask(struct task_struct *task, unsigned int pidx) |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 304 | { |
| 305 | struct svc_pool_map *m = &svc_pool_map; |
Jeff Layton | 9867d76 | 2008-06-10 08:40:38 -0400 | [diff] [blame] | 306 | unsigned int node = m->pool_to[pidx]; |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 307 | |
| 308 | /* |
| 309 | * The caller checks for sv_nrpools > 1, which |
Greg Banks | 42a7fc4 | 2007-03-06 01:42:23 -0800 | [diff] [blame] | 310 | * implies that we've been initialized. |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 311 | */ |
Weston Andros Adamson | 1bd58aa | 2012-10-23 10:43:38 -0400 | [diff] [blame] | 312 | WARN_ON_ONCE(m->count == 0); |
| 313 | if (m->count == 0) |
| 314 | return; |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 315 | |
Jeff Layton | 9867d76 | 2008-06-10 08:40:38 -0400 | [diff] [blame] | 316 | switch (m->mode) { |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 317 | case SVC_POOL_PERCPU: |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 318 | { |
Rusty Russell | aa85ea5 | 2009-03-30 22:05:15 -0600 | [diff] [blame] | 319 | set_cpus_allowed_ptr(task, cpumask_of(node)); |
Jeff Layton | 9867d76 | 2008-06-10 08:40:38 -0400 | [diff] [blame] | 320 | break; |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 321 | } |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 322 | case SVC_POOL_PERNODE: |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 323 | { |
Rusty Russell | a70f730 | 2009-03-13 14:49:46 +1030 | [diff] [blame] | 324 | set_cpus_allowed_ptr(task, cpumask_of_node(node)); |
Jeff Layton | 9867d76 | 2008-06-10 08:40:38 -0400 | [diff] [blame] | 325 | break; |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 326 | } |
Mike Travis | c5f59f0 | 2008-04-04 18:11:10 -0700 | [diff] [blame] | 327 | } |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 328 | } |
| 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 | */ |
| 335 | struct svc_pool * |
| 336 | svc_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 Banks | 42a7fc4 | 2007-03-06 01:42:23 -0800 | [diff] [blame] | 342 | * An uninitialised map happens in a pure client when |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 343 | * lockd is brought up, so silently treat it the |
| 344 | * same as SVC_POOL_GLOBAL. |
| 345 | */ |
Greg Banks | 42a7fc4 | 2007-03-06 01:42:23 -0800 | [diff] [blame] | 346 | 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 Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 355 | } |
| 356 | return &serv->sv_pools[pidx % serv->sv_nrpools]; |
| 357 | } |
| 358 | |
Stanislav Kinsbursky | bb2224d | 2012-01-31 15:08:05 +0400 | [diff] [blame] | 359 | int svc_rpcb_setup(struct svc_serv *serv, struct net *net) |
Stanislav Kinsbursky | d990856 | 2011-10-25 14:16:58 +0300 | [diff] [blame] | 360 | { |
| 361 | int err; |
| 362 | |
Stanislav Kinsbursky | bee42f68 | 2012-01-13 14:02:56 +0400 | [diff] [blame] | 363 | err = rpcb_create_local(net); |
Stanislav Kinsbursky | d990856 | 2011-10-25 14:16:58 +0300 | [diff] [blame] | 364 | if (err) |
| 365 | return err; |
| 366 | |
| 367 | /* Remove any stale portmap registrations */ |
Stanislav Kinsbursky | bee42f68 | 2012-01-13 14:02:56 +0400 | [diff] [blame] | 368 | svc_unregister(serv, net); |
Stanislav Kinsbursky | d990856 | 2011-10-25 14:16:58 +0300 | [diff] [blame] | 369 | return 0; |
| 370 | } |
Stanislav Kinsbursky | bb2224d | 2012-01-31 15:08:05 +0400 | [diff] [blame] | 371 | EXPORT_SYMBOL_GPL(svc_rpcb_setup); |
Stanislav Kinsbursky | d990856 | 2011-10-25 14:16:58 +0300 | [diff] [blame] | 372 | |
Stanislav Kinsbursky | 5ecebb7 | 2012-01-13 14:03:04 +0400 | [diff] [blame] | 373 | void svc_rpcb_cleanup(struct svc_serv *serv, struct net *net) |
Stanislav Kinsbursky | d990856 | 2011-10-25 14:16:58 +0300 | [diff] [blame] | 374 | { |
Stanislav Kinsbursky | 5ecebb7 | 2012-01-13 14:03:04 +0400 | [diff] [blame] | 375 | svc_unregister(serv, net); |
| 376 | rpcb_put_local(net); |
Stanislav Kinsbursky | d990856 | 2011-10-25 14:16:58 +0300 | [diff] [blame] | 377 | } |
Stanislav Kinsbursky | 16d0587 | 2011-10-25 14:17:28 +0300 | [diff] [blame] | 378 | EXPORT_SYMBOL_GPL(svc_rpcb_cleanup); |
Stanislav Kinsbursky | d990856 | 2011-10-25 14:16:58 +0300 | [diff] [blame] | 379 | |
| 380 | static 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 Layton | 05a45a2 | 2017-02-24 13:25:22 -0500 | [diff] [blame] | 389 | if (!progp->pg_vers[i]->vs_hidden) |
Stanislav Kinsbursky | d990856 | 2011-10-25 14:16:58 +0300 | [diff] [blame] | 390 | return 1; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | return 0; |
| 395 | } |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 396 | |
Stanislav Kinsbursky | 9793f7c | 2012-05-02 16:08:38 +0400 | [diff] [blame] | 397 | int 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 | } |
| 403 | EXPORT_SYMBOL_GPL(svc_bind); |
| 404 | |
Trond Myklebust | d002526 | 2016-08-29 20:03:49 -0400 | [diff] [blame] | 405 | #if defined(CONFIG_SUNRPC_BACKCHANNEL) |
| 406 | static 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 |
| 414 | static void |
| 415 | __svc_init_bc(struct svc_serv *serv) |
| 416 | { |
| 417 | } |
| 418 | #endif |
| 419 | |
Greg Banks | bfd2416 | 2006-10-02 02:18:01 -0700 | [diff] [blame] | 420 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | * Create an RPC service |
| 422 | */ |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 423 | static struct svc_serv * |
| 424 | __svc_create(struct svc_program *prog, unsigned int bufsize, int npools, |
Chuck Lever | afea565 | 2017-08-01 12:00:06 -0400 | [diff] [blame] | 425 | const struct svc_serv_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | { |
| 427 | struct svc_serv *serv; |
Chuck Lever | ea339d4 | 2007-10-26 13:32:56 -0400 | [diff] [blame] | 428 | unsigned int vers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | unsigned int xdrsize; |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 430 | unsigned int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 432 | if (!(serv = kzalloc(sizeof(*serv), GFP_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | return NULL; |
Andreas Gruenbacher | 9ba0263 | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 434 | serv->sv_name = prog->pg_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | serv->sv_program = prog; |
| 436 | serv->sv_nrthreads = 1; |
| 437 | serv->sv_stats = prog->pg_stats; |
NeilBrown | c6b0a9f | 2006-10-06 00:44:05 -0700 | [diff] [blame] | 438 | 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 Layton | ea126e7 | 2015-06-08 12:03:32 -0700 | [diff] [blame] | 442 | serv->sv_ops = ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | xdrsize = 0; |
Andreas Gruenbacher | 9ba0263 | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 444 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | serv->sv_xdrsize = xdrsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | INIT_LIST_HEAD(&serv->sv_tempsocks); |
| 458 | INIT_LIST_HEAD(&serv->sv_permsocks); |
Kees Cook | ff861c4 | 2017-10-16 17:29:42 -0700 | [diff] [blame] | 459 | timer_setup(&serv->sv_temptimer, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | spin_lock_init(&serv->sv_lock); |
| 461 | |
Trond Myklebust | d002526 | 2016-08-29 20:03:49 -0400 | [diff] [blame] | 462 | __svc_init_bc(serv); |
| 463 | |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 464 | serv->sv_nrpools = npools; |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 465 | serv->sv_pools = |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 466 | kcalloc(serv->sv_nrpools, sizeof(struct svc_pool), |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 467 | 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 Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 476 | dprintk("svc: initialising pool %u for %s\n", |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 477 | i, serv->sv_name); |
| 478 | |
| 479 | pool->sp_id = i; |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 480 | INIT_LIST_HEAD(&pool->sp_sockets); |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 481 | INIT_LIST_HEAD(&pool->sp_all_threads); |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 482 | spin_lock_init(&pool->sp_lock); |
| 483 | } |
| 484 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | return serv; |
| 486 | } |
| 487 | |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 488 | struct svc_serv * |
| 489 | svc_create(struct svc_program *prog, unsigned int bufsize, |
Chuck Lever | afea565 | 2017-08-01 12:00:06 -0400 | [diff] [blame] | 490 | const struct svc_serv_ops *ops) |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 491 | { |
Jeff Layton | ea126e7 | 2015-06-08 12:03:32 -0700 | [diff] [blame] | 492 | return __svc_create(prog, bufsize, /*npools*/1, ops); |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 493 | } |
Trond Myklebust | 24c3767 | 2008-12-23 16:30:12 -0500 | [diff] [blame] | 494 | EXPORT_SYMBOL_GPL(svc_create); |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 495 | |
| 496 | struct svc_serv * |
| 497 | svc_create_pooled(struct svc_program *prog, unsigned int bufsize, |
Chuck Lever | afea565 | 2017-08-01 12:00:06 -0400 | [diff] [blame] | 498 | const struct svc_serv_ops *ops) |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 499 | { |
| 500 | struct svc_serv *serv; |
Greg Banks | 42a7fc4 | 2007-03-06 01:42:23 -0800 | [diff] [blame] | 501 | unsigned int npools = svc_pool_map_get(); |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 502 | |
Jeff Layton | ea126e7 | 2015-06-08 12:03:32 -0700 | [diff] [blame] | 503 | serv = __svc_create(prog, bufsize, npools, ops); |
Jeff Layton | 067f96e | 2014-11-19 07:51:13 -0500 | [diff] [blame] | 504 | if (!serv) |
| 505 | goto out_err; |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 506 | return serv; |
Jeff Layton | 067f96e | 2014-11-19 07:51:13 -0500 | [diff] [blame] | 507 | out_err: |
| 508 | svc_pool_map_put(); |
| 509 | return NULL; |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 510 | } |
Trond Myklebust | 24c3767 | 2008-12-23 16:30:12 -0500 | [diff] [blame] | 511 | EXPORT_SYMBOL_GPL(svc_create_pooled); |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 512 | |
Stanislav Kinsbursky | 074d0f6 | 2012-01-31 14:09:25 +0400 | [diff] [blame] | 513 | void svc_shutdown_net(struct svc_serv *serv, struct net *net) |
| 514 | { |
Stanislav Kinsbursky | 074d0f6 | 2012-01-31 14:09:25 +0400 | [diff] [blame] | 515 | svc_close_net(serv, net); |
| 516 | |
Jeff Layton | ea126e7 | 2015-06-08 12:03:32 -0700 | [diff] [blame] | 517 | if (serv->sv_ops->svo_shutdown) |
| 518 | serv->sv_ops->svo_shutdown(serv, net); |
Stanislav Kinsbursky | 074d0f6 | 2012-01-31 14:09:25 +0400 | [diff] [blame] | 519 | } |
| 520 | EXPORT_SYMBOL_GPL(svc_shutdown_net); |
| 521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | /* |
Neil Brown | bedbdd8 | 2008-06-10 08:40:35 -0400 | [diff] [blame] | 523 | * Destroy an RPC service. Should be called with appropriate locking to |
| 524 | * protect the sv_nrthreads, sv_permsocks and sv_tempsocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | */ |
| 526 | void |
| 527 | svc_destroy(struct svc_serv *serv) |
| 528 | { |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 529 | dprintk("svc: svc_destroy(%s, %d)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | 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 Banks | 36bdfc8 | 2006-10-02 02:17:54 -0700 | [diff] [blame] | 541 | del_timer_sync(&serv->sv_temptimer); |
Stanislav Kinsbursky | 074d0f6 | 2012-01-31 14:09:25 +0400 | [diff] [blame] | 542 | |
Stanislav Kinsbursky | 7b147f1 | 2012-01-31 14:09:17 +0400 | [diff] [blame] | 543 | /* |
| 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)); |
NeilBrown | cda1fd4 | 2007-03-06 01:42:22 -0800 | [diff] [blame] | 549 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | cache_clean_deferred(serv); |
| 551 | |
Greg Banks | 42a7fc4 | 2007-03-06 01:42:23 -0800 | [diff] [blame] | 552 | if (svc_serv_is_pooled(serv)) |
| 553 | svc_pool_map_put(); |
| 554 | |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 555 | kfree(serv->sv_pools); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | kfree(serv); |
| 557 | } |
Trond Myklebust | 24c3767 | 2008-12-23 16:30:12 -0500 | [diff] [blame] | 558 | EXPORT_SYMBOL_GPL(svc_destroy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | |
| 560 | /* |
| 561 | * Allocate an RPC server's buffer space. |
| 562 | * We allocate pages and place them in rq_argpages. |
| 563 | */ |
| 564 | static int |
Eric Dumazet | 11fd165 | 2011-07-28 20:04:09 +0200 | [diff] [blame] | 565 | svc_init_buffer(struct svc_rqst *rqstp, unsigned int size, int node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | { |
Chuck Lever | 0dc220f | 2008-04-14 12:27:52 -0400 | [diff] [blame] | 567 | unsigned int pages, arghi; |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 568 | |
Andy Adamson | ba17686 | 2010-01-26 21:24:04 -0500 | [diff] [blame] | 569 | /* bc_xprt uses fore channel allocated buffers */ |
| 570 | if (svc_is_backchannel(rqstp)) |
| 571 | return 1; |
| 572 | |
NeilBrown | c6b0a9f | 2006-10-06 00:44:05 -0700 | [diff] [blame] | 573 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | arghi = 0; |
Weston Andros Adamson | b25cd05 | 2012-10-23 10:43:41 -0400 | [diff] [blame] | 577 | WARN_ON_ONCE(pages > RPCSVC_MAXPAGES); |
| 578 | if (pages > RPCSVC_MAXPAGES) |
| 579 | pages = RPCSVC_MAXPAGES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | while (pages) { |
Eric Dumazet | 11fd165 | 2011-07-28 20:04:09 +0200 | [diff] [blame] | 581 | struct page *p = alloc_pages_node(node, GFP_KERNEL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | if (!p) |
| 583 | break; |
NeilBrown | 4452435 | 2006-10-04 02:15:46 -0700 | [diff] [blame] | 584 | rqstp->rq_pages[arghi++] = p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | pages--; |
| 586 | } |
Chuck Lever | 0dc220f | 2008-04-14 12:27:52 -0400 | [diff] [blame] | 587 | return pages == 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | /* |
| 591 | * Release an RPC server buffer |
| 592 | */ |
| 593 | static void |
| 594 | svc_release_buffer(struct svc_rqst *rqstp) |
| 595 | { |
Chuck Lever | 50c8bb1 | 2008-04-14 12:27:45 -0400 | [diff] [blame] | 596 | unsigned int i; |
| 597 | |
| 598 | for (i = 0; i < ARRAY_SIZE(rqstp->rq_pages); i++) |
NeilBrown | 4452435 | 2006-10-04 02:15:46 -0700 | [diff] [blame] | 599 | if (rqstp->rq_pages[i]) |
| 600 | put_page(rqstp->rq_pages[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Jeff Layton | 0113ab3 | 2008-01-29 10:30:54 -0500 | [diff] [blame] | 603 | struct svc_rqst * |
Jeff Layton | 1b6dc1d | 2015-06-08 12:11:10 -0700 | [diff] [blame] | 604 | svc_rqst_alloc(struct svc_serv *serv, struct svc_pool *pool, int node) |
Jeff Layton | 0113ab3 | 2008-01-29 10:30:54 -0500 | [diff] [blame] | 605 | { |
| 606 | struct svc_rqst *rqstp; |
| 607 | |
Eric Dumazet | 11fd165 | 2011-07-28 20:04:09 +0200 | [diff] [blame] | 608 | rqstp = kzalloc_node(sizeof(*rqstp), GFP_KERNEL, node); |
Jeff Layton | 0113ab3 | 2008-01-29 10:30:54 -0500 | [diff] [blame] | 609 | if (!rqstp) |
Jeff Layton | 1b6dc1d | 2015-06-08 12:11:10 -0700 | [diff] [blame] | 610 | return rqstp; |
Jeff Layton | 0113ab3 | 2008-01-29 10:30:54 -0500 | [diff] [blame] | 611 | |
Jeff Layton | b1691bc | 2014-11-21 14:19:30 -0500 | [diff] [blame] | 612 | __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 Layton | 1b6dc1d | 2015-06-08 12:11:10 -0700 | [diff] [blame] | 616 | |
| 617 | rqstp->rq_argp = kmalloc_node(serv->sv_xdrsize, GFP_KERNEL, node); |
| 618 | if (!rqstp->rq_argp) |
| 619 | goto out_enomem; |
| 620 | |
| 621 | rqstp->rq_resp = kmalloc_node(serv->sv_xdrsize, GFP_KERNEL, node); |
| 622 | if (!rqstp->rq_resp) |
| 623 | goto out_enomem; |
| 624 | |
| 625 | if (!svc_init_buffer(rqstp, serv->sv_max_mesg, node)) |
| 626 | goto out_enomem; |
| 627 | |
| 628 | return rqstp; |
| 629 | out_enomem: |
| 630 | svc_rqst_free(rqstp); |
| 631 | return NULL; |
| 632 | } |
| 633 | EXPORT_SYMBOL_GPL(svc_rqst_alloc); |
| 634 | |
| 635 | struct svc_rqst * |
| 636 | svc_prepare_thread(struct svc_serv *serv, struct svc_pool *pool, int node) |
| 637 | { |
| 638 | struct svc_rqst *rqstp; |
| 639 | |
| 640 | rqstp = svc_rqst_alloc(serv, pool, node); |
| 641 | if (!rqstp) |
| 642 | return ERR_PTR(-ENOMEM); |
| 643 | |
| 644 | serv->sv_nrthreads++; |
Jeff Layton | 0113ab3 | 2008-01-29 10:30:54 -0500 | [diff] [blame] | 645 | spin_lock_bh(&pool->sp_lock); |
| 646 | pool->sp_nrthreads++; |
Jeff Layton | 8124438 | 2014-11-21 14:19:28 -0500 | [diff] [blame] | 647 | list_add_rcu(&rqstp->rq_all, &pool->sp_all_threads); |
Jeff Layton | 0113ab3 | 2008-01-29 10:30:54 -0500 | [diff] [blame] | 648 | spin_unlock_bh(&pool->sp_lock); |
Jeff Layton | 0113ab3 | 2008-01-29 10:30:54 -0500 | [diff] [blame] | 649 | return rqstp; |
Jeff Layton | 0113ab3 | 2008-01-29 10:30:54 -0500 | [diff] [blame] | 650 | } |
Trond Myklebust | 24c3767 | 2008-12-23 16:30:12 -0500 | [diff] [blame] | 651 | EXPORT_SYMBOL_GPL(svc_prepare_thread); |
Jeff Layton | 0113ab3 | 2008-01-29 10:30:54 -0500 | [diff] [blame] | 652 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | /* |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 654 | * Choose a pool in which to create a new thread, for svc_set_num_threads |
| 655 | */ |
| 656 | static inline struct svc_pool * |
| 657 | choose_pool(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state) |
| 658 | { |
| 659 | if (pool != NULL) |
| 660 | return pool; |
| 661 | |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 662 | return &serv->sv_pools[(*state)++ % serv->sv_nrpools]; |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | /* |
| 666 | * Choose a thread to kill, for svc_set_num_threads |
| 667 | */ |
| 668 | static inline struct task_struct * |
| 669 | choose_victim(struct svc_serv *serv, struct svc_pool *pool, unsigned int *state) |
| 670 | { |
| 671 | unsigned int i; |
| 672 | struct task_struct *task = NULL; |
| 673 | |
| 674 | if (pool != NULL) { |
| 675 | spin_lock_bh(&pool->sp_lock); |
| 676 | } else { |
| 677 | /* choose a pool in round-robin fashion */ |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 678 | for (i = 0; i < serv->sv_nrpools; i++) { |
| 679 | pool = &serv->sv_pools[--(*state) % serv->sv_nrpools]; |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 680 | spin_lock_bh(&pool->sp_lock); |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 681 | if (!list_empty(&pool->sp_all_threads)) |
| 682 | goto found_pool; |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 683 | spin_unlock_bh(&pool->sp_lock); |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 684 | } |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 685 | return NULL; |
| 686 | } |
| 687 | |
| 688 | found_pool: |
| 689 | if (!list_empty(&pool->sp_all_threads)) { |
| 690 | struct svc_rqst *rqstp; |
| 691 | |
| 692 | /* |
| 693 | * Remove from the pool->sp_all_threads list |
| 694 | * so we don't try to kill it again. |
| 695 | */ |
| 696 | rqstp = list_entry(pool->sp_all_threads.next, struct svc_rqst, rq_all); |
Jeff Layton | 8124438 | 2014-11-21 14:19:28 -0500 | [diff] [blame] | 697 | set_bit(RQ_VICTIM, &rqstp->rq_flags); |
| 698 | list_del_rcu(&rqstp->rq_all); |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 699 | task = rqstp->rq_task; |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 700 | } |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 701 | spin_unlock_bh(&pool->sp_lock); |
| 702 | |
| 703 | return task; |
| 704 | } |
| 705 | |
Trond Myklebust | 9e0d876 | 2017-04-26 11:55:26 -0400 | [diff] [blame] | 706 | /* create new threads */ |
| 707 | static int |
| 708 | svc_start_kthreads(struct svc_serv *serv, struct svc_pool *pool, int nrservs) |
| 709 | { |
| 710 | struct svc_rqst *rqstp; |
| 711 | struct task_struct *task; |
| 712 | struct svc_pool *chosen_pool; |
| 713 | unsigned int state = serv->sv_nrthreads-1; |
| 714 | int node; |
| 715 | |
| 716 | do { |
| 717 | nrservs--; |
| 718 | chosen_pool = choose_pool(serv, pool, &state); |
| 719 | |
| 720 | node = svc_pool_map_get_node(chosen_pool->sp_id); |
| 721 | rqstp = svc_prepare_thread(serv, chosen_pool, node); |
| 722 | if (IS_ERR(rqstp)) |
| 723 | return PTR_ERR(rqstp); |
| 724 | |
| 725 | __module_get(serv->sv_ops->svo_module); |
| 726 | task = kthread_create_on_node(serv->sv_ops->svo_function, rqstp, |
| 727 | node, "%s", serv->sv_name); |
| 728 | if (IS_ERR(task)) { |
| 729 | module_put(serv->sv_ops->svo_module); |
| 730 | svc_exit_thread(rqstp); |
| 731 | return PTR_ERR(task); |
| 732 | } |
| 733 | |
| 734 | rqstp->rq_task = task; |
| 735 | if (serv->sv_nrpools > 1) |
| 736 | svc_pool_map_set_cpumask(task, chosen_pool->sp_id); |
| 737 | |
| 738 | svc_sock_update_bufs(serv); |
| 739 | wake_up_process(task); |
| 740 | } while (nrservs > 0); |
| 741 | |
| 742 | return 0; |
| 743 | } |
| 744 | |
| 745 | |
| 746 | /* destroy old threads */ |
| 747 | static int |
| 748 | svc_signal_kthreads(struct svc_serv *serv, struct svc_pool *pool, int nrservs) |
| 749 | { |
| 750 | struct task_struct *task; |
| 751 | unsigned int state = serv->sv_nrthreads-1; |
| 752 | |
| 753 | /* destroy old threads */ |
| 754 | do { |
| 755 | task = choose_victim(serv, pool, &state); |
| 756 | if (task == NULL) |
| 757 | break; |
| 758 | send_sig(SIGINT, task, 1); |
| 759 | nrservs++; |
| 760 | } while (nrservs < 0); |
| 761 | |
| 762 | return 0; |
| 763 | } |
| 764 | |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 765 | /* |
| 766 | * Create or destroy enough new threads to make the number |
| 767 | * of threads the given number. If `pool' is non-NULL, applies |
| 768 | * only to threads in that pool, otherwise round-robins between |
J. Bruce Fields | 94cf317 | 2011-12-01 17:51:21 -0500 | [diff] [blame] | 769 | * all pools. Caller must ensure that mutual exclusion between this and |
| 770 | * server startup or shutdown. |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 771 | * |
| 772 | * Destroying threads relies on the service threads filling in |
| 773 | * rqstp->rq_task, which only the nfs ones do. Assumes the serv |
| 774 | * has been created using svc_create_pooled(). |
| 775 | * |
| 776 | * Based on code that used to be in nfsd_svc() but tweaked |
| 777 | * to be pool-aware. |
| 778 | */ |
| 779 | int |
| 780 | svc_set_num_threads(struct svc_serv *serv, struct svc_pool *pool, int nrservs) |
| 781 | { |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 782 | if (pool == NULL) { |
| 783 | /* The -1 assumes caller has done a svc_get() */ |
| 784 | nrservs -= (serv->sv_nrthreads-1); |
| 785 | } else { |
| 786 | spin_lock_bh(&pool->sp_lock); |
| 787 | nrservs -= pool->sp_nrthreads; |
| 788 | spin_unlock_bh(&pool->sp_lock); |
| 789 | } |
| 790 | |
Trond Myklebust | 9e0d876 | 2017-04-26 11:55:26 -0400 | [diff] [blame] | 791 | if (nrservs > 0) |
| 792 | return svc_start_kthreads(serv, pool, nrservs); |
| 793 | if (nrservs < 0) |
| 794 | return svc_signal_kthreads(serv, pool, nrservs); |
| 795 | return 0; |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 796 | } |
Trond Myklebust | 24c3767 | 2008-12-23 16:30:12 -0500 | [diff] [blame] | 797 | EXPORT_SYMBOL_GPL(svc_set_num_threads); |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 798 | |
Trond Myklebust | ed6473d | 2017-04-26 11:55:27 -0400 | [diff] [blame] | 799 | /* destroy old threads */ |
| 800 | static int |
| 801 | svc_stop_kthreads(struct svc_serv *serv, struct svc_pool *pool, int nrservs) |
| 802 | { |
| 803 | struct task_struct *task; |
| 804 | unsigned int state = serv->sv_nrthreads-1; |
| 805 | |
| 806 | /* destroy old threads */ |
| 807 | do { |
| 808 | task = choose_victim(serv, pool, &state); |
| 809 | if (task == NULL) |
| 810 | break; |
| 811 | kthread_stop(task); |
| 812 | nrservs++; |
| 813 | } while (nrservs < 0); |
| 814 | return 0; |
| 815 | } |
| 816 | |
| 817 | int |
| 818 | svc_set_num_threads_sync(struct svc_serv *serv, struct svc_pool *pool, int nrservs) |
| 819 | { |
| 820 | if (pool == NULL) { |
| 821 | /* The -1 assumes caller has done a svc_get() */ |
| 822 | nrservs -= (serv->sv_nrthreads-1); |
| 823 | } else { |
| 824 | spin_lock_bh(&pool->sp_lock); |
| 825 | nrservs -= pool->sp_nrthreads; |
| 826 | spin_unlock_bh(&pool->sp_lock); |
| 827 | } |
| 828 | |
| 829 | if (nrservs > 0) |
| 830 | return svc_start_kthreads(serv, pool, nrservs); |
| 831 | if (nrservs < 0) |
| 832 | return svc_stop_kthreads(serv, pool, nrservs); |
| 833 | return 0; |
| 834 | } |
| 835 | EXPORT_SYMBOL_GPL(svc_set_num_threads_sync); |
| 836 | |
Greg Banks | a745544 | 2006-10-02 02:17:59 -0700 | [diff] [blame] | 837 | /* |
Jeff Layton | 3c51991 | 2015-01-22 08:19:32 -0500 | [diff] [blame] | 838 | * Called from a server thread as it's exiting. Caller must hold the "service |
| 839 | * mutex" for the service. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | */ |
| 841 | void |
Jeff Layton | 1b6dc1d | 2015-06-08 12:11:10 -0700 | [diff] [blame] | 842 | svc_rqst_free(struct svc_rqst *rqstp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | svc_release_buffer(rqstp); |
Jesper Juhl | a51482b | 2005-11-08 09:41:34 -0800 | [diff] [blame] | 845 | kfree(rqstp->rq_resp); |
| 846 | kfree(rqstp->rq_argp); |
| 847 | kfree(rqstp->rq_auth_data); |
Jeff Layton | 1b6dc1d | 2015-06-08 12:11:10 -0700 | [diff] [blame] | 848 | kfree_rcu(rqstp, rq_rcu_head); |
| 849 | } |
| 850 | EXPORT_SYMBOL_GPL(svc_rqst_free); |
| 851 | |
| 852 | void |
| 853 | svc_exit_thread(struct svc_rqst *rqstp) |
| 854 | { |
| 855 | struct svc_serv *serv = rqstp->rq_server; |
| 856 | struct svc_pool *pool = rqstp->rq_pool; |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 857 | |
| 858 | spin_lock_bh(&pool->sp_lock); |
| 859 | pool->sp_nrthreads--; |
Jeff Layton | 8124438 | 2014-11-21 14:19:28 -0500 | [diff] [blame] | 860 | if (!test_and_set_bit(RQ_VICTIM, &rqstp->rq_flags)) |
| 861 | list_del_rcu(&rqstp->rq_all); |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 862 | spin_unlock_bh(&pool->sp_lock); |
| 863 | |
Jeff Layton | 1b6dc1d | 2015-06-08 12:11:10 -0700 | [diff] [blame] | 864 | svc_rqst_free(rqstp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | |
| 866 | /* Release the server */ |
| 867 | if (serv) |
| 868 | svc_destroy(serv); |
| 869 | } |
Trond Myklebust | 24c3767 | 2008-12-23 16:30:12 -0500 | [diff] [blame] | 870 | EXPORT_SYMBOL_GPL(svc_exit_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | |
| 872 | /* |
Chuck Lever | 2c7eb0b | 2008-09-15 16:27:23 -0500 | [diff] [blame] | 873 | * Register an "inet" protocol family netid with the local |
| 874 | * rpcbind daemon via an rpcbind v4 SET request. |
Chuck Lever | a26cfad | 2008-08-18 19:34:16 -0400 | [diff] [blame] | 875 | * |
Chuck Lever | 2c7eb0b | 2008-09-15 16:27:23 -0500 | [diff] [blame] | 876 | * No netconfig infrastructure is available in the kernel, so |
| 877 | * we map IP_ protocol numbers to netids by hand. |
Chuck Lever | a26cfad | 2008-08-18 19:34:16 -0400 | [diff] [blame] | 878 | * |
Chuck Lever | 2c7eb0b | 2008-09-15 16:27:23 -0500 | [diff] [blame] | 879 | * Returns zero on success; a negative errno value is returned |
| 880 | * if any error occurs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | */ |
Stanislav Kinsbursky | 5247fab | 2012-01-13 14:02:48 +0400 | [diff] [blame] | 882 | static int __svc_rpcb_register4(struct net *net, const u32 program, |
| 883 | const u32 version, |
Chuck Lever | 2c7eb0b | 2008-09-15 16:27:23 -0500 | [diff] [blame] | 884 | const unsigned short protocol, |
| 885 | const unsigned short port) |
Chuck Lever | a26cfad | 2008-08-18 19:34:16 -0400 | [diff] [blame] | 886 | { |
Chuck Lever | cadc0fa | 2009-03-18 20:47:36 -0400 | [diff] [blame] | 887 | const struct sockaddr_in sin = { |
Chuck Lever | a26cfad | 2008-08-18 19:34:16 -0400 | [diff] [blame] | 888 | .sin_family = AF_INET, |
| 889 | .sin_addr.s_addr = htonl(INADDR_ANY), |
| 890 | .sin_port = htons(port), |
| 891 | }; |
Chuck Lever | cadc0fa | 2009-03-18 20:47:36 -0400 | [diff] [blame] | 892 | const char *netid; |
| 893 | int error; |
Chuck Lever | 2c7eb0b | 2008-09-15 16:27:23 -0500 | [diff] [blame] | 894 | |
| 895 | switch (protocol) { |
| 896 | case IPPROTO_UDP: |
| 897 | netid = RPCBIND_NETID_UDP; |
| 898 | break; |
| 899 | case IPPROTO_TCP: |
| 900 | netid = RPCBIND_NETID_TCP; |
| 901 | break; |
| 902 | default: |
Chuck Lever | ba5c35e | 2009-03-18 20:46:59 -0400 | [diff] [blame] | 903 | return -ENOPROTOOPT; |
Chuck Lever | 2c7eb0b | 2008-09-15 16:27:23 -0500 | [diff] [blame] | 904 | } |
| 905 | |
Stanislav Kinsbursky | 5247fab | 2012-01-13 14:02:48 +0400 | [diff] [blame] | 906 | error = rpcb_v4_register(net, program, version, |
Chuck Lever | cadc0fa | 2009-03-18 20:47:36 -0400 | [diff] [blame] | 907 | (const struct sockaddr *)&sin, netid); |
| 908 | |
| 909 | /* |
| 910 | * User space didn't support rpcbind v4, so retry this |
| 911 | * registration request with the legacy rpcbind v2 protocol. |
| 912 | */ |
| 913 | if (error == -EPROTONOSUPPORT) |
Stanislav Kinsbursky | 5247fab | 2012-01-13 14:02:48 +0400 | [diff] [blame] | 914 | error = rpcb_register(net, program, version, protocol, port); |
Chuck Lever | cadc0fa | 2009-03-18 20:47:36 -0400 | [diff] [blame] | 915 | |
| 916 | return error; |
Chuck Lever | 2c7eb0b | 2008-09-15 16:27:23 -0500 | [diff] [blame] | 917 | } |
| 918 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 919 | #if IS_ENABLED(CONFIG_IPV6) |
Chuck Lever | 2c7eb0b | 2008-09-15 16:27:23 -0500 | [diff] [blame] | 920 | /* |
| 921 | * Register an "inet6" protocol family netid with the local |
| 922 | * rpcbind daemon via an rpcbind v4 SET request. |
| 923 | * |
| 924 | * No netconfig infrastructure is available in the kernel, so |
| 925 | * we map IP_ protocol numbers to netids by hand. |
| 926 | * |
| 927 | * Returns zero on success; a negative errno value is returned |
| 928 | * if any error occurs. |
| 929 | */ |
Stanislav Kinsbursky | 5247fab | 2012-01-13 14:02:48 +0400 | [diff] [blame] | 930 | static int __svc_rpcb_register6(struct net *net, const u32 program, |
| 931 | const u32 version, |
Chuck Lever | 2c7eb0b | 2008-09-15 16:27:23 -0500 | [diff] [blame] | 932 | const unsigned short protocol, |
| 933 | const unsigned short port) |
| 934 | { |
Chuck Lever | cadc0fa | 2009-03-18 20:47:36 -0400 | [diff] [blame] | 935 | const struct sockaddr_in6 sin6 = { |
Chuck Lever | a26cfad | 2008-08-18 19:34:16 -0400 | [diff] [blame] | 936 | .sin6_family = AF_INET6, |
| 937 | .sin6_addr = IN6ADDR_ANY_INIT, |
| 938 | .sin6_port = htons(port), |
| 939 | }; |
Chuck Lever | cadc0fa | 2009-03-18 20:47:36 -0400 | [diff] [blame] | 940 | const char *netid; |
| 941 | int error; |
Chuck Lever | a26cfad | 2008-08-18 19:34:16 -0400 | [diff] [blame] | 942 | |
Chuck Lever | 2c7eb0b | 2008-09-15 16:27:23 -0500 | [diff] [blame] | 943 | switch (protocol) { |
| 944 | case IPPROTO_UDP: |
| 945 | netid = RPCBIND_NETID_UDP6; |
| 946 | break; |
| 947 | case IPPROTO_TCP: |
| 948 | netid = RPCBIND_NETID_TCP6; |
| 949 | break; |
| 950 | default: |
Chuck Lever | ba5c35e | 2009-03-18 20:46:59 -0400 | [diff] [blame] | 951 | return -ENOPROTOOPT; |
Chuck Lever | 2c7eb0b | 2008-09-15 16:27:23 -0500 | [diff] [blame] | 952 | } |
| 953 | |
Stanislav Kinsbursky | 5247fab | 2012-01-13 14:02:48 +0400 | [diff] [blame] | 954 | error = rpcb_v4_register(net, program, version, |
Chuck Lever | cadc0fa | 2009-03-18 20:47:36 -0400 | [diff] [blame] | 955 | (const struct sockaddr *)&sin6, netid); |
| 956 | |
| 957 | /* |
| 958 | * User space didn't support rpcbind version 4, so we won't |
| 959 | * use a PF_INET6 listener. |
| 960 | */ |
| 961 | if (error == -EPROTONOSUPPORT) |
| 962 | error = -EAFNOSUPPORT; |
| 963 | |
| 964 | return error; |
Chuck Lever | 2c7eb0b | 2008-09-15 16:27:23 -0500 | [diff] [blame] | 965 | } |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 966 | #endif /* IS_ENABLED(CONFIG_IPV6) */ |
Chuck Lever | 2c7eb0b | 2008-09-15 16:27:23 -0500 | [diff] [blame] | 967 | |
| 968 | /* |
| 969 | * Register a kernel RPC service via rpcbind version 4. |
| 970 | * |
| 971 | * Returns zero on success; a negative errno value is returned |
| 972 | * if any error occurs. |
| 973 | */ |
Stanislav Kinsbursky | 5247fab | 2012-01-13 14:02:48 +0400 | [diff] [blame] | 974 | static int __svc_register(struct net *net, const char *progname, |
Chuck Lever | 363f724 | 2009-03-18 20:47:44 -0400 | [diff] [blame] | 975 | const u32 program, const u32 version, |
Chuck Lever | 4b62e58 | 2009-03-18 20:46:06 -0400 | [diff] [blame] | 976 | const int family, |
Chuck Lever | 2c7eb0b | 2008-09-15 16:27:23 -0500 | [diff] [blame] | 977 | const unsigned short protocol, |
| 978 | const unsigned short port) |
| 979 | { |
Chuck Lever | 363f724 | 2009-03-18 20:47:44 -0400 | [diff] [blame] | 980 | int error = -EAFNOSUPPORT; |
Chuck Lever | 2c7eb0b | 2008-09-15 16:27:23 -0500 | [diff] [blame] | 981 | |
Chuck Lever | a26cfad | 2008-08-18 19:34:16 -0400 | [diff] [blame] | 982 | switch (family) { |
Chuck Lever | 4b62e58 | 2009-03-18 20:46:06 -0400 | [diff] [blame] | 983 | case PF_INET: |
Stanislav Kinsbursky | 5247fab | 2012-01-13 14:02:48 +0400 | [diff] [blame] | 984 | error = __svc_rpcb_register4(net, program, version, |
Chuck Lever | 2c7eb0b | 2008-09-15 16:27:23 -0500 | [diff] [blame] | 985 | protocol, port); |
Chuck Lever | cadc0fa | 2009-03-18 20:47:36 -0400 | [diff] [blame] | 986 | break; |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 987 | #if IS_ENABLED(CONFIG_IPV6) |
Chuck Lever | 4b62e58 | 2009-03-18 20:46:06 -0400 | [diff] [blame] | 988 | case PF_INET6: |
Stanislav Kinsbursky | 5247fab | 2012-01-13 14:02:48 +0400 | [diff] [blame] | 989 | error = __svc_rpcb_register6(net, program, version, |
Chuck Lever | 2c7eb0b | 2008-09-15 16:27:23 -0500 | [diff] [blame] | 990 | protocol, port); |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 991 | #endif |
Chuck Lever | a26cfad | 2008-08-18 19:34:16 -0400 | [diff] [blame] | 992 | } |
| 993 | |
Chuck Lever | 363f724 | 2009-03-18 20:47:44 -0400 | [diff] [blame] | 994 | return error; |
Chuck Lever | a26cfad | 2008-08-18 19:34:16 -0400 | [diff] [blame] | 995 | } |
Chuck Lever | 2c7eb0b | 2008-09-15 16:27:23 -0500 | [diff] [blame] | 996 | |
Trond Myklebust | 642ee6b | 2019-04-09 11:46:17 -0400 | [diff] [blame] | 997 | int svc_rpcbind_set_version(struct net *net, |
| 998 | const struct svc_program *progp, |
| 999 | u32 version, int family, |
| 1000 | unsigned short proto, |
| 1001 | unsigned short port) |
| 1002 | { |
| 1003 | dprintk("svc: svc_register(%sv%d, %s, %u, %u)\n", |
| 1004 | progp->pg_name, version, |
| 1005 | proto == IPPROTO_UDP? "udp" : "tcp", |
| 1006 | port, family); |
| 1007 | |
| 1008 | return __svc_register(net, progp->pg_name, progp->pg_prog, |
| 1009 | version, family, proto, port); |
| 1010 | |
| 1011 | } |
| 1012 | EXPORT_SYMBOL_GPL(svc_rpcbind_set_version); |
| 1013 | |
| 1014 | int svc_generic_rpcbind_set(struct net *net, |
| 1015 | const struct svc_program *progp, |
| 1016 | u32 version, int family, |
| 1017 | unsigned short proto, |
| 1018 | unsigned short port) |
| 1019 | { |
| 1020 | const struct svc_version *vers = progp->pg_vers[version]; |
| 1021 | int error; |
| 1022 | |
| 1023 | if (vers == NULL) |
| 1024 | return 0; |
| 1025 | |
| 1026 | if (vers->vs_hidden) { |
| 1027 | dprintk("svc: svc_register(%sv%d, %s, %u, %u)" |
| 1028 | " (but not telling portmap)\n", |
| 1029 | progp->pg_name, version, |
| 1030 | proto == IPPROTO_UDP? "udp" : "tcp", |
| 1031 | port, family); |
| 1032 | return 0; |
| 1033 | } |
| 1034 | |
| 1035 | /* |
| 1036 | * Don't register a UDP port if we need congestion |
| 1037 | * control. |
| 1038 | */ |
| 1039 | if (vers->vs_need_cong_ctrl && proto == IPPROTO_UDP) |
| 1040 | return 0; |
| 1041 | |
| 1042 | error = svc_rpcbind_set_version(net, progp, version, |
| 1043 | family, proto, port); |
| 1044 | |
| 1045 | return (vers->vs_rpcb_optnl) ? 0 : error; |
| 1046 | } |
| 1047 | EXPORT_SYMBOL_GPL(svc_generic_rpcbind_set); |
| 1048 | |
Chuck Lever | a26cfad | 2008-08-18 19:34:16 -0400 | [diff] [blame] | 1049 | /** |
| 1050 | * svc_register - register an RPC service with the local portmapper |
| 1051 | * @serv: svc_serv struct for the service to register |
Stanislav Kinsbursky | 5247fab | 2012-01-13 14:02:48 +0400 | [diff] [blame] | 1052 | * @net: net namespace for the service to register |
Chuck Lever | 4b62e58 | 2009-03-18 20:46:06 -0400 | [diff] [blame] | 1053 | * @family: protocol family of service's listener socket |
Chuck Lever | a26cfad | 2008-08-18 19:34:16 -0400 | [diff] [blame] | 1054 | * @proto: transport protocol number to advertise |
| 1055 | * @port: port to advertise |
| 1056 | * |
Chuck Lever | 4b62e58 | 2009-03-18 20:46:06 -0400 | [diff] [blame] | 1057 | * Service is registered for any address in the passed-in protocol family |
Chuck Lever | a26cfad | 2008-08-18 19:34:16 -0400 | [diff] [blame] | 1058 | */ |
Stanislav Kinsbursky | 5247fab | 2012-01-13 14:02:48 +0400 | [diff] [blame] | 1059 | int svc_register(const struct svc_serv *serv, struct net *net, |
| 1060 | const int family, const unsigned short proto, |
| 1061 | const unsigned short port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | { |
| 1063 | struct svc_program *progp; |
Chuck Lever | ea339d4 | 2007-10-26 13:32:56 -0400 | [diff] [blame] | 1064 | unsigned int i; |
Chuck Lever | 14aeb21 | 2008-08-18 19:34:00 -0400 | [diff] [blame] | 1065 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | |
Weston Andros Adamson | 0af3950 | 2012-10-23 10:43:45 -0400 | [diff] [blame] | 1067 | WARN_ON_ONCE(proto == 0 && port == 0); |
| 1068 | if (proto == 0 && port == 0) |
| 1069 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | |
Olaf Kirch | bc5fea4 | 2006-10-04 02:16:05 -0700 | [diff] [blame] | 1071 | for (progp = serv->sv_program; progp; progp = progp->pg_next) { |
| 1072 | for (i = 0; i < progp->pg_nvers; i++) { |
Olaf Kirch | bc5fea4 | 2006-10-04 02:16:05 -0700 | [diff] [blame] | 1073 | |
Trond Myklebust | 642ee6b | 2019-04-09 11:46:17 -0400 | [diff] [blame] | 1074 | error = progp->pg_rpcbind_set(net, progp, i, |
| 1075 | family, proto, port); |
Kinglong Mee | 7e55b59 | 2013-12-31 13:17:20 +0800 | [diff] [blame] | 1076 | if (error < 0) { |
| 1077 | printk(KERN_WARNING "svc: failed to register " |
| 1078 | "%sv%u RPC service (errno %d).\n", |
| 1079 | progp->pg_name, i, -error); |
Olaf Kirch | bc5fea4 | 2006-10-04 02:16:05 -0700 | [diff] [blame] | 1080 | break; |
Kinglong Mee | 7e55b59 | 2013-12-31 13:17:20 +0800 | [diff] [blame] | 1081 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | } |
| 1083 | } |
| 1084 | |
Chuck Lever | 7252d57 | 2008-08-18 19:34:08 -0400 | [diff] [blame] | 1085 | return error; |
| 1086 | } |
| 1087 | |
Chuck Lever | d5a8620 | 2009-03-18 20:47:29 -0400 | [diff] [blame] | 1088 | /* |
| 1089 | * If user space is running rpcbind, it should take the v4 UNSET |
| 1090 | * and clear everything for this [program, version]. If user space |
| 1091 | * is running portmap, it will reject the v4 UNSET, but won't have |
| 1092 | * any "inet6" entries anyway. So a PMAP_UNSET should be sufficient |
| 1093 | * in this case to clear all existing entries for [program, version]. |
| 1094 | */ |
Stanislav Kinsbursky | 5247fab | 2012-01-13 14:02:48 +0400 | [diff] [blame] | 1095 | static void __svc_unregister(struct net *net, const u32 program, const u32 version, |
Chuck Lever | f6fb3f6 | 2008-09-25 11:56:57 -0400 | [diff] [blame] | 1096 | const char *progname) |
| 1097 | { |
| 1098 | int error; |
| 1099 | |
Stanislav Kinsbursky | 5247fab | 2012-01-13 14:02:48 +0400 | [diff] [blame] | 1100 | error = rpcb_v4_register(net, program, version, NULL, ""); |
Chuck Lever | d5a8620 | 2009-03-18 20:47:29 -0400 | [diff] [blame] | 1101 | |
| 1102 | /* |
| 1103 | * User space didn't support rpcbind v4, so retry this |
| 1104 | * request with the legacy rpcbind v2 protocol. |
| 1105 | */ |
| 1106 | if (error == -EPROTONOSUPPORT) |
Stanislav Kinsbursky | 5247fab | 2012-01-13 14:02:48 +0400 | [diff] [blame] | 1107 | error = rpcb_register(net, program, version, 0, 0); |
Chuck Lever | d5a8620 | 2009-03-18 20:47:29 -0400 | [diff] [blame] | 1108 | |
Chuck Lever | f6fb3f6 | 2008-09-25 11:56:57 -0400 | [diff] [blame] | 1109 | dprintk("svc: %s(%sv%u), error %d\n", |
| 1110 | __func__, progname, version, error); |
| 1111 | } |
| 1112 | |
Chuck Lever | 7252d57 | 2008-08-18 19:34:08 -0400 | [diff] [blame] | 1113 | /* |
Chuck Lever | f6fb3f6 | 2008-09-25 11:56:57 -0400 | [diff] [blame] | 1114 | * All netids, bind addresses and ports registered for [program, version] |
| 1115 | * are removed from the local rpcbind database (if the service is not |
| 1116 | * hidden) to make way for a new instance of the service. |
Chuck Lever | 7252d57 | 2008-08-18 19:34:08 -0400 | [diff] [blame] | 1117 | * |
Chuck Lever | f6fb3f6 | 2008-09-25 11:56:57 -0400 | [diff] [blame] | 1118 | * The result of unregistration is reported via dprintk for those who want |
| 1119 | * verification of the result, but is otherwise not important. |
Chuck Lever | 7252d57 | 2008-08-18 19:34:08 -0400 | [diff] [blame] | 1120 | */ |
Stanislav Kinsbursky | 5247fab | 2012-01-13 14:02:48 +0400 | [diff] [blame] | 1121 | static void svc_unregister(const struct svc_serv *serv, struct net *net) |
Chuck Lever | 7252d57 | 2008-08-18 19:34:08 -0400 | [diff] [blame] | 1122 | { |
| 1123 | struct svc_program *progp; |
| 1124 | unsigned long flags; |
| 1125 | unsigned int i; |
Chuck Lever | 7252d57 | 2008-08-18 19:34:08 -0400 | [diff] [blame] | 1126 | |
| 1127 | clear_thread_flag(TIF_SIGPENDING); |
| 1128 | |
| 1129 | for (progp = serv->sv_program; progp; progp = progp->pg_next) { |
| 1130 | for (i = 0; i < progp->pg_nvers; i++) { |
| 1131 | if (progp->pg_vers[i] == NULL) |
| 1132 | continue; |
| 1133 | if (progp->pg_vers[i]->vs_hidden) |
| 1134 | continue; |
| 1135 | |
Chuck Lever | 7402ab19 | 2011-05-09 15:22:55 -0400 | [diff] [blame] | 1136 | dprintk("svc: attempting to unregister %sv%u\n", |
| 1137 | progp->pg_name, i); |
Stanislav Kinsbursky | 5247fab | 2012-01-13 14:02:48 +0400 | [diff] [blame] | 1138 | __svc_unregister(net, progp->pg_prog, i, progp->pg_name); |
Chuck Lever | 7252d57 | 2008-08-18 19:34:08 -0400 | [diff] [blame] | 1139 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
Chuck Lever | 7252d57 | 2008-08-18 19:34:08 -0400 | [diff] [blame] | 1142 | spin_lock_irqsave(¤t->sighand->siglock, flags); |
| 1143 | recalc_sigpending(); |
| 1144 | spin_unlock_irqrestore(¤t->sighand->siglock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | /* |
J. Bruce Fields | 7032a3d | 2012-10-09 18:33:38 -0400 | [diff] [blame] | 1148 | * dprintk the given error with the address of the client that caused it. |
Dr. David Alan Gilbert | 354ecbb | 2007-08-25 16:09:27 +0100 | [diff] [blame] | 1149 | */ |
Jeff Layton | f895b25 | 2014-11-17 16:58:04 -0500 | [diff] [blame] | 1150 | #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) |
Joe Perches | b9075fa | 2011-10-31 17:11:33 -0700 | [diff] [blame] | 1151 | static __printf(2, 3) |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 1152 | void svc_printk(struct svc_rqst *rqstp, const char *fmt, ...) |
Dr. David Alan Gilbert | 354ecbb | 2007-08-25 16:09:27 +0100 | [diff] [blame] | 1153 | { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 1154 | struct va_format vaf; |
Dr. David Alan Gilbert | 354ecbb | 2007-08-25 16:09:27 +0100 | [diff] [blame] | 1155 | va_list args; |
Dr. David Alan Gilbert | 354ecbb | 2007-08-25 16:09:27 +0100 | [diff] [blame] | 1156 | char buf[RPC_MAX_ADDRBUFLEN]; |
| 1157 | |
Dr. David Alan Gilbert | 354ecbb | 2007-08-25 16:09:27 +0100 | [diff] [blame] | 1158 | va_start(args, fmt); |
Dr. David Alan Gilbert | 354ecbb | 2007-08-25 16:09:27 +0100 | [diff] [blame] | 1159 | |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 1160 | vaf.fmt = fmt; |
| 1161 | vaf.va = &args; |
| 1162 | |
J. Bruce Fields | 7032a3d | 2012-10-09 18:33:38 -0400 | [diff] [blame] | 1163 | dprintk("svc: %s: %pV", svc_print_addr(rqstp, buf, sizeof(buf)), &vaf); |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 1164 | |
| 1165 | va_end(args); |
Dr. David Alan Gilbert | 354ecbb | 2007-08-25 16:09:27 +0100 | [diff] [blame] | 1166 | } |
J. Bruce Fields | 624ab46 | 2013-01-08 16:22:15 -0500 | [diff] [blame] | 1167 | #else |
| 1168 | static __printf(2,3) void svc_printk(struct svc_rqst *rqstp, const char *fmt, ...) {} |
| 1169 | #endif |
Dr. David Alan Gilbert | 354ecbb | 2007-08-25 16:09:27 +0100 | [diff] [blame] | 1170 | |
Trond Myklebust | 83dd59a | 2019-04-09 11:46:14 -0400 | [diff] [blame] | 1171 | __be32 |
| 1172 | svc_return_autherr(struct svc_rqst *rqstp, __be32 auth_err) |
| 1173 | { |
| 1174 | set_bit(RQ_AUTHERR, &rqstp->rq_flags); |
| 1175 | return auth_err; |
| 1176 | } |
| 1177 | EXPORT_SYMBOL_GPL(svc_return_autherr); |
| 1178 | |
| 1179 | static __be32 |
| 1180 | svc_get_autherr(struct svc_rqst *rqstp, __be32 *statp) |
| 1181 | { |
| 1182 | if (test_and_clear_bit(RQ_AUTHERR, &rqstp->rq_flags)) |
| 1183 | return *statp; |
| 1184 | return rpc_auth_ok; |
| 1185 | } |
| 1186 | |
Trond Myklebust | 4532608 | 2019-04-09 11:46:16 -0400 | [diff] [blame] | 1187 | static int |
| 1188 | svc_generic_dispatch(struct svc_rqst *rqstp, __be32 *statp) |
| 1189 | { |
| 1190 | struct kvec *argv = &rqstp->rq_arg.head[0]; |
| 1191 | struct kvec *resv = &rqstp->rq_res.head[0]; |
| 1192 | const struct svc_procedure *procp = rqstp->rq_procinfo; |
| 1193 | |
| 1194 | /* |
| 1195 | * Decode arguments |
| 1196 | * XXX: why do we ignore the return value? |
| 1197 | */ |
| 1198 | if (procp->pc_decode && |
| 1199 | !procp->pc_decode(rqstp, argv->iov_base)) { |
| 1200 | *statp = rpc_garbage_args; |
| 1201 | return 1; |
| 1202 | } |
| 1203 | |
| 1204 | *statp = procp->pc_func(rqstp); |
| 1205 | |
| 1206 | if (*statp == rpc_drop_reply || |
| 1207 | test_bit(RQ_DROPME, &rqstp->rq_flags)) |
| 1208 | return 0; |
| 1209 | |
| 1210 | if (test_bit(RQ_AUTHERR, &rqstp->rq_flags)) |
| 1211 | return 1; |
| 1212 | |
| 1213 | if (*statp != rpc_success) |
| 1214 | return 1; |
| 1215 | |
| 1216 | /* Encode reply */ |
| 1217 | if (procp->pc_encode && |
| 1218 | !procp->pc_encode(rqstp, resv->iov_base + resv->iov_len)) { |
| 1219 | dprintk("svc: failed to encode reply\n"); |
| 1220 | /* serv->sv_stats->rpcsystemerr++; */ |
| 1221 | *statp = rpc_system_err; |
| 1222 | } |
| 1223 | return 1; |
| 1224 | } |
| 1225 | |
Trond Myklebust | 8e5b677 | 2019-04-09 11:46:15 -0400 | [diff] [blame] | 1226 | __be32 |
| 1227 | svc_generic_init_request(struct svc_rqst *rqstp, |
| 1228 | const struct svc_program *progp, |
| 1229 | struct svc_process_info *ret) |
| 1230 | { |
| 1231 | const struct svc_version *versp = NULL; /* compiler food */ |
| 1232 | const struct svc_procedure *procp = NULL; |
| 1233 | |
| 1234 | if (rqstp->rq_vers >= progp->pg_nvers ) |
| 1235 | goto err_bad_vers; |
Colin Ian King | e41f9ef | 2019-09-25 14:09:30 +0100 | [diff] [blame] | 1236 | versp = progp->pg_vers[rqstp->rq_vers]; |
| 1237 | if (!versp) |
Trond Myklebust | 8e5b677 | 2019-04-09 11:46:15 -0400 | [diff] [blame] | 1238 | goto err_bad_vers; |
| 1239 | |
| 1240 | /* |
| 1241 | * Some protocol versions (namely NFSv4) require some form of |
| 1242 | * congestion control. (See RFC 7530 section 3.1 paragraph 2) |
| 1243 | * In other words, UDP is not allowed. We mark those when setting |
| 1244 | * up the svc_xprt, and verify that here. |
| 1245 | * |
| 1246 | * The spec is not very clear about what error should be returned |
| 1247 | * when someone tries to access a server that is listening on UDP |
| 1248 | * for lower versions. RPC_PROG_MISMATCH seems to be the closest |
| 1249 | * fit. |
| 1250 | */ |
| 1251 | if (versp->vs_need_cong_ctrl && rqstp->rq_xprt && |
| 1252 | !test_bit(XPT_CONG_CTRL, &rqstp->rq_xprt->xpt_flags)) |
| 1253 | goto err_bad_vers; |
| 1254 | |
| 1255 | if (rqstp->rq_proc >= versp->vs_nproc) |
| 1256 | goto err_bad_proc; |
| 1257 | rqstp->rq_procinfo = procp = &versp->vs_proc[rqstp->rq_proc]; |
| 1258 | if (!procp) |
| 1259 | goto err_bad_proc; |
| 1260 | |
| 1261 | /* Initialize storage for argp and resp */ |
| 1262 | memset(rqstp->rq_argp, 0, procp->pc_argsize); |
| 1263 | memset(rqstp->rq_resp, 0, procp->pc_ressize); |
| 1264 | |
| 1265 | /* Bump per-procedure stats counter */ |
| 1266 | versp->vs_count[rqstp->rq_proc]++; |
| 1267 | |
| 1268 | ret->dispatch = versp->vs_dispatch; |
| 1269 | return rpc_success; |
| 1270 | err_bad_vers: |
| 1271 | ret->mismatch.lovers = progp->pg_lovers; |
| 1272 | ret->mismatch.hivers = progp->pg_hivers; |
| 1273 | return rpc_prog_mismatch; |
| 1274 | err_bad_proc: |
| 1275 | return rpc_proc_unavail; |
| 1276 | } |
| 1277 | EXPORT_SYMBOL_GPL(svc_generic_init_request); |
| 1278 | |
Dr. David Alan Gilbert | 354ecbb | 2007-08-25 16:09:27 +0100 | [diff] [blame] | 1279 | /* |
Ricardo Labiaga | 1cad7ea | 2009-04-01 09:23:06 -0400 | [diff] [blame] | 1280 | * Common routine for processing the RPC request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | */ |
Ricardo Labiaga | 1cad7ea | 2009-04-01 09:23:06 -0400 | [diff] [blame] | 1282 | static int |
| 1283 | svc_process_common(struct svc_rqst *rqstp, struct kvec *argv, struct kvec *resv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | { |
| 1285 | struct svc_program *progp; |
Christoph Hellwig | 860bda2 | 2017-05-12 16:11:49 +0200 | [diff] [blame] | 1286 | const struct svc_procedure *procp = NULL; |
NeilBrown | 6fb2b47 | 2006-10-02 02:17:50 -0700 | [diff] [blame] | 1287 | struct svc_serv *serv = rqstp->rq_server; |
Trond Myklebust | 8e5b677 | 2019-04-09 11:46:15 -0400 | [diff] [blame] | 1288 | struct svc_process_info process; |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 1289 | __be32 *statp; |
Trond Myklebust | 8e5b677 | 2019-04-09 11:46:15 -0400 | [diff] [blame] | 1290 | u32 prog, vers; |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 1291 | __be32 auth_stat, rpc_stat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | int auth_res; |
J.Bruce Fields | 8f8e05c | 2006-10-04 02:16:08 -0700 | [diff] [blame] | 1293 | __be32 *reply_statp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | |
| 1295 | rpc_stat = rpc_success; |
| 1296 | |
| 1297 | if (argv->iov_len < 6*4) |
| 1298 | goto err_short_len; |
| 1299 | |
Chuck Lever | 06eb8a5 | 2017-06-19 14:00:40 -0400 | [diff] [blame] | 1300 | /* Will be turned off by GSS integrity and privacy services */ |
Jeff Layton | 779fb0f | 2014-11-19 07:51:18 -0500 | [diff] [blame] | 1301 | set_bit(RQ_SPLICE_OK, &rqstp->rq_flags); |
Andy Adamson | 2f42587 | 2009-04-03 08:27:32 +0300 | [diff] [blame] | 1302 | /* Will be turned off only when NFSv4 Sessions are used */ |
Jeff Layton | 30660e04b | 2014-11-19 07:51:16 -0500 | [diff] [blame] | 1303 | set_bit(RQ_USEDEFERRAL, &rqstp->rq_flags); |
Jeff Layton | 78b65eb | 2014-11-19 07:51:17 -0500 | [diff] [blame] | 1304 | clear_bit(RQ_DROPME, &rqstp->rq_flags); |
Tom Tucker | e831fe6 | 2007-12-30 21:07:29 -0600 | [diff] [blame] | 1305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | svc_putu32(resv, rqstp->rq_xid); |
| 1307 | |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 1308 | vers = svc_getnl(argv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | |
| 1310 | /* First words of reply: */ |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 1311 | svc_putnl(resv, 1); /* REPLY */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | if (vers != 2) /* RPC version number */ |
| 1314 | goto err_bad_rpc; |
| 1315 | |
| 1316 | /* Save position in case we later decide to reject: */ |
J.Bruce Fields | 8f8e05c | 2006-10-04 02:16:08 -0700 | [diff] [blame] | 1317 | reply_statp = resv->iov_base + resv->iov_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 1319 | svc_putnl(resv, 0); /* ACCEPT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 1321 | rqstp->rq_prog = prog = svc_getnl(argv); /* program number */ |
Trond Myklebust | 8e5b677 | 2019-04-09 11:46:15 -0400 | [diff] [blame] | 1322 | rqstp->rq_vers = svc_getnl(argv); /* version number */ |
| 1323 | rqstp->rq_proc = svc_getnl(argv); /* procedure number */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | |
NeilBrown | 80d188a | 2005-11-07 01:00:27 -0800 | [diff] [blame] | 1325 | for (progp = serv->sv_program; progp; progp = progp->pg_next) |
| 1326 | if (prog == progp->pg_prog) |
| 1327 | break; |
| 1328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | /* |
| 1330 | * Decode auth data, and add verifier to reply buffer. |
| 1331 | * We do this before anything else in order to get a decent |
| 1332 | * auth verifier. |
| 1333 | */ |
| 1334 | auth_res = svc_authenticate(rqstp, &auth_stat); |
| 1335 | /* Also give the program a chance to reject this call: */ |
NeilBrown | 80d188a | 2005-11-07 01:00:27 -0800 | [diff] [blame] | 1336 | if (auth_res == SVC_OK && progp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | auth_stat = rpc_autherr_badcred; |
| 1338 | auth_res = progp->pg_authenticate(rqstp); |
| 1339 | } |
Chuck Lever | ff27e9f | 2019-10-24 09:34:10 -0400 | [diff] [blame] | 1340 | if (auth_res != SVC_OK) |
| 1341 | trace_svc_authenticate(rqstp, auth_res, auth_stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | switch (auth_res) { |
| 1343 | case SVC_OK: |
| 1344 | break; |
| 1345 | case SVC_GARBAGE: |
Harshula Jayasuriya | dd35210 | 2008-02-20 10:56:56 +1100 | [diff] [blame] | 1346 | goto err_garbage; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | case SVC_SYSERR: |
| 1348 | rpc_stat = rpc_system_err; |
| 1349 | goto err_bad; |
| 1350 | case SVC_DENIED: |
| 1351 | goto err_bad_auth; |
NeilBrown | 1ebede8 | 2010-08-12 17:04:07 +1000 | [diff] [blame] | 1352 | case SVC_CLOSE: |
Chuck Lever | 4d712ef | 2016-11-29 11:04:34 -0500 | [diff] [blame] | 1353 | goto close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | case SVC_DROP: |
| 1355 | goto dropit; |
| 1356 | case SVC_COMPLETE: |
| 1357 | goto sendit; |
| 1358 | } |
NeilBrown | 80d188a | 2005-11-07 01:00:27 -0800 | [diff] [blame] | 1359 | |
Andreas Gruenbacher | 9ba0263 | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 1360 | if (progp == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | goto err_bad_prog; |
| 1362 | |
Trond Myklebust | 8e5b677 | 2019-04-09 11:46:15 -0400 | [diff] [blame] | 1363 | rpc_stat = progp->pg_init_request(rqstp, progp, &process); |
| 1364 | switch (rpc_stat) { |
| 1365 | case rpc_success: |
| 1366 | break; |
| 1367 | case rpc_prog_unavail: |
| 1368 | goto err_bad_prog; |
| 1369 | case rpc_prog_mismatch: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | goto err_bad_vers; |
Trond Myklebust | 8e5b677 | 2019-04-09 11:46:15 -0400 | [diff] [blame] | 1371 | case rpc_proc_unavail: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | goto err_bad_proc; |
Trond Myklebust | 8e5b677 | 2019-04-09 11:46:15 -0400 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | procp = rqstp->rq_procinfo; |
| 1376 | /* Should this check go into the dispatcher? */ |
| 1377 | if (!procp || !procp->pc_func) |
| 1378 | goto err_bad_proc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | |
| 1380 | /* Syntactic check complete */ |
| 1381 | serv->sv_stats->rpccnt++; |
Chuck Lever | 0b9547b | 2018-03-27 10:51:22 -0400 | [diff] [blame] | 1382 | trace_svc_process(rqstp, progp->pg_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | |
| 1384 | /* Build the reply header. */ |
| 1385 | statp = resv->iov_base +resv->iov_len; |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 1386 | svc_putnl(resv, RPC_SUCCESS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 1388 | /* un-reserve some of the out-queue now that we have a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | * better idea of reply size |
| 1390 | */ |
| 1391 | if (procp->pc_xdrressize) |
Jeff Layton | cd123012 | 2007-05-09 02:34:50 -0700 | [diff] [blame] | 1392 | svc_reserve_auth(rqstp, procp->pc_xdrressize<<2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | |
| 1394 | /* Call the function that processes the request. */ |
Trond Myklebust | 8e5b677 | 2019-04-09 11:46:15 -0400 | [diff] [blame] | 1395 | if (!process.dispatch) { |
Trond Myklebust | 4532608 | 2019-04-09 11:46:16 -0400 | [diff] [blame] | 1396 | if (!svc_generic_dispatch(rqstp, statp)) |
| 1397 | goto release_dropit; |
| 1398 | if (*statp == rpc_garbage_args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | goto err_garbage; |
Trond Myklebust | 83dd59a | 2019-04-09 11:46:14 -0400 | [diff] [blame] | 1400 | auth_stat = svc_get_autherr(rqstp, statp); |
| 1401 | if (auth_stat != rpc_auth_ok) |
| 1402 | goto err_release_bad_auth; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | } else { |
| 1404 | dprintk("svc: calling dispatcher\n"); |
Trond Myklebust | 4532608 | 2019-04-09 11:46:16 -0400 | [diff] [blame] | 1405 | if (!process.dispatch(rqstp, statp)) |
| 1406 | goto release_dropit; /* Release reply info */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | /* Check RPC status result */ |
| 1410 | if (*statp != rpc_success) |
| 1411 | resv->iov_len = ((void*)statp) - resv->iov_base + 4; |
| 1412 | |
| 1413 | /* Release reply info */ |
| 1414 | if (procp->pc_release) |
Christoph Hellwig | 8537488 | 2017-05-08 18:48:24 +0200 | [diff] [blame] | 1415 | procp->pc_release(rqstp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | |
| 1417 | if (procp->pc_encode == NULL) |
| 1418 | goto dropit; |
| 1419 | |
| 1420 | sendit: |
| 1421 | if (svc_authorise(rqstp)) |
Chuck Lever | 4d712ef | 2016-11-29 11:04:34 -0500 | [diff] [blame] | 1422 | goto close; |
Ricardo Labiaga | 1cad7ea | 2009-04-01 09:23:06 -0400 | [diff] [blame] | 1423 | return 1; /* Caller can now send it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | |
Trond Myklebust | 4532608 | 2019-04-09 11:46:16 -0400 | [diff] [blame] | 1425 | release_dropit: |
| 1426 | if (procp->pc_release) |
| 1427 | procp->pc_release(rqstp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | dropit: |
| 1429 | svc_authorise(rqstp); /* doesn't hurt to call this twice */ |
| 1430 | dprintk("svc: svc_process dropit\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | return 0; |
| 1432 | |
Chuck Lever | 4d712ef | 2016-11-29 11:04:34 -0500 | [diff] [blame] | 1433 | close: |
Vasily Averin | d4b09ac | 2018-12-24 14:44:52 +0300 | [diff] [blame] | 1434 | if (rqstp->rq_xprt && test_bit(XPT_TEMP, &rqstp->rq_xprt->xpt_flags)) |
Chuck Lever | 4d712ef | 2016-11-29 11:04:34 -0500 | [diff] [blame] | 1435 | svc_close_xprt(rqstp->rq_xprt); |
| 1436 | dprintk("svc: svc_process close\n"); |
| 1437 | return 0; |
| 1438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | err_short_len: |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 1440 | svc_printk(rqstp, "short len %zd, dropping request\n", |
Dr. David Alan Gilbert | 354ecbb | 2007-08-25 16:09:27 +0100 | [diff] [blame] | 1441 | argv->iov_len); |
Chuck Lever | 4d712ef | 2016-11-29 11:04:34 -0500 | [diff] [blame] | 1442 | goto close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | err_bad_rpc: |
| 1445 | serv->sv_stats->rpcbadfmt++; |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 1446 | svc_putnl(resv, 1); /* REJECT */ |
| 1447 | svc_putnl(resv, 0); /* RPC_MISMATCH */ |
| 1448 | svc_putnl(resv, 2); /* Only RPCv2 supported */ |
| 1449 | svc_putnl(resv, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | goto sendit; |
| 1451 | |
Trond Myklebust | 83dd59a | 2019-04-09 11:46:14 -0400 | [diff] [blame] | 1452 | err_release_bad_auth: |
| 1453 | if (procp->pc_release) |
| 1454 | procp->pc_release(rqstp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | err_bad_auth: |
| 1456 | dprintk("svc: authentication failed (%d)\n", ntohl(auth_stat)); |
| 1457 | serv->sv_stats->rpcbadauth++; |
| 1458 | /* Restore write pointer to location of accept status: */ |
J.Bruce Fields | 8f8e05c | 2006-10-04 02:16:08 -0700 | [diff] [blame] | 1459 | xdr_ressize_check(rqstp, reply_statp); |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 1460 | svc_putnl(resv, 1); /* REJECT */ |
| 1461 | svc_putnl(resv, 1); /* AUTH_ERROR */ |
| 1462 | svc_putnl(resv, ntohl(auth_stat)); /* status */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | goto sendit; |
| 1464 | |
| 1465 | err_bad_prog: |
Andreas Gruenbacher | 9ba0263 | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 1466 | dprintk("svc: unknown program %d\n", prog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | serv->sv_stats->rpcbadfmt++; |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 1468 | svc_putnl(resv, RPC_PROG_UNAVAIL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | goto sendit; |
| 1470 | |
| 1471 | err_bad_vers: |
Dr. David Alan Gilbert | 354ecbb | 2007-08-25 16:09:27 +0100 | [diff] [blame] | 1472 | svc_printk(rqstp, "unknown version (%d for prog %d, %s)\n", |
Trond Myklebust | 8e5b677 | 2019-04-09 11:46:15 -0400 | [diff] [blame] | 1473 | rqstp->rq_vers, rqstp->rq_prog, progp->pg_name); |
NeilBrown | 34e9a63 | 2007-01-29 13:19:52 -0800 | [diff] [blame] | 1474 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | serv->sv_stats->rpcbadfmt++; |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 1476 | svc_putnl(resv, RPC_PROG_MISMATCH); |
Trond Myklebust | 8e5b677 | 2019-04-09 11:46:15 -0400 | [diff] [blame] | 1477 | svc_putnl(resv, process.mismatch.lovers); |
| 1478 | svc_putnl(resv, process.mismatch.hivers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | goto sendit; |
| 1480 | |
| 1481 | err_bad_proc: |
Trond Myklebust | 8e5b677 | 2019-04-09 11:46:15 -0400 | [diff] [blame] | 1482 | svc_printk(rqstp, "unknown procedure (%d)\n", rqstp->rq_proc); |
NeilBrown | 34e9a63 | 2007-01-29 13:19:52 -0800 | [diff] [blame] | 1483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | serv->sv_stats->rpcbadfmt++; |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 1485 | svc_putnl(resv, RPC_PROC_UNAVAIL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | goto sendit; |
| 1487 | |
| 1488 | err_garbage: |
Dr. David Alan Gilbert | 354ecbb | 2007-08-25 16:09:27 +0100 | [diff] [blame] | 1489 | svc_printk(rqstp, "failed to decode args\n"); |
NeilBrown | 34e9a63 | 2007-01-29 13:19:52 -0800 | [diff] [blame] | 1490 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | rpc_stat = rpc_garbage_args; |
| 1492 | err_bad: |
| 1493 | serv->sv_stats->rpcbadfmt++; |
Alexey Dobriyan | 7699431 | 2006-09-26 22:28:46 -0700 | [diff] [blame] | 1494 | svc_putnl(resv, ntohl(rpc_stat)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | goto sendit; |
| 1496 | } |
Greg Banks | 7adae48 | 2006-10-04 02:15:47 -0700 | [diff] [blame] | 1497 | |
| 1498 | /* |
Ricardo Labiaga | 1cad7ea | 2009-04-01 09:23:06 -0400 | [diff] [blame] | 1499 | * Process the RPC request. |
| 1500 | */ |
| 1501 | int |
| 1502 | svc_process(struct svc_rqst *rqstp) |
| 1503 | { |
| 1504 | struct kvec *argv = &rqstp->rq_arg.head[0]; |
| 1505 | struct kvec *resv = &rqstp->rq_res.head[0]; |
| 1506 | struct svc_serv *serv = rqstp->rq_server; |
| 1507 | u32 dir; |
Ricardo Labiaga | 1cad7ea | 2009-04-01 09:23:06 -0400 | [diff] [blame] | 1508 | |
| 1509 | /* |
| 1510 | * Setup response xdr_buf. |
| 1511 | * Initially it has just one page |
| 1512 | */ |
J. Bruce Fields | afc5940 | 2012-12-10 18:01:37 -0500 | [diff] [blame] | 1513 | rqstp->rq_next_page = &rqstp->rq_respages[1]; |
Ricardo Labiaga | 1cad7ea | 2009-04-01 09:23:06 -0400 | [diff] [blame] | 1514 | resv->iov_base = page_address(rqstp->rq_respages[0]); |
| 1515 | resv->iov_len = 0; |
| 1516 | rqstp->rq_res.pages = rqstp->rq_respages + 1; |
| 1517 | rqstp->rq_res.len = 0; |
| 1518 | rqstp->rq_res.page_base = 0; |
| 1519 | rqstp->rq_res.page_len = 0; |
| 1520 | rqstp->rq_res.buflen = PAGE_SIZE; |
| 1521 | rqstp->rq_res.tail[0].iov_base = NULL; |
| 1522 | rqstp->rq_res.tail[0].iov_len = 0; |
| 1523 | |
Ricardo Labiaga | 1cad7ea | 2009-04-01 09:23:06 -0400 | [diff] [blame] | 1524 | dir = svc_getnl(argv); |
| 1525 | if (dir != 0) { |
| 1526 | /* direction != CALL */ |
| 1527 | svc_printk(rqstp, "bad direction %d, dropping request\n", dir); |
| 1528 | serv->sv_stats->rpcbadfmt++; |
Jeff Layton | 860a0d9 | 2014-10-28 14:24:12 -0400 | [diff] [blame] | 1529 | goto out_drop; |
Ricardo Labiaga | 1cad7ea | 2009-04-01 09:23:06 -0400 | [diff] [blame] | 1530 | } |
| 1531 | |
Chuck Lever | 067fb11 | 2019-02-11 11:24:37 -0500 | [diff] [blame] | 1532 | /* Reserve space for the record marker */ |
| 1533 | if (rqstp->rq_prot == IPPROTO_TCP) |
| 1534 | svc_putnl(resv, 0); |
| 1535 | |
Andy Adamson | 4b5b3ba | 2011-01-06 02:04:24 +0000 | [diff] [blame] | 1536 | /* Returns 1 for send, 0 for drop */ |
Chuck Lever | 0b9547b | 2018-03-27 10:51:22 -0400 | [diff] [blame] | 1537 | if (likely(svc_process_common(rqstp, argv, resv))) |
| 1538 | return svc_send(rqstp); |
Jeff Layton | 860a0d9 | 2014-10-28 14:24:12 -0400 | [diff] [blame] | 1539 | |
Jeff Layton | 860a0d9 | 2014-10-28 14:24:12 -0400 | [diff] [blame] | 1540 | out_drop: |
Jeff Layton | 860a0d9 | 2014-10-28 14:24:12 -0400 | [diff] [blame] | 1541 | svc_drop(rqstp); |
| 1542 | return 0; |
Ricardo Labiaga | 1cad7ea | 2009-04-01 09:23:06 -0400 | [diff] [blame] | 1543 | } |
Chuck Lever | 3f87d5d | 2015-05-26 13:48:57 -0400 | [diff] [blame] | 1544 | EXPORT_SYMBOL_GPL(svc_process); |
Ricardo Labiaga | 1cad7ea | 2009-04-01 09:23:06 -0400 | [diff] [blame] | 1545 | |
Trond Myklebust | 9e00abc | 2011-07-13 19:20:49 -0400 | [diff] [blame] | 1546 | #if defined(CONFIG_SUNRPC_BACKCHANNEL) |
Ricardo Labiaga | 4d6bbb6 | 2009-04-01 09:23:07 -0400 | [diff] [blame] | 1547 | /* |
| 1548 | * Process a backchannel RPC request that arrived over an existing |
| 1549 | * outbound connection |
| 1550 | */ |
| 1551 | int |
| 1552 | bc_svc_process(struct svc_serv *serv, struct rpc_rqst *req, |
| 1553 | struct svc_rqst *rqstp) |
| 1554 | { |
| 1555 | struct kvec *argv = &rqstp->rq_arg.head[0]; |
| 1556 | struct kvec *resv = &rqstp->rq_res.head[0]; |
Chuck Lever | 632dda8 | 2015-05-11 14:04:50 -0400 | [diff] [blame] | 1557 | struct rpc_task *task; |
Trond Myklebust | 0d2a970 | 2015-06-04 15:37:10 -0400 | [diff] [blame] | 1558 | int proc_error; |
Chuck Lever | 632dda8 | 2015-05-11 14:04:50 -0400 | [diff] [blame] | 1559 | int error; |
| 1560 | |
| 1561 | dprintk("svc: %s(%p)\n", __func__, req); |
Ricardo Labiaga | 4d6bbb6 | 2009-04-01 09:23:07 -0400 | [diff] [blame] | 1562 | |
| 1563 | /* Build the svc_rqst used by the common processing routine */ |
| 1564 | rqstp->rq_xid = req->rq_xid; |
| 1565 | rqstp->rq_prot = req->rq_xprt->prot; |
| 1566 | rqstp->rq_server = serv; |
Vasily Averin | d4b09ac | 2018-12-24 14:44:52 +0300 | [diff] [blame] | 1567 | rqstp->rq_bc_net = req->rq_xprt->xprt_net; |
Ricardo Labiaga | 4d6bbb6 | 2009-04-01 09:23:07 -0400 | [diff] [blame] | 1568 | |
| 1569 | rqstp->rq_addrlen = sizeof(req->rq_xprt->addr); |
| 1570 | memcpy(&rqstp->rq_addr, &req->rq_xprt->addr, rqstp->rq_addrlen); |
| 1571 | memcpy(&rqstp->rq_arg, &req->rq_rcv_buf, sizeof(rqstp->rq_arg)); |
| 1572 | memcpy(&rqstp->rq_res, &req->rq_snd_buf, sizeof(rqstp->rq_res)); |
Trond Myklebust | 756b9b3 | 2015-12-07 12:52:23 -0800 | [diff] [blame] | 1573 | |
| 1574 | /* Adjust the argument buffer length */ |
Benjamin Coddington | 38b7631 | 2015-11-20 09:55:30 -0500 | [diff] [blame] | 1575 | rqstp->rq_arg.len = req->rq_private_buf.len; |
Trond Myklebust | 756b9b3 | 2015-12-07 12:52:23 -0800 | [diff] [blame] | 1576 | if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len) { |
| 1577 | rqstp->rq_arg.head[0].iov_len = rqstp->rq_arg.len; |
| 1578 | rqstp->rq_arg.page_len = 0; |
| 1579 | } else if (rqstp->rq_arg.len <= rqstp->rq_arg.head[0].iov_len + |
| 1580 | rqstp->rq_arg.page_len) |
| 1581 | rqstp->rq_arg.page_len = rqstp->rq_arg.len - |
| 1582 | rqstp->rq_arg.head[0].iov_len; |
| 1583 | else |
| 1584 | rqstp->rq_arg.len = rqstp->rq_arg.head[0].iov_len + |
| 1585 | rqstp->rq_arg.page_len; |
Ricardo Labiaga | 4d6bbb6 | 2009-04-01 09:23:07 -0400 | [diff] [blame] | 1586 | |
| 1587 | /* reset result send buffer "put" position */ |
| 1588 | resv->iov_len = 0; |
| 1589 | |
Ricardo Labiaga | 4d6bbb6 | 2009-04-01 09:23:07 -0400 | [diff] [blame] | 1590 | /* |
| 1591 | * Skip the next two words because they've already been |
Chuck Lever | 632dda8 | 2015-05-11 14:04:50 -0400 | [diff] [blame] | 1592 | * processed in the transport |
Ricardo Labiaga | 4d6bbb6 | 2009-04-01 09:23:07 -0400 | [diff] [blame] | 1593 | */ |
| 1594 | svc_getu32(argv); /* XID */ |
| 1595 | svc_getnl(argv); /* CALLDIR */ |
| 1596 | |
Chuck Lever | 632dda8 | 2015-05-11 14:04:50 -0400 | [diff] [blame] | 1597 | /* Parse and execute the bc call */ |
Trond Myklebust | 0d2a970 | 2015-06-04 15:37:10 -0400 | [diff] [blame] | 1598 | proc_error = svc_process_common(rqstp, argv, resv); |
| 1599 | |
Trond Myklebust | 7402a4f | 2019-07-16 13:51:29 -0400 | [diff] [blame] | 1600 | atomic_dec(&req->rq_xprt->bc_slot_count); |
Trond Myklebust | 0d2a970 | 2015-06-04 15:37:10 -0400 | [diff] [blame] | 1601 | if (!proc_error) { |
Chuck Lever | 632dda8 | 2015-05-11 14:04:50 -0400 | [diff] [blame] | 1602 | /* Processing error: drop the request */ |
Trond Myklebust | b3b02ae | 2012-05-31 15:26:38 -0400 | [diff] [blame] | 1603 | xprt_free_bc_request(req); |
Vasily Averin | 8f7766c | 2018-12-24 14:46:08 +0300 | [diff] [blame] | 1604 | error = -EINVAL; |
| 1605 | goto out; |
Andy Adamson | 4b5b3ba | 2011-01-06 02:04:24 +0000 | [diff] [blame] | 1606 | } |
Chuck Lever | 632dda8 | 2015-05-11 14:04:50 -0400 | [diff] [blame] | 1607 | /* Finally, send the reply synchronously */ |
| 1608 | memcpy(&req->rq_snd_buf, &rqstp->rq_res, sizeof(req->rq_snd_buf)); |
Trond Myklebust | 0f41979 | 2015-06-01 22:59:08 -0400 | [diff] [blame] | 1609 | task = rpc_run_bc_task(req); |
Chuck Lever | 632dda8 | 2015-05-11 14:04:50 -0400 | [diff] [blame] | 1610 | if (IS_ERR(task)) { |
| 1611 | error = PTR_ERR(task); |
| 1612 | goto out; |
| 1613 | } |
| 1614 | |
| 1615 | WARN_ON_ONCE(atomic_read(&task->tk_count) != 1); |
| 1616 | error = task->tk_status; |
| 1617 | rpc_put_task(task); |
| 1618 | |
| 1619 | out: |
| 1620 | dprintk("svc: %s(), error=%d\n", __func__, error); |
| 1621 | return error; |
Ricardo Labiaga | 4d6bbb6 | 2009-04-01 09:23:07 -0400 | [diff] [blame] | 1622 | } |
Trond Myklebust | 0d961aa | 2011-07-13 19:24:15 -0400 | [diff] [blame] | 1623 | EXPORT_SYMBOL_GPL(bc_svc_process); |
Trond Myklebust | 9e00abc | 2011-07-13 19:20:49 -0400 | [diff] [blame] | 1624 | #endif /* CONFIG_SUNRPC_BACKCHANNEL */ |
Ricardo Labiaga | 4d6bbb6 | 2009-04-01 09:23:07 -0400 | [diff] [blame] | 1625 | |
Ricardo Labiaga | 1cad7ea | 2009-04-01 09:23:06 -0400 | [diff] [blame] | 1626 | /* |
Greg Banks | 7adae48 | 2006-10-04 02:15:47 -0700 | [diff] [blame] | 1627 | * Return (transport-specific) limit on the rpc payload. |
| 1628 | */ |
| 1629 | u32 svc_max_payload(const struct svc_rqst *rqstp) |
| 1630 | { |
Tom Tucker | 4902315 | 2007-12-30 21:07:21 -0600 | [diff] [blame] | 1631 | u32 max = rqstp->rq_xprt->xpt_class->xcl_max_payload; |
Greg Banks | 7adae48 | 2006-10-04 02:15:47 -0700 | [diff] [blame] | 1632 | |
NeilBrown | c6b0a9f | 2006-10-06 00:44:05 -0700 | [diff] [blame] | 1633 | if (rqstp->rq_server->sv_max_payload < max) |
| 1634 | max = rqstp->rq_server->sv_max_payload; |
Greg Banks | 7adae48 | 2006-10-04 02:15:47 -0700 | [diff] [blame] | 1635 | return max; |
| 1636 | } |
| 1637 | EXPORT_SYMBOL_GPL(svc_max_payload); |
Chuck Lever | 8154ef2 | 2018-03-27 10:54:07 -0400 | [diff] [blame] | 1638 | |
| 1639 | /** |
Chuck Lever | 4120553 | 2020-03-02 14:45:53 -0500 | [diff] [blame^] | 1640 | * svc_encode_read_payload - mark a range of bytes as a READ payload |
| 1641 | * @rqstp: svc_rqst to operate on |
| 1642 | * @offset: payload's byte offset in rqstp->rq_res |
| 1643 | * @length: size of payload, in bytes |
| 1644 | * |
| 1645 | * Returns zero on success, or a negative errno if a permanent |
| 1646 | * error occurred. |
| 1647 | */ |
| 1648 | int svc_encode_read_payload(struct svc_rqst *rqstp, unsigned int offset, |
| 1649 | unsigned int length) |
| 1650 | { |
| 1651 | return rqstp->rq_xprt->xpt_ops->xpo_read_payload(rqstp, offset, length); |
| 1652 | } |
| 1653 | EXPORT_SYMBOL_GPL(svc_encode_read_payload); |
| 1654 | |
| 1655 | /** |
Chuck Lever | 8154ef2 | 2018-03-27 10:54:07 -0400 | [diff] [blame] | 1656 | * svc_fill_write_vector - Construct data argument for VFS write call |
| 1657 | * @rqstp: svc_rqst to operate on |
Chuck Lever | 3fd9557 | 2018-07-27 11:19:05 -0400 | [diff] [blame] | 1658 | * @pages: list of pages containing data payload |
Chuck Lever | 8154ef2 | 2018-03-27 10:54:07 -0400 | [diff] [blame] | 1659 | * @first: buffer containing first section of write payload |
| 1660 | * @total: total number of bytes of write payload |
| 1661 | * |
Chuck Lever | 3fd9557 | 2018-07-27 11:19:05 -0400 | [diff] [blame] | 1662 | * Fills in rqstp::rq_vec, and returns the number of elements. |
Chuck Lever | 8154ef2 | 2018-03-27 10:54:07 -0400 | [diff] [blame] | 1663 | */ |
Chuck Lever | 3fd9557 | 2018-07-27 11:19:05 -0400 | [diff] [blame] | 1664 | unsigned int svc_fill_write_vector(struct svc_rqst *rqstp, struct page **pages, |
| 1665 | struct kvec *first, size_t total) |
Chuck Lever | 8154ef2 | 2018-03-27 10:54:07 -0400 | [diff] [blame] | 1666 | { |
| 1667 | struct kvec *vec = rqstp->rq_vec; |
Chuck Lever | 8154ef2 | 2018-03-27 10:54:07 -0400 | [diff] [blame] | 1668 | unsigned int i; |
| 1669 | |
| 1670 | /* Some types of transport can present the write payload |
| 1671 | * entirely in rq_arg.pages. In this case, @first is empty. |
| 1672 | */ |
| 1673 | i = 0; |
| 1674 | if (first->iov_len) { |
| 1675 | vec[i].iov_base = first->iov_base; |
| 1676 | vec[i].iov_len = min_t(size_t, total, first->iov_len); |
| 1677 | total -= vec[i].iov_len; |
| 1678 | ++i; |
| 1679 | } |
| 1680 | |
Chuck Lever | 8154ef2 | 2018-03-27 10:54:07 -0400 | [diff] [blame] | 1681 | while (total) { |
| 1682 | vec[i].iov_base = page_address(*pages); |
| 1683 | vec[i].iov_len = min_t(size_t, total, PAGE_SIZE); |
| 1684 | total -= vec[i].iov_len; |
| 1685 | ++i; |
Chuck Lever | 8154ef2 | 2018-03-27 10:54:07 -0400 | [diff] [blame] | 1686 | ++pages; |
| 1687 | } |
| 1688 | |
| 1689 | WARN_ON_ONCE(i > ARRAY_SIZE(rqstp->rq_vec)); |
| 1690 | return i; |
| 1691 | } |
| 1692 | EXPORT_SYMBOL_GPL(svc_fill_write_vector); |
Chuck Lever | 38a7031 | 2018-03-27 10:54:21 -0400 | [diff] [blame] | 1693 | |
| 1694 | /** |
| 1695 | * svc_fill_symlink_pathname - Construct pathname argument for VFS symlink call |
| 1696 | * @rqstp: svc_rqst to operate on |
| 1697 | * @first: buffer containing first section of pathname |
Chuck Lever | 11b4d66 | 2018-07-27 11:19:10 -0400 | [diff] [blame] | 1698 | * @p: buffer containing remaining section of pathname |
Chuck Lever | 38a7031 | 2018-03-27 10:54:21 -0400 | [diff] [blame] | 1699 | * @total: total length of the pathname argument |
| 1700 | * |
Chuck Lever | 11b4d66 | 2018-07-27 11:19:10 -0400 | [diff] [blame] | 1701 | * The VFS symlink API demands a NUL-terminated pathname in mapped memory. |
| 1702 | * Returns pointer to a NUL-terminated string, or an ERR_PTR. Caller must free |
| 1703 | * the returned string. |
Chuck Lever | 38a7031 | 2018-03-27 10:54:21 -0400 | [diff] [blame] | 1704 | */ |
| 1705 | char *svc_fill_symlink_pathname(struct svc_rqst *rqstp, struct kvec *first, |
Chuck Lever | 11b4d66 | 2018-07-27 11:19:10 -0400 | [diff] [blame] | 1706 | void *p, size_t total) |
Chuck Lever | 38a7031 | 2018-03-27 10:54:21 -0400 | [diff] [blame] | 1707 | { |
Chuck Lever | 11b4d66 | 2018-07-27 11:19:10 -0400 | [diff] [blame] | 1708 | size_t len, remaining; |
| 1709 | char *result, *dst; |
Chuck Lever | 38a7031 | 2018-03-27 10:54:21 -0400 | [diff] [blame] | 1710 | |
Chuck Lever | 11b4d66 | 2018-07-27 11:19:10 -0400 | [diff] [blame] | 1711 | result = kmalloc(total + 1, GFP_KERNEL); |
| 1712 | if (!result) |
| 1713 | return ERR_PTR(-ESERVERFAULT); |
Chuck Lever | 38a7031 | 2018-03-27 10:54:21 -0400 | [diff] [blame] | 1714 | |
Chuck Lever | 11b4d66 | 2018-07-27 11:19:10 -0400 | [diff] [blame] | 1715 | dst = result; |
| 1716 | remaining = total; |
Chuck Lever | 38a7031 | 2018-03-27 10:54:21 -0400 | [diff] [blame] | 1717 | |
Chuck Lever | 11b4d66 | 2018-07-27 11:19:10 -0400 | [diff] [blame] | 1718 | len = min_t(size_t, total, first->iov_len); |
| 1719 | if (len) { |
Chuck Lever | 38a7031 | 2018-03-27 10:54:21 -0400 | [diff] [blame] | 1720 | memcpy(dst, first->iov_base, len); |
| 1721 | dst += len; |
| 1722 | remaining -= len; |
Chuck Lever | 38a7031 | 2018-03-27 10:54:21 -0400 | [diff] [blame] | 1723 | } |
| 1724 | |
Chuck Lever | 11b4d66 | 2018-07-27 11:19:10 -0400 | [diff] [blame] | 1725 | if (remaining) { |
| 1726 | len = min_t(size_t, remaining, PAGE_SIZE); |
| 1727 | memcpy(dst, p, len); |
| 1728 | dst += len; |
| 1729 | } |
| 1730 | |
| 1731 | *dst = '\0'; |
| 1732 | |
| 1733 | /* Sanity check: Linux doesn't allow the pathname argument to |
Chuck Lever | 38a7031 | 2018-03-27 10:54:21 -0400 | [diff] [blame] | 1734 | * contain a NUL byte. |
| 1735 | */ |
Chuck Lever | 11b4d66 | 2018-07-27 11:19:10 -0400 | [diff] [blame] | 1736 | if (strlen(result) != total) { |
| 1737 | kfree(result); |
Chuck Lever | 38a7031 | 2018-03-27 10:54:21 -0400 | [diff] [blame] | 1738 | return ERR_PTR(-EINVAL); |
Chuck Lever | 11b4d66 | 2018-07-27 11:19:10 -0400 | [diff] [blame] | 1739 | } |
Chuck Lever | 38a7031 | 2018-03-27 10:54:21 -0400 | [diff] [blame] | 1740 | return result; |
| 1741 | } |
| 1742 | EXPORT_SYMBOL_GPL(svc_fill_symlink_pathname); |