- /**
- * d_kill - kill dentry and return parent
- * @dentry: dentry to kill
- *
- * The dentry must already be unhashed and removed from the LRU.
- *
- * If this is the root of the dentry tree, return NULL.
- */
- static struct dentry *d_kill(struct dentry *dentry)
- __releases(dentry->d_lock)
- __releases(dcache_lock)
- {
- struct dentry *parent;
- list_del(&dentry->d_u.d_child);/*删除子目录*/
- dentry_stat.nr_dentry--;/*更新统计数据*/ /* For d_free, below */
- /*drops the locks, at that point nobody can reach this dentry */
- dentry_iput(dentry);/*"释放"inode*/
- if (IS_ROOT(dentry))
- parent = NULL;
- else
- parent = dentry->d_parent;
- d_free(dentry);/*释放dentry*/
- return parent;
- }
- /*
- * Release the dentry's inode, using the filesystem
- * d_iput() operation if defined.
- */
- /*释放inode*/
- static void dentry_iput(struct dentry * dentry)
- __releases(dentry->d_lock)
- __releases(dcache_lock)
- {
- struct inode *inode = dentry->d_inode;
- if (inode) {
- dentry->d_inode = NULL;
- list_del_init(&dentry->d_alias);/*从同一索引节点目录链表中删除*/
- spin_unlock(&dentry->d_lock);
- spin_unlock(&dcache_lock);
- if (!inode->i_nlink)/*如果inode没有硬链接*/
- fsnotify_inoderemove(inode);
- if (dentry->d_op && dentry->d_op->d_iput)
- dentry->d_op->d_iput(dentry, inode);
- else
- iput(inode);/*释放inode*/
- } else {
- spin_unlock(&dentry->d_lock);
- spin_unlock(&dcache_lock);
- }
- }
2.注册inode cache shrinker
Start_kernel()->vfs_caches_init()->inode_init()->register_shrinker(&icache_shrinker);
其中参数为下面定义
- static struct shrinker icache_shrinker = {
- .shrink = shrink_icache_memory,
- .seeks = DEFAULT_SEEKS,
- };
- static int shrink_icache_memory(int nr, gfp_t gfp_mask)
- {
- if (nr) {
- /*
- * Nasty deadlock avoidance. We may hold various FS locks,
- * and we don't want to recurse into the FS that called us
- * in clear_inode() and friends..
- */
- if (!(gfp_mask & __GFP_FS))
- return -1;
- prune_icache(nr);
- }
- return (inodes_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
- }
- /*
- * Scan `goal' inodes on the unused list for freeable ones. They are moved to
- * a temporary list and then are freed outside inode_lock by dispose_list().
- *
- * Any inodes which are pinned purely because of attached pagecache have their
- * pagecache removed. We expect the final iput() on that inode to add it to
- * the front of the inode_unused list. So look for it there and if the
- * inode is still freeable, proceed. The right inode is found 99.9% of the
- * time in testing on a 4-way.
- *
- * If the inode has metadata buffers attached to mapping->private_list then
- * try to remove them.
- */
- static void prune_icache(int nr_to_scan)
- {
- LIST_HEAD(freeable);/*初始化freeable,在下面需要用到,作为临时存放可被释放的inode*/
- int nr_pruned = 0;
- int nr_scanned;
- unsigned long reap = 0;
- down_read(&iprune_sem);
- spin_lock(&inode_lock);
- for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
- struct inode *inode;
- if (list_empty(&inode_unused))
- break;
- inode = list_entry(inode_unused.prev, struct inode, i_list);
- if (inode->i_state || atomic_read(&inode->i_count)) {
- /*将ionde从inode_unused链表中删除,加入inode_unused链表头*/
- list_move(&inode->i_list, &inode_unused);
- continue;
- }
- if (inode_has_buffers(inode) || inode->i_data.nrpages) {
- __iget(inode);/*移动到使用链表*/
- spin_unlock(&inode_lock);
- if (remove_inode_buffers(inode))/*从buffer链表中删除所有buffer*/
- reap += invalidate_mapping_pages(&inode->i_data,
- 0, -1);
- iput(inode);
- spin_lock(&inode_lock);
- if (inode != list_entry(inode_unused.next,
- struct inode, i_list))
- continue; /* wrong inode or list_empty */
- if (!can_unuse(inode))
- continue;
- }
- /*移动到freeable链表*/
- list_move(&inode->i_list, &freeable);
- WARN_ON(inode->i_state & I_NEW);
- inode->i_state |= I_FREEING;
- nr_pruned++;/*统计移动到freeable链表的元素个数*/
- }
- inodes_stat.nr_unused -= nr_pruned;/*更新统计数据*/
- if (current_is_kswapd())
- __count_vm_events(KSWAPD_INODESTEAL, reap);
- else
- __count_vm_events(PGINODESTEAL, reap);
- spin_unlock(&inode_lock);
- dispose_list(&freeable);/*将freeable链表中的数据处理掉*/
- up_read(&iprune_sem);
- }
