【问题标题】:Linux Kernel - What does it mean to "put" an inode?Linux 内核——“放置”一个 inode 是什么意思?
【发布时间】:2016-03-08 06:24:06
【问题描述】:
我在iput 函数顶部看到了以下评论:
/**
* iput - put an inode
* @inode: inode to put
*
* Puts an inode, dropping its usage count. If the inode use count hits
* zero, the inode is then freed and may also be destroyed.
*
* Consequently, iput() can sleep.
*/
对我来说,这听起来不是“放”任何东西,而是“放下”它。我知道 drop_inode 函数,在某些情况下会从 iput 调用,因此“put”一词的用法在这里更加令人困惑。
【问题讨论】:
标签:
linux
linux-kernel
kernel
inode
【解决方案1】:
put 是内核代码中用于减少对象引用计数的常用术语。它是get 的补码,增加了引用计数。您可以在很多地方找到它,而不仅仅是 inode。
引用计数用于防止共享对象在使用时被破坏。使用对象的代码gets 对象,使用它,然后puts 释放它。
【解决方案2】:
iput 与搜索 inode 的 iget 相反,
如有必要,为其分配内存并将对 inode 的引用返回到
来电者。
iput 将此 inode “返回”,即在需要时释放内存。
有一个引用计数器系统,因此可以使用一个 inode
由多个调用者并行,因此可以
只有在没有用户的情况下才被删除(即从内存中删除)
不再(每个用户都打电话给iput)。
/**
* iget_locked - obtain an inode from a mounted file system
* @sb: super block of file system
* @ino: inode number to get
*
* Search for the inode specified by @ino in the inode cache and if present
* return it with an increased reference count. This is for file systems
* where the inode number is sufficient for unique identification of an inode.
*
* If the inode is not in cache, allocate a new inode and return it locked,
* hashed, and with the I_NEW flag set. The file system gets to fill it in
* before unlocking it via unlock_new_inode().
*/
struct inode *iget_locked(struct super_block *sb, unsigned long ino)
【解决方案3】:
基本上一个进程有一个文件描述符表,其中包括一个指向进程打开的文件的文件指针,文件指针实际上是一个指向Open File Table(由内核维护)的一项的指针。 Open File Table 将有一个 inode 指针指向 I-node Table 中的项目(也由内核维护)。 I节点表包含文件的所有信息(文件信息和指向块存储文件数据的指针)
当您打开一个文件时,一个 inode 项会添加到 I-node 表中。为了更快地实现和释放inode,系统会维护一个inode 缓存。当 I-node Table 需要一个新的 item 时,它会使用 iget() 从缓存中获取一个 inode,当一个文件关闭时,它会使用 iput() 将相关的 inode 返回到缓存中。
所以 iput() 表示将 inode 放入 inode 缓存,DROPPING 表示减少 I-node Table 中某个 inode 的引用。更多详情请参考this page。