Skip to contentSkip to conversation

Unionfs updates

GIT PULL New Josef 'Jeff' Sipek 1 participant 14 messages Mar 5, 2007

lore
Share & tools
lore
#0 lore raw

The following patches (also available though the git tree) address a number of code cleanliness issues with Unionfs.

You can pull from 'master' branch of
git://git.kernel.org/pub/scm/linux/kernel/git/jsipek/unionfs.git

to receive the following:

Erez Zadok (6):
fs/unionfs: Fix a memory leak & null pointer dereference fs/unionfs/: Fix a memory leak in unionfs_read_super fs/unionfs/: Fix copyup_deleted_file dentry leak fs/unionfs/: mntput in __cleanup_dentry fs/unionfs/: Fix dentry leak in copyup_named_dentry fs/unionfs/: Fix unlocking in error paths

Josef 'Jeff' Sipek (7):
fs/unionfs/: Don't grab dentry private data mutex in unionfs_d_release fs/unionfs/: Several small cleanups in unionfs_interpose fs/unionfs/: Rename unionfs_d_revalidate_wrap
fs/unionfs/: Remove alloc_filldir_node
fs/unionfs/: Use SEEK_{SET,CUR} instead of hardcoded values fs/unionfs/: Check return value of d_path
fs/unionfs/: Miscellaneous coding style fixes

 fs/unionfs/commonfops.c |   22 ++++++++++++++++++----
 fs/unionfs/copyup.c     |   17 +++++++++++++----
 fs/unionfs/dentry.c     |   15 ++++-----------
 fs/unionfs/dirfops.c    |    6 +++---
 fs/unionfs/lookup.c     |   11 +++++++++--
 fs/unionfs/main.c       |   18 ++++++------------
 fs/unionfs/rdstate.c    |    8 +-------
 fs/unionfs/super.c      |    8 ++++++--
 fs/unionfs/union.h      |    2 +-
 9 files changed, 61 insertions(+), 46 deletions(-)

Josef 'Jeff' Sipek.

[email protected]

#1 lore raw

PATCH 1/13 fs/unionfs: Fix a memory leak & null pointer dereference

From: Erez Zadok <[email protected]>

---
 fs/unionfs/commonfops.c |    9 ++++++---
 1 files changed, 6 insertions(+), 3 deletions(-)
fs/unionfs/commonfops.c +6 -3
index 379c525..66d6ce9 100644
@@ -230,7 +230,7 @@ int unionfs_file_revalidate(struct file *file, int willwrite)
 	unionfs_read_lock(sb);
 	if (!unionfs_d_revalidate(dentry, NULL) && !d_deleted(dentry)) {
 		err = -ESTALE;
-		goto out;
+		goto out_nofree;
 	}
 
 	sbgen = atomic_read(&UNIONFS_SB(sb)->generation);
@@ -286,6 +286,9 @@ int unionfs_file_revalidate(struct file *file, int willwrite)
 	}
 
 out:
+	if (err)
+		kfree(UNIONFS_F(file)->lower_files);
+out_nofree:
 	unionfs_unlock_dentry(dentry);
 	unionfs_read_unlock(dentry->d_sb);
 	return err;
@@ -391,7 +394,7 @@ int unionfs_open(struct inode *inode, struct file *file)
 	file->private_data = kzalloc(sizeof(struct unionfs_file_info), GFP_KERNEL);
 	if (!UNIONFS_F(file)) {
 		err = -ENOMEM;
-		goto out;
+		goto out_nofree;
 	}
 	fbstart(file) = -1;
 	fbend(file) = -1;
@@ -444,7 +447,7 @@ out:
 		kfree(UNIONFS_F(file)->lower_files);
 		kfree(UNIONFS_F(file));
 	}
-
+out_nofree:
 	return err;
 }
 
Signature · 1 line
1.5.0.2.260.g2eb065
#2 lore raw

PATCH 2/13 fs/unionfs/: Fix a memory leak in unionfs_read_super

In reply to Josef 'Jeff' Sipek

From: Erez Zadok <[email protected]>

---
 fs/unionfs/main.c |    7 ++++---
 1 files changed, 4 insertions(+), 3 deletions(-)
