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