[PATCH 10/15] sysfs: Merge sysfs_rename_dir and sysfs_move_dir

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Eric W. Biederman
Date: Thursday, July 3, 2008 - 6:17 pm

These two functions do 90% of the same work and it doesn't
significantly obfuscate the function to allow both the parent dir and
the name to change at the same time.  So merge them together to
simplify maintenance, and increase testing.

Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
---
 fs/sysfs/dir.c |  121 +++++++++++++++++--------------------------------------
 1 files changed, 38 insertions(+), 83 deletions(-)

diff --git a/fs/sysfs/dir.c b/fs/sysfs/dir.c
index 6dc3376..fe2bb1c 100644
--- a/fs/sysfs/dir.c
+++ b/fs/sysfs/dir.c
@@ -924,44 +924,57 @@ err_out:
 	return error;
 }
 
-int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
+static int sysfs_mv_dir(struct sysfs_dirent *sd,
+	struct sysfs_dirent *new_parent_sd, const char *new_name)
 {
-	struct sysfs_dirent *sd = kobj->sd;
 	struct list_head todo;
 	struct sysfs_rename_struct *srs;
-	struct inode *parent_inode = NULL;
+	struct inode *old_parent_inode = NULL, *new_parent_inode = NULL;
 	const char *dup_name = NULL;
 	const void *old_tag, *tag;
 	int error;
 
 	INIT_LIST_HEAD(&todo);
+	BUG_ON(!sd->s_parent);
 	mutex_lock(&sysfs_rename_mutex);
+	if (!new_parent_sd)
+		new_parent_sd = &sysfs_root;
+
 	old_tag = sd->s_tag;
 	tag = sysfs_creation_tag(sd->s_parent, sd);
 
 	error = 0;
-	if ((old_tag == tag) && (strcmp(sd->s_name, new_name) == 0))
-		goto out;	/* nothing to rename */
+	if ((sd->s_parent == new_parent_sd) && (old_tag == tag) &&
+	    (strcmp(sd->s_name, new_name) == 0))
+		goto out;	/* nothing to do */
 
 	sysfs_grab_supers();
 	if (old_tag == tag) {
-		error = prep_rename(&todo, sd, sd->s_parent, new_name);
+		error = prep_rename(&todo, sd, new_parent_sd, new_name);
 		if (error)
 			goto out_release;
 	}
 
 	error = -ENOMEM;
 	mutex_lock(&sysfs_mutex);
-	parent_inode = sysfs_get_inode(sd->s_parent);
+	old_parent_inode = sysfs_get_inode(sd->s_parent);
+	new_parent_inode = sysfs_get_inode(new_parent_sd);
 	mutex_unlock(&sysfs_mutex);
-	if (!parent_inode)
+	if (!old_parent_inode || !new_parent_inode)
 		goto out_release;
 
-	mutex_lock(&parent_inode->i_mutex);
+again:
+	mutex_lock(&old_parent_inode->i_mutex);
+	if (old_parent_inode != new_parent_inode) {
+		if (!mutex_trylock(&new_parent_inode->i_mutex)) {
+			mutex_unlock(&old_parent_inode->i_mutex);
+			goto again;
+		}
+	}
 	mutex_lock(&sysfs_mutex);
 
 	error = -EEXIST;
-	if (sysfs_find_dirent(sd->s_parent, tag, new_name))
+	if (sysfs_find_dirent(new_parent_sd, tag, new_name))
 		goto out_unlock;
 
 	/* rename sysfs_dirent */
@@ -974,7 +987,7 @@ int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
 	sd->s_name = new_name;
 	sd->s_tag = tag;
 
-	/* rename */
+	/* rename dcache entries */
 	list_for_each_entry(srs, &todo, list) {
 		d_add(srs->new_dentry, NULL);
 		d_move(srs->old_dentry, srs->new_dentry);
@@ -994,77 +1007,6 @@ int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
 		}
 	}
 
