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