fs/unionfs/main.c +4 -3
index ca7ee26..bd64242 100644
@@ -584,10 +584,11 @@ static int unionfs_read_super(struct super_block *sb, void *raw_data,
 	atomic_set(&UNIONFS_D(sb->s_root)->generation, 1);
 
 	/* call interpose to create the upper level inode */
-	if ((err = unionfs_interpose(sb->s_root, sb, 0)))
-		goto out_freedpd;
+	err = unionfs_interpose(sb->s_root, sb, 0);
 	unionfs_unlock_dentry(sb->s_root);
-	goto out;
+	if (!err)
+		goto out;
+	/* else fall through */
 
 out_freedpd:
 	if (UNIONFS_D(sb->s_root)) {
Signature · 1 line
1.5.0.2.260.g2eb065
#3 lore raw

PATCH 3/13 fs/unionfs/: Don't grab dentry private data mutex in unionfs_d_release

In reply to Josef 'Jeff' Sipek

Grabbing the UNIONFS_D(dentry)->lock is completely unnecessary and there are no other references; we are about to free the object anyway. Additionally, grabbing the mutex produces warning when the slab object is reused - as it was freed while there still was a reference to it.

---
 fs/unionfs/dentry.c |    6 ------
 1 files changed, 0 insertions(+), 6 deletions(-)
fs/unionfs/dentry.c +0 -6
index 3721409..ac4bf0e 100644
@@ -190,12 +190,6 @@ static void unionfs_d_release(struct dentry *dentry)
 {
 	int bindex, bstart, bend;
 
-	/* There is no reason to lock the dentry, because we have the only
-	 * reference, but the printing functions verify that we have a lock
-	 * on the dentry before calling dbstart, etc.
-	 */
-	unionfs_lock_dentry(dentry);
-
 	/* this could be a negative dentry, so check first */
 	if (!UNIONFS_D(dentry)) {
 		printk(KERN_DEBUG "dentry without private data: %.*s",
Signature · 1 line
1.5.0.2.260.g2eb065
#4 lore raw

PATCH 4/13 fs/unionfs/: Several small cleanups in unionfs_interpose

In reply to Josef 'Jeff' Sipek

1) No need to lock the inode - lockdep was complaining about potential circular dependency

2) No need to use temporary variable for iunique() inode number

3) Removed unneeded comment

---
 fs/unionfs/main.c |   11 ++---------
 1 files changed, 2 insertions(+), 9 deletions(-)
fs/unionfs/main.c +2 -9
index bd64242..a37916d 100644
@@ -66,19 +66,14 @@ int unionfs_interpose(struct dentry *dentry, struct super_block *sb, int flag)
 			err = -ENOMEM;
 			goto out;
 		}
-		mutex_lock(&inode->i_mutex);
 	} else {
-		ino_t ino;
 		/* get unique inode number for unionfs */
-		ino = iunique(sb, UNIONFS_ROOT_INO);
-
-		inode = iget(sb, ino);
+		inode = iget(sb, iunique(sb, UNIONFS_ROOT_INO));
 		if (!inode) {
-			err = -EACCES;	/* should be impossible??? */
+			err = -EACCES;
 			goto out;
 		}
 
-		mutex_lock(&inode->i_mutex);
 		if (atomic_read(&inode->i_count) > 1)
 			goto skip;
 	}
@@ -147,8 +142,6 @@ skip:
 		BUG();
 	}
 
-	mutex_unlock(&inode->i_mutex);
-
 out:
 	return err;
 }
Signature · 1 line
1.5.0.2.260.g2eb065
#5 lore raw

PATCH 5/13 fs/unionfs/: Rename unionfs_d_revalidate_wrap

In reply to Josef 'Jeff' Sipek

Follow the convention of "foo" calling "__foo".

---
 fs/unionfs/commonfops.c |    2 +-
 fs/unionfs/dentry.c     |    9 ++++-----
 fs/unionfs/union.h      |    2 +-
 3 files changed, 6 insertions(+), 7 deletions(-)
fs/unionfs/commonfops.c +1 -1
index 66d6ce9..2664be9 100644
@@ -228,7 +228,7 @@ int unionfs_file_revalidate(struct file *file, int willwrite)
 	unionfs_lock_dentry(dentry);
 	sb = dentry->d_sb;
 	unionfs_read_lock(sb);
