In Linux kernel how to identify the opened file when we can get "struct file"
In linux kernel how to identify the opened files? When we can get the information about inodes or (struct file) or dentry? Which structure can help us to identify the opened files which is the same opened file? Can y开发者_运维百科ou tell me details?
One physical file has only one struct inode
at any one time.
On most filesystems the inode number will stay the same even when the struct inode
is deallocated and read back later, so you can identify by that (plus device number), but there are exceptions. But it is always ensured at least that only one file on a device will have a particular inode number at any given time (this is used to check two open filehandles point to the same file in userland where you don't have access to struct inode
—in kernel comparing the pointers will be easier).
One file may have more struct file
associated with it, one for each file handle in some process and it may have more than one struct dentry
associated with it, one per hardlink.
It comes from this that there is only one struct dentry
for any path in the filesystem. The path may however look differently from different process point of view (think chroots and namespaces). It can be reconstructed by walking up the chain of dentries and inodes (each dentry has parent inode and each inode knows dentries currently associated with it), but you have to be careful to avoid the dentries not visible to current process.
精彩评论