Skip to contentSkip to conversation

sysfs: Remove first pass at shadow directory support

PATCH Applied Eric W. Biederman 7 participants 79 messages Jul 19, 2007 – Aug 6, 2007 64 messages not in the archive

Subsystems (3)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): 1182446577.8138.29.camel@localhost

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#9 lore raw

PATCH 1/4 sysfs: Remove first pass at shadow directory support

While shadow directories appear to be a good idea, the current scheme of controlling their creation and destruction outside of sysfs appears to be a locking and maintenance nightmare in the face of sysfs directories dynamically coming and going. Which can now occur for directories containing network devices when CONFIG_SYSFS_DEPRECATED is not set.

This patch removes everything from the initial shadow directory support that allowed the shadow directory creation to be controlled at a higher level. So except for a few bits of sysfs_rename_dir everything from commit b592fcfe7f06c15ec11774b5be7ce0de3aa86e73 is now gone.

---
This patchset is against 589f1e81bde732dd0b1bc5d01b6bddd4bcb4527b in Linus's tree. I took a quick skim through your tree and all of the sysfs patches appear to be merged.

Sorry for taking so long getting this done but the locking changes in the last round for Tejun were significant. Plus there was some additional work needed to actually test the shadow directory support.

 fs/sysfs/dir.c          |  167 ++++++----------------------------------------
 fs/sysfs/group.c        |    1 -
 fs/sysfs/inode.c        |   10 ---
 fs/sysfs/mount.c        |    2 +-
 fs/sysfs/sysfs.h        |    6 --
 include/linux/kobject.h |    5 --
 include/linux/sysfs.h   |   27 +-------
 lib/kobject.c           |   44 ++-----------
 8 files changed, 33 insertions(+), 229 deletions(-)
fs/sysfs/dir.c +22 -145
index 048e605..f88130c 100644
@@ -569,9 +569,6 @@ static void sysfs_drop_dentry(struct sysfs_dirent *sd)
 	spin_unlock(&dcache_lock);
 	spin_unlock(&sysfs_assoc_lock);
 
-	/* dentries for shadowed inodes are pinned, unpin */
-	if (dentry && sysfs_is_shadowed_inode(dentry->d_inode))
-		dput(dentry);
 	dput(dentry);
 
 	/* adjust nlink and update timestamp */
@@ -723,19 +720,15 @@ int sysfs_create_subdir(struct kobject *kobj, const char *name,
 /**
  *	sysfs_create_dir - create a directory for an object.
  *	@kobj:		object we're creating directory for. 
- *	@shadow_parent:	parent object.
  */
-int sysfs_create_dir(struct kobject *kobj,
-		     struct sysfs_dirent *shadow_parent_sd)
+int sysfs_create_dir(struct kobject * kobj)
 {
 	struct sysfs_dirent *parent_sd, *sd;
 	int error = 0;
 
 	BUG_ON(!kobj);
 
-	if (shadow_parent_sd)
-		parent_sd = shadow_parent_sd;
-	else if (kobj->parent)
+	if (kobj->parent)
 		parent_sd = kobj->parent->sd;
 	else if (sysfs_mount && sysfs_mount->mnt_sb)
 		parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata;
@@ -887,45 +880,44 @@ void sysfs_remove_dir(struct kobject * kobj)
 	__sysfs_remove_dir(sd);
 }
 
-int sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd,
-		     const char *new_name)
+int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
 {
-	struct sysfs_dirent *sd = kobj->sd;
-	struct dentry *new_parent = NULL;
+	struct sysfs_dirent *sd;
+	struct dentry *parent = NULL;
 	struct dentry *old_dentry = NULL, *new_dentry = NULL;
+	struct sysfs_dirent *parent_sd;
 	const char *dup_name = NULL;
 	int error;
 
+	if (!kobj->parent)
+		return -EINVAL;
+
 	/* get dentries */
+	sd = kobj->sd;
 	old_dentry = sysfs_get_dentry(sd);
 	if (IS_ERR(old_dentry)) {
 		error = PTR_ERR(old_dentry);
 		goto out_dput;
 	}
 
-	new_parent = sysfs_get_dentry(new_parent_sd);
-	if (IS_ERR(new_parent)) {
-		error = PTR_ERR(new_parent);
+	parent_sd = kobj->parent->sd;
+	parent = sysfs_get_dentry(parent_sd);
+	if (IS_ERR(parent)) {
+		error = PTR_ERR(parent);
 		goto out_dput;
 	}
 
-	/* lock new_parent and get dentry for new name */
-	mutex_lock(&new_parent->d_inode->i_mutex);
+	/* lock parent and get dentry for new name */
+	mutex_lock(&parent->d_inode->i_mutex);
 
-	new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
+	new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
 	if (IS_ERR(new_dentry)) {
 		error = PTR_ERR(new_dentry);
 		goto out_unlock;
 	}
 
-	/* By allowing two different directories with the same
-	 * d_parent we allow this routine to move between different
-	 * shadows of the same directory
-	 */
 	error = -EINVAL;
-	if (old_dentry->d_parent->d_inode != new_parent->d_inode ||
-	    new_dentry->d_parent->d_inode != new_parent->d_inode ||
-	    old_dentry == new_dentry)
+	if (old_dentry == new_dentry)
 		goto out_unlock;
 
 	error = -EEXIST;
@@ -952,9 +944,9 @@ int sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd,
 	mutex_lock(&sysfs_mutex);
 
 	sysfs_unlink_sibling(sd);
-	sysfs_get(new_parent_sd);
+	sysfs_get(parent_sd);
 	sysfs_put(sd->s_parent);
-	sd->s_parent = new_parent_sd;
+	sd->s_parent = parent_sd;
 	sysfs_link_sibling(sd);
 
 	mutex_unlock(&sysfs_mutex);
@@ -965,10 +957,10 @@ int sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd,
  out_drop:
 	d_drop(new_dentry);
  out_unlock:
-	mutex_unlock(&new_parent->d_inode->i_mutex);
+	mutex_unlock(&parent->d_inode->i_mutex);
  out_dput:
 	kfree(dup_name);
-	dput(new_parent);
+	dput(parent);
 	dput(old_dentry);
 	dput(new_dentry);
 	return error;
@@ -1189,121 +1181,6 @@ static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
 	return offset;
 }
 
-
-/**
- *	sysfs_make_shadowed_dir - Setup so a directory can be shadowed
- *	@kobj:	object we're creating shadow of.
- */
-
-int sysfs_make_shadowed_dir(struct kobject *kobj,
-	void * (*follow_link)(struct dentry *, struct nameidata *))
-{
-	struct dentry *dentry;
-	struct inode *inode;
-	struct inode_operations *i_op;
-
-	/* get dentry for @kobj->sd, dentry of a shadowed dir is pinned */
-	dentry = sysfs_get_dentry(kobj->sd);
-	if (IS_ERR(dentry))
-		return PTR_ERR(dentry);
-
-	inode = dentry->d_inode;
-	if (inode->i_op != &sysfs_dir_inode_operations) {
-		dput(dentry);
-		return -EINVAL;
-	}
-
-	i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
-	if (!i_op)
-		return -ENOMEM;
-
-	memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
-	i_op->follow_link = follow_link;
-
-	/* Locking of inode->i_op?
-	 * Since setting i_op is a single word write and they
-	 * are atomic we should be ok here.
-	 */
-	inode->i_op = i_op;
-	return 0;
-}
-
-/**
- *	sysfs_create_shadow_dir - create a shadow directory for an object.
- *	@kobj:	object we're creating directory for.
- *
- *	sysfs_make_shadowed_dir must already have been called on this
- *	directory.
- */
-
-struct sysfs_dirent *sysfs_create_shadow_dir(struct kobject *kobj)
-{
-	struct sysfs_dirent *parent_sd = kobj->sd->s_parent;
-	struct dentry *dir, *parent, *shadow;
-	struct inode *inode;
-	struct sysfs_dirent *sd;
-	struct sysfs_addrm_cxt acxt;
-
-	dir = sysfs_get_dentry(kobj->sd);
-	if (IS_ERR(dir)) {
-		sd = (void *)dir;
-		goto out;
-	}
-	parent = dir->d_parent;
-
-	inode = dir->d_inode;
-	sd = ERR_PTR(-EINVAL);
-	if (!sysfs_is_shadowed_inode(inode))
-		goto out_dput;
-
-	shadow = d_alloc(parent, &dir->d_name);
-	if (!shadow)
-		goto nomem;
-
-	sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR);
-	if (!sd)
-		goto nomem;
-	sd->s_elem.dir.kobj = kobj;
-
-	sysfs_addrm_start(&acxt, parent_sd);
-
-	/* add but don't link into children list */
-	sysfs_add_one(&acxt, sd);
-
-	/* attach and instantiate dentry */
-	sysfs_attach_dentry(sd, shadow);
-	d_instantiate(shadow, igrab(inode));
-	inc_nlink(inode);	/* tj: synchronization? */
-
-	sysfs_addrm_finish(&acxt);
-
-	dget(shadow);		/* Extra count - pin the dentry in core */
-
-	goto out_dput;
-
- nomem:
-	dput(shadow);
-	sd = ERR_PTR(-ENOMEM);
- out_dput:
-	dput(dir);
- out:
-	return sd;
-}
-
-/**
- *	sysfs_remove_shadow_dir - remove an object's directory.
- *	@shadow_sd: sysfs_dirent of shadow directory
- *
- *	The only thing special about this is that we remove any files in
- *	the directory before we remove the directory, and we've inlined
- *	what used to be sysfs_rmdir() below, instead of calling separately.
- */
-
-void sysfs_remove_shadow_dir(struct sysfs_dirent *shadow_sd)
-{
-	__sysfs_remove_dir(shadow_sd);
-}
-
 const struct file_operations sysfs_dir_operations = {
 	.open		= sysfs_dir_open,
 	.release	= sysfs_dir_close,
fs/sysfs/group.c +0 -1
index f318b73..4606f7c 100644
@@ -13,7 +13,6 @@
 #include <linux/dcache.h>
 #include <linux/namei.h>
 #include <linux/err.h>
-#include <linux/fs.h>
 #include <asm/semaphore.h>
 #include "sysfs.h"
 
fs/sysfs/inode.c +0 -10
index 10d1b52..9671164 100644
@@ -34,16 +34,6 @@ static const struct inode_operations sysfs_inode_operations ={
 	.setattr	= sysfs_setattr,
 };
 
-void sysfs_delete_inode(struct inode *inode)
-{
-	/* Free the shadowed directory inode operations */
-	if (sysfs_is_shadowed_inode(inode)) {
-		kfree(inode->i_op);
-		inode->i_op = NULL;
-	}
-	return generic_delete_inode(inode);
-}
-
 int sysfs_setattr(struct dentry * dentry, struct iattr * iattr)
 {
 	struct inode * inode = dentry->d_inode;
fs/sysfs/mount.c +1 -1
index 60714d0..e2073e6 100644
@@ -21,7 +21,7 @@ struct kmem_cache *sysfs_dir_cachep;
 
 static const struct super_operations sysfs_ops = {
 	.statfs		= simple_statfs,
-	.drop_inode	= sysfs_delete_inode,
+	.drop_inode	= generic_delete_inode,
 };
 
 struct sysfs_dirent sysfs_root = {
fs/sysfs/sysfs.h +0 -6
index 6b8c8d7..b55e510 100644
@@ -70,7 +70,6 @@ extern void sysfs_remove_one(struct sysfs_addrm_cxt *acxt,
 			     struct sysfs_dirent *sd);
 extern int sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt);
 
-extern void sysfs_delete_inode(struct inode *inode);
 extern struct inode * sysfs_get_inode(struct sysfs_dirent *sd);
 extern void sysfs_instantiate(struct dentry *dentry, struct inode *inode);
 
@@ -121,8 +120,3 @@ static inline void sysfs_put(struct sysfs_dirent * sd)
 	if (sd && atomic_dec_and_test(&sd->s_count))
 		release_sysfs_dirent(sd);
 }
-
-static inline int sysfs_is_shadowed_inode(struct inode *inode)
-{
-	return S_ISDIR(inode->i_mode) && inode->i_op->follow_link;
-}
include/linux/kobject.h +0 -5
index aa2fe22..7108a3e 100644
@@ -80,14 +80,9 @@ extern void kobject_init(struct kobject *);
 extern void kobject_cleanup(struct kobject *);
 
 extern int __must_check kobject_add(struct kobject *);
-extern int __must_check kobject_shadow_add(struct kobject *kobj,
-					   struct sysfs_dirent *shadow_parent);
 extern void kobject_del(struct kobject *);
 
 extern int __must_check kobject_rename(struct kobject *, const char *new_name);
-extern int __must_check kobject_shadow_rename(struct kobject *kobj,
-					      struct sysfs_dirent *new_parent,
-					      const char *new_name);
 extern int __must_check kobject_move(struct kobject *, struct kobject *);
 
 extern int __must_check kobject_register(struct kobject *);
include/linux/sysfs.h +4 -23
index be8228e..c16e4c5 100644
@@ -17,9 +17,6 @@
 
 struct kobject;
 struct module;
-struct nameidata;
-struct dentry;
-struct sysfs_dirent;
 
 /* FIXME
  * The *owner field is no longer used, but leave around
@@ -94,14 +91,13 @@ extern int sysfs_schedule_callback(struct kobject *kobj,
 		void (*func)(void *), void *data, struct module *owner);
 
 extern int __must_check
-sysfs_create_dir(struct kobject *kobj, struct sysfs_dirent *shadow_parent_sd);
+sysfs_create_dir(struct kobject *);
 
 extern void
 sysfs_remove_dir(struct kobject *);
 
 extern int __must_check
-sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd,
-		 const char *new_name);
+sysfs_rename_dir(struct kobject *kobj, const char *new_name);
 
 extern int __must_check
 sysfs_move_dir(struct kobject *, struct kobject *);
@@ -138,12 +134,6 @@ void sysfs_remove_file_from_group(struct kobject *kobj,
 
 void sysfs_notify(struct kobject * k, char *dir, char *attr);
 
-
-extern int sysfs_make_shadowed_dir(struct kobject *kobj,
-	void * (*follow_link)(struct dentry *, struct nameidata *));
-extern struct sysfs_dirent *sysfs_create_shadow_dir(struct kobject *kobj);
-extern void sysfs_remove_shadow_dir(struct sysfs_dirent *shadow_sd);
-
 extern int __must_check sysfs_init(void);
 
 #else /* CONFIG_SYSFS */
@@ -154,8 +144,7 @@ static inline int sysfs_schedule_callback(struct kobject *kobj,
 	return -ENOSYS;
 }
 
-static inline int sysfs_create_dir(struct kobject *kobj,
-				   struct sysfs_dirent *shadow_parent_sd)
+static inline int sysfs_create_dir(struct kobject * kobj)
 {
 	return 0;
 }
@@ -165,9 +154,7 @@ static inline void sysfs_remove_dir(struct kobject * k)
 	;
 }
 
-static inline int sysfs_rename_dir(struct kobject *kobj,
-				   struct sysfs_dirent *new_parent_sd,
-				   const char *new_name)
+static inline int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
 {
 	return 0;
 }
@@ -242,12 +229,6 @@ static inline void sysfs_notify(struct kobject * k, char *dir, char *attr)
 {
 }
 
