UBUNTU: Ubuntu-2.6.38-12.51
[linux-flexiantxendom0-natty.git] / kernel / params.c
index 3e78fdb..dcb711d 100644 (file)
 #define DEBUGP(fmt, a...)
 #endif
 
+/* Protects all parameters, and incidentally kmalloced_param list. */
+static DEFINE_MUTEX(param_lock);
+
+/* This just allows us to keep track of which parameters are kmalloced. */
+struct kmalloced_param {
+       struct list_head list;
+       char val[];
+};
+static LIST_HEAD(kmalloced_params);
+
+static void *kmalloc_parameter(unsigned int size)
+{
+       struct kmalloced_param *p;
+
+       p = kmalloc(sizeof(*p) + size, GFP_KERNEL);
+       if (!p)
+               return NULL;
+
+       list_add(&p->list, &kmalloced_params);
+       return p->val;
+}
+
+/* Does nothing if parameter wasn't kmalloced above. */
+static void maybe_kfree_parameter(void *param)
+{
+       struct kmalloced_param *p;
+
+       list_for_each_entry(p, &kmalloced_params, list) {
+               if (p->val == param) {
+                       list_del(&p->list);
+                       kfree(p);
+                       break;
+               }
+       }
+}
+
 static inline char dash2underscore(char c)
 {
        if (c == '-')
@@ -49,27 +85,39 @@ static inline int parameq(const char *input, const char *paramname)
 
 static int parse_one(char *param,
                     char *val,
-                    struct kernel_param *params, 
+                    const struct kernel_param *params,
                     unsigned num_params,
-                    int (*handle_unknown)(char *param, char *val))
+                    int (*handle_arg)(char *param, char *val, int known))
 {
        unsigned int i;
+       int err;
 
        /* Find parameter */
        for (i = 0; i < num_params; i++) {
                if (parameq(param, params[i].name)) {
                        /* Noone handled NULL, so do it here. */
-                       if (!val && params[i].set != param_set_bool)
+                       if (!val && params[i].ops->set != param_set_bool)
                                return -EINVAL;
+                       if (handle_arg) {
+                               int ret;
+                               DEBUGP("Valid argument: calling %p\n",
+                                      handle_arg);
+                               ret = handle_arg(param, val, 1);
+                               if (ret)
+                                       return ret;
+                       }
                        DEBUGP("They are equal!  Calling %p\n",
-                              params[i].set);
-                       return params[i].set(val, &params[i]);
+                              params[i].ops->set);
+                       mutex_lock(&param_lock);
+                       err = params[i].ops->set(val, &params[i]);
+                       mutex_unlock(&param_lock);
+                       return err;
                }
        }
 
