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