31
31
#define DEBUGP(fmt, a...)
34
/* Protects all parameters, and incidentally kmalloced_param list. */
35
static DEFINE_MUTEX(param_lock);
37
/* This just allows us to keep track of which parameters are kmalloced. */
38
struct kmalloced_param {
39
struct list_head list;
42
static LIST_HEAD(kmalloced_params);
44
static void *kmalloc_parameter(unsigned int size)
46
struct kmalloced_param *p;
48
p = kmalloc(sizeof(*p) + size, GFP_KERNEL);
52
list_add(&p->list, &kmalloced_params);
56
/* Does nothing if parameter wasn't kmalloced above. */
57
static void maybe_kfree_parameter(void *param)
59
struct kmalloced_param *p;
61
list_for_each_entry(p, &kmalloced_params, list) {
62
if (p->val == param) {
34
70
static inline char dash2underscore(char c)
50
86
static int parse_one(char *param,
52
struct kernel_param *params,
88
const struct kernel_param *params,
53
89
unsigned num_params,
54
90
int (*handle_unknown)(char *param, char *val))
58
95
/* Find parameter */
59
96
for (i = 0; i < num_params; i++) {
60
97
if (parameq(param, params[i].name)) {
98
/* Noone handled NULL, so do it here. */
99
if (!val && params[i].ops->set != param_set_bool)
61
101
DEBUGP("They are equal! Calling %p\n",
63
return params[i].set(val, ¶ms[i]);
103
mutex_lock(¶m_lock);
104
err = params[i].ops->set(val, ¶ms[i]);
105
mutex_unlock(¶m_lock);
124
167
/* Chew up trailing spaces. */
125
while (isspace(*next))
168
return skip_spaces(next);
130
171
/* Args looks like "foo=bar,bar2 baz=fuz wiz". */
131
172
int parse_args(const char *name,
133
struct kernel_param *params,
174
const struct kernel_param *params,
135
176
int (*unknown)(char *param, char *val))
180
220
/* Lazy bastard, eh? */
181
221
#define STANDARD_PARAM_DEF(name, type, format, tmptype, strtolfn) \
182
int param_set_##name(const char *val, struct kernel_param *kp) \
222
int param_set_##name(const char *val, const struct kernel_param *kp) \
187
if (!val) return -EINVAL; \
188
227
ret = strtolfn(val, 0, &l); \
189
228
if (ret == -EINVAL || ((type)l != l)) \
190
229
return -EINVAL; \
191
230
*((type *)kp->arg) = l; \
194
int param_get_##name(char *buffer, struct kernel_param *kp) \
233
int param_get_##name(char *buffer, const struct kernel_param *kp) \
196
235
return sprintf(buffer, format, *((type *)kp->arg)); \
237
struct kernel_param_ops param_ops_##name = { \
238
.set = param_set_##name, \
239
.get = param_get_##name, \
241
EXPORT_SYMBOL(param_set_##name); \
242
EXPORT_SYMBOL(param_get_##name); \
243
EXPORT_SYMBOL(param_ops_##name)
199
246
STANDARD_PARAM_DEF(byte, unsigned char, "%c", unsigned long, strict_strtoul);
200
247
STANDARD_PARAM_DEF(short, short, "%hi", long, strict_strtol);
204
251
STANDARD_PARAM_DEF(long, long, "%li", long, strict_strtol);
205
252
STANDARD_PARAM_DEF(ulong, unsigned long, "%lu", unsigned long, strict_strtoul);
207
int param_set_charp(const char *val, struct kernel_param *kp)
254
int param_set_charp(const char *val, const struct kernel_param *kp)
210
printk(KERN_ERR "%s: string parameter expected\n",
215
256
if (strlen(val) > 1024) {
216
257
printk(KERN_ERR "%s: string parameter too long\n",
221
/* This is a hack. We can't need to strdup in early boot, and we
262
maybe_kfree_parameter(*(char **)kp->arg);
264
/* This is a hack. We can't kmalloc in early boot, and we
222
265
* don't need to; this mangled commandline is preserved. */
223
266
if (slab_is_available()) {
224
*(char **)kp->arg = kstrdup(val, GFP_KERNEL);
267
*(char **)kp->arg = kmalloc_parameter(strlen(val)+1);
225
268
if (!*(char **)kp->arg)
270
strcpy(*(char **)kp->arg, val);
228
272
*(const char **)kp->arg = val;
276
EXPORT_SYMBOL(param_set_charp);
233
int param_get_charp(char *buffer, struct kernel_param *kp)
278
int param_get_charp(char *buffer, const struct kernel_param *kp)
235
280
return sprintf(buffer, "%s", *((char **)kp->arg));
282
EXPORT_SYMBOL(param_get_charp);
284
static void param_free_charp(void *arg)
286
maybe_kfree_parameter(*((char **)arg));
289
struct kernel_param_ops param_ops_charp = {
290
.set = param_set_charp,
291
.get = param_get_charp,
292
.free = param_free_charp,
294
EXPORT_SYMBOL(param_ops_charp);
238
296
/* Actually could be a bool or an int, for historical reasons. */
239
int param_set_bool(const char *val, struct kernel_param *kp)
297
int param_set_bool(const char *val, const struct kernel_param *kp)
261
319
*(int *)kp->arg = v;
322
EXPORT_SYMBOL(param_set_bool);
265
int param_get_bool(char *buffer, struct kernel_param *kp)
324
int param_get_bool(char *buffer, const struct kernel_param *kp)
268
327
if (kp->flags & KPARAM_ISBOOL)
273
332
/* Y and N chosen as being relatively non-coder friendly */
274
333
return sprintf(buffer, "%c", val ? 'Y' : 'N');
335
EXPORT_SYMBOL(param_get_bool);
337
struct kernel_param_ops param_ops_bool = {
338
.set = param_set_bool,
339
.get = param_get_bool,
341
EXPORT_SYMBOL(param_ops_bool);
277
343
/* This one must be bool. */
278
int param_set_invbool(const char *val, struct kernel_param *kp)
344
int param_set_invbool(const char *val, const struct kernel_param *kp)
288
354
*(bool *)kp->arg = !boolval;
357
EXPORT_SYMBOL(param_set_invbool);
292
int param_get_invbool(char *buffer, struct kernel_param *kp)
359
int param_get_invbool(char *buffer, const struct kernel_param *kp)
294
361
return sprintf(buffer, "%c", (*(bool *)kp->arg) ? 'N' : 'Y');
363
EXPORT_SYMBOL(param_get_invbool);
365
struct kernel_param_ops param_ops_invbool = {
366
.set = param_set_invbool,
367
.get = param_get_invbool,
369
EXPORT_SYMBOL(param_ops_invbool);
297
371
/* We break the rule and mangle the string. */
298
372
static int param_array(const char *name,
300
374
unsigned int min, unsigned int max,
301
375
void *elem, int elemsize,
302
int (*set)(const char *, struct kernel_param *kp),
376
int (*set)(const char *, const struct kernel_param *kp),
304
378
unsigned int *num)
353
int param_array_set(const char *val, struct kernel_param *kp)
422
static int param_array_set(const char *val, const struct kernel_param *kp)
355
424
const struct kparam_array *arr = kp->arr;
356
425
unsigned int temp_num;
358
427
return param_array(kp->name, val, 1, arr->max, arr->elem,
359
arr->elemsize, arr->set, kp->flags,
428
arr->elemsize, arr->ops->set, kp->flags,
360
429
arr->num ?: &temp_num);
363
int param_array_get(char *buffer, struct kernel_param *kp)
432
static int param_array_get(char *buffer, const struct kernel_param *kp)
366
435
const struct kparam_array *arr = kp->arr;
372
441
buffer[off++] = ',';
373
442
p.arg = arr->elem + arr->elemsize * i;
374
ret = arr->get(buffer + off, &p);
443
BUG_ON(!mutex_is_locked(¶m_lock));
444
ret = arr->ops->get(buffer + off, &p);
383
int param_set_copystring(const char *val, struct kernel_param *kp)
453
static void param_array_free(void *arg)
456
const struct kparam_array *arr = arg;
459
for (i = 0; i < (arr->num ? *arr->num : arr->max); i++)
460
arr->ops->free(arr->elem + arr->elemsize * i);
463
struct kernel_param_ops param_array_ops = {
464
.set = param_array_set,
465
.get = param_array_get,
466
.free = param_array_free,
468
EXPORT_SYMBOL(param_array_ops);
470
int param_set_copystring(const char *val, const struct kernel_param *kp)
385
472
const struct kparam_string *kps = kp->str;
388
printk(KERN_ERR "%s: missing param set value\n", kp->name);
391
474
if (strlen(val)+1 > kps->maxlen) {
392
475
printk(KERN_ERR "%s: string doesn't fit in %u chars.\n",
393
476
kp->name, kps->maxlen-1);
396
479
strcpy(kps->string, val);
482
EXPORT_SYMBOL(param_set_copystring);
400
int param_get_string(char *buffer, struct kernel_param *kp)
484
int param_get_string(char *buffer, const struct kernel_param *kp)
402
486
const struct kparam_string *kps = kp->str;
403
487
return strlcpy(buffer, kps->string, kps->maxlen);
489
EXPORT_SYMBOL(param_get_string);
491
struct kernel_param_ops param_ops_string = {
492
.set = param_set_copystring,
493
.get = param_get_string,
495
EXPORT_SYMBOL(param_ops_string);
406
497
/* sysfs output in /sys/modules/XYZ/parameters/ */
407
#define to_module_attr(n) container_of(n, struct module_attribute, attr);
408
#define to_module_kobject(n) container_of(n, struct module_kobject, kobj);
498
#define to_module_attr(n) container_of(n, struct module_attribute, attr)
499
#define to_module_kobject(n) container_of(n, struct module_kobject, kobj)
410
501
extern struct kernel_param __start___param[], __stop___param[];
412
503
struct param_attribute
414
505
struct module_attribute mattr;
415
struct kernel_param *param;
506
const struct kernel_param *param;
418
509
struct module_param_attrs
425
516
#ifdef CONFIG_SYSFS
426
#define to_param_attr(n) container_of(n, struct param_attribute, mattr);
517
#define to_param_attr(n) container_of(n, struct param_attribute, mattr)
428
519
static ssize_t param_attr_show(struct module_attribute *mattr,
429
520
struct module *mod, char *buf)
432
523
struct param_attribute *attribute = to_param_attr(mattr);
434
if (!attribute->param->get)
525
if (!attribute->param->ops->get)
437
count = attribute->param->get(buf, attribute->param);
528
mutex_lock(¶m_lock);
529
count = attribute->param->ops->get(buf, attribute->param);
530
mutex_unlock(¶m_lock);
439
532
strcat(buf, "\n");
451
544
struct param_attribute *attribute = to_param_attr(mattr);
453
if (!attribute->param->set)
546
if (!attribute->param->ops->set)
456
err = attribute->param->set(buf, attribute->param);
549
mutex_lock(¶m_lock);
550
err = attribute->param->ops->set(buf, attribute->param);
551
mutex_unlock(¶m_lock);
469
564
#ifdef CONFIG_SYSFS
565
void __kernel_param_lock(void)
567
mutex_lock(¶m_lock);
569
EXPORT_SYMBOL(__kernel_param_lock);
571
void __kernel_param_unlock(void)
573
mutex_unlock(¶m_lock);
575
EXPORT_SYMBOL(__kernel_param_unlock);
471
578
* add_sysfs_param - add a parameter to sysfs
472
579
* @mk: struct module_kobject
519
626
new->grp.attrs = attrs;
521
628
/* Tack new one on the end. */
629
sysfs_attr_init(&new->attrs[num].mattr.attr);
522
630
new->attrs[num].param = kp;
523
631
new->attrs[num].mattr.show = param_attr_show;
524
632
new->attrs[num].mattr.store = param_attr_store;
559
667
* /sys/module/[mod->name]/parameters/
561
669
int module_param_sysfs_setup(struct module *mod,
562
struct kernel_param *kparam,
670
const struct kernel_param *kparam,
563
671
unsigned int num_params)
605
713
void destroy_params(const struct kernel_param *params, unsigned num)
607
/* FIXME: This should free kmalloced charp parameters. It doesn't. */
717
for (i = 0; i < num; i++)
718
if (params[i].ops->free)
719
params[i].ops->free(params[i].arg);
610
static void __init kernel_add_sysfs_param(const char *name,
611
struct kernel_param *kparam,
612
unsigned int name_skip)
722
static struct module_kobject * __init locate_module_kobject(const char *name)
614
724
struct module_kobject *mk;
615
725
struct kobject *kobj;
618
728
kobj = kset_find_obj(module_kset, name);
620
/* We already have one. Remove params so we can add more. */
621
730
mk = to_module_kobject(kobj);
622
/* We need to remove it before adding parameters. */
623
sysfs_remove_group(&mk->kobj, &mk->mp->grp);
625
732
mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL);
633
740
kobject_put(&mk->kobj);
634
printk(KERN_ERR "Module '%s' failed add to sysfs, "
635
"error number %d\n", name, err);
636
printk(KERN_ERR "The system will be unstable now.\n");
742
"Module '%s' failed add to sysfs, error number %d\n",
745
"The system will be unstable now.\n");
639
/* So that exit path is even. */
749
/* So that we hold reference in both cases. */
640
750
kobject_get(&mk->kobj);
756
static void __init kernel_add_sysfs_param(const char *name,
757
struct kernel_param *kparam,
758
unsigned int name_skip)
760
struct module_kobject *mk;
763
mk = locate_module_kobject(name);
767
/* We need to remove old parameters before adding more. */
769
sysfs_remove_group(&mk->kobj, &mk->mp->grp);
643
771
/* These should not fail at boot. */
644
772
err = add_sysfs_param(mk, kparam, kparam->name + name_skip);
815
ssize_t __modver_version_show(struct module_attribute *mattr,
816
struct module *mod, char *buf)
818
struct module_version_attribute *vattr =
819
container_of(mattr, struct module_version_attribute, mattr);
821
return sprintf(buf, "%s\n", vattr->version);
824
extern struct module_version_attribute __start___modver[], __stop___modver[];
826
static void __init version_sysfs_builtin(void)
828
const struct module_version_attribute *vattr;
829
struct module_kobject *mk;
832
for (vattr = __start___modver; vattr < __stop___modver; vattr++) {
833
mk = locate_module_kobject(vattr->module_name);
835
err = sysfs_create_file(&mk->kobj, &vattr->mattr.attr);
836
kobject_uevent(&mk->kobj, KOBJ_ADD);
837
kobject_put(&mk->kobj);
688
842
/* module-related sysfs stuff */
728
static struct sysfs_ops module_sysfs_ops = {
882
static const struct sysfs_ops module_sysfs_ops = {
729
883
.show = module_attr_show,
730
884
.store = module_attr_store,
770
925
subsys_initcall(param_sysfs_init);
772
927
#endif /* CONFIG_SYSFS */
774
EXPORT_SYMBOL(param_set_byte);
775
EXPORT_SYMBOL(param_get_byte);
776
EXPORT_SYMBOL(param_set_short);
777
EXPORT_SYMBOL(param_get_short);
778
EXPORT_SYMBOL(param_set_ushort);
779
EXPORT_SYMBOL(param_get_ushort);
780
EXPORT_SYMBOL(param_set_int);
781
EXPORT_SYMBOL(param_get_int);
782
EXPORT_SYMBOL(param_set_uint);
783
EXPORT_SYMBOL(param_get_uint);
784
EXPORT_SYMBOL(param_set_long);
785
EXPORT_SYMBOL(param_get_long);
786
EXPORT_SYMBOL(param_set_ulong);
787
EXPORT_SYMBOL(param_get_ulong);
788
EXPORT_SYMBOL(param_set_charp);
789
EXPORT_SYMBOL(param_get_charp);
790
EXPORT_SYMBOL(param_set_bool);
791
EXPORT_SYMBOL(param_get_bool);
792
EXPORT_SYMBOL(param_set_invbool);
793
EXPORT_SYMBOL(param_get_invbool);
794
EXPORT_SYMBOL(param_array_set);
795
EXPORT_SYMBOL(param_array_get);
796
EXPORT_SYMBOL(param_set_copystring);
797
EXPORT_SYMBOL(param_get_string);