Skip to content

Commit d59729f

Browse files
committed
ext4: fix races in ext4_sync_parent()
Fix problems if fsync() races against a rename of a parent directory as pointed out by Al Viro in his own inimitable way: >While we are at it, could somebody please explain what the hell is ext4 >doing in >static int ext4_sync_parent(struct inode *inode) >{ > struct writeback_control wbc; > struct dentry *dentry = NULL; > int ret = 0; > > while (inode && ext4_test_inode_state(inode, EXT4_STATE_NEWENTRY)) { > ext4_clear_inode_state(inode, EXT4_STATE_NEWENTRY); > dentry = list_entry(inode->i_dentry.next, > struct dentry, d_alias); > if (!dentry || !dentry->d_parent || !dentry->d_parent->d_inode) > break; > inode = dentry->d_parent->d_inode; > ret = sync_mapping_buffers(inode->i_mapping); > ... >Note that dentry obviously can't be NULL there. dentry->d_parent is never >NULL. And dentry->d_parent would better not be negative, for crying out >loud! What's worse, there's no guarantees that dentry->d_parent will >remain our parent over that sync_mapping_buffers() *and* that inode won't >just be freed under us (after rename() and memory pressure leading to >eviction of what used to be our dentry->d_parent)...... Reported-by: Al Viro <viro@ZenIV.linux.org.uk> Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
1 parent 29ae07b commit d59729f

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

‎fs/ext4/fsync.c‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,30 @@ static int ext4_sync_parent(struct inode *inode)
129129
{
130130
struct writeback_control wbc;
131131
struct dentry *dentry = NULL;
132+
struct inode *next;
132133
int ret = 0;
133134

134-
while (inode && ext4_test_inode_state(inode, EXT4_STATE_NEWENTRY)) {
135+
if (!ext4_test_inode_state(inode, EXT4_STATE_NEWENTRY))
136+
return 0;
137+
inode = igrab(inode);
138+
while (ext4_test_inode_state(inode, EXT4_STATE_NEWENTRY)) {
135139
ext4_clear_inode_state(inode, EXT4_STATE_NEWENTRY);
136-
dentry = list_entry(inode->i_dentry.next,
137-
struct dentry, d_alias);
138-
if (!dentry || !dentry->d_parent || !dentry->d_parent->d_inode)
140+
dentry = NULL;
141+
spin_lock(&inode->i_lock);
142+
if (!list_empty(&inode->i_dentry)) {
143+
dentry = list_first_entry(&inode->i_dentry,
144+
struct dentry, d_alias);
145+
dget(dentry);
146+
}
147+
spin_unlock(&inode->i_lock);
148+
if (!dentry)
149+
break;
150+
next = igrab(dentry->d_parent->d_inode);
151+
dput(dentry);
152+
if (!next)
139153
break;
140-
inode = dentry->d_parent->d_inode;
154+
iput(inode);
155+
inode = next;
141156
ret = sync_mapping_buffers(inode->i_mapping);
142157
if (ret)
143158
break;
@@ -148,6 +163,7 @@ static int ext4_sync_parent(struct inode *inode)
148163
if (ret)
149164
break;
150165
}
166+
iput(inode);
151167
return ret;
152168
}
153169

0 commit comments

Comments
 (0)