Commit c386f08b authored by Andrew Morton's avatar Andrew Morton Committed by Greg Kroah-Hartman

[PATCH] sysfs backing store - add sysfs_direct structure

From: Maneesh Soni <maneesh@in.ibm.com>

o This patch introduces the new sysfs_dirent data structure. The sysfs_dirent
  is added to the dentry corresponding to each of the element which can be
  represented in sysfs like, kobject (directory), text or binary attributes
  (files), attribute groups (directory) and symlinks.

o It uses dentry's d_fsdata field to attach the corresponding sysfs_dirent.

o The sysfs_dirents are maintained in a tree of all the sysfs entries using the
  s_children and s_sibling list pointers.

o This patch also changes how we access attributes and kobjects in
  file_operations from a given dentry, basically introducing one more level of
  indirection.

o The sysfs_dirents are freed and the sysfs_dirent tree is updated accordingly
  upon the deletion of corresponding dentry. The sysfs dirents are kept alive
  as long as there is corresponding dentry around. The are freed when the
  dentry is finally out of dcache using the ->d_iput() method.

o This also fixes the dentry leaks in case of error paths after sysfs has
  got a newly alocated (and hashed) dentry from sysfs_get_dentry() by
  d_drop()'ing the dentry.
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarGreg Kroah-Hartman <greg@kroah.com>
parent d74a8e5a
...@@ -160,24 +160,26 @@ int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr) ...@@ -160,24 +160,26 @@ int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr)
{ {
struct dentry * dentry; struct dentry * dentry;
struct dentry * parent; struct dentry * parent;
umode_t mode = (attr->attr.mode & S_IALLUGO) | S_IFREG;
int error = 0; int error = 0;
if (!kobj || !attr) BUG_ON(!kobj || !kobj->dentry || !attr);
return -EINVAL;
parent = kobj->dentry; parent = kobj->dentry;
down(&parent->d_inode->i_sem); down(&parent->d_inode->i_sem);
dentry = sysfs_get_dentry(parent,attr->attr.name); dentry = sysfs_get_dentry(parent,attr->attr.name);
if (!IS_ERR(dentry)) { if (!IS_ERR(dentry)) {
dentry->d_fsdata = (void *)attr; error = sysfs_create(dentry, mode, NULL);
error = sysfs_create(dentry,
(attr->attr.mode & S_IALLUGO) | S_IFREG,
NULL);
if (!error) { if (!error) {
dentry->d_inode->i_size = attr->size; dentry->d_inode->i_size = attr->size;
dentry->d_inode->i_fop = &bin_fops; dentry->d_inode->i_fop = &bin_fops;
error = sysfs_make_dirent(parent->d_fsdata, dentry,
(void *) attr, mode,
SYSFS_KOBJ_BIN_ATTR);
} }
if (error)
d_drop(dentry);
dput(dentry); dput(dentry);
} else } else
error = PTR_ERR(dentry); error = PTR_ERR(dentry);
......
...@@ -12,6 +12,61 @@ ...@@ -12,6 +12,61 @@
DECLARE_RWSEM(sysfs_rename_sem); DECLARE_RWSEM(sysfs_rename_sem);
static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
{
struct sysfs_dirent * sd = dentry->d_fsdata;
if (sd) {
BUG_ON(sd->s_dentry != dentry);
sd->s_dentry = NULL;
release_sysfs_dirent(sd);
}
iput(inode);
}
static struct dentry_operations sysfs_dentry_ops = {
.d_iput = sysfs_d_iput,
};
/*
* Allocates a new sysfs_dirent and links it to the parent sysfs_dirent
*/
static struct sysfs_dirent * sysfs_new_dirent(struct sysfs_dirent * parent_sd,
void * element)
{
struct sysfs_dirent * sd;
sd = kmalloc(sizeof(*sd), GFP_KERNEL);
if (!sd)
return ERR_PTR(-ENOMEM);
memset(sd, 0, sizeof(*sd));
atomic_set(&sd->s_count, 1);
INIT_LIST_HEAD(&sd->s_children);
list_add(&sd->s_sibling, &parent_sd->s_children);
sd->s_element = element;
return sd;
}
int sysfs_make_dirent(struct sysfs_dirent * parent_sd, struct dentry * dentry,
void * element, umode_t mode, int type)
{
struct sysfs_dirent * sd;
sd = sysfs_new_dirent(parent_sd, element);
if (!sd)
return -ENOMEM;
sd->s_mode = mode;
sd->s_type = type;
sd->s_dentry = dentry;
dentry->d_fsdata = sd;
dentry->d_op = &sysfs_dentry_ops;
return 0;
}
static int init_dir(struct inode * inode) static int init_dir(struct inode * inode)
{ {
inode->i_op = &simple_dir_inode_operations; inode->i_op = &simple_dir_inode_operations;
...@@ -27,17 +82,20 @@ static int create_dir(struct kobject * k, struct dentry * p, ...@@ -27,17 +82,20 @@ static int create_dir(struct kobject * k, struct dentry * p,
const char * n, struct dentry ** d) const char * n, struct dentry ** d)
{ {
int error; int error;
umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
down(&p->d_inode->i_sem); down(&p->d_inode->i_sem);
*d = sysfs_get_dentry(p,n); *d = sysfs_get_dentry(p,n);
if (!IS_ERR(*d)) { if (!IS_ERR(*d)) {
error = sysfs_create(*d, error = sysfs_create(*d, mode, init_dir);
S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO,
init_dir);
if (!error) { if (!error) {
(*d)->d_fsdata = k; error = sysfs_make_dirent(p->d_fsdata, *d, k, mode,
SYSFS_DIR);
if (!error)
p->d_inode->i_nlink++; p->d_inode->i_nlink++;
} }
if (error)
d_drop(*d);
dput(*d); dput(*d);
} else } else
error = PTR_ERR(*d); error = PTR_ERR(*d);
...@@ -63,8 +121,7 @@ int sysfs_create_dir(struct kobject * kobj) ...@@ -63,8 +121,7 @@ int sysfs_create_dir(struct kobject * kobj)
struct dentry * parent; struct dentry * parent;
int error = 0; int error = 0;
if (!kobj) BUG_ON(!kobj);
return -EINVAL;
if (kobj->parent) if (kobj->parent)
parent = kobj->parent->dentry; parent = kobj->parent->dentry;
...@@ -83,8 +140,12 @@ int sysfs_create_dir(struct kobject * kobj) ...@@ -83,8 +140,12 @@ int sysfs_create_dir(struct kobject * kobj)
static void remove_dir(struct dentry * d) static void remove_dir(struct dentry * d)
{ {
struct dentry * parent = dget(d->d_parent); struct dentry * parent = dget(d->d_parent);
struct sysfs_dirent * sd;
down(&parent->d_inode->i_sem); down(&parent->d_inode->i_sem);
d_delete(d); d_delete(d);
sd = d->d_fsdata;
list_del_init(&sd->s_sibling);
if (d->d_inode) if (d->d_inode)
simple_rmdir(parent->d_inode,d); simple_rmdir(parent->d_inode,d);
...@@ -130,6 +191,7 @@ void sysfs_remove_dir(struct kobject * kobj) ...@@ -130,6 +191,7 @@ void sysfs_remove_dir(struct kobject * kobj)
node = node->next; node = node->next;
pr_debug(" o %s (%d): ",d->d_name.name,atomic_read(&d->d_count)); pr_debug(" o %s (%d): ",d->d_name.name,atomic_read(&d->d_count));
if (!d_unhashed(d) && (d->d_inode)) { if (!d_unhashed(d) && (d->d_inode)) {
struct sysfs_dirent * sd = d->d_fsdata;
d = dget_locked(d); d = dget_locked(d);
pr_debug("removing"); pr_debug("removing");
...@@ -142,8 +204,9 @@ void sysfs_remove_dir(struct kobject * kobj) ...@@ -142,8 +204,9 @@ void sysfs_remove_dir(struct kobject * kobj)
* a symlink * a symlink
*/ */
if (S_ISLNK(d->d_inode->i_mode)) if (S_ISLNK(d->d_inode->i_mode))
kobject_put(d->d_fsdata); kobject_put(sd->s_element);
list_del_init(&sd->s_sibling);
simple_unlink(dentry->d_inode,d); simple_unlink(dentry->d_inode,d);
dput(d); dput(d);
pr_debug(" done\n"); pr_debug(" done\n");
...@@ -184,7 +247,10 @@ int sysfs_rename_dir(struct kobject * kobj, const char *new_name) ...@@ -184,7 +247,10 @@ int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
error = kobject_set_name(kobj, "%s", new_name); error = kobject_set_name(kobj, "%s", new_name);
if (!error) if (!error)
d_move(kobj->dentry, new_dentry); d_move(kobj->dentry, new_dentry);
} else
d_drop(new_dentry);
} else
error = -EEXIST;
dput(new_dentry); dput(new_dentry);
} }
up(&parent->d_inode->i_sem); up(&parent->d_inode->i_sem);
......
...@@ -346,19 +346,22 @@ static struct file_operations sysfs_file_operations = { ...@@ -346,19 +346,22 @@ static struct file_operations sysfs_file_operations = {
}; };
int sysfs_add_file(struct dentry * dir, const struct attribute * attr) int sysfs_add_file(struct dentry * dir, const struct attribute * attr, int type)
{ {
struct dentry * dentry; struct dentry * dentry;
int error; struct sysfs_dirent * parent_sd = dir->d_fsdata;
umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
int error = 0;
down(&dir->d_inode->i_sem); down(&dir->d_inode->i_sem);
dentry = sysfs_get_dentry(dir,attr->name); dentry = sysfs_get_dentry(dir,attr->name);
if (!IS_ERR(dentry)) { if (!IS_ERR(dentry)) {
error = sysfs_create(dentry, error = sysfs_create(dentry, mode, init_file);
(attr->mode & S_IALLUGO) | S_IFREG,
init_file);
if (!error) if (!error)
dentry->d_fsdata = (void *)attr; error = sysfs_make_dirent(parent_sd, dentry,
(void *) attr, mode, type);
if (error)
d_drop(dentry);
dput(dentry); dput(dentry);
} else } else
error = PTR_ERR(dentry); error = PTR_ERR(dentry);
...@@ -375,9 +378,10 @@ int sysfs_add_file(struct dentry * dir, const struct attribute * attr) ...@@ -375,9 +378,10 @@ int sysfs_add_file(struct dentry * dir, const struct attribute * attr)
int sysfs_create_file(struct kobject * kobj, const struct attribute * attr) int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
{ {
if (kobj && attr) BUG_ON(!kobj || !kobj->dentry || !attr);
return sysfs_add_file(kobj->dentry,attr);
return -EINVAL; return sysfs_add_file(kobj->dentry, attr, SYSFS_KOBJ_ATTR);
} }
...@@ -409,7 +413,8 @@ int sysfs_update_file(struct kobject * kobj, const struct attribute * attr) ...@@ -409,7 +413,8 @@ int sysfs_update_file(struct kobject * kobj, const struct attribute * attr)
*/ */
dput(victim); dput(victim);
res = 0; res = 0;
} } else
d_drop(victim);
/** /**
* Drop the reference acquired from sysfs_get_dentry() above. * Drop the reference acquired from sysfs_get_dentry() above.
......
...@@ -31,7 +31,7 @@ static int create_files(struct dentry * dir, ...@@ -31,7 +31,7 @@ static int create_files(struct dentry * dir,
int error = 0; int error = 0;
for (attr = grp->attrs; *attr && !error; attr++) { for (attr = grp->attrs; *attr && !error; attr++) {
error = sysfs_add_file(dir,*attr); error = sysfs_add_file(dir, *attr, SYSFS_KOBJ_ATTR);
} }
if (error) if (error)
remove_files(dir,grp); remove_files(dir,grp);
...@@ -45,6 +45,8 @@ int sysfs_create_group(struct kobject * kobj, ...@@ -45,6 +45,8 @@ int sysfs_create_group(struct kobject * kobj,
struct dentry * dir; struct dentry * dir;
int error; int error;
BUG_ON(!kobj || !kobj->dentry);
if (grp->name) { if (grp->name) {
error = sysfs_create_subdir(kobj,grp->name,&dir); error = sysfs_create_subdir(kobj,grp->name,&dir);
if (error) if (error)
......
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
#include <linux/pagemap.h> #include <linux/pagemap.h>
#include <linux/namei.h> #include <linux/namei.h>
#include <linux/backing-dev.h> #include <linux/backing-dev.h>
#include "sysfs.h"
extern struct super_block * sysfs_sb; extern struct super_block * sysfs_sb;
static struct address_space_operations sysfs_aops = { static struct address_space_operations sysfs_aops = {
...@@ -91,6 +93,7 @@ struct dentry * sysfs_get_dentry(struct dentry * parent, const char * name) ...@@ -91,6 +93,7 @@ struct dentry * sysfs_get_dentry(struct dentry * parent, const char * name)
void sysfs_hash_and_remove(struct dentry * dir, const char * name) void sysfs_hash_and_remove(struct dentry * dir, const char * name)
{ {
struct dentry * victim; struct dentry * victim;
struct sysfs_dirent * sd;
down(&dir->d_inode->i_sem); down(&dir->d_inode->i_sem);
victim = sysfs_get_dentry(dir,name); victim = sysfs_get_dentry(dir,name);
...@@ -101,14 +104,17 @@ void sysfs_hash_and_remove(struct dentry * dir, const char * name) ...@@ -101,14 +104,17 @@ void sysfs_hash_and_remove(struct dentry * dir, const char * name)
pr_debug("sysfs: Removing %s (%d)\n", victim->d_name.name, pr_debug("sysfs: Removing %s (%d)\n", victim->d_name.name,
atomic_read(&victim->d_count)); atomic_read(&victim->d_count));
sd = victim->d_fsdata;
d_drop(victim); d_drop(victim);
/* release the target kobject in case of /* release the target kobject in case of
* a symlink * a symlink
*/ */
if (S_ISLNK(victim->d_inode->i_mode)) if (S_ISLNK(victim->d_inode->i_mode))
kobject_put(victim->d_fsdata); kobject_put(sd->s_element);
list_del_init(&sd->s_sibling);
simple_unlink(dir->d_inode,victim); simple_unlink(dir->d_inode,victim);
} } else
d_drop(victim);
/* /*
* Drop reference from sysfs_get_dentry() above. * Drop reference from sysfs_get_dentry() above.
*/ */
......
...@@ -22,6 +22,13 @@ static struct super_operations sysfs_ops = { ...@@ -22,6 +22,13 @@ static struct super_operations sysfs_ops = {
.drop_inode = generic_delete_inode, .drop_inode = generic_delete_inode,
}; };
struct sysfs_dirent sysfs_root = {
.s_sibling = LIST_HEAD_INIT(sysfs_root.s_sibling),
.s_children = LIST_HEAD_INIT(sysfs_root.s_children),
.s_element = NULL,
.s_type = SYSFS_ROOT,
};
static int sysfs_fill_super(struct super_block *sb, void *data, int silent) static int sysfs_fill_super(struct super_block *sb, void *data, int silent)
{ {
struct inode *inode; struct inode *inode;
...@@ -50,6 +57,7 @@ static int sysfs_fill_super(struct super_block *sb, void *data, int silent) ...@@ -50,6 +57,7 @@ static int sysfs_fill_super(struct super_block *sb, void *data, int silent)
iput(inode); iput(inode);
return -ENOMEM; return -ENOMEM;
} }
root->d_fsdata = &sysfs_root;
sb->s_root = root; sb->s_root = root;
return 0; return 0;
} }
......
...@@ -55,6 +55,36 @@ static void fill_object_path(struct kobject * kobj, char * buffer, int length) ...@@ -55,6 +55,36 @@ static void fill_object_path(struct kobject * kobj, char * buffer, int length)
} }
} }
static int sysfs_add_link(struct dentry * dentry, char * name, struct kobject * target)
{
struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
struct sysfs_symlink * sl;
int error = 0;
error = -ENOMEM;
sl = kmalloc(sizeof(*sl), GFP_KERNEL);
if (!sl)
goto exit1;
sl->link_name = kmalloc(strlen(name) + 1, GFP_KERNEL);
if (!sl->link_name)
goto exit2;
strcpy(sl->link_name, name);
sl->target_kobj = kobject_get(target);
error = sysfs_make_dirent(parent_sd, dentry, sl, S_IFLNK|S_IRWXUGO,
SYSFS_KOBJ_LINK);
if (!error)
return 0;
kfree(sl->link_name);
exit2:
kfree(sl);
exit1:
return error;
}
/** /**
* sysfs_create_link - create symlink between two objects. * sysfs_create_link - create symlink between two objects.
* @kobj: object whose directory we're creating the link in. * @kobj: object whose directory we're creating the link in.
...@@ -67,15 +97,16 @@ int sysfs_create_link(struct kobject * kobj, struct kobject * target, char * nam ...@@ -67,15 +97,16 @@ int sysfs_create_link(struct kobject * kobj, struct kobject * target, char * nam
struct dentry * d; struct dentry * d;
int error = 0; int error = 0;
BUG_ON(!kobj || !kobj->dentry || !name);
down(&dentry->d_inode->i_sem); down(&dentry->d_inode->i_sem);
d = sysfs_get_dentry(dentry,name); d = sysfs_get_dentry(dentry,name);
if (!IS_ERR(d)) { if (!IS_ERR(d)) {
error = sysfs_create(d, S_IFLNK|S_IRWXUGO, init_symlink); error = sysfs_create(d, S_IFLNK|S_IRWXUGO, init_symlink);
if (!error) if (!error)
/* error = sysfs_add_link(d, name, target);
* associate the link dentry with the target kobject if (error)
*/ d_drop(d);
d->d_fsdata = kobject_get(target);
dput(d); dput(d);
} else } else
error = PTR_ERR(d); error = PTR_ERR(d);
......
...@@ -4,9 +4,11 @@ extern struct vfsmount * sysfs_mount; ...@@ -4,9 +4,11 @@ extern struct vfsmount * sysfs_mount;
extern struct inode * sysfs_new_inode(mode_t mode); extern struct inode * sysfs_new_inode(mode_t mode);
extern int sysfs_create(struct dentry *, int mode, int (*init)(struct inode *)); extern int sysfs_create(struct dentry *, int mode, int (*init)(struct inode *));
extern int sysfs_make_dirent(struct sysfs_dirent *, struct dentry *, void *,
umode_t, int);
extern struct dentry * sysfs_get_dentry(struct dentry *, const char *); extern struct dentry * sysfs_get_dentry(struct dentry *, const char *);
extern int sysfs_add_file(struct dentry * dir, const struct attribute * attr); extern int sysfs_add_file(struct dentry *, const struct attribute *, int);
extern void sysfs_hash_and_remove(struct dentry * dir, const char * name); extern void sysfs_hash_and_remove(struct dentry * dir, const char * name);
extern int sysfs_create_subdir(struct kobject *, const char *, struct dentry **); extern int sysfs_create_subdir(struct kobject *, const char *, struct dentry **);
...@@ -16,19 +18,27 @@ extern int sysfs_follow_link(struct dentry *, struct nameidata *); ...@@ -16,19 +18,27 @@ extern int sysfs_follow_link(struct dentry *, struct nameidata *);
extern void sysfs_put_link(struct dentry *, struct nameidata *); extern void sysfs_put_link(struct dentry *, struct nameidata *);
extern struct rw_semaphore sysfs_rename_sem; extern struct rw_semaphore sysfs_rename_sem;
struct sysfs_symlink {
char * link_name;
struct kobject * target_kobj;
};
static inline struct kobject * to_kobj(struct dentry * dentry) static inline struct kobject * to_kobj(struct dentry * dentry)
{ {
return ((struct kobject *) dentry->d_fsdata); struct sysfs_dirent * sd = dentry->d_fsdata;
return ((struct kobject *) sd->s_element);
} }
static inline struct attribute * to_attr(struct dentry * dentry) static inline struct attribute * to_attr(struct dentry * dentry)
{ {
return ((struct attribute *) dentry->d_fsdata); struct sysfs_dirent * sd = dentry->d_fsdata;
return ((struct attribute *) sd->s_element);
} }
static inline struct bin_attribute * to_bin_attr(struct dentry * dentry) static inline struct bin_attribute * to_bin_attr(struct dentry * dentry)
{ {
return ((struct bin_attribute *) dentry->d_fsdata); struct sysfs_dirent * sd = dentry->d_fsdata;
return ((struct bin_attribute *) sd->s_element);
} }
static inline struct kobject *sysfs_get_kobject(struct dentry *dentry) static inline struct kobject *sysfs_get_kobject(struct dentry *dentry)
...@@ -36,10 +46,27 @@ static inline struct kobject *sysfs_get_kobject(struct dentry *dentry) ...@@ -36,10 +46,27 @@ static inline struct kobject *sysfs_get_kobject(struct dentry *dentry)
struct kobject * kobj = NULL; struct kobject * kobj = NULL;
spin_lock(&dcache_lock); spin_lock(&dcache_lock);
if (!d_unhashed(dentry)) if (!d_unhashed(dentry)) {
kobj = kobject_get(to_kobj(dentry)); struct sysfs_dirent * sd = dentry->d_fsdata;
if (sd->s_type & SYSFS_KOBJ_LINK) {
struct sysfs_symlink * sl = sd->s_element;
kobj = kobject_get(sl->target_kobj);
} else
kobj = kobject_get(sd->s_element);
}
spin_unlock(&dcache_lock); spin_unlock(&dcache_lock);
return kobj; return kobj;
} }
static inline void release_sysfs_dirent(struct sysfs_dirent * sd)
{
if (sd->s_type & SYSFS_KOBJ_LINK) {
struct sysfs_symlink * sl = sd->s_element;
kfree(sl->link_name);
kobject_put(sl->target_kobj);
kfree(sl);
}
kfree(sd);
}
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
#ifndef _SYSFS_H_ #ifndef _SYSFS_H_
#define _SYSFS_H_ #define _SYSFS_H_
#include <asm/atomic.h>
struct kobject; struct kobject;
struct module; struct module;
...@@ -57,6 +59,23 @@ struct sysfs_ops { ...@@ -57,6 +59,23 @@ struct sysfs_ops {
ssize_t (*store)(struct kobject *,struct attribute *,const char *, size_t); ssize_t (*store)(struct kobject *,struct attribute *,const char *, size_t);
}; };
struct sysfs_dirent {
atomic_t s_count;
struct list_head s_sibling;
struct list_head s_children;
void * s_element;
int s_type;
umode_t s_mode;
struct dentry * s_dentry;
};
#define SYSFS_ROOT 0x0001
#define SYSFS_DIR 0x0002
#define SYSFS_KOBJ_ATTR 0x0004
#define SYSFS_KOBJ_BIN_ATTR 0x0008
#define SYSFS_KOBJ_LINK 0x0020
#define SYSFS_NOT_PINNED (SYSFS_KOBJ_ATTR | SYSFS_KOBJ_BIN_ATTR | SYSFS_KOBJ_LINK)
#ifdef CONFIG_SYSFS #ifdef CONFIG_SYSFS
extern int extern int
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment