blob: 1e52ca233fd9a45484418f14820c40b95e89d6ba [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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 Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
19#include <linux/string.h>
20#include <linux/errno.h>
21#include <linux/module.h>
22#include <linux/device.h>
23#include <linux/err.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080024#include <linux/slab.h>
Peter Oberparleiter26d052b2009-07-06 17:11:22 +020025#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Rusty Russell907b29e2010-08-11 23:04:19 -060027/* Protects all parameters, and incidentally kmalloced_param list. */
28static DEFINE_MUTEX(param_lock);
29
Rusty Russella1054322010-08-11 23:04:18 -060030/* This just allows us to keep track of which parameters are kmalloced. */
31struct kmalloced_param {
32 struct list_head list;
33 char val[];
34};
Rusty Russella1054322010-08-11 23:04:18 -060035static LIST_HEAD(kmalloced_params);
36
37static void *kmalloc_parameter(unsigned int size)
38{
39 struct kmalloced_param *p;
40
41 p = kmalloc(sizeof(*p) + size, GFP_KERNEL);
42 if (!p)
43 return NULL;
44
Rusty Russella1054322010-08-11 23:04:18 -060045 list_add(&p->list, &kmalloced_params);
Rusty Russella1054322010-08-11 23:04:18 -060046 return p->val;
47}
48
49/* Does nothing if parameter wasn't kmalloced above. */
50static void maybe_kfree_parameter(void *param)
51{
52 struct kmalloced_param *p;
53
Rusty Russella1054322010-08-11 23:04:18 -060054 list_for_each_entry(p, &kmalloced_params, list) {
55 if (p->val == param) {
56 list_del(&p->list);
57 kfree(p);
58 break;
59 }
60 }
Rusty Russella1054322010-08-11 23:04:18 -060061}
62
Michal Schmidtb1e4d202011-10-10 00:03:37 +020063static char dash2underscore(char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 if (c == '-')
66 return '_';
67 return c;
68}
69
Michal Schmidtb1e4d202011-10-10 00:03:37 +020070bool parameqn(const char *a, const char *b, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Michal Schmidtb1e4d202011-10-10 00:03:37 +020072 size_t i;
73
74 for (i = 0; i < n; i++) {
75 if (dash2underscore(a[i]) != dash2underscore(b[i]))
76 return false;
77 }
78 return true;
79}
80
81bool parameq(const char *a, const char *b)
82{
83 return parameqn(a, b, strlen(a)+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
86static int parse_one(char *param,
87 char *val,
Jim Cromie9fb48c72012-04-27 14:30:34 -060088 const char *doing,
Rusty Russell914dcaa2010-08-11 23:04:18 -060089 const struct kernel_param *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 unsigned num_params,
Pawel Moll026cee02012-03-26 12:50:51 +103091 s16 min_level,
92 s16 max_level,
Jim Cromie9fb48c72012-04-27 14:30:34 -060093 int (*handle_unknown)(char *param, char *val,
94 const char *doing))
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 unsigned int i;
Rusty Russell907b29e2010-08-11 23:04:19 -060097 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 /* Find parameter */
100 for (i = 0; i < num_params; i++) {
101 if (parameq(param, params[i].name)) {
Pawel Moll026cee02012-03-26 12:50:51 +1030102 if (params[i].level < min_level
103 || params[i].level > max_level)
104 return 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300105 /* No one handled NULL, so do it here. */
Steven Rostedtab013c52013-08-20 15:33:19 +0930106 if (!val &&
107 !(params[i].ops->flags & KERNEL_PARAM_FL_NOARG))
Rusty Russell2e9fb992010-08-11 23:04:10 -0600108 return -EINVAL;
Jim Cromie9fb48c72012-04-27 14:30:34 -0600109 pr_debug("handling %s with %p\n", param,
110 params[i].ops->set);
Rusty Russell907b29e2010-08-11 23:04:19 -0600111 mutex_lock(&param_lock);
112 err = params[i].ops->set(val, &params[i]);
113 mutex_unlock(&param_lock);
114 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
116 }
117
118 if (handle_unknown) {
Jim Cromie9fb48c72012-04-27 14:30:34 -0600119 pr_debug("doing %s: %s='%s'\n", doing, param, val);
120 return handle_unknown(param, val, doing);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122
Jim Cromie9fb48c72012-04-27 14:30:34 -0600123 pr_debug("Unknown argument '%s'\n", param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return -ENOENT;
125}
126
127/* You can use " around spaces, but can't escape ". */
128/* Hyphens and underscores equivalent in parameter names. */
129static char *next_arg(char *args, char **param, char **val)
130{
131 unsigned int i, equals = 0;
132 int in_quote = 0, quoted = 0;
133 char *next;
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (*args == '"') {
136 args++;
137 in_quote = 1;
138 quoted = 1;
139 }
140
141 for (i = 0; args[i]; i++) {
Peter Oberparleiter26d052b2009-07-06 17:11:22 +0200142 if (isspace(args[i]) && !in_quote)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 break;
144 if (equals == 0) {
145 if (args[i] == '=')
146 equals = i;
147 }
148 if (args[i] == '"')
149 in_quote = !in_quote;
150 }
151
152 *param = args;
153 if (!equals)
154 *val = NULL;
155 else {
156 args[equals] = '\0';
157 *val = args + equals + 1;
158
159 /* Don't include quotes in value. */
160 if (**val == '"') {
161 (*val)++;
162 if (args[i-1] == '"')
163 args[i-1] = '\0';
164 }
165 if (quoted && args[i-1] == '"')
166 args[i-1] = '\0';
167 }
168
169 if (args[i]) {
170 args[i] = '\0';
171 next = args + i + 1;
172 } else
173 next = args + i;
Rusty Russellf36462f2005-09-27 21:45:34 -0700174
175 /* Chew up trailing spaces. */
André Goddard Rosae7d28602009-12-14 18:01:06 -0800176 return skip_spaces(next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
179/* Args looks like "foo=bar,bar2 baz=fuz wiz". */
Rusty Russell51e158c2014-04-28 11:34:33 +0930180char *parse_args(const char *doing,
181 char *args,
182 const struct kernel_param *params,
183 unsigned num,
184 s16 min_level,
185 s16 max_level,
186 int (*unknown)(char *param, char *val, const char *doing))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 char *param, *val;
189
Rusty Russellf36462f2005-09-27 21:45:34 -0700190 /* Chew leading spaces */
André Goddard Rosae7d28602009-12-14 18:01:06 -0800191 args = skip_spaces(args);
Rusty Russellf36462f2005-09-27 21:45:34 -0700192
Jim Cromie1ef9eaf2012-05-03 11:57:37 -0600193 if (*args)
Jim Cromie9fb48c72012-04-27 14:30:34 -0600194 pr_debug("doing %s, parsing ARGS: '%s'\n", doing, args);
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 while (*args) {
197 int ret;
Ard van Breemena416aba2007-01-05 16:36:20 -0800198 int irq_was_disabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 args = next_arg(args, &param, &val);
Rusty Russell51e158c2014-04-28 11:34:33 +0930201 /* Stop at -- */
202 if (!val && strcmp(param, "--") == 0)
203 return args;
Ard van Breemena416aba2007-01-05 16:36:20 -0800204 irq_was_disabled = irqs_disabled();
Jim Cromie9fb48c72012-04-27 14:30:34 -0600205 ret = parse_one(param, val, doing, params, num,
Pawel Moll026cee02012-03-26 12:50:51 +1030206 min_level, max_level, unknown);
Jim Cromieb5f3abf2012-05-03 18:22:44 -0600207 if (irq_was_disabled && !irqs_disabled())
208 pr_warn("%s: option '%s' enabled irq's!\n",
209 doing, param);
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 switch (ret) {
212 case -ENOENT:
Jim Cromieb5f3abf2012-05-03 18:22:44 -0600213 pr_err("%s: Unknown parameter `%s'\n", doing, param);
Rusty Russell51e158c2014-04-28 11:34:33 +0930214 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 case -ENOSPC:
Jim Cromieb5f3abf2012-05-03 18:22:44 -0600216 pr_err("%s: `%s' too large for parameter `%s'\n",
Jim Cromie9fb48c72012-04-27 14:30:34 -0600217 doing, val ?: "", param);
Rusty Russell51e158c2014-04-28 11:34:33 +0930218 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 case 0:
220 break;
221 default:
Jim Cromieb5f3abf2012-05-03 18:22:44 -0600222 pr_err("%s: `%s' invalid for parameter `%s'\n",
Jim Cromie9fb48c72012-04-27 14:30:34 -0600223 doing, val ?: "", param);
Rusty Russell51e158c2014-04-28 11:34:33 +0930224 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226 }
227
228 /* All parsed OK. */
Rusty Russell51e158c2014-04-28 11:34:33 +0930229 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
232/* Lazy bastard, eh? */
Felipe Contreras88a88b322013-12-04 14:09:38 +1030233#define STANDARD_PARAM_DEF(name, type, format, strtolfn) \
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600234 int param_set_##name(const char *val, const struct kernel_param *kp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 { \
Felipe Contreras88a88b322013-12-04 14:09:38 +1030236 return strtolfn(val, 0, (type *)kp->arg); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 } \
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600238 int param_get_##name(char *buffer, const struct kernel_param *kp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 { \
Chen Gangf4940ab2013-08-20 15:35:04 +0930240 return scnprintf(buffer, PAGE_SIZE, format, \
241 *((type *)kp->arg)); \
Rusty Russella14fe242010-08-11 23:04:11 -0600242 } \
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600243 struct kernel_param_ops param_ops_##name = { \
244 .set = param_set_##name, \
245 .get = param_get_##name, \
246 }; \
Rusty Russella14fe242010-08-11 23:04:11 -0600247 EXPORT_SYMBOL(param_set_##name); \
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600248 EXPORT_SYMBOL(param_get_##name); \
249 EXPORT_SYMBOL(param_ops_##name)
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Felipe Contreras88a88b322013-12-04 14:09:38 +1030252STANDARD_PARAM_DEF(byte, unsigned char, "%hhu", kstrtou8);
253STANDARD_PARAM_DEF(short, short, "%hi", kstrtos16);
254STANDARD_PARAM_DEF(ushort, unsigned short, "%hu", kstrtou16);
255STANDARD_PARAM_DEF(int, int, "%i", kstrtoint);
256STANDARD_PARAM_DEF(uint, unsigned int, "%u", kstrtouint);
257STANDARD_PARAM_DEF(long, long, "%li", kstrtol);
258STANDARD_PARAM_DEF(ulong, unsigned long, "%lu", kstrtoul);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600260int param_set_charp(const char *val, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (strlen(val) > 1024) {
Jim Cromieb5f3abf2012-05-03 18:22:44 -0600263 pr_err("%s: string parameter too long\n", kp->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return -ENOSPC;
265 }
266
Rusty Russella1054322010-08-11 23:04:18 -0600267 maybe_kfree_parameter(*(char **)kp->arg);
268
269 /* This is a hack. We can't kmalloc in early boot, and we
Rusty Russelle180a6b2009-03-31 13:05:29 -0600270 * don't need to; this mangled commandline is preserved. */
271 if (slab_is_available()) {
Rusty Russella1054322010-08-11 23:04:18 -0600272 *(char **)kp->arg = kmalloc_parameter(strlen(val)+1);
Rusty Russelld553ad82009-10-29 08:56:17 -0600273 if (!*(char **)kp->arg)
Rusty Russelle180a6b2009-03-31 13:05:29 -0600274 return -ENOMEM;
Rusty Russella1054322010-08-11 23:04:18 -0600275 strcpy(*(char **)kp->arg, val);
Rusty Russelle180a6b2009-03-31 13:05:29 -0600276 } else
277 *(const char **)kp->arg = val;
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return 0;
280}
Rusty Russella14fe242010-08-11 23:04:11 -0600281EXPORT_SYMBOL(param_set_charp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600283int param_get_charp(char *buffer, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Chen Gangf4940ab2013-08-20 15:35:04 +0930285 return scnprintf(buffer, PAGE_SIZE, "%s", *((char **)kp->arg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
Rusty Russella14fe242010-08-11 23:04:11 -0600287EXPORT_SYMBOL(param_get_charp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Rusty Russella1054322010-08-11 23:04:18 -0600289static void param_free_charp(void *arg)
290{
291 maybe_kfree_parameter(*((char **)arg));
292}
293
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600294struct kernel_param_ops param_ops_charp = {
295 .set = param_set_charp,
296 .get = param_get_charp,
Rusty Russella1054322010-08-11 23:04:18 -0600297 .free = param_free_charp,
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600298};
299EXPORT_SYMBOL(param_ops_charp);
300
Rusty Russellfddd5202009-06-12 21:46:57 -0600301/* Actually could be a bool or an int, for historical reasons. */
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600302int param_set_bool(const char *val, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 /* No equals means "set"... */
305 if (!val) val = "1";
306
307 /* One of =[yYnN01] */
Rusty Russell8b825282012-03-26 12:50:51 +1030308 return strtobool(val, kp->arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
Rusty Russella14fe242010-08-11 23:04:11 -0600310EXPORT_SYMBOL(param_set_bool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600312int param_get_bool(char *buffer, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 /* Y and N chosen as being relatively non-coder friendly */
Rusty Russell8b825282012-03-26 12:50:51 +1030315 return sprintf(buffer, "%c", *(bool *)kp->arg ? 'Y' : 'N');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
Rusty Russella14fe242010-08-11 23:04:11 -0600317EXPORT_SYMBOL(param_get_bool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600319struct kernel_param_ops param_ops_bool = {
Steven Rostedtab013c52013-08-20 15:33:19 +0930320 .flags = KERNEL_PARAM_FL_NOARG,
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600321 .set = param_set_bool,
322 .get = param_get_bool,
323};
324EXPORT_SYMBOL(param_ops_bool);
325
Rusty Russellfddd5202009-06-12 21:46:57 -0600326/* This one must be bool. */
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600327int param_set_invbool(const char *val, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Rusty Russellfddd5202009-06-12 21:46:57 -0600329 int ret;
330 bool boolval;
Jan Beulich22e48ea2007-10-16 23:29:34 -0700331 struct kernel_param dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Jan Beulich22e48ea2007-10-16 23:29:34 -0700333 dummy.arg = &boolval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 ret = param_set_bool(val, &dummy);
335 if (ret == 0)
Rusty Russell9a71af22009-06-12 21:46:53 -0600336 *(bool *)kp->arg = !boolval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return ret;
338}
Rusty Russella14fe242010-08-11 23:04:11 -0600339EXPORT_SYMBOL(param_set_invbool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600341int param_get_invbool(char *buffer, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Rusty Russell9a71af22009-06-12 21:46:53 -0600343 return sprintf(buffer, "%c", (*(bool *)kp->arg) ? 'N' : 'Y');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
Rusty Russella14fe242010-08-11 23:04:11 -0600345EXPORT_SYMBOL(param_get_invbool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600347struct kernel_param_ops param_ops_invbool = {
348 .set = param_set_invbool,
349 .get = param_get_invbool,
350};
351EXPORT_SYMBOL(param_ops_invbool);
352
Rusty Russell69116f22012-01-13 09:32:17 +1030353int param_set_bint(const char *val, const struct kernel_param *kp)
354{
355 struct kernel_param boolkp;
356 bool v;
357 int ret;
358
359 /* Match bool exactly, by re-using it. */
360 boolkp = *kp;
361 boolkp.arg = &v;
Rusty Russell69116f22012-01-13 09:32:17 +1030362
363 ret = param_set_bool(val, &boolkp);
364 if (ret == 0)
365 *(int *)kp->arg = v;
366 return ret;
367}
368EXPORT_SYMBOL(param_set_bint);
369
370struct kernel_param_ops param_ops_bint = {
Steven Rostedtab013c52013-08-20 15:33:19 +0930371 .flags = KERNEL_PARAM_FL_NOARG,
Rusty Russell69116f22012-01-13 09:32:17 +1030372 .set = param_set_bint,
373 .get = param_get_int,
374};
375EXPORT_SYMBOL(param_ops_bint);
376
Bert Wesarg9730b5b2007-05-08 00:28:50 -0700377/* We break the rule and mangle the string. */
Adrian Bunk98717282006-03-25 03:07:06 -0800378static int param_array(const char *name,
379 const char *val,
380 unsigned int min, unsigned int max,
381 void *elem, int elemsize,
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600382 int (*set)(const char *, const struct kernel_param *kp),
Pawel Moll026cee02012-03-26 12:50:51 +1030383 s16 level,
Richard Knutssoneb38a992008-02-06 01:37:50 -0800384 unsigned int *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 int ret;
387 struct kernel_param kp;
388 char save;
389
390 /* Get the name right for errors. */
391 kp.name = name;
392 kp.arg = elem;
Pawel Moll026cee02012-03-26 12:50:51 +1030393 kp.level = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 *num = 0;
396 /* We expect a comma-separated list of values. */
397 do {
398 int len;
399
400 if (*num == max) {
Jim Cromieb5f3abf2012-05-03 18:22:44 -0600401 pr_err("%s: can only take %i arguments\n", name, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return -EINVAL;
403 }
404 len = strcspn(val, ",");
405
406 /* nul-terminate and parse */
407 save = val[len];
408 ((char *)val)[len] = '\0';
Rusty Russell907b29e2010-08-11 23:04:19 -0600409 BUG_ON(!mutex_is_locked(&param_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 ret = set(val, &kp);
411
412 if (ret != 0)
413 return ret;
414 kp.arg += elemsize;
415 val += len+1;
416 (*num)++;
417 } while (save == ',');
418
419 if (*num < min) {
Jim Cromieb5f3abf2012-05-03 18:22:44 -0600420 pr_err("%s: needs at least %i arguments\n", name, min);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return -EINVAL;
422 }
423 return 0;
424}
425
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600426static int param_array_set(const char *val, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Jan Beulich22e48ea2007-10-16 23:29:34 -0700428 const struct kparam_array *arr = kp->arr;
Bert Wesarg31143a12005-04-16 15:25:42 -0700429 unsigned int temp_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 return param_array(kp->name, val, 1, arr->max, arr->elem,
Pawel Moll026cee02012-03-26 12:50:51 +1030432 arr->elemsize, arr->ops->set, kp->level,
Rusty Russell3c7d76e2009-10-29 08:56:19 -0600433 arr->num ?: &temp_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600436static int param_array_get(char *buffer, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 int i, off, ret;
Jan Beulich22e48ea2007-10-16 23:29:34 -0700439 const struct kparam_array *arr = kp->arr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 struct kernel_param p;
441
442 p = *kp;
443 for (i = off = 0; i < (arr->num ? *arr->num : arr->max); i++) {
444 if (i)
445 buffer[off++] = ',';
446 p.arg = arr->elem + arr->elemsize * i;
Rusty Russell907b29e2010-08-11 23:04:19 -0600447 BUG_ON(!mutex_is_locked(&param_lock));
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600448 ret = arr->ops->get(buffer + off, &p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (ret < 0)
450 return ret;
451 off += ret;
452 }
453 buffer[off] = '\0';
454 return off;
455}
456
Rusty Russelle6df34a2010-08-11 23:04:17 -0600457static void param_array_free(void *arg)
458{
459 unsigned int i;
460 const struct kparam_array *arr = arg;
461
462 if (arr->ops->free)
463 for (i = 0; i < (arr->num ? *arr->num : arr->max); i++)
464 arr->ops->free(arr->elem + arr->elemsize * i);
465}
466
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600467struct kernel_param_ops param_array_ops = {
468 .set = param_array_set,
469 .get = param_array_get,
Rusty Russelle6df34a2010-08-11 23:04:17 -0600470 .free = param_array_free,
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600471};
472EXPORT_SYMBOL(param_array_ops);
473
474int param_set_copystring(const char *val, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Jan Beulich22e48ea2007-10-16 23:29:34 -0700476 const struct kparam_string *kps = kp->str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 if (strlen(val)+1 > kps->maxlen) {
Jim Cromieb5f3abf2012-05-03 18:22:44 -0600479 pr_err("%s: string doesn't fit in %u chars.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 kp->name, kps->maxlen-1);
481 return -ENOSPC;
482 }
483 strcpy(kps->string, val);
484 return 0;
485}
Rusty Russella14fe242010-08-11 23:04:11 -0600486EXPORT_SYMBOL(param_set_copystring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600488int param_get_string(char *buffer, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Jan Beulich22e48ea2007-10-16 23:29:34 -0700490 const struct kparam_string *kps = kp->str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return strlcpy(buffer, kps->string, kps->maxlen);
492}
Rusty Russella14fe242010-08-11 23:04:11 -0600493EXPORT_SYMBOL(param_get_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600495struct kernel_param_ops param_ops_string = {
496 .set = param_set_copystring,
497 .get = param_get_string,
498};
499EXPORT_SYMBOL(param_ops_string);
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501/* sysfs output in /sys/modules/XYZ/parameters/ */
Edward Z. Yang350f8252010-02-01 18:26:59 -0500502#define to_module_attr(n) container_of(n, struct module_attribute, attr)
503#define to_module_kobject(n) container_of(n, struct module_kobject, kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505extern struct kernel_param __start___param[], __stop___param[];
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507struct param_attribute
508{
509 struct module_attribute mattr;
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600510 const struct kernel_param *param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511};
512
513struct module_param_attrs
514{
Rusty Russell9b473de2008-10-22 10:00:22 -0500515 unsigned int num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 struct attribute_group grp;
517 struct param_attribute attrs[0];
518};
519
Randy Dunlapef665c12007-02-13 15:19:06 -0800520#ifdef CONFIG_SYSFS
Edward Z. Yang350f8252010-02-01 18:26:59 -0500521#define to_param_attr(n) container_of(n, struct param_attribute, mattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523static ssize_t param_attr_show(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +0930524 struct module_kobject *mk, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 int count;
527 struct param_attribute *attribute = to_param_attr(mattr);
528
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600529 if (!attribute->param->ops->get)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return -EPERM;
531
Rusty Russell907b29e2010-08-11 23:04:19 -0600532 mutex_lock(&param_lock);
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600533 count = attribute->param->ops->get(buf, attribute->param);
Rusty Russell907b29e2010-08-11 23:04:19 -0600534 mutex_unlock(&param_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (count > 0) {
536 strcat(buf, "\n");
537 ++count;
538 }
539 return count;
540}
541
542/* sysfs always hands a nul-terminated string in buf. We rely on that. */
543static ssize_t param_attr_store(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +0930544 struct module_kobject *km,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 const char *buf, size_t len)
546{
547 int err;
548 struct param_attribute *attribute = to_param_attr(mattr);
549
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600550 if (!attribute->param->ops->set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return -EPERM;
552
Rusty Russell907b29e2010-08-11 23:04:19 -0600553 mutex_lock(&param_lock);
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600554 err = attribute->param->ops->set(buf, attribute->param);
Rusty Russell907b29e2010-08-11 23:04:19 -0600555 mutex_unlock(&param_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (!err)
557 return len;
558 return err;
559}
Randy Dunlapef665c12007-02-13 15:19:06 -0800560#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562#ifdef CONFIG_MODULES
563#define __modinit
564#else
565#define __modinit __init
566#endif
567
Randy Dunlapef665c12007-02-13 15:19:06 -0800568#ifdef CONFIG_SYSFS
Rusty Russell907b29e2010-08-11 23:04:19 -0600569void __kernel_param_lock(void)
570{
571 mutex_lock(&param_lock);
572}
573EXPORT_SYMBOL(__kernel_param_lock);
574
575void __kernel_param_unlock(void)
576{
577 mutex_unlock(&param_lock);
578}
579EXPORT_SYMBOL(__kernel_param_unlock);
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581/*
Rusty Russell9b473de2008-10-22 10:00:22 -0500582 * add_sysfs_param - add a parameter to sysfs
583 * @mk: struct module_kobject
584 * @kparam: the actual parameter definition to add to sysfs
585 * @name: name of parameter
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 *
Rusty Russell9b473de2008-10-22 10:00:22 -0500587 * Create a kobject if for a (per-module) parameter if mp NULL, and
588 * create file in sysfs. Returns an error on out of memory. Always cleans up
589 * if there's an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 */
Rusty Russell9b473de2008-10-22 10:00:22 -0500591static __modinit int add_sysfs_param(struct module_kobject *mk,
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600592 const struct kernel_param *kp,
Rusty Russell9b473de2008-10-22 10:00:22 -0500593 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Rusty Russell9b473de2008-10-22 10:00:22 -0500595 struct module_param_attrs *new;
596 struct attribute **attrs;
597 int err, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Rusty Russell9b473de2008-10-22 10:00:22 -0500599 /* We don't bother calling this with invisible parameters. */
600 BUG_ON(!kp->perm);
601
602 if (!mk->mp) {
603 num = 0;
604 attrs = NULL;
605 } else {
606 num = mk->mp->num;
607 attrs = mk->mp->grp.attrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609
Rusty Russell9b473de2008-10-22 10:00:22 -0500610 /* Enlarge. */
611 new = krealloc(mk->mp,
612 sizeof(*mk->mp) + sizeof(mk->mp->attrs[0]) * (num+1),
613 GFP_KERNEL);
614 if (!new) {
David Woodhouse636621392013-03-14 13:23:11 +0000615 kfree(attrs);
Rusty Russell9b473de2008-10-22 10:00:22 -0500616 err = -ENOMEM;
617 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
David Woodhouse636621392013-03-14 13:23:11 +0000619 /* Despite looking like the typical realloc() bug, this is safe.
620 * We *want* the old 'attrs' to be freed either way, and we'll store
621 * the new one in the success case. */
Rusty Russell9b473de2008-10-22 10:00:22 -0500622 attrs = krealloc(attrs, sizeof(new->grp.attrs[0])*(num+2), GFP_KERNEL);
623 if (!attrs) {
624 err = -ENOMEM;
625 goto fail_free_new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
Rusty Russell9b473de2008-10-22 10:00:22 -0500627
628 /* Sysfs wants everything zeroed. */
629 memset(new, 0, sizeof(*new));
630 memset(&new->attrs[num], 0, sizeof(new->attrs[num]));
631 memset(&attrs[num], 0, sizeof(attrs[num]));
632 new->grp.name = "parameters";
633 new->grp.attrs = attrs;
634
635 /* Tack new one on the end. */
Eric W. Biedermana07e4152010-02-11 15:23:05 -0800636 sysfs_attr_init(&new->attrs[num].mattr.attr);
Rusty Russell9b473de2008-10-22 10:00:22 -0500637 new->attrs[num].param = kp;
638 new->attrs[num].mattr.show = param_attr_show;
639 new->attrs[num].mattr.store = param_attr_store;
640 new->attrs[num].mattr.attr.name = (char *)name;
641 new->attrs[num].mattr.attr.mode = kp->perm;
642 new->num = num+1;
643
644 /* Fix up all the pointers, since krealloc can move us */
645 for (num = 0; num < new->num; num++)
646 new->grp.attrs[num] = &new->attrs[num].mattr.attr;
647 new->grp.attrs[num] = NULL;
648
649 mk->mp = new;
650 return 0;
651
652fail_free_new:
653 kfree(new);
654fail:
655 mk->mp = NULL;
656 return err;
657}
658
Linus Torvaldsd2441182008-10-23 12:07:17 -0700659#ifdef CONFIG_MODULES
Rusty Russell9b473de2008-10-22 10:00:22 -0500660static void free_module_param_attrs(struct module_kobject *mk)
661{
662 kfree(mk->mp->grp.attrs);
663 kfree(mk->mp);
664 mk->mp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667/*
668 * module_param_sysfs_setup - setup sysfs support for one module
669 * @mod: module
670 * @kparam: module parameters (array)
671 * @num_params: number of module parameters
672 *
Rusty Russell9b473de2008-10-22 10:00:22 -0500673 * Adds sysfs entries for module parameters under
674 * /sys/module/[mod->name]/parameters/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 */
676int module_param_sysfs_setup(struct module *mod,
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600677 const struct kernel_param *kparam,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 unsigned int num_params)
679{
Rusty Russell9b473de2008-10-22 10:00:22 -0500680 int i, err;
681 bool params = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Rusty Russell9b473de2008-10-22 10:00:22 -0500683 for (i = 0; i < num_params; i++) {
684 if (kparam[i].perm == 0)
685 continue;
686 err = add_sysfs_param(&mod->mkobj, &kparam[i], kparam[i].name);
687 if (err)
688 return err;
689 params = true;
690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Rusty Russell9b473de2008-10-22 10:00:22 -0500692 if (!params)
693 return 0;
694
695 /* Create the param group. */
696 err = sysfs_create_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);
697 if (err)
698 free_module_param_attrs(&mod->mkobj);
699 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
702/*
703 * module_param_sysfs_remove - remove sysfs support for one module
704 * @mod: module
705 *
706 * Remove sysfs entries for module parameters and the corresponding
707 * kobject.
708 */
709void module_param_sysfs_remove(struct module *mod)
710{
Rusty Russell9b473de2008-10-22 10:00:22 -0500711 if (mod->mkobj.mp) {
712 sysfs_remove_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 /* We are positive that no one is using any param
714 * attrs at this point. Deallocate immediately. */
Rusty Russell9b473de2008-10-22 10:00:22 -0500715 free_module_param_attrs(&mod->mkobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
717}
718#endif
719
Rusty Russelle180a6b2009-03-31 13:05:29 -0600720void destroy_params(const struct kernel_param *params, unsigned num)
721{
Rusty Russelle6df34a2010-08-11 23:04:17 -0600722 unsigned int i;
723
724 for (i = 0; i < num; i++)
725 if (params[i].ops->free)
726 params[i].ops->free(params[i].arg);
Rusty Russelle180a6b2009-03-31 13:05:29 -0600727}
728
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800729static struct module_kobject * __init locate_module_kobject(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
731 struct module_kobject *mk;
Rusty Russell9b473de2008-10-22 10:00:22 -0500732 struct kobject *kobj;
733 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Rusty Russell9b473de2008-10-22 10:00:22 -0500735 kobj = kset_find_obj(module_kset, name);
736 if (kobj) {
Rusty Russell9b473de2008-10-22 10:00:22 -0500737 mk = to_module_kobject(kobj);
Rusty Russell9b473de2008-10-22 10:00:22 -0500738 } else {
739 mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL);
740 BUG_ON(!mk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Rusty Russell9b473de2008-10-22 10:00:22 -0500742 mk->mod = THIS_MODULE;
743 mk->kobj.kset = module_kset;
744 err = kobject_init_and_add(&mk->kobj, &module_ktype, NULL,
745 "%s", name);
Kay Sievers88bfa322011-07-24 22:06:04 +0930746#ifdef CONFIG_MODULES
747 if (!err)
748 err = sysfs_create_file(&mk->kobj, &module_uevent.attr);
749#endif
Rusty Russell9b473de2008-10-22 10:00:22 -0500750 if (err) {
751 kobject_put(&mk->kobj);
Jim Cromieb5f3abf2012-05-03 18:22:44 -0600752 pr_crit("Adding module '%s' to sysfs failed (%d), the system may be unstable.\n",
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800753 name, err);
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800754 return NULL;
Rusty Russell9b473de2008-10-22 10:00:22 -0500755 }
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800756
757 /* So that we hold reference in both cases. */
Rusty Russell9b473de2008-10-22 10:00:22 -0500758 kobject_get(&mk->kobj);
Greg Kroah-Hartman74c5b592007-07-30 11:26:38 -0700759 }
Rusty Russell9b473de2008-10-22 10:00:22 -0500760
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800761 return mk;
762}
763
764static void __init kernel_add_sysfs_param(const char *name,
765 struct kernel_param *kparam,
766 unsigned int name_skip)
767{
768 struct module_kobject *mk;
769 int err;
770
771 mk = locate_module_kobject(name);
772 if (!mk)
773 return;
774
775 /* We need to remove old parameters before adding more. */
776 if (mk->mp)
777 sysfs_remove_group(&mk->kobj, &mk->mp->grp);
778
Rusty Russell9b473de2008-10-22 10:00:22 -0500779 /* These should not fail at boot. */
780 err = add_sysfs_param(mk, kparam, kparam->name + name_skip);
781 BUG_ON(err);
782 err = sysfs_create_group(&mk->kobj, &mk->mp->grp);
783 BUG_ON(err);
Kay Sieversf30c53a2007-01-15 20:22:02 +0100784 kobject_uevent(&mk->kobj, KOBJ_ADD);
Rusty Russell9b473de2008-10-22 10:00:22 -0500785 kobject_put(&mk->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787
788/*
Jean Delvareb634d132013-07-02 15:35:11 +0930789 * param_sysfs_builtin - add sysfs parameters for built-in modules
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 *
791 * Add module_parameters to sysfs for "modules" built into the kernel.
792 *
793 * The "module" name (KBUILD_MODNAME) is stored before a dot, the
794 * "parameter" name is stored behind a dot in kernel_param->name. So,
795 * extract the "module" name for all built-in kernel_param-eters,
Rusty Russell9b473de2008-10-22 10:00:22 -0500796 * and for all who have the same, call kernel_add_sysfs_param.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 */
798static void __init param_sysfs_builtin(void)
799{
Rusty Russell9b473de2008-10-22 10:00:22 -0500800 struct kernel_param *kp;
801 unsigned int name_len;
802 char modname[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Rusty Russell9b473de2008-10-22 10:00:22 -0500804 for (kp = __start___param; kp < __stop___param; kp++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 char *dot;
806
Rusty Russell9b473de2008-10-22 10:00:22 -0500807 if (kp->perm == 0)
808 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Rusty Russell730b69d2008-10-22 10:00:22 -0500810 dot = strchr(kp->name, '.');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (!dot) {
Rusty Russell67e67ce2008-10-22 10:00:23 -0500812 /* This happens for core_param() */
813 strcpy(modname, "kernel");
814 name_len = 0;
815 } else {
816 name_len = dot - kp->name + 1;
817 strlcpy(modname, kp->name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
Rusty Russell67e67ce2008-10-22 10:00:23 -0500819 kernel_add_sysfs_param(modname, kp, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800823ssize_t __modver_version_show(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +0930824 struct module_kobject *mk, char *buf)
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800825{
826 struct module_version_attribute *vattr =
827 container_of(mattr, struct module_version_attribute, mattr);
828
Chen Gangf4940ab2013-08-20 15:35:04 +0930829 return scnprintf(buf, PAGE_SIZE, "%s\n", vattr->version);
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800830}
831
Dmitry Torokhovb4bc84282011-02-07 16:02:25 -0800832extern const struct module_version_attribute *__start___modver[];
833extern const struct module_version_attribute *__stop___modver[];
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800834
835static void __init version_sysfs_builtin(void)
836{
Dmitry Torokhovb4bc84282011-02-07 16:02:25 -0800837 const struct module_version_attribute **p;
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800838 struct module_kobject *mk;
839 int err;
840
Dmitry Torokhovb4bc84282011-02-07 16:02:25 -0800841 for (p = __start___modver; p < __stop___modver; p++) {
842 const struct module_version_attribute *vattr = *p;
843
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800844 mk = locate_module_kobject(vattr->module_name);
845 if (mk) {
846 err = sysfs_create_file(&mk->kobj, &vattr->mattr.attr);
847 kobject_uevent(&mk->kobj, KOBJ_ADD);
848 kobject_put(&mk->kobj);
849 }
850 }
851}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853/* module-related sysfs stuff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855static ssize_t module_attr_show(struct kobject *kobj,
856 struct attribute *attr,
857 char *buf)
858{
859 struct module_attribute *attribute;
860 struct module_kobject *mk;
861 int ret;
862
863 attribute = to_module_attr(attr);
864 mk = to_module_kobject(kobj);
865
866 if (!attribute->show)
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500867 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Kay Sievers4befb022011-07-24 22:06:04 +0930869 ret = attribute->show(attribute, mk, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return ret;
872}
873
874static ssize_t module_attr_store(struct kobject *kobj,
875 struct attribute *attr,
876 const char *buf, size_t len)
877{
878 struct module_attribute *attribute;
879 struct module_kobject *mk;
880 int ret;
881
882 attribute = to_module_attr(attr);
883 mk = to_module_kobject(kobj);
884
885 if (!attribute->store)
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500886 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Kay Sievers4befb022011-07-24 22:06:04 +0930888 ret = attribute->store(attribute, mk, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return ret;
891}
892
Emese Revfy52cf25d2010-01-19 02:58:23 +0100893static const struct sysfs_ops module_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 .show = module_attr_show,
895 .store = module_attr_store,
896};
897
Kay Sievers270a6c42007-01-18 13:26:15 +0100898static int uevent_filter(struct kset *kset, struct kobject *kobj)
899{
900 struct kobj_type *ktype = get_ktype(kobj);
901
902 if (ktype == &module_ktype)
903 return 1;
904 return 0;
905}
906
Emese Revfy9cd43612009-12-31 14:52:51 +0100907static const struct kset_uevent_ops module_uevent_ops = {
Kay Sievers270a6c42007-01-18 13:26:15 +0100908 .filter = uevent_filter,
909};
910
Greg Kroah-Hartman7405c1e2007-11-01 10:39:50 -0700911struct kset *module_kset;
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700912int module_sysfs_initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Li Zhong942e4432013-09-03 16:33:57 +0930914static void module_kobj_release(struct kobject *kobj)
915{
916 struct module_kobject *mk = to_module_kobject(kobj);
917 complete(mk->kobj_completion);
918}
919
Greg Kroah-Hartman7405c1e2007-11-01 10:39:50 -0700920struct kobj_type module_ktype = {
Li Zhong942e4432013-09-03 16:33:57 +0930921 .release = module_kobj_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 .sysfs_ops = &module_sysfs_ops,
923};
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925/*
926 * param_sysfs_init - wrapper for built-in params support
927 */
928static int __init param_sysfs_init(void)
929{
Greg Kroah-Hartman7405c1e2007-11-01 10:39:50 -0700930 module_kset = kset_create_and_add("module", &module_uevent_ops, NULL);
931 if (!module_kset) {
932 printk(KERN_WARNING "%s (%d): error creating kset\n",
933 __FILE__, __LINE__);
934 return -ENOMEM;
Randy Dunlapd8c76492006-09-29 01:58:55 -0700935 }
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700936 module_sysfs_initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800938 version_sysfs_builtin();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 param_sysfs_builtin();
940
941 return 0;
942}
Mark Huangd10be6d2006-09-29 01:59:34 -0700943subsys_initcall(param_sysfs_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Greg Kroah-Hartman7405c1e2007-11-01 10:39:50 -0700945#endif /* CONFIG_SYSFS */