aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ubifs/dir.c
diff options
context:
space:
mode:
authorHunter Adrian <adrian.hunter@nokia.com>2009-05-14 00:32:30 -0400
committerArtem Bityutskiy <Artem.Bityutskiy@nokia.com>2009-05-19 04:01:31 -0400
commit8b3884a841f398f6e0a0411d6929d8d9381bb265 (patch)
tree01bbbaad779c43b14ea3e809c5fc9271eca257d4 /fs/ubifs/dir.c
parent6d6cb0d688d0f262cb4fd5771648b0ac01d4f82c (diff)
UBIFS: return error if link and unlink race
Consider a scenario when 'vfs_link(dirA/fileA)' and 'vfs_unlink(dirA/fileA, dirB/fileB)' race. 'vfs_link()' does not lock 'dirA->i_mutex', so this is possible. Both of the functions lock 'fileA->i_mutex' though. Suppose 'vfs_unlink()' wins, and takes 'fileA->i_mutex' mutex first. Suppose 'fileA->i_nlink' is 1. In this case 'ubifs_unlink()' will drop the last reference, and put 'inodeA' to the list of orphans. After this, 'vfs_link()' will link 'dirB/fileB' to 'inodeA'. Thir is a problem because, for example, the subsequent 'vfs_unlink(dirB/fileB)' will add the same inode to the list of orphans. This problem was reported by J. R. Okajima <hooanon05@yahoo.co.jp> [Artem: add more comments, amended commit message] Signed-off-by: Adrian Hunter <adrian.hunter@nokia.com> Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
Diffstat (limited to 'fs/ubifs/dir.c')
-rw-r--r--fs/ubifs/dir.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/fs/ubifs/dir.c b/fs/ubifs/dir.c
index f55d523c52bb..552fb0111fff 100644
--- a/fs/ubifs/dir.c
+++ b/fs/ubifs/dir.c
@@ -528,6 +528,25 @@ static int ubifs_link(struct dentry *old_dentry, struct inode *dir,
528 inode->i_nlink, dir->i_ino); 528 inode->i_nlink, dir->i_ino);
529 ubifs_assert(mutex_is_locked(&dir->i_mutex)); 529 ubifs_assert(mutex_is_locked(&dir->i_mutex));
530 ubifs_assert(mutex_is_locked(&inode->i_mutex)); 530 ubifs_assert(mutex_is_locked(&inode->i_mutex));
531
532 /*
533 * Return -ENOENT if we've raced with unlink and i_nlink is 0. Doing
534 * otherwise has the potential to corrupt the orphan inode list.
535 *
536 * Indeed, consider a scenario when 'vfs_link(dirA/fileA)' and
537 * 'vfs_unlink(dirA/fileA, dirB/fileB)' race. 'vfs_link()' does not
538 * lock 'dirA->i_mutex', so this is possible. Both of the functions
539 * lock 'fileA->i_mutex' though. Suppose 'vfs_unlink()' wins, and takes
540 * 'fileA->i_mutex' mutex first. Suppose 'fileA->i_nlink' is 1. In this
541 * case 'ubifs_unlink()' will drop the last reference, and put 'inodeA'
542 * to the list of orphans. After this, 'vfs_link()' will link
543 * 'dirB/fileB' to 'inodeA'. This is a problem because, for example,
544 * the subsequent 'vfs_unlink(dirB/fileB)' will add the same inode
545 * to the list of orphans.
546 */
547 if (inode->i_nlink == 0)
548 return -ENOENT;
549
531 err = dbg_check_synced_i_size(inode); 550 err = dbg_check_synced_i_size(inode);
532 if (err) 551 if (err)
533 return err; 552 return err;