Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Helpers for initial module or kernel cmdline parsing |
| 2 | Copyright (C) 2001 Rusty Russell. |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License as published by |
| 6 | the Free Software Foundation; either version 2 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program; if not, write to the Free Software |
| 16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
Paul Gortmaker | 72a59aa | 2011-05-26 21:07:10 -0400 | [diff] [blame] | 18 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/kernel.h> |
| 20 | #include <linux/string.h> |
| 21 | #include <linux/errno.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/device.h> |
| 24 | #include <linux/err.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 25 | #include <linux/slab.h> |
Peter Oberparleiter | 26d052b | 2009-07-06 17:11:22 +0200 | [diff] [blame] | 26 | #include <linux/ctype.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Rusty Russell | 907b29e | 2010-08-11 23:04:19 -0600 | [diff] [blame] | 28 | /* Protects all parameters, and incidentally kmalloced_param list. */ |
| 29 | static DEFINE_MUTEX(param_lock); |
| 30 | |
Rusty Russell | a105432 | 2010-08-11 23:04:18 -0600 | [diff] [blame] | 31 | /* This just allows us to keep track of which parameters are kmalloced. */ |
| 32 | struct kmalloced_param { |
| 33 | struct list_head list; |
| 34 | char val[]; |
| 35 | }; |
Rusty Russell | a105432 | 2010-08-11 23:04:18 -0600 | [diff] [blame] | 36 | static LIST_HEAD(kmalloced_params); |
| 37 | |
| 38 | static void *kmalloc_parameter(unsigned int size) |
| 39 | { |
| 40 | struct kmalloced_param *p; |
| 41 | |
| 42 | p = kmalloc(sizeof(*p) + size, GFP_KERNEL); |
| 43 | if (!p) |
| 44 | return NULL; |
| 45 | |
Rusty Russell | a105432 | 2010-08-11 23:04:18 -0600 | [diff] [blame] | 46 | list_add(&p->list, &kmalloced_params); |
Rusty Russell | a105432 | 2010-08-11 23:04:18 -0600 | [diff] [blame] | 47 | return p->val; |
| 48 | } |
| 49 | |
| 50 | /* Does nothing if parameter wasn't kmalloced above. */ |
| 51 | static void maybe_kfree_parameter(void *param) |
| 52 | { |
| 53 | struct kmalloced_param *p; |
| 54 | |
Rusty Russell | a105432 | 2010-08-11 23:04:18 -0600 | [diff] [blame] | 55 | list_for_each_entry(p, &kmalloced_params, list) { |
| 56 | if (p->val == param) { |
| 57 | list_del(&p->list); |
| 58 | kfree(p); |
| 59 | break; |
| 60 | } |
| 61 | } |
Rusty Russell | a105432 | 2010-08-11 23:04:18 -0600 | [diff] [blame] | 62 | } |
| 63 | |
Michal Schmidt | b1e4d20 | 2011-10-10 00:03:37 +0200 | [diff] [blame] | 64 | static char dash2underscore(char c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | { |
| 66 | if (c == '-') |
| 67 | return '_'; |
| 68 | return c; |
| 69 | } |
| 70 | |
Michal Schmidt | b1e4d20 | 2011-10-10 00:03:37 +0200 | [diff] [blame] | 71 | bool parameqn(const char *a, const char *b, size_t n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
Michal Schmidt | b1e4d20 | 2011-10-10 00:03:37 +0200 | [diff] [blame] | 73 | size_t i; |
| 74 | |
| 75 | for (i = 0; i < n; i++) { |
| 76 | if (dash2underscore(a[i]) != dash2underscore(b[i])) |
| 77 | return false; |
| 78 | } |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | bool parameq(const char *a, const char *b) |
| 83 | { |
| 84 | return parameqn(a, b, strlen(a)+1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static int parse_one(char *param, |
| 88 | char *val, |
Rusty Russell | 914dcaa | 2010-08-11 23:04:18 -0600 | [diff] [blame] | 89 | const struct kernel_param *params, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | unsigned num_params, |
| 91 | int (*handle_unknown)(char *param, char *val)) |
| 92 | { |
| 93 | unsigned int i; |
Rusty Russell | 907b29e | 2010-08-11 23:04:19 -0600 | [diff] [blame] | 94 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | /* Find parameter */ |
| 97 | for (i = 0; i < num_params; i++) { |
| 98 | if (parameq(param, params[i].name)) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 99 | /* No one handled NULL, so do it here. */ |
Dave Young | 10f296c | 2012-02-01 10:33:11 +0800 | [diff] [blame] | 100 | if (!val && params[i].ops->set != param_set_bool |
| 101 | && params[i].ops->set != param_set_bint) |
Rusty Russell | 2e9fb99 | 2010-08-11 23:04:10 -0600 | [diff] [blame] | 102 | return -EINVAL; |
Jim Cromie | 8487bfd | 2011-12-06 12:11:31 -0700 | [diff] [blame] | 103 | pr_debug("They are equal! Calling %p\n", |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 104 | params[i].ops->set); |
Rusty Russell | 907b29e | 2010-08-11 23:04:19 -0600 | [diff] [blame] | 105 | mutex_lock(¶m_lock); |
| 106 | err = params[i].ops->set(val, ¶ms[i]); |
| 107 | mutex_unlock(¶m_lock); |
| 108 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | if (handle_unknown) { |
Jim Cromie | 8487bfd | 2011-12-06 12:11:31 -0700 | [diff] [blame] | 113 | pr_debug("Unknown argument: calling %p\n", handle_unknown); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | return handle_unknown(param, val); |
| 115 | } |
| 116 | |
Jim Cromie | 8487bfd | 2011-12-06 12:11:31 -0700 | [diff] [blame] | 117 | pr_debug("Unknown argument `%s'\n", param); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | return -ENOENT; |
| 119 | } |
| 120 | |
| 121 | /* You can use " around spaces, but can't escape ". */ |
| 122 | /* Hyphens and underscores equivalent in parameter names. */ |
| 123 | static char *next_arg(char *args, char **param, char **val) |
| 124 | { |
| 125 | unsigned int i, equals = 0; |
| 126 | int in_quote = 0, quoted = 0; |
| 127 | char *next; |
| 128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | if (*args == '"') { |
| 130 | args++; |
| 131 | in_quote = 1; |
| 132 | quoted = 1; |
| 133 | } |
| 134 | |
| 135 | for (i = 0; args[i]; i++) { |
Peter Oberparleiter | 26d052b | 2009-07-06 17:11:22 +0200 | [diff] [blame] | 136 | if (isspace(args[i]) && !in_quote) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | break; |
| 138 | if (equals == 0) { |
| 139 | if (args[i] == '=') |
| 140 | equals = i; |
| 141 | } |
| 142 | if (args[i] == '"') |
| 143 | in_quote = !in_quote; |
| 144 | } |
| 145 | |
| 146 | *param = args; |
| 147 | if (!equals) |
| 148 | *val = NULL; |
| 149 | else { |
| 150 | args[equals] = '\0'; |
| 151 | *val = args + equals + 1; |
| 152 | |
| 153 | /* Don't include quotes in value. */ |
| 154 | if (**val == '"') { |
| 155 | (*val)++; |
| 156 | if (args[i-1] == '"') |
| 157 | args[i-1] = '\0'; |
| 158 | } |
| 159 | if (quoted && args[i-1] == '"') |
| 160 | args[i-1] = '\0'; |
| 161 | } |
| 162 | |
| 163 | if (args[i]) { |
| 164 | args[i] = '\0'; |
| 165 | next = args + i + 1; |
| 166 | } else |
| 167 | next = args + i; |
Rusty Russell | f36462f | 2005-09-27 21:45:34 -0700 | [diff] [blame] | 168 | |
| 169 | /* Chew up trailing spaces. */ |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 170 | return skip_spaces(next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /* Args looks like "foo=bar,bar2 baz=fuz wiz". */ |
| 174 | int parse_args(const char *name, |
| 175 | char *args, |
Rusty Russell | 914dcaa | 2010-08-11 23:04:18 -0600 | [diff] [blame] | 176 | const struct kernel_param *params, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | unsigned num, |
| 178 | int (*unknown)(char *param, char *val)) |
| 179 | { |
| 180 | char *param, *val; |
| 181 | |
Jim Cromie | 8487bfd | 2011-12-06 12:11:31 -0700 | [diff] [blame] | 182 | pr_debug("Parsing ARGS: %s\n", args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Rusty Russell | f36462f | 2005-09-27 21:45:34 -0700 | [diff] [blame] | 184 | /* Chew leading spaces */ |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 185 | args = skip_spaces(args); |
Rusty Russell | f36462f | 2005-09-27 21:45:34 -0700 | [diff] [blame] | 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | while (*args) { |
| 188 | int ret; |
Ard van Breemen | a416aba | 2007-01-05 16:36:20 -0800 | [diff] [blame] | 189 | int irq_was_disabled; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
| 191 | args = next_arg(args, ¶m, &val); |
Ard van Breemen | a416aba | 2007-01-05 16:36:20 -0800 | [diff] [blame] | 192 | irq_was_disabled = irqs_disabled(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | ret = parse_one(param, val, params, num, unknown); |
Ard van Breemen | a416aba | 2007-01-05 16:36:20 -0800 | [diff] [blame] | 194 | if (irq_was_disabled && !irqs_disabled()) { |
| 195 | printk(KERN_WARNING "parse_args(): option '%s' enabled " |
| 196 | "irq's!\n", param); |
| 197 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | switch (ret) { |
| 199 | case -ENOENT: |
| 200 | printk(KERN_ERR "%s: Unknown parameter `%s'\n", |
| 201 | name, param); |
| 202 | return ret; |
| 203 | case -ENOSPC: |
| 204 | printk(KERN_ERR |
| 205 | "%s: `%s' too large for parameter `%s'\n", |
| 206 | name, val ?: "", param); |
| 207 | return ret; |
| 208 | case 0: |
| 209 | break; |
| 210 | default: |
| 211 | printk(KERN_ERR |
| 212 | "%s: `%s' invalid for parameter `%s'\n", |
| 213 | name, val ?: "", param); |
| 214 | return ret; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /* All parsed OK. */ |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | /* Lazy bastard, eh? */ |
| 223 | #define STANDARD_PARAM_DEF(name, type, format, tmptype, strtolfn) \ |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 224 | int param_set_##name(const char *val, const struct kernel_param *kp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | { \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | tmptype l; \ |
Yi Yang | 06b2a76 | 2008-02-08 04:21:57 -0800 | [diff] [blame] | 227 | int ret; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | \ |
Yi Yang | 06b2a76 | 2008-02-08 04:21:57 -0800 | [diff] [blame] | 229 | ret = strtolfn(val, 0, &l); \ |
Satoru Moriya | 81c7413 | 2011-05-26 19:38:04 -0400 | [diff] [blame] | 230 | if (ret < 0 || ((type)l != l)) \ |
| 231 | return ret < 0 ? ret : -EINVAL; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | *((type *)kp->arg) = l; \ |
| 233 | return 0; \ |
| 234 | } \ |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 235 | int param_get_##name(char *buffer, const struct kernel_param *kp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | { \ |
| 237 | return sprintf(buffer, format, *((type *)kp->arg)); \ |
Rusty Russell | a14fe24 | 2010-08-11 23:04:11 -0600 | [diff] [blame] | 238 | } \ |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 239 | struct kernel_param_ops param_ops_##name = { \ |
| 240 | .set = param_set_##name, \ |
| 241 | .get = param_get_##name, \ |
| 242 | }; \ |
Rusty Russell | a14fe24 | 2010-08-11 23:04:11 -0600 | [diff] [blame] | 243 | EXPORT_SYMBOL(param_set_##name); \ |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 244 | EXPORT_SYMBOL(param_get_##name); \ |
| 245 | EXPORT_SYMBOL(param_ops_##name) |
| 246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
Yi Yang | 06b2a76 | 2008-02-08 04:21:57 -0800 | [diff] [blame] | 248 | STANDARD_PARAM_DEF(byte, unsigned char, "%c", unsigned long, strict_strtoul); |
| 249 | STANDARD_PARAM_DEF(short, short, "%hi", long, strict_strtol); |
| 250 | STANDARD_PARAM_DEF(ushort, unsigned short, "%hu", unsigned long, strict_strtoul); |
| 251 | STANDARD_PARAM_DEF(int, int, "%i", long, strict_strtol); |
| 252 | STANDARD_PARAM_DEF(uint, unsigned int, "%u", unsigned long, strict_strtoul); |
| 253 | STANDARD_PARAM_DEF(long, long, "%li", long, strict_strtol); |
| 254 | STANDARD_PARAM_DEF(ulong, unsigned long, "%lu", unsigned long, strict_strtoul); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 256 | int param_set_charp(const char *val, const struct kernel_param *kp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | if (strlen(val) > 1024) { |
| 259 | printk(KERN_ERR "%s: string parameter too long\n", |
| 260 | kp->name); |
| 261 | return -ENOSPC; |
| 262 | } |
| 263 | |
Rusty Russell | a105432 | 2010-08-11 23:04:18 -0600 | [diff] [blame] | 264 | maybe_kfree_parameter(*(char **)kp->arg); |
| 265 | |
| 266 | /* This is a hack. We can't kmalloc in early boot, and we |
Rusty Russell | e180a6b | 2009-03-31 13:05:29 -0600 | [diff] [blame] | 267 | * don't need to; this mangled commandline is preserved. */ |
| 268 | if (slab_is_available()) { |
Rusty Russell | a105432 | 2010-08-11 23:04:18 -0600 | [diff] [blame] | 269 | *(char **)kp->arg = kmalloc_parameter(strlen(val)+1); |
Rusty Russell | d553ad8 | 2009-10-29 08:56:17 -0600 | [diff] [blame] | 270 | if (!*(char **)kp->arg) |
Rusty Russell | e180a6b | 2009-03-31 13:05:29 -0600 | [diff] [blame] | 271 | return -ENOMEM; |
Rusty Russell | a105432 | 2010-08-11 23:04:18 -0600 | [diff] [blame] | 272 | strcpy(*(char **)kp->arg, val); |
Rusty Russell | e180a6b | 2009-03-31 13:05:29 -0600 | [diff] [blame] | 273 | } else |
| 274 | *(const char **)kp->arg = val; |
| 275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | return 0; |
| 277 | } |
Rusty Russell | a14fe24 | 2010-08-11 23:04:11 -0600 | [diff] [blame] | 278 | EXPORT_SYMBOL(param_set_charp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 280 | int param_get_charp(char *buffer, const struct kernel_param *kp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | { |
| 282 | return sprintf(buffer, "%s", *((char **)kp->arg)); |
| 283 | } |
Rusty Russell | a14fe24 | 2010-08-11 23:04:11 -0600 | [diff] [blame] | 284 | EXPORT_SYMBOL(param_get_charp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
Rusty Russell | a105432 | 2010-08-11 23:04:18 -0600 | [diff] [blame] | 286 | static void param_free_charp(void *arg) |
| 287 | { |
| 288 | maybe_kfree_parameter(*((char **)arg)); |
| 289 | } |
| 290 | |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 291 | struct kernel_param_ops param_ops_charp = { |
| 292 | .set = param_set_charp, |
| 293 | .get = param_get_charp, |
Rusty Russell | a105432 | 2010-08-11 23:04:18 -0600 | [diff] [blame] | 294 | .free = param_free_charp, |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 295 | }; |
| 296 | EXPORT_SYMBOL(param_ops_charp); |
| 297 | |
Rusty Russell | fddd520 | 2009-06-12 21:46:57 -0600 | [diff] [blame] | 298 | /* Actually could be a bool or an int, for historical reasons. */ |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 299 | int param_set_bool(const char *val, const struct kernel_param *kp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | { |
Rusty Russell | fddd520 | 2009-06-12 21:46:57 -0600 | [diff] [blame] | 301 | bool v; |
Jonathan Cameron | f721a46 | 2011-04-19 12:43:47 +0100 | [diff] [blame] | 302 | int ret; |
Rusty Russell | fddd520 | 2009-06-12 21:46:57 -0600 | [diff] [blame] | 303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | /* No equals means "set"... */ |
| 305 | if (!val) val = "1"; |
| 306 | |
| 307 | /* One of =[yYnN01] */ |
Jonathan Cameron | f721a46 | 2011-04-19 12:43:47 +0100 | [diff] [blame] | 308 | ret = strtobool(val, &v); |
| 309 | if (ret) |
| 310 | return ret; |
Rusty Russell | fddd520 | 2009-06-12 21:46:57 -0600 | [diff] [blame] | 311 | |
| 312 | if (kp->flags & KPARAM_ISBOOL) |
| 313 | *(bool *)kp->arg = v; |
| 314 | else |
| 315 | *(int *)kp->arg = v; |
| 316 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | } |
Rusty Russell | a14fe24 | 2010-08-11 23:04:11 -0600 | [diff] [blame] | 318 | EXPORT_SYMBOL(param_set_bool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 320 | int param_get_bool(char *buffer, const struct kernel_param *kp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | { |
Rusty Russell | fddd520 | 2009-06-12 21:46:57 -0600 | [diff] [blame] | 322 | bool val; |
| 323 | if (kp->flags & KPARAM_ISBOOL) |
| 324 | val = *(bool *)kp->arg; |
| 325 | else |
| 326 | val = *(int *)kp->arg; |
| 327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | /* Y and N chosen as being relatively non-coder friendly */ |
Rusty Russell | fddd520 | 2009-06-12 21:46:57 -0600 | [diff] [blame] | 329 | return sprintf(buffer, "%c", val ? 'Y' : 'N'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |
Rusty Russell | a14fe24 | 2010-08-11 23:04:11 -0600 | [diff] [blame] | 331 | EXPORT_SYMBOL(param_get_bool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 333 | struct kernel_param_ops param_ops_bool = { |
| 334 | .set = param_set_bool, |
| 335 | .get = param_get_bool, |
| 336 | }; |
| 337 | EXPORT_SYMBOL(param_ops_bool); |
| 338 | |
Rusty Russell | fddd520 | 2009-06-12 21:46:57 -0600 | [diff] [blame] | 339 | /* This one must be bool. */ |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 340 | int param_set_invbool(const char *val, const struct kernel_param *kp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | { |
Rusty Russell | fddd520 | 2009-06-12 21:46:57 -0600 | [diff] [blame] | 342 | int ret; |
| 343 | bool boolval; |
Jan Beulich | 22e48ea | 2007-10-16 23:29:34 -0700 | [diff] [blame] | 344 | struct kernel_param dummy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
Jan Beulich | 22e48ea | 2007-10-16 23:29:34 -0700 | [diff] [blame] | 346 | dummy.arg = &boolval; |
Rusty Russell | fddd520 | 2009-06-12 21:46:57 -0600 | [diff] [blame] | 347 | dummy.flags = KPARAM_ISBOOL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | ret = param_set_bool(val, &dummy); |
| 349 | if (ret == 0) |
Rusty Russell | 9a71af2 | 2009-06-12 21:46:53 -0600 | [diff] [blame] | 350 | *(bool *)kp->arg = !boolval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | return ret; |
| 352 | } |
Rusty Russell | a14fe24 | 2010-08-11 23:04:11 -0600 | [diff] [blame] | 353 | EXPORT_SYMBOL(param_set_invbool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 355 | int param_get_invbool(char *buffer, const struct kernel_param *kp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | { |
Rusty Russell | 9a71af2 | 2009-06-12 21:46:53 -0600 | [diff] [blame] | 357 | return sprintf(buffer, "%c", (*(bool *)kp->arg) ? 'N' : 'Y'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | } |
Rusty Russell | a14fe24 | 2010-08-11 23:04:11 -0600 | [diff] [blame] | 359 | EXPORT_SYMBOL(param_get_invbool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 361 | struct kernel_param_ops param_ops_invbool = { |
| 362 | .set = param_set_invbool, |
| 363 | .get = param_get_invbool, |
| 364 | }; |
| 365 | EXPORT_SYMBOL(param_ops_invbool); |
| 366 | |
Rusty Russell | 69116f2 | 2012-01-13 09:32:17 +1030 | [diff] [blame] | 367 | int param_set_bint(const char *val, const struct kernel_param *kp) |
| 368 | { |
| 369 | struct kernel_param boolkp; |
| 370 | bool v; |
| 371 | int ret; |
| 372 | |
| 373 | /* Match bool exactly, by re-using it. */ |
| 374 | boolkp = *kp; |
| 375 | boolkp.arg = &v; |
| 376 | boolkp.flags |= KPARAM_ISBOOL; |
| 377 | |
| 378 | ret = param_set_bool(val, &boolkp); |
| 379 | if (ret == 0) |
| 380 | *(int *)kp->arg = v; |
| 381 | return ret; |
| 382 | } |
| 383 | EXPORT_SYMBOL(param_set_bint); |
| 384 | |
| 385 | struct kernel_param_ops param_ops_bint = { |
| 386 | .set = param_set_bint, |
| 387 | .get = param_get_int, |
| 388 | }; |
| 389 | EXPORT_SYMBOL(param_ops_bint); |
| 390 | |
Bert Wesarg | 9730b5b | 2007-05-08 00:28:50 -0700 | [diff] [blame] | 391 | /* We break the rule and mangle the string. */ |
Adrian Bunk | 9871728 | 2006-03-25 03:07:06 -0800 | [diff] [blame] | 392 | static int param_array(const char *name, |
| 393 | const char *val, |
| 394 | unsigned int min, unsigned int max, |
| 395 | void *elem, int elemsize, |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 396 | int (*set)(const char *, const struct kernel_param *kp), |
Rusty Russell | 3c7d76e | 2009-10-29 08:56:19 -0600 | [diff] [blame] | 397 | u16 flags, |
Richard Knutsson | eb38a99 | 2008-02-06 01:37:50 -0800 | [diff] [blame] | 398 | unsigned int *num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | { |
| 400 | int ret; |
| 401 | struct kernel_param kp; |
| 402 | char save; |
| 403 | |
| 404 | /* Get the name right for errors. */ |
| 405 | kp.name = name; |
| 406 | kp.arg = elem; |
Rusty Russell | 3c7d76e | 2009-10-29 08:56:19 -0600 | [diff] [blame] | 407 | kp.flags = flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | *num = 0; |
| 410 | /* We expect a comma-separated list of values. */ |
| 411 | do { |
| 412 | int len; |
| 413 | |
| 414 | if (*num == max) { |
| 415 | printk(KERN_ERR "%s: can only take %i arguments\n", |
| 416 | name, max); |
| 417 | return -EINVAL; |
| 418 | } |
| 419 | len = strcspn(val, ","); |
| 420 | |
| 421 | /* nul-terminate and parse */ |
| 422 | save = val[len]; |
| 423 | ((char *)val)[len] = '\0'; |
Rusty Russell | 907b29e | 2010-08-11 23:04:19 -0600 | [diff] [blame] | 424 | BUG_ON(!mutex_is_locked(¶m_lock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | ret = set(val, &kp); |
| 426 | |
| 427 | if (ret != 0) |
| 428 | return ret; |
| 429 | kp.arg += elemsize; |
| 430 | val += len+1; |
| 431 | (*num)++; |
| 432 | } while (save == ','); |
| 433 | |
| 434 | if (*num < min) { |
| 435 | printk(KERN_ERR "%s: needs at least %i arguments\n", |
| 436 | name, min); |
| 437 | return -EINVAL; |
| 438 | } |
| 439 | return 0; |
| 440 | } |
| 441 | |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 442 | static int param_array_set(const char *val, const struct kernel_param *kp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | { |
Jan Beulich | 22e48ea | 2007-10-16 23:29:34 -0700 | [diff] [blame] | 444 | const struct kparam_array *arr = kp->arr; |
Bert Wesarg | 31143a1 | 2005-04-16 15:25:42 -0700 | [diff] [blame] | 445 | unsigned int temp_num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | |
| 447 | return param_array(kp->name, val, 1, arr->max, arr->elem, |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 448 | arr->elemsize, arr->ops->set, kp->flags, |
Rusty Russell | 3c7d76e | 2009-10-29 08:56:19 -0600 | [diff] [blame] | 449 | arr->num ?: &temp_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 452 | static int param_array_get(char *buffer, const struct kernel_param *kp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | { |
| 454 | int i, off, ret; |
Jan Beulich | 22e48ea | 2007-10-16 23:29:34 -0700 | [diff] [blame] | 455 | const struct kparam_array *arr = kp->arr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | struct kernel_param p; |
| 457 | |
| 458 | p = *kp; |
| 459 | for (i = off = 0; i < (arr->num ? *arr->num : arr->max); i++) { |
| 460 | if (i) |
| 461 | buffer[off++] = ','; |
| 462 | p.arg = arr->elem + arr->elemsize * i; |
Rusty Russell | 907b29e | 2010-08-11 23:04:19 -0600 | [diff] [blame] | 463 | BUG_ON(!mutex_is_locked(¶m_lock)); |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 464 | ret = arr->ops->get(buffer + off, &p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | if (ret < 0) |
| 466 | return ret; |
| 467 | off += ret; |
| 468 | } |
| 469 | buffer[off] = '\0'; |
| 470 | return off; |
| 471 | } |
| 472 | |
Rusty Russell | e6df34a | 2010-08-11 23:04:17 -0600 | [diff] [blame] | 473 | static void param_array_free(void *arg) |
| 474 | { |
| 475 | unsigned int i; |
| 476 | const struct kparam_array *arr = arg; |
| 477 | |
| 478 | if (arr->ops->free) |
| 479 | for (i = 0; i < (arr->num ? *arr->num : arr->max); i++) |
| 480 | arr->ops->free(arr->elem + arr->elemsize * i); |
| 481 | } |
| 482 | |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 483 | struct kernel_param_ops param_array_ops = { |
| 484 | .set = param_array_set, |
| 485 | .get = param_array_get, |
Rusty Russell | e6df34a | 2010-08-11 23:04:17 -0600 | [diff] [blame] | 486 | .free = param_array_free, |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 487 | }; |
| 488 | EXPORT_SYMBOL(param_array_ops); |
| 489 | |
| 490 | int param_set_copystring(const char *val, const struct kernel_param *kp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | { |
Jan Beulich | 22e48ea | 2007-10-16 23:29:34 -0700 | [diff] [blame] | 492 | const struct kparam_string *kps = kp->str; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
| 494 | if (strlen(val)+1 > kps->maxlen) { |
| 495 | printk(KERN_ERR "%s: string doesn't fit in %u chars.\n", |
| 496 | kp->name, kps->maxlen-1); |
| 497 | return -ENOSPC; |
| 498 | } |
| 499 | strcpy(kps->string, val); |
| 500 | return 0; |
| 501 | } |
Rusty Russell | a14fe24 | 2010-08-11 23:04:11 -0600 | [diff] [blame] | 502 | EXPORT_SYMBOL(param_set_copystring); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 504 | int param_get_string(char *buffer, const struct kernel_param *kp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | { |
Jan Beulich | 22e48ea | 2007-10-16 23:29:34 -0700 | [diff] [blame] | 506 | const struct kparam_string *kps = kp->str; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | return strlcpy(buffer, kps->string, kps->maxlen); |
| 508 | } |
Rusty Russell | a14fe24 | 2010-08-11 23:04:11 -0600 | [diff] [blame] | 509 | EXPORT_SYMBOL(param_get_string); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 511 | struct kernel_param_ops param_ops_string = { |
| 512 | .set = param_set_copystring, |
| 513 | .get = param_get_string, |
| 514 | }; |
| 515 | EXPORT_SYMBOL(param_ops_string); |
| 516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | /* sysfs output in /sys/modules/XYZ/parameters/ */ |
Edward Z. Yang | 350f825 | 2010-02-01 18:26:59 -0500 | [diff] [blame] | 518 | #define to_module_attr(n) container_of(n, struct module_attribute, attr) |
| 519 | #define to_module_kobject(n) container_of(n, struct module_kobject, kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
| 521 | extern struct kernel_param __start___param[], __stop___param[]; |
| 522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | struct param_attribute |
| 524 | { |
| 525 | struct module_attribute mattr; |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 526 | const struct kernel_param *param; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | }; |
| 528 | |
| 529 | struct module_param_attrs |
| 530 | { |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 531 | unsigned int num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | struct attribute_group grp; |
| 533 | struct param_attribute attrs[0]; |
| 534 | }; |
| 535 | |
Randy Dunlap | ef665c1 | 2007-02-13 15:19:06 -0800 | [diff] [blame] | 536 | #ifdef CONFIG_SYSFS |
Edward Z. Yang | 350f825 | 2010-02-01 18:26:59 -0500 | [diff] [blame] | 537 | #define to_param_attr(n) container_of(n, struct param_attribute, mattr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
| 539 | static ssize_t param_attr_show(struct module_attribute *mattr, |
Kay Sievers | 4befb02 | 2011-07-24 22:06:04 +0930 | [diff] [blame] | 540 | struct module_kobject *mk, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | { |
| 542 | int count; |
| 543 | struct param_attribute *attribute = to_param_attr(mattr); |
| 544 | |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 545 | if (!attribute->param->ops->get) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | return -EPERM; |
| 547 | |
Rusty Russell | 907b29e | 2010-08-11 23:04:19 -0600 | [diff] [blame] | 548 | mutex_lock(¶m_lock); |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 549 | count = attribute->param->ops->get(buf, attribute->param); |
Rusty Russell | 907b29e | 2010-08-11 23:04:19 -0600 | [diff] [blame] | 550 | mutex_unlock(¶m_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | if (count > 0) { |
| 552 | strcat(buf, "\n"); |
| 553 | ++count; |
| 554 | } |
| 555 | return count; |
| 556 | } |
| 557 | |
| 558 | /* sysfs always hands a nul-terminated string in buf. We rely on that. */ |
| 559 | static ssize_t param_attr_store(struct module_attribute *mattr, |
Kay Sievers | 4befb02 | 2011-07-24 22:06:04 +0930 | [diff] [blame] | 560 | struct module_kobject *km, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | const char *buf, size_t len) |
| 562 | { |
| 563 | int err; |
| 564 | struct param_attribute *attribute = to_param_attr(mattr); |
| 565 | |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 566 | if (!attribute->param->ops->set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | return -EPERM; |
| 568 | |
Rusty Russell | 907b29e | 2010-08-11 23:04:19 -0600 | [diff] [blame] | 569 | mutex_lock(¶m_lock); |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 570 | err = attribute->param->ops->set(buf, attribute->param); |
Rusty Russell | 907b29e | 2010-08-11 23:04:19 -0600 | [diff] [blame] | 571 | mutex_unlock(¶m_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | if (!err) |
| 573 | return len; |
| 574 | return err; |
| 575 | } |
Randy Dunlap | ef665c1 | 2007-02-13 15:19:06 -0800 | [diff] [blame] | 576 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | |
| 578 | #ifdef CONFIG_MODULES |
| 579 | #define __modinit |
| 580 | #else |
| 581 | #define __modinit __init |
| 582 | #endif |
| 583 | |
Randy Dunlap | ef665c1 | 2007-02-13 15:19:06 -0800 | [diff] [blame] | 584 | #ifdef CONFIG_SYSFS |
Rusty Russell | 907b29e | 2010-08-11 23:04:19 -0600 | [diff] [blame] | 585 | void __kernel_param_lock(void) |
| 586 | { |
| 587 | mutex_lock(¶m_lock); |
| 588 | } |
| 589 | EXPORT_SYMBOL(__kernel_param_lock); |
| 590 | |
| 591 | void __kernel_param_unlock(void) |
| 592 | { |
| 593 | mutex_unlock(¶m_lock); |
| 594 | } |
| 595 | EXPORT_SYMBOL(__kernel_param_unlock); |
| 596 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | /* |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 598 | * add_sysfs_param - add a parameter to sysfs |
| 599 | * @mk: struct module_kobject |
| 600 | * @kparam: the actual parameter definition to add to sysfs |
| 601 | * @name: name of parameter |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | * |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 603 | * Create a kobject if for a (per-module) parameter if mp NULL, and |
| 604 | * create file in sysfs. Returns an error on out of memory. Always cleans up |
| 605 | * if there's an error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | */ |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 607 | static __modinit int add_sysfs_param(struct module_kobject *mk, |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 608 | const struct kernel_param *kp, |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 609 | const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | { |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 611 | struct module_param_attrs *new; |
| 612 | struct attribute **attrs; |
| 613 | int err, num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 615 | /* We don't bother calling this with invisible parameters. */ |
| 616 | BUG_ON(!kp->perm); |
| 617 | |
| 618 | if (!mk->mp) { |
| 619 | num = 0; |
| 620 | attrs = NULL; |
| 621 | } else { |
| 622 | num = mk->mp->num; |
| 623 | attrs = mk->mp->grp.attrs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | } |
| 625 | |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 626 | /* Enlarge. */ |
| 627 | new = krealloc(mk->mp, |
| 628 | sizeof(*mk->mp) + sizeof(mk->mp->attrs[0]) * (num+1), |
| 629 | GFP_KERNEL); |
| 630 | if (!new) { |
| 631 | kfree(mk->mp); |
| 632 | err = -ENOMEM; |
| 633 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | } |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 635 | attrs = krealloc(attrs, sizeof(new->grp.attrs[0])*(num+2), GFP_KERNEL); |
| 636 | if (!attrs) { |
| 637 | err = -ENOMEM; |
| 638 | goto fail_free_new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | } |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 640 | |
| 641 | /* Sysfs wants everything zeroed. */ |
| 642 | memset(new, 0, sizeof(*new)); |
| 643 | memset(&new->attrs[num], 0, sizeof(new->attrs[num])); |
| 644 | memset(&attrs[num], 0, sizeof(attrs[num])); |
| 645 | new->grp.name = "parameters"; |
| 646 | new->grp.attrs = attrs; |
| 647 | |
| 648 | /* Tack new one on the end. */ |
Eric W. Biederman | a07e415 | 2010-02-11 15:23:05 -0800 | [diff] [blame] | 649 | sysfs_attr_init(&new->attrs[num].mattr.attr); |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 650 | new->attrs[num].param = kp; |
| 651 | new->attrs[num].mattr.show = param_attr_show; |
| 652 | new->attrs[num].mattr.store = param_attr_store; |
| 653 | new->attrs[num].mattr.attr.name = (char *)name; |
| 654 | new->attrs[num].mattr.attr.mode = kp->perm; |
| 655 | new->num = num+1; |
| 656 | |
| 657 | /* Fix up all the pointers, since krealloc can move us */ |
| 658 | for (num = 0; num < new->num; num++) |
| 659 | new->grp.attrs[num] = &new->attrs[num].mattr.attr; |
| 660 | new->grp.attrs[num] = NULL; |
| 661 | |
| 662 | mk->mp = new; |
| 663 | return 0; |
| 664 | |
| 665 | fail_free_new: |
| 666 | kfree(new); |
| 667 | fail: |
| 668 | mk->mp = NULL; |
| 669 | return err; |
| 670 | } |
| 671 | |
Linus Torvalds | d244118 | 2008-10-23 12:07:17 -0700 | [diff] [blame] | 672 | #ifdef CONFIG_MODULES |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 673 | static void free_module_param_attrs(struct module_kobject *mk) |
| 674 | { |
| 675 | kfree(mk->mp->grp.attrs); |
| 676 | kfree(mk->mp); |
| 677 | mk->mp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | } |
| 679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | /* |
| 681 | * module_param_sysfs_setup - setup sysfs support for one module |
| 682 | * @mod: module |
| 683 | * @kparam: module parameters (array) |
| 684 | * @num_params: number of module parameters |
| 685 | * |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 686 | * Adds sysfs entries for module parameters under |
| 687 | * /sys/module/[mod->name]/parameters/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | */ |
| 689 | int module_param_sysfs_setup(struct module *mod, |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 690 | const struct kernel_param *kparam, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | unsigned int num_params) |
| 692 | { |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 693 | int i, err; |
| 694 | bool params = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 696 | for (i = 0; i < num_params; i++) { |
| 697 | if (kparam[i].perm == 0) |
| 698 | continue; |
| 699 | err = add_sysfs_param(&mod->mkobj, &kparam[i], kparam[i].name); |
| 700 | if (err) |
| 701 | return err; |
| 702 | params = true; |
| 703 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 705 | if (!params) |
| 706 | return 0; |
| 707 | |
| 708 | /* Create the param group. */ |
| 709 | err = sysfs_create_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp); |
| 710 | if (err) |
| 711 | free_module_param_attrs(&mod->mkobj); |
| 712 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | /* |
| 716 | * module_param_sysfs_remove - remove sysfs support for one module |
| 717 | * @mod: module |
| 718 | * |
| 719 | * Remove sysfs entries for module parameters and the corresponding |
| 720 | * kobject. |
| 721 | */ |
| 722 | void module_param_sysfs_remove(struct module *mod) |
| 723 | { |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 724 | if (mod->mkobj.mp) { |
| 725 | sysfs_remove_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | /* We are positive that no one is using any param |
| 727 | * attrs at this point. Deallocate immediately. */ |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 728 | free_module_param_attrs(&mod->mkobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | #endif |
| 732 | |
Rusty Russell | e180a6b | 2009-03-31 13:05:29 -0600 | [diff] [blame] | 733 | void destroy_params(const struct kernel_param *params, unsigned num) |
| 734 | { |
Rusty Russell | e6df34a | 2010-08-11 23:04:17 -0600 | [diff] [blame] | 735 | unsigned int i; |
| 736 | |
| 737 | for (i = 0; i < num; i++) |
| 738 | if (params[i].ops->free) |
| 739 | params[i].ops->free(params[i].arg); |
Rusty Russell | e180a6b | 2009-03-31 13:05:29 -0600 | [diff] [blame] | 740 | } |
| 741 | |
Dmitry Torokhov | e94965e | 2010-12-15 14:00:19 -0800 | [diff] [blame] | 742 | static struct module_kobject * __init locate_module_kobject(const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | { |
| 744 | struct module_kobject *mk; |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 745 | struct kobject *kobj; |
| 746 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 748 | kobj = kset_find_obj(module_kset, name); |
| 749 | if (kobj) { |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 750 | mk = to_module_kobject(kobj); |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 751 | } else { |
| 752 | mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL); |
| 753 | BUG_ON(!mk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 755 | mk->mod = THIS_MODULE; |
| 756 | mk->kobj.kset = module_kset; |
| 757 | err = kobject_init_and_add(&mk->kobj, &module_ktype, NULL, |
| 758 | "%s", name); |
Kay Sievers | 88bfa32 | 2011-07-24 22:06:04 +0930 | [diff] [blame] | 759 | #ifdef CONFIG_MODULES |
| 760 | if (!err) |
| 761 | err = sysfs_create_file(&mk->kobj, &module_uevent.attr); |
| 762 | #endif |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 763 | if (err) { |
| 764 | kobject_put(&mk->kobj); |
Dmitry Torokhov | e94965e | 2010-12-15 14:00:19 -0800 | [diff] [blame] | 765 | printk(KERN_ERR |
| 766 | "Module '%s' failed add to sysfs, error number %d\n", |
| 767 | name, err); |
| 768 | printk(KERN_ERR |
| 769 | "The system will be unstable now.\n"); |
| 770 | return NULL; |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 771 | } |
Dmitry Torokhov | e94965e | 2010-12-15 14:00:19 -0800 | [diff] [blame] | 772 | |
| 773 | /* So that we hold reference in both cases. */ |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 774 | kobject_get(&mk->kobj); |
Greg Kroah-Hartman | 74c5b59 | 2007-07-30 11:26:38 -0700 | [diff] [blame] | 775 | } |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 776 | |
Dmitry Torokhov | e94965e | 2010-12-15 14:00:19 -0800 | [diff] [blame] | 777 | return mk; |
| 778 | } |
| 779 | |
| 780 | static void __init kernel_add_sysfs_param(const char *name, |
| 781 | struct kernel_param *kparam, |
| 782 | unsigned int name_skip) |
| 783 | { |
| 784 | struct module_kobject *mk; |
| 785 | int err; |
| 786 | |
| 787 | mk = locate_module_kobject(name); |
| 788 | if (!mk) |
| 789 | return; |
| 790 | |
| 791 | /* We need to remove old parameters before adding more. */ |
| 792 | if (mk->mp) |
| 793 | sysfs_remove_group(&mk->kobj, &mk->mp->grp); |
| 794 | |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 795 | /* These should not fail at boot. */ |
| 796 | err = add_sysfs_param(mk, kparam, kparam->name + name_skip); |
| 797 | BUG_ON(err); |
| 798 | err = sysfs_create_group(&mk->kobj, &mk->mp->grp); |
| 799 | BUG_ON(err); |
Kay Sievers | f30c53a | 2007-01-15 20:22:02 +0100 | [diff] [blame] | 800 | kobject_uevent(&mk->kobj, KOBJ_ADD); |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 801 | kobject_put(&mk->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | /* |
| 805 | * param_sysfs_builtin - add contents in /sys/parameters for built-in modules |
| 806 | * |
| 807 | * Add module_parameters to sysfs for "modules" built into the kernel. |
| 808 | * |
| 809 | * The "module" name (KBUILD_MODNAME) is stored before a dot, the |
| 810 | * "parameter" name is stored behind a dot in kernel_param->name. So, |
| 811 | * extract the "module" name for all built-in kernel_param-eters, |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 812 | * and for all who have the same, call kernel_add_sysfs_param. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | */ |
| 814 | static void __init param_sysfs_builtin(void) |
| 815 | { |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 816 | struct kernel_param *kp; |
| 817 | unsigned int name_len; |
| 818 | char modname[MODULE_NAME_LEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 820 | for (kp = __start___param; kp < __stop___param; kp++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | char *dot; |
| 822 | |
Rusty Russell | 9b473de | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 823 | if (kp->perm == 0) |
| 824 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | |
Rusty Russell | 730b69d | 2008-10-22 10:00:22 -0500 | [diff] [blame] | 826 | dot = strchr(kp->name, '.'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | if (!dot) { |
Rusty Russell | 67e67ce | 2008-10-22 10:00:23 -0500 | [diff] [blame] | 828 | /* This happens for core_param() */ |
| 829 | strcpy(modname, "kernel"); |
| 830 | name_len = 0; |
| 831 | } else { |
| 832 | name_len = dot - kp->name + 1; |
| 833 | strlcpy(modname, kp->name, name_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | } |
Rusty Russell | 67e67ce | 2008-10-22 10:00:23 -0500 | [diff] [blame] | 835 | kernel_add_sysfs_param(modname, kp, name_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | } |
| 838 | |
Dmitry Torokhov | e94965e | 2010-12-15 14:00:19 -0800 | [diff] [blame] | 839 | ssize_t __modver_version_show(struct module_attribute *mattr, |
Kay Sievers | 4befb02 | 2011-07-24 22:06:04 +0930 | [diff] [blame] | 840 | struct module_kobject *mk, char *buf) |
Dmitry Torokhov | e94965e | 2010-12-15 14:00:19 -0800 | [diff] [blame] | 841 | { |
| 842 | struct module_version_attribute *vattr = |
| 843 | container_of(mattr, struct module_version_attribute, mattr); |
| 844 | |
| 845 | return sprintf(buf, "%s\n", vattr->version); |
| 846 | } |
| 847 | |
Dmitry Torokhov | b4bc8428 | 2011-02-07 16:02:25 -0800 | [diff] [blame] | 848 | extern const struct module_version_attribute *__start___modver[]; |
| 849 | extern const struct module_version_attribute *__stop___modver[]; |
Dmitry Torokhov | e94965e | 2010-12-15 14:00:19 -0800 | [diff] [blame] | 850 | |
| 851 | static void __init version_sysfs_builtin(void) |
| 852 | { |
Dmitry Torokhov | b4bc8428 | 2011-02-07 16:02:25 -0800 | [diff] [blame] | 853 | const struct module_version_attribute **p; |
Dmitry Torokhov | e94965e | 2010-12-15 14:00:19 -0800 | [diff] [blame] | 854 | struct module_kobject *mk; |
| 855 | int err; |
| 856 | |
Dmitry Torokhov | b4bc8428 | 2011-02-07 16:02:25 -0800 | [diff] [blame] | 857 | for (p = __start___modver; p < __stop___modver; p++) { |
| 858 | const struct module_version_attribute *vattr = *p; |
| 859 | |
Dmitry Torokhov | e94965e | 2010-12-15 14:00:19 -0800 | [diff] [blame] | 860 | mk = locate_module_kobject(vattr->module_name); |
| 861 | if (mk) { |
| 862 | err = sysfs_create_file(&mk->kobj, &vattr->mattr.attr); |
| 863 | kobject_uevent(&mk->kobj, KOBJ_ADD); |
| 864 | kobject_put(&mk->kobj); |
| 865 | } |
| 866 | } |
| 867 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | |
| 869 | /* module-related sysfs stuff */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | static ssize_t module_attr_show(struct kobject *kobj, |
| 872 | struct attribute *attr, |
| 873 | char *buf) |
| 874 | { |
| 875 | struct module_attribute *attribute; |
| 876 | struct module_kobject *mk; |
| 877 | int ret; |
| 878 | |
| 879 | attribute = to_module_attr(attr); |
| 880 | mk = to_module_kobject(kobj); |
| 881 | |
| 882 | if (!attribute->show) |
Dmitry Torokhov | 70f2817 | 2005-04-29 01:27:34 -0500 | [diff] [blame] | 883 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
Kay Sievers | 4befb02 | 2011-07-24 22:06:04 +0930 | [diff] [blame] | 885 | ret = attribute->show(attribute, mk, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | return ret; |
| 888 | } |
| 889 | |
| 890 | static ssize_t module_attr_store(struct kobject *kobj, |
| 891 | struct attribute *attr, |
| 892 | const char *buf, size_t len) |
| 893 | { |
| 894 | struct module_attribute *attribute; |
| 895 | struct module_kobject *mk; |
| 896 | int ret; |
| 897 | |
| 898 | attribute = to_module_attr(attr); |
| 899 | mk = to_module_kobject(kobj); |
| 900 | |
| 901 | if (!attribute->store) |
Dmitry Torokhov | 70f2817 | 2005-04-29 01:27:34 -0500 | [diff] [blame] | 902 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | |
Kay Sievers | 4befb02 | 2011-07-24 22:06:04 +0930 | [diff] [blame] | 904 | ret = attribute->store(attribute, mk, buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | return ret; |
| 907 | } |
| 908 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 909 | static const struct sysfs_ops module_sysfs_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | .show = module_attr_show, |
| 911 | .store = module_attr_store, |
| 912 | }; |
| 913 | |
Kay Sievers | 270a6c4 | 2007-01-18 13:26:15 +0100 | [diff] [blame] | 914 | static int uevent_filter(struct kset *kset, struct kobject *kobj) |
| 915 | { |
| 916 | struct kobj_type *ktype = get_ktype(kobj); |
| 917 | |
| 918 | if (ktype == &module_ktype) |
| 919 | return 1; |
| 920 | return 0; |
| 921 | } |
| 922 | |
Emese Revfy | 9cd4361 | 2009-12-31 14:52:51 +0100 | [diff] [blame] | 923 | static const struct kset_uevent_ops module_uevent_ops = { |
Kay Sievers | 270a6c4 | 2007-01-18 13:26:15 +0100 | [diff] [blame] | 924 | .filter = uevent_filter, |
| 925 | }; |
| 926 | |
Greg Kroah-Hartman | 7405c1e | 2007-11-01 10:39:50 -0700 | [diff] [blame] | 927 | struct kset *module_kset; |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 928 | int module_sysfs_initialized; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | |
Greg Kroah-Hartman | 7405c1e | 2007-11-01 10:39:50 -0700 | [diff] [blame] | 930 | struct kobj_type module_ktype = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | .sysfs_ops = &module_sysfs_ops, |
| 932 | }; |
| 933 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | /* |
| 935 | * param_sysfs_init - wrapper for built-in params support |
| 936 | */ |
| 937 | static int __init param_sysfs_init(void) |
| 938 | { |
Greg Kroah-Hartman | 7405c1e | 2007-11-01 10:39:50 -0700 | [diff] [blame] | 939 | module_kset = kset_create_and_add("module", &module_uevent_ops, NULL); |
| 940 | if (!module_kset) { |
| 941 | printk(KERN_WARNING "%s (%d): error creating kset\n", |
| 942 | __FILE__, __LINE__); |
| 943 | return -ENOMEM; |
Randy Dunlap | d8c7649 | 2006-09-29 01:58:55 -0700 | [diff] [blame] | 944 | } |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 945 | module_sysfs_initialized = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | |
Dmitry Torokhov | e94965e | 2010-12-15 14:00:19 -0800 | [diff] [blame] | 947 | version_sysfs_builtin(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | param_sysfs_builtin(); |
| 949 | |
| 950 | return 0; |
| 951 | } |
Mark Huang | d10be6d | 2006-09-29 01:59:34 -0700 | [diff] [blame] | 952 | subsys_initcall(param_sysfs_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | |
Greg Kroah-Hartman | 7405c1e | 2007-11-01 10:39:50 -0700 | [diff] [blame] | 954 | #endif /* CONFIG_SYSFS */ |