Skip to contentSkip to conversation

fix-sysfs-symlinks.patch

2.6.6-mm3 Applied Maneesh Soni 3 participants 3 messages May 17, 2004 Driver core

lore
Share & tools
lore
#0 lore raw

Hi Andrew,

I have fixed the rejects and re-diffed the following patch. There are no code changes as such.

Thanks
Maneesh

- Rediffed the patch for 2.6.6-mm3 to fix rejects in the latest changes in sysfs code.

o The symlinks code in sysfs doesnot point to the correct target kobject whenever target kobject is renamed and suffers from dangling symlinks if target kobject is removed.

o The following patch implements ->readlink and ->follow_link operations for sysfs instead of using the page_symlink_inode_operations. The pointer to target kobject is saved in the link dentry's d_fsdata field. The target path is generated everytime we do ->readlink and ->follow_link. This results in generating the correct target path during readlink and follow_link operations inspite of renamed target kobject.

o This also pins the target kobject during link creation and the ref. is released when the link is removed.

o Apart from being correct this patch also saves some memory by not pinning a whole page for saving the target information.

o Used a rw_semaphor sysfs_rename_sem to avoid clashing with renaming of ancestors while the target path is generated.

o Used dcache_lock in fs/sysfs/sysfs.h:sysfs_get_kobject() because of using d_drop() while removing dentries.

 fs/sysfs/dir.c     |   12 ++++
 fs/sysfs/inode.c   |    7 ++
 fs/sysfs/symlink.c |  135 ++++++++++++++++++++++++++++++++++++-----------------
 fs/sysfs/sysfs.h   |    7 +-
 4 files changed, 116 insertions(+), 45 deletions(-)
fs/sysfs/dir.c renamed from fs/sysfs/dir.c~fix-sysfs-symlinks +11 -1
diff -puN fs/sysfs/dir.c~fix-sysfs-symlinks fs/sysfs/dir.c
@@ -10,6 +10,8 @@
 #include <linux/kobject.h>
 #include "sysfs.h"
 
+DECLARE_RWSEM(sysfs_rename_sem);
+
 static int init_dir(struct inode * inode)
 {
 	inode->i_op = &simple_dir_inode_operations;
@@ -134,8 +136,14 @@ restart:
 			/**
 			 * Unlink and unhash.
 			 */
+			__d_drop(d);
 			spin_unlock(&dcache_lock);
-			d_delete(d);
+			/* release the target kobject in case of 
+			 * a symlink
+			 */
+			if (S_ISLNK(d->d_inode->i_mode))
+				kobject_put(d->d_fsdata);
+			
 			simple_unlink(dentry->d_inode,d);
 			dput(d);
 			pr_debug(" done\n");
@@ -165,6 +173,7 @@ int sysfs_rename_dir(struct kobject * ko
 	if (!kobj->parent)
 		return -EINVAL;
 
+	down_write(&sysfs_rename_sem);
 	parent = kobj->parent->dentry;
 
 	down(&parent->d_inode->i_sem);
@@ -179,6 +188,7 @@ int sysfs_rename_dir(struct kobject * ko
 		dput(new_dentry);
 	}
 	up(&parent->d_inode->i_sem);	
+	up_write(&sysfs_rename_sem);
 
 	return error;
 }
fs/sysfs/inode.c renamed from fs/sysfs/inode.c~fix-sysfs-symlinks +6 -1
diff -puN fs/sysfs/inode.c~fix-sysfs-symlinks fs/sysfs/inode.c
@@ -96,7 +96,12 @@ void sysfs_hash_and_remove(struct dentry
 			pr_debug("sysfs: Removing %s (%d)\n", victim->d_name.name,
 				 atomic_read(&victim->d_count));
 
-			d_delete(victim);
+			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);
 			simple_unlink(dir->d_inode,victim);
 		}
 		/*
fs/sysfs/symlink.c renamed from fs/sysfs/symlink.c~fix-sysfs-symlinks +94 -41

164 lines not on this page · raw message

fs/sysfs/sysfs.h renamed from fs/sysfs/sysfs.h~fix-sysfs-symlinks +5 -2
diff -puN fs/sysfs/sysfs.h~fix-sysfs-symlinks fs/sysfs/sysfs.h
@@ -12,15 +12,18 @@ extern void sysfs_hash_and_remove(struct
 extern int sysfs_create_subdir(struct kobject *, const char *, struct dentry **);
 extern void sysfs_remove_subdir(struct dentry *);
 
+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;
 
 static inline struct kobject *sysfs_get_kobject(struct dentry *dentry)
 {
 	struct kobject * kobj = NULL;
 
-	spin_lock(&dentry->d_lock);
+	spin_lock(&dcache_lock);
 	if (!d_unhashed(dentry))
 		kobj = kobject_get(dentry->d_fsdata);
-	spin_unlock(&dentry->d_lock);
+	spin_unlock(&dcache_lock);
 
 	return kobj;
 }

_

Signature · 6 lines
Maneesh Soni
Linux Technology Center, 
IBM Software Lab, Bangalore, India
email: [email protected]
Phone: 91-80-25044999 Fax: 91-80-25268553
T/L : 9243696
#1 lore raw
Quoted 4 lines · Maneesh Soni

Hi Andrew,

I have fixed the rejects and re-diffed the following patch. There are no code changes as such.

ACK. Let's merge that puppy (and my vote is to merge it upstream) and move on to the rest of patchset.

Applied
#2 lore raw
In reply to Maneesh Soni
Quoted 4 lines · Maneesh Soni

Hi Andrew,

I have fixed the rejects and re-diffed the following patch. There are no code changes as such.

Applied to my trees, thanks. I'll send this to Linus in a day or so.

greg k-h