Skip to content

Commit 705304a

Browse files
konistorvalds
authored andcommitted
nilfs2: fix the nilfs_iget() vs. nilfs_new_inode() races
Same story as in commit 41080b5 ("nfsd race fixes: ext2") (similar ext2 fix) except that nilfs2 needs to use insert_inode_locked4() instead of insert_inode_locked() and a bug of a check for dead inodes needs to be fixed. If nilfs_iget() is called from nfsd after nilfs_new_inode() calls insert_inode_locked4(), nilfs_iget() will wait for unlock_new_inode() at the end of nilfs_mkdir()/nilfs_create()/etc to unlock the inode. If nilfs_iget() is called before nilfs_new_inode() calls insert_inode_locked4(), it will create an in-core inode and read its data from the on-disk inode. But, nilfs_iget() will find i_nlink equals zero and fail at nilfs_read_inode_common(), which will lead it to call iget_failed() and cleanly fail. However, this sanity check doesn't work as expected for reused on-disk inodes because they leave a non-zero value in i_mode field and it hinders the test of i_nlink. This patch also fixes the issue by removing the test on i_mode that nilfs2 doesn't need. Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp> Cc: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 72b9918 commit 705304a

2 files changed

Lines changed: 36 additions & 11 deletions

File tree

‎fs/nilfs2/inode.c‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ struct nilfs_iget_args {
4949
int for_gc;
5050
};
5151

52+
static int nilfs_iget_test(struct inode *inode, void *opaque);
53+
5254
void nilfs_inode_add_blocks(struct inode *inode, int n)
5355
{
5456
struct nilfs_root *root = NILFS_I(inode)->i_root;
@@ -348,6 +350,17 @@ const struct address_space_operations nilfs_aops = {
348350
.is_partially_uptodate = block_is_partially_uptodate,
349351
};
350352

353+
static int nilfs_insert_inode_locked(struct inode *inode,
354+
struct nilfs_root *root,
355+
unsigned long ino)
356+
{
357+
struct nilfs_iget_args args = {
358+
.ino = ino, .root = root, .cno = 0, .for_gc = 0
359+
};
360+
361+
return insert_inode_locked4(inode, ino, nilfs_iget_test, &args);
362+
}
363+
351364
struct inode *nilfs_new_inode(struct inode *dir, umode_t mode)
352365
{
353366
struct super_block *sb = dir->i_sb;
@@ -383,7 +396,7 @@ struct inode *nilfs_new_inode(struct inode *dir, umode_t mode)
383396
if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) {
384397
err = nilfs_bmap_read(ii->i_bmap, NULL);
385398
if (err < 0)
386-
goto failed_bmap;
399+
goto failed_after_creation;
387400

388401
set_bit(NILFS_I_BMAP, &ii->i_state);
389402
/* No lock is needed; iget() ensures it. */
@@ -399,21 +412,24 @@ struct inode *nilfs_new_inode(struct inode *dir, umode_t mode)
399412
spin_lock(&nilfs->ns_next_gen_lock);
400413
inode->i_generation = nilfs->ns_next_generation++;
401414
spin_unlock(&nilfs->ns_next_gen_lock);
402-
insert_inode_hash(inode);
415+
if (nilfs_insert_inode_locked(inode, root, ino) < 0) {
416+
err = -EIO;
417+
goto failed_after_creation;
418+
}
403419

404420
err = nilfs_init_acl(inode, dir);
405421
if (unlikely(err))
406-
goto failed_acl; /* never occur. When supporting
422+
goto failed_after_creation; /* never occur. When supporting
407423
nilfs_init_acl(), proper cancellation of
408424
above jobs should be considered */
409425

410426
return inode;
411427

412-
failed_acl:
413-
failed_bmap:
428+
failed_after_creation:
414429
clear_nlink(inode);
430+
unlock_new_inode(inode);
415431
iput(inode); /* raw_inode will be deleted through
416-
generic_delete_inode() */
432+
nilfs_evict_inode() */
417433
goto failed;
418434

419435
failed_ifile_create_inode:
@@ -461,8 +477,8 @@ int nilfs_read_inode_common(struct inode *inode,
461477
inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
462478
inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec);
463479
inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec);
464-
if (inode->i_nlink == 0 && inode->i_mode == 0)
465-
return -EINVAL; /* this inode is deleted */
480+
if (inode->i_nlink == 0)
481+
return -ESTALE; /* this inode is deleted */
466482

467483
inode->i_blocks = le64_to_cpu(raw_inode->i_blocks);
468484
ii->i_flags = le32_to_cpu(raw_inode->i_flags);

‎fs/nilfs2/namei.c‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ static inline int nilfs_add_nondir(struct dentry *dentry, struct inode *inode)
5151
int err = nilfs_add_link(dentry, inode);
5252
if (!err) {
5353
d_instantiate(dentry, inode);
54+
unlock_new_inode(inode);
5455
return 0;
5556
}
5657
inode_dec_link_count(inode);
58+
unlock_new_inode(inode);
5759
iput(inode);
5860
return err;
5961
}
@@ -182,6 +184,7 @@ static int nilfs_symlink(struct inode *dir, struct dentry *dentry,
182184
out_fail:
183185
drop_nlink(inode);
184186
nilfs_mark_inode_dirty(inode);
187+
unlock_new_inode(inode);
185188
iput(inode);
186189
goto out;
187190
}
@@ -201,11 +204,15 @@ static int nilfs_link(struct dentry *old_dentry, struct inode *dir,
201204
inode_inc_link_count(inode);
202205
ihold(inode);
203206

204-
err = nilfs_add_nondir(dentry, inode);
205-
if (!err)
207+
err = nilfs_add_link(dentry, inode);
208+
if (!err) {
209+
d_instantiate(dentry, inode);
206210
err = nilfs_transaction_commit(dir->i_sb);
207-
else
211+
} else {
212+
inode_dec_link_count(inode);
213+
iput(inode);
208214
nilfs_transaction_abort(dir->i_sb);
215+
}
209216

210217
return err;
211218
}
@@ -243,6 +250,7 @@ static int nilfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
243250

244251
nilfs_mark_inode_dirty(inode);
245252
d_instantiate(dentry, inode);
253+
unlock_new_inode(inode);
246254
out:
247255
if (!err)
248256
err = nilfs_transaction_commit(dir->i_sb);
@@ -255,6 +263,7 @@ static int nilfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
255263
drop_nlink(inode);
256264
drop_nlink(inode);
257265
nilfs_mark_inode_dirty(inode);
266+
unlock_new_inode(inode);
258267
iput(inode);
259268
out_dir:
260269
drop_nlink(dir);

0 commit comments

Comments
 (0)