I’m reading the Understand the Linux Kernel book, and it says that the list of file_lock is stored in the file’s inode (of field i_flock).
But in the sys_flock() of Linux 2.6.11.12, which will eventually call flock_lock_file(). It uses filp->f_dentry->d_inode->i_flock to get the list of file_lock and filp->f_dentry is an dentry of the directory which “contains” the file.
int flock_lock_file(struct file *filp, struct file_lock *new_fl) {
// ...
struct inode * inode = filp->f_dentry->d_inode;
// ...
}
Suppose that the file_lock list are linked with filp->f_dentry->d_inode->i_flock, What will happen when a hard link exists:
/some_path/foo/file.txt /another_path/bar/file_link
and file_link is a hard link to file.txt
When we use this two path to open the same file, sys_open() will set filp->f_dentry to foo and bar separately, isn’t it? If my guess is right, how file_lock can work?
Advertisement
Answer
The file_lock is indeed stored in the inode of the corresponding file.
An inode can be referred by several directory entries, which linked in inode’s i_dentry field. Even for a unique file might have different filp->f_dentry, filp->f_dentry->d_inode is all refer to the same inode.