-	if (!unionfs_d_revalidate(dentry, NULL) && !d_deleted(dentry)) {
+	if (!__unionfs_d_revalidate(dentry, NULL) && !d_deleted(dentry)) {
 		err = -ESTALE;
 		goto out_nofree;
 	}
fs/unionfs/dentry.c +4 -5
index ac4bf0e..cae4897 100644
@@ -21,7 +21,7 @@
 /*
  * returns 1 if valid, 0 otherwise.
  */
-int unionfs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
+int __unionfs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
 {
 	int valid = 1;		/* default is valid (1); invalid is 0. */
 	struct dentry *hidden_dentry;
@@ -174,13 +174,12 @@ out:
 	return valid;
 }
 
-static int unionfs_d_revalidate_wrap(struct dentry *dentry,
-				     struct nameidata *nd)
+static int unionfs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
 {
 	int err;
 
 	unionfs_lock_dentry(dentry);
-	err = unionfs_d_revalidate(dentry, nd);
+	err = __unionfs_d_revalidate(dentry, nd);
 	unionfs_unlock_dentry(dentry);
 
 	return err;
@@ -226,7 +225,7 @@ out:
 }
 
 struct dentry_operations unionfs_dops = {
-	.d_revalidate	= unionfs_d_revalidate_wrap,
+	.d_revalidate	= unionfs_d_revalidate,
 	.d_release	= unionfs_d_release,
 };
 
fs/unionfs/union.h +1 -1
index 60276c7..bae3c76 100644
@@ -296,7 +296,7 @@ extern int unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
 int unionfs_unlink(struct inode *dir, struct dentry *dentry);
 int unionfs_rmdir(struct inode *dir, struct dentry *dentry);
 
-int unionfs_d_revalidate(struct dentry *dentry, struct nameidata *nd);
+int __unionfs_d_revalidate(struct dentry *dentry, struct nameidata *nd);
 
 /* The values for unionfs_interpose's flag. */
 #define INTERPOSE_DEFAULT	0
Signature · 1 line
1.5.0.2.260.g2eb065
#6 lore raw

PATCH 6/13 fs/unionfs/: Remove alloc_filldir_node

In reply to Josef 'Jeff' Sipek

alloc_filldir_node was used only once. Additionally, all the arguments passed to it were ignored wasting stack space for no reason whatsoever.

---
 fs/unionfs/rdstate.c |    8 +-------
 1 files changed, 1 insertions(+), 7 deletions(-)
fs/unionfs/rdstate.c +1 -7
index e240285..b67a86a 100644
@@ -225,12 +225,6 @@ struct filldir_node *find_filldir_node(struct unionfs_dir_state *rdstate,
 	return cursor;
 }
 
-static struct filldir_node *alloc_filldir_node(const char *name, int namelen,
-					       unsigned int hash, int bindex)
-{
-	return kmem_cache_alloc(unionfs_filldir_cachep, GFP_KERNEL);
-}
-
 int add_filldir_node(struct unionfs_dir_state *rdstate, const char *name,
 		     int namelen, int bindex, int whiteout)
 {
@@ -246,7 +240,7 @@ int add_filldir_node(struct unionfs_dir_state *rdstate, const char *name,
 	index = hash % rdstate->size;
 	head = &(rdstate->list[index]);
 
-	new = alloc_filldir_node(name, namelen, hash, bindex);
+	new = kmem_cache_alloc(unionfs_filldir_cachep, GFP_KERNEL);
 	if (!new) {
 		err = -ENOMEM;
 		goto out;
Signature · 1 line
1.5.0.2.260.g2eb065
#7 lore raw

PATCH 7/13 fs/unionfs/: Use SEEK_{SET,CUR} instead of hardcoded values

In reply to Josef 'Jeff' Sipek
---
 fs/unionfs/dirfops.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
fs/unionfs/dirfops.c +3 -3
index 2b77fa9..8f568c7 100644
@@ -135,15 +135,15 @@ static int unionfs_readdir(struct file *file, void *dirent, filldir_t filldir)
 		buf.sb = inode->i_sb;
 
 		/* Read starting from where we last left off. */
-		offset = vfs_llseek(hidden_file, uds->dirpos, 0);
+		offset = vfs_llseek(hidden_file, uds->dirpos, SEEK_SET);
 		if (offset < 0) {
 			err = offset;
 			goto out;
 		}
 		err = vfs_readdir(hidden_file, unionfs_filldir, &buf);
-		/* Save the position for when we continue. */
 
-		offset = vfs_llseek(hidden_file, 0, 1);
+		/* Save the position for when we continue. */
+		offset = vfs_llseek(hidden_file, 0, SEEK_CUR);
 		if (offset < 0) {
 			err = offset;
 			goto out;
Signature · 1 line
1.5.0.2.260.g2eb065
#8 lore raw

PATCH 8/13 fs/unionfs/: Check return value of d_path

In reply to Josef 'Jeff' Sipek
---
 fs/unionfs/super.c |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
fs/unionfs/super.c +5 -0
index 38443c7..176cfb6 100644
@@ -311,6 +311,11 @@ static int unionfs_show_options(struct seq_file *m, struct vfsmount *mnt)
 		path = d_path(unionfs_lower_dentry_idx(sb->s_root, bindex),
 			   unionfs_lower_mnt_idx(sb->s_root, bindex), tmp_page,
 			   PAGE_SIZE);
+		if (IS_ERR(path)) {
+			ret = PTR_ERR(path);
+			goto out;
+		}
+
 		perms = branchperms(sb, bindex);
 
 		seq_printf(m, "%s=%s", path,
Signature · 1 line
1.5.0.2.260.g2eb065
#9 lore raw

PATCH 9/13 fs/unionfs/: Miscellaneous coding style fixes

In reply to Josef 'Jeff' Sipek
---
 fs/unionfs/super.c |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)
fs/unionfs/super.c +1 -2
index 176cfb6..571b589 100644
@@ -320,9 +320,8 @@ static int unionfs_show_options(struct seq_file *m, struct vfsmount *mnt)
 
 		seq_printf(m, "%s=%s", path,
 			   perms & MAY_WRITE ? "rw" : "ro");
-		if (bindex != bend) {
+		if (bindex != bend)
 			seq_printf(m, ":");
-		}
 	}
 
 out:
Signature · 1 line
1.5.0.2.260.g2eb065
#10 lore raw

PATCH 10/13 fs/unionfs/: Fix copyup_deleted_file dentry leak

In reply to Josef 'Jeff' Sipek

From: Erez Zadok <[email protected]>

---
 fs/unionfs/commonfops.c |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
fs/unionfs/commonfops.c +11 -0
index 2664be9..aa7c75d 100644
@@ -41,6 +41,15 @@ static int copyup_deleted_file(struct file *file, struct dentry *dentry,
 	sprintf(name, ".unionfs%*.*lx",
 			i_inosize, i_inosize, hidden_dentry->d_inode->i_ino);
 
+	/*
+	 * Loop, looking for an unused temp name to copyup to.
+	 *
+	 * It's somewhat silly that we look for a free temp tmp name in the
+	 * source branch (bstart) instead of the dest branch (bindex), where
+	 * the final name will be created.  We _will_ catch it if somehow
+	 * the name exists in the dest branch, but it'd be nice to catch it
+	 * sooner than later.
+	 */
 	tmp_dentry = NULL;
 	do {
 		char *suffix = name + nlen - countersize;
@@ -58,7 +67,9 @@ static int copyup_deleted_file(struct file *file, struct dentry *dentry,
 			err = PTR_ERR(tmp_dentry);
 			goto out;
 		}
+		/* don't dput here because of do-while condition eval order */
 	} while (tmp_dentry->d_inode != NULL);	/* need negative dentry */
+	dput(tmp_dentry);
 
 	err = copyup_named_file(dentry->d_parent->d_inode, file, name, bstart,
 				bindex, file->f_dentry->d_inode->i_size);
Signature · 1 line
1.5.0.2.260.g2eb065
#11 lore raw

PATCH 11/13 fs/unionfs/: mntput in __cleanup_dentry

In reply to Josef 'Jeff' Sipek

From: Erez Zadok <[email protected]>

This fixes a mnt refleak which occured during copyup when directory hierarchy was recreated on a writable branch.

---
 fs/unionfs/copyup.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
fs/unionfs/copyup.c +3 -0
index 998cc69..e0075ca 100644
@@ -548,6 +548,9 @@ static void __cleanup_dentry(struct dentry * dentry, int bindex,
 		if (!unionfs_lower_dentry_idx(dentry, i)->d_inode) {
 			dput(unionfs_lower_dentry_idx(dentry, i));
 			unionfs_set_lower_dentry_idx(dentry, i, NULL);
+
+			mntput(unionfs_lower_mnt_idx(dentry, i));
+			unionfs_set_lower_mnt_idx(dentry, i, NULL);
 		} else {
 			if (new_bstart < 0)
 				new_bstart = i;
Signature · 1 line
1.5.0.2.260.g2eb065
#12 lore raw

PATCH 12/13 fs/unionfs/: Fix dentry leak in copyup_named_dentry

In reply to Josef 'Jeff' Sipek

From: Erez Zadok <[email protected]>

When we chmod a directory on a readonly branch, and have to copy it up, we forget to dput(). If this was a file, it gets dput indirectly through other functions we call, but not if it was a directory.

---
 fs/unionfs/copyup.c |   14 ++++++++++----
 1 files changed, 10 insertions(+), 4 deletions(-)
fs/unionfs/copyup.c +10 -4
index e0075ca..e24d940 100644
@@ -352,20 +352,18 @@ static int copyup_named_dentry(struct inode *dir, struct dentry *dentry,
 
 	unionfs_read_lock(sb);
 
-	if ((err = is_robranch_super(sb, new_bindex))) {
-		dput(old_hidden_dentry);
+	if ((err = is_robranch_super(sb, new_bindex)))
 		goto out;
-	}
 
 	/* Create the directory structure above this dentry. */
 	new_hidden_dentry = create_parents_named(dir, dentry, name, new_bindex);
 	if (IS_ERR(new_hidden_dentry)) {
-		dput(old_hidden_dentry);
 		err = PTR_ERR(new_hidden_dentry);
 		goto out;
 	}
 
 	old_hidden_dentry = unionfs_lower_dentry_idx(dentry, old_bindex);
+	/* we conditionally dput this old_hidden_dentry at end of function */
 	dget(old_hidden_dentry);
 
 	/* For symlinks, we must read the link before we lock the directory. */
@@ -460,6 +458,14 @@ out_unlock:
 	unlock_dir(new_hidden_parent_dentry);
 
 out_free:
+	/*
+	 * If old_hidden_dentry was a directory, we need to dput it.  If it
+	 * was a file, then it was already dput indirectly by other
+	 * functions we call ablve which operate on regular files.
+	 */
+	if (old_hidden_dentry && old_hidden_dentry->d_inode &&
+	    S_ISDIR(old_hidden_dentry->d_inode->i_mode))
+		dput(old_hidden_dentry);
 	kfree(symbuf);
 
 out:
Signature · 1 line
1.5.0.2.260.g2eb065
#13 lore raw

PATCH 13/13 fs/unionfs/: Fix unlocking in error paths

In reply to Josef 'Jeff' Sipek

From: Erez Zadok <[email protected]>

---
 fs/unionfs/lookup.c |   11 +++++++++--
 1 files changed, 9 insertions(+), 2 deletions(-)
fs/unionfs/lookup.c +9 -2
index df929e9..967bb5b 100644
@@ -84,6 +84,7 @@ struct dentry *unionfs_lookup_backend(struct dentry *dentry, struct nameidata *n
 	struct vfsmount *first_hidden_mnt = NULL;
 	int locked_parent = 0;
 	int locked_child = 0;
+	int allocated_new_info = 0;
 
 	int opaque;
 	char *whname = NULL;
@@ -101,9 +102,11 @@ struct dentry *unionfs_lookup_backend(struct dentry *dentry, struct nameidata *n
 		BUG_ON(UNIONFS_D(dentry) != NULL);
 		locked_child = 1;
 	}
-	if (lookupmode != INTERPOSE_PARTIAL)
+	if (lookupmode != INTERPOSE_PARTIAL) {
 		if ((err = new_dentry_private_data(dentry)))
 			goto out;
+		allocated_new_info = 1;
+	}
 	/* must initialize dentry operations */
 	dentry->d_op = &unionfs_dops;
 
@@ -380,7 +383,7 @@ out:
 	if (locked_parent)
 		unionfs_unlock_dentry(parent_dentry);
 	dput(parent_dentry);
-	if (locked_child)
+	if (locked_child || (err && allocated_new_info))
 		unionfs_unlock_dentry(dentry);
 	return ERR_PTR(err);
 }
@@ -431,6 +434,7 @@ int new_dentry_private_data(struct dentry *dentry)
 	int newsize;
 	int oldsize = 0;
 	struct unionfs_dentry_info *info = UNIONFS_D(dentry);
+	int unlock_on_err = 0;
 
 	spin_lock(&dentry->d_lock);
 	if (!info) {
@@ -443,6 +447,7 @@ int new_dentry_private_data(struct dentry *dentry)
 
 		mutex_init(&info->lock);
 		mutex_lock(&info->lock);
+		unlock_on_err = 1;
 
 		info->lower_paths = NULL;
 	} else
@@ -482,6 +487,8 @@ int new_dentry_private_data(struct dentry *dentry)
 
 out_free:
 	kfree(info->lower_paths);
+	if (unlock_on_err)
+		mutex_unlock(&info->lock);
 
 out:
 	free_dentry_private_data(info);
Signature · 1 line
1.5.0.2.260.g2eb065