【问题标题】:Why does Linux boot with a few dentries initialized for root directory "/"为什么 Linux 启动时会为根目录“/”初始化一些目录
【发布时间】:2012-03-12 02:39:04
【问题描述】:

我正在研究 Linux 内核代码,尤其是文件系统部分。我发现当内核启动时,为根目录“/”分配了一些 dentry 对象。为什么需要在 RAM 中分配根目录的多个副本?而且,既然dcache(dentry缓存,本质上是一个大的hash表)似乎是在用一个hash函数H(parent_dentry_address, name_hash)来计算一个dentry抵抗的bucket。是不是意味着每个根dentry“/”都做了一个dcache中hash桶的不同dentry映射?

顺便说一句,上述行为是在 Linux-3.3.0-rc4 上观察到的。

【问题讨论】:

  • 如果您链接到您正在谈论的代码,这将有所帮助,使用例如lxr.linux.no
  • 我还没有找到导致这个问题的原因。我只在 __d_alloc() 中添加了几个 printk 来打印出 dentry->d_name.name 和 dentry 的指针地址。它表明,当 Linux 启动时,分配了几个具有不同指针的“/”目录。

标签: linux filesystems linux-kernel vfs


【解决方案1】:

我将闭上眼睛,不看任何代码,然后脱口而出,这可能是/ 超过/ 的结果,而且不止一次?

如果您在/ 上挂载某些内容,则底层/ 不能消失,因为它可以通过umount 暴露。

【讨论】:

  • 这很可能是 OP 看到的原因。
【解决方案2】:

从内核源代码中阅读 Documentation/initrd.txt 以了解引导过程中发生了什么:

When using initrd, the system typically boots as follows:

  1) the boot loader loads the kernel and the initial RAM disk
  2) the kernel converts initrd into a "normal" RAM disk and
     frees the memory used by initrd
  3) if the root device is not /dev/ram0, the old (deprecated)
     change_root procedure is followed. see the "Obsolete root change
     mechanism" section below.
  4) root device is mounted. if it is /dev/ram0, the initrd image is
     then mounted as root
  5) /sbin/init is executed (this can be any valid executable, including
     shell scripts; it is run with uid 0 and can do basically everything
     init can do).
  6) init mounts the "real" root file system
  7) init places the root file system at the root directory using the
     pivot_root system call
  8) init execs the /sbin/init on the new root filesystem, performing
     the usual boot sequence
  9) the initrd file system is removed

Note that changing the root directory does not involve unmounting it.
It is therefore possible to leave processes running on initrd during that
procedure. Also note that file systems mounted under initrd continue to
be accessible.

我希望这能回答内核为什么要为“/”分配一些dentries的问题

【讨论】:

    【解决方案3】:

    内核中有两种类型'/',一种是进程根目录,另一种是文件系统根目录。

    文件系统注册挂载时,会先为mount root分配一个dentry作为这个文件系统的入口,一般这个dentry使用'/'名。RAM fs如proc/devtmpfs...挂载内核,所以会有几个同名的dentry '/'。

    【讨论】:

      猜你喜欢
      • 2021-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-02
      • 2017-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多