Use dentry_path() to create full path to inode object
[pohmelfs.git] / kernel / params.c
blob4bc965d8a1fe8d66b66b7e7e220c08104084205a
1 /* Helpers for initial module or kernel cmdline parsing
2 Copyright (C) 2001 Rusty Russell.
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.
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.
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
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/module.h>
23 #include <linux/device.h>
24 #include <linux/err.h>
25 #include <linux/slab.h>
26 #include <linux/ctype.h>
28 /* Protects all parameters, and incidentally kmalloced_param list. */
29 static DEFINE_MUTEX(param_lock);
31 /* This just allows us to keep track of which parameters are kmalloced. */
32 struct kmalloced_param {
33 struct list_head list;
34 char val[];
36 static LIST_HEAD(kmalloced_params);
38 static void *kmalloc_parameter(unsigned int size)
40 struct kmalloced_param *p;
42 p = kmalloc(sizeof(*p) + size, GFP_KERNEL);
43 if (!p)
44 return NULL;
46 list_add(&p->list, &kmalloced_params);
47 return p->val;
50 /* Does nothing if parameter wasn't kmalloced above. */
51 static void maybe_kfree_parameter(void *param)
53 struct kmalloced_param *p;
55 list_for_each_entry(p, &kmalloced_params, list) {
56 if (p->val == param) {
57 list_del(&p->list);
58 kfree(p);
59 break;
64 static char dash2underscore(char c)
66 if (c == '-')
67 return '_';
68 return c;
71 bool parameqn(const char *a, const char *b, size_t n)
73 size_t i;
75 for (i = 0; i < n; i++) {
76 if (dash2underscore(a[i]) != dash2underscore(b[i]))
77 return false;
79 return true;
82 bool parameq(const char *a, const char *b)
84 return parameqn(a, b, strlen(a)+1);
87 static int parse_one(char *param,
88 char *val,
89 const struct kernel_param *params,
90 unsigned num_params,
91 int (*handle_unknown)(char *param, char *val))
93 unsigned int i;
94 int err;
96 /* Find parameter */
97 for (i = 0; i < num_params; i++) {
98 if (parameq(param, params[i].name)) {
99 /* No one handled NULL, so do it here. */
100 if (!val && params[i].ops->set != param_set_bool
101 && params[i].ops->set != param_set_bint)
102 return -EINVAL;
103 pr_debug("They are equal! Calling %p\n",
104 params[i].ops->set);
105 mutex_lock(&param_lock);
106 err = params[i].ops->set(val, &params[i]);
107 mutex_unlock(&param_lock);
108 return err;
112 if (handle_unknown) {
113 pr_debug("Unknown argument: calling %p\n", handle_unknown);
114 return handle_unknown(param, val);
117 pr_debug("Unknown argument `%s'\n", param);
118 return -ENOENT;
121 /* You can use " around spaces, but can't escape ". */
122 /* Hyphens and underscores equivalent in parameter names. */
123 static char *next_arg(char *args, char **param, char **val)
125 unsigned int i, equals = 0;
126 int in_quote = 0, quoted = 0;
127 char *next;
129 if (*args == '"') {
130 args++;
131 in_quote = 1;
132 quoted = 1;
135 for (i = 0; args[i]; i++) {
136 if (isspace(args[i]) && !in_quote)
137 break;
138 if (equals == 0) {
139 if (args[i] == '=')
140 equals = i;
142 if (args[i] == '"')
143 in_quote = !in_quote;
146 *param = args;
147 if (!equals)
148 *val = NULL;
149 else {
150 args[equals] = '\0';
151 *val = args + equals + 1;
153 /* Don't include quotes in value. */
154 if (**val == '"') {
155 (*val)++;
156 if (args[i-1] == '"')
157 args[i-1] = '\0';
159 if (quoted && args[i-1] == '"')
160 args[i-1] = '\0';
163 if (args[i]) {
164 args[i] = '\0';
165 next = args + i + 1;
166 } else
167 next = args + i;
169 /* Chew up trailing spaces. */
170 return skip_spaces(next);
173 /* Args looks like "foo=bar,bar2 baz=fuz wiz". */
174 int parse_args(const char *name,
175 char *args,
176 const struct kernel_param *params,
177 unsigned num,
178 int (*unknown)(char *param, char *val))
180 char *param, *val;
182 pr_debug("Parsing ARGS: %s\n", args);
184 /* Chew leading spaces */
185 args = skip_spaces(args);
187 while (*args) {
188 int ret;
189 int irq_was_disabled;
191 args = next_arg(args, &param, &val);
192 irq_was_disabled = irqs_disabled();
193 ret = parse_one(param, val, params, num, unknown);
194 if (irq_was_disabled && !irqs_disabled()) {
195 printk(KERN_WARNING "parse_args(): option '%s' enabled "
196 "irq's!\n", param);
198 switch (ret) {
199 case -ENOENT:
200 printk(KERN_ERR "%s: Unknown parameter `%s'\n",
201 name, param);
202 return ret;
203 case -ENOSPC:
204 printk(KERN_ERR
205 "%s: `%s' too large for parameter `%s'\n",
206 name, val ?: "", param);
207 return ret;
208 case 0:
209 break;
210 default:
211 printk(KERN_ERR
212 "%s: `%s' invalid for parameter `%s'\n",
213 name, val ?: "", param);
214 return ret;
218 /* All parsed OK. */
219 return 0;
222 /* Lazy bastard, eh? */
223 #define STANDARD_PARAM_DEF(name, type, format, tmptype, strtolfn) \
224 int param_set_##name(const char *val, const struct kernel_param *kp) \
226 tmptype l; \
227 int ret; \
229 ret = strtolfn(val, 0, &l); \
230 if (ret < 0 || ((type)l != l)) \
231 return ret < 0 ? ret : -EINVAL; \
232 *((type *)kp->arg) = l; \
233 return 0; \
235 int param_get_##name(char *buffer, const struct kernel_param *kp) \
237 return sprintf(buffer, format, *((type *)kp->arg)); \
239 struct kernel_param_ops param_ops_##name = { \
240 .set = param_set_##name, \
241 .get = param_get_##name, \
242 }; \
243 EXPORT_SYMBOL(param_set_##name); \
244 EXPORT_SYMBOL(param_get_##name); \
245 EXPORT_SYMBOL(param_ops_##name)
248 STANDARD_PARAM_DEF(byte, unsigned char, "%c", unsigned long, strict_strtoul);
249 STANDARD_PARAM_DEF(short, short, "%hi", long, strict_strtol);
250 STANDARD_PARAM_DEF(ushort, unsigned short, "%hu", unsigned long, strict_strtoul);
251 STANDARD_PARAM_DEF(int, int, "%i", long, strict_strtol);
252 STANDARD_PARAM_DEF(uint, unsigned int, "%u", unsigned long, strict_strtoul);
253 STANDARD_PARAM_DEF(long, long, "%li", long, strict_strtol);
254 STANDARD_PARAM_DEF(ulong, unsigned long, "%lu", unsigned long, strict_strtoul);
256 int param_set_charp(const char *val, const struct kernel_param *kp)
258 if (strlen(val) > 1024) {
259 printk(KERN_ERR "%s: string parameter too long\n",
260 kp->name);
261 return -ENOSPC;
264 maybe_kfree_parameter(*(char **)kp->arg);
266 /* This is a hack. We can't kmalloc in early boot, and we
267 * don't need to; this mangled commandline is preserved. */
268 if (slab_is_available()) {
269 *(char **)kp->arg = kmalloc_parameter(strlen(val)+1);
270 if (!*(char **)kp->arg)
271 return -ENOMEM;
272 strcpy(*(char **)kp->arg, val);
273 } else
274 *(const char **)kp->arg = val;
276 return 0;
278 EXPORT_SYMBOL(param_set_charp);
280 int param_get_charp(char *buffer, const struct kernel_param *kp)
282 return sprintf(buffer, "%s", *((char **)kp->arg));
284 EXPORT_SYMBOL(param_get_charp);
286 static void param_free_charp(void *arg)
288 maybe_kfree_parameter(*((char **)arg));
291 struct kernel_param_ops param_ops_charp = {
292 .set = param_set_charp,
293 .get = param_get_charp,
294 .free = param_free_charp,
296 EXPORT_SYMBOL(param_ops_charp);
298 /* Actually could be a bool or an int, for historical reasons. */
299 int param_set_bool(const char *val, const struct kernel_param *kp)
301 bool v;
302 int ret;
304 /* No equals means "set"... */
305 if (!val) val = "1";
307 /* One of =[yYnN01] */
308 ret = strtobool(val, &v);
309 if (ret)
310 return ret;
312 if (kp->flags & KPARAM_ISBOOL)
313 *(bool *)kp->arg = v;
314 else
315 *(int *)kp->arg = v;
316 return 0;
318 EXPORT_SYMBOL(param_set_bool);
320 int param_get_bool(char *buffer, const struct kernel_param *kp)
322 bool val;
323 if (kp->flags & KPARAM_ISBOOL)
324 val = *(bool *)kp->arg;
325 else
326 val = *(int *)kp->arg;
328 /* Y and N chosen as being relatively non-coder friendly */
329 return sprintf(buffer, "%c", val ? 'Y' : 'N');
331 EXPORT_SYMBOL(param_get_bool);
333 struct kernel_param_ops param_ops_bool = {
334 .set = param_set_bool,
335 .get = param_get_bool,
337 EXPORT_SYMBOL(param_ops_bool);
339 /* This one must be bool. */
340 int param_set_invbool(const char *val, const struct kernel_param *kp)
342 int ret;
343 bool boolval;
344 struct kernel_param dummy;
346 dummy.arg = &boolval;
347 dummy.flags = KPARAM_ISBOOL;
348 ret = param_set_bool(val, &dummy);
349 if (ret == 0)
350 *(bool *)kp->arg = !boolval;
351 return ret;
353 EXPORT_SYMBOL(param_set_invbool);
355 int param_get_invbool(char *buffer, const struct kernel_param *kp)
357 return sprintf(buffer, "%c", (*(bool *)kp->arg) ? 'N' : 'Y');
359 EXPORT_SYMBOL(param_get_invbool);
361 struct kernel_param_ops param_ops_invbool = {
362 .set = param_set_invbool,
363 .get = param_get_invbool,
365 EXPORT_SYMBOL(param_ops_invbool);
367 int param_set_bint(const char *val, const struct kernel_param *kp)
369 struct kernel_param boolkp;
370 bool v;
371 int ret;
373 /* Match bool exactly, by re-using it. */
374 boolkp = *kp;
375 boolkp.arg = &v;
376 boolkp.flags |= KPARAM_ISBOOL;
378 ret = param_set_bool(val, &boolkp);
379 if (ret == 0)
380 *(int *)kp->arg = v;
381 return ret;
383 EXPORT_SYMBOL(param_set_bint);
385 struct kernel_param_ops param_ops_bint = {
386 .set = param_set_bint,
387 .get = param_get_int,
389 EXPORT_SYMBOL(param_ops_bint);
391 /* We break the rule and mangle the string. */
392 static int param_array(const char *name,
393 const char *val,
394 unsigned int min, unsigned int max,
395 void *elem, int elemsize,
396 int (*set)(const char *, const struct kernel_param *kp),
397 u16 flags,
398 unsigned int *num)
400 int ret;
401 struct kernel_param kp;
402 char save;
404 /* Get the name right for errors. */
405 kp.name = name;
406 kp.arg = elem;
407 kp.flags = flags;
409 *num = 0;
410 /* We expect a comma-separated list of values. */
411 do {
412 int len;
414 if (*num == max) {
415 printk(KERN_ERR "%s: can only take %i arguments\n",
416 name, max);
417 return -EINVAL;
419 len = strcspn(val, ",");
421 /* nul-terminate and parse */
422 save = val[len];
423 ((char *)val)[len] = '\0';
424 BUG_ON(!mutex_is_locked(&param_lock));
425 ret = set(val, &kp);
427 if (ret != 0)
428 return ret;
429 kp.arg += elemsize;
430 val += len+1;
431 (*num)++;
432 } while (save == ',');
434 if (*num < min) {
435 printk(KERN_ERR "%s: needs at least %i arguments\n",
436 name, min);
437 return -EINVAL;
439 return 0;
442 static int param_array_set(const char *val, const struct kernel_param *kp)
444 const struct kparam_array *arr = kp->arr;
445 unsigned int temp_num;
447 return param_array(kp->name, val, 1, arr->max, arr->elem,
448 arr->elemsize, arr->ops->set, kp->flags,
449 arr->num ?: &temp_num);
452 static int param_array_get(char *buffer, const struct kernel_param *kp)
454 int i, off, ret;
455 const struct kparam_array *arr = kp->arr;
456 struct kernel_param p;
458 p = *kp;
459 for (i = off = 0; i < (arr->num ? *arr->num : arr->max); i++) {
460 if (i)
461 buffer[off++] = ',';
462 p.arg = arr->elem + arr->elemsize * i;
463 BUG_ON(!mutex_is_locked(&param_lock));
464 ret = arr->ops->get(buffer + off, &p);
465 if (ret < 0)
466 return ret;
467 off += ret;
469 buffer[off] = '\0';
470 return off;
473 static void param_array_free(void *arg)
475 unsigned int i;
476 const struct kparam_array *arr = arg;
478 if (arr->ops->free)
479 for (i = 0; i < (arr->num ? *arr->num : arr->max); i++)
480 arr->ops->free(arr->elem + arr->elemsize * i);
483 struct kernel_param_ops param_array_ops = {
484 .set = param_array_set,
485 .get = param_array_get,
486 .free = param_array_free,
488 EXPORT_SYMBOL(param_array_ops);
490 int param_set_copystring(const char *val, const struct kernel_param *kp)
492 const struct kparam_string *kps = kp->str;
494 if (strlen(val)+1 > kps->maxlen) {
495 printk(KERN_ERR "%s: string doesn't fit in %u chars.\n",
496 kp->name, kps->maxlen-1);
497 return -ENOSPC;
499 strcpy(kps->string, val);
500 return 0;
502 EXPORT_SYMBOL(param_set_copystring);
504 int param_get_string(char *buffer, const struct kernel_param *kp)
506 const struct kparam_string *kps = kp->str;
507 return strlcpy(buffer, kps->string, kps->maxlen);
509 EXPORT_SYMBOL(param_get_string);
511 struct kernel_param_ops param_ops_string = {
512 .set = param_set_copystring,
513 .get = param_get_string,
515 EXPORT_SYMBOL(param_ops_string);
517 /* sysfs output in /sys/modules/XYZ/parameters/ */
518 #define to_module_attr(n) container_of(n, struct module_attribute, attr)
519 #define to_module_kobject(n) container_of(n, struct module_kobject, kobj)
521 extern struct kernel_param __start___param[], __stop___param[];
523 struct param_attribute
525 struct module_attribute mattr;
526 const struct kernel_param *param;
529 struct module_param_attrs
531 unsigned int num;
532 struct attribute_group grp;
533 struct param_attribute attrs[0];
536 #ifdef CONFIG_SYSFS
537 #define to_param_attr(n) container_of(n, struct param_attribute, mattr)
539 static ssize_t param_attr_show(struct module_attribute *mattr,
540 struct module_kobject *mk, char *buf)
542 int count;
543 struct param_attribute *attribute = to_param_attr(mattr);
545 if (!attribute->param->ops->get)
546 return -EPERM;
548 mutex_lock(&param_lock);
549 count = attribute->param->ops->get(buf, attribute->param);
550 mutex_unlock(&param_lock);
551 if (count > 0) {
552 strcat(buf, "\n");
553 ++count;
555 return count;
558 /* sysfs always hands a nul-terminated string in buf. We rely on that. */
559 static ssize_t param_attr_store(struct module_attribute *mattr,
560 struct module_kobject *km,
561 const char *buf, size_t len)
563 int err;
564 struct param_attribute *attribute = to_param_attr(mattr);
566 if (!attribute->param->ops->set)
567 return -EPERM;
569 mutex_lock(&param_lock);
570 err = attribute->param->ops->set(buf, attribute->param);
571 mutex_unlock(&param_lock);
572 if (!err)
573 return len;
574 return err;
576 #endif
578 #ifdef CONFIG_MODULES
579 #define __modinit
580 #else
581 #define __modinit __init
582 #endif
584 #ifdef CONFIG_SYSFS
585 void __kernel_param_lock(void)
587 mutex_lock(&param_lock);
589 EXPORT_SYMBOL(__kernel_param_lock);
591 void __kernel_param_unlock(void)
593 mutex_unlock(&param_lock);
595 EXPORT_SYMBOL(__kernel_param_unlock);
598 * add_sysfs_param - add a parameter to sysfs
599 * @mk: struct module_kobject
600 * @kparam: the actual parameter definition to add to sysfs
601 * @name: name of parameter
603 * Create a kobject if for a (per-module) parameter if mp NULL, and
604 * create file in sysfs. Returns an error on out of memory. Always cleans up
605 * if there's an error.
607 static __modinit int add_sysfs_param(struct module_kobject *mk,
608 const struct kernel_param *kp,
609 const char *name)
611 struct module_param_attrs *new;
612 struct attribute **attrs;
613 int err, num;
615 /* We don't bother calling this with invisible parameters. */
616 BUG_ON(!kp->perm);
618 if (!mk->mp) {
619 num = 0;
620 attrs = NULL;
621 } else {
622 num = mk->mp->num;
623 attrs = mk->mp->grp.attrs;
626 /* Enlarge. */
627 new = krealloc(mk->mp,
628 sizeof(*mk->mp) + sizeof(mk->mp->attrs[0]) * (num+1),
629 GFP_KERNEL);
630 if (!new) {
631 kfree(mk->mp);
632 err = -ENOMEM;
633 goto fail;
635 attrs = krealloc(attrs, sizeof(new->grp.attrs[0])*(num+2), GFP_KERNEL);
636 if (!attrs) {
637 err = -ENOMEM;
638 goto fail_free_new;
641 /* Sysfs wants everything zeroed. */
642 memset(new, 0, sizeof(*new));
643 memset(&new->attrs[num], 0, sizeof(new->attrs[num]));
644 memset(&attrs[num], 0, sizeof(attrs[num]));
645 new->grp.name = "parameters";
646 new->grp.attrs = attrs;
648 /* Tack new one on the end. */
649 sysfs_attr_init(&new->attrs[num].mattr.attr);
650 new->attrs[num].param = kp;
651 new->attrs[num].mattr.show = param_attr_show;
652 new->attrs[num].mattr.store = param_attr_store;
653 new->attrs[num].mattr.attr.name = (char *)name;
654 new->attrs[num].mattr.attr.mode = kp->perm;
655 new->num = num+1;
657 /* Fix up all the pointers, since krealloc can move us */
658 for (num = 0; num < new->num; num++)
659 new->grp.attrs[num] = &new->attrs[num].mattr.attr;
660 new->grp.attrs[num] = NULL;
662 mk->mp = new;
663 return 0;
665 fail_free_new:
666 kfree(new);
667 fail:
668 mk->mp = NULL;
669 return err;
672 #ifdef CONFIG_MODULES
673 static void free_module_param_attrs(struct module_kobject *mk)
675 kfree(mk->mp->grp.attrs);
676 kfree(mk->mp);
677 mk->mp = NULL;
681 * module_param_sysfs_setup - setup sysfs support for one module
682 * @mod: module
683 * @kparam: module parameters (array)
684 * @num_params: number of module parameters
686 * Adds sysfs entries for module parameters under
687 * /sys/module/[mod->name]/parameters/
689 int module_param_sysfs_setup(struct module *mod,
690 const struct kernel_param *kparam,
691 unsigned int num_params)
693 int i, err;
694 bool params = false;
696 for (i = 0; i < num_params; i++) {
697 if (kparam[i].perm == 0)
698 continue;
699 err = add_sysfs_param(&mod->mkobj, &kparam[i], kparam[i].name);
700 if (err)
701 return err;
702 params = true;
705 if (!params)
706 return 0;
708 /* Create the param group. */
709 err = sysfs_create_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);
710 if (err)
711 free_module_param_attrs(&mod->mkobj);
712 return err;
716 * module_param_sysfs_remove - remove sysfs support for one module
717 * @mod: module
719 * Remove sysfs entries for module parameters and the corresponding
720 * kobject.
722 void module_param_sysfs_remove(struct module *mod)
724 if (mod->mkobj.mp) {
725 sysfs_remove_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);
726 /* We are positive that no one is using any param
727 * attrs at this point. Deallocate immediately. */
728 free_module_param_attrs(&mod->mkobj);
731 #endif
733 void destroy_params(const struct kernel_param *params, unsigned num)
735 unsigned int i;
737 for (i = 0; i < num; i++)
738 if (params[i].ops->free)
739 params[i].ops->free(params[i].arg);
742 static struct module_kobject * __init locate_module_kobject(const char *name)
744 struct module_kobject *mk;
745 struct kobject *kobj;
746 int err;
748 kobj = kset_find_obj(module_kset, name);
749 if (kobj) {
750 mk = to_module_kobject(kobj);
751 } else {
752 mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL);
753 BUG_ON(!mk);
755 mk->mod = THIS_MODULE;
756 mk->kobj.kset = module_kset;
757 err = kobject_init_and_add(&mk->kobj, &module_ktype, NULL,
758 "%s", name);
759 #ifdef CONFIG_MODULES
760 if (!err)
761 err = sysfs_create_file(&mk->kobj, &module_uevent.attr);
762 #endif
763 if (err) {
764 kobject_put(&mk->kobj);
765 printk(KERN_ERR
766 "Module '%s' failed add to sysfs, error number %d\n",
767 name, err);
768 printk(KERN_ERR
769 "The system will be unstable now.\n");
770 return NULL;
773 /* So that we hold reference in both cases. */
774 kobject_get(&mk->kobj);
777 return mk;
780 static void __init kernel_add_sysfs_param(const char *name,
781 struct kernel_param *kparam,
782 unsigned int name_skip)
784 struct module_kobject *mk;
785 int err;
787 mk = locate_module_kobject(name);
788 if (!mk)
789 return;
791 /* We need to remove old parameters before adding more. */
792 if (mk->mp)
793 sysfs_remove_group(&mk->kobj, &mk->mp->grp);
795 /* These should not fail at boot. */
796 err = add_sysfs_param(mk, kparam, kparam->name + name_skip);
797 BUG_ON(err);
798 err = sysfs_create_group(&mk->kobj, &mk->mp->grp);
799 BUG_ON(err);
800 kobject_uevent(&mk->kobj, KOBJ_ADD);
801 kobject_put(&mk->kobj);
805 * param_sysfs_builtin - add contents in /sys/parameters for built-in modules
807 * Add module_parameters to sysfs for "modules" built into the kernel.
809 * The "module" name (KBUILD_MODNAME) is stored before a dot, the
810 * "parameter" name is stored behind a dot in kernel_param->name. So,
811 * extract the "module" name for all built-in kernel_param-eters,
812 * and for all who have the same, call kernel_add_sysfs_param.
814 static void __init param_sysfs_builtin(void)
816 struct kernel_param *kp;
817 unsigned int name_len;
818 char modname[MODULE_NAME_LEN];
820 for (kp = __start___param; kp < __stop___param; kp++) {
821 char *dot;
823 if (kp->perm == 0)
824 continue;
826 dot = strchr(kp->name, '.');
827 if (!dot) {
828 /* This happens for core_param() */
829 strcpy(modname, "kernel");
830 name_len = 0;
831 } else {
832 name_len = dot - kp->name + 1;
833 strlcpy(modname, kp->name, name_len);
835 kernel_add_sysfs_param(modname, kp, name_len);
839 ssize_t __modver_version_show(struct module_attribute *mattr,
840 struct module_kobject *mk, char *buf)
842 struct module_version_attribute *vattr =
843 container_of(mattr, struct module_version_attribute, mattr);
845 return sprintf(buf, "%s\n", vattr->version);
848 extern const struct module_version_attribute *__start___modver[];
849 extern const struct module_version_attribute *__stop___modver[];
851 static void __init version_sysfs_builtin(void)
853 const struct module_version_attribute **p;
854 struct module_kobject *mk;
855 int err;
857 for (p = __start___modver; p < __stop___modver; p++) {
858 const struct module_version_attribute *vattr = *p;
860 mk = locate_module_kobject(vattr->module_name);
861 if (mk) {
862 err = sysfs_create_file(&mk->kobj, &vattr->mattr.attr);
863 kobject_uevent(&mk->kobj, KOBJ_ADD);
864 kobject_put(&mk->kobj);
869 /* module-related sysfs stuff */
871 static ssize_t module_attr_show(struct kobject *kobj,
872 struct attribute *attr,
873 char *buf)
875 struct module_attribute *attribute;
876 struct module_kobject *mk;
877 int ret;
879 attribute = to_module_attr(attr);
880 mk = to_module_kobject(kobj);
882 if (!attribute->show)
883 return -EIO;
885 ret = attribute->show(attribute, mk, buf);
887 return ret;
890 static ssize_t module_attr_store(struct kobject *kobj,
891 struct attribute *attr,
892 const char *buf, size_t len)
894 struct module_attribute *attribute;
895 struct module_kobject *mk;
896 int ret;
898 attribute = to_module_attr(attr);
899 mk = to_module_kobject(kobj);
901 if (!attribute->store)
902 return -EIO;
904 ret = attribute->store(attribute, mk, buf, len);
906 return ret;
909 static const struct sysfs_ops module_sysfs_ops = {
910 .show = module_attr_show,
911 .store = module_attr_store,
914 static int uevent_filter(struct kset *kset, struct kobject *kobj)
916 struct kobj_type *ktype = get_ktype(kobj);
918 if (ktype == &module_ktype)
919 return 1;
920 return 0;
923 static const struct kset_uevent_ops module_uevent_ops = {
924 .filter = uevent_filter,
927 struct kset *module_kset;
928 int module_sysfs_initialized;
930 struct kobj_type module_ktype = {
931 .sysfs_ops = &module_sysfs_ops,
935 * param_sysfs_init - wrapper for built-in params support
937 static int __init param_sysfs_init(void)
939 module_kset = kset_create_and_add("module", &module_uevent_ops, NULL);
940 if (!module_kset) {
941 printk(KERN_WARNING "%s (%d): error creating kset\n",
942 __FILE__, __LINE__);
943 return -ENOMEM;
945 module_sysfs_initialized = 1;
947 version_sysfs_builtin();
948 param_sysfs_builtin();
950 return 0;
952 subsys_initcall(param_sysfs_init);
954 #endif /* CONFIG_SYSFS */