blob: 041b5899d5e2d0c70f791224d4e5697ddff2880b [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
Rusty Russell7a486d372014-08-27 06:25:23 +093086static void param_check_unsafe(const struct kernel_param *kp)
87{
88 if (kp->flags & KERNEL_PARAM_FL_UNSAFE) {
89 pr_warn("Setting dangerous option %s - tainting kernel\n",
90 kp->name);
91 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
92 }
93}
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static int parse_one(char *param,
96 char *val,
Jim Cromie9fb48c72012-04-27 14:30:34 -060097 const char *doing,
Rusty Russell914dcaa2010-08-11 23:04:18 -060098 const struct kernel_param *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 unsigned num_params,
Pawel Moll026cee02012-03-26 12:50:51 +1030100 s16 min_level,
101 s16 max_level,
Jim Cromie9fb48c72012-04-27 14:30:34 -0600102 int (*handle_unknown)(char *param, char *val,
103 const char *doing))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 unsigned int i;
Rusty Russell907b29e2010-08-11 23:04:19 -0600106 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 /* Find parameter */
109 for (i = 0; i < num_params; i++) {
110 if (parameq(param, params[i].name)) {
Pawel Moll026cee02012-03-26 12:50:51 +1030111 if (params[i].level < min_level
112 || params[i].level > max_level)
113 return 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300114 /* No one handled NULL, so do it here. */
Steven Rostedtab013c52013-08-20 15:33:19 +0930115 if (!val &&
Jani Nikula6a4c2642014-08-27 06:21:23 +0930116 !(params[i].ops->flags & KERNEL_PARAM_OPS_FL_NOARG))
Rusty Russell2e9fb992010-08-11 23:04:10 -0600117 return -EINVAL;
Jim Cromie9fb48c72012-04-27 14:30:34 -0600118 pr_debug("handling %s with %p\n", param,
119 params[i].ops->set);
Rusty Russell907b29e2010-08-11 23:04:19 -0600120 mutex_lock(&param_lock);
Rusty Russell7a486d372014-08-27 06:25:23 +0930121 param_check_unsafe(&params[i]);
Rusty Russell907b29e2010-08-11 23:04:19 -0600122 err = params[i].ops->set(val, &params[i]);
123 mutex_unlock(&param_lock);
124 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
126 }
127
128 if (handle_unknown) {
Jim Cromie9fb48c72012-04-27 14:30:34 -0600129 pr_debug("doing %s: %s='%s'\n", doing, param, val);
130 return handle_unknown(param, val, doing);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
132
Jim Cromie9fb48c72012-04-27 14:30:34 -0600133 pr_debug("Unknown argument '%s'\n", param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return -ENOENT;
135}
136
137/* You can use " around spaces, but can't escape ". */
138/* Hyphens and underscores equivalent in parameter names. */
139static char *next_arg(char *args, char **param, char **val)
140{
141 unsigned int i, equals = 0;
142 int in_quote = 0, quoted = 0;
143 char *next;
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (*args == '"') {
146 args++;
147 in_quote = 1;
148 quoted = 1;
149 }
150
151 for (i = 0; args[i]; i++) {
Peter Oberparleiter26d052b2009-07-06 17:11:22 +0200152 if (isspace(args[i]) && !in_quote)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 break;
154 if (equals == 0) {
155 if (args[i] == '=')
156 equals = i;
157 }
158 if (args[i] == '"')
159 in_quote = !in_quote;
160 }
161
162 *param = args;
163 if (!equals)
164 *val = NULL;
165 else {
166 args[equals] = '\0';
167 *val = args + equals + 1;
168
169 /* Don't include quotes in value. */
170 if (**val == '"') {
171 (*val)++;
172 if (args[i-1] == '"')
173 args[i-1] = '\0';
174 }
175 if (quoted && args[i-1] == '"')
176 args[i-1] = '\0';
177 }
178
179 if (args[i]) {
180 args[i] = '\0';
181 next = args + i + 1;
182 } else
183 next = args + i;
Rusty Russellf36462f2005-09-27 21:45:34 -0700184
185 /* Chew up trailing spaces. */
André Goddard Rosae7d28602009-12-14 18:01:06 -0800186 return skip_spaces(next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
189/* Args looks like "foo=bar,bar2 baz=fuz wiz". */
Rusty Russell51e158c2014-04-28 11:34:33 +0930190char *parse_args(const char *doing,
191 char *args,
192 const struct kernel_param *params,
193 unsigned num,
194 s16 min_level,
195 s16 max_level,
196 int (*unknown)(char *param, char *val, const char *doing))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 char *param, *val;
199
Rusty Russellf36462f2005-09-27 21:45:34 -0700200 /* Chew leading spaces */
André Goddard Rosae7d28602009-12-14 18:01:06 -0800201 args = skip_spaces(args);
Rusty Russellf36462f2005-09-27 21:45:34 -0700202
Jim Cromie1ef9eaf2012-05-03 11:57:37 -0600203 if (*args)
Jim Cromie9fb48c72012-04-27 14:30:34 -0600204 pr_debug("doing %s, parsing ARGS: '%s'\n", doing, args);
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 while (*args) {
207 int ret;
Ard van Breemena416aba2007-01-05 16:36:20 -0800208 int irq_was_disabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 args = next_arg(args, &param, &val);
Rusty Russell51e158c2014-04-28 11:34:33 +0930211 /* Stop at -- */
212 if (!val && strcmp(param, "--") == 0)
213 return args;
Ard van Breemena416aba2007-01-05 16:36:20 -0800214 irq_was_disabled = irqs_disabled();
Jim Cromie9fb48c72012-04-27 14:30:34 -0600215 ret = parse_one(param, val, doing, params, num,
Pawel Moll026cee02012-03-26 12:50:51 +1030216 min_level, max_level, unknown);
Jim Cromieb5f3abf2012-05-03 18:22:44 -0600217 if (irq_was_disabled && !irqs_disabled())
218 pr_warn("%s: option '%s' enabled irq's!\n",
219 doing, param);
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 switch (ret) {
222 case -ENOENT:
Jim Cromieb5f3abf2012-05-03 18:22:44 -0600223 pr_err("%s: Unknown parameter `%s'\n", doing, param);
Rusty Russell51e158c2014-04-28 11:34:33 +0930224 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 case -ENOSPC:
Jim Cromieb5f3abf2012-05-03 18:22:44 -0600226 pr_err("%s: `%s' too large for parameter `%s'\n",
Jim Cromie9fb48c72012-04-27 14:30:34 -0600227 doing, val ?: "", param);
Rusty Russell51e158c2014-04-28 11:34:33 +0930228 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 case 0:
230 break;
231 default:
Jim Cromieb5f3abf2012-05-03 18:22:44 -0600232 pr_err("%s: `%s' invalid for parameter `%s'\n",
Jim Cromie9fb48c72012-04-27 14:30:34 -0600233 doing, val ?: "", param);
Rusty Russell51e158c2014-04-28 11:34:33 +0930234 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236 }
237
238 /* All parsed OK. */
Rusty Russell51e158c2014-04-28 11:34:33 +0930239 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
242/* Lazy bastard, eh? */
Felipe Contreras88a88b322013-12-04 14:09:38 +1030243#define STANDARD_PARAM_DEF(name, type, format, strtolfn) \
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600244 int param_set_##name(const char *val, const struct kernel_param *kp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 { \
Felipe Contreras88a88b322013-12-04 14:09:38 +1030246 return strtolfn(val, 0, (type *)kp->arg); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 } \
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600248 int param_get_##name(char *buffer, const struct kernel_param *kp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 { \
Chen Gangf4940ab2013-08-20 15:35:04 +0930250 return scnprintf(buffer, PAGE_SIZE, format, \
251 *((type *)kp->arg)); \
Rusty Russella14fe242010-08-11 23:04:11 -0600252 } \
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600253 struct kernel_param_ops param_ops_##name = { \
254 .set = param_set_##name, \
255 .get = param_get_##name, \
256 }; \
Rusty Russella14fe242010-08-11 23:04:11 -0600257 EXPORT_SYMBOL(param_set_##name); \
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600258 EXPORT_SYMBOL(param_get_##name); \
259 EXPORT_SYMBOL(param_ops_##name)
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Felipe Contreras88a88b322013-12-04 14:09:38 +1030262STANDARD_PARAM_DEF(byte, unsigned char, "%hhu", kstrtou8);
263STANDARD_PARAM_DEF(short, short, "%hi", kstrtos16);
264STANDARD_PARAM_DEF(ushort, unsigned short, "%hu", kstrtou16);
265STANDARD_PARAM_DEF(int, int, "%i", kstrtoint);
266STANDARD_PARAM_DEF(uint, unsigned int, "%u", kstrtouint);
267STANDARD_PARAM_DEF(long, long, "%li", kstrtol);
268STANDARD_PARAM_DEF(ulong, unsigned long, "%lu", kstrtoul);
Hannes Reineckeb4210b82014-06-25 15:27:37 +0200269STANDARD_PARAM_DEF(ullong, unsigned long long, "%llu", kstrtoull);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600271int param_set_charp(const char *val, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (strlen(val) > 1024) {
Jim Cromieb5f3abf2012-05-03 18:22:44 -0600274 pr_err("%s: string parameter too long\n", kp->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return -ENOSPC;
276 }
277
Rusty Russella1054322010-08-11 23:04:18 -0600278 maybe_kfree_parameter(*(char **)kp->arg);
279
280 /* This is a hack. We can't kmalloc in early boot, and we
Rusty Russelle180a6b2009-03-31 13:05:29 -0600281 * don't need to; this mangled commandline is preserved. */
282 if (slab_is_available()) {
Rusty Russella1054322010-08-11 23:04:18 -0600283 *(char **)kp->arg = kmalloc_parameter(strlen(val)+1);
Rusty Russelld553ad82009-10-29 08:56:17 -0600284 if (!*(char **)kp->arg)
Rusty Russelle180a6b2009-03-31 13:05:29 -0600285 return -ENOMEM;
Rusty Russella1054322010-08-11 23:04:18 -0600286 strcpy(*(char **)kp->arg, val);
Rusty Russelle180a6b2009-03-31 13:05:29 -0600287 } else
288 *(const char **)kp->arg = val;
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return 0;
291}
Rusty Russella14fe242010-08-11 23:04:11 -0600292EXPORT_SYMBOL(param_set_charp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600294int param_get_charp(char *buffer, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Chen Gangf4940ab2013-08-20 15:35:04 +0930296 return scnprintf(buffer, PAGE_SIZE, "%s", *((char **)kp->arg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
Rusty Russella14fe242010-08-11 23:04:11 -0600298EXPORT_SYMBOL(param_get_charp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Rusty Russella1054322010-08-11 23:04:18 -0600300static void param_free_charp(void *arg)
301{
302 maybe_kfree_parameter(*((char **)arg));
303}
304
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600305struct kernel_param_ops param_ops_charp = {
306 .set = param_set_charp,
307 .get = param_get_charp,
Rusty Russella1054322010-08-11 23:04:18 -0600308 .free = param_free_charp,
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600309};
310EXPORT_SYMBOL(param_ops_charp);
311
Rusty Russellfddd5202009-06-12 21:46:57 -0600312/* Actually could be a bool or an int, for historical reasons. */
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600313int param_set_bool(const char *val, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 /* No equals means "set"... */
316 if (!val) val = "1";
317
318 /* One of =[yYnN01] */
Rusty Russell8b825282012-03-26 12:50:51 +1030319 return strtobool(val, kp->arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
Rusty Russella14fe242010-08-11 23:04:11 -0600321EXPORT_SYMBOL(param_set_bool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600323int param_get_bool(char *buffer, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 /* Y and N chosen as being relatively non-coder friendly */
Rusty Russell8b825282012-03-26 12:50:51 +1030326 return sprintf(buffer, "%c", *(bool *)kp->arg ? 'Y' : 'N');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
Rusty Russella14fe242010-08-11 23:04:11 -0600328EXPORT_SYMBOL(param_get_bool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600330struct kernel_param_ops param_ops_bool = {
Jani Nikula6a4c2642014-08-27 06:21:23 +0930331 .flags = KERNEL_PARAM_OPS_FL_NOARG,
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600332 .set = param_set_bool,
333 .get = param_get_bool,
334};
335EXPORT_SYMBOL(param_ops_bool);
336
Rusty Russellfddd5202009-06-12 21:46:57 -0600337/* This one must be bool. */
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600338int param_set_invbool(const char *val, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Rusty Russellfddd5202009-06-12 21:46:57 -0600340 int ret;
341 bool boolval;
Jan Beulich22e48ea2007-10-16 23:29:34 -0700342 struct kernel_param dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Jan Beulich22e48ea2007-10-16 23:29:34 -0700344 dummy.arg = &boolval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 ret = param_set_bool(val, &dummy);
346 if (ret == 0)
Rusty Russell9a71af22009-06-12 21:46:53 -0600347 *(bool *)kp->arg = !boolval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return ret;
349}
Rusty Russella14fe242010-08-11 23:04:11 -0600350EXPORT_SYMBOL(param_set_invbool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600352int param_get_invbool(char *buffer, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Rusty Russell9a71af22009-06-12 21:46:53 -0600354 return sprintf(buffer, "%c", (*(bool *)kp->arg) ? 'N' : 'Y');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
Rusty Russella14fe242010-08-11 23:04:11 -0600356EXPORT_SYMBOL(param_get_invbool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600358struct kernel_param_ops param_ops_invbool = {
359 .set = param_set_invbool,
360 .get = param_get_invbool,
361};
362EXPORT_SYMBOL(param_ops_invbool);
363
Rusty Russell69116f22012-01-13 09:32:17 +1030364int param_set_bint(const char *val, const struct kernel_param *kp)
365{
366 struct kernel_param boolkp;
367 bool v;
368 int ret;
369
370 /* Match bool exactly, by re-using it. */
371 boolkp = *kp;
372 boolkp.arg = &v;
Rusty Russell69116f22012-01-13 09:32:17 +1030373
374 ret = param_set_bool(val, &boolkp);
375 if (ret == 0)
376 *(int *)kp->arg = v;
377 return ret;
378}
379EXPORT_SYMBOL(param_set_bint);
380
381struct kernel_param_ops param_ops_bint = {
Jani Nikula6a4c2642014-08-27 06:21:23 +0930382 .flags = KERNEL_PARAM_OPS_FL_NOARG,
Rusty Russell69116f22012-01-13 09:32:17 +1030383 .set = param_set_bint,
384 .get = param_get_int,
385};
386EXPORT_SYMBOL(param_ops_bint);
387
Bert Wesarg9730b5b2007-05-08 00:28:50 -0700388/* We break the rule and mangle the string. */
Adrian Bunk98717282006-03-25 03:07:06 -0800389static int param_array(const char *name,
390 const char *val,
391 unsigned int min, unsigned int max,
392 void *elem, int elemsize,
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600393 int (*set)(const char *, const struct kernel_param *kp),
Pawel Moll026cee02012-03-26 12:50:51 +1030394 s16 level,
Richard Knutssoneb38a992008-02-06 01:37:50 -0800395 unsigned int *num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 int ret;
398 struct kernel_param kp;
399 char save;
400
401 /* Get the name right for errors. */
402 kp.name = name;
403 kp.arg = elem;
Pawel Moll026cee02012-03-26 12:50:51 +1030404 kp.level = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 *num = 0;
407 /* We expect a comma-separated list of values. */
408 do {
409 int len;
410
411 if (*num == max) {
Jim Cromieb5f3abf2012-05-03 18:22:44 -0600412 pr_err("%s: can only take %i arguments\n", name, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return -EINVAL;
414 }
415 len = strcspn(val, ",");
416
417 /* nul-terminate and parse */
418 save = val[len];
419 ((char *)val)[len] = '\0';
Rusty Russell907b29e2010-08-11 23:04:19 -0600420 BUG_ON(!mutex_is_locked(&param_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 ret = set(val, &kp);
422
423 if (ret != 0)
424 return ret;
425 kp.arg += elemsize;
426 val += len+1;
427 (*num)++;
428 } while (save == ',');
429
430 if (*num < min) {
Jim Cromieb5f3abf2012-05-03 18:22:44 -0600431 pr_err("%s: needs at least %i arguments\n", name, min);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return -EINVAL;
433 }
434 return 0;
435}
436
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600437static int param_array_set(const char *val, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Jan Beulich22e48ea2007-10-16 23:29:34 -0700439 const struct kparam_array *arr = kp->arr;
Bert Wesarg31143a12005-04-16 15:25:42 -0700440 unsigned int temp_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 return param_array(kp->name, val, 1, arr->max, arr->elem,
Pawel Moll026cee02012-03-26 12:50:51 +1030443 arr->elemsize, arr->ops->set, kp->level,
Rusty Russell3c7d76e2009-10-29 08:56:19 -0600444 arr->num ?: &temp_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600447static int param_array_get(char *buffer, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 int i, off, ret;
Jan Beulich22e48ea2007-10-16 23:29:34 -0700450 const struct kparam_array *arr = kp->arr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 struct kernel_param p;
452
453 p = *kp;
454 for (i = off = 0; i < (arr->num ? *arr->num : arr->max); i++) {
455 if (i)
456 buffer[off++] = ',';
457 p.arg = arr->elem + arr->elemsize * i;
Rusty Russell907b29e2010-08-11 23:04:19 -0600458 BUG_ON(!mutex_is_locked(&param_lock));
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600459 ret = arr->ops->get(buffer + off, &p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (ret < 0)
461 return ret;
462 off += ret;
463 }
464 buffer[off] = '\0';
465 return off;
466}
467
Rusty Russelle6df34a2010-08-11 23:04:17 -0600468static void param_array_free(void *arg)
469{
470 unsigned int i;
471 const struct kparam_array *arr = arg;
472
473 if (arr->ops->free)
474 for (i = 0; i < (arr->num ? *arr->num : arr->max); i++)
475 arr->ops->free(arr->elem + arr->elemsize * i);
476}
477
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600478struct kernel_param_ops param_array_ops = {
479 .set = param_array_set,
480 .get = param_array_get,
Rusty Russelle6df34a2010-08-11 23:04:17 -0600481 .free = param_array_free,
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600482};
483EXPORT_SYMBOL(param_array_ops);
484
485int param_set_copystring(const char *val, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Jan Beulich22e48ea2007-10-16 23:29:34 -0700487 const struct kparam_string *kps = kp->str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 if (strlen(val)+1 > kps->maxlen) {
Jim Cromieb5f3abf2012-05-03 18:22:44 -0600490 pr_err("%s: string doesn't fit in %u chars.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 kp->name, kps->maxlen-1);
492 return -ENOSPC;
493 }
494 strcpy(kps->string, val);
495 return 0;
496}
Rusty Russella14fe242010-08-11 23:04:11 -0600497EXPORT_SYMBOL(param_set_copystring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600499int param_get_string(char *buffer, const struct kernel_param *kp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Jan Beulich22e48ea2007-10-16 23:29:34 -0700501 const struct kparam_string *kps = kp->str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return strlcpy(buffer, kps->string, kps->maxlen);
503}
Rusty Russella14fe242010-08-11 23:04:11 -0600504EXPORT_SYMBOL(param_get_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600506struct kernel_param_ops param_ops_string = {
507 .set = param_set_copystring,
508 .get = param_get_string,
509};
510EXPORT_SYMBOL(param_ops_string);
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512/* sysfs output in /sys/modules/XYZ/parameters/ */
Edward Z. Yang350f8252010-02-01 18:26:59 -0500513#define to_module_attr(n) container_of(n, struct module_attribute, attr)
514#define to_module_kobject(n) container_of(n, struct module_kobject, kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516extern struct kernel_param __start___param[], __stop___param[];
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518struct param_attribute
519{
520 struct module_attribute mattr;
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600521 const struct kernel_param *param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522};
523
524struct module_param_attrs
525{
Rusty Russell9b473de2008-10-22 10:00:22 -0500526 unsigned int num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 struct attribute_group grp;
528 struct param_attribute attrs[0];
529};
530
Randy Dunlapef665c12007-02-13 15:19:06 -0800531#ifdef CONFIG_SYSFS
Edward Z. Yang350f8252010-02-01 18:26:59 -0500532#define to_param_attr(n) container_of(n, struct param_attribute, mattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534static ssize_t param_attr_show(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +0930535 struct module_kobject *mk, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
537 int count;
538 struct param_attribute *attribute = to_param_attr(mattr);
539
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600540 if (!attribute->param->ops->get)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return -EPERM;
542
Rusty Russell907b29e2010-08-11 23:04:19 -0600543 mutex_lock(&param_lock);
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600544 count = attribute->param->ops->get(buf, attribute->param);
Rusty Russell907b29e2010-08-11 23:04:19 -0600545 mutex_unlock(&param_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (count > 0) {
547 strcat(buf, "\n");
548 ++count;
549 }
550 return count;
551}
552
553/* sysfs always hands a nul-terminated string in buf. We rely on that. */
554static ssize_t param_attr_store(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +0930555 struct module_kobject *km,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 const char *buf, size_t len)
557{
558 int err;
559 struct param_attribute *attribute = to_param_attr(mattr);
560
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600561 if (!attribute->param->ops->set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return -EPERM;
563
Rusty Russell907b29e2010-08-11 23:04:19 -0600564 mutex_lock(&param_lock);
Rusty Russell7a486d372014-08-27 06:25:23 +0930565 param_check_unsafe(attribute->param);
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600566 err = attribute->param->ops->set(buf, attribute->param);
Rusty Russell907b29e2010-08-11 23:04:19 -0600567 mutex_unlock(&param_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (!err)
569 return len;
570 return err;
571}
Randy Dunlapef665c12007-02-13 15:19:06 -0800572#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574#ifdef CONFIG_MODULES
575#define __modinit
576#else
577#define __modinit __init
578#endif
579
Randy Dunlapef665c12007-02-13 15:19:06 -0800580#ifdef CONFIG_SYSFS
Rusty Russell907b29e2010-08-11 23:04:19 -0600581void __kernel_param_lock(void)
582{
583 mutex_lock(&param_lock);
584}
585EXPORT_SYMBOL(__kernel_param_lock);
586
587void __kernel_param_unlock(void)
588{
589 mutex_unlock(&param_lock);
590}
591EXPORT_SYMBOL(__kernel_param_unlock);
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593/*
Rusty Russell9b473de2008-10-22 10:00:22 -0500594 * add_sysfs_param - add a parameter to sysfs
595 * @mk: struct module_kobject
596 * @kparam: the actual parameter definition to add to sysfs
597 * @name: name of parameter
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 *
Rusty Russell9b473de2008-10-22 10:00:22 -0500599 * Create a kobject if for a (per-module) parameter if mp NULL, and
600 * create file in sysfs. Returns an error on out of memory. Always cleans up
601 * if there's an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 */
Rusty Russell9b473de2008-10-22 10:00:22 -0500603static __modinit int add_sysfs_param(struct module_kobject *mk,
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600604 const struct kernel_param *kp,
Rusty Russell9b473de2008-10-22 10:00:22 -0500605 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Rusty Russell9b473de2008-10-22 10:00:22 -0500607 struct module_param_attrs *new;
608 struct attribute **attrs;
609 int err, num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Rusty Russell9b473de2008-10-22 10:00:22 -0500611 /* We don't bother calling this with invisible parameters. */
612 BUG_ON(!kp->perm);
613
614 if (!mk->mp) {
615 num = 0;
616 attrs = NULL;
617 } else {
618 num = mk->mp->num;
619 attrs = mk->mp->grp.attrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621
Rusty Russell9b473de2008-10-22 10:00:22 -0500622 /* Enlarge. */
623 new = krealloc(mk->mp,
624 sizeof(*mk->mp) + sizeof(mk->mp->attrs[0]) * (num+1),
625 GFP_KERNEL);
626 if (!new) {
David Woodhouse636621392013-03-14 13:23:11 +0000627 kfree(attrs);
Rusty Russell9b473de2008-10-22 10:00:22 -0500628 err = -ENOMEM;
629 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
David Woodhouse636621392013-03-14 13:23:11 +0000631 /* Despite looking like the typical realloc() bug, this is safe.
632 * We *want* the old 'attrs' to be freed either way, and we'll store
633 * the new one in the success case. */
Rusty Russell9b473de2008-10-22 10:00:22 -0500634 attrs = krealloc(attrs, sizeof(new->grp.attrs[0])*(num+2), GFP_KERNEL);
635 if (!attrs) {
636 err = -ENOMEM;
637 goto fail_free_new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
Rusty Russell9b473de2008-10-22 10:00:22 -0500639
640 /* Sysfs wants everything zeroed. */
641 memset(new, 0, sizeof(*new));
642 memset(&new->attrs[num], 0, sizeof(new->attrs[num]));
643 memset(&attrs[num], 0, sizeof(attrs[num]));
644 new->grp.name = "parameters";
645 new->grp.attrs = attrs;
646
647 /* Tack new one on the end. */
Eric W. Biedermana07e4152010-02-11 15:23:05 -0800648 sysfs_attr_init(&new->attrs[num].mattr.attr);
Rusty Russell9b473de2008-10-22 10:00:22 -0500649 new->attrs[num].param = kp;
650 new->attrs[num].mattr.show = param_attr_show;
651 new->attrs[num].mattr.store = param_attr_store;
652 new->attrs[num].mattr.attr.name = (char *)name;
653 new->attrs[num].mattr.attr.mode = kp->perm;
654 new->num = num+1;
655
656 /* Fix up all the pointers, since krealloc can move us */
657 for (num = 0; num < new->num; num++)
658 new->grp.attrs[num] = &new->attrs[num].mattr.attr;
659 new->grp.attrs[num] = NULL;
660
661 mk->mp = new;
662 return 0;
663
664fail_free_new:
665 kfree(new);
666fail:
667 mk->mp = NULL;
668 return err;
669}
670
Linus Torvaldsd2441182008-10-23 12:07:17 -0700671#ifdef CONFIG_MODULES
Rusty Russell9b473de2008-10-22 10:00:22 -0500672static void free_module_param_attrs(struct module_kobject *mk)
673{
674 kfree(mk->mp->grp.attrs);
675 kfree(mk->mp);
676 mk->mp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679/*
680 * module_param_sysfs_setup - setup sysfs support for one module
681 * @mod: module
682 * @kparam: module parameters (array)
683 * @num_params: number of module parameters
684 *
Rusty Russell9b473de2008-10-22 10:00:22 -0500685 * Adds sysfs entries for module parameters under
686 * /sys/module/[mod->name]/parameters/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 */
688int module_param_sysfs_setup(struct module *mod,
Rusty Russell9bbb9e52010-08-11 23:04:12 -0600689 const struct kernel_param *kparam,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 unsigned int num_params)
691{
Rusty Russell9b473de2008-10-22 10:00:22 -0500692 int i, err;
693 bool params = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Rusty Russell9b473de2008-10-22 10:00:22 -0500695 for (i = 0; i < num_params; i++) {
696 if (kparam[i].perm == 0)
697 continue;
698 err = add_sysfs_param(&mod->mkobj, &kparam[i], kparam[i].name);
699 if (err)
700 return err;
701 params = true;
702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Rusty Russell9b473de2008-10-22 10:00:22 -0500704 if (!params)
705 return 0;
706
707 /* Create the param group. */
708 err = sysfs_create_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);
709 if (err)
710 free_module_param_attrs(&mod->mkobj);
711 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
714/*
715 * module_param_sysfs_remove - remove sysfs support for one module
716 * @mod: module
717 *
718 * Remove sysfs entries for module parameters and the corresponding
719 * kobject.
720 */
721void module_param_sysfs_remove(struct module *mod)
722{
Rusty Russell9b473de2008-10-22 10:00:22 -0500723 if (mod->mkobj.mp) {
724 sysfs_remove_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 /* We are positive that no one is using any param
726 * attrs at this point. Deallocate immediately. */
Rusty Russell9b473de2008-10-22 10:00:22 -0500727 free_module_param_attrs(&mod->mkobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
729}
730#endif
731
Rusty Russelle180a6b2009-03-31 13:05:29 -0600732void destroy_params(const struct kernel_param *params, unsigned num)
733{
Rusty Russelle6df34a2010-08-11 23:04:17 -0600734 unsigned int i;
735
736 for (i = 0; i < num; i++)
737 if (params[i].ops->free)
738 params[i].ops->free(params[i].arg);
Rusty Russelle180a6b2009-03-31 13:05:29 -0600739}
740
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800741static struct module_kobject * __init locate_module_kobject(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 struct module_kobject *mk;
Rusty Russell9b473de2008-10-22 10:00:22 -0500744 struct kobject *kobj;
745 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Rusty Russell9b473de2008-10-22 10:00:22 -0500747 kobj = kset_find_obj(module_kset, name);
748 if (kobj) {
Rusty Russell9b473de2008-10-22 10:00:22 -0500749 mk = to_module_kobject(kobj);
Rusty Russell9b473de2008-10-22 10:00:22 -0500750 } else {
751 mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL);
752 BUG_ON(!mk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Rusty Russell9b473de2008-10-22 10:00:22 -0500754 mk->mod = THIS_MODULE;
755 mk->kobj.kset = module_kset;
756 err = kobject_init_and_add(&mk->kobj, &module_ktype, NULL,
757 "%s", name);
Kay Sievers88bfa322011-07-24 22:06:04 +0930758#ifdef CONFIG_MODULES
759 if (!err)
760 err = sysfs_create_file(&mk->kobj, &module_uevent.attr);
761#endif
Rusty Russell9b473de2008-10-22 10:00:22 -0500762 if (err) {
763 kobject_put(&mk->kobj);
Jim Cromieb5f3abf2012-05-03 18:22:44 -0600764 pr_crit("Adding module '%s' to sysfs failed (%d), the system may be unstable.\n",
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800765 name, err);
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800766 return NULL;
Rusty Russell9b473de2008-10-22 10:00:22 -0500767 }
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800768
769 /* So that we hold reference in both cases. */
Rusty Russell9b473de2008-10-22 10:00:22 -0500770 kobject_get(&mk->kobj);
Greg Kroah-Hartman74c5b592007-07-30 11:26:38 -0700771 }
Rusty Russell9b473de2008-10-22 10:00:22 -0500772
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800773 return mk;
774}
775
776static void __init kernel_add_sysfs_param(const char *name,
777 struct kernel_param *kparam,
778 unsigned int name_skip)
779{
780 struct module_kobject *mk;
781 int err;
782
783 mk = locate_module_kobject(name);
784 if (!mk)
785 return;
786
787 /* We need to remove old parameters before adding more. */
788 if (mk->mp)
789 sysfs_remove_group(&mk->kobj, &mk->mp->grp);
790
Rusty Russell9b473de2008-10-22 10:00:22 -0500791 /* These should not fail at boot. */
792 err = add_sysfs_param(mk, kparam, kparam->name + name_skip);
793 BUG_ON(err);
794 err = sysfs_create_group(&mk->kobj, &mk->mp->grp);
795 BUG_ON(err);
Kay Sieversf30c53a2007-01-15 20:22:02 +0100796 kobject_uevent(&mk->kobj, KOBJ_ADD);
Rusty Russell9b473de2008-10-22 10:00:22 -0500797 kobject_put(&mk->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
800/*
Jean Delvareb634d132013-07-02 15:35:11 +0930801 * param_sysfs_builtin - add sysfs parameters for built-in modules
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 *
803 * Add module_parameters to sysfs for "modules" built into the kernel.
804 *
805 * The "module" name (KBUILD_MODNAME) is stored before a dot, the
806 * "parameter" name is stored behind a dot in kernel_param->name. So,
807 * extract the "module" name for all built-in kernel_param-eters,
Rusty Russell9b473de2008-10-22 10:00:22 -0500808 * and for all who have the same, call kernel_add_sysfs_param.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 */
810static void __init param_sysfs_builtin(void)
811{
Rusty Russell9b473de2008-10-22 10:00:22 -0500812 struct kernel_param *kp;
813 unsigned int name_len;
814 char modname[MODULE_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Rusty Russell9b473de2008-10-22 10:00:22 -0500816 for (kp = __start___param; kp < __stop___param; kp++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 char *dot;
818
Rusty Russell9b473de2008-10-22 10:00:22 -0500819 if (kp->perm == 0)
820 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Rusty Russell730b69d2008-10-22 10:00:22 -0500822 dot = strchr(kp->name, '.');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (!dot) {
Rusty Russell67e67ce2008-10-22 10:00:23 -0500824 /* This happens for core_param() */
825 strcpy(modname, "kernel");
826 name_len = 0;
827 } else {
828 name_len = dot - kp->name + 1;
829 strlcpy(modname, kp->name, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
Rusty Russell67e67ce2008-10-22 10:00:23 -0500831 kernel_add_sysfs_param(modname, kp, name_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800835ssize_t __modver_version_show(struct module_attribute *mattr,
Kay Sievers4befb022011-07-24 22:06:04 +0930836 struct module_kobject *mk, char *buf)
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800837{
838 struct module_version_attribute *vattr =
839 container_of(mattr, struct module_version_attribute, mattr);
840
Chen Gangf4940ab2013-08-20 15:35:04 +0930841 return scnprintf(buf, PAGE_SIZE, "%s\n", vattr->version);
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800842}
843
Dmitry Torokhovb4bc84282011-02-07 16:02:25 -0800844extern const struct module_version_attribute *__start___modver[];
845extern const struct module_version_attribute *__stop___modver[];
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800846
847static void __init version_sysfs_builtin(void)
848{
Dmitry Torokhovb4bc84282011-02-07 16:02:25 -0800849 const struct module_version_attribute **p;
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800850 struct module_kobject *mk;
851 int err;
852
Dmitry Torokhovb4bc84282011-02-07 16:02:25 -0800853 for (p = __start___modver; p < __stop___modver; p++) {
854 const struct module_version_attribute *vattr = *p;
855
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800856 mk = locate_module_kobject(vattr->module_name);
857 if (mk) {
858 err = sysfs_create_file(&mk->kobj, &vattr->mattr.attr);
859 kobject_uevent(&mk->kobj, KOBJ_ADD);
860 kobject_put(&mk->kobj);
861 }
862 }
863}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865/* module-related sysfs stuff */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867static ssize_t module_attr_show(struct kobject *kobj,
868 struct attribute *attr,
869 char *buf)
870{
871 struct module_attribute *attribute;
872 struct module_kobject *mk;
873 int ret;
874
875 attribute = to_module_attr(attr);
876 mk = to_module_kobject(kobj);
877
878 if (!attribute->show)
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500879 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Kay Sievers4befb022011-07-24 22:06:04 +0930881 ret = attribute->show(attribute, mk, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return ret;
884}
885
886static ssize_t module_attr_store(struct kobject *kobj,
887 struct attribute *attr,
888 const char *buf, size_t len)
889{
890 struct module_attribute *attribute;
891 struct module_kobject *mk;
892 int ret;
893
894 attribute = to_module_attr(attr);
895 mk = to_module_kobject(kobj);
896
897 if (!attribute->store)
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500898 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Kay Sievers4befb022011-07-24 22:06:04 +0930900 ret = attribute->store(attribute, mk, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return ret;
903}
904
Emese Revfy52cf25d2010-01-19 02:58:23 +0100905static const struct sysfs_ops module_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 .show = module_attr_show,
907 .store = module_attr_store,
908};
909
Kay Sievers270a6c42007-01-18 13:26:15 +0100910static int uevent_filter(struct kset *kset, struct kobject *kobj)
911{
912 struct kobj_type *ktype = get_ktype(kobj);
913
914 if (ktype == &module_ktype)
915 return 1;
916 return 0;
917}
918
Emese Revfy9cd43612009-12-31 14:52:51 +0100919static const struct kset_uevent_ops module_uevent_ops = {
Kay Sievers270a6c42007-01-18 13:26:15 +0100920 .filter = uevent_filter,
921};
922
Greg Kroah-Hartman7405c1e2007-11-01 10:39:50 -0700923struct kset *module_kset;
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700924int module_sysfs_initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Li Zhong942e4432013-09-03 16:33:57 +0930926static void module_kobj_release(struct kobject *kobj)
927{
928 struct module_kobject *mk = to_module_kobject(kobj);
929 complete(mk->kobj_completion);
930}
931
Greg Kroah-Hartman7405c1e2007-11-01 10:39:50 -0700932struct kobj_type module_ktype = {
Li Zhong942e4432013-09-03 16:33:57 +0930933 .release = module_kobj_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 .sysfs_ops = &module_sysfs_ops,
935};
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937/*
938 * param_sysfs_init - wrapper for built-in params support
939 */
940static int __init param_sysfs_init(void)
941{
Greg Kroah-Hartman7405c1e2007-11-01 10:39:50 -0700942 module_kset = kset_create_and_add("module", &module_uevent_ops, NULL);
943 if (!module_kset) {
944 printk(KERN_WARNING "%s (%d): error creating kset\n",
945 __FILE__, __LINE__);
946 return -ENOMEM;
Randy Dunlapd8c76492006-09-29 01:58:55 -0700947 }
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700948 module_sysfs_initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Dmitry Torokhove94965e2010-12-15 14:00:19 -0800950 version_sysfs_builtin();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 param_sysfs_builtin();
952
953 return 0;
954}
Mark Huangd10be6d2006-09-29 01:59:34 -0700955subsys_initcall(param_sysfs_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Greg Kroah-Hartman7405c1e2007-11-01 10:39:50 -0700957#endif /* CONFIG_SYSFS */