-	error = 0;
-out_unlock:
-	mutex_unlock(&sysfs_mutex);
-	mutex_unlock(&parent_inode->i_mutex);
-	kfree(dup_name);
-out_release:
-	iput(parent_inode);
-	post_rename(&todo);
-	sysfs_release_supers();
-out:
-	mutex_unlock(&sysfs_rename_mutex);
-	return error;
-}
-
-int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
-{
-	struct sysfs_dirent *sd = kobj->sd;
-	struct sysfs_dirent *new_parent_sd;
-	struct list_head todo;
-	struct sysfs_rename_struct *srs;
-	struct inode *old_parent_inode = NULL, *new_parent_inode = NULL;
-	int error;
-	const void *tag;
-
-	INIT_LIST_HEAD(&todo);
-	mutex_lock(&sysfs_rename_mutex);
-	BUG_ON(!sd->s_parent);
-	new_parent_sd = new_parent_kobj->sd ? new_parent_kobj->sd : &sysfs_root;
-	tag = sd->s_tag;
-
-	error = 0;
-	if (sd->s_parent == new_parent_sd)
-		goto out;	/* nothing to move */
-
-	sysfs_grab_supers();
-	error = prep_rename(&todo, sd, new_parent_sd, sd->s_name);
-	if (error)
-		goto out_release;
-
-	error = -ENOMEM;
-	mutex_lock(&sysfs_mutex);
-	old_parent_inode = sysfs_get_inode(sd->s_parent);
-	mutex_unlock(&sysfs_mutex);
-	if (!old_parent_inode)
-		goto out_release;
-
-	error = -ENOMEM;
-	mutex_lock(&sysfs_mutex);
-	new_parent_inode = sysfs_get_inode(new_parent_sd);
-	mutex_unlock(&sysfs_mutex);
-	if (!new_parent_inode)
-		goto out_release;
-
-again:
-	mutex_lock(&old_parent_inode->i_mutex);
-	if (!mutex_trylock(&new_parent_inode->i_mutex)) {
-		mutex_unlock(&old_parent_inode->i_mutex);
-		goto again;
-	}
-	mutex_lock(&sysfs_mutex);
-
-	error = -EEXIST;
-	if (sysfs_find_dirent(new_parent_sd, tag, sd->s_name))
-		goto out_unlock;
-
-	error = 0;
-	list_for_each_entry(srs, &todo, list) {
-		d_add(srs->new_dentry, NULL);
-		d_move(srs->old_dentry, srs->new_dentry);
-	}
-
 	/* Remove from old parent's list and insert into new parent's list. */
 	sysfs_unlink_sibling(sd);
 	sysfs_get(new_parent_sd);
@@ -1072,10 +1014,13 @@ again:
 	sd->s_parent = new_parent_sd;
 	sysfs_link_sibling(sd);
 
+	error = 0;
 out_unlock:
 	mutex_unlock(&sysfs_mutex);
-	mutex_unlock(&new_parent_inode->i_mutex);
+	if (new_parent_inode != old_parent_inode)
+		mutex_unlock(&new_parent_inode->i_mutex);
 	mutex_unlock(&old_parent_inode->i_mutex);
+	kfree(dup_name);
 
 out_release:
 	iput(new_parent_inode);
@@ -1087,6 +1032,16 @@ out:
 	return error;
 }
 
