Skip to contentSkip to conversation

query re unlink() ... inotify ... open() race

DISCUSSION Pádraig Brady 2 participants 2 messages Sep 12, 2015

lore
Share & tools
lore
#0 lore raw

Hi,

We're noticing a rare race here with open() in tail(1), where this happens:

tail --follow=name "file"
/* "file" is unlinked() by another process */ read(IN_ATTRIB from inotify); /* for st_nlink-- */ open("file") /* Done to check if deleted, but this succeeds! */

The open() succeeding is surprising. Is that allowed? The summary of the sequence in the kernel is:

  vfs_unlink() {
    mutex_lock(&(dentry->d_inode->i_mutex));
    security_inode_unlink(dir, dentry);
    try_break_deleg(target, delegated_inode);
    dir->i_op->unlink(dir, dentry);
    dont_mount(dentry);
    detach_mounts(dentry);
    mutex_unlock(&(dentry->d_inode->i_mutex));
    fsnotify_link_count(target)
    d_delete(dentry);

}

thanks,
Pádraig.

#1 lore raw
Quoted 12 lines · Pádraig Brady

The open() succeeding is surprising. Is that allowed? The summary of the sequence in the kernel is:

  vfs_unlink() {
    mutex_lock(&(dentry->d_inode->i_mutex));
    security_inode_unlink(dir, dentry);
    try_break_deleg(target, delegated_inode);
    dir->i_op->unlink(dir, dentry);
    dont_mount(dentry);
    detach_mounts(dentry);
    mutex_unlock(&(dentry->d_inode->i_mutex));
Quoted 3 lines
    fsnotify_link_count(target)
    d_delete(dentry);

}

... and ->unlink() is the one to decrement the link count. With lookup via dcache coming at some point after that. idiotify is a red herring here - stat(2) will report decremented link count just as well.