-       if (handle_unknown) {
-               DEBUGP("Unknown argument: calling %p\n", handle_unknown);
-               return handle_unknown(param, val);
+       if (handle_arg) {
+               DEBUGP("Unknown argument: calling %p\n", handle_arg);
+               return handle_arg(param, val, 0);
        }
 
        DEBUGP("Unknown argument `%s'\n", param);
@@ -131,9 +179,9 @@ static char *next_arg(char *args, char **param, char **val)
 /* Args looks like "foo=bar,bar2 baz=fuz wiz". */
 int parse_args(const char *name,
               char *args,
-              struct kernel_param *params,
+              const struct kernel_param *params,
               unsigned num,
-              int (*unknown)(char *param, char *val))
+              int (*handle_arg)(char *param, char *val, int arg))
 {
        char *param, *val;
 
@@ -148,7 +196,7 @@ int parse_args(const char *name,
 
                args = next_arg(args, &param, &val);
                irq_was_disabled = irqs_disabled();
-               ret = parse_one(param, val, params, num, unknown);
+               ret = parse_one(param, val, params, num, handle_arg);
                if (irq_was_disabled && !irqs_disabled()) {
                        printk(KERN_WARNING "parse_args(): option '%s' enabled "
                                        "irq's!\n", param);
@@ -179,7 +227,7 @@ int parse_args(const char *name,
 
 /* Lazy bastard, eh? */
 #define STANDARD_PARAM_DEF(name, type, format, tmptype, strtolfn)              \
-       int param_set_##name(const char *val, struct kernel_param *kp)  \
+       int param_set_##name(const char *val, const struct kernel_param *kp) \
        {                                                               \
                tmptype l;                                              \
                int ret;                                                \
@@ -190,12 +238,18 @@ int parse_args(const char *name,
                *((type *)kp->arg) = l;                                 \
                return 0;                                               \
        }                                                               \
-       int param_get_##name(char *buffer, struct kernel_param *kp)     \
+       int param_get_##name(char *buffer, const struct kernel_param *kp) \
        {                                                               \
                return sprintf(buffer, format, *((type *)kp->arg));     \
        }                                                               \
+       struct kernel_param_ops param_ops_##name = {                    \
+               .set = param_set_##name,                                \
+               .get = param_get_##name,                                \
+       };                                                              \
        EXPORT_SYMBOL(param_set_##name);                                \
-       EXPORT_SYMBOL(param_get_##name)
+       EXPORT_SYMBOL(param_get_##name);                                \
+       EXPORT_SYMBOL(param_ops_##name)
+
 
 STANDARD_PARAM_DEF(byte, unsigned char, "%c", unsigned long, strict_strtoul);
 STANDARD_PARAM_DEF(short, short, "%hi", long, strict_strtol);
@@ -205,7 +259,7 @@ STANDARD_PARAM_DEF(uint, unsigned int, "%u", unsigned long, strict_strtoul);
 STANDARD_PARAM_DEF(long, long, "%li", long, strict_strtol);
 STANDARD_PARAM_DEF(ulong, unsigned long, "%lu", unsigned long, strict_strtoul);
 
-int param_set_charp(const char *val, struct kernel_param *kp)
+int param_set_charp(const char *val, const struct kernel_param *kp)
 {
        if (strlen(val) > 1024) {
                printk(KERN_ERR "%s: string parameter too long\n",
@@ -213,12 +267,15 @@ int param_set_charp(const char *val, struct kernel_param *kp)
                return -ENOSPC;
        }
 
-       /* This is a hack.  We can't need to strdup in early boot, and we
+       maybe_kfree_parameter(*(char **)kp->arg);
+
+       /* This is a hack.  We can't kmalloc in early boot, and we
         * don't need to; this mangled commandline is preserved. */
        if (slab_is_available()) {
-               *(char **)kp->arg = kstrdup(val, GFP_KERNEL);
+               *(char **)kp->arg = kmalloc_parameter(strlen(val)+1);
                if (!*(char **)kp->arg)
                        return -ENOMEM;
+               strcpy(*(char **)kp->arg, val);
        } else
                *(const char **)kp->arg = val;
 
@@ -226,14 +283,26 @@ int param_set_charp(const char *val, struct kernel_param *kp)
 }
 EXPORT_SYMBOL(param_set_charp);
 
-int param_get_charp(char *buffer, struct kernel_param *kp)
+int param_get_charp(char *buffer, const struct kernel_param *kp)
 {
        return sprintf(buffer, "%s", *((char **)kp->arg));
 }
 EXPORT_SYMBOL(param_get_charp);
 
+static void param_free_charp(void *arg)
+{
+       maybe_kfree_parameter(*((char **)arg));
+}
+
+struct kernel_param_ops param_ops_charp = {
+       .set = param_set_charp,
+       .get = param_get_charp,
+       .free = param_free_charp,
+};
+EXPORT_SYMBOL(param_ops_charp);
+
 /* Actually could be a bool or an int, for historical reasons. */
-int param_set_bool(const char *val, struct kernel_param *kp)
+int param_set_bool(const char *val, const struct kernel_param *kp)
 {
        bool v;
 
@@ -260,7 +329,7 @@ int param_set_bool(const char *val, struct kernel_param *kp)
 }
 EXPORT_SYMBOL(param_set_bool);
 
-int param_get_bool(char *buffer, struct kernel_param *kp)
+int param_get_bool(char *buffer, const struct kernel_param *kp)
 {
        bool val;
        if (kp->flags & KPARAM_ISBOOL)
@@ -273,8 +342,14 @@ int param_get_bool(char *buffer, struct kernel_param *kp)
 }
 EXPORT_SYMBOL(param_get_bool);
 
+struct kernel_param_ops param_ops_bool = {
+       .set = param_set_bool,
+       .get = param_get_bool,
+};
+EXPORT_SYMBOL(param_ops_bool);
+
 /* This one must be bool. */
-int param_set_invbool(const char *val, struct kernel_param *kp)
+int param_set_invbool(const char *val, const struct kernel_param *kp)
 {
        int ret;
        bool boolval;
@@ -289,18 +364,24 @@ int param_set_invbool(const char *val, struct kernel_param *kp)
 }
 EXPORT_SYMBOL(param_set_invbool);
 
-int param_get_invbool(char *buffer, struct kernel_param *kp)
+int param_get_invbool(char *buffer, const struct kernel_param *kp)
 {
        return sprintf(buffer, "%c", (*(bool *)kp->arg) ? 'N' : 'Y');
 }
 EXPORT_SYMBOL(param_get_invbool);
 
+struct kernel_param_ops param_ops_invbool = {
+       .set = param_set_invbool,
+       .get = param_get_invbool,
+};
+EXPORT_SYMBOL(param_ops_invbool);
+
 /* We break the rule and mangle the string. */
 static int param_array(const char *name,
                       const char *val,
                       unsigned int min, unsigned int max,
                       void *elem, int elemsize,
-                      int (*set)(const char *, struct kernel_param *kp),
+                      int (*set)(const char *, const struct kernel_param *kp),
                       u16 flags,
                       unsigned int *num)
 {
@@ -328,6 +409,7 @@ static int param_array(const char *name,
                /* nul-terminate and parse */
                save = val[len];
                ((char *)val)[len] = '\0';
+               BUG_ON(!mutex_is_locked(&param_lock));
                ret = set(val, &kp);
 
                if (ret != 0)
@@ -345,18 +427,17 @@ static int param_array(const char *name,
        return 0;
 }
 
-int param_array_set(const char *val, struct kernel_param *kp)
+static int param_array_set(const char *val, const struct kernel_param *kp)
 {
        const struct kparam_array *arr = kp->arr;
        unsigned int temp_num;
 
        return param_array(kp->name, val, 1, arr->max, arr->elem,
-                          arr->elemsize, arr->set, kp->flags,
+                          arr->elemsize, arr->ops->set, kp->flags,
                           arr->num ?: &temp_num);
 }
-EXPORT_SYMBOL(param_array_set);
 
-int param_array_get(char *buffer, struct kernel_param *kp)
+static int param_array_get(char *buffer, const struct kernel_param *kp)
 {
        int i, off, ret;
        const struct kparam_array *arr = kp->arr;
@@ -367,7 +448,8 @@ int param_array_get(char *buffer, struct kernel_param *kp)
                if (i)
                        buffer[off++] = ',';
                p.arg = arr->elem + arr->elemsize * i;
-               ret = arr->get(buffer + off, &p);
+               BUG_ON(!mutex_is_locked(&param_lock));
+               ret = arr->ops->get(buffer + off, &p);
                if (ret < 0)
                        return ret;
                off += ret;
@@ -375,9 +457,25 @@ int param_array_get(char *buffer, struct kernel_param *kp)
        buffer[off] = '\0';
        return off;
 }
-EXPORT_SYMBOL(param_array_get);
 
-int param_set_copystring(const char *val, struct kernel_param *kp)
+static void param_array_free(void *arg)
+{
+       unsigned int i;
+       const struct kparam_array *arr = arg;
+
+       if (arr->ops->free)
+               for (i = 0; i < (arr->num ? *arr->num : arr->max); i++)
+                       arr->ops->free(arr->elem + arr->elemsize * i);
+}
+
+struct kernel_param_ops param_array_ops = {
+       .set = param_array_set,
+       .get = param_array_get,
+       .free = param_array_free,
+};
+EXPORT_SYMBOL(param_array_ops);
+
+int param_set_copystring(const char *val, const struct kernel_param *kp)
 {
        const struct kparam_string *kps = kp->str;
 
@@ -391,13 +489,19 @@ int param_set_copystring(const char *val, struct kernel_param *kp)
 }
 EXPORT_SYMBOL(param_set_copystring);
 
-int param_get_string(char *buffer, struct kernel_param *kp)
+int param_get_string(char *buffer, const struct kernel_param *kp)
 {
        const struct kparam_string *kps = kp->str;
        return strlcpy(buffer, kps->string, kps->maxlen);
 }
 EXPORT_SYMBOL(param_get_string);
 
+struct kernel_param_ops param_ops_string = {
+       .set = param_set_copystring,
+       .get = param_get_string,
+};
+EXPORT_SYMBOL(param_ops_string);
+
 /* sysfs output in /sys/modules/XYZ/parameters/ */
 #define to_module_attr(n) container_of(n, struct module_attribute, attr)
 #define to_module_kobject(n) container_of(n, struct module_kobject, kobj)
@@ -407,7 +511,7 @@ extern struct kernel_param __start___param[], __stop___param[];
 struct param_attribute
 {
        struct module_attribute mattr;
-       struct kernel_param *param;
+       const struct kernel_param *param;
 };
 
 struct module_param_attrs
@@ -426,10 +530,12 @@ static ssize_t param_attr_show(struct module_attribute *mattr,
        int count;
        struct param_attribute *attribute = to_param_attr(mattr);
 
-       if (!attribute->param->get)
+       if (!attribute->param->ops->get)
                return -EPERM;
 
-       count = attribute->param->get(buf, attribute->param);
+       mutex_lock(&param_lock);
+       count = attribute->param->ops->get(buf, attribute->param);
+       mutex_unlock(&param_lock);
        if (count > 0) {
                strcat(buf, "\n");
                ++count;
@@ -445,10 +551,12 @@ static ssize_t param_attr_store(struct module_attribute *mattr,
        int err;
        struct param_attribute *attribute = to_param_attr(mattr);
 
-       if (!attribute->param->set)
+       if (!attribute->param->ops->set)
                return -EPERM;
 
-       err = attribute->param->set(buf, attribute->param);
+       mutex_lock(&param_lock);
+       err = attribute->param->ops->set(buf, attribute->param);
+       mutex_unlock(&param_lock);
        if (!err)
                return len;
        return err;
@@ -462,6 +570,18 @@ static ssize_t param_attr_store(struct module_attribute *mattr,
 #endif
 
 #ifdef CONFIG_SYSFS
+void __kernel_param_lock(void)
+{
+       mutex_lock(&param_lock);
+}
+EXPORT_SYMBOL(__kernel_param_lock);
+
+void __kernel_param_unlock(void)
+{
+       mutex_unlock(&param_lock);
+}
+EXPORT_SYMBOL(__kernel_param_unlock);
+
 /*
  * add_sysfs_param - add a parameter to sysfs
  * @mk: struct module_kobject
@@ -473,7 +593,7 @@ static ssize_t param_attr_store(struct module_attribute *mattr,
  * if there's an error.
  */
 static __modinit int add_sysfs_param(struct module_kobject *mk,
-                                    struct kernel_param *kp,
+                                    const struct kernel_param *kp,
                                     const char *name)
 {
        struct module_param_attrs *new;
@@ -555,7 +675,7 @@ static void free_module_param_attrs(struct module_kobject *mk)
  * /sys/module/[mod->name]/parameters/
  */
 int module_param_sysfs_setup(struct module *mod,
-                            struct kernel_param *kparam,
+                            const struct kernel_param *kparam,
                             unsigned int num_params)
 {
        int i, err;
@@ -600,12 +720,14 @@ void module_param_sysfs_remove(struct module *mod)
 
 void destroy_params(const struct kernel_param *params, unsigned num)
 {
-       /* FIXME: This should free kmalloced charp parameters.  It doesn't. */
+       unsigned int i;
+
+       for (i = 0; i < num; i++)
+               if (params[i].ops->free)
+                       params[i].ops->free(params[i].arg);
 }
 
-static void __init kernel_add_sysfs_param(const char *name,
-                                         struct kernel_param *kparam,
-                                         unsigned int name_skip)
+static struct module_kobject * __init locate_module_kobject(const char *name)
 {
        struct module_kobject *mk;
        struct kobject *kobj;
@@ -613,10 +735,7 @@ static void __init kernel_add_sysfs_param(const char *name,
 
        kobj = kset_find_obj(module_kset, name);
        if (kobj) {
-               /* We already have one.  Remove params so we can add more. */
                mk = to_module_kobject(kobj);
-               /* We need to remove it before adding parameters. */
-               sysfs_remove_group(&mk->kobj, &mk->mp->grp);
        } else {
                mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL);
                BUG_ON(!mk);
@@ -627,15 +746,36 @@ static void __init kernel_add_sysfs_param(const char *name,
                                           "%s", name);
                if (err) {
                        kobject_put(&mk->kobj);
-                       printk(KERN_ERR "Module '%s' failed add to sysfs, "
-                              "error number %d\n", name, err);
-                       printk(KERN_ERR "The system will be unstable now.\n");
-                       return;
+                       printk(KERN_ERR
+                               "Module '%s' failed add to sysfs, error number %d\n",
+                               name, err);
+                       printk(KERN_ERR
+                               "The system will be unstable now.\n");
+                       return NULL;
                }
-               /* So that exit path is even. */
+
+               /* So that we hold reference in both cases. */
                kobject_get(&mk->kobj);
        }
 
+       return mk;
+}
+
+static void __init kernel_add_sysfs_param(const char *name,
+                                         struct kernel_param *kparam,
+                                         unsigned int name_skip)
+{
+       struct module_kobject *mk;
+       int err;
+
+       mk = locate_module_kobject(name);
+       if (!mk)
+               return;
+
+       /* We need to remove old parameters before adding more. */
+       if (mk->mp)
+               sysfs_remove_group(&mk->kobj, &mk->mp->grp);
+
        /* These should not fail at boot. */
        err = add_sysfs_param(mk, kparam, kparam->name + name_skip);
        BUG_ON(err);
@@ -680,6 +820,32 @@ static void __init param_sysfs_builtin(void)
        }
 }
 
+ssize_t __modver_version_show(struct module_attribute *mattr,
+                             struct module *mod, char *buf)
+{
+       struct module_version_attribute *vattr =
+               container_of(mattr, struct module_version_attribute, mattr);
+
+       return sprintf(buf, "%s\n", vattr->version);
+}
+
+extern struct module_version_attribute __start___modver[], __stop___modver[];
+
+static void __init version_sysfs_builtin(void)
+{
+       const struct module_version_attribute *vattr;
+       struct module_kobject *mk;
+       int err;
+
+       for (vattr = __start___modver; vattr < __stop___modver; vattr++) {
+               mk = locate_module_kobject(vattr->module_name);
+               if (mk) {
+                       err = sysfs_create_file(&mk->kobj, &vattr->mattr.attr);
+                       kobject_uevent(&mk->kobj, KOBJ_ADD);
+                       kobject_put(&mk->kobj);
+               }
+       }
+}
 
 /* module-related sysfs stuff */
 
@@ -759,6 +925,7 @@ static int __init param_sysfs_init(void)
        }
        module_sysfs_initialized = 1;
 
+       version_sysfs_builtin();
        param_sysfs_builtin();
 
        return 0;