936 lines · 21.7 KB · C
RawDownload
 /* 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) extern struct kernel_param __start___param[], __stop___param[]; struct param_attribute{	struct module_attribute mattr;	const struct kernel_param *param;}; struct module_param_attrs{	unsigned int num;	struct attribute_group grp;	struct param_attribute attrs[0];}; #ifdef CONFIG_SYSFS#define to_param_attr(n) container_of(n, struct param_attribute, mattr) static ssize_t param_attr_show(struct module_attribute *mattr,			       struct module_kobject *mk, char *buf){	int count;	struct param_attribute *attribute = to_param_attr(mattr); 	if (!attribute->param->ops->get)		return -EPERM; 	mutex_lock(&param_lock);	count = attribute->param->ops->get(buf, attribute->param);	mutex_unlock(&param_lock);	if (count > 0) {		strcat(buf, "\n");		++count;	}	return count;} /* sysfs always hands a nul-terminated string in buf.  We rely on that. */static ssize_t param_attr_store(struct module_attribute *mattr,				struct module_kobject *km,				const char *buf, size_t len){ 	int err;	struct param_attribute *attribute = to_param_attr(mattr); 	if (!attribute->param->ops->set)		return -EPERM; 	mutex_lock(&param_lock);	err = attribute->param->ops->set(buf, attribute->param);	mutex_unlock(&param_lock);	if (!err)		return len;	return err;}#endif #ifdef CONFIG_MODULES#define __modinit#else#define __modinit __init#endif #ifdef CONFIG_SYSFSvoid __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 * @kparam: the actual parameter definition to add to sysfs * @name: name of parameter * * Create a kobject if for a (per-module) parameter if mp NULL, and * create file in sysfs.  Returns an error on out of memory.  Always cleans up * if there's an error. */static __modinit int add_sysfs_param(struct module_kobject *mk,				     const struct kernel_param *kp,				     const char *name){	struct module_param_attrs *new;	struct attribute **attrs;	int err, num; 	/* We don't bother calling this with invisible parameters. */	BUG_ON(!kp->perm); 	if (!mk->mp) {		num = 0;		attrs = NULL;	} else {		num = mk->mp->num;		attrs = mk->mp->grp.attrs;	} 	/* Enlarge. */	new = krealloc(mk->mp,		       sizeof(*mk->mp) + sizeof(mk->mp->attrs[0]) * (num+1),		       GFP_KERNEL);	if (!new) {		kfree(mk->mp);		err = -ENOMEM;		goto fail;	}	attrs = krealloc(attrs, sizeof(new->grp.attrs[0])*(num+2), GFP_KERNEL);	if (!attrs) {		err = -ENOMEM;		goto fail_free_new;	} 	/* Sysfs wants everything zeroed. */	memset(new, 0, sizeof(*new));	memset(&new->attrs[num], 0, sizeof(new->attrs[num]));	memset(&attrs[num], 0, sizeof(attrs[num]));	new->grp.name = "parameters";	new->grp.attrs = attrs; 	/* Tack new one on the end. */	sysfs_attr_init(&new->attrs[num].mattr.attr);	new->attrs[num].param = kp;	new->attrs[num].mattr.show = param_attr_show;	new->attrs[num].mattr.store = param_attr_store;	new->attrs[num].mattr.attr.name = (char *)name;	new->attrs[num].mattr.attr.mode = kp->perm;	new->num = num+1; 	/* Fix up all the pointers, since krealloc can move us */	for (num = 0; num < new->num; num++)		new->grp.attrs[num] = &new->attrs[num].mattr.attr;	new->grp.attrs[num] = NULL; 	mk->mp = new;	return 0; fail_free_new:	kfree(new);fail:	mk->mp = NULL;	return err;} #ifdef CONFIG_MODULESstatic void free_module_param_attrs(struct module_kobject *mk){	kfree(mk->mp->grp.attrs);	kfree(mk->mp);	mk->mp = NULL;} /* * module_param_sysfs_setup - setup sysfs support for one module * @mod: module * @kparam: module parameters (array) * @num_params: number of module parameters * * Adds sysfs entries for module parameters under * /sys/module/[mod->name]/parameters/ */int module_param_sysfs_setup(struct module *mod,			     const struct kernel_param *kparam,			     unsigned int num_params){	int i, err;	bool params = false; 	for (i = 0; i < num_params; i++) {		if (kparam[i].perm == 0)			continue;		err = add_sysfs_param(&mod->mkobj, &kparam[i], kparam[i].name);		if (err)			return err;		params = true;	} 	if (!params)		return 0; 	/* Create the param group. */	err = sysfs_create_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);	if (err)		free_module_param_attrs(&mod->mkobj);	return err;} /* * module_param_sysfs_remove - remove sysfs support for one module * @mod: module * * Remove sysfs entries for module parameters and the corresponding * kobject. */void module_param_sysfs_remove(struct module *mod){	if (mod->mkobj.mp) {		sysfs_remove_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);		/* We are positive that no one is using any param		 * attrs at this point.  Deallocate immediately. */		free_module_param_attrs(&mod->mkobj);	}}#endif void destroy_params(const struct kernel_param *params, unsigned num){	unsigned int i; 	for (i = 0; i < num; i++)		if (params[i].ops->free)			params[i].ops->free(params[i].arg);} static struct module_kobject * __init locate_module_kobject(const char *name){	struct module_kobject *mk;	struct kobject *kobj;	int err; 	kobj = kset_find_obj(module_kset, name);	if (kobj) {		mk = to_module_kobject(kobj);	} else {		mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL);		BUG_ON(!mk); 		mk->mod = THIS_MODULE;		mk->kobj.kset = module_kset;		err = kobject_init_and_add(&mk->kobj, &module_ktype, NULL,					   "%s", name);#ifdef CONFIG_MODULES		if (!err)			err = sysfs_create_file(&mk->kobj, &module_uevent.attr);#endif		if (err) {			kobject_put(&mk->kobj);			pr_crit("Adding module '%s' to sysfs failed (%d), the system may be unstable.\n",				name, err);			return NULL;		} 		/* 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);	err = sysfs_create_group(&mk->kobj, &mk->mp->grp);	BUG_ON(err);	kobject_uevent(&mk->kobj, KOBJ_ADD);	kobject_put(&mk->kobj);} /* * param_sysfs_builtin - add contents in /sys/parameters for built-in modules * * Add module_parameters to sysfs for "modules" built into the kernel. * * The "module" name (KBUILD_MODNAME) is stored before a dot, the * "parameter" name is stored behind a dot in kernel_param->name. So, * extract the "module" name for all built-in kernel_param-eters, * and for all who have the same, call kernel_add_sysfs_param. */static void __init param_sysfs_builtin(void){	struct kernel_param *kp;	unsigned int name_len;	char modname[MODULE_NAME_LEN]; 	for (kp = __start___param; kp < __stop___param; kp++) {		char *dot; 		if (kp->perm == 0)			continue; 		dot = strchr(kp->name, '.');		if (!dot) {			/* This happens for core_param() */			strcpy(modname, "kernel");			name_len = 0;		} else {			name_len = dot - kp->name + 1;			strlcpy(modname, kp->name, name_len);		}		kernel_add_sysfs_param(modname, kp, name_len);	}} ssize_t __modver_version_show(struct module_attribute *mattr,			      struct module_kobject *mk, char *buf){	struct module_version_attribute *vattr =		container_of(mattr, struct module_version_attribute, mattr); 	return sprintf(buf, "%s\n", vattr->version);} extern const struct module_version_attribute *__start___modver[];extern const struct module_version_attribute *__stop___modver[]; static void __init version_sysfs_builtin(void){	const struct module_version_attribute **p;	struct module_kobject *mk;	int err; 	for (p = __start___modver; p < __stop___modver; p++) {		const struct module_version_attribute *vattr = *p; 		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 */ static ssize_t module_attr_show(struct kobject *kobj,				struct attribute *attr,				char *buf){	struct module_attribute *attribute;	struct module_kobject *mk;	int ret; 	attribute = to_module_attr(attr);	mk = to_module_kobject(kobj); 	if (!attribute->show)		return -EIO; 	ret = attribute->show(attribute, mk, buf); 	return ret;} static ssize_t module_attr_store(struct kobject *kobj,				struct attribute *attr,				const char *buf, size_t len){	struct module_attribute *attribute;	struct module_kobject *mk;	int ret; 	attribute = to_module_attr(attr);	mk = to_module_kobject(kobj); 	if (!attribute->store)		return -EIO; 	ret = attribute->store(attribute, mk, buf, len); 	return ret;} static const struct sysfs_ops module_sysfs_ops = {	.show = module_attr_show,	.store = module_attr_store,}; static int uevent_filter(struct kset *kset, struct kobject *kobj){	struct kobj_type *ktype = get_ktype(kobj); 	if (ktype == &module_ktype)		return 1;	return 0;} static const struct kset_uevent_ops module_uevent_ops = {	.filter = uevent_filter,}; struct kset *module_kset;int module_sysfs_initialized; struct kobj_type module_ktype = {	.sysfs_ops =	&module_sysfs_ops,}; /* * param_sysfs_init - wrapper for built-in params support */static int __init param_sysfs_init(void){	module_kset = kset_create_and_add("module", &module_uevent_ops, NULL);	if (!module_kset) {		printk(KERN_WARNING "%s (%d): error creating kset\n",			__FILE__, __LINE__);		return -ENOMEM;	}	module_sysfs_initialized = 1; 	version_sysfs_builtin();	param_sysfs_builtin(); 	return 0;}subsys_initcall(param_sysfs_init); #endif /* CONFIG_SYSFS */

Lines 501–936 of 936. Previous lines