+int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
+{
+	return sysfs_mv_dir(kobj->sd, kobj->sd->s_parent, new_name);
+}
+
+int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
+{
+	return sysfs_mv_dir(kobj->sd, new_parent_kobj->sd, kobj->sd->s_name);
+}
+
 /* Relationship between s_mode and the DT_xxx types */
 static inline unsigned char dt_type(struct sysfs_dirent *sd)
 {
-- 
1.5.3.rc6.17.g1911

--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
Re: [PATCH 06/11] sysfs: Implement sysfs tagged directory ..., Eric W. Biederman, (Thu Jun 26, 1:21 pm)
Re: [PATCH 06/11] sysfs: Implement sysfs tagged directory ..., Eric W. Biederman, (Mon Jun 30, 11:56 am)
Re: [PATCH 06/11] sysfs: Implement sysfs tagged directory ..., Eric W. Biederman, (Tue Jul 1, 12:50 am)
Re: [PATCH 06/11] sysfs: Implement sysfs tagged directory ..., Eric W. Biederman, (Tue Jul 1, 2:20 am)
Re: [PATCH 06/11] sysfs: Implement sysfs tagged directory ..., Eric W. Biederman, (Tue Jul 1, 5:30 am)
Re: [PATCH 06/11] sysfs: Implement sysfs tagged directory ..., Eric W. Biederman, (Tue Jul 1, 8:53 pm)
Re: [PATCH 06/11] sysfs: Implement sysfs tagged directory ..., Eric W. Biederman, (Wed Jul 2, 9:49 am)
Re: [PATCH 06/11] sysfs: Implement sysfs tagged directory ..., Eric W. Biederman, (Wed Jul 2, 10:11 pm)
Re: [PATCH 06/11] sysfs: Implement sysfs tagged directory ..., Eric W. Biederman, (Thu Jul 3, 5:27 am)
Re: [PATCH 06/11] sysfs: Implement sysfs tagged directory ..., Eric W. Biederman, (Thu Jul 3, 12:57 pm)
Re: [PATCH 06/11] sysfs: Implement sysfs tagged directory ..., Eric W. Biederman, (Thu Jul 3, 1:08 pm)
[PATCH 00/15] sysfs support for namespaces, Eric W. Biederman, (Thu Jul 3, 5:48 pm)
[PATCH 02/15] sysfs: Support for preventing unmounts., Eric W. Biederman, (Thu Jul 3, 6:07 pm)
[PATCH 03/15] sysfs: sysfs_get_dentry add a sb parameter, Eric W. Biederman, (Thu Jul 3, 6:08 pm)
[PATCH 04/15] sysfs: Implement __sysfs_get_dentry, Eric W. Biederman, (Thu Jul 3, 6:09 pm)
[PATCH 05/15] sysfs: Rename Support multiple superblocks, Eric W. Biederman, (Thu Jul 3, 6:10 pm)
[PATCH 06/15] Introduce sysfs_sd_setattr and fix sysfs_chmod, Eric W. Biederman, (Thu Jul 3, 6:11 pm)
[PATCH 08/15] sysfs: Make sysfs_mount static once again., Eric W. Biederman, (Thu Jul 3, 6:14 pm)
[PATCH 10/15] sysfs: Merge sysfs_rename_dir and sysfs_move_dir, Eric W. Biederman, (Thu Jul 3, 6:17 pm)
Re: [PATCH 00/15] sysfs support for namespaces, Eric W. Biederman, (Thu Jul 3, 6:27 pm)
Re: [PATCH 12/15] driver core: Implement tagged directory ..., Eric W. Biederman, (Fri Jul 4, 6:31 am)
Re: [PATCH 12/15] driver core: Implement tagged directory ..., Eric W. Biederman, (Fri Jul 4, 2:49 pm)
Re: [PATCH 12/15] driver core: Implement tagged directory ..., Eric W. Biederman, (Fri Jul 4, 3:00 pm)
Re: [PATCH 00/15] sysfs support for namespaces, Eric W. Biederman, (Sat Jul 5, 9:42 pm)
Re: [PATCH 00/15] sysfs support for namespaces, Cornelia Huck, (Mon Jul 7, 4:41 am)
Re: [PATCH 00/15] sysfs support for namespaces, Eric W. Biederman, (Mon Jul 7, 5:22 am)
Re: [PATCH 12/15] driver core: Implement tagged directory ..., Eric W. Biederman, (Sun Jul 13, 6:54 pm)
Re: [PATCH 12/15] driver core: Implement tagged directory ..., Eric W. Biederman, (Tue Jul 15, 10:41 pm)
Re: [PATCH 12/15] driver core: Implement tagged directory ..., Eric W. Biederman, (Tue Jul 15, 11:32 pm)
Re: [PATCH 12/15] driver core: Implement tagged directory ..., Eric W. Biederman, (Wed Jul 16, 12:07 pm)
Re: [PATCH 12/15] driver core: Implement tagged directory ..., Eric W. Biederman, (Wed Jul 16, 2:09 pm)
Re: [PATCH 12/15] driver core: Implement tagged directory ..., Eric W. Biederman, (Fri Jul 18, 1:19 pm)
Re: [PATCH 12/15] driver core: Implement tagged directory ..., Eric W. Biederman, (Sat Aug 2, 11:59 pm)
Re: [PATCH 09/15] sysfs: Implement sysfs tagged directory ..., Eric W. Biederman, (Tue Aug 19, 11:58 pm)
[PATCH 0/8] sysfs namespace support, Eric W. Biederman, (Wed Aug 20, 11:31 pm)
[PATCH 1/8] sysfs: Implement sysfs tagged directory support., Eric W. Biederman, (Wed Aug 20, 11:33 pm)
[PATCH 2/8] sysfs: Merge sysfs_rename_dir and sysfs_move_dir, Eric W. Biederman, (Wed Aug 20, 11:34 pm)
[PATCH 3/8] sysfs: Implement sysfs_delete_link and sysfs_r ..., Eric W. Biederman, (Wed Aug 20, 11:35 pm)
[PATCH 5/8] sysfs: Remove sysfs_create_link_nowarn, Eric W. Biederman, (Wed Aug 20, 11:36 pm)
Re: [PATCH 0/8] sysfs namespace support, David Miller, (Wed Aug 20, 11:37 pm)
[PATCH 4/8] driver core: Implement tagged directory suppor ..., Eric W. Biederman, (Wed Aug 20, 11:37 pm)
[PATCH 7/8] netns: Enable tagging for net_class directorie ..., Eric W. Biederman, (Wed Aug 20, 11:39 pm)
[PATCH 8/8] sysfs: user namespaces: fix bug with clone(CLO ..., Eric W. Biederman, (Wed Aug 20, 11:40 pm)
Re: [PATCH 1/8] sysfs: Implement sysfs tagged directory su ..., Eric W. Biederman, (Mon Oct 13, 8:20 pm)