sysfs backing store - updated
cover sysfs backing store - updated
Hello Viro,
I have revised the sysfs backing store patchset. It took some what longer time due to last minute dentry leak obeserved in intermediate patches. Now I have verified individual patches work properly and without any leaks.
I have also re-arranged it according to your last comments. The patch set also fixes the recent uid/gid problem reported om lkml. Now we always create sysfs files with root uid/gid (i.e 0).
Appended below is the first patch in the series and rest of them follow this mail.
Andrew, please replace all the sysfs-backing-store-xxx.patch again in -mm.
Thanks
Maneesh
o The following patch provides dumb helpers to access the corresponding kobject, attribute or binary attribute given a dentry and prepare the sysfs_file_operation methods for using sysfs_dirents.
fs/sysfs/bin.c | 14 +++++++------- fs/sysfs/file.c | 24 ++++++++++++------------ fs/sysfs/sysfs.h | 18 +++++++++++++++++- 3 files changed, 36 insertions(+), 20 deletions(-)
fs/sysfs/sysfs.h renamed from fs/sysfs/sysfs.h~sysfs-backing-store-prepare-file_operations +17 -1
@@ -16,14 +16,30 @@ extern int sysfs_readlink(struct dentryextern int sysfs_follow_link (struct dentry *,struct nameidata *);extern struct rw_semaphore sysfs_rename_sem; +static inline struct kobject *to_kobj (struct dentry * dentry) +{ +return ((struct kobject *) dentry->d_fsdata); +} + +static inline struct attribute *to_attr (struct dentry * dentry) +{ +return ((struct attribute *) dentry->d_fsdata); +} + +static inline struct bin_attribute *to_bin_attr (struct dentry * dentry) +{ +return ((struct bin_attribute *) dentry->d_fsdata); +} +static inline struct kobject *sysfs_get_kobject (struct dentry *dentry) {struct kobject * kobj =NULL ;spin_lock (&dcache_lock);if (!d_unhashed (dentry)) - kobj =kobject_get (dentry->d_fsdata); + kobj =kobject_get (to_kobj (dentry));spin_unlock (&dcache_lock);return kobj; } +
fs/sysfs/file.c renamed from fs/sysfs/file.c~sysfs-backing-store-prepare-file_operations +12 -12
@@ -67,7 +67,7 @@ struct sysfs_buffer {/** * fill_read_buffer - allocate and fill buffer from object. -* @file: file pointer. +* @dentry: dentry pointer. * @buffer: data buffer for file. * * Allocate @buffer->page, if it hasn't been already, then call the
@@ -75,10 +75,10 @@ struct sysfs_buffer {* data. * This is called only once, on the file's first read. */ -static int fill_read_buffer (struct file * file,struct sysfs_buffer * buffer) +static int fill_read_buffer (struct dentry * dentry,struct sysfs_buffer * buffer) { -struct attribute * attr = file->f_dentry->d_fsdata; -struct kobject * kobj = file->f_dentry->d_parent->d_fsdata; +struct attribute * attr =to_attr (dentry); +struct kobject * kobj =to_kobj (dentry->d_parent);struct sysfs_ops * ops = buffer->ops;int ret =0 ;ssize_t count;
@@ -150,7 +150,7 @@ sysfs_read_file(struct file *file, charssize_t retval =0 ;if (!*ppos) { -if ((retval =fill_read_buffer (file,buffer))) +if ((retval =fill_read_buffer (file->f_dentry,buffer)))return retval; } pr_debug("%s: count = %d, ppos = %lld, buf = %s\n",
@@ -197,10 +197,10 @@ fill_write_buffer(struct sysfs_buffer **/ static int -flush_write_buffer (struct file * file,struct sysfs_buffer * buffer,size_t count) +flush_write_buffer (struct dentry * dentry,struct sysfs_buffer * buffer,size_t count) { -struct attribute * attr = file->f_dentry->d_fsdata; -struct kobject * kobj = file->f_dentry->d_parent->d_fsdata; +struct attribute * attr =to_attr (dentry); +struct kobject * kobj =to_kobj (dentry->d_parent);struct sysfs_ops * ops = buffer->ops;return ops->store (kobj,attr,buffer->page,count);
@@ -231,7 +231,7 @@ sysfs_write_file(struct file *file, cons count =fill_write_buffer (buffer,buf,count);if (count >0 ) - count =flush_write_buffer (file,buffer,count); + count =flush_write_buffer (file->f_dentry,buffer,count);if (count >0 ) *ppos += count;return count;
@@ -240,7 +240,7 @@ sysfs_write_file(struct file *file, consstatic int check_perm (struct inode * inode,struct file * file) {struct kobject *kobj =sysfs_get_kobject (file->f_dentry->d_parent); -struct attribute * attr = file->f_dentry->d_fsdata; +struct attribute * attr =to_attr (file->f_dentry);struct sysfs_buffer * buffer;struct sysfs_ops * ops =NULL ;int error =0 ;
@@ -321,8 +321,8 @@ static int sysfs_open_file(struct inodestatic int sysfs_release (struct inode * inode,struct file * filp) { -struct kobject * kobj = filp->f_dentry->d_parent->d_fsdata; -struct attribute * attr = filp->f_dentry->d_fsdata; +struct kobject * kobj =to_kobj (filp->f_dentry->d_parent); +struct attribute * attr =to_attr (filp->f_dentry);struct sysfs_buffer * buffer = filp->private_data;if (kobj)
fs/sysfs/bin.c renamed from fs/sysfs/bin.c~sysfs-backing-store-prepare-file_operations +7 -7
@@ -17,8 +17,8 @@static int fill_read (struct dentry *dentry,char *buffer,loff_t off,size_t count) { -struct bin_attribute * attr = dentry->d_fsdata; -struct kobject * kobj = dentry->d_parent->d_fsdata; +struct bin_attribute * attr =to_bin_attr (dentry); +struct kobject * kobj =to_kobj (dentry->d_parent);return attr->read (kobj, buffer, off, count); }
@@ -60,8 +60,8 @@ read(struct file * file, char __user * ustatic int flush_write (struct dentry *dentry,char *buffer,loff_t offset,size_t count) { -struct bin_attribute *attr = dentry->d_fsdata; -struct kobject *kobj = dentry->d_parent->d_fsdata; +struct bin_attribute *attr =to_bin_attr (dentry->d_parent); +struct kobject *kobj =to_kobj (dentry);return attr->write (kobj, buffer, offset, count); }
@@ -95,7 +95,7 @@ static ssize_t write(struct file * file,static int open (struct inode * inode,struct file * file) {struct kobject *kobj =sysfs_get_kobject (file->f_dentry->d_parent); -struct bin_attribute * attr = file->f_dentry->d_fsdata; +struct bin_attribute * attr =to_bin_attr (file->f_dentry);int error = -EINVAL ;if (!kobj || !attr)
@@ -130,8 +130,8 @@ static int open(struct inode * inode, ststatic int release (struct inode * inode,struct file * file) { -struct kobject * kobj = file->f_dentry->d_parent->d_fsdata; -struct bin_attribute * attr = file->f_dentry->d_fsdata; +struct kobject * kobj =to_kobj (file->f_dentry->d_parent); +struct bin_attribute * attr =to_bin_attr (file->f_dentry);u8 * buffer = file->private_data;if (kobj)
_
Re: [PATCH 1/4] Add sysfs_dirent
oops, very sorry for the repeate telecase... Andrew will curse me if I use bad subject line.. Please discard the last one.
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.
fs/sysfs/bin.c | 14 ++++---- fs/sysfs/dir.c | 84 ++++++++++++++++++++++++++++++++++++++++++++------ fs/sysfs/file.c | 25 ++++++++------ fs/sysfs/group.c | 4 +- fs/sysfs/inode.c | 10 ++++- fs/sysfs/mount.c | 8 ++++ fs/sysfs/symlink.c | 39 ++++++++++++++++++++--- fs/sysfs/sysfs.h | 39 +++++++++++++++++++---- include/linux/sysfs.h | 19 +++++++++++ 9 files changed, 204 insertions(+), 38 deletions(-)
include/linux/sysfs.h renamed from include/linux/sysfs.h~sysfs-backing-store-add-sysfs_dirent +19 -0
@@ -9,6 +9,8 @@#ifndef _SYSFS_H_#define _SYSFS_H_ +#include <asm/atomic.h> +struct kobject ;struct module ;
@@ -57,6 +59,23 @@ struct sysfs_ops {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 extern int
fs/sysfs/dir.c renamed from fs/sysfs/dir.c~sysfs-backing-store-add-sysfs_dirent +75 -9
135 lines not on this page · raw message
fs/sysfs/file.c renamed from fs/sysfs/file.c~sysfs-backing-store-add-sysfs_dirent +15 -10
@@ -346,19 +346,22 @@ static struct file_operations sysfs_file }; -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; -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); dentry =sysfs_get_dentry (dir,attr->name);if (!IS_ERR (dentry)) { - error =sysfs_create (dentry, - (attr->mode &S_IALLUGO ) |S_IFREG , - init_file); + error =sysfs_create (dentry, mode, init_file);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); }else error =PTR_ERR (dentry);
@@ -375,9 +378,10 @@ int sysfs_add_file(struct dentry * dir,int sysfs_create_file (struct kobject * kobj,const struct attribute * attr) { -if (kobj && attr) -return sysfs_add_file (kobj->dentry,attr); -return -EINVAL ; +BUG_ON (!kobj || !kobj->dentry || !attr); + +return sysfs_add_file (kobj->dentry, attr,SYSFS_KOBJ_ATTR ); + }
@@ -409,7 +413,8 @@ int sysfs_update_file(struct kobject * k*/ dput (victim); res =0 ; - } + }else +d_drop (victim);/** * Drop the reference acquired from sysfs_get_dentry() above.
fs/sysfs/bin.c renamed from fs/sysfs/bin.c~sysfs-backing-store-add-sysfs_dirent +8 -6
@@ -160,24 +160,26 @@ int sysfs_create_bin_file(struct kobject {struct dentry * dentry;struct dentry * parent; +umode_t mode = (attr->attr.mode &S_IALLUGO ) |S_IFREG ;int error =0 ; -if (!kobj || !attr) -return -EINVAL ; +BUG_ON (!kobj || !kobj->dentry || !attr); parent = kobj->dentry;down (&parent->d_inode->i_sem); dentry =sysfs_get_dentry (parent,attr->attr.name);if (!IS_ERR (dentry)) { - dentry->d_fsdata = (void *)attr; - error =sysfs_create (dentry, - (attr->attr.mode &S_IALLUGO ) |S_IFREG , -NULL ); + error =sysfs_create (dentry, mode,NULL );if (!error) { dentry->d_inode->i_size = attr->size; 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); }else error =PTR_ERR (dentry);
fs/sysfs/group.c renamed from fs/sysfs/group.c~sysfs-backing-store-add-sysfs_dirent +3 -1
@@ -31,7 +31,7 @@ static int create_files(struct dentry *int error =0 ;for (attr = grp->attrs; *attr && !error; attr++) { - error =sysfs_add_file (dir,*attr); + error =sysfs_add_file (dir, *attr,SYSFS_KOBJ_ATTR ); }if (error)remove_files (dir,grp);
@@ -45,6 +45,8 @@ int sysfs_create_group(struct kobject *struct dentry * dir;int error; +BUG_ON (!kobj || !kobj->dentry); +if (grp->name) { error =sysfs_create_subdir (kobj,grp->name,&dir);if (error)
fs/sysfs/mount.c renamed from fs/sysfs/mount.c~sysfs-backing-store-add-sysfs_dirent +8 -0
@@ -22,6 +22,13 @@ static struct super_operations sysfs_ops .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) {struct inode *inode;
@@ -50,6 +57,7 @@ static int sysfs_fill_super(struct superiput (inode);return -ENOMEM ; } + root->d_fsdata = &sysfs_root; sb->s_root = root;return 0 ; }
fs/sysfs/sysfs.h renamed from fs/sysfs/sysfs.h~sysfs-backing-store-add-sysfs_dirent +33 -6
@@ -4,9 +4,11 @@ extern struct vfsmount * sysfs_mount;extern struct inode *sysfs_new_inode (mode_t mode);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 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 int sysfs_create_subdir (struct kobject *,const char *,struct dentry **);
@@ -16,19 +18,27 @@ extern int sysfs_readlink(struct dentryextern int sysfs_follow_link (struct dentry *,struct nameidata *);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) { -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) { -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) { -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)
@@ -36,10 +46,27 @@ static inline struct kobject *sysfs_get_struct kobject * kobj =NULL ;spin_lock (&dcache_lock); -if (!d_unhashed (dentry)) - kobj =kobject_get (to_kobj (dentry)); +if (!d_unhashed (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);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); +} +
fs/sysfs/symlink.c renamed from fs/sysfs/symlink.c~sysfs-backing-store-add-sysfs_dirent +35 -4
@@ -53,6 +53,36 @@ static void fill_object_path(struct kobj } } +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. * @kobj: object whose directory we're creating the link in.
@@ -65,15 +95,16 @@ int sysfs_create_link(struct kobject * kstruct dentry * d;int error =0 ; +BUG_ON (!kobj || !kobj->dentry || !name); +down (&dentry->d_inode->i_sem); d =sysfs_get_dentry (dentry,name);if (!IS_ERR (d)) { error =sysfs_create (d,S_IFLNK |S_IRWXUGO , init_symlink);if (!error) -/* -* associate the link dentry with the target kobject -*/ - d->d_fsdata =kobject_get (target); + error =sysfs_add_link (d, name, target); +if (error) +d_drop (d);dput (d); }else error =PTR_ERR (d);
fs/sysfs/inode.c renamed from fs/sysfs/inode.c~sysfs-backing-store-add-sysfs_dirent +8 -2
@@ -11,6 +11,8 @@#include <linux/pagemap.h> #include <linux/namei.h> #include <linux/backing-dev.h> +#include "sysfs.h" +extern struct super_block * sysfs_sb;static struct address_space_operations sysfs_aops = {
@@ -91,6 +93,7 @@ struct dentry * sysfs_get_dentry(structvoid sysfs_hash_and_remove (struct dentry * dir,const char * name) {struct dentry * victim; +struct sysfs_dirent * sd;down (&dir->d_inode->i_sem); victim =sysfs_get_dentry (dir,name);
@@ -101,14 +104,17 @@ void sysfs_hash_and_remove(struct dentrypr_debug ("sysfs: Removing %s (%d)\n" , victim->d_name.name,atomic_read (&victim->d_count)); + sd = victim->d_fsdata;d_drop (victim);/* release the target kobject in case of * a symlink */ 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); - } + }else +d_drop (victim);/* * Drop reference from sysfs_get_dentry() above. */
_
Signature · 6 lines
Maneesh Soni Linux Technology Center, IBM Austin email: [email protected] Phone: 1-512-838-1896 Fax: T/L : 6781896
Re: [PATCH 2/4] Use sysfs_dirent based tree in file removal
o This patch uses the sysfs_dirent based tree while removing sysfs files and directories. This avoids holding dcache_lock by not using dentry based vfs tree. Thus simplyfying the removal logic in sysfs.
o It uses two helper routines sysfs_get_name(), to get the name for sysfs element and sysfs_drop_dentry() to delete the dentry given a sysfs_dirent.
fs/sysfs/dir.c | 48 ++++++---------------------- fs/sysfs/inode.c | 92 +++++++++++++++++++++++++++++++++++++++---------------- fs/sysfs/sysfs.h | 17 ++++++++++ 3 files changed, 94 insertions(+), 63 deletions(-)
fs/sysfs/dir.c renamed from fs/sysfs/dir.c~sysfs-backing-store-use-sysfs_dirent-tree-in-removal +11 -37
@@ -19,7 +19,7 @@ static void sysfs_d_iput(struct dentry *if (sd) {BUG_ON (sd->s_dentry != dentry); sd->s_dentry =NULL ; -release_sysfs_dirent (sd); +sysfs_put (sd); }iput (inode); }
@@ -61,7 +61,7 @@ int sysfs_make_dirent(struct sysfs_diren sd->s_mode = mode; sd->s_type = type; sd->s_dentry = dentry; - dentry->d_fsdata = sd; + dentry->d_fsdata =sysfs_get (sd); dentry->d_op = &sysfs_dentry_ops;return 0 ;
@@ -146,6 +146,7 @@ static void remove_dir(struct dentry * dd_delete (d); sd = d->d_fsdata;list_del_init (&sd->s_sibling); +sysfs_put (sd);if (d->d_inode)simple_rmdir (parent->d_inode,d);
@@ -173,49 +174,22 @@ void sysfs_remove_subdir(struct dentry *void sysfs_remove_dir (struct kobject * kobj) { -struct list_head * node;struct dentry * dentry =dget (kobj->dentry); +struct sysfs_dirent * parent_sd = dentry->d_fsdata; +struct sysfs_dirent * sd, * tmp;if (!dentry)return ;pr_debug ("sysfs %s: removing dir\n" ,dentry->d_name.name);down (&dentry->d_inode->i_sem); - -spin_lock (&dcache_lock); -restart: - node = dentry->d_subdirs.next; -while (node != &dentry->d_subdirs) { - struct dentry * d = list_entry(node,struct dentry,d_child); - - node = node->next; -pr_debug (" o %s (%d): " ,d->d_name.name,atomic_read (&d->d_count)); -if (!d_unhashed (d) && (d->d_inode)) { -struct sysfs_dirent * sd = d->d_fsdata; - d =dget_locked (d); -pr_debug ("removing" ); - -/** -* Unlink and unhash. -*/ -__d_drop (d); -spin_unlock (&dcache_lock); -/* release the target kobject in case of -* a symlink -*/ -if (S_ISLNK (d->d_inode->i_mode)) -kobject_put (sd->s_element); - -list_del_init (&sd->s_sibling); -simple_unlink (dentry->d_inode,d); -dput (d); -pr_debug (" done\n" ); -spin_lock (&dcache_lock); -/* re-acquired dcache_lock, need to restart */ - goto restart; - } +list_for_each_entry_safe (sd, tmp, &parent_sd->s_children, s_sibling) { +if (!sd->s_element) +continue ; +list_del_init (&sd->s_sibling); +sysfs_drop_dentry (sd, dentry); +sysfs_put (sd); } -spin_unlock (&dcache_lock);up (&dentry->d_inode->i_sem);remove_dir (dentry);
fs/sysfs/inode.c renamed from fs/sysfs/inode.c~sysfs-backing-store-use-sysfs_dirent-tree-in-removal +66 -26
116 lines not on this page · raw message
fs/sysfs/sysfs.h renamed from fs/sysfs/sysfs.h~sysfs-backing-store-use-sysfs_dirent-tree-in-removal +17 -0
@@ -14,6 +14,8 @@ extern void sysfs_hash_and_remove(structextern int sysfs_create_subdir (struct kobject *,const char *,struct dentry **);extern void sysfs_remove_subdir (struct dentry *); +extern const unsigned char *sysfs_get_name (struct sysfs_dirent *sd); +extern void sysfs_drop_dentry (struct sysfs_dirent * sd,struct dentry * parent);extern int sysfs_readlink (struct dentry *,char __user *,int );extern int sysfs_follow_link (struct dentry *,struct nameidata *);extern struct rw_semaphore sysfs_rename_sem;
@@ -70,3 +72,18 @@ static inline void release_sysfs_dirent(kfree (sd); } +static inline struct sysfs_dirent *sysfs_get (struct sysfs_dirent * sd) +{ +if (sd) { +WARN_ON (!atomic_read (&sd->s_count)); +atomic_inc (&sd->s_count); + } +return sd; +} + +static inline void sysfs_put (struct sysfs_dirent * sd) +{ +if (atomic_dec_and_test (&sd->s_count)) +release_sysfs_dirent (sd); +} +
_
Signature · 6 lines
Maneesh Soni Linux Technology Center, IBM Austin email: [email protected] Phone: 1-512-838-1896 Fax: T/L : 6781896
Re: [PATCH 3/4] Use sysfs_dirent based tree in dir file operations
o This patch implements the sysfs_dir_operations file_operations strucutre for sysfs directories. It uses the sysfs_dirent based tree for ->readdir() and ->lseek() methods instead of simple_dir_operations which use dentry based tree.
fs/sysfs/dir.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- fs/sysfs/mount.c | 2 fs/sysfs/sysfs.h | 2 3 files changed, 143 insertions(+), 2 deletions(-)
fs/sysfs/dir.c renamed from fs/sysfs/dir.c~sysfs-backing-store-use-sysfs_dirent-tree-in-dir-file_operations +140 -1
@@ -70,7 +70,7 @@ int sysfs_make_dirent(struct sysfs_direnstatic int init_dir (struct inode * inode) { inode->i_op = &simple_dir_inode_operations; - inode->i_fop = &simple_dir_operations; + inode->i_fop = &sysfs_dir_operations;/* directory inodes start off with i_nlink == 2 (for "." entry) */ inode->i_nlink++;
@@ -233,6 +233,145 @@ int sysfs_rename_dir(struct kobject * koreturn error; } +static int sysfs_dir_open (struct inode *inode,struct file *file) +{ +struct dentry * dentry = file->f_dentry; +struct sysfs_dirent * parent_sd = dentry->d_fsdata; + +down (&dentry->d_inode->i_sem); + file->private_data =sysfs_new_dirent (parent_sd,NULL ); +up (&dentry->d_inode->i_sem); + +return file->private_data ?0 : -ENOMEM ; + +} + +static int sysfs_dir_close (struct inode *inode,struct file *file) +{ +struct dentry * dentry = file->f_dentry; +struct sysfs_dirent * cursor = file->private_data; + +down (&dentry->d_inode->i_sem); +list_del_init (&cursor->s_sibling); +up (&dentry->d_inode->i_sem); +sysfs_put (file->private_data); + +return 0 ; +} + +/* Relationship between s_mode and the DT_xxx types */ +static inline unsigned char dt_type (struct sysfs_dirent *sd) +{ +return (sd->s_mode >>12 ) &15 ; +} + +static int sysfs_readdir (struct file * filp,void * dirent,filldir_t filldir) +{ +struct dentry *dentry = filp->f_dentry; +struct sysfs_dirent * parent_sd = dentry->d_fsdata; +struct sysfs_dirent *cursor = filp->private_data; +struct list_head *p, *q = &cursor->s_sibling; +ino_t ino; +int i = filp->f_pos; + +switch (i) { +case 0 : + ino = dentry->d_inode->i_ino; +if (filldir (dirent,"." ,1 , i, ino,DT_DIR ) <0 ) +break ; + filp->f_pos++; + i++; +/* fallthrough */ +case 1 : + ino =parent_ino (dentry); +if (filldir (dirent,".." ,2 , i, ino,DT_DIR ) <0 ) +break ; + filp->f_pos++; + i++; +/* fallthrough */ +default : +if (filp->f_pos ==2 ) { +list_del (q); +list_add (q, &parent_sd->s_children); + } +for (p=q->next; p!= &parent_sd->s_children; p=p->next) { +struct sysfs_dirent *next; +const char * name; +int len; + + next = list_entry(p, struct sysfs_dirent, + s_sibling); +if (!next->s_element) +continue ; + + name =sysfs_get_name (next); + len =strlen (name); +if (next->s_dentry) + ino = next->s_dentry->d_inode->i_ino; +else + ino =iunique (sysfs_sb,2 ); + +if (filldir (dirent, name, len, filp->f_pos, ino, +dt_type (next)) <0 ) +return 0 ; + +list_del (q); +list_add (q, p); + p = q; + filp->f_pos++; + } + } +return 0 ; +} + +static loff_t sysfs_dir_lseek (struct file * file,loff_t offset,int origin) +{ +struct dentry * dentry = file->f_dentry; + +down (&dentry->d_inode->i_sem); +switch (origin) { +case 1 : + offset += file->f_pos; +case 0 : +if (offset >=0 ) +break ; +default : +up (&file->f_dentry->d_inode->i_sem); +return -EINVAL ; + } +if (offset != file->f_pos) { + file->f_pos = offset; +if (file->f_pos >=2 ) { +struct sysfs_dirent *sd = dentry->d_fsdata; +struct sysfs_dirent *cursor = file->private_data; +struct list_head *p; +loff_t n = file->f_pos -2 ; + +list_del (&cursor->s_sibling); + p = sd->s_children.next; +while (n && p != &sd->s_children) { +struct sysfs_dirent *next; + next = list_entry(p, struct sysfs_dirent, + s_sibling); +if (next->s_element) + n--; + p = p->next; + } +list_add_tail (&cursor->s_sibling, p); + } + } +up (&dentry->d_inode->i_sem); +return offset; +} + +struct file_operations sysfs_dir_operations = { + .open = sysfs_dir_open, + .release = sysfs_dir_close, + .llseek = sysfs_dir_lseek, + .read = generic_read_dir, + .readdir = sysfs_readdir, +}; +EXPORT_SYMBOL (sysfs_create_dir);EXPORT_SYMBOL (sysfs_remove_dir);EXPORT_SYMBOL (sysfs_rename_dir);
fs/sysfs/mount.c renamed from fs/sysfs/mount.c~sysfs-backing-store-use-sysfs_dirent-tree-in-dir-file_operations +1 -1
@@ -43,7 +43,7 @@ static int sysfs_fill_super(struct super inode =sysfs_new_inode (S_IFDIR |S_IRWXU |S_IRUGO |S_IXUGO );if (inode) { inode->i_op = &simple_dir_inode_operations; - inode->i_fop = &simple_dir_operations; + inode->i_fop = &sysfs_dir_operations;/* directory inodes start off with i_nlink == 2 (for "." entry) */ inode->i_nlink++; }else {
fs/sysfs/sysfs.h renamed from fs/sysfs/sysfs.h~sysfs-backing-store-use-sysfs_dirent-tree-in-dir-file_operations +2 -0
@@ -19,6 +19,8 @@ extern void sysfs_drop_dentry(struct sysextern int sysfs_readlink (struct dentry *,char __user *,int );extern int sysfs_follow_link (struct dentry *,struct nameidata *);extern struct rw_semaphore sysfs_rename_sem; +extern struct super_block * sysfs_sb; +extern struct file_operations sysfs_dir_operations;struct sysfs_symlink {char * link_name;
_
Signature · 6 lines
Maneesh Soni Linux Technology Center, IBM Austin email: [email protected] Phone: 1-512-838-1896 Fax: T/L : 6781896
Re: [PATCH 4/4] Stop pinning dentries/inodes for leaf entries
o This patch stops the pinning of non-directory or leaf dentries and inodes. The leaf dentries and inodes are created during lookup based on the entries on sysfs_dirent tree. These leaves are removed from the dcache through the VFS dentry ageing process during shrink dcache operations. Thus reducing about 80% of sysfs lowmem needs.
o This implments the ->lookup() for sysfs directory inodes and allocates dentry and inode if the lookup is successful and avoids the need of allocating and pinning of dentry and inodes during the creation of corresponding sysfs leaf entry. As of now the implementation has not required negative dentry creation on failed lookup. As sysfs is still a RAM based filesystem, negative dentries are not of any use IMO.
o The leaf dentry allocated after successful lookup is connected to the existing corresponding sysfs_dirent through the d_fsdata field.
sysfs/group.c | 0 fs/sysfs/bin.c | 28 +------------- fs/sysfs/dir.c | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++--- fs/sysfs/file.c | 25 +----------- fs/sysfs/mount.c | 2 - fs/sysfs/symlink.c | 26 ++----------- fs/sysfs/sysfs.h | 4 ++ 7 files changed, 115 insertions(+), 76 deletions(-)
fs/sysfs/dir.c renamed from fs/sysfs/dir.c~sysfs-backing-store-stop-pinning-dentries-inodes-for-leaves +100 -6
@@ -61,15 +61,17 @@ int sysfs_make_dirent(struct sysfs_diren sd->s_mode = mode; sd->s_type = type; sd->s_dentry = dentry; - dentry->d_fsdata =sysfs_get (sd); - dentry->d_op = &sysfs_dentry_ops; +if (dentry) { + dentry->d_fsdata =sysfs_get (sd); + dentry->d_op = &sysfs_dentry_ops; + }return 0 ; }static int init_dir (struct inode * inode) { - inode->i_op = &simple_dir_inode_operations; + inode->i_op = &sysfs_dir_inode_operations; inode->i_fop = &sysfs_dir_operations;/* directory inodes start off with i_nlink == 2 (for "." entry) */
@@ -77,6 +79,18 @@ static int init_dir(struct inode * inodereturn 0 ; } +static int init_file (struct inode * inode) +{ + inode->i_size =PAGE_SIZE ; + inode->i_fop = &sysfs_file_operations; +return 0 ; +} + +static int init_symlink (struct inode * inode) +{ + inode->i_op = &sysfs_symlink_inode_operations; +return 0 ; +}static int create_dir (struct kobject * k,struct dentry * p,const char * n,struct dentry ** d)
@@ -91,8 +105,11 @@ static int create_dir(struct kobject * kif (!error) { error =sysfs_make_dirent (p->d_fsdata, *d, k, mode,SYSFS_DIR ); -if (!error) +if (!error) { p->d_inode->i_nlink++; + (*d)->d_op = &sysfs_dentry_ops; +d_rehash (*d); + } }if (error)d_drop (*d);
@@ -136,6 +153,82 @@ int sysfs_create_dir(struct kobject * koreturn error; } +/* attaches attribute's sysfs_dirent to the dentry corresponding to the +* attribute file +*/ +static int sysfs_attach_attr (struct sysfs_dirent * sd,struct dentry * dentry) +{ +struct attribute * attr =NULL ; +struct bin_attribute * bin_attr =NULL ; +int (* init) (struct inode *) =NULL ; +int error =0 ; + +if (sd->s_type &SYSFS_KOBJ_BIN_ATTR ) { + bin_attr = sd->s_element; + attr = &bin_attr->attr; + }else { + attr = sd->s_element; + init = init_file; + } + + error =sysfs_create (dentry, (attr->mode &S_IALLUGO ) |S_IFREG , init); +if (error) +return error; + +if (bin_attr) { + dentry->d_inode->i_size = bin_attr->size; + dentry->d_inode->i_fop = &bin_fops; + } + dentry->d_op = &sysfs_dentry_ops; + dentry->d_fsdata =sysfs_get (sd); + sd->s_dentry = dentry; +d_rehash (dentry); + +return 0 ; +} + +static int sysfs_attach_link (struct sysfs_dirent * sd,struct dentry * dentry) +{ +int err =0 ; + + err =sysfs_create (dentry,S_IFLNK |S_IRWXUGO , init_symlink); +if (!err) { + dentry->d_op = &sysfs_dentry_ops; + dentry->d_fsdata =sysfs_get (sd); + sd->s_dentry = dentry; +d_rehash (dentry); + } +return err; +} + +struct dentry *sysfs_lookup (struct inode *dir,struct dentry *dentry, +struct nameidata *nd) +{ +struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata; +struct sysfs_dirent * sd; +int err =0 ; + +list_for_each_entry (sd, &parent_sd->s_children, s_sibling) { +if (sd->s_type &SYSFS_NOT_PINNED ) { +const unsigned char * name =sysfs_get_name (sd); + +if (strcmp (name, dentry->d_name.name)) +continue ; + +if (sd->s_type &SYSFS_KOBJ_LINK ) + err =sysfs_attach_link (sd, dentry); +else + err =sysfs_attach_attr (sd, dentry); +break ; + } + } + +return ERR_PTR (err); +} + +struct inode_operations sysfs_dir_inode_operations = { + .lookup = sysfs_lookup, +};static void remove_dir (struct dentry * d) {
@@ -219,8 +312,10 @@ int sysfs_rename_dir(struct kobject * koif (!IS_ERR (new_dentry)) {if (!new_dentry->d_inode) { error =kobject_set_name (kobj,new_name); -if (!error) +if (!error) { +d_add (new_dentry,NULL );d_move (kobj->dentry, new_dentry); + }else d_drop (new_dentry); }else
@@ -254,7 +349,6 @@ static int sysfs_dir_close(struct inodedown (&dentry->d_inode->i_sem);list_del_init (&cursor->s_sibling);up (&dentry->d_inode->i_sem); -sysfs_put (file->private_data);return 0 ; }
fs/sysfs/file.c renamed from fs/sysfs/file.c~sysfs-backing-store-stop-pinning-dentries-inodes-for-leaves +3 -22
@@ -9,15 +9,6 @@#include "sysfs.h" -static struct file_operations sysfs_file_operations; - -static int init_file (struct inode * inode) -{ - inode->i_size =PAGE_SIZE ; - inode->i_fop = &sysfs_file_operations; -return 0 ; -} -#define to_subsys (k) container_of(k,struct subsystem,kset.kobj)#define to_sattr (a) container_of(a,struct subsys_attribute,attr)
@@ -337,7 +328,7 @@ static int sysfs_release(struct inode *return 0 ; } -static struct file_operations sysfs_file_operations = { +struct file_operations sysfs_file_operations = { .read = sysfs_read_file, .write = sysfs_write_file, .llseek = generic_file_llseek,
@@ -348,24 +339,14 @@ static struct file_operations sysfs_fileint sysfs_add_file (struct dentry * dir,const struct attribute * attr,int type) { -struct dentry * dentry;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); - dentry =sysfs_get_dentry (dir,attr->name); -if (!IS_ERR (dentry)) { - error =sysfs_create (dentry, mode, init_file); -if (!error) - error =sysfs_make_dirent (parent_sd, dentry, - (void *) attr, mode, type); -if (error) -d_drop (dentry); -dput (dentry); - }else - error =PTR_ERR (dentry); + error =sysfs_make_dirent (parent_sd,NULL , (void *) attr, mode, type);up (&dir->d_inode->i_sem); +return error; }
fs/sysfs/bin.c renamed from fs/sysfs/bin.c~sysfs-backing-store-stop-pinning-dentries-inodes-for-leaves +2 -26
@@ -141,7 +141,7 @@ static int release(struct inode * inode,return 0 ; } -static struct file_operations bin_fops = { +struct file_operations bin_fops = { .read = read, .write = write, .llseek = generic_file_llseek,
@@ -158,33 +158,9 @@ static struct file_operations bin_fops =int sysfs_create_bin_file (struct kobject * kobj,struct bin_attribute * attr) { -struct dentry * dentry; -struct dentry * parent; -umode_t mode = (attr->attr.mode &S_IALLUGO ) |S_IFREG ; -int error =0 ; -BUG_ON (!kobj || !kobj->dentry || !attr); - parent = kobj->dentry; - -down (&parent->d_inode->i_sem); - dentry =sysfs_get_dentry (parent,attr->attr.name); -if (!IS_ERR (dentry)) { - error =sysfs_create (dentry, mode,NULL ); -if (!error) { - dentry->d_inode->i_size = attr->size; - 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); - }else - error =PTR_ERR (dentry); -up (&parent->d_inode->i_sem); -return error; +return sysfs_add_file (kobj->dentry, &attr->attr,SYSFS_KOBJ_BIN_ATTR ); }
fs/sysfs/sysfs.h renamed from fs/sysfs/sysfs.h~sysfs-backing-store-stop-pinning-dentries-inodes-for-leaves +4 -0
@@ -21,6 +21,10 @@ extern int sysfs_follow_link(struct dentextern struct rw_semaphore sysfs_rename_sem;extern struct super_block * sysfs_sb;extern struct file_operations sysfs_dir_operations; +extern struct file_operations sysfs_file_operations; +extern struct file_operations bin_fops; +extern struct inode_operations sysfs_dir_inode_operations; +extern struct inode_operations sysfs_symlink_inode_operations;struct sysfs_symlink {char * link_name;
fs/sysfs/symlink.c renamed from fs/sysfs/symlink.c~sysfs-backing-store-stop-pinning-dentries-inodes-for-leaves +5 -21
@@ -8,17 +8,11 @@#include "sysfs.h" -static struct inode_operations sysfs_symlink_inode_operations = { +struct inode_operations sysfs_symlink_inode_operations = { .readlink = sysfs_readlink, .follow_link = sysfs_follow_link, }; -static int init_symlink (struct inode * inode) -{ - inode->i_op = &sysfs_symlink_inode_operations; -return 0 ; -} -static int object_depth (struct kobject * kobj) {struct kobject * p = kobj;
@@ -53,9 +47,9 @@ static void fill_object_path(struct kobj } } -static int sysfs_add_link (struct dentry * dentry,char * name,struct kobject * target) +static int sysfs_add_link (struct dentry * parent,char * name,struct kobject * target) { -struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata; +struct sysfs_dirent * parent_sd = parent->d_fsdata;struct sysfs_symlink * sl;int error =0 ;
@@ -71,7 +65,7 @@ static int sysfs_add_link(struct dentrystrcpy (sl->link_name, name); sl->target_kobj =kobject_get (target); - error =sysfs_make_dirent (parent_sd, dentry, sl,S_IFLNK |S_IRWXUGO , + error =sysfs_make_dirent (parent_sd,NULL , sl,S_IFLNK |S_IRWXUGO ,SYSFS_KOBJ_LINK );if (!error)return 0 ;
@@ -92,22 +86,12 @@ exit1:int sysfs_create_link (struct kobject * kobj,struct kobject * target,char * name) {struct dentry * dentry = kobj->dentry; -struct dentry * d;int error =0 ;BUG_ON (!kobj || !kobj->dentry || !name);down (&dentry->d_inode->i_sem); - d =sysfs_get_dentry (dentry,name); -if (!IS_ERR (d)) { - error =sysfs_create (d,S_IFLNK |S_IRWXUGO , init_symlink); -if (!error) - error =sysfs_add_link (d, name, target); -if (error) -d_drop (d); -dput (d); - }else - error =PTR_ERR (d); + error =sysfs_add_link (dentry, name, target);up (&dentry->d_inode->i_sem);return error; }
diff -puN fs/sysfs/group.c~sysfs-backing-store-stop-pinning-dentries-inodes-for-leaves fs/sysfs/group.c
fs/sysfs/mount.c renamed from fs/sysfs/mount.c~sysfs-backing-store-stop-pinning-dentries-inodes-for-leaves +1 -1
@@ -42,7 +42,7 @@ static int sysfs_fill_super(struct super inode =sysfs_new_inode (S_IFDIR |S_IRWXU |S_IRUGO |S_IXUGO );if (inode) { - inode->i_op = &simple_dir_inode_operations; + inode->i_op = &sysfs_dir_inode_operations; inode->i_fop = &sysfs_dir_operations;/* directory inodes start off with i_nlink == 2 (for "." entry) */ inode->i_nlink++;
_
Signature · 6 lines
Maneesh Soni Linux Technology Center, IBM Austin email: [email protected] Phone: 1-512-838-1896 Fax: T/L : 6781896
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.
fs/sysfs/bin.c | 14 ++++---- fs/sysfs/dir.c | 84 ++++++++++++++++++++++++++++++++++++++++++++------ fs/sysfs/file.c | 25 ++++++++------ fs/sysfs/group.c | 4 +- fs/sysfs/inode.c | 10 ++++- fs/sysfs/mount.c | 8 ++++ fs/sysfs/symlink.c | 39 ++++++++++++++++++++--- fs/sysfs/sysfs.h | 39 +++++++++++++++++++---- include/linux/sysfs.h | 19 +++++++++++ 9 files changed, 204 insertions(+), 38 deletions(-)
include/linux/sysfs.h renamed from include/linux/sysfs.h~sysfs-backing-store-add-sysfs_dirent +19 -0
@@ -9,6 +9,8 @@#ifndef _SYSFS_H_#define _SYSFS_H_ +#include <asm/atomic.h> +struct kobject ;struct module ;
@@ -57,6 +59,23 @@ struct sysfs_ops {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 extern int
fs/sysfs/dir.c renamed from fs/sysfs/dir.c~sysfs-backing-store-add-sysfs_dirent +75 -9
135 lines not on this page · raw message
fs/sysfs/file.c renamed from fs/sysfs/file.c~sysfs-backing-store-add-sysfs_dirent +15 -10
@@ -346,19 +346,22 @@ static struct file_operations sysfs_file }; -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; -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); dentry =sysfs_get_dentry (dir,attr->name);if (!IS_ERR (dentry)) { - error =sysfs_create (dentry, - (attr->mode &S_IALLUGO ) |S_IFREG , - init_file); + error =sysfs_create (dentry, mode, init_file);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); }else error =PTR_ERR (dentry);
@@ -375,9 +378,10 @@ int sysfs_add_file(struct dentry * dir,int sysfs_create_file (struct kobject * kobj,const struct attribute * attr) { -if (kobj && attr) -return sysfs_add_file (kobj->dentry,attr); -return -EINVAL ; +BUG_ON (!kobj || !kobj->dentry || !attr); + +return sysfs_add_file (kobj->dentry, attr,SYSFS_KOBJ_ATTR ); + }
@@ -409,7 +413,8 @@ int sysfs_update_file(struct kobject * k*/ dput (victim); res =0 ; - } + }else +d_drop (victim);/** * Drop the reference acquired from sysfs_get_dentry() above.
fs/sysfs/bin.c renamed from fs/sysfs/bin.c~sysfs-backing-store-add-sysfs_dirent +8 -6
@@ -160,24 +160,26 @@ int sysfs_create_bin_file(struct kobject {struct dentry * dentry;struct dentry * parent; +umode_t mode = (attr->attr.mode &S_IALLUGO ) |S_IFREG ;int error =0 ; -if (!kobj || !attr) -return -EINVAL ; +BUG_ON (!kobj || !kobj->dentry || !attr); parent = kobj->dentry;down (&parent->d_inode->i_sem); dentry =sysfs_get_dentry (parent,attr->attr.name);if (!IS_ERR (dentry)) { - dentry->d_fsdata = (void *)attr; - error =sysfs_create (dentry, - (attr->attr.mode &S_IALLUGO ) |S_IFREG , -NULL ); + error =sysfs_create (dentry, mode,NULL );if (!error) { dentry->d_inode->i_size = attr->size; 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); }else error =PTR_ERR (dentry);
fs/sysfs/group.c renamed from fs/sysfs/group.c~sysfs-backing-store-add-sysfs_dirent +3 -1
@@ -31,7 +31,7 @@ static int create_files(struct dentry *int error =0 ;for (attr = grp->attrs; *attr && !error; attr++) { - error =sysfs_add_file (dir,*attr); + error =sysfs_add_file (dir, *attr,SYSFS_KOBJ_ATTR ); }if (error)remove_files (dir,grp);
@@ -45,6 +45,8 @@ int sysfs_create_group(struct kobject *struct dentry * dir;int error; +BUG_ON (!kobj || !kobj->dentry); +if (grp->name) { error =sysfs_create_subdir (kobj,grp->name,&dir);if (error)
fs/sysfs/mount.c renamed from fs/sysfs/mount.c~sysfs-backing-store-add-sysfs_dirent +8 -0
@@ -22,6 +22,13 @@ static struct super_operations sysfs_ops .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) {struct inode *inode;
@@ -50,6 +57,7 @@ static int sysfs_fill_super(struct superiput (inode);return -ENOMEM ; } + root->d_fsdata = &sysfs_root; sb->s_root = root;return 0 ; }
fs/sysfs/sysfs.h renamed from fs/sysfs/sysfs.h~sysfs-backing-store-add-sysfs_dirent +33 -6
@@ -4,9 +4,11 @@ extern struct vfsmount * sysfs_mount;extern struct inode *sysfs_new_inode (mode_t mode);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 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 int sysfs_create_subdir (struct kobject *,const char *,struct dentry **);
@@ -16,19 +18,27 @@ extern int sysfs_readlink(struct dentryextern int sysfs_follow_link (struct dentry *,struct nameidata *);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) { -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) { -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) { -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)
@@ -36,10 +46,27 @@ static inline struct kobject *sysfs_get_struct kobject * kobj =NULL ;spin_lock (&dcache_lock); -if (!d_unhashed (dentry)) - kobj =kobject_get (to_kobj (dentry)); +if (!d_unhashed (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);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); +} +
fs/sysfs/symlink.c renamed from fs/sysfs/symlink.c~sysfs-backing-store-add-sysfs_dirent +35 -4
@@ -53,6 +53,36 @@ static void fill_object_path(struct kobj } } +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. * @kobj: object whose directory we're creating the link in.
@@ -65,15 +95,16 @@ int sysfs_create_link(struct kobject * kstruct dentry * d;int error =0 ; +BUG_ON (!kobj || !kobj->dentry || !name); +down (&dentry->d_inode->i_sem); d =sysfs_get_dentry (dentry,name);if (!IS_ERR (d)) { error =sysfs_create (d,S_IFLNK |S_IRWXUGO , init_symlink);if (!error) -/* -* associate the link dentry with the target kobject -*/ - d->d_fsdata =kobject_get (target); + error =sysfs_add_link (d, name, target); +if (error) +d_drop (d);dput (d); }else error =PTR_ERR (d);
fs/sysfs/inode.c renamed from fs/sysfs/inode.c~sysfs-backing-store-add-sysfs_dirent +8 -2
@@ -11,6 +11,8 @@#include <linux/pagemap.h> #include <linux/namei.h> #include <linux/backing-dev.h> +#include "sysfs.h" +extern struct super_block * sysfs_sb;static struct address_space_operations sysfs_aops = {
@@ -91,6 +93,7 @@ struct dentry * sysfs_get_dentry(structvoid sysfs_hash_and_remove (struct dentry * dir,const char * name) {struct dentry * victim; +struct sysfs_dirent * sd;down (&dir->d_inode->i_sem); victim =sysfs_get_dentry (dir,name);
@@ -101,14 +104,17 @@ void sysfs_hash_and_remove(struct dentrypr_debug ("sysfs: Removing %s (%d)\n" , victim->d_name.name,atomic_read (&victim->d_count)); + sd = victim->d_fsdata;d_drop (victim);/* release the target kobject in case of * a symlink */ 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); - } + }else +d_drop (victim);/* * Drop reference from sysfs_get_dentry() above. */
_
:--
Maneesh Soni
Linux Technology Center,
IBM Austin
email: [email protected]
Phone: 1-512-838-1896 Fax:
T/L : 6781896