code:iput - ikarishinjieva/unixV6-code-analyze-chs GitHub Wiki

Table of Contents

Source

  • 指定inode结构的 引用数-1,此时
    • 若引用数为0(内存中没有使用该inode块的进程),则
      • 若没有勾连(该inode代表的文件在文件系统中已无效),则释放inode结构和其文件块
      • 否则,只释放内存inode结构(磁盘inode结构和其文件块在文件系统中仍有效,不可释放)

7336

7337 /*

7338  * Decrement reference count of

7339  * an inode structure.

7340  * On the last reference,

7341  * write the inode out and if necessary,

7342  * truncate and deallocate the file.

7343  */

7344 iput(p)

7345 struct inode *p;

7346 {

7347     register *rp;

7348

7349     rp = p;

7350     if(rp->i_count == 1) {

7351          rp->i_flag =| ILOCK;

7352          if(rp->i_nlink <= 0) {

7353           itrunc(rp);

7354           rp->i_mode = 0;

7355           ifree(rp->i_dev, rp->i_number);

7356          }

  • 若勾连数<=0,则inode代表的文件已从文件系统中被删除(不可通过查找找到其存在),则
    • itrunc释放inode对应的文件块
    • 清零 i_mode
    • ifree释放磁盘上的inode块(但未写回磁盘,写回操作在7357)
7357          iupdat(rp, time);

7358          prele(rp);

  • 7358 与 7363 在此分支执行时重复
  • 7358 是为了在释放内存inode块之前 解锁并唤醒等待进程
  • 7363在此分支执行时 多余
7359          rp->i_flag = 0;

7360          rp->i_number = 0;

  • 相当于释放内存inode块
7361     }

7362     rp->i_count--;

7363     prele(rp);

7364 }

7365 /* ------------------------- */

Extend

附图

Ref

Caller

⚠️ **GitHub.com Fallback** ⚠️