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