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