aboutsummaryrefslogtreecommitdiffstats
path: root/fs/sysfs
diff options
context:
space:
mode:
authorTejun Heo <htejun@gmail.com>2007-06-13 14:45:14 -0400
committerGreg Kroah-Hartman <gregkh@suse.de>2007-07-11 19:09:04 -0400
commit13b3086d2ea483cbcae5a4236446cecc082a72cf (patch)
treef6932aaf486a302aa7b03857d780bafda33db2d6 /fs/sysfs
parenta26cd7226c24c3be5dd5f48a74832fe64beb8489 (diff)
sysfs: add sysfs_dirent->s_parent
Add sysfs_dirent->s_parent. With this patch, each sd points to and holds a reference to its parent. This allows walking sysfs tree without referencing sd->s_dentry which can go away anytime if the user doesn't control when it's deleted. sd->s_parent is initialized and parent is referenced in sysfs_attach_dirent(). Reference to parent is released when the sd is released, so as long as reference to a sd is held, s_parent can be followed. dentry walk in sysfs_readdir() is convereted to s_parent walk. This will be used to reimplement symlink such that it uses only sysfs_dirent tree. Signed-off-by: Tejun Heo <htejun@gmail.com> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'fs/sysfs')
-rw-r--r--fs/sysfs/dir.c30
-rw-r--r--fs/sysfs/mount.c1
-rw-r--r--fs/sysfs/sysfs.h1
3 files changed, 25 insertions, 7 deletions
diff --git a/fs/sysfs/dir.c b/fs/sysfs/dir.c
index f16aa7e3eafc..5d50e1ddfbd1 100644
--- a/fs/sysfs/dir.c
+++ b/fs/sysfs/dir.c
@@ -47,6 +47,11 @@ static void sysfs_free_ino(ino_t ino)
47 47
48void release_sysfs_dirent(struct sysfs_dirent * sd) 48void release_sysfs_dirent(struct sysfs_dirent * sd)
49{ 49{
50 struct sysfs_dirent *parent_sd;
51
52 repeat:
53 parent_sd = sd->s_parent;
54
50 if (sd->s_type & SYSFS_KOBJ_LINK) { 55 if (sd->s_type & SYSFS_KOBJ_LINK) {
51 struct sysfs_symlink * sl = sd->s_element; 56 struct sysfs_symlink * sl = sd->s_element;
52 kfree(sl->link_name); 57 kfree(sl->link_name);
@@ -56,6 +61,10 @@ void release_sysfs_dirent(struct sysfs_dirent * sd)
56 kfree(sd->s_iattr); 61 kfree(sd->s_iattr);
57 sysfs_free_ino(sd->s_ino); 62 sysfs_free_ino(sd->s_ino);
58 kmem_cache_free(sysfs_dir_cachep, sd); 63 kmem_cache_free(sysfs_dir_cachep, sd);
64
65 sd = parent_sd;
66 if (sd && atomic_dec_and_test(&sd->s_count))
67 goto repeat;
59} 68}
60 69
61static void sysfs_d_iput(struct dentry * dentry, struct inode * inode) 70static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
@@ -119,8 +128,10 @@ void sysfs_attach_dirent(struct sysfs_dirent *sd,
119 dentry->d_op = &sysfs_dentry_ops; 128 dentry->d_op = &sysfs_dentry_ops;
120 } 129 }
121 130
122 if (parent_sd) 131 if (parent_sd) {
132 sd->s_parent = sysfs_get(parent_sd);
123 list_add(&sd->s_sibling, &parent_sd->s_children); 133 list_add(&sd->s_sibling, &parent_sd->s_children);
134 }
124} 135}
125 136
126/* 137/*
@@ -571,7 +582,10 @@ static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
571 i++; 582 i++;
572 /* fallthrough */ 583 /* fallthrough */
573 case 1: 584 case 1:
574 ino = parent_ino(dentry); 585 if (parent_sd->s_parent)
586 ino = parent_sd->s_parent->s_ino;
587 else
588 ino = parent_sd->s_ino;
575 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) 589 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
576 break; 590 break;
577 filp->f_pos++; 591 filp->f_pos++;
@@ -688,13 +702,13 @@ int sysfs_make_shadowed_dir(struct kobject *kobj,
688 702
689struct dentry *sysfs_create_shadow_dir(struct kobject *kobj) 703struct dentry *sysfs_create_shadow_dir(struct kobject *kobj)
690{ 704{
705 struct dentry *dir = kobj->dentry;
706 struct inode *inode = dir->d_inode;
707 struct dentry *parent = dir->d_parent;
708 struct sysfs_dirent *parent_sd = parent->d_fsdata;
709 struct dentry *shadow;
691 struct sysfs_dirent *sd; 710 struct sysfs_dirent *sd;
692 struct dentry *parent, *dir, *shadow;
693 struct inode *inode;
694 711
695 dir = kobj->dentry;
696 inode = dir->d_inode;
697 parent = dir->d_parent;
698 shadow = ERR_PTR(-EINVAL); 712 shadow = ERR_PTR(-EINVAL);
699 if (!sysfs_is_shadowed_inode(inode)) 713 if (!sysfs_is_shadowed_inode(inode))
700 goto out; 714 goto out;
@@ -706,6 +720,8 @@ struct dentry *sysfs_create_shadow_dir(struct kobject *kobj)
706 sd = sysfs_new_dirent(kobj, inode->i_mode, SYSFS_DIR); 720 sd = sysfs_new_dirent(kobj, inode->i_mode, SYSFS_DIR);
707 if (!sd) 721 if (!sd)
708 goto nomem; 722 goto nomem;
723 /* point to parent_sd but don't attach to it */
724 sd->s_parent = sysfs_get(parent_sd);
709 sysfs_attach_dirent(sd, NULL, shadow); 725 sysfs_attach_dirent(sd, NULL, shadow);
710 726
711 d_instantiate(shadow, igrab(inode)); 727 d_instantiate(shadow, igrab(inode));
diff --git a/fs/sysfs/mount.c b/fs/sysfs/mount.c
index 00ab9125d398..31c1fc67f604 100644
--- a/fs/sysfs/mount.c
+++ b/fs/sysfs/mount.c
@@ -28,6 +28,7 @@ static const struct super_operations sysfs_ops = {
28}; 28};
29 29
30static struct sysfs_dirent sysfs_root = { 30static struct sysfs_dirent sysfs_root = {
31 .s_count = ATOMIC_INIT(1),
31 .s_sibling = LIST_HEAD_INIT(sysfs_root.s_sibling), 32 .s_sibling = LIST_HEAD_INIT(sysfs_root.s_sibling),
32 .s_children = LIST_HEAD_INIT(sysfs_root.s_children), 33 .s_children = LIST_HEAD_INIT(sysfs_root.s_children),
33 .s_element = NULL, 34 .s_element = NULL,
diff --git a/fs/sysfs/sysfs.h b/fs/sysfs/sysfs.h
index f8f49cc5c852..ce05d6fd7522 100644
--- a/fs/sysfs/sysfs.h
+++ b/fs/sysfs/sysfs.h
@@ -1,5 +1,6 @@
1struct sysfs_dirent { 1struct sysfs_dirent {
2 atomic_t s_count; 2 atomic_t s_count;
3 struct sysfs_dirent * s_parent;
3 struct list_head s_sibling; 4 struct list_head s_sibling;
4 struct list_head s_children; 5 struct list_head s_children;
5 void * s_element; 6 void * s_element;