内核 dentry 中的 qstr 结构是否包含 Linux 文件的文件名?

Does the qstr struct in a kernel dentry hold the filename of a Linux file?

下面是来自 http://lxr.free-electrons.com/source/include/linux/dcache.h#L150 的 Linux dentry 结构的片段。该结构包含一个成员 struct qstr d_name - 定义如下。我想知道这是否是在运行时对应于该 dentry 的特定文件的名称。令我困惑的是 proc/PID/maps 使用 struct dentry_operations -> d_name (dentry 的另一个成员)来生成文件名...所以 waht 是 struct qstr [=18 的目的=]?请注意,我是从纯内存自省的角度 (libvmi) 来处理这个问题的,因此我将 "walking memory" 用于这些结构,并且使用 C/C++ 代码进行检索并不是那么简单。

 struct dentry {

     /* RCU lookup touched fields */
     unsigned int d_flags;           /* protected by d_lock */
     seqcount_t d_seq;               /* per dentry seqlock */
     struct hlist_bl_node d_hash;    /* lookup hash list */
     struct dentry *d_parent;        /* parent directory */
     struct qstr d_name;

              ....

struct qstr {
     union {
             struct {
                      HASH_LEN_DECLARE;
             };
              u64 hash_len;
      };
     const unsigned char *name;
};

Dentry 的字段 d_name 是维护 dentry 命名的一种简单(且最常用)的方法。在这种情况下,文件系统驱动程序需要指定一次 dentry 的名称,所有其他工作将由 VFS 完成。

但在某些情况下,静态分配的名称对于文件系统来说是不够的。正如您所注意到的,这是 proc 文件系统的情况,它向用户 space 展示每个进程的信息。对于这种情况,存在方法 ->d_op->d_dname

d_path 方法的实现可能有助于理解 d_named_op->d_dname:

...
if (path->dentry->d_op && path->dentry->d_op->d_dname &&
    (!IS_ROOT(path->dentry) || path->dentry != path->mnt->mnt_root))
        return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
rcu_read_lock();
get_fs_root_rcu(current->fs, &root);
error = path_with_deleted(path, &root, &res, &buflen);
rcu_read_unlock();
...

如您所见,d_op->d_dname 方法的字段首先被检查。如果它不为 NULL,则使用该方法。否则 d_name 字段被读取。