inode缓存的生命周期

本文详细介绍了Linux文件系统中inode的管理机制,包括inode的状态定义、相关操作函数、以及inode缓存的释放流程等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

inode state

/* Inode state bits.. */
#define I_DIRTY_SYNC          1 /* Not dirty enough for O_DATASYNC */
#define I_DIRTY_DATASYNC     2 /* Data-related inode changes pending */
#define I_DIRTY_PAGES          4 /* Data-related inode changes pending */
#define I_LOCK               8     涉及到的索引节点处于I/O传输中
#define I_FREEING          16     索引节点对象正在被释放
#define I_CLEAR               32     索引节点对象内容不再有意义

          I_NEW                              索引节点对象已经分配,但还没有用从磁盘索引节点读取的数据来填充

inode_unused

inode_in_use

s_dirty(sb)

 

s_inodes(sb)

 

inode_hashtabel

 

inode相关操作

destroy_inode 释放inode内存对象

put_inode 减少索引节点引用计数时调用
drop_inode
delete_inode 删除索引节点在硬盘上的数据(包括修改所在块组的位图)
clear_inode 重置inode内存对象中的数据,重置状态,解除和buffer_head的关联等等。
 
 
iput-->put_inode
     -->(drop_inode 2.4内核没找到)
     -->delete_inode | clear_inode
     -->destroy_inode
 
 
 
ext2_delete_inode-->ext2_truncate,在这里释放文件的所有数据块
 
dget_locked
__dget_locked 
dget 增加dentry使用数
dput 减少dentry使用数
 
iget 增加inode中的使用数(usecount)
iput 减少inode中的使用数(usecount),当usecount为0且link数为0时可以释放内存
 
pathwalk的过程中,dget和dput是成对调用的,最后返回的结果dentry只调用了dget,由使用此dentry的函数,在使用完成后调用dput。
 
open时也要获取dentry,这个dentry调用dget把使用数增加,在close时调用dput(fput->dput)减少使用数。
 
unlink时,调用op->unlink和d_delete,最后调用dput,op_unlink是各文件系统自己实现,主要是减少node中link数,并修改硬盘中名字空间和文件记录。d_delete判断dentry的使用数是否为1,如果为1,减少dentry中inode记录的使用数(负状态?),如果大于1,说明有人还在使用此dentry, 重置d_hash,让其他人再无法查询到该dentry。调用dput,如果没有人在使用,并且不能通过d_hash查到该dentry时,释放该dentry,如果还可以通过d_hash查到该dentry,把dentry加入freelist中(未使用状态?)
 
疑问:为什么不在d_delete函数中直接重置d_hash,这样在dput时就可以释放dentry了?
 
/**
* d_invalidate - invalidate a dentry
* @dentry: dentry to invalidate
*
* Try to invalidate the dentry if it turns out to be
* possible. If there are other dentries that can be
* reached through this one we can't delete it and we
* return -EBUSY. On success we return 0.
*
* no dcache lock.
*/

int d_invalidate(struct dentry * dentry)
 
 
/*
* This is dput
*
* This is complicated by the fact that we do not want to put
* dentries that are no longer on any hash chain on the unused
* list: we'd much rather just get rid of them immediately.
*
* However, that implies that we have to traverse the dentry
* tree upwards to the parents which might _also_ now be
* scheduled for deletion (it may have been only waiting for
* its last child to go away).
*
* This tail recursion is done by hand as we don't want to depend
* on the compiler to always get this right (gcc generally doesn't).
* Real recursion would eat up our stack space.
*/

/*
* dput - release a dentry
* @dentry: dentry to release
*
* Release a dentry. This will drop the usage count and if appropriate
* call the dentry unlink method as well as removing it from the queues and
* releasing its resources. If the parent dentries were scheduled for release
* they too may now get deleted.
*
* no dcache lock, please.
*/

void dput(struct dentry *dentry)
 
空闲状态
 
未使用状态
 
正在使用状态
 
负状态
 

inode缓存的释放

__invalidate_device-->invalidate_inodes中会将不在使用的inode释放

shrink_slab-->shrinker->shrink(shrink_icache_memory)-->prune_icache会将不在使用的inode释放

03-12
<think>嗯,用户想了解Linux文件系统中的dentry概念。首先,我得回忆一下dentry是什么。记得在Linux文件系统管理中,dentry是目录项的意思,属于VFS的一部分。它连接inode和文件名,起到桥梁的作用。可能需要解释dentry的结构和作用,比如如何组织目录结构。 然后,用户可能想知道dentry和inode的关系。需要区分两者的不同,inode存储元数据,而dentry是目录条目。还要提到dentry缓存,这是提高文件访问速度的关键机制,减少磁盘操作。 可能还需要举例子,比如路径解析时dentry如何逐级查找。另外,dentry的生命周期缓存管理也是重点,比如引用计数和LRU算法。最后,结合引用中的信息,引用相关的资料,比如引用[1]提到dentry使用inode,而引用[2]说明没有链接的文件系统如何处理。需要确保回答准确,结构清晰,分点说明,并自然地加入引用标注。</think>### 关于 Linux 文件系统中 `dentry` 的解析 #### 1. **dentry 的基本概念** `dentry`(目录项,Directory Entry)是 Linux 虚拟文件系统(VFS)中的核心数据结构,用于表示文件系统中的目录或文件的路径节点。其核心作用包括: - **连接文件名与 inode**:`dentry` 将文件名(用户可见)与存储文件元数据的 `inode`(用户不可见)关联起来[^1]。 - **组织目录结构**:通过父子关系构建目录树,例如 `/home/user/file.txt` 的路径会被分解为多个 `dentry` 节点(如 `home`、`user`、`file.txt`)。 #### 2. **dentry 与 inode 的关系** - **inode**:存储文件的元数据(如权限、大小、时间戳)和物理数据块的位置。 - **dentry**:仅包含文件名、父目录、子目录等信息,不涉及文件内容。一个 `inode` 可以被多个 `dentry` 引用(例如硬链接)[^2]。 #### 3. **dentry 缓存的作用** Linux 通过 `dentry` 缓存(`dcache`)加速文件访问: - **减少磁盘 I/O**:缓存频繁访问的目录项,避免重复解析路径。 - **快速查找**:哈希表结构(`dentry_hashtable`)支持快速通过路径名查找 `dentry`。 #### 4. **dentry 的生命周期** - **创建**:路径解析时动态生成(例如 `open("/tmp/file")`)。 - **缓存**:未被释放的 `dentry` 会驻留在内存中,直到内存压力触发回收。 - **释放**:通过引用计数(`d_count`)管理,当引用为 0 时可能被放入 LRU 列表等待回收[^2]。 #### 5. **示例:路径解析过程** 访问 `/home/user/file.txt` 时: 1. 从根目录 `/` 的 `dentry` 开始查找 `home`。 2. 找到 `home` 的 `dentry`,继续查找 `user`。 3. 最后找到 `file.txt` 的 `dentry`,并通过其关联的 `inode` 访问文件内容[^4]。 #### 6. **dentry 的优化与问题** - **负缓存(Negative Caching)**:缓存不存在的文件查询结果,避免重复查找。 - **内存开销**:大量小文件场景可能导致 `dcache` 占用过高,需调整内核参数(如 `dentry-state`)。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值