linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: hooanon05@yahoo.co.jp
To: dedekind@infradead.org, ext-adrian.hunter@nokia.com
Cc: linux-fsdevel@vger.kernel.org
Subject: ubifs, race between link and unlink/rename?
Date: Thu, 14 May 2009 01:31:48 +0900	[thread overview]
Message-ID: <9917.1242232308@jrobl> (raw)


Hello,

Is there a race condition in ubifs?
Here is a scenario.

Process A		Process B
----------------------+---------------------------
create("dirA/fileA"); |
unlink("dirA/fileA"); |	link("dirA/fileA", "dirB/fileB");
		      | unlink("dirB/fileB");
----------------------+---------------------------

In link(2), dirA->i_mutex is not held. So unlink("dirA/fileA") can run
concurrently (after lookup).
While ubifs acquires ubifs_inode->ui_mutex, it doesn't check i_nlink.
When unlink("dirA/fileA") wins ui_mutex race and link() loses,
- ubifs_unlink("dirA/fileA") will call ubifs_jnl_update/ubifs_add_orphan()
- ubifs_link() will operate the inode with i_nlink == 0 (finally it will
  be 1)
- ubifs_unlink("dirB/fileB") will call ubifs_add_orphan() again for the
  same inode
- and it will produce "orphaned twice" error.
- (ubifs_add_orphan() is called by ubifs_rename/ubifs_jnl_rename() too)

If this scenario is possible, it may happen in every FS.
To check i_nlink in vfs_link (like this) may be one option, but it might
be better to check in ubifs since ubifs_add_orphan() is for i_nlink == 0
and ubifs specific.

diff --git a/fs/namei.c b/fs/namei.c
index b207821..820c386 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -2409,9 +2409,12 @@ int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_de
 	if (error)
 		return error;
 
+	error = -ENOENT;
 	mutex_lock(&inode->i_mutex);
-	DQUOT_INIT(dir);
-	error = dir->i_op->link(old_dentry, dir, new_dentry);
+	if (inode->i_nlink) {
+		DQUOT_INIT(dir);
+		error = dir->i_op->link(old_dentry, dir, new_dentry);
+	}
 	mutex_unlock(&inode->i_mutex);
 	if (!error)
 		fsnotify_link(dir, inode, new_dentry);


J. R. Okajima

             reply	other threads:[~2009-05-13 16:32 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-13 16:31 hooanon05 [this message]
2009-05-13 18:28 ` ubifs, race between link and unlink/rename? Adrian Hunter
2009-05-15  8:44 ` Artem Bityutskiy
2009-05-15  9:35 ` Artem Bityutskiy
2009-05-19  6:09   ` Artem Bityutskiy
2009-05-19  6:18     ` hooanon05

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9917.1242232308@jrobl \
    --to=hooanon05@yahoo.co.jp \
    --cc=dedekind@infradead.org \
    --cc=ext-adrian.hunter@nokia.com \
    --cc=linux-fsdevel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).