OSDN Git Service

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