为什么挂载结构有两个挂载点字段?

Why does mount structure has two mountpoint fields?

我正在尝试了解 linux 文件系统的工作原理。 struct mountmnt_mountpoint 字段,指向挂载点目录并用于路径查找。

struct mount {
    /*...*/
    struct dentry *mnt_mountpoint;
    /*...*/
    struct mountpoint *mnt_mp;
    /*...*/
} 

此时我不明白第二个挂载点字段的用途是什么?它指向 mountpoint 结构,该结构又指向另一个挂载点目录 m_dentry.

struct mountpoint {
    struct hlist_node m_hash;
    struct dentry *m_dentry;
    struct hlist_head m_list;
    int m_count;
};

We also have struct mountpoint in the picture. Once upon a time it used to be a part of struct dentry - the list of all mounts on given mountpoint. Since it doesn't make sense to bloat every dentry for the sake of a very small fraction that will ever be anyone's mountpoints, that thing got separated.

What we have is

  • mark in dentry flags (DCACHE_MOUNTED) set for dentries that are currently mountpoints
  • for each of those we have a struct mountpoint instance (exactly one for each of those dentries).

https://lwn.net/Articles/793073/

不是struct dentry有一个列表指针,所有的挂载点都存储在一个散列table中。 m_dentry字段用于区分落入同一哈希桶的不同挂载点。

我猜 struct mount 持有对 struct mountpoint 的引用主要是为了清理。 IE。当 struct mount 被销毁时,我们调用 put_mountpoint(mnt_mp)m_count 递减,如果减到零则挂载点被销毁。