Linux-2.6.12-rc2
[safe/jmp/linux-2.6] / 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/config.h>
19 #include <linux/moduleparam.h>
20 #include <linux/kernel.h>
21 #include <linux/string.h>
22 #include <linux/errno.h>
23 #include <linux/module.h>
24 #include <linux/device.h>
25 #include <linux/err.h>
26
27 #if 0
28 #define DEBUGP printk
29 #else
30 #define DEBUGP(fmt, a...)
31 #endif
32
33 static inline int dash2underscore(char c)
34 {
35         if (c == '-')
36                 return '_';
37         return c;
38 }
39
40 static inline int parameq(const char *input, const char *paramname)
41 {
42         unsigned int i;
43         for (i = 0; dash2underscore(input[i]) == paramname[i]; i++)
44                 if (input[i] == '\0')
45                         return 1;
46         return 0;
47 }
48
49 static int parse_one(char *param,
50                      char *val,
51                      struct kernel_param *params, 
52                      unsigned num_params,
53                      int (*handle_unknown)(char *param, char *val))
54 {
55         unsigned int i;
56
57         /* Find parameter */
58         for (i = 0; i < num_params; i++) {
59                 if (parameq(param, params[i].name)) {
60                         DEBUGP("They are equal!  Calling %p\n",
61                                params[i].set);
62                         return params[i].set(val, &params[i]);
63                 }
64         }
65
66         if (handle_unknown) {
67                 DEBUGP("Unknown argument: calling %p\n", handle_unknown);
68                 return handle_unknown(param, val);
69         }
70
71         DEBUGP("Unknown argument `%s'\n", param);
72         return -ENOENT;
73 }
74
75 /* You can use " around spaces, but can't escape ". */
76 /* Hyphens and underscores equivalent in parameter names. */
77 static char *next_arg(char *args, char **param, char **val)
78 {
79         unsigned int i, equals = 0;
80         int in_quote = 0, quoted = 0;
81         char *next;
82
83         /* Chew any extra spaces */
84         while (*args == ' ') args++;
85         if (*args == '"') {
86                 args++;
87                 in_quote = 1;
88                 quoted = 1;
89         }
90
91         for (i = 0; args[i]; i++) {
92                 if (args[i] == ' ' && !in_quote)
93                         break;
94                 if (equals == 0) {
95                         if (args[i] == '=')
96                                 equals = i;
97                 }
98                 if (args[i] == '"')
99                         in_quote = !in_quote;
100         }
101
102         *param = args;
103         if (!equals)
104                 *val = NULL;
105         else {
106                 args[equals] = '\0';
107                 *val = args + equals + 1;
108
109                 /* Don't include quotes in value. */
110                 if (**val == '"') {
111                         (*val)++;
112                         if (args[i-1] == '"')
113                                 args[i-1] = '\0';
114                 }
115                 if (quoted && args[i-1] == '"')
116                         args[i-1] = '\0';
117         }
118
119         if (args[i]) {
120                 args[i] = '\0';
121                 next = args + i + 1;
122         } else
123                 next = args + i;
124         return next;
125 }
126
127 /* Args looks like "foo=bar,bar2 baz=fuz wiz". */
128 int parse_args(const char *name,
129                char *args,
130                struct kernel_param *params,
131                unsigned num,
132                int (*unknown)(char *param, char *val))
133 {
134         char *param, *val;
135
136         DEBUGP("Parsing ARGS: %s\n", args);
137
138         while (*args) {
139                 int ret;
140
141                 args = next_arg(args, &param, &val);
142                 ret = parse_one(param, val, params, num, unknown);
143                 switch (ret) {
144                 case -ENOENT:
145                         printk(KERN_ERR "%s: Unknown parameter `%s'\n",
146                                name, param);
147                         return ret;
148                 case -ENOSPC:
149                         printk(KERN_ERR
150                                "%s: `%s' too large for parameter `%s'\n",
151                                name, val ?: "", param);
152                         return ret;
153                 case 0:
154                         break;
155                 default:
156                         printk(KERN_ERR
157                                "%s: `%s' invalid for parameter `%s'\n",
158                                name, val ?: "", param);
159                         return ret;
160                 }
161         }
162
163         /* All parsed OK. */
164         return 0;
165 }
166
167 /* Lazy bastard, eh? */
168 #define STANDARD_PARAM_DEF(name, type, format, tmptype, strtolfn)       \
169         int param_set_##name(const char *val, struct kernel_param *kp)  \
170         {                                                               \
171                 char *endp;                                             \
172                 tmptype l;                                              \
173                                                                         \
174                 if (!val) return -EINVAL;                               \
175                 l = strtolfn(val, &endp, 0);                            \
176                 if (endp == val || ((type)l != l))                      \
177                         return -EINVAL;                                 \
178                 *((type *)kp->arg) = l;                                 \
179                 return 0;                                               \
180         }                                                               \
181         int param_get_##name(char *buffer, struct kernel_param *kp)     \
182         {                                                               \
183                 return sprintf(buffer, format, *((type *)kp->arg));     \
184         }
185
186 STANDARD_PARAM_DEF(byte, unsigned char, "%c", unsigned long, simple_strtoul);
187 STANDARD_PARAM_DEF(short, short, "%hi", long, simple_strtol);
188 STANDARD_PARAM_DEF(ushort, unsigned short, "%hu", unsigned long, simple_strtoul);
189 STANDARD_PARAM_DEF(int, int, "%i", long, simple_strtol);
190 STANDARD_PARAM_DEF(uint, unsigned int, "%u", unsigned long, simple_strtoul);
191 STANDARD_PARAM_DEF(long, long, "%li", long, simple_strtol);
192 STANDARD_PARAM_DEF(ulong, unsigned long, "%lu", unsigned long, simple_strtoul);
193
194 int param_set_charp(const char *val, struct kernel_param *kp)
195 {
196         if (!val) {
197                 printk(KERN_ERR "%s: string parameter expected\n",
198                        kp->name);
199                 return -EINVAL;
200         }
201
202         if (strlen(val) > 1024) {
203                 printk(KERN_ERR "%s: string parameter too long\n",
204                        kp->name);
205                 return -ENOSPC;
206         }
207
208         *(char **)kp->arg = (char *)val;
209         return 0;
210 }
211
212 int param_get_charp(char *buffer, struct kernel_param *kp)
213 {
214         return sprintf(buffer, "%s", *((char **)kp->arg));
215 }
216
217 int param_set_bool(const char *val, struct kernel_param *kp)
218 {
219         /* No equals means "set"... */
220         if (!val) val = "1";
221
222         /* One of =[yYnN01] */
223         switch (val[0]) {
224         case 'y': case 'Y': case '1':
225                 *(int *)kp->arg = 1;
226                 return 0;
227         case 'n': case 'N': case '0':
228                 *(int *)kp->arg = 0;
229                 return 0;
230         }
231         return -EINVAL;
232 }
233
234 int param_get_bool(char *buffer, struct kernel_param *kp)
235 {
236         /* Y and N chosen as being relatively non-coder friendly */
237         return sprintf(buffer, "%c", (*(int *)kp->arg) ? 'Y' : 'N');
238 }
239
240 int param_set_invbool(const char *val, struct kernel_param *kp)
241 {
242         int boolval, ret;
243         struct kernel_param dummy = { .arg = &boolval };
244
245         ret = param_set_bool(val, &dummy);
246         if (ret == 0)
247                 *(int *)kp->arg = !boolval;
248         return ret;
249 }
250
251 int param_get_invbool(char *buffer, struct kernel_param *kp)
252 {
253         int val;
254         struct kernel_param dummy = { .arg = &val };
255
256         val = !*(int *)kp->arg;
257         return param_get_bool(buffer, &dummy);
258 }
259
260 /* We cheat here and temporarily mangle the string. */
261 int param_array(const char *name,
262                 const char *val,
263                 unsigned int min, unsigned int max,
264                 void *elem, int elemsize,
265                 int (*set)(const char *, struct kernel_param *kp),
266                 int *num)
267 {
268         int ret;
269         struct kernel_param kp;
270         char save;
271
272         /* Get the name right for errors. */
273         kp.name = name;
274         kp.arg = elem;
275
276         /* No equals sign? */
277         if (!val) {
278                 printk(KERN_ERR "%s: expects arguments\n", name);
279                 return -EINVAL;
280         }
281
282         *num = 0;
283         /* We expect a comma-separated list of values. */
284         do {
285                 int len;
286
287                 if (*num == max) {
288                         printk(KERN_ERR "%s: can only take %i arguments\n",
289                                name, max);
290                         return -EINVAL;
291                 }
292                 len = strcspn(val, ",");
293
294                 /* nul-terminate and parse */
295                 save = val[len];
296                 ((char *)val)[len] = '\0';
297                 ret = set(val, &kp);
298
299                 if (ret != 0)
300                         return ret;
301                 kp.arg += elemsize;
302                 val += len+1;
303                 (*num)++;
304         } while (save == ',');
305
306         if (*num < min) {
307                 printk(KERN_ERR "%s: needs at least %i arguments\n",
308                        name, min);
309                 return -EINVAL;
310         }
311         return 0;
312 }
313
314 int param_array_set(const char *val, struct kernel_param *kp)
315 {
316         struct kparam_array *arr = kp->arg;
317
318         return param_array(kp->name, val, 1, arr->max, arr->elem,
319                            arr->elemsize, arr->set, arr->num ?: &arr->max);
320 }
321
322 int param_array_get(char *buffer, struct kernel_param *kp)
323 {
324         int i, off, ret;
325         struct kparam_array *arr = kp->arg;
326         struct kernel_param p;
327
328         p = *kp;
329         for (i = off = 0; i < (arr->num ? *arr->num : arr->max); i++) {
330                 if (i)
331                         buffer[off++] = ',';
332                 p.arg = arr->elem + arr->elemsize * i;
333                 ret = arr->get(buffer + off, &p);
334                 if (ret < 0)
335                         return ret;
336                 off += ret;
337         }
338         buffer[off] = '\0';
339         return off;
340 }
341
342 int param_set_copystring(const char *val, struct kernel_param *kp)
343 {
344         struct kparam_string *kps = kp->arg;
345
346         if (strlen(val)+1 > kps->maxlen) {
347                 printk(KERN_ERR "%s: string doesn't fit in %u chars.\n",
348                        kp->name, kps->maxlen-1);
349                 return -ENOSPC;
350         }
351         strcpy(kps->string, val);
352         return 0;
353 }
354
355 int param_get_string(char *buffer, struct kernel_param *kp)
356 {
357         struct kparam_string *kps = kp->arg;
358         return strlcpy(buffer, kps->string, kps->maxlen);
359 }
360
361 /* sysfs output in /sys/modules/XYZ/parameters/ */
362
363 extern struct kernel_param __start___param[], __stop___param[];
364
365 #define MAX_KBUILD_MODNAME KOBJ_NAME_LEN
366
367 struct param_attribute
368 {
369         struct module_attribute mattr;
370         struct kernel_param *param;
371 };
372
373 struct module_param_attrs
374 {
375         struct attribute_group grp;
376         struct param_attribute attrs[0];
377 };
378
379 #define to_param_attr(n) container_of(n, struct param_attribute, mattr);
380
381 static ssize_t param_attr_show(struct module_attribute *mattr,
382                                struct module *mod, char *buf)
383 {
384         int count;
385         struct param_attribute *attribute = to_param_attr(mattr);
386
387         if (!attribute->param->get)
388                 return -EPERM;
389
390         count = attribute->param->get(buf, attribute->param);
391         if (count > 0) {
392                 strcat(buf, "\n");
393                 ++count;
394         }
395         return count;
396 }
397
398 /* sysfs always hands a nul-terminated string in buf.  We rely on that. */
399 static ssize_t param_attr_store(struct module_attribute *mattr,
400                                 struct module *owner,
401                                 const char *buf, size_t len)
402 {
403         int err;
404         struct param_attribute *attribute = to_param_attr(mattr);
405
406         if (!attribute->param->set)
407                 return -EPERM;
408
409         err = attribute->param->set(buf, attribute->param);
410         if (!err)
411                 return len;
412         return err;
413 }
414
415 #ifdef CONFIG_MODULES
416 #define __modinit
417 #else
418 #define __modinit __init
419 #endif
420
421 /*
422  * param_sysfs_setup - setup sysfs support for one module or KBUILD_MODNAME
423  * @mk: struct module_kobject (contains parent kobject)
424  * @kparam: array of struct kernel_param, the actual parameter definitions
425  * @num_params: number of entries in array
426  * @name_skip: offset where the parameter name start in kparam[].name. Needed for built-in "modules"
427  *
428  * Create a kobject for a (per-module) group of parameters, and create files
429  * in sysfs. A pointer to the param_kobject is returned on success,
430  * NULL if there's no parameter to export, or other ERR_PTR(err).
431  */
432 static __modinit struct module_param_attrs *
433 param_sysfs_setup(struct module_kobject *mk,
434                   struct kernel_param *kparam,
435                   unsigned int num_params,
436                   unsigned int name_skip)
437 {
438         struct module_param_attrs *mp;
439         unsigned int valid_attrs = 0;
440         unsigned int i, size[2];
441         struct param_attribute *pattr;
442         struct attribute **gattr;
443         int err;
444
445         for (i=0; i<num_params; i++) {
446                 if (kparam[i].perm)
447                         valid_attrs++;
448         }
449
450         if (!valid_attrs)
451                 return NULL;
452
453         size[0] = ALIGN(sizeof(*mp) +
454                         valid_attrs * sizeof(mp->attrs[0]),
455                         sizeof(mp->grp.attrs[0]));
456         size[1] = (valid_attrs + 1) * sizeof(mp->grp.attrs[0]);
457
458         mp = kmalloc(size[0] + size[1], GFP_KERNEL);
459         if (!mp)
460                 return ERR_PTR(-ENOMEM);
461
462         mp->grp.name = "parameters";
463         mp->grp.attrs = (void *)mp + size[0];
464
465         pattr = &mp->attrs[0];
466         gattr = &mp->grp.attrs[0];
467         for (i = 0; i < num_params; i++) {
468                 struct kernel_param *kp = &kparam[i];
469                 if (kp->perm) {
470                         pattr->param = kp;
471                         pattr->mattr.show = param_attr_show;
472                         pattr->mattr.store = param_attr_store;
473                         pattr->mattr.attr.name = (char *)&kp->name[name_skip];
474                         pattr->mattr.attr.owner = mk->mod;
475                         pattr->mattr.attr.mode = kp->perm;
476                         *(gattr++) = &(pattr++)->mattr.attr;
477                 }
478         }
479         *gattr = NULL;
480
481         if ((err = sysfs_create_group(&mk->kobj, &mp->grp))) {
482                 kfree(mp);
483                 return ERR_PTR(err);
484         }
485         return mp;
486 }
487
488
489 #ifdef CONFIG_MODULES
490
491 /*
492  * module_param_sysfs_setup - setup sysfs support for one module
493  * @mod: module
494  * @kparam: module parameters (array)
495  * @num_params: number of module parameters
496  *
497  * Adds sysfs entries for module parameters, and creates a link from
498  * /sys/module/[mod->name]/parameters to /sys/parameters/[mod->name]/
499  */
500 int module_param_sysfs_setup(struct module *mod,
501                              struct kernel_param *kparam,
502                              unsigned int num_params)
503 {
504         struct module_param_attrs *mp;
505
506         mp = param_sysfs_setup(&mod->mkobj, kparam, num_params, 0);
507         if (IS_ERR(mp))
508                 return PTR_ERR(mp);
509
510         mod->param_attrs = mp;
511         return 0;
512 }
513
514 /*
515  * module_param_sysfs_remove - remove sysfs support for one module
516  * @mod: module
517  *
518  * Remove sysfs entries for module parameters and the corresponding
519  * kobject.
520  */
521 void module_param_sysfs_remove(struct module *mod)
522 {
523         if (mod->param_attrs) {
524                 sysfs_remove_group(&mod->mkobj.kobj,
525                                    &mod->param_attrs->grp);
526                 /* We are positive that no one is using any param
527                  * attrs at this point.  Deallocate immediately. */
528                 kfree(mod->param_attrs);
529                 mod->param_attrs = NULL;
530         }
531 }
532 #endif
533
534 /*
535  * kernel_param_sysfs_setup - wrapper for built-in params support
536  */
537 static void __init kernel_param_sysfs_setup(const char *name,
538                                             struct kernel_param *kparam,
539                                             unsigned int num_params,
540                                             unsigned int name_skip)
541 {
542         struct module_kobject *mk;
543
544         mk = kmalloc(sizeof(struct module_kobject), GFP_KERNEL);
545         memset(mk, 0, sizeof(struct module_kobject));
546
547         mk->mod = THIS_MODULE;
548         kobj_set_kset_s(mk, module_subsys);
549         kobject_set_name(&mk->kobj, name);
550         kobject_register(&mk->kobj);
551
552         /* no need to keep the kobject if no parameter is exported */
553         if (!param_sysfs_setup(mk, kparam, num_params, name_skip)) {
554                 kobject_unregister(&mk->kobj);
555                 kfree(mk);
556         }
557 }
558
559 /*
560  * param_sysfs_builtin - add contents in /sys/parameters for built-in modules
561  *
562  * Add module_parameters to sysfs for "modules" built into the kernel.
563  *
564  * The "module" name (KBUILD_MODNAME) is stored before a dot, the
565  * "parameter" name is stored behind a dot in kernel_param->name. So,
566  * extract the "module" name for all built-in kernel_param-eters,
567  * and for all who have the same, call kernel_param_sysfs_setup.
568  */
569 static void __init param_sysfs_builtin(void)
570 {
571         struct kernel_param *kp, *kp_begin = NULL;
572         unsigned int i, name_len, count = 0;
573         char modname[MAX_KBUILD_MODNAME + 1] = "";
574
575         for (i=0; i < __stop___param - __start___param; i++) {
576                 char *dot;
577
578                 kp = &__start___param[i];
579
580                 /* We do not handle args without periods. */
581                 dot = memchr(kp->name, '.', MAX_KBUILD_MODNAME);
582                 if (!dot) {
583                         DEBUGP("couldn't find period in %s\n", kp->name);
584                         continue;
585                 }
586                 name_len = dot - kp->name;
587
588                 /* new kbuild_modname? */
589                 if (strlen(modname) != name_len
590                     || strncmp(modname, kp->name, name_len) != 0) {
591                         /* add a new kobject for previous kernel_params. */
592                         if (count)
593                                 kernel_param_sysfs_setup(modname,
594                                                          kp_begin,
595                                                          count,
596                                                          strlen(modname)+1);
597
598                         strncpy(modname, kp->name, name_len);
599                         modname[name_len] = '\0';
600                         count = 0;
601                         kp_begin = kp;
602                 }
603                 count++;
604         }
605
606         /* last kernel_params need to be registered as well */
607         if (count)
608                 kernel_param_sysfs_setup(modname, kp_begin, count,
609                                          strlen(modname)+1);
610 }
611
612
613 /* module-related sysfs stuff */
614 #ifdef CONFIG_MODULES
615
616 #define to_module_attr(n) container_of(n, struct module_attribute, attr);
617 #define to_module_kobject(n) container_of(n, struct module_kobject, kobj);
618
619 static ssize_t module_attr_show(struct kobject *kobj,
620                                 struct attribute *attr,
621                                 char *buf)
622 {
623         struct module_attribute *attribute;
624         struct module_kobject *mk;
625         int ret;
626
627         attribute = to_module_attr(attr);
628         mk = to_module_kobject(kobj);
629
630         if (!attribute->show)
631                 return -EPERM;
632
633         if (!try_module_get(mk->mod))
634                 return -ENODEV;
635
636         ret = attribute->show(attribute, mk->mod, buf);
637
638         module_put(mk->mod);
639
640         return ret;
641 }
642
643 static ssize_t module_attr_store(struct kobject *kobj,
644                                 struct attribute *attr,
645                                 const char *buf, size_t len)
646 {
647         struct module_attribute *attribute;
648         struct module_kobject *mk;
649         int ret;
650
651         attribute = to_module_attr(attr);
652         mk = to_module_kobject(kobj);
653
654         if (!attribute->store)
655                 return -EPERM;
656
657         if (!try_module_get(mk->mod))
658                 return -ENODEV;
659
660         ret = attribute->store(attribute, mk->mod, buf, len);
661
662         module_put(mk->mod);
663
664         return ret;
665 }
666
667 static struct sysfs_ops module_sysfs_ops = {
668         .show = module_attr_show,
669         .store = module_attr_store,
670 };
671
672 #else
673 static struct sysfs_ops module_sysfs_ops = {
674         .show = NULL,
675         .store = NULL,
676 };
677 #endif
678
679 static struct kobj_type module_ktype = {
680         .sysfs_ops =    &module_sysfs_ops,
681 };
682
683 decl_subsys(module, &module_ktype, NULL);
684
685 /*
686  * param_sysfs_init - wrapper for built-in params support
687  */
688 static int __init param_sysfs_init(void)
689 {
690         subsystem_register(&module_subsys);
691
692         param_sysfs_builtin();
693
694         return 0;
695 }
696 __initcall(param_sysfs_init);
697
698 EXPORT_SYMBOL(param_set_byte);
699 EXPORT_SYMBOL(param_get_byte);
700 EXPORT_SYMBOL(param_set_short);
701 EXPORT_SYMBOL(param_get_short);
702 EXPORT_SYMBOL(param_set_ushort);
703 EXPORT_SYMBOL(param_get_ushort);
704 EXPORT_SYMBOL(param_set_int);
705 EXPORT_SYMBOL(param_get_int);
706 EXPORT_SYMBOL(param_set_uint);
707 EXPORT_SYMBOL(param_get_uint);
708 EXPORT_SYMBOL(param_set_long);
709 EXPORT_SYMBOL(param_get_long);
710 EXPORT_SYMBOL(param_set_ulong);
711 EXPORT_SYMBOL(param_get_ulong);
712 EXPORT_SYMBOL(param_set_charp);
713 EXPORT_SYMBOL(param_get_charp);
714 EXPORT_SYMBOL(param_set_bool);
715 EXPORT_SYMBOL(param_get_bool);
716 EXPORT_SYMBOL(param_set_invbool);
717 EXPORT_SYMBOL(param_get_invbool);
718 EXPORT_SYMBOL(param_array_set);
719 EXPORT_SYMBOL(param_array_get);
720 EXPORT_SYMBOL(param_set_copystring);
721 EXPORT_SYMBOL(param_get_string);