-static inline int sysfs_make_shadowed_dir(struct kobject *kobj,
-	void * (*follow_link)(struct dentry *, struct nameidata *))
-{
-	return 0;
-}
-
 static inline int __must_check sysfs_init(void)
 {
 	return 0;
lib/kobject.c +6 -38

86 lines not on this page · raw message

Signature · 1 line
1.5.1.1.181.g2de0

A message that is not in the archive (sent off-list or not yet received): m14pk13svx.fsf-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#11 lore raw

PATCH 2/4 sysfs: Implement sysfs manged shadow directory support.

The problem. When implementing a network namespace I need to be able to have multiple network devices with the same name. Currently this is a problem for /sys/class/net/*, /sys/devices/virtual/net/*, and potentially a few other directories of the form /sys/ ... /net/*.

What I want is for each network namespace to have it's own separate set of directories. /sys/class/net/, /sys/devices/virtual/net, and /sys/ ... /net/, and in each set I want to name them /sys/class/net/, sys/devices/virtual/net/ and /sys/ ... /net/ respectively.

I looked and the VFS actually allows that. All that is needed is for /sys/class/net to implement a follow link method to redirect lookups to the real directory you want.

I am calling the concept of multiple directories all at the same path in the filesystem shadow directories, the directory entry that implements the follow_link method the shadow master, and the directories that are the target of the follow link method shadow directories.

It turns out that just implementing a follow_link method is not quite enough. The existince of directories of the form /sys/ ... /net/ can depend on the presence or absence of hotplug hardware, which means I need a simple race free way to create and destroy these directories.

To achieve a race free design all shadow directories are created and managed by sysfs itself. The upper level code that knows what shadow directories we need provides just two methods that enable this:
current_tag() - that returns a "void *" tag that identifies the context of
the current process.
kobject_tag(kobj) - that returns a "void *" tag that identifies the context
a kobject should be in.
Everything else is left up to sysfs.

For the network namespace current_tag and kobject_tag are essentially one line functions, and look to remain that.

The work needed in sysfs is more extensive. At each directory or symlink creation I need to check if the shadow directory it belongs in exists and if it does not create it. Likewise at each symlink or directory removal I need to check if sysfs directory it is being removed from is a shadow directory and if this is the last object in the shadow directory and if so to remove the shadow directory as well.

I also need a bunch of boiler plate that properly finds, creates, and removes/frees the shadow directories.

Doing all of that in sysfs isn't bad it is just a bit tedious. Being race free is just a manner of ensure we have the directory inode mutex and the sysfs mutex when we add or remove a shadow directory. Trying to do this race free anywhere besides in sysfs is very nasty, and requires unhealthy amounts of information about how sysfs is implemented.

Currently only directories which hold kobjects, and symlinks are supported. There is not enough information in the current file attribute interfaces to give us anything to discriminate on which makes it useless, and there are not potential users which makes it an uniteresting problem to solve.

---
 fs/sysfs/bin.c        |    2 +-
 fs/sysfs/dir.c        |  404 +++++++++++++++++++++++++++++++++++++++++--------
 fs/sysfs/file.c       |    4 +-
 fs/sysfs/group.c      |   12 +-
 fs/sysfs/inode.c      |   11 +-
 fs/sysfs/symlink.c    |   30 +++-
 fs/sysfs/sysfs.h      |    9 +-
 include/linux/sysfs.h |   15 ++
 8 files changed, 396 insertions(+), 91 deletions(-)
fs/sysfs/bin.c +1 -1
index 135353f..1ef0a07 100644
@@ -248,7 +248,7 @@ int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr)
 
 void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
 {
-	if (sysfs_hash_and_remove(kobj->sd, attr->attr.name) < 0) {
+	if (sysfs_hash_and_remove(kobj, kobj->sd, attr->attr.name) < 0) {
 		printk(KERN_ERR "%s: "
 			"bad dentry or inode or no such file: \"%s\"\n",
 			__FUNCTION__, attr->attr.name);
fs/sysfs/dir.c +337 -67

538 lines not on this page · raw message

fs/sysfs/file.c +2 -2
index 3e1cc06..530a830 100644
@@ -544,7 +544,7 @@ EXPORT_SYMBOL_GPL(sysfs_chmod_file);
 
 void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
 {
-	sysfs_hash_and_remove(kobj->sd, attr->name);
+	sysfs_hash_and_remove(kobj, kobj->sd, attr->name);
 }
 
 
@@ -561,7 +561,7 @@ void sysfs_remove_file_from_group(struct kobject *kobj,
 
 	dir_sd = sysfs_get_dirent(kobj->sd, group);
 	if (dir_sd) {
-		sysfs_hash_and_remove(dir_sd, attr->name);
+		sysfs_hash_and_remove(kobj, dir_sd, attr->name);
 		sysfs_put(dir_sd);
 	}
 }
fs/sysfs/group.c +6 -6
index 4606f7c..9e928fd 100644
@@ -17,16 +17,16 @@
 #include "sysfs.h"
 
 
-static void remove_files(struct sysfs_dirent *dir_sd,
+static void remove_files(struct kobject *kobj, struct sysfs_dirent *dir_sd,
 			 const struct attribute_group *grp)
 {
 	struct attribute *const* attr;
 
 	for (attr = grp->attrs; *attr; attr++)
-		sysfs_hash_and_remove(dir_sd, (*attr)->name);
+		sysfs_hash_and_remove(kobj, dir_sd, (*attr)->name);
 }
 
-static int create_files(struct sysfs_dirent *dir_sd,
+static int create_files(struct kobject *kobj, struct sysfs_dirent *dir_sd,
 			const struct attribute_group *grp)
 {
 	struct attribute *const* attr;
@@ -35,7 +35,7 @@ static int create_files(struct sysfs_dirent *dir_sd,
 	for (attr = grp->attrs; *attr && !error; attr++)
 		error = sysfs_add_file(dir_sd, *attr, SYSFS_KOBJ_ATTR);
 	if (error)
-		remove_files(dir_sd, grp);
+		remove_files(kobj, dir_sd, grp);
 	return error;
 }
 
@@ -55,7 +55,7 @@ int sysfs_create_group(struct kobject * kobj,
 	} else
 		sd = kobj->sd;
 	sysfs_get(sd);
-	error = create_files(sd, grp);
+	error = create_files(kobj, sd, grp);
 	if (error) {
 		if (grp->name)
 			sysfs_remove_subdir(sd);
@@ -76,7 +76,7 @@ void sysfs_remove_group(struct kobject * kobj,
 	} else
 		sd = sysfs_get(dir_sd);
 
-	remove_files(sd, grp);
+	remove_files(kobj, sd, grp);
 	if (grp->name)
 		sysfs_remove_subdir(sd);
 
fs/sysfs/inode.c +5 -6
index 9671164..e31896e 100644
@@ -187,17 +187,16 @@ void sysfs_instantiate(struct dentry *dentry, struct inode *inode)
 	d_instantiate(dentry, inode);
 }
 
-int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name)
+int sysfs_hash_and_remove(struct kobject *kobj, struct sysfs_dirent *dir_sd, const char *name)
 {
 	struct sysfs_addrm_cxt acxt;
 	struct sysfs_dirent **pos, *sd;
 
-	if (!dir_sd)
-		return -ENOENT;
-
 	sysfs_addrm_start(&acxt, dir_sd);
+	if (!sysfs_resolve_for_remove(kobj, &acxt.parent_sd))
+		goto addrm_finish;
 
-	for (pos = &dir_sd->s_children; *pos; pos = &(*pos)->s_sibling) {
+	for (pos = &acxt.parent_sd->s_children; *pos; pos = &(*pos)->s_sibling) {
 		sd = *pos;
 
 		if (!sysfs_type(sd))
@@ -209,7 +208,7 @@ int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name)
 			break;
 		}
 	}
-
+addrm_finish:
 	if (sysfs_addrm_finish(&acxt))
 		return 0;
 	return -ENOENT;
fs/sysfs/symlink.c +22 -8
index 4ce687f..e52d3ef 100644
@@ -15,8 +15,11 @@ static int object_depth(struct sysfs_dirent *sd)
 {
 	int depth = 0;
 
-	for (; sd->s_parent; sd = sd->s_parent)
+	for (; sd->s_parent; sd = sd->s_parent) {
+		if (sysfs_type(sd) == SYSFS_SHADOW_DIR)
+			continue;
 		depth++;
+	}
 
 	return depth;
 }
@@ -25,17 +28,24 @@ static int object_path_length(struct sysfs_dirent * sd)
 {
 	int length = 1;
 
-	for (; sd->s_parent; sd = sd->s_parent)
+	for (; sd->s_parent; sd = sd->s_parent) {
+		if (sysfs_type(sd) == SYSFS_SHADOW_DIR)
+			continue;
 		length += strlen(sd->s_name) + 1;
+	}
 
 	return length;
 }
 
 static void fill_object_path(struct sysfs_dirent *sd, char *buffer, int length)
 {
+	int cur;
 	--length;
 	for (; sd->s_parent; sd = sd->s_parent) {
-		int cur = strlen(sd->s_name);
+		if (sysfs_type(sd) == SYSFS_SHADOW_DIR)
+			continue;
+
+		cur = strlen(sd->s_name);
 
 		/* back up enough to print this bus id with '/' */
 		length -= cur;
@@ -91,16 +101,20 @@ int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char
 	target_sd = NULL;	/* reference is now owned by the symlink */
 
 	sysfs_addrm_start(&acxt, parent_sd);
+	error = -ENOENT;
+	if (!sysfs_resolve_for_create(target, &acxt.parent_sd))
+		goto addrm_finish;
 
-	if (!sysfs_find_dirent(parent_sd, name)) {
+	error = -EEXIST;
+	if (!sysfs_find_dirent(acxt.parent_sd, name)) {
+		error = 0;
 		sysfs_add_one(&acxt, sd);
 		sysfs_link_sibling(sd);
 	}
 
-	if (!sysfs_addrm_finish(&acxt)) {
-		error = -EEXIST;
+addrm_finish:
+	if (!sysfs_addrm_finish(&acxt))
 		goto out_put;
-	}
 
 	return 0;
 
@@ -119,7 +133,7 @@ int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char
 
 void sysfs_remove_link(struct kobject * kobj, const char * name)
 {
-	sysfs_hash_and_remove(kobj->sd, name);
+	sysfs_hash_and_remove(kobj, kobj->sd, name);
 }
 
 static int sysfs_get_target_path(struct sysfs_dirent * parent_sd,
fs/sysfs/sysfs.h +8 -1
index b55e510..ddfcef2 100644
@@ -58,6 +58,12 @@ extern struct kmem_cache *sysfs_dir_cachep;
 extern struct dentry *sysfs_get_dentry(struct sysfs_dirent *sd);
 extern void sysfs_link_sibling(struct sysfs_dirent *sd);
 extern void sysfs_unlink_sibling(struct sysfs_dirent *sd);
+
+extern int sysfs_resolve_for_create(struct kobject *kobj,
+				    struct sysfs_dirent **parent_sd);
+extern int sysfs_resolve_for_remove(struct kobject *kobj,
+				    struct sysfs_dirent **parent_sd);
+
 extern struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd);
 extern void sysfs_put_active(struct sysfs_dirent *sd);
 extern struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd);
@@ -83,7 +89,8 @@ extern struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode,
 
 extern int sysfs_add_file(struct sysfs_dirent *dir_sd,
 			  const struct attribute *attr, int type);
-extern int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name);
+extern int sysfs_hash_and_remove(struct kobject *kobj,
+				 struct sysfs_dirent *dir_sd, const char *name);
 extern struct sysfs_dirent *sysfs_find(struct sysfs_dirent *dir, const char * name);
 
 extern int sysfs_create_subdir(struct kobject *kobj, const char *name,
include/linux/sysfs.h +15 -0
index c16e4c5..7cda047 100644
@@ -74,16 +74,23 @@ struct sysfs_ops {
 	ssize_t	(*store)(struct kobject *,struct attribute *,const char *, size_t);
 };
 
+struct shadow_dir_operations {
+	const void *(*current_tag)(void);
+	const void *(*kobject_tag)(struct kobject *kobj);
+};
+
 #define SYSFS_TYPE_MASK		0x00ff
 #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_SHADOW_DIR	0x0040
 #define SYSFS_COPY_NAME		(SYSFS_DIR | SYSFS_KOBJ_LINK)
 
 #define SYSFS_FLAG_MASK		~SYSFS_TYPE_MASK
 #define SYSFS_FLAG_REMOVED	0x0100
+#define SYSFS_FLAG_SHADOWED	0x0200
 
 #ifdef CONFIG_SYSFS
 
@@ -134,6 +141,8 @@ void sysfs_remove_file_from_group(struct kobject *kobj,
 
 void sysfs_notify(struct kobject * k, char *dir, char *attr);
 
+int sysfs_enable_shadowing(struct kobject *, const struct shadow_dir_operations *);
+
 extern int __must_check sysfs_init(void);
 
 #else /* CONFIG_SYSFS */
@@ -229,6 +238,12 @@ static inline void sysfs_notify(struct kobject * k, char *dir, char *attr)
 {
 }
 
+static inline int sysfs_enable_shadowing(struct kobject *kobj,
+				const struct shadow_dir_operations *shadow_ops)
+{
+	return 0;
+}
+
 static inline int __must_check sysfs_init(void)
 {
 	return 0;
Signature · 1 line
1.5.1.1.181.g2de0

A message that is not in the archive (sent off-list or not yet received): m1zm1t2e92.fsf_-_-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#13 lore raw

PATCH 3/4 sysfs: Implement sysfs_delete_link and sysfs_rename_link

When removing a symlink sysfs_remove_link does not provide enough information to figure out which shadow directory the symlink falls in. So I need sysfs_delete_link which is passed the target of the symlink to delete.

Further half the time when we are removing a symlink the code is actually renaming the symlink but not doing so explicitly because we don't have a symlink rename method. So I have added sysfs_rename_link as well.

Both of these functions now have enough information to find a symlink in a shadow directory. The only restriction is that they must be called before the target kobject is renamed or deleted. If they are called later I loose track of which tag the target kobject was marked with and can no longer find the old symlink to remove it.

---
 fs/sysfs/symlink.c    |   31 +++++++++++++++++++++++++++++++
 include/linux/sysfs.h |   18 ++++++++++++++++++
 2 files changed, 49 insertions(+), 0 deletions(-)
fs/sysfs/symlink.c +31 -0
index e52d3ef..62b0aeb 100644
@@ -126,6 +126,21 @@ addrm_finish:
 
 
 /**
+ *	sysfs_delete_link - remove symlink in object's directory.
+ *	@kobj:	object we're acting for.
+ *	@targ:	object we're pointing to.
+ *	@name:	name of the symlink to remove.
+ *
+ *	Unlike sysfs_remove_link sysfs_delete_link has enough information
+ *	to successfully delete symlinks in shadow directories.
+ */
+void sysfs_delete_link(struct kobject *kobj, struct kobject *targ,
+			const char *name)
+{
+	sysfs_hash_and_remove(targ, kobj->sd, name);
+}
+
+/**
  *	sysfs_remove_link - remove symlink in object's directory.
  *	@kobj:	object we're acting for.
  *	@name:	name of the symlink to remove.
@@ -136,6 +151,22 @@ void sysfs_remove_link(struct kobject * kobj, const char * name)
 	sysfs_hash_and_remove(kobj, kobj->sd, name);
 }
 
+/**
+ *	sysfs_rename_link - rename symlink in object's directory.
+ *	@kobj:	object we're acting for.
+ *	@targ:	object we're pointing to.
+ *	@old:	previous name of the symlink.
+ *	@new:	new name of the symlink.
+ *
+ *	A helper function for the common rename symlink idiom.
+ */
+int sysfs_rename_link(struct kobject *kobj, struct kobject *targ,
+			const char *old, const char *new)
+{
+	sysfs_delete_link(kobj, targ, old);
+	return sysfs_create_link(kobj, targ, new);
+}
+
 static int sysfs_get_target_path(struct sysfs_dirent * parent_sd,
 				 struct sysfs_dirent * target_sd, char *path)
 {
include/linux/sysfs.h +18 -0
index 7cda047..e195cb5 100644
@@ -127,6 +127,13 @@ sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * n
 extern void
 sysfs_remove_link(struct kobject *, const char * name);
 
+extern int
+sysfs_rename_link(struct kobject *kobj, struct kobject *target,
+			const char *old_name, const char *new_name);
+
+extern void
+sysfs_delete_link(struct kobject *dir, struct kobject *targ, const char *name);
+
 int __must_check sysfs_create_bin_file(struct kobject *kobj,
 					struct bin_attribute *attr);
 void sysfs_remove_bin_file(struct kobject *kobj, struct bin_attribute *attr);
@@ -202,6 +209,17 @@ static inline void sysfs_remove_link(struct kobject * k, const char * name)
 	;
 }
 
+static inline int
+sysfs_rename_link(struct kobject * k, struct kobject *t,
+			const char *old_name, const char * new_name)
+{
+	return 0;
+}
+
+static inline void
+sysfs_delete_link(struct kobject *k, struct kobject *t, const char *name)
+{
+}
 
 static inline int sysfs_create_bin_file(struct kobject * k, struct bin_attribute * a)
 {
Signature · 1 line
1.5.1.1.181.g2de0

A message that is not in the archive (sent off-list or not yet received): m1vech2e6o.fsf_-_-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#15 lore raw

PATCH 4/4 driver core: Implement shadow directory support for device classes.

This patch enables shadowing on every class directory if struct class has shadow_ops.

In addition device_del and device_rename were modified to use sysfs_delete_link and sysfs_rename_link respectively to ensure when these operations happen on devices whos classes have shadow operations that they work properly.

---
 drivers/base/class.c   |   30 ++++++++++++++++++++++++++----
 drivers/base/core.c    |   45 ++++++++++++++++++++++++---------------------
 include/linux/device.h |    2 ++
 3 files changed, 52 insertions(+), 25 deletions(-)
drivers/base/class.c +26 -4
index 4d22226..c981f75 100644
@@ -134,6 +134,17 @@ static void remove_class_attrs(struct class * cls)
 	}
 }
 
+static int class_setup_shadowing(struct class *cls)
+{
+	const struct shadow_dir_operations *shadow_ops;
+
+	shadow_ops = cls->shadow_ops;
+	if (!shadow_ops)
+		return 0;
+
+	return sysfs_enable_shadowing(&cls->subsys.kobj, shadow_ops);
+}
+
 int class_register(struct class * cls)
 {
 	int error;
@@ -152,11 +163,22 @@ int class_register(struct class * cls)
 	subsys_set_kset(cls, class_subsys);
 
 	error = subsystem_register(&cls->subsys);
-	if (!error) {
-		error = add_class_attrs(class_get(cls));
-		class_put(cls);
-	}
+	if (error)
+		goto out;
+
+	error = class_setup_shadowing(cls);
+	if (error)
+		goto out_unregister;
+
+	error = add_class_attrs(cls);
+	if (error)
+		goto out_unregister;
+
+out:
 	return error;
+out_unregister:
+	subsystem_unregister(&cls->subsys);
+	goto out;
 }
 
 void class_unregister(struct class * cls)
drivers/base/core.c +24 -21

73 lines not on this page · raw message

include/linux/device.h +2 -0
index d9f0a57..ed31e43 100644
@@ -200,6 +200,8 @@ struct class {
 
 	int	(*suspend)(struct device *, pm_message_t state);
 	int	(*resume)(struct device *);
+
+	const struct shadow_dir_operations *shadow_ops;
 };
 
 extern int __must_check class_register(struct class *);
Signature · 1 line
1.5.1.1.181.g2de0

A message that is not in the archive (sent off-list or not yet received): m1r6n52e5c.fsf_-_-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#17 lore raw

patch driver-core-implement-shadow-directory-support-for-device-classes.patch added to gregkh-2.6 tree

This is a note to let you know that I've just added the patch titled

Subject: [PATCH 4/4] driver core: Implement shadow directory support for device classes.

to my gregkh-2.6 tree. Its filename is

driver-core-implement-shadow-directory-support-for-device-classes.patch

This tree can be found at
http://www.kernel.org/pub/linux/kernel/people/gregkh/gregkh-2.6/patches/

From ebiederm-aS9lmoZGLiVWk0Htik3J/[email protected] Fri Jul 20 23:21:18 2007
From: ebiederm-aS9lmoZGLiVWk0Htik3J/[email protected] (Eric W. Biederman)
Date: Wed, 18 Jul 2007 22:47:27 -0600
Subject: [PATCH 4/4] driver core: Implement shadow directory support for device classes.
To: Greg KH <[email protected]>
Cc: Greg KH <[email protected]>, Dave Hansen <hansendc-r/[email protected]>, Benjamin Thery <benjamin.thery-6ktuUTfB/[email protected]>, Linux Containers <[email protected]>, Tejun Heo <[email protected]>
Message-ID: <m1r6n52e5c.fsf_-_-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org>

This patch enables shadowing on every class directory if struct class has shadow_ops.

In addition device_del and device_rename were modified to use sysfs_delete_link and sysfs_rename_link respectively to ensure when these operations happen on devices whos classes have shadow operations that they work properly.

---
 drivers/base/class.c   |   30 ++++++++++++++++++++++++++----
 drivers/base/core.c    |   45 ++++++++++++++++++++++++---------------------
 include/linux/device.h |    2 ++
 3 files changed, 52 insertions(+), 25 deletions(-)
drivers/base/class.c +26 -4
@@ -134,6 +134,17 @@ static void remove_class_attrs(struct cl
 	}
 }
 
+static int class_setup_shadowing(struct class *cls)
+{
+	const struct shadow_dir_operations *shadow_ops;
+
+	shadow_ops = cls->shadow_ops;
+	if (!shadow_ops)
+		return 0;
+
+	return sysfs_enable_shadowing(&cls->subsys.kobj, shadow_ops);
+}
+
 int class_register(struct class * cls)
 {
 	int error;
@@ -152,11 +163,22 @@ int class_register(struct class * cls)
 	subsys_set_kset(cls, class_subsys);
 
 	error = subsystem_register(&cls->subsys);
-	if (!error) {
-		error = add_class_attrs(class_get(cls));
-		class_put(cls);
-	}
+	if (error)
+		goto out;
+
+	error = class_setup_shadowing(cls);
+	if (error)
+		goto out_unregister;
+
+	error = add_class_attrs(cls);
+	if (error)
+		goto out_unregister;
+
+out:
 	return error;
+out_unregister:
+	subsystem_unregister(&cls->subsys);
+	goto out;
 }
 
 void class_unregister(struct class * cls)
drivers/base/core.c +24 -21

73 lines not on this page · raw message

include/linux/device.h +2 -0
@@ -200,6 +200,8 @@ struct class {
 
 	int	(*suspend)(struct device *, pm_message_t state);
 	int	(*resume)(struct device *);
+
+	const struct shadow_dir_operations *shadow_ops;
 };
 
 extern int __must_check class_register(struct class *);

Patches currently in gregkh-2.6 which might be from [email protected] are

bad/pci-domain/pci-device-ensure-sysdata-initialised.patch bad/pci-domain/pci-fix-the-x86-pci-domain-support-fix.patch bad/relayfs/sysfs-update-relay-file-support-for-generic-relay-api.patch bad/relayfs/relay-consolidate-relayfs-core-into-kernel-relay.c.patch bad/relayfs/relay-relay-header-cleanup.patch bad/relayfs/relayfs-remove-relayfs-in-favour-of-config_relay.patch bad/relayfs/sysfs-add-__attr_relay-helper-for-relay-attributes.patch bad/relayfs/sysfs-relay-channel-buffers-as-sysfs-attributes.patch bad/usbip/usb-usbip-more-dead-code-fix.patch bad/usbip/usb-usbip-build-fix.patch bad/usbip/usb-usbip-warning-fixes.patch
bad/ndevfs.patch
bad/battery-class-driver.patch bad/driver-model-convert-driver-model-to-mutexes.patch bad/gpl_future-test.patch
bad/gregkh-debugfs_example.patch
bad/speakup-kconfig-fix.patch
bad/speakup-build-fix.patch bad/pci-use-new-multi-phase-suspend-infrastructure.patch bad/shot-accross-the-bow.patch
bad/no-more-non-gpl-modules.patch
bad/spi-device.patch
bad/ata_piix-multithread.patch
bad/uio-irq.patch
bad/pci-two-drivers-on-one-pci-device.patch bad/pci-dynamic-id-cleanup.patch
bad/input-device.patch
bad/usb-stimulus.patch
driver/nozomi.patch driver/kobject-put-kobject_actions-in-kobject.h.patch driver/sysfs-implement-sysfs-manged-shadow-directory-support.patch driver/sysfs-implement-sysfs_delete_link-and-sysfs_rename_link.patch driver/sysfs-remove-first-pass-at-shadow-directory-support.patch driver/driver-core-implement-shadow-directory-support-for-device-classes.patch gregkh/gkh-version.patch
gregkh/sysfs-test.patch
gregkh/sysrq-u-laptop.patch
pci/pci_bridge-device.patch
pci/pci-piggy-bus.patch pci/pci-move-prototypes-for-pci_bus_find_capability-to-include-linux-pci.h.patch pci/pci-document-pci_iomap.patch
usb/usb-gotemp.patch usb/kobject-put-kobject_actions-in-kobject.h.patch usb/usb-add-the-concept-of-default-authorization-to-usb-hosts.patch usb/usb-cleanup-usb_register_bus-and-hook-up-sysfs-group.patch usb/usb-initialize-authorization-and-wusb-bits-in-usb-devices.patch usb/usb-introduce-usb_device-authorization-bits.patch usb/usb-usb_set_configuration-obeys-authorization.patch usb/usb-usb.h-kernel-doc-additions.patch
HOWTO

A message that is not in the archive (sent off-list or not yet received): 20070721063634.9337314458DB-j1pC+zEt+uWoYr4blSSd5g@public.gmane.org

#19 lore raw

Re: patch driver-core-implement-shadow-directory-support-for-device-classes.patch added to gregkh-2.6 tree

Quoted 11 lines · <[email protected]> writes:

This is a note to let you know that I've just added the patch titled

Subject: [PATCH 4/4] driver core: Implement shadow directory support for
device classes.

to my gregkh-2.6 tree. Its filename is

driver-core-implement-shadow-directory-support-for-device-classes.patch

This tree can be found at
http://www.kernel.org/pub/linux/kernel/people/gregkh/gregkh-2.6/patches/

Hip hip hooray. The patches finally apply!!!!

Eric

#20 lore raw

patch sysfs-implement-sysfs_delete_link-and-sysfs_rename_link.patch added to gregkh-2.6 tree

In reply to a message not in the archive

This is a note to let you know that I've just added the patch titled

Subject: [PATCH 3/4] sysfs: Implement sysfs_delete_link and sysfs_rename_link

to my gregkh-2.6 tree. Its filename is

sysfs-implement-sysfs_delete_link-and-sysfs_rename_link.patch

This tree can be found at
http://www.kernel.org/pub/linux/kernel/people/gregkh/gregkh-2.6/patches/

From ebiederm-aS9lmoZGLiVWk0Htik3J/[email protected] Fri Jul 20 23:21:06 2007
From: ebiederm-aS9lmoZGLiVWk0Htik3J/[email protected] (Eric W. Biederman)
Date: Wed, 18 Jul 2007 22:46:39 -0600
Subject: [PATCH 3/4] sysfs: Implement sysfs_delete_link and sysfs_rename_link
To: Greg KH <[email protected]>
Cc: Greg KH <[email protected]>, Dave Hansen <hansendc-r/[email protected]>, Benjamin Thery <benjamin.thery-6ktuUTfB/[email protected]>, Linux Containers <[email protected]>, Tejun Heo <[email protected]>
Message-ID: <m1vech2e6o.fsf_-_-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org>

When removing a symlink sysfs_remove_link does not provide enough information to figure out which shadow directory the symlink falls in. So I need sysfs_delete_link which is passed the target of the symlink to delete.

Further half the time when we are removing a symlink the code is actually renaming the symlink but not doing so explicitly because we don't have a symlink rename method. So I have added sysfs_rename_link as well.

Both of these functions now have enough information to find a symlink in a shadow directory. The only restriction is that they must be called before the target kobject is renamed or deleted. If they are called later I loose track of which tag the target kobject was marked with and can no longer find the old symlink to remove it.

---
 fs/sysfs/symlink.c    |   31 +++++++++++++++++++++++++++++++
 include/linux/sysfs.h |   18 ++++++++++++++++++
 2 files changed, 49 insertions(+)
fs/sysfs/symlink.c +31 -0
@@ -126,6 +126,21 @@ addrm_finish:
 
 
 /**
+ *	sysfs_delete_link - remove symlink in object's directory.
+ *	@kobj:	object we're acting for.
+ *	@targ:	object we're pointing to.
+ *	@name:	name of the symlink to remove.
+ *
+ *	Unlike sysfs_remove_link sysfs_delete_link has enough information
+ *	to successfully delete symlinks in shadow directories.
+ */
+void sysfs_delete_link(struct kobject *kobj, struct kobject *targ,
+			const char *name)
+{
+	sysfs_hash_and_remove(targ, kobj->sd, name);
+}
+
+/**
  *	sysfs_remove_link - remove symlink in object's directory.
  *	@kobj:	object we're acting for.
  *	@name:	name of the symlink to remove.
@@ -136,6 +151,22 @@ void sysfs_remove_link(struct kobject * 
 	sysfs_hash_and_remove(kobj, kobj->sd, name);
 }
 
+/**
+ *	sysfs_rename_link - rename symlink in object's directory.
+ *	@kobj:	object we're acting for.
+ *	@targ:	object we're pointing to.
+ *	@old:	previous name of the symlink.
+ *	@new:	new name of the symlink.
+ *
+ *	A helper function for the common rename symlink idiom.
+ */
+int sysfs_rename_link(struct kobject *kobj, struct kobject *targ,
+			const char *old, const char *new)
+{
+	sysfs_delete_link(kobj, targ, old);
+	return sysfs_create_link(kobj, targ, new);
+}
+
 static int sysfs_get_target_path(struct sysfs_dirent * parent_sd,
 				 struct sysfs_dirent * target_sd, char *path)
 {
include/linux/sysfs.h +18 -0
@@ -127,6 +127,13 @@ sysfs_create_link(struct kobject * kobj,
 extern void
 sysfs_remove_link(struct kobject *, const char * name);
 
+extern int
+sysfs_rename_link(struct kobject *kobj, struct kobject *target,
+			const char *old_name, const char *new_name);
+
+extern void
+sysfs_delete_link(struct kobject *dir, struct kobject *targ, const char *name);
+
 int __must_check sysfs_create_bin_file(struct kobject *kobj,
 					struct bin_attribute *attr);
 void sysfs_remove_bin_file(struct kobject *kobj, struct bin_attribute *attr);
@@ -202,6 +209,17 @@ static inline void sysfs_remove_link(str
 	;
 }
 
+static inline int
+sysfs_rename_link(struct kobject * k, struct kobject *t,
+			const char *old_name, const char * new_name)
+{
+	return 0;
+}
+
+static inline void
+sysfs_delete_link(struct kobject *k, struct kobject *t, const char *name)
+{
+}
 
 static inline int sysfs_create_bin_file(struct kobject * k, struct bin_attribute * a)
 {

Patches currently in gregkh-2.6 which might be from [email protected] are

bad/pci-domain/pci-device-ensure-sysdata-initialised.patch bad/pci-domain/pci-fix-the-x86-pci-domain-support-fix.patch bad/relayfs/sysfs-update-relay-file-support-for-generic-relay-api.patch bad/relayfs/relay-consolidate-relayfs-core-into-kernel-relay.c.patch bad/relayfs/relay-relay-header-cleanup.patch bad/relayfs/relayfs-remove-relayfs-in-favour-of-config_relay.patch bad/relayfs/sysfs-add-__attr_relay-helper-for-relay-attributes.patch bad/relayfs/sysfs-relay-channel-buffers-as-sysfs-attributes.patch bad/usbip/usb-usbip-more-dead-code-fix.patch bad/usbip/usb-usbip-build-fix.patch bad/usbip/usb-usbip-warning-fixes.patch
bad/ndevfs.patch
bad/battery-class-driver.patch bad/driver-model-convert-driver-model-to-mutexes.patch bad/gpl_future-test.patch
bad/gregkh-debugfs_example.patch
bad/speakup-kconfig-fix.patch
bad/speakup-build-fix.patch bad/pci-use-new-multi-phase-suspend-infrastructure.patch bad/shot-accross-the-bow.patch
bad/no-more-non-gpl-modules.patch
bad/spi-device.patch
bad/ata_piix-multithread.patch
bad/uio-irq.patch
bad/pci-two-drivers-on-one-pci-device.patch bad/pci-dynamic-id-cleanup.patch
bad/input-device.patch
bad/usb-stimulus.patch
driver/nozomi.patch driver/kobject-put-kobject_actions-in-kobject.h.patch driver/sysfs-implement-sysfs-manged-shadow-directory-support.patch driver/sysfs-implement-sysfs_delete_link-and-sysfs_rename_link.patch driver/sysfs-remove-first-pass-at-shadow-directory-support.patch driver/driver-core-implement-shadow-directory-support-for-device-classes.patch gregkh/gkh-version.patch
gregkh/sysfs-test.patch
gregkh/sysrq-u-laptop.patch
pci/pci_bridge-device.patch
pci/pci-piggy-bus.patch pci/pci-move-prototypes-for-pci_bus_find_capability-to-include-linux-pci.h.patch pci/pci-document-pci_iomap.patch
usb/usb-gotemp.patch usb/kobject-put-kobject_actions-in-kobject.h.patch usb/usb-add-the-concept-of-default-authorization-to-usb-hosts.patch usb/usb-cleanup-usb_register_bus-and-hook-up-sysfs-group.patch usb/usb-initialize-authorization-and-wusb-bits-in-usb-devices.patch usb/usb-introduce-usb_device-authorization-bits.patch usb/usb-usb_set_configuration-obeys-authorization.patch usb/usb-usb.h-kernel-doc-additions.patch
HOWTO

#21 lore raw

patch sysfs-implement-sysfs-manged-shadow-directory-support.patch added to gregkh-2.6 tree

In reply to a message not in the archive

This is a note to let you know that I've just added the patch titled

Subject: [PATCH 2/4] sysfs: Implement sysfs manged shadow directory support.

to my gregkh-2.6 tree. Its filename is

sysfs-implement-sysfs-manged-shadow-directory-support.patch

This tree can be found at
http://www.kernel.org/pub/linux/kernel/people/gregkh/gregkh-2.6/patches/

From ebiederm-aS9lmoZGLiVWk0Htik3J/[email protected] Fri Jul 20 23:20:42 2007
From: ebiederm-aS9lmoZGLiVWk0Htik3J/[email protected] (Eric W. Biederman)
Date: Wed, 18 Jul 2007 22:45:13 -0600
Subject: [PATCH 2/4] sysfs: Implement sysfs manged shadow directory support.
To: Greg KH <[email protected]>
Cc: Greg KH <[email protected]>, Dave Hansen <hansendc-r/[email protected]>, Benjamin Thery <benjamin.thery-6ktuUTfB/[email protected]>, Linux Containers <[email protected]>, Tejun Heo <[email protected]>
Message-ID: <m1zm1t2e92.fsf_-_-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org>

The problem. When implementing a network namespace I need to be able to have multiple network devices with the same name. Currently this is a problem for /sys/class/net/*, /sys/devices/virtual/net/*, and potentially a few other directories of the form /sys/ ... /net/*.

What I want is for each network namespace to have it's own separate set of directories. /sys/class/net/, /sys/devices/virtual/net, and /sys/ ... /net/, and in each set I want to name them /sys/class/net/, sys/devices/virtual/net/ and /sys/ ... /net/ respectively.

I looked and the VFS actually allows that. All that is needed is for /sys/class/net to implement a follow link method to redirect lookups to the real directory you want.

I am calling the concept of multiple directories all at the same path in the filesystem shadow directories, the directory entry that implements the follow_link method the shadow master, and the directories that are the target of the follow link method shadow directories.

It turns out that just implementing a follow_link method is not quite enough. The existince of directories of the form /sys/ ... /net/ can depend on the presence or absence of hotplug hardware, which means I need a simple race free way to create and destroy these directories.

To achieve a race free design all shadow directories are created and managed by sysfs itself. The upper level code that knows what shadow directories we need provides just two methods that enable this:
current_tag() - that returns a "void *" tag that identifies the context of
the current process.
kobject_tag(kobj) - that returns a "void *" tag that identifies the context
a kobject should be in.
Everything else is left up to sysfs.

For the network namespace current_tag and kobject_tag are essentially one line functions, and look to remain that.

The work needed in sysfs is more extensive. At each directory or symlink creation I need to check if the shadow directory it belongs in exists and if it does not create it. Likewise at each symlink or directory removal I need to check if sysfs directory it is being removed from is a shadow directory and if this is the last object in the shadow directory and if so to remove the shadow directory as well.

I also need a bunch of boiler plate that properly finds, creates, and removes/frees the shadow directories.

Doing all of that in sysfs isn't bad it is just a bit tedious. Being race free is just a manner of ensure we have the directory inode mutex and the sysfs mutex when we add or remove a shadow directory. Trying to do this race free anywhere besides in sysfs is very nasty, and requires unhealthy amounts of information about how sysfs is implemented.

Currently only directories which hold kobjects, and symlinks are supported. There is not enough information in the current file attribute interfaces to give us anything to discriminate on which makes it useless, and there are not potential users which makes it an uniteresting problem to solve.

---
 fs/sysfs/bin.c        |    2 
 fs/sysfs/dir.c        |  404 +++++++++++++++++++++++++++++++++++++++++---------
 fs/sysfs/file.c       |    4 
 fs/sysfs/group.c      |   12 -
 fs/sysfs/inode.c      |   11 -
 fs/sysfs/symlink.c    |   30 ++-
 fs/sysfs/sysfs.h      |    9 -
 include/linux/sysfs.h |   15 +
 8 files changed, 396 insertions(+), 91 deletions(-)
fs/sysfs/bin.c +1 -1
@@ -248,7 +248,7 @@ int sysfs_create_bin_file(struct kobject
 
 void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
 {
-	if (sysfs_hash_and_remove(kobj->sd, attr->attr.name) < 0) {
+	if (sysfs_hash_and_remove(kobj, kobj->sd, attr->attr.name) < 0) {
 		printk(KERN_ERR "%s: "
 			"bad dentry or inode or no such file: \"%s\"\n",
 			__FUNCTION__, attr->attr.name);
fs/sysfs/dir.c +337 -67

538 lines not on this page · raw message

fs/sysfs/file.c +2 -2
@@ -544,7 +544,7 @@ EXPORT_SYMBOL_GPL(sysfs_chmod_file);
 
 void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
 {
-	sysfs_hash_and_remove(kobj->sd, attr->name);
+	sysfs_hash_and_remove(kobj, kobj->sd, attr->name);
 }
 
 
@@ -561,7 +561,7 @@ void sysfs_remove_file_from_group(struct
 
 	dir_sd = sysfs_get_dirent(kobj->sd, group);
 	if (dir_sd) {
-		sysfs_hash_and_remove(dir_sd, attr->name);
+		sysfs_hash_and_remove(kobj, dir_sd, attr->name);
 		sysfs_put(dir_sd);
 	}
 }
fs/sysfs/group.c +6 -6
@@ -17,16 +17,16 @@
 #include "sysfs.h"
 
 
-static void remove_files(struct sysfs_dirent *dir_sd,
+static void remove_files(struct kobject *kobj, struct sysfs_dirent *dir_sd,
 			 const struct attribute_group *grp)
 {
 	struct attribute *const* attr;
 
 	for (attr = grp->attrs; *attr; attr++)
-		sysfs_hash_and_remove(dir_sd, (*attr)->name);
+		sysfs_hash_and_remove(kobj, dir_sd, (*attr)->name);
 }
 
-static int create_files(struct sysfs_dirent *dir_sd,
+static int create_files(struct kobject *kobj, struct sysfs_dirent *dir_sd,
 			const struct attribute_group *grp)
 {
 	struct attribute *const* attr;
@@ -35,7 +35,7 @@ static int create_files(struct sysfs_dir
 	for (attr = grp->attrs; *attr && !error; attr++)
 		error = sysfs_add_file(dir_sd, *attr, SYSFS_KOBJ_ATTR);
 	if (error)
-		remove_files(dir_sd, grp);
+		remove_files(kobj, dir_sd, grp);
 	return error;
 }
 
@@ -55,7 +55,7 @@ int sysfs_create_group(struct kobject * 
 	} else
 		sd = kobj->sd;
 	sysfs_get(sd);
-	error = create_files(sd, grp);
+	error = create_files(kobj, sd, grp);
 	if (error) {
 		if (grp->name)
 			sysfs_remove_subdir(sd);
@@ -76,7 +76,7 @@ void sysfs_remove_group(struct kobject *
 	} else
 		sd = sysfs_get(dir_sd);
 
-	remove_files(sd, grp);
+	remove_files(kobj, sd, grp);
 	if (grp->name)
 		sysfs_remove_subdir(sd);
 
fs/sysfs/inode.c +5 -6
@@ -187,17 +187,16 @@ void sysfs_instantiate(struct dentry *de
 	d_instantiate(dentry, inode);
 }
 
-int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name)
+int sysfs_hash_and_remove(struct kobject *kobj, struct sysfs_dirent *dir_sd, const char *name)
 {
 	struct sysfs_addrm_cxt acxt;
 	struct sysfs_dirent **pos, *sd;
 
-	if (!dir_sd)
-		return -ENOENT;
-
 	sysfs_addrm_start(&acxt, dir_sd);
+	if (!sysfs_resolve_for_remove(kobj, &acxt.parent_sd))
+		goto addrm_finish;
 
-	for (pos = &dir_sd->s_children; *pos; pos = &(*pos)->s_sibling) {
+	for (pos = &acxt.parent_sd->s_children; *pos; pos = &(*pos)->s_sibling) {
 		sd = *pos;
 
 		if (!sysfs_type(sd))
@@ -209,7 +208,7 @@ int sysfs_hash_and_remove(struct sysfs_d
 			break;
 		}
 	}
-
+addrm_finish:
 	if (sysfs_addrm_finish(&acxt))
 		return 0;
 	return -ENOENT;
fs/sysfs/symlink.c +22 -8
@@ -15,8 +15,11 @@ static int object_depth(struct sysfs_dir
 {
 	int depth = 0;
 
-	for (; sd->s_parent; sd = sd->s_parent)
+	for (; sd->s_parent; sd = sd->s_parent) {
+		if (sysfs_type(sd) == SYSFS_SHADOW_DIR)
+			continue;
 		depth++;
+	}
 
 	return depth;
 }
@@ -25,17 +28,24 @@ static int object_path_length(struct sys
 {
 	int length = 1;
 
-	for (; sd->s_parent; sd = sd->s_parent)
+	for (; sd->s_parent; sd = sd->s_parent) {
+		if (sysfs_type(sd) == SYSFS_SHADOW_DIR)
+			continue;
 		length += strlen(sd->s_name) + 1;
+	}
 
 	return length;
 }
 
 static void fill_object_path(struct sysfs_dirent *sd, char *buffer, int length)
 {
+	int cur;
 	--length;
 	for (; sd->s_parent; sd = sd->s_parent) {
-		int cur = strlen(sd->s_name);
+		if (sysfs_type(sd) == SYSFS_SHADOW_DIR)
+			continue;
+
+		cur = strlen(sd->s_name);
 
 		/* back up enough to print this bus id with '/' */
 		length -= cur;
@@ -91,16 +101,20 @@ int sysfs_create_link(struct kobject * k
 	target_sd = NULL;	/* reference is now owned by the symlink */
 
 	sysfs_addrm_start(&acxt, parent_sd);
+	error = -ENOENT;
+	if (!sysfs_resolve_for_create(target, &acxt.parent_sd))
+		goto addrm_finish;
 
-	if (!sysfs_find_dirent(parent_sd, name)) {
+	error = -EEXIST;
+	if (!sysfs_find_dirent(acxt.parent_sd, name)) {
+		error = 0;
 		sysfs_add_one(&acxt, sd);
 		sysfs_link_sibling(sd);
 	}
 
-	if (!sysfs_addrm_finish(&acxt)) {
-		error = -EEXIST;
+addrm_finish:
+	if (!sysfs_addrm_finish(&acxt))
 		goto out_put;
-	}
 
 	return 0;
 
@@ -119,7 +133,7 @@ int sysfs_create_link(struct kobject * k
 
 void sysfs_remove_link(struct kobject * kobj, const char * name)
 {
-	sysfs_hash_and_remove(kobj->sd, name);
+	sysfs_hash_and_remove(kobj, kobj->sd, name);
 }
 
 static int sysfs_get_target_path(struct sysfs_dirent * parent_sd,
fs/sysfs/sysfs.h +8 -1
@@ -58,6 +58,12 @@ extern struct kmem_cache *sysfs_dir_cach
 extern struct dentry *sysfs_get_dentry(struct sysfs_dirent *sd);
 extern void sysfs_link_sibling(struct sysfs_dirent *sd);
 extern void sysfs_unlink_sibling(struct sysfs_dirent *sd);
+
+extern int sysfs_resolve_for_create(struct kobject *kobj,
+				    struct sysfs_dirent **parent_sd);
+extern int sysfs_resolve_for_remove(struct kobject *kobj,
+				    struct sysfs_dirent **parent_sd);
+
 extern struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd);
 extern void sysfs_put_active(struct sysfs_dirent *sd);
 extern struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd);
@@ -83,7 +89,8 @@ extern struct sysfs_dirent *sysfs_new_di
 
 extern int sysfs_add_file(struct sysfs_dirent *dir_sd,
 			  const struct attribute *attr, int type);
-extern int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name);
+extern int sysfs_hash_and_remove(struct kobject *kobj,
+				 struct sysfs_dirent *dir_sd, const char *name);
 extern struct sysfs_dirent *sysfs_find(struct sysfs_dirent *dir, const char * name);
 
 extern int sysfs_create_subdir(struct kobject *kobj, const char *name,
include/linux/sysfs.h +15 -0
@@ -74,16 +74,23 @@ struct sysfs_ops {
 	ssize_t	(*store)(struct kobject *,struct attribute *,const char *, size_t);
 };
 
+struct shadow_dir_operations {
+	const void *(*current_tag)(void);
+	const void *(*kobject_tag)(struct kobject *kobj);
+};
+
 #define SYSFS_TYPE_MASK		0x00ff
 #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_SHADOW_DIR	0x0040
 #define SYSFS_COPY_NAME		(SYSFS_DIR | SYSFS_KOBJ_LINK)
 
 #define SYSFS_FLAG_MASK		~SYSFS_TYPE_MASK
 #define SYSFS_FLAG_REMOVED	0x0100
+#define SYSFS_FLAG_SHADOWED	0x0200
 
 #ifdef CONFIG_SYSFS
 
@@ -134,6 +141,8 @@ void sysfs_remove_file_from_group(struct
 
 void sysfs_notify(struct kobject * k, char *dir, char *attr);
 
+int sysfs_enable_shadowing(struct kobject *, const struct shadow_dir_operations *);
+
 extern int __must_check sysfs_init(void);
 
 #else /* CONFIG_SYSFS */
@@ -229,6 +238,12 @@ static inline void sysfs_notify(struct k
 {
 }
 
+static inline int sysfs_enable_shadowing(struct kobject *kobj,
+				const struct shadow_dir_operations *shadow_ops)
+{
+	return 0;
+}
+
 static inline int __must_check sysfs_init(void)
 {
 	return 0;

Patches currently in gregkh-2.6 which might be from [email protected] are

bad/pci-domain/pci-device-ensure-sysdata-initialised.patch bad/pci-domain/pci-fix-the-x86-pci-domain-support-fix.patch bad/relayfs/sysfs-update-relay-file-support-for-generic-relay-api.patch bad/relayfs/relay-consolidate-relayfs-core-into-kernel-relay.c.patch bad/relayfs/relay-relay-header-cleanup.patch bad/relayfs/relayfs-remove-relayfs-in-favour-of-config_relay.patch bad/relayfs/sysfs-add-__attr_relay-helper-for-relay-attributes.patch bad/relayfs/sysfs-relay-channel-buffers-as-sysfs-attributes.patch bad/usbip/usb-usbip-more-dead-code-fix.patch bad/usbip/usb-usbip-build-fix.patch bad/usbip/usb-usbip-warning-fixes.patch
bad/ndevfs.patch
bad/battery-class-driver.patch bad/driver-model-convert-driver-model-to-mutexes.patch bad/gpl_future-test.patch
bad/gregkh-debugfs_example.patch
bad/speakup-kconfig-fix.patch
bad/speakup-build-fix.patch bad/pci-use-new-multi-phase-suspend-infrastructure.patch bad/shot-accross-the-bow.patch
bad/no-more-non-gpl-modules.patch
bad/spi-device.patch
bad/ata_piix-multithread.patch
bad/uio-irq.patch
bad/pci-two-drivers-on-one-pci-device.patch bad/pci-dynamic-id-cleanup.patch
bad/input-device.patch
bad/usb-stimulus.patch
driver/nozomi.patch driver/kobject-put-kobject_actions-in-kobject.h.patch driver/sysfs-implement-sysfs-manged-shadow-directory-support.patch driver/sysfs-implement-sysfs_delete_link-and-sysfs_rename_link.patch driver/sysfs-remove-first-pass-at-shadow-directory-support.patch driver/driver-core-implement-shadow-directory-support-for-device-classes.patch gregkh/gkh-version.patch
gregkh/sysfs-test.patch
gregkh/sysrq-u-laptop.patch
pci/pci_bridge-device.patch
pci/pci-piggy-bus.patch pci/pci-move-prototypes-for-pci_bus_find_capability-to-include-linux-pci.h.patch pci/pci-document-pci_iomap.patch
usb/usb-gotemp.patch usb/kobject-put-kobject_actions-in-kobject.h.patch usb/usb-add-the-concept-of-default-authorization-to-usb-hosts.patch usb/usb-cleanup-usb_register_bus-and-hook-up-sysfs-group.patch usb/usb-initialize-authorization-and-wusb-bits-in-usb-devices.patch usb/usb-introduce-usb_device-authorization-bits.patch usb/usb-usb_set_configuration-obeys-authorization.patch usb/usb-usb.h-kernel-doc-additions.patch
HOWTO

#22 lore raw

Re: [PATCH 2/4] sysfs: Implement sysfs manged shadow directory support.

In reply to a message not in the archive

Hello,

fs/sysfs/dir.c

Quoted 2 lines · Eric W. Biederman
fs/sysfs/dir.c +1 -0
+static struct sysfs_dirent *find_shadow_sd(struct sysfs_dirent

*parent_sd, const void *target)

fs/sysfs/dir.c line 32

Quoted 7 lines
fs/sysfs/dir.c +7 -0
+{
+	/* Find the shadow directory for the specified tag */
+	struct sysfs_dirent *sd;
+
+	for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) {
+		if (sd->s_name != target)
+			continue;

This is way too cryptic and, plus, no comment. This kind of stuff can cause a lot of confusion later when other people wanna work on the code. Please move s_name into sysfs_elem_* which need s_name and create sysfs_elem_shadow which doesn't have ->name but has ->tag.

fs/sysfs/dir.c line 42

Quoted 5 lines
fs/sysfs/dir.c +5 -0
+static const void *find_shadow_tag(struct kobject *kobj)
+{
+	/* Find the tag the current kobj is cached with */
+	return kobj->sd->s_parent->s_name;
+}

Please don't use kobj inside sysfs. Use sysfs_dirent instead.

fs/sysfs/dir.c static void sysfs_attach_dentry(struct

Quoted 1 line
fs/sysfs/dir.c +0 -0
@@ -414,7 +436,8 @@ static void sysfs_attach_dentry(struct

sysfs_dirent *sd, struct dentry *dentry)

fs/sysfs/dir.c static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry) · line 440

Quoted 6 lines
fs/sysfs/dir.c +2 -1
 	sd->s_dentry = dentry;
 	spin_unlock(&sysfs_assoc_lock);
 
-	d_rehash(dentry);
+	if (dentry->d_flags & DCACHE_UNHASHED)
+		d_rehash(dentry);

I think we can use some comment for subtle things like this. DCACHE_UNHASHED is being tested without holding dcache_lock which also can use some comment.

fs/sysfs/dir.c static void sysfs_drop_dentry(struct sysfs_dirent

Quoted 1 line
fs/sysfs/dir.c +0 -0
@@ -569,6 +592,10 @@ static void sysfs_drop_dentry(struct sysfs_dirent

*sd)

fs/sysfs/dir.c static int create_dir(struct kobject *kobj, struct

Quoted 19 lines
fs/sysfs/dir.c +5 -0
 	spin_unlock(&dcache_lock);
 	spin_unlock(&sysfs_assoc_lock);
 
+	/* dentries for shadowed directories are pinned, unpin */
+	if ((sysfs_type(sd) == SYSFS_SHADOW_DIR) ||
+	    (sd->s_flags & SYSFS_FLAG_SHADOWED))
+		dput(dentry);
 	dput(dentry);
 
 	/* adjust nlink and update timestamp */
@@ -622,6 +649,7 @@ int sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
 		acxt->removed = sd->s_sibling;
 		sd->s_sibling = NULL;
 
+		sysfs_prune_shadow_sd(sd->s_parent);
 		sysfs_drop_dentry(sd);
 		sysfs_deactivate(sd);
 		sysfs_put(sd);
@@ -687,6 +715,7 @@ static int create_dir(struct kobject *kobj, struct

sysfs_dirent *parent_sd,

fs/sysfs/dir.c static int create_dir(struct kobject *kobj,

Quoted 8 lines
fs/sysfs/dir.c +1 -0
 	umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
 	struct sysfs_addrm_cxt acxt;
 	struct sysfs_dirent *sd;
+	int err;
 
 	/* allocate */
 	sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
@@ -696,15 +725,21 @@ static int create_dir(struct kobject *kobj,

struct sysfs_dirent *parent_sd,

fs/sysfs/dir.c static struct dentry * sysfs_lookup(struct inode

Quoted 24 lines
fs/sysfs/dir.c +8 -2
 	/* link in */
 	sysfs_addrm_start(&acxt, parent_sd);
+	err = -ENOENT;
+	if (!sysfs_resolve_for_create(kobj, &acxt.parent_sd))
+		goto addrm_finish;
 
-	if (!sysfs_find_dirent(parent_sd, name)) {
+	err = -EEXIST;
+	if (!sysfs_find_dirent(acxt.parent_sd, name)) {
 		sysfs_add_one(&acxt, sd);
 		sysfs_link_sibling(sd);
+		err = 0;
 	}
 
+addrm_finish:
 	if (!sysfs_addrm_finish(&acxt)) {
 		sysfs_put(sd);
-		return -EEXIST;
+		return err;
 	}
 
 	*p_sd = sd;
@@ -813,18 +848,56 @@ static struct dentry * sysfs_lookup(struct inode

*dir, struct dentry *dentry,

fs/sysfs/dir.c static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,

Quoted 4 lines
fs/sysfs/dir.c +1 -0
 	return NULL;
 }
 
+static void *sysfs_shadow_follow_link(struct dentry *dentry, struct

nameidata *nd)

fs/sysfs/dir.c static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry, · line 858

Quoted 7 lines
fs/sysfs/dir.c +7 -0
+{
+	struct sysfs_dirent *sd;
+	struct dentry *dest;
+
+	sd = dentry->d_fsdata;
+	dest = NULL;
+	if (sd->s_flags & SYSFS_FLAG_SHADOWED) {

sd->s_flags should be protected by sysfs_mutex. Please don't depend on inode locking for synchronization internal to sysfs.

fs/sysfs/dir.c static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd) · line 953

Quoted 13 lines
fs/sysfs/dir.c +13 -0
+static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
+{
+	struct sysfs_addrm_cxt acxt;
+
+	if (!dir_sd)
+		return;
+
+	pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
+	sysfs_addrm_start(&acxt, dir_sd);
+	if (sysfs_type(dir_sd) == SYSFS_DIR)
+		sysfs_empty_dir(&acxt, dir_sd);
+	else
+		sysfs_remove_shadows(&acxt, dir_sd);

Care to explain this a bit?

fs/sysfs/dir.c void sysfs_remove_dir(struct kobject * kobj) · line 979

Quoted 6 lines
fs/sysfs/dir.c +0 -0
 	sysfs_addrm_finish(&acxt);
 
 	remove_dir(dir_sd);
@@ -882,86 +978,75 @@ void sysfs_remove_dir(struct kobject * kobj)
 
 int sysfs_rename_dir(struct kobject * kobj, const char *new_name)

This rename modification is painful. Please explain why we need to rename nodes between shadows? Can't we just create new ones? Also, please add some comment when performing black magic.

fs/sysfs/dir.c static int sysfs_readdir(struct file * filp,

Quoted 1 line
fs/sysfs/dir.c +0 -0
@@ -1098,8 +1183,11 @@ static int sysfs_readdir(struct file * filp,

void * dirent, filldir_t filldir)

fs/sysfs/dir.c static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir) · line 1187

Quoted 6 lines
fs/sysfs/dir.c +2 -1
 			i++;
 			/* fallthrough */
 		default:
-			mutex_lock(&sysfs_mutex);
+			/* If I am the shadow master return nothing. */
+			if (parent_sd->s_flags & SYSFS_FLAG_SHADOWED)

s_flags protection?

fs/sysfs/dir.c const struct file_operations

Quoted 1 line
fs/sysfs/dir.c +0 -0
@@ -1188,3 +1276,185 @@ const struct file_operations

sysfs_dir_operations = {

fs/sysfs/dir.c const struct file_operations sysfs_dir_operations = { · line 1281

Quoted 6 lines
fs/sysfs/dir.c +3 -0
 	.read		= generic_read_dir,
 	.readdir	= sysfs_readdir,
 };
+
+
+static void sysfs_prune_shadow_sd(struct sysfs_dirent *sd)

Please put this before sysfs_addrm_finish(). That's the only place this function is used.

fs/sysfs/dir.c const struct file_operations · line 1282

Quoted 1 line
fs/sysfs/dir.c +1 -0
+static struct sysfs_dirent *add_shadow_sd(struct sysfs_dirent

*parent_sd, const void *tag)

Quoted 33 lines
fs/sysfs/dir.c +33 -0
+{
+	struct sysfs_dirent *sd = NULL;
+	struct dentry *dir, *shadow;
+	struct inode *inode;
+
+	dir = parent_sd->s_dentry;
+	inode = dir->d_inode;
+
+	shadow = d_alloc(dir->d_parent, &dir->d_name);
+	if (!shadow)
+		goto out;
+
+	/* Since the shadow directory is reachable make it look
+	 * like it is actually hashed.
+	 */
+	shadow->d_hash.pprev = &shadow->d_hash.next;
+	shadow->d_hash.next = NULL;
+	shadow->d_flags &= ~DCACHE_UNHASHED;
+
+	sd = sysfs_new_dirent(tag, parent_sd->s_mode, SYSFS_SHADOW_DIR);
+	if (!sd)
+		goto error;
+
+	sd->s_elem.dir.kobj = parent_sd->s_elem.dir.kobj;
+	sd->s_parent = sysfs_get(parent_sd);
+
+	/* Use the inode number of the parent we are shadowing */
+	sysfs_free_ino(sd->s_ino);
+	sd->s_ino = parent_sd->s_ino;
+
+	inc_nlink(inode);
+	inc_nlink(dir->d_parent->d_inode);
+

fs/sysfs/dir.c const struct file_operations sysfs_dir_operations = { · line 1348

Quoted 10 lines
fs/sysfs/dir.c +10 -0
+	sysfs_link_sibling(sd);
+	__iget(inode);
+	sysfs_instantiate(shadow, inode);
+	sysfs_attach_dentry(sd, shadow);
+out:
+	return sd;
+error:
+	dput(shadow);
+	goto out;
+}

Can we just add sd here and resolve the rest the same way as other nodes such that each shadow has its own dentry and inode? Am I missing something? Also, why do we need the intermediate shadowed sd at all? Can't we do the following?

* non-shadowed case

parent_sd - sd

* shadowed case

parent_sd - sd0

	     sd1
	     sd2

I think we can reduce considerable special case handlings if we do like the above including the implicit shadow creation, parent pruning and symlink tricks. After all, it's just multiple siblings sharing a name which needs some extra context to look up the correct one. We wouldn't even need 'shadow' at all.

Sorry but I don't think the current approach is the correct one. It's too painful and too much complexity is scattered all over the place. I'm afraid this implementation is going to be a maintenance nightmare.

Signature · 1 line
tejun

A message that is not in the archive (sent off-list or not yet received): [email protected]

#24 lore raw

Re: [PATCH 2/4] sysfs: Implement sysfs manged shadow directory support.

Quoted 3 lines · Tejun Heo

Sorry but I don't think the current approach is the correct one. It's too painful and too much complexity is scattered all over the place. I'm afraid this implementation is going to be a maintenance nightmare.

In looking over this again, and due to the fact that there are no in-kernel users of this code, I'm going to drop the other patches in this series and only keep the first one that removes the current implementation.

Eric, is that ok? It gives you time to revisit these changes.

thanks,

greg k-h

A message that is not in the archive (sent off-list or not yet received): [email protected]

#26 lore raw

Re: [PATCH 2/4] sysfs: Implement sysfs manged shadow directory support.

Quoted 11 lines · Greg KH
Quoted 3 lines · Tejun Heo

Sorry but I don't think the current approach is the correct one. It's too painful and too much complexity is scattered all over the place. I'm afraid this implementation is going to be a maintenance nightmare.

In looking over this again, and due to the fact that there are no in-kernel users of this code, I'm going to drop the other patches in this series and only keep the first one that removes the current implementation.

Eric, is that ok? It gives you time to revisit these changes.

Greg a big part of the reason I don't have internal kernel users is keeping up with changes of sysfs has meant I haven't had time to clean up and submit the patches for the users. With a little luck I will have users by 2.6.24. I am in the process of cleaning up those patches right now. So I can send the off to Dave Miller.

So please can we try and at least keep these sysfs patches in a development tree so that people will see them.

Further while there are a few little nits I think mostly Tejun is mostly objecting to the fundamental complexity of the problem rather then to things that can be fixed by a cleaner implementation.

If it didn't take me a week every time I had to update this code after Tejun changes the locking rules in fs/sysfs/dir.c or if there was someone I could delegate the work of maintaining this code to I probably would not mind dropping the patches for a little bit. As it stands I am having horrible nightmares about how the internals of sysfs will be completely different if you drop the last 3 patches by the time I come back and I will need to spend several more weeks just catching up.

Eric

A message that is not in the archive (sent off-list or not yet received): m17iosw075.fsf-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#28 lore raw

Re: [PATCH 2/4] sysfs: Implement sysfs manged shadow directory support.

Quoted 3 lines · Eric W. Biederman

Further while there are a few little nits I think mostly Tejun is mostly objecting to the fundamental complexity of the problem rather then to things that can be fixed by a cleaner implementation.

Oh well, I don't think so but I might be wrong.

Quoted 8 lines

If it didn't take me a week every time I had to update this code after Tejun changes the locking rules in fs/sysfs/dir.c or if there was someone I could delegate the work of maintaining this code to I probably would not mind dropping the patches for a little bit. As it stands I am having horrible nightmares about how the internals of sysfs will be completely different if you drop the last 3 patches by the time I come back and I will need to spend several more weeks just catching up.

Yeah, sysfs has gone through a lot of changes but I think most of internal restructuring is complete now. What's left is removing kobj completely from sysfs internals and interface.

We kind of share the pain here although yours seems much worse than mine. Shadow directories have been major pain in the ass while restructuring sysfs and I basically had to shoot in the dark because there was no in-kernel user. I guess the blame falls on the timing.

I'll give a shot at the no intermediate shadowed directory implementation. I think things will fit a lot easier that way but I really dunno till I try. I'll try to post prototype early.

As long as the current shadow implementation doesn't get into mainline.
I'm okay with it staying in Greg's tree until this is resolved.

Thanks.

Signature · 1 line
tejun

A message that is not in the archive (sent off-list or not yet received): [email protected]

#30 lore raw

Re: [PATCH 2/4] sysfs: Implement sysfs manged shadow directory support.

Quoted 25 lines · Tejun Heo
Quoted 3 lines · Eric W. Biederman

Further while there are a few little nits I think mostly Tejun is mostly objecting to the fundamental complexity of the problem rather then to things that can be fixed by a cleaner implementation.

Oh well, I don't think so but I might be wrong.

Quoted 8 lines

If it didn't take me a week every time I had to update this code after Tejun changes the locking rules in fs/sysfs/dir.c or if there was someone I could delegate the work of maintaining this code to I probably would not mind dropping the patches for a little bit. As it stands I am having horrible nightmares about how the internals of sysfs will be completely different if you drop the last 3 patches by the time I come back and I will need to spend several more weeks just catching up.

Yeah, sysfs has gone through a lot of changes but I think most of internal restructuring is complete now. What's left is removing kobj completely from sysfs internals and interface.

We kind of share the pain here although yours seems much worse than mine. Shadow directories have been major pain in the ass while restructuring sysfs and I basically had to shoot in the dark because there was no in-kernel user. I guess the blame falls on the timing.

Quoted 6 lines

I'll give a shot at the no intermediate shadowed directory implementation. I think things will fit a lot easier that way but I really dunno till I try. I'll try to post prototype early.

As long as the current shadow implementation doesn't get into mainline.
I'm okay with it staying in Greg's tree until this is resolved.

Don't worry, I will not be sending it on to Linus unless you give the ok to do so :)

thanks,

greg k-h

#31 lore raw

Re: [PATCH 2/4] sysfs: Implement sysfs manged shadow directory support.

In reply to a message not in the archive

Hello, Eric.

Quoted 6 lines · Tejun Heo
Quoted 3 lines · Eric W. Biederman

Further while there are a few little nits I think mostly Tejun is mostly objecting to the fundamental complexity of the problem rather then to things that can be fixed by a cleaner implementation.

Oh well, I don't think so but I might be wrong.

And I'm wrong. Mine didn't turn out to be much cleaner than yours. What I did was (still broken)...

* No shadower/shadowee. Each dentry is tagged.
* dentries of tagged sd's are taken out of dcache and always go through ->lookup() where the correct sd is looked up considering the current tag.

Tagging and adding new entries could be done rather cleanly but shooting down existing dentries on rename/move turned out to be a mess. Things will be much simpler if no sysfs dentry is hashed on dcache and always go through ->lookup() but that will hurt big machines.

The basic problem here is that dcache layer doesn't allow different views and sysfs shadow is trying to work behind its back. I don't think this is a viable approach. Both implementations bend too many rules and
are too fragile. It will be a genuine pain in the ass to maintain.

Sorry that I can't come up with an alternative but NACK.

Signature · 1 line
tejun

A message that is not in the archive (sent off-list or not yet received): [email protected]

#33 lore raw

Re: [PATCH 2/4] sysfs: Implement sysfs manged shadow directory support.

Quoted 25 lines · Tejun Heo

Hello, Eric.

Quoted 7 lines · Tejun Heo
Quoted 3 lines · Eric W. Biederman

Further while there are a few little nits I think mostly Tejun is mostly objecting to the fundamental complexity of the problem rather then to things that can be fixed by a cleaner implementation.

Oh well, I don't think so but I might be wrong.

And I'm wrong. Mine didn't turn out to be much cleaner than yours. What I did was (still broken)...

* No shadower/shadowee. Each dentry is tagged.
* dentries of tagged sd's are taken out of dcache and always go through ->lookup() where the correct sd is looked up considering the current tag.

Tagging and adding new entries could be done rather cleanly but shooting down existing dentries on rename/move turned out to be a mess. Things will be much simpler if no sysfs dentry is hashed on dcache and always go through ->lookup() but that will hurt big machines.

Quoted 6 lines

The basic problem here is that dcache layer doesn't allow different views and sysfs shadow is trying to work behind its back. I don't think this is a viable approach. Both implementations bend too many rules and
are too fragile. It will be a genuine pain in the ass to maintain.

Sorry that I can't come up with an alternative but NACK.

Imho then OpenOVZ approach with multiple sysfs trees is better. it allows to use cached dentries with moultiple sysfs mounts each having different view.
It also allows to hide hw-related entries and events from the containers and has quite little modifications in the code.

Thanks,
Kirill

A message that is not in the archive (sent off-list or not yet received): [email protected]

#35 lore raw

Re: [PATCH 2/4] sysfs: Implement sysfs manged shadow directory support.

Hello, Kirill.

Quoted 5 lines · Kirill Korotaev

Imho then OpenOVZ approach with multiple sysfs trees is better. it allows to use cached dentries with moultiple sysfs mounts each having different view.
It also allows to hide hw-related entries and events from the containers and has quite little modifications in the code.

I thought something like supermount plus some twists or fuse based sysfs proxy would fit better. Dunno whether or how uevent and polling stuff can work that way tho. Note that sysfs no longer keeps dentries and inodes pinned. It might make the shared dentry stuff harder.

Thanks.

Signature · 1 line
tejun

A message that is not in the archive (sent off-list or not yet received): [email protected]

#37 lore raw

Re: [PATCH 2/4] sysfs: Implement sysfs manged shadow directory support.

Quoted 11 lines · Tejun Heo

Hello, Kirill.

Quoted 5 lines · Kirill Korotaev

Imho then OpenOVZ approach with multiple sysfs trees is better. it allows to use cached dentries with moultiple sysfs mounts each having different view.
It also allows to hide hw-related entries and events from the containers and has quite little modifications in the code.

Quoted 4 lines

I thought something like supermount plus some twists or fuse based sysfs proxy would fit better. Dunno whether or how uevent and polling stuff can work that way tho. Note that sysfs no longer keeps dentries and inodes pinned. It might make the shared dentry stuff harder.

We simply don't share sysfs dentries/inodes between containers. It's not that frequently used time critical fs to be super-optimized... :) I don't like the idea with fuse, since sysfs exports kernel-related stuff, so doing it via user-space would be pain.

Thanks,
Kirill

A message that is not in the archive (sent off-list or not yet received): [email protected]

#39 lore raw

Re: [PATCH 2/4] sysfs: Implement sysfs manged shadow directory support.

Quoted 8 lines · Kirill Korotaev
Quoted 4 lines · Tejun Heo

I thought something like supermount plus some twists or fuse based sysfs proxy would fit better. Dunno whether or how uevent and polling stuff can work that way tho. Note that sysfs no longer keeps dentries and inodes pinned. It might make the shared dentry stuff harder.

We simply don't share sysfs dentries/inodes between containers. It's not that frequently used time critical fs to be super-optimized... :)

OIC, dentries and inodes are not shared. Good then. Agreed that sysfs doesn't need to be super-optimized as long as big machines aren't penalized too much (both memory and cpu cycle wise).

Quoted 2 lines

I don't like the idea with fuse, since sysfs exports kernel-related stuff, so doing it via user-space would be pain.

Yeah, it would be cumbersome to setup but it's also fast and easy to toy with for prototypes at least.

Thanks.

Signature · 1 line
tejun

A message that is not in the archive (sent off-list or not yet received): [email protected]

#41 lore raw

Re: [PATCH 2/4] sysfs: Implement sysfs manged shadow directory support.

Quoted 14 lines · Tejun Heo
Quoted 8 lines · Kirill Korotaev
Quoted 4 lines · Tejun Heo

I thought something like supermount plus some twists or fuse based sysfs proxy would fit better. Dunno whether or how uevent and polling stuff can work that way tho. Note that sysfs no longer keeps dentries and inodes pinned. It might make the shared dentry stuff harder.

We simply don't share sysfs dentries/inodes between containers. It's not that frequently used time critical fs to be super-optimized... :)

OIC, dentries and inodes are not shared. Good then. Agreed that sysfs doesn't need to be super-optimized as long as big machines aren't penalized too much (both memory and cpu cycle wise).

Quoted 5 lines
Quoted 2 lines

I don't like the idea with fuse, since sysfs exports kernel-related stuff, so doing it via user-space would be pain.

Yeah, it would be cumbersome to setup but it's also fast and easy to toy with for prototypes at least.

How close are we to the point where we can get mount sysfs multiple times and get multiple dentry trees with different super blocks?

That really does sound like the right way to go. Especially as it simplifies the monitoring of containers. If you want to watch what the view looks like in some container your bind mount his sysfs and look at that.

If we can do that the dcache side at least will be beautiful. And with a little care we may be able to reduce the work to a special case in lookup, some extra handling to mark directories as belonging only to a certain mount of sysfs.

If we can find something that is stupid and simple I'm all for that.

To reach the no-kobj utopia we may also need a special device_migrate that is a super set of device_rename (because sometimes we need to rename devices when we move them between namespaces).

So are we close to having a sysfs that we can have multiple super blocks for?

I'm on a sysctl tangent, but I should be able to look at that in just a little bit.

Eric

A message that is not in the archive (sent off-list or not yet received): m1ps29vqin.fsf-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#43 lore raw

Re: [PATCH 2/4] sysfs: Implement sysfs manged shadow directory support.

Ugh. I need to step back and carefully define what I'm seeing but it looks like the current sysfs locking is wrong.

I'm starting to find little inconsistencies all over the place such as:

Which lock actually protects sd->s_children?
- It isn't sysfs_mutex. (see sysfs_lookup)
- It isn't inode->i_mutex (we only get it if we happen to have the inode in core)

At first glance sysfs_assoc_lock looks just as bad.

Eric

A message that is not in the archive (sent off-list or not yet received): m1k5shuugc.fsf-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#45 lore raw

Re: [PATCH 2/4] sysfs: Implement sysfs manged shadow directory support.

Hello,

Quoted 10 lines · Eric W. Biederman

Ugh. I need to step back and carefully define what I'm seeing but it looks like the current sysfs locking is wrong.

I'm starting to find little inconsistencies all over the place such as:

Which lock actually protects sd->s_children?
- It isn't sysfs_mutex. (see sysfs_lookup)
- It isn't inode->i_mutex (we only get it if we happen to have the inode in core)

Yeah, I missed two places while converting to sysfs_mutex. sysfs_lookup() and rename(). I'm about to post patch to fix it.

Quoted 1 line

At first glance sysfs_assoc_lock looks just as bad.

I think sysfs_assoc_lock is okay. It's tricky tho. Why do you think it's bad?

Signature · 1 line
tejun

A message that is not in the archive (sent off-list or not yet received): [email protected]

#47 lore raw

Re: [PATCH 2/4] sysfs: Implement sysfs manged shadow directory support.

Quoted 9 lines · Tejun Heo

Hello,

Quoted 6 lines · Eric W. Biederman

Ugh. I need to step back and carefully define what I'm seeing but it looks like the current sysfs locking is wrong.

I'm starting to find little inconsistencies all over the place such as:

Quoted 7 lines
Quoted 4 lines

Which lock actually protects sd->s_children?
- It isn't sysfs_mutex. (see sysfs_lookup)
- It isn't inode->i_mutex (we only get it if we happen to have the inode in core)

Yeah, I missed two places while converting to sysfs_mutex. sysfs_lookup() and rename(). I'm about to post patch to fix it.

Yes. Make certain to get the name change under sysfs_mutex while you are at it.

What do we use inode->i_mutex for? I think we might be able to kill that.

I'm starting to wonder if we can completely remove sysfs from grabbing inode->i_mutex.

Quoted 4 lines
Quoted 1 line

At first glance sysfs_assoc_lock looks just as bad.

I think sysfs_assoc_lock is okay. It's tricky tho. Why do you think it's bad?

I'm still looking. I just have a weird vibe so far. sysfs_get_dentry is really nasty with respect to locking.

Eric

A message that is not in the archive (sent off-list or not yet received): m1fy35uso4.fsf-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#49 lore raw

Re: [PATCH 2/4] sysfs: Implement sysfs manged shadow directory support.

Quoted 5 lines · Eric W. Biederman

What do we use inode->i_mutex for? I think we might be able to kill that.

I'm starting to wonder if we can completely remove sysfs from grabbing inode->i_mutex.

i_mutex is grabbed when dentry and inode locking requires it. It's not used to protect sysfs internal data structure anymore. I don't think we can remove i_mutex grabbing without violating dentry/inode locking rules.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

At first glance sysfs_assoc_lock looks just as bad.

I think sysfs_assoc_lock is okay. It's tricky tho. Why do you think it's bad?

I'm still looking. I just have a weird vibe so far. sysfs_get_dentry is really nasty with respect to locking.

Yes, sysfs_get_dentry() is pretty hairy. I wish I could use path_lookup() there but can't allocate memory for path name because looking up must succeed when it's called from removal path if dentry already exists. Also, lookup_one_len_kern() bypasses security checks and there's no equivalent path_lookup() like function which does that.

Locking rule aruond sysfs_assoc_lock is tricky. It's mainly used to avoid race condition between sysfs_d_iput() vs. dentry creation, node removal, etc. As long as sysfs_assoc_lock is held, sd->s_dentry can be dereferenced but you also need dcache_lock to determine whether the dentry is alive (dentry->d_inode != NULL) or in the process of being killed. There were two or three race conditions around dentry reclamation in the past and several discussion threads about them.

Thanks.

Signature · 1 line
tejun

A message that is not in the archive (sent off-list or not yet received): [email protected]

#51 lore raw

Re: [PATCH 2/4] sysfs: Implement sysfs manged shadow directory support.

Quoted 10 lines · Tejun Heo
Quoted 5 lines · Eric W. Biederman

What do we use inode->i_mutex for? I think we might be able to kill that.

I'm starting to wonder if we can completely remove sysfs from grabbing inode->i_mutex.

i_mutex is grabbed when dentry and inode locking requires it. It's not used to protect sysfs internal data structure anymore. I don't think we can remove i_mutex grabbing without violating dentry/inode locking rules.

Not entirely no. I think we can remove i_mutex from protecting dentry tree modifications.

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Quoted 1 line

At first glance sysfs_assoc_lock looks just as bad.

I think sysfs_assoc_lock is okay. It's tricky tho. Why do you think it's bad?

Quoted 8 lines
Quoted 2 lines

I'm still looking. I just have a weird vibe so far. sysfs_get_dentry is really nasty with respect to locking.

Yes, sysfs_get_dentry() is pretty hairy. I wish I could use path_lookup() there but can't allocate memory for path name because looking up must succeed when it's called from removal path if dentry already exists. Also, lookup_one_len_kern() bypasses security checks and there's no equivalent path_lookup() like function which does that.

We can use d_hash_and_lookup and that helps a lot. I have attached my in-progress rewrite of sysfs_get_dentry. It's a little less efficient but a whole lot easier to maintain.

Quoted 7 lines

Locking rule aruond sysfs_assoc_lock is tricky. It's mainly used to avoid race condition between sysfs_d_iput() vs. dentry creation, node removal, etc. As long as sysfs_assoc_lock is held, sd->s_dentry can be dereferenced but you also need dcache_lock to determine whether the dentry is alive (dentry->d_inode != NULL) or in the process of being killed. There were two or three race conditions around dentry reclamation in the past and several discussion threads about them.

I think I have figured out how to safely remove s_dentry entirely from sysfs_dirent and that winds up removing a lot of subtle and nasty locking. I'm hoping to have a good patch series after another couple of hours of work.

struct dentry *__sysfs_get_dentry(struct sysfs_dirent *sd, int create)

{
	struct sysfs_dirent *cur;
	struct dentry *parent_dentry, *dentry;
	struct qstr name;
	struct inode *inode;

	parent_dentry = NULL;
	dentry = dget(sysfs_sb->s_root);

	do {
		/* Find the first ancestor I have not looked up */
		cur = sd;
		while (cur->s_parent != dentry->d_fsdata)
			cur = cur->s_parent;

		/* look it up */
		dput(parent_dentry);
		parent_dentry = dentry;
		name.name = cur->s_name;
		name.len = strlen(cur->s_name);
		dentry = d_hash_and_lookup(parent_dentry, &name);
		if (dentry)
			continue;
		if (!create)
			goto out;
		dentry = d_alloc(parent_dentry, &name);
		if (!dentry) {
			dentry = ERR_PTR(-ENOMEM);
			goto out;
		}
		inode = sysfs_get_inode(cur);
		if (!inode) {
			dput(dentry);
			dentry = ERR_PTR(-ENOMEM);
			goto out;
		}
		d_instantiate(dentry, inode);
		sysfs_attach_dentry(cur, dentry);
	} while (cur != sd);

out:

	dput(parent_dentry);
	return dentry;
}

struct dentry *sysfs_get_dentry(struct sysfs_dirent *sd)

{
	struct dentry *dentry;

	mutex_lock(&sysfs_mutex);
	dentry = __sysfs_get_dentry(sd, 1);
	mutex_unlock(&sysfs_mutex);
	return dentry;
}

Eric

A message that is not in the archive (sent off-list or not yet received): m1bqdtt34l.fsf-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#53 lore raw

Re: [PATCH 2/4] sysfs: Implement sysfs manged shadow directory support.

Quoted 4 lines · Eric W. Biederman

I think I have figured out how to safely remove s_dentry entirely from sysfs_dirent and that winds up removing a lot of subtle and nasty locking. I'm hoping to have a good patch series after another couple of hours of work.

Great. Yeah, open coding lookup seems much better idea than checking whether tree changed while sysfs_mutex was releated.

Thanks.

Signature · 1 line
tejun

A message that is not in the archive (sent off-list or not yet received): [email protected]

#55 lore raw

Re: [PATCH 2/4] sysfs: Implement sysfs manged shadow directory support.

Quoted 8 lines · Tejun Heo
Quoted 4 lines · Eric W. Biederman

I think I have figured out how to safely remove s_dentry entirely from sysfs_dirent and that winds up removing a lot of subtle and nasty locking. I'm hoping to have a good patch series after another couple of hours of work.

Great. Yeah, open coding lookup seems much better idea than checking whether tree changed while sysfs_mutex was releated.

s/related/released/

Signature · 1 line
tejun

A message that is not in the archive (sent off-list or not yet received): [email protected]

#57 lore raw

cover sysfs cleanups

Ok. I don't know how alert I am at the moment (it's way past my bed time) but I have been digging in and cleaning up sysfs. I'm not quite to the point of supporting multiple mounts and shadow directories again but the locking is looking much simpler and cleaner. So with a little luck this will be a better base to build on.

If something looks horrible please holler.

Eric

A message that is not in the archive (sent off-list or not yet received): m14pjkubd2.fsf_-_-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#59 lore raw

PATCH 1/14 sysfs: Remove first pass at shadow directory support

While shadow directories appear to be a good idea, the current scheme of controlling their creation and destruction outside of sysfs appears to be a locking and maintenance nightmare in the face of sysfs directories dynamically coming and going. Which can now occur for directories containing network devices when CONFIG_SYSFS_DEPRECATED is not set.

This patch removes everything from the initial shadow directory support that allowed the shadow directory creation to be controlled at a higher level. So except for a few bits of sysfs_rename_dir everything from commit b592fcfe7f06c15ec11774b5be7ce0de3aa86e73 is now gone.

---
I have sanitized my changes to sysfs_rename_dir since last time I posted this patch.

 fs/sysfs/dir.c          |  155 ++--------------------------------------------
 fs/sysfs/group.c        |    1 -
 fs/sysfs/inode.c        |   10 ---
 fs/sysfs/mount.c        |    2 +-
 fs/sysfs/sysfs.h        |    6 --
 include/linux/kobject.h |    5 --
 include/linux/sysfs.h   |   27 +-------
 lib/kobject.c           |   44 ++------------
 8 files changed, 18 insertions(+), 232 deletions(-)
fs/sysfs/dir.c +7 -148
index 048e605..40b9efe 100644
@@ -569,9 +569,6 @@ static void sysfs_drop_dentry(struct sysfs_dirent *sd)
 	spin_unlock(&dcache_lock);
 	spin_unlock(&sysfs_assoc_lock);
 
-	/* dentries for shadowed inodes are pinned, unpin */
-	if (dentry && sysfs_is_shadowed_inode(dentry->d_inode))
-		dput(dentry);
 	dput(dentry);
 
 	/* adjust nlink and update timestamp */
@@ -723,19 +720,15 @@ int sysfs_create_subdir(struct kobject *kobj, const char *name,
 /**
  *	sysfs_create_dir - create a directory for an object.
  *	@kobj:		object we're creating directory for. 
- *	@shadow_parent:	parent object.
  */
-int sysfs_create_dir(struct kobject *kobj,
-		     struct sysfs_dirent *shadow_parent_sd)
+int sysfs_create_dir(struct kobject * kobj)
 {
 	struct sysfs_dirent *parent_sd, *sd;
 	int error = 0;
 
 	BUG_ON(!kobj);
 
-	if (shadow_parent_sd)
-		parent_sd = shadow_parent_sd;
-	else if (kobj->parent)
+	if (kobj->parent)
 		parent_sd = kobj->parent->sd;
 	else if (sysfs_mount && sysfs_mount->mnt_sb)
 		parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata;
@@ -887,8 +880,7 @@ void sysfs_remove_dir(struct kobject * kobj)
 	__sysfs_remove_dir(sd);
 }
 
-int sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd,
-		     const char *new_name)
+int sysfs_rename_dir(struct kobject *kobj, const char *new_name)
 {
 	struct sysfs_dirent *sd = kobj->sd;
 	struct dentry *new_parent = NULL;
@@ -903,11 +895,7 @@ int sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd,
 		goto out_dput;
 	}
 
-	new_parent = sysfs_get_dentry(new_parent_sd);
-	if (IS_ERR(new_parent)) {
-		error = PTR_ERR(new_parent);
-		goto out_dput;
-	}
+	new_parent = dget(old_dentry->d_parent);
 
 	/* lock new_parent and get dentry for new name */
 	mutex_lock(&new_parent->d_inode->i_mutex);
@@ -918,14 +906,8 @@ int sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd,
 		goto out_unlock;
 	}
 
-	/* By allowing two different directories with the same
-	 * d_parent we allow this routine to move between different
-	 * shadows of the same directory
-	 */
 	error = -EINVAL;
-	if (old_dentry->d_parent->d_inode != new_parent->d_inode ||
-	    new_dentry->d_parent->d_inode != new_parent->d_inode ||
-	    old_dentry == new_dentry)
+	if (old_dentry == new_dentry)
 		goto out_unlock;
 
 	error = -EEXIST;
@@ -942,23 +924,15 @@ int sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd,
 	if (error)
 		goto out_drop;
 
+	mutex_lock(&sysfs_mutex);
 	dup_name = sd->s_name;
 	sd->s_name = new_name;
+	mutex_unlock(&sysfs_mutex);
 
 	/* move under the new parent */
 	d_add(new_dentry, NULL);
 	d_move(sd->s_dentry, new_dentry);
 
-	mutex_lock(&sysfs_mutex);
-
-	sysfs_unlink_sibling(sd);
-	sysfs_get(new_parent_sd);
-	sysfs_put(sd->s_parent);
-	sd->s_parent = new_parent_sd;
-	sysfs_link_sibling(sd);
-
-	mutex_unlock(&sysfs_mutex);
-
 	error = 0;
 	goto out_unlock;
 
@@ -1189,121 +1163,6 @@ static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
 	return offset;
 }
 
-
-/**
- *	sysfs_make_shadowed_dir - Setup so a directory can be shadowed
- *	@kobj:	object we're creating shadow of.
- */
-
-int sysfs_make_shadowed_dir(struct kobject *kobj,
-	void * (*follow_link)(struct dentry *, struct nameidata *))
-{
-	struct dentry *dentry;
-	struct inode *inode;
-	struct inode_operations *i_op;
-
-	/* get dentry for @kobj->sd, dentry of a shadowed dir is pinned */
-	dentry = sysfs_get_dentry(kobj->sd);
-	if (IS_ERR(dentry))
-		return PTR_ERR(dentry);
-
-	inode = dentry->d_inode;
-	if (inode->i_op != &sysfs_dir_inode_operations) {
-		dput(dentry);
-		return -EINVAL;
-	}
-
-	i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
-	if (!i_op)
-		return -ENOMEM;
-
-	memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
-	i_op->follow_link = follow_link;
-
-	/* Locking of inode->i_op?
-	 * Since setting i_op is a single word write and they
-	 * are atomic we should be ok here.
-	 */
-	inode->i_op = i_op;
-	return 0;
-}
-
-/**
- *	sysfs_create_shadow_dir - create a shadow directory for an object.
- *	@kobj:	object we're creating directory for.
- *
- *	sysfs_make_shadowed_dir must already have been called on this
- *	directory.
- */
-
-struct sysfs_dirent *sysfs_create_shadow_dir(struct kobject *kobj)
-{
-	struct sysfs_dirent *parent_sd = kobj->sd->s_parent;
-	struct dentry *dir, *parent, *shadow;
-	struct inode *inode;
-	struct sysfs_dirent *sd;
-	struct sysfs_addrm_cxt acxt;
-
-	dir = sysfs_get_dentry(kobj->sd);
-	if (IS_ERR(dir)) {
-		sd = (void *)dir;
-		goto out;
-	}
-	parent = dir->d_parent;
-
-	inode = dir->d_inode;
-	sd = ERR_PTR(-EINVAL);
-	if (!sysfs_is_shadowed_inode(inode))
-		goto out_dput;
-
-	shadow = d_alloc(parent, &dir->d_name);
-	if (!shadow)
-		goto nomem;
-
-	sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR);
-	if (!sd)
-		goto nomem;
-	sd->s_elem.dir.kobj = kobj;
-
-	sysfs_addrm_start(&acxt, parent_sd);
-
-	/* add but don't link into children list */
-	sysfs_add_one(&acxt, sd);
-
-	/* attach and instantiate dentry */
-	sysfs_attach_dentry(sd, shadow);
-	d_instantiate(shadow, igrab(inode));
-	inc_nlink(inode);	/* tj: synchronization? */
-
-	sysfs_addrm_finish(&acxt);
-
-	dget(shadow);		/* Extra count - pin the dentry in core */
-
-	goto out_dput;
-
- nomem:
-	dput(shadow);
-	sd = ERR_PTR(-ENOMEM);
- out_dput:
-	dput(dir);
- out:
-	return sd;
-}
-
-/**
- *	sysfs_remove_shadow_dir - remove an object's directory.
- *	@shadow_sd: sysfs_dirent of shadow directory
- *
- *	The only thing special about this is that we remove any files in
- *	the directory before we remove the directory, and we've inlined
- *	what used to be sysfs_rmdir() below, instead of calling separately.
- */
-
-void sysfs_remove_shadow_dir(struct sysfs_dirent *shadow_sd)
-{
-	__sysfs_remove_dir(shadow_sd);
-}
-
 const struct file_operations sysfs_dir_operations = {
 	.open		= sysfs_dir_open,
 	.release	= sysfs_dir_close,
fs/sysfs/group.c +0 -1
index f318b73..4606f7c 100644
@@ -13,7 +13,6 @@
 #include <linux/dcache.h>
 #include <linux/namei.h>
 #include <linux/err.h>
-#include <linux/fs.h>
 #include <asm/semaphore.h>
 #include "sysfs.h"
 
fs/sysfs/inode.c +0 -10
index 10d1b52..9671164 100644
@@ -34,16 +34,6 @@ static const struct inode_operations sysfs_inode_operations ={
 	.setattr	= sysfs_setattr,
 };
 
-void sysfs_delete_inode(struct inode *inode)
-{
-	/* Free the shadowed directory inode operations */
-	if (sysfs_is_shadowed_inode(inode)) {
-		kfree(inode->i_op);
-		inode->i_op = NULL;
-	}
-	return generic_delete_inode(inode);
-}
-
 int sysfs_setattr(struct dentry * dentry, struct iattr * iattr)
 {
 	struct inode * inode = dentry->d_inode;
fs/sysfs/mount.c +1 -1
index fbc7b65..0c016e1 100644
@@ -21,7 +21,7 @@ struct kmem_cache *sysfs_dir_cachep;
 
 static const struct super_operations sysfs_ops = {
 	.statfs		= simple_statfs,
-	.drop_inode	= sysfs_delete_inode,
+	.drop_inode	= generic_delete_inode,
 };
 
 struct sysfs_dirent sysfs_root = {
fs/sysfs/sysfs.h +0 -6
index 6b8c8d7..b55e510 100644
@@ -70,7 +70,6 @@ extern void sysfs_remove_one(struct sysfs_addrm_cxt *acxt,
 			     struct sysfs_dirent *sd);
 extern int sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt);
 
-extern void sysfs_delete_inode(struct inode *inode);
 extern struct inode * sysfs_get_inode(struct sysfs_dirent *sd);
 extern void sysfs_instantiate(struct dentry *dentry, struct inode *inode);
 
@@ -121,8 +120,3 @@ static inline void sysfs_put(struct sysfs_dirent * sd)
 	if (sd && atomic_dec_and_test(&sd->s_count))
 		release_sysfs_dirent(sd);
 }
-
-static inline int sysfs_is_shadowed_inode(struct inode *inode)
-{
-	return S_ISDIR(inode->i_mode) && inode->i_op->follow_link;
-}
include/linux/kobject.h +0 -5
index aa2fe22..7108a3e 100644
@@ -80,14 +80,9 @@ extern void kobject_init(struct kobject *);
 extern void kobject_cleanup(struct kobject *);
 
 extern int __must_check kobject_add(struct kobject *);
-extern int __must_check kobject_shadow_add(struct kobject *kobj,
-					   struct sysfs_dirent *shadow_parent);
 extern void kobject_del(struct kobject *);
 
 extern int __must_check kobject_rename(struct kobject *, const char *new_name);
-extern int __must_check kobject_shadow_rename(struct kobject *kobj,
-					      struct sysfs_dirent *new_parent,
-					      const char *new_name);
 extern int __must_check kobject_move(struct kobject *, struct kobject *);
 
 extern int __must_check kobject_register(struct kobject *);
include/linux/sysfs.h +4 -23
index be8228e..c16e4c5 100644
@@ -17,9 +17,6 @@
 
 struct kobject;
 struct module;
-struct nameidata;
-struct dentry;
-struct sysfs_dirent;
 
 /* FIXME
  * The *owner field is no longer used, but leave around
@@ -94,14 +91,13 @@ extern int sysfs_schedule_callback(struct kobject *kobj,
 		void (*func)(void *), void *data, struct module *owner);
 
 extern int __must_check
-sysfs_create_dir(struct kobject *kobj, struct sysfs_dirent *shadow_parent_sd);
+sysfs_create_dir(struct kobject *);
 
 extern void
 sysfs_remove_dir(struct kobject *);
 
 extern int __must_check
-sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd,
-		 const char *new_name);
+sysfs_rename_dir(struct kobject *kobj, const char *new_name);
 
 extern int __must_check
 sysfs_move_dir(struct kobject *, struct kobject *);
@@ -138,12 +134,6 @@ void sysfs_remove_file_from_group(struct kobject *kobj,
 
 void sysfs_notify(struct kobject * k, char *dir, char *attr);
 
-
-extern int sysfs_make_shadowed_dir(struct kobject *kobj,
-	void * (*follow_link)(struct dentry *, struct nameidata *));
-extern struct sysfs_dirent *sysfs_create_shadow_dir(struct kobject *kobj);
-extern void sysfs_remove_shadow_dir(struct sysfs_dirent *shadow_sd);
-
 extern int __must_check sysfs_init(void);
 
 #else /* CONFIG_SYSFS */
@@ -154,8 +144,7 @@ static inline int sysfs_schedule_callback(struct kobject *kobj,
 	return -ENOSYS;
 }
 
-static inline int sysfs_create_dir(struct kobject *kobj,
-				   struct sysfs_dirent *shadow_parent_sd)
+static inline int sysfs_create_dir(struct kobject * kobj)
 {
 	return 0;
 }
@@ -165,9 +154,7 @@ static inline void sysfs_remove_dir(struct kobject * k)
 	;
 }
 
-static inline int sysfs_rename_dir(struct kobject *kobj,
-				   struct sysfs_dirent *new_parent_sd,
-				   const char *new_name)
+static inline int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
 {
 	return 0;
 }
@@ -242,12 +229,6 @@ static inline void sysfs_notify(struct kobject * k, char *dir, char *attr)
 {
 }
 
-static inline int sysfs_make_shadowed_dir(struct kobject *kobj,
-	void * (*follow_link)(struct dentry *, struct nameidata *))
-{
-	return 0;
-}
-
 static inline int __must_check sysfs_init(void)
 {
 	return 0;
lib/kobject.c +6 -38

86 lines not on this page · raw message

Signature · 1 line
1.5.1.1.181.g2de0

A message that is not in the archive (sent off-list or not yet received): m1zm1cswp0.fsf_-_-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#61 lore raw

PATCH 2/14 sysfs: In sysfs_lookup protect s_parent with sysfs_mutex

It turns out we have been being silly and have failed to protect ourselves against races between sysfs_lookup and operations that modify the sysfs directory like sysfs_readdir and create_dir.

So this patch modifies sysfs_lookup to grab sysfs_mutex before walking the parent->s_children list.

---
 fs/sysfs/dir.c |   15 +++++++++------
 1 files changed, 9 insertions(+), 6 deletions(-)
fs/sysfs/dir.c +9 -6
index 40b9efe..b27a38c 100644
@@ -756,11 +756,13 @@ static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
 				struct nameidata *nd)
 {
 	struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
+	struct dentry *result = NULL;
 	struct sysfs_dirent * sd;
 	struct bin_attribute *bin_attr;
 	struct inode *inode;
 	int found = 0;
 
+	mutex_lock(&sysfs_mutex);
 	for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) {
 		if (sysfs_type(sd) &&
 		    !strcmp(sd->s_name, dentry->d_name.name)) {
@@ -771,14 +773,14 @@ static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
 
 	/* no such entry */
 	if (!found)
-		return NULL;
+		goto out;
 
 	/* attach dentry and inode */
 	inode = sysfs_get_inode(sd);
-	if (!inode)
-		return ERR_PTR(-ENOMEM);
-
-	mutex_lock(&sysfs_mutex);
+	if (!inode) {
+		result = ERR_PTR(-ENOMEM);
+		goto out;
+	}
 
 	if (inode->i_state & I_NEW) {
 		/* initialize inode according to type */
@@ -808,9 +810,10 @@ static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
 	sysfs_instantiate(dentry, inode);
 	sysfs_attach_dentry(sd, dentry);
 
+out:
 	mutex_unlock(&sysfs_mutex);
 
-	return NULL;
+	return result;
 }
 
 const struct inode_operations sysfs_dir_inode_operations = {
Signature · 1 line
1.5.1.1.181.g2de0

A message that is not in the archive (sent off-list or not yet received): m1vec0swmi.fsf_-_-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#63 lore raw

PATCH 3/14 sysfs: Move all of inode initialization into sysfs_init_inode

Besides being a good general cleanup the ultimate effect of this patch is that sysfs_get_inode now gives you a usable inode that doesn't need any further initialization to be useful.

---
 fs/sysfs/dir.c   |   37 -------------------------------------
 fs/sysfs/inode.c |   48 +++++++++++++++++++++++++++++++++++++++++++++---
 fs/sysfs/mount.c |    5 -----
 3 files changed, 45 insertions(+), 45 deletions(-)
fs/sysfs/dir.c +0 -37
index b27a38c..834ed74 100644
@@ -741,24 +741,12 @@ int sysfs_create_dir(struct kobject * kobj)
 	return error;
 }
 
-static int sysfs_count_nlink(struct sysfs_dirent *sd)
-{
-	struct sysfs_dirent *child;
-	int nr = 0;
-
-	for (child = sd->s_children; child; child = child->s_sibling)
-		if (sysfs_type(child) == SYSFS_DIR)
-			nr++;
-	return nr + 2;
-}
-
 static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
 				struct nameidata *nd)
 {
 	struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
 	struct dentry *result = NULL;
 	struct sysfs_dirent * sd;
-	struct bin_attribute *bin_attr;
 	struct inode *inode;
 	int found = 0;
 
@@ -782,31 +770,6 @@ static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
 		goto out;
 	}
 
-	if (inode->i_state & I_NEW) {
-		/* initialize inode according to type */
-		switch (sysfs_type(sd)) {
-		case SYSFS_DIR:
-			inode->i_op = &sysfs_dir_inode_operations;
-			inode->i_fop = &sysfs_dir_operations;
-			inode->i_nlink = sysfs_count_nlink(sd);
-			break;
-		case SYSFS_KOBJ_ATTR:
-			inode->i_size = PAGE_SIZE;
-			inode->i_fop = &sysfs_file_operations;
-			break;
-		case SYSFS_KOBJ_BIN_ATTR:
-			bin_attr = sd->s_elem.bin_attr.bin_attr;
-			inode->i_size = bin_attr->size;
-			inode->i_fop = &bin_fops;
-			break;
-		case SYSFS_KOBJ_LINK:
-			inode->i_op = &sysfs_symlink_inode_operations;
-			break;
-		default:
-			BUG();
-		}
-	}
-
 	sysfs_instantiate(dentry, inode);
 	sysfs_attach_dentry(sd, dentry);
 
fs/sysfs/inode.c +45 -3

68 lines not on this page · raw message

fs/sysfs/mount.c +0 -5
index 0c016e1..919eaaf 100644
@@ -50,11 +50,6 @@ static int sysfs_fill_super(struct super_block *sb, void *data, int silent)
 		return -ENOMEM;
 	}
 
-	inode->i_op = &sysfs_dir_inode_operations;
-	inode->i_fop = &sysfs_dir_operations;
-	inc_nlink(inode); /* directory, account for "." */
-	unlock_new_inode(inode);
-
 	/* instantiate and link root dentry */
 	root = d_alloc_root(inode);
 	if (!root) {
Signature · 1 line
1.5.1.1.181.g2de0

A message that is not in the archive (sent off-list or not yet received): m1r6moswhr.fsf_-_-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#65 lore raw

PATCH 4/14 sysfs: Remove unnecessary variable found from sysfs_lookup

The subject says it all.

---
 fs/sysfs/dir.c |    7 ++-----
 1 files changed, 2 insertions(+), 5 deletions(-)
fs/sysfs/dir.c +2 -5
index 834ed74..8430633 100644
@@ -748,19 +748,16 @@ static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
 	struct dentry *result = NULL;
 	struct sysfs_dirent * sd;
 	struct inode *inode;
-	int found = 0;
 
 	mutex_lock(&sysfs_mutex);
 	for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) {
 		if (sysfs_type(sd) &&
-		    !strcmp(sd->s_name, dentry->d_name.name)) {
-			found = 1;
+		    !strcmp(sd->s_name, dentry->d_name.name))
 			break;
-		}
 	}
 
 	/* no such entry */
-	if (!found)
+	if (!sd)
 		goto out;
 
 	/* attach dentry and inode */
Signature · 1 line
1.5.1.1.181.g2de0

A message that is not in the archive (sent off-list or not yet received): m1myxcswfr.fsf_-_-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#67 lore raw

PATCH 5/14 sysfs: Remove sysfs_instantiate

Now that sysfs_get_inode is dropping the inode lock we no longer have a need for sysfs_instantiate. So kill it and just use d_instantiate instead.

---
 fs/sysfs/dir.c   |    2 +-
 fs/sysfs/inode.c |   17 -----------------
 fs/sysfs/sysfs.h |    1 -
 3 files changed, 1 insertions(+), 19 deletions(-)
fs/sysfs/dir.c +1 -1
index 8430633..6933f36 100644
@@ -767,7 +767,7 @@ static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
 		goto out;
 	}
 
-	sysfs_instantiate(dentry, inode);
+	d_instantiate(dentry, inode);
 	sysfs_attach_dentry(sd, dentry);
 
 out:
fs/sysfs/inode.c +0 -17
index e832a5d..f55d9da 100644
@@ -212,23 +212,6 @@ struct inode * sysfs_get_inode(struct sysfs_dirent *sd)
 	return inode;
 }
 
-/**
- *	sysfs_instantiate - instantiate dentry
- *	@dentry: dentry to be instantiated
- *	@inode: inode associated with @sd
- *
- *	Unlock @inode if locked and instantiate @dentry with @inode.
- *
- *	LOCKING:
- *	None.
- */
-void sysfs_instantiate(struct dentry *dentry, struct inode *inode)
-{
-	BUG_ON(!dentry || dentry->d_inode);
-
-	d_instantiate(dentry, inode);
-}
-
 int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name)
 {
 	struct sysfs_addrm_cxt acxt;
fs/sysfs/sysfs.h +0 -1
index b55e510..3b4b989 100644
@@ -71,7 +71,6 @@ extern void sysfs_remove_one(struct sysfs_addrm_cxt *acxt,
 extern int sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt);
 
 extern struct inode * sysfs_get_inode(struct sysfs_dirent *sd);
-extern void sysfs_instantiate(struct dentry *dentry, struct inode *inode);
 
 extern void release_sysfs_dirent(struct sysfs_dirent * sd);
 extern struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
Signature · 1 line
1.5.1.1.181.g2de0

A message that is not in the archive (sent off-list or not yet received): m1ir80swdl.fsf_-_-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#69 lore raw

PATCH 6/14 sysfs: Rewrite sysfs_get_dentry

Currently sysfs_get_dentry is very hairy and requires all kinds of locking magic. This patch rewrites sysfs_get_dentry to not use the cached sd->s_dentry, and instead simply lookup and create dcache entries.

This lays the foundation for removing s_dentry and the current hairy sysfs_assoc_lock logic.

---
 fs/sysfs/dir.c |  117 +++++++++++++++++++++++--------------------------------
 1 files changed, 49 insertions(+), 68 deletions(-)
fs/sysfs/dir.c +49 -68
index 6933f36..f22d60c 100644
@@ -20,6 +20,8 @@ spinlock_t sysfs_assoc_lock = SPIN_LOCK_UNLOCKED;
 static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED;
 static DEFINE_IDA(sysfs_ino_ida);
 
+static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry);
+
 /**
  *	sysfs_link_sibling - link sysfs_dirent into sibling list
  *	@sd: sysfs_dirent of interest
@@ -66,10 +68,10 @@ void sysfs_unlink_sibling(struct sysfs_dirent *sd)
  *	sysfs_get_dentry - get dentry for the given sysfs_dirent
  *	@sd: sysfs_dirent of interest
  *
- *	Get dentry for @sd.  Dentry is looked up if currently not
- *	present.  This function climbs sysfs_dirent tree till it
- *	reaches a sysfs_dirent with valid dentry attached and descends
- *	down from there looking up dentry for each step.
+ *	Get dentry for @sd.
+ *
+ *	This function descends the sysfs dentry tree from the root
+ *	populating it if necessary until it reaches the dentry for @sd.
  *
  *	LOCKING:
  *	Kernel thread context (may sleep)
@@ -77,85 +79,58 @@ void sysfs_unlink_sibling(struct sysfs_dirent *sd)
  *	RETURNS:
  *	Pointer to found dentry on success, ERR_PTR() value on error.
  */
-struct dentry *sysfs_get_dentry(struct sysfs_dirent *sd)
+struct dentry *__sysfs_get_dentry(struct sysfs_dirent *sd, int create)
 {
 	struct sysfs_dirent *cur;
 	struct dentry *parent_dentry, *dentry;
-	int i, depth;
-
-	/* Find the first parent which has valid s_dentry and get the
-	 * dentry.
-	 */
-	mutex_lock(&sysfs_mutex);
- restart0:
-	spin_lock(&sysfs_assoc_lock);
- restart1:
-	spin_lock(&dcache_lock);
-
-	dentry = NULL;
-	depth = 0;
-	cur = sd;
-	while (!cur->s_dentry || !cur->s_dentry->d_inode) {
-		if (cur->s_flags & SYSFS_FLAG_REMOVED) {
-			dentry = ERR_PTR(-ENOENT);
-			depth = 0;
-			break;
-		}
-		cur = cur->s_parent;
-		depth++;
-	}
-	if (!IS_ERR(dentry))
-		dentry = dget_locked(cur->s_dentry);
+	struct qstr name;
+	struct inode *inode;
 
-	spin_unlock(&dcache_lock);
-	spin_unlock(&sysfs_assoc_lock);
+	parent_dentry = NULL;
+	dentry = dget(sysfs_sb->s_root);
 
-	/* from the found dentry, look up depth times */
-	while (depth--) {
-		/* find and get depth'th ancestor */
-		for (cur = sd, i = 0; cur && i < depth; i++)
+	do {
+		/* Find the first ancestor I have not looked up */
+		cur = sd;
+		while (cur->s_parent != dentry->d_fsdata)
 			cur = cur->s_parent;
 
-		/* This can happen if tree structure was modified due
-		 * to move/rename.  Restart.
-		 */
-		if (i != depth) {
-			dput(dentry);
-			goto restart0;
-		}
-
-		sysfs_get(cur);
-
-		mutex_unlock(&sysfs_mutex);
-
 		/* look it up */
-		parent_dentry = dentry;
-		dentry = lookup_one_len_kern(cur->s_name, parent_dentry,
-					     strlen(cur->s_name));
 		dput(parent_dentry);
-
-		if (IS_ERR(dentry)) {
-			sysfs_put(cur);
-			return dentry;
+		parent_dentry = dentry;
+		name.name = cur->s_name;
+		name.len = strlen(cur->s_name);
+		dentry = d_hash_and_lookup(parent_dentry, &name);
+		if (dentry)
+			continue;
+		if (!create)
+			goto out;
+		dentry = d_alloc(parent_dentry, &name);
+		if (!dentry) {
+			dentry = ERR_PTR(-ENOMEM);
+			goto out;
 		}
-
-		mutex_lock(&sysfs_mutex);
-		spin_lock(&sysfs_assoc_lock);
-
-		/* This, again, can happen if tree structure has
-		 * changed and we looked up the wrong thing.  Restart.
-		 */
-		if (cur->s_dentry != dentry) {
+		inode = sysfs_get_inode(cur);
+		if (!inode) {
 			dput(dentry);
-			sysfs_put(cur);
-			goto restart1;
+			dentry = ERR_PTR(-ENOMEM);
+			goto out;
 		}
+		d_instantiate(dentry, inode);
+		sysfs_attach_dentry(cur, dentry);
+	} while (cur != sd);
 
-		spin_unlock(&sysfs_assoc_lock);
+out:
+	dput(parent_dentry);
+	return dentry;
+}
 
-		sysfs_put(cur);
-	}
+struct dentry *sysfs_get_dentry(struct sysfs_dirent *sd)
+{
+	struct dentry *dentry;
 
+	mutex_lock(&sysfs_mutex);
+	dentry = __sysfs_get_dentry(sd, 1);
 	mutex_unlock(&sysfs_mutex);
 	return dentry;
 }
@@ -750,6 +725,12 @@ static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
 	struct inode *inode;
 
 	mutex_lock(&sysfs_mutex);
+
+	/* Guard against races with sysfs_get_dentry */
+	result = d_hash_and_lookup(dentry->d_parent, &dentry->d_name);
+	if (result)
+		goto out;
+
 	for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) {
 		if (sysfs_type(sd) &&
 		    !strcmp(sd->s_name, dentry->d_name.name))
Signature · 1 line
1.5.1.1.181.g2de0

A message that is not in the archive (sent off-list or not yet received): m1ejioswai.fsf_-_-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#71 lore raw

PATCH 7/14 vfs: Remove lookup_one_len_kern

Now that sysfs no longer uses lookup_one_len_kern the function has no users so remove it from the kernel.

---
 fs/namei.c            |   46 +++++++++++-----------------------------------
 include/linux/namei.h |    1 -
 2 files changed, 11 insertions(+), 36 deletions(-)
fs/namei.c +11 -35
index a83160a..69d3304 100644
@@ -1273,7 +1273,12 @@ int __user_path_lookup_open(const char __user *name, unsigned int lookup_flags,
 	return err;
 }
 
-static inline struct dentry *__lookup_hash_kern(struct qstr *name, struct dentry *base, struct nameidata *nd)
+/*
+ * Restricted form of lookup. Doesn't follow links, single-component only,
+ * needs parent already locked. Doesn't follow mounts.
+ * SMP-safe.
+ */
+static inline struct dentry * __lookup_hash(struct qstr *name, struct dentry *base, struct nameidata *nd)
 {
 	struct dentry *dentry;
 	struct inode *inode;
@@ -1281,6 +1286,11 @@ static inline struct dentry *__lookup_hash_kern(struct qstr *name, struct dentry
 
 	inode = base->d_inode;
 
+	err = permission(inode, MAY_EXEC, nd);
+	dentry = ERR_PTR(err);
+	if (err)
+		goto out;
+
 	/*
 	 * See if the low-level filesystem might want
 	 * to use its own hash..
@@ -1308,29 +1318,6 @@ out:
 	return dentry;
 }
 
-/*
- * Restricted form of lookup. Doesn't follow links, single-component only,
- * needs parent already locked. Doesn't follow mounts.
- * SMP-safe.
- */
-static inline struct dentry * __lookup_hash(struct qstr *name, struct dentry *base, struct nameidata *nd)
-{
-	struct dentry *dentry;
-	struct inode *inode;
-	int err;
-
-	inode = base->d_inode;
-
-	err = permission(inode, MAY_EXEC, nd);
-	dentry = ERR_PTR(err);
-	if (err)
-		goto out;
-
-	dentry = __lookup_hash_kern(name, base, nd);
-out:
-	return dentry;
-}
-
 static struct dentry *lookup_hash(struct nameidata *nd)
 {
 	return __lookup_hash(&nd->last, nd->dentry, nd);
@@ -1369,17 +1356,6 @@ struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
 	return __lookup_hash(&this, base, NULL);
 }
 
-struct dentry *lookup_one_len_kern(const char *name, struct dentry *base, int len)
-{
-	int err;
-	struct qstr this;
-
-	err = __lookup_one_len(name, &this, base, len);
-	if (err)
-		return ERR_PTR(err);
-	return __lookup_hash_kern(&this, base, NULL);
-}
-
 int fastcall __user_walk_fd(int dfd, const char __user *name, unsigned flags,
 			    struct nameidata *nd)
 {
include/linux/namei.h +0 -1
index 6c38efb..36e5690 100644
@@ -82,7 +82,6 @@ extern struct file *nameidata_to_filp(struct nameidata *nd, int flags);
 extern void release_open_intent(struct nameidata *);
 
 extern struct dentry * lookup_one_len(const char *, struct dentry *, int);
-extern struct dentry *lookup_one_len_kern(const char *, struct dentry *, int);
 
 extern int follow_down(struct vfsmount **, struct dentry **);
 extern int follow_up(struct vfsmount **, struct dentry **);
Signature · 1 line
1.5.1.1.181.g2de0

A message that is not in the archive (sent off-list or not yet received): m1abtcsw8w.fsf_-_-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#73 lore raw

PATCH 8/14 sysfs: Perform renames under sysfs_mutex

This modifies sysfs_rename_dir to use sysfs_addrm_start/sysfs_addrm_finish making it more like the rest of the sysfs directory manipulation functions.

This moves the entire rename operation inside of sysfs_mutex removing the need to grab i_mutex, and incidentally this kills one s_dentry use.

---
 fs/sysfs/dir.c |   46 +++++++++++++++++-----------------------------
 1 files changed, 17 insertions(+), 29 deletions(-)
fs/sysfs/dir.c +17 -29
index f22d60c..8e614d3 100644
@@ -827,7 +827,7 @@ void sysfs_remove_dir(struct kobject * kobj)
 int sysfs_rename_dir(struct kobject *kobj, const char *new_name)
 {
 	struct sysfs_dirent *sd = kobj->sd;
-	struct dentry *new_parent = NULL;
+	struct sysfs_addrm_cxt acxt;
 	struct dentry *old_dentry = NULL, *new_dentry = NULL;
 	const char *dup_name = NULL;
 	int error;
@@ -836,59 +836,47 @@ int sysfs_rename_dir(struct kobject *kobj, const char *new_name)
 	old_dentry = sysfs_get_dentry(sd);
 	if (IS_ERR(old_dentry)) {
 		error = PTR_ERR(old_dentry);
-		goto out_dput;
+		goto out;
 	}
 
-	new_parent = dget(old_dentry->d_parent);
-
-	/* lock new_parent and get dentry for new name */
-	mutex_lock(&new_parent->d_inode->i_mutex);
-
-	new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
-	if (IS_ERR(new_dentry)) {
-		error = PTR_ERR(new_dentry);
-		goto out_unlock;
-	}
+	sysfs_addrm_start(&acxt, sd->s_parent);
 
 	error = -EINVAL;
-	if (old_dentry == new_dentry)
-		goto out_unlock;
+	if (strcmp(new_name, sd->s_name) == 0)
+		goto addrm_finish;
 
 	error = -EEXIST;
-	if (new_dentry->d_inode)
-		goto out_unlock;
+	if (sysfs_find_dirent(acxt.parent_sd, new_name))
+		goto addrm_finish;
+
+	new_dentry = d_alloc_name(old_dentry->d_parent, new_name);
+	if (!new_dentry)
+		goto addrm_finish;
 
 	/* rename kobject and sysfs_dirent */
 	error = -ENOMEM;
 	new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
 	if (!new_name)
-		goto out_drop;
+		goto addrm_finish;
 
 	error = kobject_set_name(kobj, "%s", new_name);
 	if (error)
-		goto out_drop;
+		goto addrm_finish;
 
-	mutex_lock(&sysfs_mutex);
 	dup_name = sd->s_name;
 	sd->s_name = new_name;
-	mutex_unlock(&sysfs_mutex);
 
 	/* move under the new parent */
 	d_add(new_dentry, NULL);
-	d_move(sd->s_dentry, new_dentry);
+	d_move(old_dentry, new_dentry);
 
 	error = 0;
-	goto out_unlock;
-
- out_drop:
-	d_drop(new_dentry);
- out_unlock:
-	mutex_unlock(&new_parent->d_inode->i_mutex);
- out_dput:
+addrm_finish:
+	sysfs_addrm_finish(&acxt);
 	kfree(dup_name);
-	dput(new_parent);
 	dput(old_dentry);
 	dput(new_dentry);
+out:
 	return error;
 }
 
Signature · 1 line
1.5.1.1.181.g2de0

A message that is not in the archive (sent off-list or not yet received): m16440sw6d.fsf_-_-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#75 lore raw

PATCH 9/14 sysfs: Move all of sysfs_move_dir under sysfs_mutex

This patch modifies sysfs_move_dir to perform all of it's operations under the sysfs_mutex. By looking for conflicts using sysfs_find_dirent we accidentally moving something onto a name that already exists but just not in the dcache right now.

Two s_dentry usages are killed.

And it has probably become unnecessary to grab i_mutex at all but I'm not brave enough to do that just yet.

---
 fs/sysfs/dir.c |   24 +++++++++++++-----------
 1 files changed, 13 insertions(+), 11 deletions(-)
fs/sysfs/dir.c +13 -11
index 8e614d3..ed2e6f3 100644
@@ -897,7 +897,7 @@ int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
 		error = PTR_ERR(old_dentry);
 		goto out_dput;
 	}
-	old_parent = sd->s_parent->s_dentry;
+	old_parent = old_dentry->d_parent;
 
 	new_parent = sysfs_get_dentry(new_parent_sd);
 	if (IS_ERR(new_parent)) {
@@ -915,29 +915,31 @@ again:
 		mutex_unlock(&old_parent->d_inode->i_mutex);
 		goto again;
 	}
+	mutex_lock(&sysfs_mutex);
 
-	new_dentry = lookup_one_len(kobj->name, new_parent, strlen(kobj->name));
-	if (IS_ERR(new_dentry)) {
-		error = PTR_ERR(new_dentry);
+	error = -EEXIST;
+	if (sysfs_find_dirent(new_parent_sd, kobj->name))
 		goto out_unlock;
-	} else
-		error = 0;
+
+	error = -ENOMEM;
+	new_dentry = d_alloc_name(new_parent, kobj->name);
+	if (!new_dentry)
+		goto out_unlock;
+
+	error = 0;
 	d_add(new_dentry, NULL);
-	d_move(sd->s_dentry, new_dentry);
+	d_move(old_dentry, new_dentry);
 	dput(new_dentry);
 
 	/* Remove from old parent's list and insert into new parent's list. */
-	mutex_lock(&sysfs_mutex);
-
 	sysfs_unlink_sibling(sd);
 	sysfs_get(new_parent_sd);
 	sysfs_put(sd->s_parent);
 	sd->s_parent = new_parent_sd;
 	sysfs_link_sibling(sd);
 
-	mutex_unlock(&sysfs_mutex);
-
  out_unlock:
+	mutex_unlock(&sysfs_mutex);
 	mutex_unlock(&new_parent->d_inode->i_mutex);
 	mutex_unlock(&old_parent->d_inode->i_mutex);
  out_dput:
Signature · 1 line
1.5.1.1.181.g2de0

A message that is not in the archive (sent off-list or not yet received): m11weosvzz.fsf_-_-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#77 lore raw

PATCH 10/14 sysfs: Rework sysfs_drop_dentry

This modifies sysfs_drop_dentry so it no longers needs the sysfs_assoc_lock and it's assorted challenges.

The principal idea is that __sysfs_get_dentry when not creating dentry will return a dentry for the sysfs_dirent if there is one in the
dcache. If we do get a dentry we can drop it and force it out
of the cache.

For good measure I am also calling shrink_dcache_parent which will force all of the child dentries out of the dcache as well if it is a directory.

This is essentially and adaption of proc_flush_task from procfs to handle the similar problems of sysfs.

---
 fs/sysfs/dir.c |   34 ++++++++++++----------------------
 1 files changed, 12 insertions(+), 22 deletions(-)
fs/sysfs/dir.c +12 -22
index ed2e6f3..f8de6fb 100644
@@ -516,35 +516,25 @@ void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
  *	parent on entry to this function such that it can't be looked
  *	up anymore.
  *
- *	@sd->s_dentry which is protected with sysfs_assoc_lock points
- *	to the currently associated dentry but we're not holding a
- *	reference to it and racing with dput().  Grab dcache_lock and
- *	verify dentry before dropping it.  If @sd->s_dentry is NULL or
- *	dput() beats us, no need to bother.
+ *	We find the dentry for @sd by probing the dcache while holding
+ *	sysfs_mutex to keep it from changing until we are done.
  */
 static void sysfs_drop_dentry(struct sysfs_dirent *sd)
 {
 	struct dentry *dentry = NULL;
 	struct inode *inode;
 
-	/* We're not holding a reference to ->s_dentry dentry but the
-	 * field will stay valid as long as sysfs_assoc_lock is held.
-	 */
-	spin_lock(&sysfs_assoc_lock);
-	spin_lock(&dcache_lock);
-
-	/* drop dentry if it's there and dput() didn't kill it yet */
-	if (sd->s_dentry && sd->s_dentry->d_inode) {
-		dentry = dget_locked(sd->s_dentry);
-		spin_lock(&dentry->d_lock);
-		__d_drop(dentry);
-		spin_unlock(&dentry->d_lock);
+	/* Remove a dentry for a sd from the dcache if present */
+	mutex_lock(&sysfs_mutex);
+	dentry = __sysfs_get_dentry(sd, 0);
+	if (IS_ERR(dentry))
+		dentry = NULL;
+	if (dentry) {
+		shrink_dcache_parent(dentry);
+		d_drop(dentry);
+		dput(dentry);
 	}
-
-	spin_unlock(&dcache_lock);
-	spin_unlock(&sysfs_assoc_lock);
-
-	dput(dentry);
+	mutex_unlock(&sysfs_mutex);
 
 	/* adjust nlink and update timestamp */
 	inode = ilookup(sysfs_sb, sd->s_ino);
Signature · 1 line
1.5.1.1.181.g2de0

A message that is not in the archive (sent off-list or not yet received): m1wswgrh8x.fsf_-_-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#79 lore raw

PATCH 11/14 sysfs: Remove s_dentry form sysfs_dirent.

After the last round of refactoring nothing is really using using s_dirent so remove it. Which makes the code easier to work with and follow. And with a little luck makes it easier to support multiple dentry trees for sysfs.

---
 fs/sysfs/dir.c   |   26 +-------------------------
 fs/sysfs/mount.c |    1 -
 fs/sysfs/sysfs.h |    1 -
 3 files changed, 1 insertions(+), 27 deletions(-)
fs/sysfs/dir.c +1 -25
index f8de6fb..c17e601 100644
@@ -310,22 +310,7 @@ static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
 {
 	struct sysfs_dirent * sd = dentry->d_fsdata;
 
-	if (sd) {
-		/* sd->s_dentry is protected with sysfs_assoc_lock.
-		 * This allows sysfs_drop_dentry() to dereference it.
-		 */
-		spin_lock(&sysfs_assoc_lock);
-
-		/* The dentry might have been deleted or another
-		 * lookup could have happened updating sd->s_dentry to
-		 * point the new dentry.  Ignore if it isn't pointing
-		 * to this dentry.
-		 */
-		if (sd->s_dentry == dentry)
-			sd->s_dentry = NULL;
-		spin_unlock(&sysfs_assoc_lock);
-		sysfs_put(sd);
-	}
+	sysfs_put(sd);
 	iput(inode);
 }
 
@@ -373,9 +358,6 @@ struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
  *	@sd: target sysfs_dirent
  *	@dentry: dentry to associate
  *
- *	Associate @sd with @dentry.  This is protected by
- *	sysfs_assoc_lock to avoid race with sysfs_d_iput().
- *
  *	LOCKING:
  *	mutex_lock(sysfs_mutex)
  */
@@ -383,12 +365,6 @@ static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry)
 {
 	dentry->d_op = &sysfs_dentry_ops;
 	dentry->d_fsdata = sysfs_get(sd);
-
-	/* protect sd->s_dentry against sysfs_d_iput */
-	spin_lock(&sysfs_assoc_lock);
-	sd->s_dentry = dentry;
-	spin_unlock(&sysfs_assoc_lock);
-
 	d_rehash(dentry);
 }
 
fs/sysfs/mount.c +0 -1
index 919eaaf..3f652be 100644
@@ -57,7 +57,6 @@ static int sysfs_fill_super(struct super_block *sb, void *data, int silent)
 		iput(inode);
 		return -ENOMEM;
 	}
-	sysfs_root.s_dentry = root;
 	root->d_fsdata = &sysfs_root;
 	sb->s_root = root;
 	return 0;
fs/sysfs/sysfs.h +0 -1
index 3b4b989..60af864 100644
@@ -37,7 +37,6 @@ struct sysfs_dirent {
 	unsigned int		s_flags;
 	umode_t			s_mode;
 	ino_t			s_ino;
-	struct dentry		* s_dentry;
 	struct iattr		* s_iattr;
 	atomic_t		s_event;
 };
Signature · 1 line
1.5.1.1.181.g2de0

A message that is not in the archive (sent off-list or not yet received): m1sl74rh6e.fsf_-_-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#81 lore raw

PATCH 12/14 sysfs: Make sysfs_mount static

This patch modifies the users of sysfs_mount to use sysfs_root instead (which is what they are looking for). It then makes sysfs_mount static to keep people from using it by accident.

The net result is slightly faster and clearer code.

---
 fs/sysfs/dir.c     |    4 +---
 fs/sysfs/mount.c   |    2 +-
 fs/sysfs/symlink.c |    7 +++----
 fs/sysfs/sysfs.h   |    1 -
 4 files changed, 5 insertions(+), 9 deletions(-)
fs/sysfs/dir.c +1 -3
index c17e601..1611cbd 100644
@@ -671,10 +671,8 @@ int sysfs_create_dir(struct kobject * kobj)
 
 	if (kobj->parent)
 		parent_sd = kobj->parent->sd;
-	else if (sysfs_mount && sysfs_mount->mnt_sb)
-		parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata;
 	else
-		return -EFAULT;
+		parent_sd = &sysfs_root;
 
 	error = create_dir(kobj, parent_sd, kobject_name(kobj), &sd);
 	if (!error)
fs/sysfs/mount.c +1 -1
index 3f652be..0ad731b 100644
@@ -15,7 +15,7 @@
 /* Random magic number */
 #define SYSFS_MAGIC 0x62656572
 
-struct vfsmount *sysfs_mount;
+static struct vfsmount *sysfs_mount;
 struct super_block * sysfs_sb = NULL;
 struct kmem_cache *sysfs_dir_cachep;
 
fs/sysfs/symlink.c +3 -4
index 4ce687f..2b542dc 100644
@@ -60,10 +60,9 @@ int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char
 
 	BUG_ON(!name);
 
-	if (!kobj) {
-		if (sysfs_mount && sysfs_mount->mnt_sb)
-			parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata;
-	} else
+	if (!kobj)
+		parent_sd = &sysfs_root;
+	else
 		parent_sd = kobj->sd;
 
 	error = -EFAULT;
fs/sysfs/sysfs.h +0 -1
index 60af864..265a16a 100644
@@ -50,7 +50,6 @@ struct sysfs_addrm_cxt {
 	int			cnt;
 };
 
-extern struct vfsmount * sysfs_mount;
 extern struct sysfs_dirent sysfs_root;
 extern struct kmem_cache *sysfs_dir_cachep;
 
Signature · 1 line
1.5.1.1.181.g2de0

A message that is not in the archive (sent off-list or not yet received): m1odhsrh46.fsf_-_-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#83 lore raw

PATCH 13/14 sysfs: Simplify readdir.

At some point someone wrote sysfs_readdir to insert a cursor into the list of sysfs_dirents to ensure that sysfs_readdir would restart properly. That works but it is complex code and tends to be expensive.

The same effect can be achived by keeping the sysfs_dirents in inode order and using the inode number as the f_pos. Then when we restart we just have to find the first dirent whose inode number is equal or greater then the last sysfs_dirent we attempted to return.

Removing the sysfs directory cursor also allows the removal of
all of the mysterious checks for sysfs_type(sd) != 0. Which
were nonbovious checks to see if a cursor was in a directory list.

---
 fs/sysfs/dir.c   |  178 ++++++++++++++----------------------------------------
 fs/sysfs/inode.c |    2 -
 2 files changed, 45 insertions(+), 135 deletions(-)
fs/sysfs/dir.c +45 -133
index 1611cbd..f9be856 100644
@@ -35,10 +35,20 @@ static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry);
 void sysfs_link_sibling(struct sysfs_dirent *sd)
 {
 	struct sysfs_dirent *parent_sd = sd->s_parent;
+	struct sysfs_dirent **pos;
 
 	BUG_ON(sd->s_sibling);
-	sd->s_sibling = parent_sd->s_children;
-	parent_sd->s_children = sd;
+
+	/* Store directory entries in order by ino.  This allows
+	 * readdir to properly restart without having to add a
+	 * cursor into the s_children list.
+	 */
+	for (pos = &parent_sd->s_children; *pos; pos = &(*pos)->s_sibling) {
+		if (sd->s_ino < (*pos)->s_ino)
+			break;
+	}
+	sd->s_sibling = *pos;
+	*pos = sd;
 }
 
 /**
@@ -590,7 +600,7 @@ struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
 	struct sysfs_dirent *sd;
 
 	for (sd = parent_sd->s_children; sd; sd = sd->s_sibling)
-		if (sysfs_type(sd) && !strcmp(sd->s_name, name))
+		if (!strcmp(sd->s_name, name))
 			return sd;
 	return NULL;
 }
@@ -696,8 +706,7 @@ static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
 		goto out;
 
 	for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) {
-		if (sysfs_type(sd) &&
-		    !strcmp(sd->s_name, dentry->d_name.name))
+		if (!strcmp(sd->s_name, dentry->d_name.name))
 			break;
 	}
 
@@ -756,7 +765,7 @@ static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
 	while (*pos) {
 		struct sysfs_dirent *sd = *pos;
 
-		if (sysfs_type(sd) && sysfs_type(sd) != SYSFS_DIR) {
+		if (sysfs_type(sd) != SYSFS_DIR) {
 			*pos = sd->s_sibling;
 			sd->s_sibling = NULL;
 			sysfs_remove_one(&acxt, sd);
@@ -913,37 +922,6 @@ again:
 	return error;
 }
 
-static int sysfs_dir_open(struct inode *inode, struct file *file)
-{
-	struct dentry * dentry = file->f_path.dentry;
-	struct sysfs_dirent * parent_sd = dentry->d_fsdata;
-	struct sysfs_dirent * sd;
-
-	sd = sysfs_new_dirent("_DIR_", 0, 0);
-	if (sd) {
-		mutex_lock(&sysfs_mutex);
-		sd->s_parent = sysfs_get(parent_sd);
-		sysfs_link_sibling(sd);
-		mutex_unlock(&sysfs_mutex);
-	}
-
-	file->private_data = sd;
-	return sd ? 0 : -ENOMEM;
-}
-
-static int sysfs_dir_close(struct inode *inode, struct file *file)
-{
-	struct sysfs_dirent * cursor = file->private_data;
-
-	mutex_lock(&sysfs_mutex);
-	sysfs_unlink_sibling(cursor);
-	mutex_unlock(&sysfs_mutex);
-
-	release_sysfs_dirent(cursor);
-
-	return 0;
-}
-
 /* Relationship between s_mode and the DT_xxx types */
 static inline unsigned char dt_type(struct sysfs_dirent *sd)
 {
@@ -954,117 +932,51 @@ static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
 {
 	struct dentry *dentry = filp->f_path.dentry;
 	struct sysfs_dirent * parent_sd = dentry->d_fsdata;
-	struct sysfs_dirent *cursor = filp->private_data;
-	struct sysfs_dirent **pos;
+	struct sysfs_dirent *pos;
 	ino_t ino;
-	int i = filp->f_pos;
 
-	switch (i) {
-		case 0:
-			ino = parent_sd->s_ino;
-			if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
-				break;
+	if (filp->f_pos == 0) {
+		ino = parent_sd->s_ino;
+		if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) == 0)
 			filp->f_pos++;
-			i++;
-			/* fallthrough */
-		case 1:
-			if (parent_sd->s_parent)
-				ino = parent_sd->s_parent->s_ino;
-			else
-				ino = parent_sd->s_ino;
-			if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
-				break;
+	}
+	if (filp->f_pos == 1) {
+		if (parent_sd->s_parent)
+			ino = parent_sd->s_parent->s_ino;
+		else
+			ino = parent_sd->s_ino;
+		if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) == 0)
 			filp->f_pos++;
-			i++;
-			/* fallthrough */
-		default:
-			mutex_lock(&sysfs_mutex);
-
-			pos = &parent_sd->s_children;
-			while (*pos != cursor)
-				pos = &(*pos)->s_sibling;
-
-			/* unlink cursor */
-			*pos = cursor->s_sibling;
-
-			if (filp->f_pos == 2)
-				pos = &parent_sd->s_children;
-
-			for ( ; *pos; pos = &(*pos)->s_sibling) {
-				struct sysfs_dirent *next = *pos;
-				const char * name;
-				int len;
-
-				if (!sysfs_type(next))
-					continue;
-
-				name = next->s_name;
-				len = strlen(name);
-				ino = next->s_ino;
-
-				if (filldir(dirent, name, len, filp->f_pos, ino,
-						 dt_type(next)) < 0)
-					break;
-
-				filp->f_pos++;
-			}
+	}
+	if ((filp->f_pos > 1) && (filp->f_pos < UINT_MAX)) {
+		mutex_lock(&sysfs_mutex);
 
-			/* put cursor back in */
-			cursor->s_sibling = *pos;
-			*pos = cursor;
+		/* Skip the dentries we have already reported */
+		pos = parent_sd->s_children;
+		while (pos && (filp->f_pos > pos->s_ino))
+			pos = pos->s_sibling;
 
-			mutex_unlock(&sysfs_mutex);
-	}
-	return 0;
-}
+		for ( ; pos; pos = pos->s_sibling) {
+			const char * name;
+			int len;
 
-static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
-{
-	struct dentry * dentry = file->f_path.dentry;
+			name = pos->s_name;
+			len = strlen(name);
+			filp->f_pos = ino = pos->s_ino;
 
-	switch (origin) {
-		case 1:
-			offset += file->f_pos;
-		case 0:
-			if (offset >= 0)
+			if (filldir(dirent, name, len, filp->f_pos, ino,
+					 dt_type(pos)) < 0)
 				break;
-		default:
-			return -EINVAL;
-	}
-	if (offset != file->f_pos) {
-		mutex_lock(&sysfs_mutex);
-
-		file->f_pos = offset;
-		if (file->f_pos >= 2) {
-			struct sysfs_dirent *sd = dentry->d_fsdata;
-			struct sysfs_dirent *cursor = file->private_data;
-			struct sysfs_dirent **pos;
-			loff_t n = file->f_pos - 2;
-
-			sysfs_unlink_sibling(cursor);
-
-			pos = &sd->s_children;
-			while (n && *pos) {
-				struct sysfs_dirent *next = *pos;
-				if (sysfs_type(next))
-					n--;
-				pos = &(*pos)->s_sibling;
-			}
-
-			cursor->s_sibling = *pos;
-			*pos = cursor;
 		}
-
+		if (!pos)
+			filp->f_pos = UINT_MAX;
 		mutex_unlock(&sysfs_mutex);
 	}
-
-	return offset;
+	return 0;
 }
 
+
 const 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,
 };
fs/sysfs/inode.c +0 -2
index f55d9da..e212c69 100644
@@ -225,8 +225,6 @@ int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name)
 	for (pos = &dir_sd->s_children; *pos; pos = &(*pos)->s_sibling) {
 		sd = *pos;
 
-		if (!sysfs_type(sd))
-			continue;
 		if (!strcmp(sd->s_name, name)) {
 			*pos = sd->s_sibling;
 			sd->s_sibling = NULL;
Signature · 1 line
1.5.1.1.181.g2de0

A message that is not in the archive (sent off-list or not yet received): m1k5sgrh1h.fsf_-_-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#85 lore raw

PATCH 14/14 sysfs: In sysfs_lookup don't open code sysfs_find_dirent

This is a small cleanup patch that makes the code just a little bit cleaner.

---
 fs/sysfs/dir.c |    5 +----
 1 files changed, 1 insertions(+), 4 deletions(-)
fs/sysfs/dir.c +1 -4
index f9be856..66d418a 100644
@@ -705,10 +705,7 @@ static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
 	if (result)
 		goto out;
 
-	for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) {
-		if (!strcmp(sd->s_name, dentry->d_name.name))
-			break;
-	}
+	sd = sysfs_find_dirent(parent_sd, dentry->d_name.name);
 
 	/* no such entry */
 	if (!sd)
Signature · 1 line
1.5.1.1.181.g2de0

A message that is not in the archive (sent off-list or not yet received): m1fy34rh08.fsf_-_-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

Acked-by
#87 lore raw

Re: [PATCH 14/14] sysfs: In sysfs_lookup don't open code sysfs_find_dirent

Quoted 4 lines · Eric W. Biederman

This is a small cleanup patch that makes the code just a little bit cleaner.

I think this series of patches need wider review than Linux Containers mailing list. Please cc LKML, [email protected] and stern-nwvwT67g6+6dFdvTe/[email protected] when posting the next round.

Thanks.

Signature · 1 line
tejun

A message that is not in the archive (sent off-list or not yet received): [email protected]

#89 lore raw

Re: [PATCH 14/14] sysfs: In sysfs_lookup don't open code sysfs_find_dirent

Quoted 11 lines · Tejun Heo
Quoted 4 lines · Eric W. Biederman

This is a small cleanup patch that makes the code just a little bit cleaner.

I think this series of patches need wider review than Linux Containers mailing list. Please cc LKML, [email protected] and stern-nwvwT67g6+6dFdvTe/[email protected] when posting the next round.

Sure. I was just Cc'ing those I could think of and I was tired.

Eric

#90 lore raw

Re: [PATCH 13/14] sysfs: Simplify readdir.

In reply to a message not in the archive
Quoted 11 lines · Eric W. Biederman

At some point someone wrote sysfs_readdir to insert a cursor into the list of sysfs_dirents to ensure that sysfs_readdir would restart properly. That works but it is complex code and tends to be expensive.

The same effect can be achived by keeping the sysfs_dirents in inode order and using the inode number as the f_pos. Then when we restart we just have to find the first dirent whose inode number is equal or greater then the last sysfs_dirent we attempted to return.

Quoted 5 lines

Removing the sysfs directory cursor also allows the removal of
all of the mysterious checks for sysfs_type(sd) != 0. Which
were nonbovious checks to see if a cursor was in a directory list.

Great, Acked-by: Tejun Heo <[email protected]>

Signature · 1 line
tejun
Acked-by
#91 lore raw

Re: [PATCH 12/14] sysfs: Make sysfs_mount static

In reply to a message not in the archive
Quoted 7 lines · Eric W. Biederman

This patch modifies the users of sysfs_mount to use sysfs_root instead (which is what they are looking for). It then makes sysfs_mount static to keep people from using it by accident.

The net result is slightly faster and clearer code.

Signature · 1 line
tejun
#92 lore raw

Re: [PATCH 11/14] sysfs: Remove s_dentry form sysfs_dirent.

In reply to a message not in the archive
Quoted 6 lines · Eric W. Biederman

After the last round of refactoring nothing is really using using s_dirent so remove it. Which makes the code easier to work with and follow. And with a little luck makes it easier to support multiple dentry trees for sysfs.

Hooray, Acked-by: Tejun Heo <[email protected]>

Signature · 1 line
tejun
#93 lore raw

Re: [PATCH 10/14] sysfs: Rework sysfs_drop_dentry

In reply to a message not in the archive
Quoted 11 lines · Eric W. Biederman

This modifies sysfs_drop_dentry so it no longers needs the sysfs_assoc_lock and it's assorted challenges.

The principal idea is that __sysfs_get_dentry when not creating dentry will return a dentry for the sysfs_dirent if there is one in the
dcache. If we do get a dentry we can drop it and force it out
of the cache.

For good measure I am also calling shrink_dcache_parent which will force all of the child dentries out of the dcache as well if it is a directory.

I don't think we need this as we drop all sd's recursively anyway. Can we drop it?

Other than that, looks good.

Signature · 1 line
tejun
Acked-by
#94 lore raw

Re: [PATCH 09/14] sysfs: Move all of sysfs_move_dir under sysfs_mutex

In reply to a message not in the archive
Quoted 6 lines · Eric W. Biederman

This patch modifies sysfs_move_dir to perform all of it's operations under the sysfs_mutex. By looking for conflicts using sysfs_find_dirent we accidentally moving something onto a name that already exists but just not in the dcache right now.

Quoted 6 lines

Two s_dentry usages are killed.

And it has probably become unnecessary to grab i_mutex at all but I'm not brave enough to do that just yet.

Signature · 1 line
tejun
Acked-by
#95 lore raw

Re: [PATCH 08/14] sysfs: Perform renames under sysfs_mutex

In reply to a message not in the archive
Quoted 7 lines · Eric W. Biederman

This modifies sysfs_rename_dir to use sysfs_addrm_start/sysfs_addrm_finish making it more like the rest of the sysfs directory manipulation functions.

This moves the entire rename operation inside of sysfs_mutex removing the need to grab i_mutex, and incidentally this kills one s_dentry use.

Looks good.

Signature · 1 line
tejun

A message that is not in the archive (sent off-list or not yet received): [email protected]

#97 lore raw

Re: [PATCH 08/14] sysfs: Perform renames under sysfs_mutex

Quoted 4 lines · Tejun Heo
Quoted 3 lines · Eric W. Biederman

This modifies sysfs_rename_dir to use sysfs_addrm_start/sysfs_addrm_finish making it more like the rest of the sysfs directory manipulation functions.

Quoted 9 lines
Quoted 4 lines

This moves the entire rename operation inside of sysfs_mutex removing the need to grab i_mutex, and incidentally this kills one s_dentry use.

Looks good.

Just one afterthought. addrm_start/finish are basically used as wrapper to lock both sysfs_mutex and i_mutex. It might be better to just open code them. sysfs_move_dir() has to open code locking anyway and IMHO it's better to keep rename and move look similar.

Thanks.

Signature · 1 line
tejun
#98 lore raw

Re: [PATCH 06/14] sysfs: Rewrite sysfs_get_dentry

In reply to a message not in the archive
Quoted 4 lines · Eric W. Biederman

Currently sysfs_get_dentry is very hairy and requires all kinds of locking magic. This patch rewrites sysfs_get_dentry to not use the cached sd->s_dentry, and instead simply lookup and create dcache entries.

I wanted to kill sd->s_dentry from the beginning. The obstacle was actually the shadow directory support, ironic.

Quoted 4 lines
fs/sysfs/dir.c +3 -0
+	struct qstr name;
+	struct inode *inode;
 
+	parent_dentry = NULL;

fs/sysfs/dir.c void sysfs_unlink_sibling(struct sysfs_dirent *sd) · line 99

Quoted 10 lines
fs/sysfs/dir.c +5 -0
+	dentry = dget(sysfs_sb->s_root);
 
+	do {
+		/* Find the first ancestor I have not looked up */
+		cur = sd;
+		while (cur->s_parent != dentry->d_fsdata)
 			cur = cur->s_parent;
 
 		/* look it up */
 		dput(parent_dentry);

Shouldn't this be done after looking up the child?

fs/sysfs/dir.c void sysfs_unlink_sibling(struct sysfs_dirent *sd) · line 107

Quoted 8 lines
fs/sysfs/dir.c +8 -0
+		parent_dentry = dentry;
+		name.name = cur->s_name;
+		name.len = strlen(cur->s_name);
+		dentry = d_hash_and_lookup(parent_dentry, &name);
+		if (dentry)
+			continue;
+		if (!create)
+			goto out;

Probably missing dentry = NULL?

Quoted 21 lines
fs/sysfs/dir.c +15 -0
+		dentry = d_alloc(parent_dentry, &name);
+		if (!dentry) {
+			dentry = ERR_PTR(-ENOMEM);
+			goto out;
 		}
+		inode = sysfs_get_inode(cur);
+		if (!inode) {
 			dput(dentry);
+			dentry = ERR_PTR(-ENOMEM);
+			goto out;
 		}
+		d_instantiate(dentry, inode);
+		sysfs_attach_dentry(cur, dentry);
+	} while (cur != sd);
 
+out:
+	dput(parent_dentry);
+	return dentry;
+}

fs/sysfs/dir.c static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry, · line 733

Quoted 9 lines
fs/sysfs/dir.c +6 -0
@@ -750,6 +725,12 @@ static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
 	struct inode *inode;
 
 	mutex_lock(&sysfs_mutex);
+
+	/* Guard against races with sysfs_get_dentry */
+	result = d_hash_and_lookup(dentry->d_parent, &dentry->d_name);
+	if (result)
+		goto out;
+

Hmmm... This is tricky but probably better than the previous hairy sysfs_get_dentry(). I think it would be worthwhile to comment about creating dentry/inode behind vfs's back in __sysfs_get_dentry().

One thing I'm worried about is that dentry can now be looked up without holding i_mutex. In sysfs proper, there's no synchronization problem but I'm not sure whether we're willing to cross that line set by vfs. It might come back and bite our asses hard later.

Thanks.

Signature · 1 line
tejun

A message that is not in the archive (sent off-list or not yet received): [email protected]

#100 lore raw

Re: [PATCH 06/14] sysfs: Rewrite sysfs_get_dentry

Quoted 21 lines · Tejun Heo
Quoted 4 lines · Eric W. Biederman

Currently sysfs_get_dentry is very hairy and requires all kinds of locking magic. This patch rewrites sysfs_get_dentry to not use the cached sd->s_dentry, and instead simply lookup and create dcache entries.

I wanted to kill sd->s_dentry from the beginning. The obstacle was actually the shadow directory support, ironic.

Quoted 12 lines
fs/sysfs/dir.c +8 -0
+	struct qstr name;
+	struct inode *inode;
 
+	parent_dentry = NULL;
+	dentry = dget(sysfs_sb->s_root);
 
+	do {
+		/* Find the first ancestor I have not looked up */
+		cur = sd;
+		while (cur->s_parent != dentry->d_fsdata)
 			cur = cur->s_parent;
Quoted 4 lines

fs/sysfs/dir.c void sysfs_unlink_sibling(struct sysfs_dirent *sd) · line 99

Quoted 2 lines
fs/sysfs/dir.c +0 -0
 		/* look it up */
 		dput(parent_dentry);

Shouldn't this be done after looking up the child?

Yes and that is what this is. Delaying it a little longer made the conditionals easier to write and verify for correctness.

Quoted 10 lines

fs/sysfs/dir.c void sysfs_unlink_sibling(struct sysfs_dirent *sd) · line 107

Quoted 8 lines
fs/sysfs/dir.c +8 -0
+		parent_dentry = dentry;
+		name.name = cur->s_name;
+		name.len = strlen(cur->s_name);
+		dentry = d_hash_and_lookup(parent_dentry, &name);
+		if (dentry)
+			continue;
+		if (!create)
+			goto out;

Probably missing dentry = NULL?

d_hash_and_lookup sets dentry, and we can't get here if (dentry != NULL)

Quoted 24 lines

fs/sysfs/dir.c static struct dentry * sysfs_lookup(struct inode *dir,

Quoted 21 lines
fs/sysfs/dir.c +15 -0
+		dentry = d_alloc(parent_dentry, &name);
+		if (!dentry) {
+			dentry = ERR_PTR(-ENOMEM);
+			goto out;
 		}
+		inode = sysfs_get_inode(cur);
+		if (!inode) {
 			dput(dentry);
+			dentry = ERR_PTR(-ENOMEM);
+			goto out;
 		}
+		d_instantiate(dentry, inode);
+		sysfs_attach_dentry(cur, dentry);
+	} while (cur != sd);
 
+out:
+	dput(parent_dentry);
+	return dentry;
+}
@@ -750,6 +725,12 @@ static struct dentry * sysfs_lookup(struct inode *dir,

struct dentry *dentry,

Quoted 2 lines
fs/sysfs/dir.c +0 -0
 	struct inode *inode;
 
Quoted 11 lines

fs/sysfs/dir.c static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry, · line 733

Quoted 7 lines
fs/sysfs/dir.c +6 -0
 	mutex_lock(&sysfs_mutex);
+
+	/* Guard against races with sysfs_get_dentry */
+	result = d_hash_and_lookup(dentry->d_parent, &dentry->d_name);
+	if (result)
+		goto out;
+

Hmmm... This is tricky but probably better than the previous hairy sysfs_get_dentry(). I think it would be worthwhile to comment about creating dentry/inode behind vfs's back in __sysfs_get_dentry().

Yes. Good point. That is sufficiently non-intuitive and non-obvious to deserve a comment.

Quoted 4 lines

One thing I'm worried about is that dentry can now be looked up without holding i_mutex. In sysfs proper, there's no synchronization problem but I'm not sure whether we're willing to cross that line set by vfs. It might come back and bite our asses hard later.

It's a reasonable concern. I'm wondering if there are any precedents set by distributed filesystems. Because in a sense that is what we are.

As for crossing that line I don't know what to say it makes the code a lot cleaner, and if we are merged into the kernel at least it will be visible if someone rewrites the vfs.

If sysfs_mutex nested the other way things would be easier, and we could grab all of the i_mutexes we wanted. I wonder if we can be annoying in sysfs_lookup and treat that as the lock inversion case using mutex_trylock etc. And have sysfs_mutex be on the outside for the rest of the cases?

Anyway back to bed with me. I've been dreaming up to many silly ways to abuse the dcache...

Eric

A message that is not in the archive (sent off-list or not yet received): m11weorf3l.fsf-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#102 lore raw

Re: [PATCH 06/14] sysfs: Rewrite sysfs_get_dentry

Quoted 11 lines · Eric W. Biederman
Quoted 9 lines

fs/sysfs/dir.c void sysfs_unlink_sibling(struct sysfs_dirent *sd) · line 99

Quoted 8 lines
fs/sysfs/dir.c +4 -0
+	do {
+		/* Find the first ancestor I have not looked up */
+		cur = sd;
+		while (cur->s_parent != dentry->d_fsdata)
 			cur = cur->s_parent;
 
 		/* look it up */
 		dput(parent_dentry);

Shouldn't this be done after looking up the child?

Yes and that is what this is. Delaying it a little longer made the conditionals easier to write and verify for correctness.

Right, missed the next line.

Quoted 10 lines
Quoted 9 lines

fs/sysfs/dir.c void sysfs_unlink_sibling(struct sysfs_dirent *sd) · line 107

Quoted 8 lines
fs/sysfs/dir.c +8 -0
+		parent_dentry = dentry;
+		name.name = cur->s_name;
+		name.len = strlen(cur->s_name);
+		dentry = d_hash_and_lookup(parent_dentry, &name);
+		if (dentry)
+			continue;
+		if (!create)
+			goto out;

Probably missing dentry = NULL?

d_hash_and_lookup sets dentry, and we can't get here if (dentry != NULL)

Yes.

Quoted 8 lines
Quoted 4 lines

One thing I'm worried about is that dentry can now be looked up without holding i_mutex. In sysfs proper, there's no synchronization problem but I'm not sure whether we're willing to cross that line set by vfs. It might come back and bite our asses hard later.

It's a reasonable concern. I'm wondering if there are any precedents set by distributed filesystems. Because in a sense that is what we are.

Yeah, that's the weird thing about sysfs. sysfs interface acts as a different access point to the filesystem making it virtually distributed.

Quoted 9 lines

As for crossing that line I don't know what to say it makes the code a lot cleaner, and if we are merged into the kernel at least it will be visible if someone rewrites the vfs.

If sysfs_mutex nested the other way things would be easier, and we could grab all of the i_mutexes we wanted. I wonder if we can be annoying in sysfs_lookup and treat that as the lock inversion case using mutex_trylock etc. And have sysfs_mutex be on the outside for the rest of the cases?

The problem with treating sysfs_lookup as inversion case is that vfs layer grabs i_mutex outside of sysfs_lookup. Releasing i_mutex from inside sysfs_lookup would be a hacky layering violation.

Then again, the clean up which can come from the new sysfs_looukp_dentry is very significant. I'll think about it a bit more.

Thanks.

Signature · 1 line
tejun

A message that is not in the archive (sent off-list or not yet received): [email protected]

#104 lore raw

Re: [PATCH 06/14] sysfs: Rewrite sysfs_get_dentry

Quoted 6 lines · Tejun Heo
Quoted 5 lines

If sysfs_mutex nested the other way things would be easier, and we could grab all of the i_mutexes we wanted. I wonder if we can be annoying in sysfs_lookup and treat that as the lock inversion case using mutex_trylock etc. And have sysfs_mutex be on the outside for the rest of the cases?

Quoted 6 lines

The problem with treating sysfs_lookup as inversion case is that vfs layer grabs i_mutex outside of sysfs_lookup. Releasing i_mutex from inside sysfs_lookup would be a hacky layering violation.

Then again, the clean up which can come from the new sysfs_looukp_dentry is very significant. I'll think about it a bit more.

How about something like this? __sysfs_get_dentry() never creates any dentry, it just looks up existing ones. sysfs_get_dentry() calls __sysfs_get_dentry() and if it fails, it builds a path string and look up using regular vfs_path_lookup(). Once in the creation path, sysfs_get_dentry() is allowed to fail, so allocating path buf is fine.

It still needs to retry when vfs_path_lookup() returns -ENOENT or the wrong dentry but things are much simpler now. It doesn't violate any VFS locking rule while maintaining all the benefits of sysfs_get_dentry() cleanup.

Something like LOOKUP_KERNEL is needed to ignore security checks; otherwise, we'll need to resurrect lookup_one_len_kern() and open code look up.

The patch is on top of all your patches and is in barely working form.

fs/sysfs/dir.c +49 -31
index 66d418a..0a6e036 100644
@@ -81,20 +81,19 @@ void sysfs_unlink_sibling(struct sysfs_dirent *sd)
  *	Get dentry for @sd.
  *
  *	This function descends the sysfs dentry tree from the root
- *	populating it if necessary until it reaches the dentry for @sd.
+ *	until it reaches the dentry for @sd.
  *
  *	LOCKING:
- *	Kernel thread context (may sleep)
+ *	mutex_lock(sysfs_mutex)
  *
  *	RETURNS:
- *	Pointer to found dentry on success, ERR_PTR() value on error.
+ *	Pointer to found dentry on success, NULL if doesn't exist.
  */
-struct dentry *__sysfs_get_dentry(struct sysfs_dirent *sd, int create)
+struct dentry *__sysfs_get_dentry(struct sysfs_dirent *sd)
 {
 	struct sysfs_dirent *cur;
 	struct dentry *parent_dentry, *dentry;
 	struct qstr name;
-	struct inode *inode;
 
 	parent_dentry = NULL;
 	dentry = dget(sysfs_sb->s_root);
@@ -111,26 +110,8 @@ struct dentry *__sysfs_get_dentry(struct sysfs_dirent *sd, int create)
 		name.name = cur->s_name;
 		name.len = strlen(cur->s_name);
 		dentry = d_hash_and_lookup(parent_dentry, &name);
-		if (dentry)
-			continue;
-		if (!create)
-			goto out;
-		dentry = d_alloc(parent_dentry, &name);
-		if (!dentry) {
-			dentry = ERR_PTR(-ENOMEM);
-			goto out;
-		}
-		inode = sysfs_get_inode(cur);
-		if (!inode) {
-			dput(dentry);
-			dentry = ERR_PTR(-ENOMEM);
-			goto out;
-		}
-		d_instantiate(dentry, inode);
-		sysfs_attach_dentry(cur, dentry);
-	} while (cur != sd);
+	} while (dentry && cur != sd);
 
-out:
 	dput(parent_dentry);
 	return dentry;
 }
@@ -138,10 +119,52 @@ out:
 struct dentry *sysfs_get_dentry(struct sysfs_dirent *sd)
 {
 	struct dentry *dentry;
+	struct nameidata nd;
+	char *path_buf;
+	int len, rc;
+	
+	mutex_lock(&sysfs_mutex);
+	dentry = __sysfs_get_dentry(sd);
+	mutex_unlock(&sysfs_mutex);
+
+	if (dentry)
+		return dentry;
+
+	/* Look up failed.  This means that the dentry associated with
+	 * @sd currently doesn't exist and we're allowed to fail.
+	 * Let's allocate path buffer and look up like a sane person.
+	 */
+	path_buf = kmalloc(PATH_MAX, GFP_KERNEL);
+	if (!path_buf)
+		return ERR_PTR(-ENOMEM);
 
+ retry:
 	mutex_lock(&sysfs_mutex);
-	dentry = __sysfs_get_dentry(sd, 1);
+	/* XXX - clean up */
+	len = object_path_length(sd);
+	BUG_ON(len > PATH_MAX);
+	path_buf[len - 1] = '\0';
+	fill_object_path(sd, path_buf, len);
 	mutex_unlock(&sysfs_mutex);
+
+	/* XXX - we need a flag to override security check or need to
+	 * open code lookup.  The former is far better, IMHO.
+	 */
+	rc = vfs_path_lookup(sysfs_mount->mnt_root, sysfs_mount,
+			     path_buf + 1, 0, &nd);
+	dentry = nd.dentry;
+
+	/* renamed/moved beneath us? */
+	if (rc == -ENOENT)
+		goto retry;
+	if (rc == 0 && dentry->d_fsdata != sd) {
+		dput(dentry);
+		goto retry;
+	}
+	if (rc && rc != -ENOENT)
+		dentry = ERR_PTR(rc);
+
+	kfree(path_buf);
 	return dentry;
 }
 
@@ -512,7 +535,7 @@ static void sysfs_drop_dentry(struct sysfs_dirent *sd)
 
 	/* Remove a dentry for a sd from the dcache if present */
 	mutex_lock(&sysfs_mutex);
-	dentry = __sysfs_get_dentry(sd, 0);
+	dentry = __sysfs_get_dentry(sd);
 	if (IS_ERR(dentry))
 		dentry = NULL;
 	if (dentry) {
@@ -700,11 +723,6 @@ static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
 
 	mutex_lock(&sysfs_mutex);
 
-	/* Guard against races with sysfs_get_dentry */
-	result = d_hash_and_lookup(dentry->d_parent, &dentry->d_name);
-	if (result)
-		goto out;
-
 	sd = sysfs_find_dirent(parent_sd, dentry->d_name.name);
 
 	/* no such entry */
fs/sysfs/mount.c +1 -1
index 0ad731b..3f652be 100644
@@ -15,7 +15,7 @@
 /* Random magic number */
 #define SYSFS_MAGIC 0x62656572
 
-static struct vfsmount *sysfs_mount;
+struct vfsmount *sysfs_mount;
 struct super_block * sysfs_sb = NULL;
 struct kmem_cache *sysfs_dir_cachep;
 
fs/sysfs/symlink.c +2 -2
index 2b542dc..336823c 100644
@@ -21,7 +21,7 @@ static int object_depth(struct sysfs_dirent *sd)
 	return depth;
 }
 
-static int object_path_length(struct sysfs_dirent * sd)
+int object_path_length(struct sysfs_dirent * sd)
 {
 	int length = 1;
 
@@ -31,7 +31,7 @@ static int object_path_length(struct sysfs_dirent * sd)
 	return length;
 }
 
-static void fill_object_path(struct sysfs_dirent *sd, char *buffer, int length)
+void fill_object_path(struct sysfs_dirent *sd, char *buffer, int length)
 {
 	--length;
 	for (; sd->s_parent; sd = sd->s_parent) {
fs/sysfs/sysfs.h +4 -0
index 265a16a..86704ef 100644
@@ -51,6 +51,7 @@ struct sysfs_addrm_cxt {
 };
 
 extern struct sysfs_dirent sysfs_root;
+extern struct vfsmount *sysfs_mount;
 extern struct kmem_cache *sysfs_dir_cachep;
 
 extern struct dentry *sysfs_get_dentry(struct sysfs_dirent *sd);
@@ -89,6 +90,9 @@ extern void sysfs_remove_subdir(struct sysfs_dirent *sd);
 
 extern int sysfs_setattr(struct dentry *dentry, struct iattr *iattr);
 
+extern int object_path_length(struct sysfs_dirent * sd);
+extern void fill_object_path(struct sysfs_dirent *sd, char *buffer, int length);
+
 extern spinlock_t sysfs_assoc_lock;
 extern struct mutex sysfs_mutex;
 extern struct super_block * sysfs_sb;

A message that is not in the archive (sent off-list or not yet received): 20070731141613.GC13674-Gd/HAXX7CRxy/[email protected]

#106 lore raw

Re: [PATCH 06/14] sysfs: Rewrite sysfs_get_dentry

Quoted 25 lines · Tejun Heo
Quoted 6 lines · Tejun Heo
Quoted 5 lines

If sysfs_mutex nested the other way things would be easier, and we could grab all of the i_mutexes we wanted. I wonder if we can be annoying in sysfs_lookup and treat that as the lock inversion case using mutex_trylock etc. And have sysfs_mutex be on the outside for the rest of the cases?

Quoted 6 lines

The problem with treating sysfs_lookup as inversion case is that vfs layer grabs i_mutex outside of sysfs_lookup. Releasing i_mutex from inside sysfs_lookup would be a hacky layering violation.

Then again, the clean up which can come from the new sysfs_looukp_dentry is very significant. I'll think about it a bit more.

How about something like this? __sysfs_get_dentry() never creates any dentry, it just looks up existing ones. sysfs_get_dentry() calls __sysfs_get_dentry() and if it fails, it builds a path string and look up using regular vfs_path_lookup(). Once in the creation path, sysfs_get_dentry() is allowed to fail, so allocating path buf is fine.

It still needs to retry when vfs_path_lookup() returns -ENOENT or the wrong dentry but things are much simpler now. It doesn't violate any VFS locking rule while maintaining all the benefits of sysfs_get_dentry() cleanup.

Quoted 5 lines

Something like LOOKUP_KERNEL is needed to ignore security checks; otherwise, we'll need to resurrect lookup_one_len_kern() and open code look up.

The patch is on top of all your patches and is in barely working form.

Thanks. I need to look some more. I've got a case where __sysfs_get_dentry called from sysfs_drop_dentry is not successfully walking of the sysfs_dirent tree to the initial root directory. (This is with deleted sysfs_dirents but that expected from the context).

I haven't yet had a chance to dig in and see what is going on yet.

Your patch doesn't touch that part of my logic so I don't know yet what is going on.

Eric

#107 lore raw

Re: [PATCH 06/14] sysfs: Rewrite sysfs_get_dentry

In reply to a message not in the archive
Quoted 25 lines · Tejun Heo
Quoted 6 lines · Tejun Heo
Quoted 5 lines

If sysfs_mutex nested the other way things would be easier, and we could grab all of the i_mutexes we wanted. I wonder if we can be annoying in sysfs_lookup and treat that as the lock inversion case using mutex_trylock etc. And have sysfs_mutex be on the outside for the rest of the cases?

Quoted 6 lines

The problem with treating sysfs_lookup as inversion case is that vfs layer grabs i_mutex outside of sysfs_lookup. Releasing i_mutex from inside sysfs_lookup would be a hacky layering violation.

Then again, the clean up which can come from the new sysfs_looukp_dentry is very significant. I'll think about it a bit more.

How about something like this? __sysfs_get_dentry() never creates any dentry, it just looks up existing ones. sysfs_get_dentry() calls __sysfs_get_dentry() and if it fails, it builds a path string and look up using regular vfs_path_lookup(). Once in the creation path, sysfs_get_dentry() is allowed to fail, so allocating path buf is fine.

It still needs to retry when vfs_path_lookup() returns -ENOENT or the wrong dentry but things are much simpler now. It doesn't violate any VFS locking rule while maintaining all the benefits of sysfs_get_dentry() cleanup.

Quoted 5 lines

Something like LOOKUP_KERNEL is needed to ignore security checks; otherwise, we'll need to resurrect lookup_one_len_kern() and open code look up.

The patch is on top of all your patches and is in barely working form.

I will look a little more and see. But right now it looks like the real problem with locking is that we use sysfs_mutex to lock the sysfs_dirent s_children list.

Instead it really looks like we should use i_mutex from the appropriate inode. Or is there a real performance problem with forcing the directory inodes in core when we modify the directories?

Using i_mutex to lock the s_children list. Allows us to make sysfs_mutex come before i_mutex, and it removes the need for an additional lock in sysfs_lookup. So generally it looks like the right thing to do and it should noticeably simplify the sysfs locking.

Was this an oversight or is there some good reason we aren't using i_mutex to lock the s_children list?

Eric

A message that is not in the archive (sent off-list or not yet received): m1fy33pq1r.fsf-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#109 lore raw

Re: [PATCH 06/14] sysfs: Rewrite sysfs_get_dentry

Hello, Eric.

Quoted 7 lines · Eric W. Biederman

I will look a little more and see. But right now it looks like the real problem with locking is that we use sysfs_mutex to lock the sysfs_dirent s_children list.

Instead it really looks like we should use i_mutex from the appropriate inode. Or is there a real performance problem with forcing the directory inodes in core when we modify the directories?

I don't think there is any performance problem. Problems with using i_mutex were...

* It was messy. I don't remember all the details now but IIRC symlink walk code was pretty complex.

* And more importantly, inodes are reclaimable and might or might not be there.

Signature · 1 line
tejun

A message that is not in the archive (sent off-list or not yet received): [email protected]

#111 lore raw

Re: [PATCH 06/14] sysfs: Rewrite sysfs_get_dentry

Quoted 11 lines · Tejun Heo

Hello, Eric.

Quoted 7 lines · Eric W. Biederman

I will look a little more and see. But right now it looks like the real problem with locking is that we use sysfs_mutex to lock the sysfs_dirent s_children list.

Instead it really looks like we should use i_mutex from the appropriate inode. Or is there a real performance problem with forcing the directory inodes in core when we modify the directories?

Quoted 8 lines

I don't think there is any performance problem. Problems with using i_mutex were...

* It was messy. I don't remember all the details now but IIRC symlink walk code was pretty complex.

* And more importantly, inodes are reclaimable and might or might not be there.

Yes. But we can always force inodes into the cache when we need them. When I complete it I will have to show you a patch using the inode lock for locking directory modifications. From what I can tell so far it allows me to fix the weird lock order problems and generally simplify the locking.

Eric

A message that is not in the archive (sent off-list or not yet received): m1vebznpyc.fsf-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#113 lore raw

Re: [PATCH 06/14] sysfs: Rewrite sysfs_get_dentry

Quoted 18 lines · Eric W. Biederman
Quoted 16 lines · Tejun Heo

Hello, Eric.

Quoted 7 lines · Eric W. Biederman

I will look a little more and see. But right now it looks like the real problem with locking is that we use sysfs_mutex to lock the sysfs_dirent s_children list.

Instead it really looks like we should use i_mutex from the appropriate inode. Or is there a real performance problem with forcing the directory inodes in core when we modify the directories?

I don't think there is any performance problem. Problems with using i_mutex were...

* It was messy. I don't remember all the details now but IIRC symlink walk code was pretty complex.

Quoted 7 lines
Quoted 2 lines

* And more importantly, inodes are reclaimable and might or might not be there.

Yes. But we can always force inodes into the cache when we need them. When I complete it I will have to show you a patch using the inode lock for locking directory modifications. From what I can tell so far it allows me to fix the weird lock order problems and generally simplify the locking.

Hmmm... I think we can live with a bit of complexity in sysfs_get_dentry(). It's very well localized and not even long. I have been trying hard to untangle sysfs internals from vfs and have a bit of resistance against going back. But, then again, if we can achieve something better and simpler, why not?

Signature · 1 line
tejun

A message that is not in the archive (sent off-list or not yet received): [email protected]

#115 lore raw

Re: [PATCH 06/14] sysfs: Rewrite sysfs_get_dentry

Quoted 5 lines · Tejun Heo

Hmmm... I think we can live with a bit of complexity in sysfs_get_dentry(). It's very well localized and not even long. I have been trying hard to untangle sysfs internals from vfs and have a bit of resistance against going back. But, then again, if we can achieve something better and simpler, why not?

Right now my real challenge is untangling sysfs_move_dir. In the case of multiple mounts of the sysfs tree.

My practical problem is that I need to hold a lock for the sysfs dirents and while that lock is held I need to call sysfs_get_dentry for the destination directory once for each superblock.

It might be that some kind of reader-writer lock strategy is what I need to untangle this mess. Rather then making changing to i_mutex. All I know is at the moment it is taking a lot of code reading and brain storm to come of with something that is easy to maintain.

Eric

A message that is not in the archive (sent off-list or not yet received): m1ir7znog9.fsf-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#117 lore raw

Re: [PATCH 06/14] sysfs: Rewrite sysfs_get_dentry

Quoted 10 lines · Eric W. Biederman
Quoted 5 lines · Tejun Heo

Hmmm... I think we can live with a bit of complexity in sysfs_get_dentry(). It's very well localized and not even long. I have been trying hard to untangle sysfs internals from vfs and have a bit of resistance against going back. But, then again, if we can achieve something better and simpler, why not?

Right now my real challenge is untangling sysfs_move_dir. In the case of multiple mounts of the sysfs tree.

OIC. Yeah, I guess moving a sd with multiple dentries attached could be pretty difficult.

Quoted 8 lines

My practical problem is that I need to hold a lock for the sysfs dirents and while that lock is held I need to call sysfs_get_dentry for the destination directory once for each superblock.

It might be that some kind of reader-writer lock strategy is what I need to untangle this mess. Rather then making changing to i_mutex. All I know is at the moment it is taking a lot of code reading and brain storm to come of with something that is easy to maintain.

Just in case, sysfs used to have sysfs_rename_rwsem to protect move/rename against tree walking, which became unnecessary after i_mutex -> sysfs_mutex conversion. Move/rename can use stupid big fat locks if that helps.

Good luck.

Signature · 1 line
tejun

A message that is not in the archive (sent off-list or not yet received): [email protected]

#119 lore raw

Re: [PATCH 06/14] sysfs: Rewrite sysfs_get_dentry

Quoted 10 lines · Tejun Heo
Quoted 8 lines · Eric W. Biederman

My practical problem is that I need to hold a lock for the sysfs dirents and while that lock is held I need to call sysfs_get_dentry for the destination directory once for each superblock.

It might be that some kind of reader-writer lock strategy is what I need to untangle this mess. Rather then making changing to i_mutex. All I know is at the moment it is taking a lot of code reading and brain storm to come of with something that is easy to maintain.

Quoted 4 lines

Just in case, sysfs used to have sysfs_rename_rwsem to protect move/rename against tree walking, which became unnecessary after i_mutex -> sysfs_mutex conversion. Move/rename can use stupid big fat locks if that helps.

I second that. Reintroduction of sysfs_rename_rwsem or something similar may be the best way to avoid headaches.

A message that is not in the archive (sent off-list or not yet received): 20070802102819.183720a4-XQvu0L+U/[email protected]

#121 lore raw

Re: [PATCH 06/14] sysfs: Rewrite sysfs_get_dentry

Quoted 13 lines · Cornelia Huck
Quoted 10 lines · Tejun Heo
Quoted 8 lines · Eric W. Biederman

My practical problem is that I need to hold a lock for the sysfs dirents and while that lock is held I need to call sysfs_get_dentry for the destination directory once for each superblock.

It might be that some kind of reader-writer lock strategy is what I need to untangle this mess. Rather then making changing to i_mutex. All I know is at the moment it is taking a lot of code reading and brain storm to come of with something that is easy to maintain.

Quoted 7 lines
Quoted 4 lines

Just in case, sysfs used to have sysfs_rename_rwsem to protect move/rename against tree walking, which became unnecessary after i_mutex -> sysfs_mutex conversion. Move/rename can use stupid big fat locks if that helps.

I second that. Reintroduction of sysfs_rename_rwsem or something similar may be the best way to avoid headaches.

I guess I haven't looked at that because we already have a big fact lock we just have an ordering problem in taking that lock. Introducing another lock just because of that doesn't quite feel right.

I currently have two practical solutions on the table.

- Make the s_sibling list walk RCU safe so it does not require us to grab the sysfs_mutex. This works but is a bit complicated.

- Just kill all of the dentries in sysfs_move_dir and sysfs_rename_dir. The semantics are that no one can be using the device at the time of a rename or a move (as I read the callers) so dropping the dentries and making it look like a delete/add pair to the users should be acceptable and a whole lot simpler.

Eric

A message that is not in the archive (sent off-list or not yet received): m1tzrgl8mo.fsf-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#123 lore raw

Re: [PATCH 06/14] sysfs: Rewrite sysfs_get_dentry

Quoted 4 lines · On Fri, 03 Aug 2007 13:29:19 -0600, ebiederm-aS9lmoZGLiVWk0Htik3J/[email protected]…

I currently have two practical solutions on the table.

- Make the s_sibling list walk RCU safe so it does not require us to grab the sysfs_mutex. This works but is a bit complicated.

But it would be nice, I guess :)

Quoted 6 lines

- Just kill all of the dentries in sysfs_move_dir and sysfs_rename_dir. The semantics are that no one can be using the device at the time of a rename or a move (as I read the callers) so dropping the dentries and making it look like a delete/add pair to the users should be acceptable and a whole lot simpler.

Hm, I'm not sure I understand you here. What do you mean by "using the device"? Do you mean dropping the sysfs_dirents? Or am I just confused?

#124 lore raw

Re: [PATCH 05/14] sysfs: Remove sysfs_instantiate

In reply to a message not in the archive
Quoted 5 lines · Eric W. Biederman

Now that sysfs_get_inode is dropping the inode lock we no longer have a need for sysfs_instantiate. So kill it and just use d_instantiate instead.

Oh, here it is. Forget the second nit in my previous comment. Yeap, this looks good.

Signature · 1 line
tejun
#126 lore raw

Re: [PATCH 03/14] sysfs: Move all of inode initialization into sysfs_init_inode

In reply to a message not in the archive
Quoted 6 lines · Eric W. Biederman

Besides being a good general cleanup the ultimate effect of this patch is that sysfs_get_inode now gives you a usable inode that doesn't need any further initialization to be useful.

I generally like it. The only nits are that I think we're better off keeping the function in fs/sysfs/dir.c as all the interesting parts are there anyway and sysfs_instantiate() can just die. The function is just a remnant of days where inode initialization was scattered all over the place.

Thanks.

Signature · 1 line
tejun
#127 lore raw

Re: [PATCH 02/14] sysfs: In sysfs_lookup protect s_parent with sysfs_mutex

In reply to a message not in the archive
Quoted 7 lines · Eric W. Biederman

It turns out we have been being silly and have failed to protect ourselves against races between sysfs_lookup and operations that modify the sysfs directory like sysfs_readdir and create_dir.

So this patch modifies sysfs_lookup to grab sysfs_mutex before walking the parent->s_children list.

Updated portion of #01 and #02 are identical to the cleanup patches I just posted. :-)

Signature · 1 line
tejun

A message that is not in the archive (sent off-list or not yet received): [email protected]

#129 lore raw

Re: [PATCH 02/14] sysfs: In sysfs_lookup protect s_parent with sysfs_mutex

Quoted 11 lines · Tejun Heo
Quoted 7 lines · Eric W. Biederman

It turns out we have been being silly and have failed to protect ourselves against races between sysfs_lookup and operations that modify the sysfs directory like sysfs_readdir and create_dir.

So this patch modifies sysfs_lookup to grab sysfs_mutex before walking the parent->s_children list.

Updated portion of #01 and #02 are identical to the cleanup patches I just posted. :-)

I'm not surprised. I will have to see what you have done after I have slept.

Eric

A message that is not in the archive (sent off-list or not yet received): m1bqdsrgvy.fsf-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org

#131 lore raw

Re: [PATCH 02/14] sysfs: In sysfs_lookup protect s_parent with sysfs_mutex

Quoted 8 lines · Eric W. Biederman
Quoted 6 lines · Tejun Heo
Quoted 5 lines · Eric W. Biederman

It turns out we have been being silly and have failed to protect ourselves against races between sysfs_lookup and operations that modify the sysfs directory like sysfs_readdir and create_dir.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

So this patch modifies sysfs_lookup to grab sysfs_mutex before walking the parent->s_children list.

Updated portion of #01 and #02 are identical to the cleanup patches I just posted. :-)

I'm not surprised. I will have to see what you have done after I have slept.

Good night.

Signature · 1 line
tejun
#132 lore raw

Re: [PATCH 2/4] sysfs: Implement sysfs manged shadow directory support.

In reply to a message not in the archive

Hello,

Quoted 2 lines · Eric W. Biederman

How close are we to the point where we can get mount sysfs multiple times and get multiple dentry trees with different super blocks?

Yeah, that sounds much better. We only have to pay attention to getting sysfs_dirent tree correct. The rest can be done by just looking up the correct sysfs_dirent in sysfs_lookup(). We would still need to pin all shadows to keep sysfs_get_dentry() working.

Quoted 11 lines

That really does sound like the right way to go. Especially as it simplifies the monitoring of containers. If you want to watch what the view looks like in some container your bind mount his sysfs and look at that.

If we can do that the dcache side at least will be beautiful. And with a little care we may be able to reduce the work to a special case in lookup, some extra handling to mark directories as belonging only to a certain mount of sysfs.

If we can find something that is stupid and simple I'm all for that.

Amen.

Quoted 3 lines

To reach the no-kobj utopia we may also need a special device_migrate that is a super set of device_rename (because sometimes we need to rename devices when we move them between namespaces).

One thing I'm curious about is which semantic is appropriate behavior when a node is migrated from one namespace to another - renaming or deactivation followed by activation in new name space. I guess it doesn't really matter.

Quoted 2 lines

So are we close to having a sysfs that we can have multiple super blocks for?

Sorry but I dunno. It sounds much more appealing than other approaches tho.

Signature · 1 line
tejun
#133 lore raw

Re: [PATCH 2/4] sysfs: Implement sysfs manged shadow directory support.

In reply to a message not in the archive

Hello,

Okay, some questions.

* What do you think about not allowing duplicate names across different tags? ie. there's only one ethX anywhere but it's visible only in a specific namespace (and maybe in the default global one). Or does everyone need its own eth0. If this is acceptable, the problem becomes _much_ simpler.

* I think we can do away with the magic tag and use a pointer to vfsmount instead. So, a process which wants to be in certain namespace can bind-mount /sysfs to its own /sysfs and make needed sysfs nodes bound to the mount. Does this sound okay? Such process should probably be in its own chrooted environment to function properly.

* I haven't really followed the containers thread. Do people generally agree on including it in mainline when we have all the fancy virtualization stuff?

Thanks.

Signature · 1 line
tejun

A message that is not in the archive (sent off-list or not yet received): [email protected]

#135 lore raw

Re: [Devel] Re: [PATCH 2/4] sysfs: Implement sysfs manged shadow directory support.

Quoted 9 lines · Tejun Heo

Hello,

Okay, some questions.

* What do you think about not allowing duplicate names across different tags? ie. there's only one ethX anywhere but it's visible only in a specific namespace (and maybe in the default global one). Or does everyone need its own eth0. If this is acceptable, the problem becomes _much_ simpler.

at least on checkpoint/restart names should be restored as is and conflicts are unavoidable.
this includes ifindex as well by the way.

Quoted 9 lines

* I think we can do away with the magic tag and use a pointer to vfsmount instead. So, a process which wants to be in certain namespace can bind-mount /sysfs to its own /sysfs and make needed sysfs nodes bound to the mount. Does this sound okay? Such process should probably be in its own chrooted environment to function properly.

* I haven't really followed the containers thread. Do people generally agree on including it in mainline when we have all the fancy virtualization stuff?

Thanks,
Kirill

#136 lore raw

Re: [PATCH 2/4] sysfs: Implement sysfs manged shadow directory support.

In reply to a message not in the archive

Hi,

Quoted 7 lines · Tejun Heo

Okay, some questions.

* What do you think about not allowing duplicate names across different tags? ie. there's only one ethX anywhere but it's visible only in a specific namespace (and maybe in the default global one). Or does everyone need its own eth0. If this is acceptable, the problem becomes _much_ simpler.

Duplicating names across different namespaces (eth0 for everyone) is a feature which allows me to use a very similar configurations for different namespaces.
There's also a security argument saying that one namespace shouldn't be able to infer information about other namespaces. If an interface name must not be reused across namespaces you can enumerate the obvious interface name list from any namespace and find out which interface names are used by other namespaces. I'm not suggesting that this argument is valid, but it needs to be considered and if we decide it is invalid, we should document why.

Regards,
Carl-Daniel

#137 lore raw

Re: [PATCH 2/4] sysfs: Implement sysfs manged shadow directory support.

In reply to a message not in the archive
Quoted 9 lines · Tejun Heo

Hello,

Okay, some questions.

* What do you think about not allowing duplicate names across different tags? ie. there's only one ethX anywhere but it's visible only in a specific namespace (and maybe in the default global one). Or does everyone need its own eth0. If this is acceptable, the problem becomes _much_ simpler.

I agree, and unfortunately this is the problem I am really trying to solve. Everyone namespace has it's own loopback interface.

Quoted 5 lines

* I think we can do away with the magic tag and use a pointer to vfsmount instead. So, a process which wants to be in certain namespace can bind-mount /sysfs to its own /sysfs and make needed sysfs nodes bound to the mount. Does this sound okay? Such process should probably be in its own chrooted environment to function properly.

That would actually be preferable.

Quoted 3 lines

* I haven't really followed the containers thread. Do people generally agree on including it in mainline when we have all the fancy virtualization stuff?

Generally. Assuming all of the i's are dotted and all of the t's crossed.

Eric

#138 lore raw

Re: [PATCH 2/4] sysfs: Implement sysfs manged shadow directory support.

In reply to a message not in the archive
Quoted 6 lines · Tejun Heo

Hello,

fs/sysfs/dir.c

Quoted 2 lines · Eric W. Biederman
fs/sysfs/dir.c +1 -0
+static struct sysfs_dirent *find_shadow_sd(struct sysfs_dirent

*parent_sd, const void *target)

Quoted 12 lines

fs/sysfs/dir.c line 32

Quoted 7 lines
fs/sysfs/dir.c +7 -0
+{
+	/* Find the shadow directory for the specified tag */
+	struct sysfs_dirent *sd;
+
+	for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) {
+		if (sd->s_name != target)
+			continue;

This is way too cryptic and, plus, no comment. This kind of stuff can cause a lot of confusion later when other people wanna work on the code. Please move s_name into sysfs_elem_* which need s_name and create sysfs_elem_shadow which doesn't have ->name but has ->tag.

I'm been staring at this to long to know. I just know the name is more or less reasonable and following how it is called tends to show what it is used for.

In one sense the name is very much the tag. So I don't feel bad about using it that way. In another sense if we can cleanup the code by having the s_name field be a string that would be an improvement and be appreciated.

Quoted 7 lines

fs/sysfs/dir.c line 42

Quoted 5 lines
fs/sysfs/dir.c +5 -0
+static const void *find_shadow_tag(struct kobject *kobj)
+{
+	/* Find the tag the current kobj is cached with */
+	return kobj->sd->s_parent->s_name;
+}

Please don't use kobj inside sysfs. Use sysfs_dirent instead.

The interface to sysfs is kobjects and names, so I'm limited in what I can do.

Where this is used I know a directory and a name I want to remove. What I don't necessarily know is which shadow of that directory I want to remove from without looking at the kobject.

sysfs_delete_link is a good code path to follow to understand this.

Quoted 5 lines

fs/sysfs/dir.c static void sysfs_attach_dentry(struct

Quoted 1 line
fs/sysfs/dir.c +0 -0
@@ -414,7 +436,8 @@ static void sysfs_attach_dentry(struct

sysfs_dirent *sd, struct dentry *dentry)

Quoted 3 lines
fs/sysfs/dir.c +0 -0
 	sd->s_dentry = dentry;
 	spin_unlock(&sysfs_assoc_lock);
 
Quoted 7 lines

fs/sysfs/dir.c static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry) · line 440

Quoted 3 lines
fs/sysfs/dir.c +2 -1
-	d_rehash(dentry);
+	if (dentry->d_flags & DCACHE_UNHASHED)
+		d_rehash(dentry);

I think we can use some comment for subtle things like this. DCACHE_UNHASHED is being tested without holding dcache_lock which also can use some comment.

Basically this is a test to see if we have already been placed in the dcache. I'm trying to remember my reasoning

Quoted 61 lines

fs/sysfs/dir.c static void sysfs_drop_dentry(struct sysfs_dirent

Quoted 1 line
fs/sysfs/dir.c +0 -0
@@ -569,6 +592,10 @@ static void sysfs_drop_dentry(struct sysfs_dirent

*sd)

fs/sysfs/dir.c static int create_dir(struct kobject *kobj, struct

Quoted 19 lines
fs/sysfs/dir.c +5 -0
 	spin_unlock(&dcache_lock);
 	spin_unlock(&sysfs_assoc_lock);
 
+	/* dentries for shadowed directories are pinned, unpin */
+	if ((sysfs_type(sd) == SYSFS_SHADOW_DIR) ||
+	    (sd->s_flags & SYSFS_FLAG_SHADOWED))
+		dput(dentry);
 	dput(dentry);
 
 	/* adjust nlink and update timestamp */
@@ -622,6 +649,7 @@ int sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt)
 		acxt->removed = sd->s_sibling;
 		sd->s_sibling = NULL;
 
+		sysfs_prune_shadow_sd(sd->s_parent);
 		sysfs_drop_dentry(sd);
 		sysfs_deactivate(sd);
 		sysfs_put(sd);
@@ -687,6 +715,7 @@ static int create_dir(struct kobject *kobj, struct

sysfs_dirent *parent_sd,

fs/sysfs/dir.c static int create_dir(struct kobject *kobj,

Quoted 8 lines
fs/sysfs/dir.c +1 -0
 	umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
 	struct sysfs_addrm_cxt acxt;
 	struct sysfs_dirent *sd;
+	int err;
 
 	/* allocate */
 	sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
@@ -696,15 +725,21 @@ static int create_dir(struct kobject *kobj,

struct sysfs_dirent *parent_sd,

fs/sysfs/dir.c static struct dentry * sysfs_lookup(struct inode

Quoted 24 lines
fs/sysfs/dir.c +8 -2
 	/* link in */
 	sysfs_addrm_start(&acxt, parent_sd);
+	err = -ENOENT;
+	if (!sysfs_resolve_for_create(kobj, &acxt.parent_sd))
+		goto addrm_finish;
 
-	if (!sysfs_find_dirent(parent_sd, name)) {
+	err = -EEXIST;
+	if (!sysfs_find_dirent(acxt.parent_sd, name)) {
 		sysfs_add_one(&acxt, sd);
 		sysfs_link_sibling(sd);
+		err = 0;
 	}
 
+addrm_finish:
 	if (!sysfs_addrm_finish(&acxt)) {
 		sysfs_put(sd);
-		return -EEXIST;
+		return err;
 	}
 
 	*p_sd = sd;
@@ -813,18 +848,56 @@ static struct dentry * sysfs_lookup(struct inode

*dir, struct dentry *dentry,

fs/sysfs/dir.c static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,

Quoted 4 lines
fs/sysfs/dir.c +1 -0
 	return NULL;
 }
 
+static void *sysfs_shadow_follow_link(struct dentry *dentry, struct

nameidata *nd)

Quoted 10 lines

fs/sysfs/dir.c static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry, · line 858

Quoted 7 lines
fs/sysfs/dir.c +7 -0
+{
+	struct sysfs_dirent *sd;
+	struct dentry *dest;
+
+	sd = dentry->d_fsdata;
+	dest = NULL;
+	if (sd->s_flags & SYSFS_FLAG_SHADOWED) {

sd->s_flags should be protected by sysfs_mutex. Please don't depend on inode locking for synchronization internal to sysfs.

Basically this is a set once bit. That is never expected to be cleared. And is never expected to show up until it is set. So that is minor.

I was hoping to avoid taking the sysfs_mutex for non-shadow directories while always having the same code.

Whatever moving the sysfs_mutex up just a little bit in that function is trivial if it is important. As is potentially having two copies of the directory operations.

Quoted 7 lines
Quoted 7 lines
fs/sysfs/dir.c +7 -0
+static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
+{
+	struct sysfs_addrm_cxt acxt;
+
+	if (!dir_sd)
+		return;
+
Quoted 8 lines

fs/sysfs/dir.c static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd) · line 953

Quoted 6 lines
fs/sysfs/dir.c +6 -0
+	pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
+	sysfs_addrm_start(&acxt, dir_sd);
+	if (sysfs_type(dir_sd) == SYSFS_DIR)
+		sysfs_empty_dir(&acxt, dir_sd);
+	else
+		sysfs_remove_shadows(&acxt, dir_sd);

Care to explain this a bit?

Hmm. It looks like in all of the thrashing of sysfs I got my tested goofed up. It should say:

	if (!(dir_sd->s_flags & SYSFS_FLAG_SHAODWED))
		sysfs_empty_dir(&acxt, dir_sd);
	else
		sysfs_remove_shadows(&acxt, dir_sd);

The logic is if this is an ordinary directory just empty it. However if this directory has shadows empty each shadow. Because we need to delete them all.

Quoted 10 lines

fs/sysfs/dir.c void sysfs_remove_dir(struct kobject * kobj) · line 979

Quoted 6 lines
fs/sysfs/dir.c +0 -0
 	sysfs_addrm_finish(&acxt);
 
 	remove_dir(dir_sd);
@@ -882,86 +978,75 @@ void sysfs_remove_dir(struct kobject * kobj)
 
 int sysfs_rename_dir(struct kobject * kobj, const char *new_name)

This rename modification is painful. Please explain why we need to rename nodes between shadows? Can't we just create new ones? Also, please add some comment when performing black magic.

Well the context is more in the changelog. Although something in the code might help.

Are you looking at just the diff or the applied patch? It may just be that the diff looks horrible. I don't see deep black magic in there that needs an explicit comment.

Please look at sysfs_rename_dir. While the shadow logic may be overkill I think it would probably be worth moving rename dir in a direction where we don't require the dentries to be in cache and we can use sysfs_addrm_start in any event.

Which is the bulk of what I have done there.

Quoted 10 lines

fs/sysfs/dir.c static int sysfs_readdir(struct file * filp,

Quoted 1 line
fs/sysfs/dir.c +0 -0
@@ -1098,8 +1183,11 @@ static int sysfs_readdir(struct file * filp,

void * dirent, filldir_t filldir)

fs/sysfs/dir.c static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir) · line 1187

Quoted 6 lines
fs/sysfs/dir.c +2 -1
 			i++;
 			/* fallthrough */
 		default:
-			mutex_lock(&sysfs_mutex);
+			/* If I am the shadow master return nothing. */
+			if (parent_sd->s_flags & SYSFS_FLAG_SHADOWED)

s_flags protection?

sysfs_mutex? And the set once property.
Basically the check here is just a sanity check and I don't think we will ever get there.

Quoted 11 lines

fs/sysfs/dir.c const struct file_operations

Quoted 1 line
fs/sysfs/dir.c +0 -0
@@ -1188,3 +1276,185 @@ const struct file_operations

sysfs_dir_operations = {

fs/sysfs/dir.c const struct file_operations sysfs_dir_operations = { · line 1281

Quoted 6 lines
fs/sysfs/dir.c +3 -0
 	.read		= generic_read_dir,
 	.readdir	= sysfs_readdir,
 };
+
+
+static void sysfs_prune_shadow_sd(struct sysfs_dirent *sd)

Please put this before sysfs_addrm_finish(). That's the only place this function is used.

Sure. I was also calling it in sysfs_move_dir but that case was to much of a pain and I wasn't really using it so I removed it.

Quoted 42 lines

fs/sysfs/dir.c const struct file_operations · line 1282

Quoted 1 line
fs/sysfs/dir.c +1 -0
+static struct sysfs_dirent *add_shadow_sd(struct sysfs_dirent

*parent_sd, const void *tag)

Quoted 40 lines
fs/sysfs/dir.c +40 -0
+{
+	struct sysfs_dirent *sd = NULL;
+	struct dentry *dir, *shadow;
+	struct inode *inode;
+
+	dir = parent_sd->s_dentry;
+	inode = dir->d_inode;
+
+	shadow = d_alloc(dir->d_parent, &dir->d_name);
+	if (!shadow)
+		goto out;
+
+	/* Since the shadow directory is reachable make it look
+	 * like it is actually hashed.
+	 */
+	shadow->d_hash.pprev = &shadow->d_hash.next;
+	shadow->d_hash.next = NULL;
+	shadow->d_flags &= ~DCACHE_UNHASHED;
+
+	sd = sysfs_new_dirent(tag, parent_sd->s_mode, SYSFS_SHADOW_DIR);
+	if (!sd)
+		goto error;
+
+	sd->s_elem.dir.kobj = parent_sd->s_elem.dir.kobj;
+	sd->s_parent = sysfs_get(parent_sd);
+
+	/* Use the inode number of the parent we are shadowing */
+	sysfs_free_ino(sd->s_ino);
+	sd->s_ino = parent_sd->s_ino;
+
+	inc_nlink(inode);
+	inc_nlink(dir->d_parent->d_inode);
+
+	sysfs_link_sibling(sd);
+	__iget(inode);
+	sysfs_instantiate(shadow, inode);
+	sysfs_attach_dentry(sd, shadow);
+out:
+	return sd;
+error:
Quoted 8 lines

fs/sysfs/dir.c const struct file_operations sysfs_dir_operations = { · line 1348

Quoted 3 lines
fs/sysfs/dir.c +3 -0
+	dput(shadow);
+	goto out;
+}

Can we just add sd here and resolve the rest the same way as other nodes such that each shadow has its own dentry and inode? Am I missing something? Also, why do we need the intermediate shadowed sd at all? Can't we do the following?

Sharing the struct inode means when we switch from one shadow to another we keep the same inode lock. Which means we can resolve which shadow we are working with inside of sysfs_addrm_start, because we don't have to drop and reacquire the locks.

I do the hashed but not in the dcache dentry so that I don't confuse the VFS with multiple dentries of the same name as children on the same dentry. Currently they are locked in cache to simplify things.

Quoted 8 lines

* non-shadowed case

parent_sd - sd

* shadowed case

parent_sd - sd0

Quoted 8 lines
	     sd1
	     sd2

I think we can reduce considerable special case handlings if we do like the above including the implicit shadow creation, parent pruning and symlink tricks. After all, it's just multiple siblings sharing a name which needs some extra context to look up the correct one. We wouldn't even need 'shadow' at all.

I disagree. Or at least I don't see what you are suggesting. Currently I see sysfs_resolve_for_create and sysfs_resolve_for_remove (the implicit shadow directory creation/finding) as fundamental complexity of the problem. We may be able to simplify the implementation of those to a small extent but I don't see those code paths going away.

The big problem I have is I don't know which directories I will need to shadow. Currently on my test system I have:
/sys/class/net /sys/devices/pci0000:00/0000:00:1c.5/0000:04:00.0/net /sys/devices/virtual/net

But hotpluging a new pci device could add yet another directory. So the only code which actually has a clue which directories I need to handle is the sysfs/kobject layer I can't handle it externally. And adding to my fun my network namespaces (the tags) are created and destroyed asynchronously to the devices coming in and going. That is why I have the implicit creation, because I don't know what is happening.

So while I think changing how exactly I use sysfs_dirents may simplify some things I don't see things getting much simpler.

Originally things worked out more nicely because we had the dcache in the form you suggest and the sysfs_dirent tree in the current form. But we were always looking at the dcache when we really cared. So things were a little nicer and there were one or two fewer special cases. But the code was effectively the same.

Now things are a little worse because we don't use the dcache tree for anything. So getting the full path name has a special case.

What do I have to deal with.
- readdir knows it can return the inode number.
- readdir needs to only return one entry for a shadow.

- When I move a network device between namespaces I need sysfs to follow. In particular as I recall we have kobject attributes added by individual devices that I need to preserve. Which strongly suggests doing something a rename to switch contexts is what is needed.

So for the rename I need to get sd->parent_sd is the old directory.

Now perhaps I should be calling something besides device_rename while I move a network device between namespaces and I should have a completely different code path. But that doesn't feel correct to me.

- The dcache can only hold one entry with a given name so I need to use a magic follow_link to switch to the proper name.

- Implicit directory creation is essential because I don't know which directories I will be shadowing a priori.

Quoted 4 lines

Sorry but I don't think the current approach is the correct one. It's too painful and too much complexity is scattered all over the place. I'm afraid this implementation is going to be a maintenance nightmare.

Frankly. The tightly coupled sysfs + kobject debacle of a user interface adds a lot of complexity to maintaining a stable interface to user space. And if something should pay it should be the sysfs code itself not the other pieces of the kernel that are stuck with sysfs. So I will happily argue that my interface to the upper levels is correct or very close to it.

At the same time. If we can find a way to do this with less complexity in sysfs I'm all for it. I just don't see it yet.

To some extent the ideal user space interface would be one where we have multiple different mounts of sysfs. At mount time each one calling shadow_ops->current_tag() and only displaying one tag in that sysfs mount instance. Given the current coupling of everything that still looks noticeably harder to implement then what I have done so far.

Eric

#139 lore raw

patch sysfs-remove-first-pass-at-shadow-directory-support.patch added to gregkh-2.6 tree

In reply to a message not in the archive

This is a note to let you know that I've just added the patch titled

Subject: [PATCH 1/4] sysfs: Remove first pass at shadow directory support

to my gregkh-2.6 tree. Its filename is

sysfs-remove-first-pass-at-shadow-directory-support.patch

This tree can be found at
http://www.kernel.org/pub/linux/kernel/people/gregkh/gregkh-2.6/patches/

From ebiederm-aS9lmoZGLiVWk0Htik3J/[email protected] Fri Jul 20 23:20:07 2007
From: ebiederm-aS9lmoZGLiVWk0Htik3J/[email protected] (Eric W. Biederman)
Date: Wed, 18 Jul 2007 22:43:46 -0600
Subject: [PATCH 1/4] sysfs: Remove first pass at shadow directory support
To: Greg KH <[email protected]>
Cc: Greg KH <[email protected]>, Dave Hansen <hansendc-r/[email protected]>, Benjamin Thery <benjamin.thery-6ktuUTfB/[email protected]>, Linux Containers <[email protected]>, Tejun Heo <[email protected]>
Message-ID: <m14pk13svx.fsf-T1Yj925okcoyDheHMi7gv2pdwda3JcWeAL8bYrjMMd8@public.gmane.org>

While shadow directories appear to be a good idea, the current scheme of controlling their creation and destruction outside of sysfs appears to be a locking and maintenance nightmare in the face of sysfs directories dynamically coming and going. Which can now occur for directories containing network devices when CONFIG_SYSFS_DEPRECATED is not set.

This patch removes everything from the initial shadow directory support that allowed the shadow directory creation to be controlled at a higher level. So except for a few bits of sysfs_rename_dir everything from commit b592fcfe7f06c15ec11774b5be7ce0de3aa86e73 is now gone.

---
 fs/sysfs/dir.c          |  167 ++++++------------------------------------------
 fs/sysfs/group.c        |    1 
 fs/sysfs/inode.c        |   10 --
 fs/sysfs/mount.c        |    2 
 fs/sysfs/sysfs.h        |    6 -
 include/linux/kobject.h |    5 -
 include/linux/sysfs.h   |   27 +------
 lib/kobject.c           |   44 +-----------
 8 files changed, 33 insertions(+), 229 deletions(-)
fs/sysfs/dir.c +22 -145
@@ -569,9 +569,6 @@ static void sysfs_drop_dentry(struct sys
 	spin_unlock(&dcache_lock);
 	spin_unlock(&sysfs_assoc_lock);
 
-	/* dentries for shadowed inodes are pinned, unpin */
-	if (dentry && sysfs_is_shadowed_inode(dentry->d_inode))
-		dput(dentry);
 	dput(dentry);
 
 	/* adjust nlink and update timestamp */
@@ -723,19 +720,15 @@ int sysfs_create_subdir(struct kobject *
 /**
  *	sysfs_create_dir - create a directory for an object.
  *	@kobj:		object we're creating directory for. 
- *	@shadow_parent:	parent object.
  */
-int sysfs_create_dir(struct kobject *kobj,
-		     struct sysfs_dirent *shadow_parent_sd)
+int sysfs_create_dir(struct kobject * kobj)
 {
 	struct sysfs_dirent *parent_sd, *sd;
 	int error = 0;
 
 	BUG_ON(!kobj);
 
-	if (shadow_parent_sd)
-		parent_sd = shadow_parent_sd;
-	else if (kobj->parent)
+	if (kobj->parent)
 		parent_sd = kobj->parent->sd;
 	else if (sysfs_mount && sysfs_mount->mnt_sb)
 		parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata;
@@ -887,45 +880,44 @@ void sysfs_remove_dir(struct kobject * k
 	__sysfs_remove_dir(sd);
 }
 
-int sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd,
-		     const char *new_name)
+int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
 {
-	struct sysfs_dirent *sd = kobj->sd;
-	struct dentry *new_parent = NULL;
+	struct sysfs_dirent *sd;
+	struct dentry *parent = NULL;
 	struct dentry *old_dentry = NULL, *new_dentry = NULL;
+	struct sysfs_dirent *parent_sd;
 	const char *dup_name = NULL;
 	int error;
 
+	if (!kobj->parent)
+		return -EINVAL;
+
 	/* get dentries */
+	sd = kobj->sd;
 	old_dentry = sysfs_get_dentry(sd);
 	if (IS_ERR(old_dentry)) {
 		error = PTR_ERR(old_dentry);
 		goto out_dput;
 	}
 
-	new_parent = sysfs_get_dentry(new_parent_sd);
-	if (IS_ERR(new_parent)) {
-		error = PTR_ERR(new_parent);
+	parent_sd = kobj->parent->sd;
+	parent = sysfs_get_dentry(parent_sd);
+	if (IS_ERR(parent)) {
+		error = PTR_ERR(parent);
 		goto out_dput;
 	}
 
-	/* lock new_parent and get dentry for new name */
-	mutex_lock(&new_parent->d_inode->i_mutex);
+	/* lock parent and get dentry for new name */
+	mutex_lock(&parent->d_inode->i_mutex);
 
-	new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
+	new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
 	if (IS_ERR(new_dentry)) {
 		error = PTR_ERR(new_dentry);
 		goto out_unlock;
 	}
 
-	/* By allowing two different directories with the same
-	 * d_parent we allow this routine to move between different
-	 * shadows of the same directory
-	 */
 	error = -EINVAL;
-	if (old_dentry->d_parent->d_inode != new_parent->d_inode ||
-	    new_dentry->d_parent->d_inode != new_parent->d_inode ||
-	    old_dentry == new_dentry)
+	if (old_dentry == new_dentry)
 		goto out_unlock;
 
 	error = -EEXIST;
@@ -952,9 +944,9 @@ int sysfs_rename_dir(struct kobject *kob
 	mutex_lock(&sysfs_mutex);
 
 	sysfs_unlink_sibling(sd);
-	sysfs_get(new_parent_sd);
+	sysfs_get(parent_sd);
 	sysfs_put(sd->s_parent);
-	sd->s_parent = new_parent_sd;
+	sd->s_parent = parent_sd;
 	sysfs_link_sibling(sd);
 
 	mutex_unlock(&sysfs_mutex);
@@ -965,10 +957,10 @@ int sysfs_rename_dir(struct kobject *kob
  out_drop:
 	d_drop(new_dentry);
  out_unlock:
-	mutex_unlock(&new_parent->d_inode->i_mutex);
+	mutex_unlock(&parent->d_inode->i_mutex);
  out_dput:
 	kfree(dup_name);
-	dput(new_parent);
+	dput(parent);
 	dput(old_dentry);
 	dput(new_dentry);
 	return error;
@@ -1189,121 +1181,6 @@ static loff_t sysfs_dir_lseek(struct fil
 	return offset;
 }
 
-
-/**
- *	sysfs_make_shadowed_dir - Setup so a directory can be shadowed
- *	@kobj:	object we're creating shadow of.
- */
-
-int sysfs_make_shadowed_dir(struct kobject *kobj,
-	void * (*follow_link)(struct dentry *, struct nameidata *))
-{
-	struct dentry *dentry;
-	struct inode *inode;
-	struct inode_operations *i_op;
-
-	/* get dentry for @kobj->sd, dentry of a shadowed dir is pinned */
-	dentry = sysfs_get_dentry(kobj->sd);
-	if (IS_ERR(dentry))
-		return PTR_ERR(dentry);
-
-	inode = dentry->d_inode;
-	if (inode->i_op != &sysfs_dir_inode_operations) {
-		dput(dentry);
-		return -EINVAL;
-	}
-
-	i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
-	if (!i_op)
-		return -ENOMEM;
-
-	memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
-	i_op->follow_link = follow_link;
-
-	/* Locking of inode->i_op?
-	 * Since setting i_op is a single word write and they
-	 * are atomic we should be ok here.
-	 */
-	inode->i_op = i_op;
-	return 0;
-}
-
-/**
- *	sysfs_create_shadow_dir - create a shadow directory for an object.
- *	@kobj:	object we're creating directory for.
- *
- *	sysfs_make_shadowed_dir must already have been called on this
- *	directory.
- */
-
-struct sysfs_dirent *sysfs_create_shadow_dir(struct kobject *kobj)
-{
-	struct sysfs_dirent *parent_sd = kobj->sd->s_parent;
-	struct dentry *dir, *parent, *shadow;
-	struct inode *inode;
-	struct sysfs_dirent *sd;
-	struct sysfs_addrm_cxt acxt;
-
-	dir = sysfs_get_dentry(kobj->sd);
-	if (IS_ERR(dir)) {
-		sd = (void *)dir;
-		goto out;
-	}
-	parent = dir->d_parent;
-
-	inode = dir->d_inode;
-	sd = ERR_PTR(-EINVAL);
-	if (!sysfs_is_shadowed_inode(inode))
-		goto out_dput;
-
-	shadow = d_alloc(parent, &dir->d_name);
-	if (!shadow)
-		goto nomem;
-
-	sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR);
-	if (!sd)
-		goto nomem;
-	sd->s_elem.dir.kobj = kobj;
-
-	sysfs_addrm_start(&acxt, parent_sd);
-
-	/* add but don't link into children list */
-	sysfs_add_one(&acxt, sd);
-
-	/* attach and instantiate dentry */
-	sysfs_attach_dentry(sd, shadow);
-	d_instantiate(shadow, igrab(inode));
-	inc_nlink(inode);	/* tj: synchronization? */
-
-	sysfs_addrm_finish(&acxt);
-
-	dget(shadow);		/* Extra count - pin the dentry in core */
-
-	goto out_dput;
-
- nomem:
-	dput(shadow);
-	sd = ERR_PTR(-ENOMEM);
- out_dput:
-	dput(dir);
- out:
-	return sd;
-}
-
-/**
- *	sysfs_remove_shadow_dir - remove an object's directory.
- *	@shadow_sd: sysfs_dirent of shadow directory
- *
- *	The only thing special about this is that we remove any files in
- *	the directory before we remove the directory, and we've inlined
- *	what used to be sysfs_rmdir() below, instead of calling separately.
- */
-
-void sysfs_remove_shadow_dir(struct sysfs_dirent *shadow_sd)
-{
-	__sysfs_remove_dir(shadow_sd);
-}
-
 const struct file_operations sysfs_dir_operations = {
 	.open		= sysfs_dir_open,
 	.release	= sysfs_dir_close,
fs/sysfs/group.c +0 -1
@@ -13,7 +13,6 @@
 #include <linux/dcache.h>
 #include <linux/namei.h>
 #include <linux/err.h>
-#include <linux/fs.h>
 #include <asm/semaphore.h>
 #include "sysfs.h"
 
fs/sysfs/inode.c +0 -10
@@ -34,16 +34,6 @@ static const struct inode_operations sys
 	.setattr	= sysfs_setattr,
 };
 
-void sysfs_delete_inode(struct inode *inode)
-{
-	/* Free the shadowed directory inode operations */
-	if (sysfs_is_shadowed_inode(inode)) {
-		kfree(inode->i_op);
-		inode->i_op = NULL;
-	}
-	return generic_delete_inode(inode);
-}
-
 int sysfs_setattr(struct dentry * dentry, struct iattr * iattr)
 {
 	struct inode * inode = dentry->d_inode;
fs/sysfs/mount.c +1 -1
@@ -21,7 +21,7 @@ struct kmem_cache *sysfs_dir_cachep;
 
 static const struct super_operations sysfs_ops = {
 	.statfs		= simple_statfs,
-	.drop_inode	= sysfs_delete_inode,
+	.drop_inode	= generic_delete_inode,
 };
 
 struct sysfs_dirent sysfs_root = {
fs/sysfs/sysfs.h +0 -6
@@ -70,7 +70,6 @@ extern void sysfs_remove_one(struct sysf
 			     struct sysfs_dirent *sd);
 extern int sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt);
 
-extern void sysfs_delete_inode(struct inode *inode);
 extern struct inode * sysfs_get_inode(struct sysfs_dirent *sd);
 extern void sysfs_instantiate(struct dentry *dentry, struct inode *inode);
 
@@ -121,8 +120,3 @@ static inline void sysfs_put(struct sysf
 	if (sd && atomic_dec_and_test(&sd->s_count))
 		release_sysfs_dirent(sd);
 }
-
-static inline int sysfs_is_shadowed_inode(struct inode *inode)
-{
-	return S_ISDIR(inode->i_mode) && inode->i_op->follow_link;
-}
include/linux/kobject.h +0 -5
@@ -83,14 +83,9 @@ extern void kobject_init(struct kobject 
 extern void kobject_cleanup(struct kobject *);
 
 extern int __must_check kobject_add(struct kobject *);
-extern int __must_check kobject_shadow_add(struct kobject *kobj,
-					   struct sysfs_dirent *shadow_parent);
 extern void kobject_del(struct kobject *);
 
 extern int __must_check kobject_rename(struct kobject *, const char *new_name);
-extern int __must_check kobject_shadow_rename(struct kobject *kobj,
-					      struct sysfs_dirent *new_parent,
-					      const char *new_name);
 extern int __must_check kobject_move(struct kobject *, struct kobject *);
 
 extern int __must_check kobject_register(struct kobject *);
include/linux/sysfs.h +4 -23
@@ -17,9 +17,6 @@
 
 struct kobject;
 struct module;
-struct nameidata;
-struct dentry;
-struct sysfs_dirent;
 
 /* FIXME
  * The *owner field is no longer used, but leave around
@@ -94,14 +91,13 @@ extern int sysfs_schedule_callback(struc
 		void (*func)(void *), void *data, struct module *owner);
 
 extern int __must_check
-sysfs_create_dir(struct kobject *kobj, struct sysfs_dirent *shadow_parent_sd);
+sysfs_create_dir(struct kobject *);
 
 extern void
 sysfs_remove_dir(struct kobject *);
 
 extern int __must_check
-sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd,
-		 const char *new_name);
+sysfs_rename_dir(struct kobject *kobj, const char *new_name);
 
 extern int __must_check
 sysfs_move_dir(struct kobject *, struct kobject *);
@@ -138,12 +134,6 @@ void sysfs_remove_file_from_group(struct
 
 void sysfs_notify(struct kobject * k, char *dir, char *attr);
 
-
-extern int sysfs_make_shadowed_dir(struct kobject *kobj,
-	void * (*follow_link)(struct dentry *, struct nameidata *));
-extern struct sysfs_dirent *sysfs_create_shadow_dir(struct kobject *kobj);
-extern void sysfs_remove_shadow_dir(struct sysfs_dirent *shadow_sd);
-
 extern int __must_check sysfs_init(void);
 
 #else /* CONFIG_SYSFS */
@@ -154,8 +144,7 @@ static inline int sysfs_schedule_callbac
 	return -ENOSYS;
 }
 
-static inline int sysfs_create_dir(struct kobject *kobj,
-				   struct sysfs_dirent *shadow_parent_sd)
+static inline int sysfs_create_dir(struct kobject * kobj)
 {
 	return 0;
 }
@@ -165,9 +154,7 @@ static inline void sysfs_remove_dir(stru
 	;
 }
 
-static inline int sysfs_rename_dir(struct kobject *kobj,
-				   struct sysfs_dirent *new_parent_sd,
-				   const char *new_name)
+static inline int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
 {
 	return 0;
 }
@@ -242,12 +229,6 @@ static inline void sysfs_notify(struct k
 {
 }
 
-static inline int sysfs_make_shadowed_dir(struct kobject *kobj,
-	void * (*follow_link)(struct dentry *, struct nameidata *))
-{
-	return 0;
-}
-
 static inline int __must_check sysfs_init(void)
 {
 	return 0;
lib/kobject.c +6 -38

86 lines not on this page · raw message

Patches currently in gregkh-2.6 which might be from [email protected] are

bad/pci-domain/pci-device-ensure-sysdata-initialised.patch bad/pci-domain/pci-fix-the-x86-pci-domain-support-fix.patch bad/relayfs/sysfs-update-relay-file-support-for-generic-relay-api.patch bad/relayfs/relay-consolidate-relayfs-core-into-kernel-relay.c.patch bad/relayfs/relay-relay-header-cleanup.patch bad/relayfs/relayfs-remove-relayfs-in-favour-of-config_relay.patch bad/relayfs/sysfs-add-__attr_relay-helper-for-relay-attributes.patch bad/relayfs/sysfs-relay-channel-buffers-as-sysfs-attributes.patch bad/usbip/usb-usbip-more-dead-code-fix.patch bad/usbip/usb-usbip-build-fix.patch bad/usbip/usb-usbip-warning-fixes.patch
bad/ndevfs.patch
bad/battery-class-driver.patch bad/driver-model-convert-driver-model-to-mutexes.patch bad/gpl_future-test.patch
bad/gregkh-debugfs_example.patch
bad/speakup-kconfig-fix.patch
bad/speakup-build-fix.patch bad/pci-use-new-multi-phase-suspend-infrastructure.patch bad/shot-accross-the-bow.patch
bad/no-more-non-gpl-modules.patch
bad/spi-device.patch
bad/ata_piix-multithread.patch
bad/uio-irq.patch
bad/pci-two-drivers-on-one-pci-device.patch bad/pci-dynamic-id-cleanup.patch
bad/input-device.patch
bad/usb-stimulus.patch
driver/nozomi.patch driver/kobject-put-kobject_actions-in-kobject.h.patch driver/sysfs-implement-sysfs-manged-shadow-directory-support.patch driver/sysfs-implement-sysfs_delete_link-and-sysfs_rename_link.patch driver/sysfs-remove-first-pass-at-shadow-directory-support.patch driver/driver-core-implement-shadow-directory-support-for-device-classes.patch gregkh/gkh-version.patch
gregkh/sysfs-test.patch
gregkh/sysrq-u-laptop.patch
pci/pci_bridge-device.patch
pci/pci-piggy-bus.patch pci/pci-move-prototypes-for-pci_bus_find_capability-to-include-linux-pci.h.patch pci/pci-document-pci_iomap.patch
usb/usb-gotemp.patch usb/kobject-put-kobject_actions-in-kobject.h.patch usb/usb-add-the-concept-of-default-authorization-to-usb-hosts.patch usb/usb-cleanup-usb_register_bus-and-hook-up-sysfs-group.patch usb/usb-initialize-authorization-and-wusb-bits-in-usb-devices.patch usb/usb-introduce-usb_device-authorization-bits.patch usb/usb-usb_set_configuration-obeys-authorization.patch usb/usb-usb.h-kernel-doc-additions.patch
HOWTO

#140 lore raw
In reply to a message not in the archive

Hello,

Hope my late review isn't too late.

Quoted 4 lines · Eric W. Biederman

While shadow directories appear to be a good idea, the current scheme of controlling their creation and destruction outside of sysfs appears to be a locking and maintenance nightmare in the face of sysfs directories dynamically coming and going.

Yeah, shadow directory was a real PITA while restructuring sysfs. I tried hard to keep it working but, without test case and detailed documentation, I'm pretty sure I broke something.

My feeling was that it was merged too early and implementation was too tightly coupled with other more regular paths. Anyways, glad shadow dirs are getting some loving care. If properly done, we should be able to simplify sysfs_get_dentry(), removal logic and save a pointer in sysfs_dirent.

Quoted 3 lines

Which can now occur for
directories containing network devices when CONFIG_SYSFS_DEPRECATED is not set.

Shadow directories were always pinned - both the shadowed one and shadows. Well, that was the theory at least.

Quoted 5 lines
fs/sysfs/dir.c +1 -3
-int sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd,
-		     const char *new_name)
+int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
 {
-	struct sysfs_dirent *sd = kobj->sd;

fs/sysfs/dir.c void sysfs_remove_dir(struct kobject * kobj) · line 893

Quoted 10 lines
fs/sysfs/dir.c +5 -1
-	struct dentry *new_parent = NULL;
+	struct sysfs_dirent *sd;
+	struct dentry *parent = NULL;
 	struct dentry *old_dentry = NULL, *new_dentry = NULL;
+	struct sysfs_dirent *parent_sd;
 	const char *dup_name = NULL;
 	int error;
 
+	if (!kobj->parent)
+		return -EINVAL;

Please don't do this. The goal is to decouple sysfs and kobj.

Quoted 6 lines
fs/sysfs/dir.c +1 -0
 	/* get dentries */
+	sd = kobj->sd;
 	old_dentry = sysfs_get_dentry(sd);
 	if (IS_ERR(old_dentry)) {
 		error = PTR_ERR(old_dentry);
 		goto out_dput;

fs/sysfs/dir.c void sysfs_remove_dir(struct kobject * kobj) · line 907

Quoted 10 lines
fs/sysfs/dir.c +4 -3
 	}
 
-	new_parent = sysfs_get_dentry(new_parent_sd);
-	if (IS_ERR(new_parent)) {
-		error = PTR_ERR(new_parent);
+	parent_sd = kobj->parent->sd;
+	parent = sysfs_get_dentry(parent_sd);
+	if (IS_ERR(parent)) {
+		error = PTR_ERR(parent);
 		goto out_dput;

You can simply do parent = old_dentry->d_parent. parent is guaranteed to be there as long as we hold old_dentry. In the original code, new_parent needed to be looked because new_parent wasn't necessarily old_dentry's parent.

fs/sysfs/dir.c int sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd, · line 963

Quoted 10 lines
fs/sysfs/dir.c +2 -2
@@ -965,10 +957,10 @@ int sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd,
  out_drop:
 	d_drop(new_dentry);
  out_unlock:
-	mutex_unlock(&new_parent->d_inode->i_mutex);
+	mutex_unlock(&parent->d_inode->i_mutex);
  out_dput:
 	kfree(dup_name);
-	dput(new_parent);
+	dput(parent);

So, dput(parent) can go away too.

fs/sysfs/group.c line 17

Quoted 11 lines
fs/sysfs/group.c +0 -1
index f318b73..4606f7c 100644
@@ -13,7 +13,6 @@
 #include <linux/dcache.h>
 #include <linux/namei.h>
 #include <linux/err.h>
-#include <linux/fs.h>
 #include <asm/semaphore.h>
 #include "sysfs.h"

Can you explain this one a bit?

Thanks.

Signature · 1 line
tejun

A message that is not in the archive (sent off-list or not yet received): [email protected]

#142 lore raw
Quoted 10 lines · Tejun Heo

Hello,

Hope my late review isn't too late.

Quoted 4 lines · Eric W. Biederman

While shadow directories appear to be a good idea, the current scheme of controlling their creation and destruction outside of sysfs appears to be a locking and maintenance nightmare in the face of sysfs directories dynamically coming and going.

Quoted 6 lines

Yeah, shadow directory was a real PITA while restructuring sysfs. I tried hard to keep it working but, without test case and detailed documentation, I'm pretty sure I broke something.

My feeling was that it was merged too early and implementation was too tightly coupled with other more regular paths.

It's a problem because sysfs has this tendency especially before your cleanups to be tightly coupled.

But yes the original implementation was probably factored at the wrong level, not giving sysfs enough responsibility. Which became apparent when the single objects started living all over the sysfs tree.

Quoted 4 lines

Anyways, glad shadow
dirs are getting some loving care. If properly done, we should be able to simplify sysfs_get_dentry(), removal logic and save a pointer in sysfs_dirent.

Hopefully. The logic in sysfs_resolve_for_create (i.e. which shadow dir do you intend to create something is tricky with the current locking logic).

Quoted 6 lines
Quoted 3 lines

Which can now occur for
directories containing network devices when CONFIG_SYSFS_DEPRECATED is not set.

Shadow directories were always pinned - both the shadowed one and shadows. Well, that was the theory at least.

Yes. The difference I was referring to was:
/sys/class/net/ used to be the only directory I needed to shadow.

Now there is one net/ in each networking device and several other little things.

Quoted 14 lines
Quoted 1 line

-int sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent

*new_parent_sd,

Quoted 12 lines
fs/sysfs/dir.c +4 -3
-		     const char *new_name)
+int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
 {
-	struct sysfs_dirent *sd = kobj->sd;
-	struct dentry *new_parent = NULL;
+	struct sysfs_dirent *sd;
+	struct dentry *parent = NULL;
 	struct dentry *old_dentry = NULL, *new_dentry = NULL;
+	struct sysfs_dirent *parent_sd;
 	const char *dup_name = NULL;
 	int error;
Quoted 4 lines

fs/sysfs/dir.c void sysfs_remove_dir(struct kobject * kobj) · line 893

Quoted 2 lines
fs/sysfs/dir.c +2 -0
+	if (!kobj->parent)
+		return -EINVAL;

Please don't do this. The goal is to decouple sysfs and kobj.

I have no problem moving that test back to a higher level. My practical goal of this first patch was to remove the original shadow dir work so I had a clean slate to start with. I got tired and perhaps didn't do as clean a break here as I could have.

Do you mind an incremental patch to move the kobj->parent test?

As long as we don't break git-bisect support I would really prefer to just build on top of the patches that are there.

It has been a major hair pulling exercise to get everything to get everything to work in the presence of other parallel changes in sysfs. I only had to relearn the locking and organization rules 3 times. I really don't want to repeat it but I am happy to incrementally improve things.

Quoted 8 lines
Quoted 8 lines
fs/sysfs/dir.c +1 -0
 	/* get dentries */
+	sd = kobj->sd;
 	old_dentry = sysfs_get_dentry(sd);
 	if (IS_ERR(old_dentry)) {
 		error = PTR_ERR(old_dentry);
 		goto out_dput;
 	}
Quoted 13 lines

fs/sysfs/dir.c void sysfs_remove_dir(struct kobject * kobj) · line 907

Quoted 8 lines
fs/sysfs/dir.c +4 -3
-	new_parent = sysfs_get_dentry(new_parent_sd);
-	if (IS_ERR(new_parent)) {
-		error = PTR_ERR(new_parent);
+	parent_sd = kobj->parent->sd;
+	parent = sysfs_get_dentry(parent_sd);
+	if (IS_ERR(parent)) {
+		error = PTR_ERR(parent);
 		goto out_dput;

You can simply do parent = old_dentry->d_parent. parent is guaranteed to be there as long as we hold old_dentry. In the original code, new_parent needed to be looked because new_parent wasn't necessarily old_dentry's parent.

Yes. This part is probably the ugliest, and least well factored part of this patchset. sysfs_rename_dir wasn't something I could completely revert to it's pre shadow directory state as that was no longer available. What do you think of sysfs_rename_dir after the second patch in the patchset?

I still think I have the limitation that new_parent and old_parent may be separate. (Although clearly not at this point in the patch series). I seem to remember trying to minimize the flux in sysfs_rename_dir and eventually giving up. Having found a path that seemed to be good enough.

Quoted 13 lines

Review int sysfs_rename_dir(struct kobject *kobj, struct

Quoted 1 line
(quoted diff) +0 -0
@@ -965,10 +957,10 @@ int sysfs_rename_dir(struct kobject *kobj, struct

sysfs_dirent *new_parent_sd,

fs/sysfs/dir.c int sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd, · line 963

Quoted 9 lines
fs/sysfs/dir.c +2 -2
  out_drop:
 	d_drop(new_dentry);
  out_unlock:
-	mutex_unlock(&new_parent->d_inode->i_mutex);
+	mutex_unlock(&parent->d_inode->i_mutex);
  out_dput:
 	kfree(dup_name);
-	dput(new_parent);
+	dput(parent);

So, dput(parent) can go away too.

I actually managed to remove sysfs_get_dentry entirely from sysfs_rename_dir and to use sysfs_addrm_start.

Quoted 5 lines
Quoted 5 lines
fs/sysfs/group.c +0 -0
index f318b73..4606f7c 100644
@@ -13,7 +13,6 @@
Quoted 8 lines

fs/sysfs/group.c line 17

Quoted 6 lines
fs/sysfs/group.c +0 -1
 #include <linux/dcache.h>
 #include <linux/namei.h>
 #include <linux/err.h>
-#include <linux/fs.h>
 #include <asm/semaphore.h>
 #include "sysfs.h"

Can you explain this one a bit?

This include was added by the original shadow directory support and with it the original shadow directory support removed the include became unnecessary. I forget the exact details.

But so I could catch subtle things like that is why my patchset is structured as first a removal pass and then the new stuff.

Eric