为什么Linux启动时会为根目录“/”初始化一些dentries

时间:2012-03-12 02:39:04

标签: linux filesystems linux-kernel vfs

我正在玩Linux内核代码,尤其是文件系统部分。我发现当内核启动时,会为根目录“/”分配一些dentry对象。为什么需要在RAM中分配多个根目录副本?此外,因为看起来dcache(dentry缓存,本质上是一个大的哈希表)使用哈希函数H(parent_dentry_address,name_hash)来计算一个dentry阻止的桶。这是否意味着每个root dentry“/”生成一个不同的dentry映射到dcache中的哈希桶?

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

2 个答案:

答案 0 :(得分:1)

从内核源代码中读取Documentation / initrd.txt,看看在bootstrapping上发生了什么:

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的问题

答案 1 :(得分:0)

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

当文件系统注册并挂载时,它将首先为mount root分配一个dentry作为此文件系统的条目,通常这个dentry使用' /' name.RAM fs就像proc / devtmpfs ...挂载内核,所以会有几个dentry具有相同的名称' /'。