手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux虚拟文件系统(内核初始化<二>)

[日期:2012-02-09] 来源:Linux社区  作者:bullbat [字体:大 中 小]
[cpp]
  1. /** 
  2.  * d_kill - kill dentry and return parent 
  3.  * @dentry: dentry to kill 
  4.  * 
  5.  * The dentry must already be unhashed and removed from the LRU. 
  6.  * 
  7.  * If this is the root of the dentry tree, return NULL. 
  8.  */  
  9. static struct dentry *d_kill(struct dentry *dentry)  
  10.     __releases(dentry->d_lock)  
  11.     __releases(dcache_lock)  
  12. {  
  13.     struct dentry *parent;  
  14.   
  15.     list_del(&dentry->d_u.d_child);/*删除子目录*/  
  16.     dentry_stat.nr_dentry--;/*更新统计数据*/  /* For d_free, below */  
  17.     /*drops the locks, at that point nobody can reach this dentry */  
  18.     dentry_iput(dentry);/*"释放"inode*/  
  19.     if (IS_ROOT(dentry))  
  20.         parent = NULL;  
  21.     else  
  22.         parent = dentry->d_parent;  
  23.     d_free(dentry);/*释放dentry*/  
  24.     return parent;  
  25. }  
[cpp]
  1. /* 
  2.  * Release the dentry's inode, using the filesystem 
  3.  * d_iput() operation if defined. 
  4.  */  
  5.  /*释放inode*/  
  6. static void dentry_iput(struct dentry * dentry)  
  7.     __releases(dentry->d_lock)  
  8.     __releases(dcache_lock)  
  9. {  
  10.     struct inode *inode = dentry->d_inode;  
  11.     if (inode) {  
  12.         dentry->d_inode = NULL;  
  13.         list_del_init(&dentry->d_alias);/*从同一索引节点目录链表中删除*/  
  14.         spin_unlock(&dentry->d_lock);  
  15.         spin_unlock(&dcache_lock);  
  16.         if (!inode->i_nlink)/*如果inode没有硬链接*/  
  17.             fsnotify_inoderemove(inode);  
  18.         if (dentry->d_op && dentry->d_op->d_iput)  
  19.             dentry->d_op->d_iput(dentry, inode);  
  20.         else  
  21.             iput(inode);/*释放inode*/  
  22.     } else {  
  23.         spin_unlock(&dentry->d_lock);  
  24.         spin_unlock(&dcache_lock);  
  25.     }  
  26. }  

2.注册inode cache shrinker

Start_kernel()->vfs_caches_init()->inode_init()->register_shrinker(&icache_shrinker);

其中参数为下面定义

[cpp]
  1. static struct shrinker icache_shrinker = {  
  2.     .shrink = shrink_icache_memory,  
  3.     .seeks = DEFAULT_SEEKS,  
  4. };  
[cpp]
  1. static int shrink_icache_memory(int nr, gfp_t gfp_mask)  
  2. {  
  3.     if (nr) {  
  4.         /* 
  5.          * Nasty deadlock avoidance.  We may hold various FS locks, 
  6.          * and we don't want to recurse into the FS that called us 
  7.          * in clear_inode() and friends.. 
  8.          */  
  9.         if (!(gfp_mask & __GFP_FS))  
  10.             return -1;  
  11.         prune_icache(nr);  
  12.     }  
  13.     return (inodes_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;  
  14. }  
[cpp]
  1. /* 
  2.  * Scan `goal' inodes on the unused list for freeable ones. They are moved to 
  3.  * a temporary list and then are freed outside inode_lock by dispose_list(). 
  4.  * 
  5.  * Any inodes which are pinned purely because of attached pagecache have their 
  6.  * pagecache removed.  We expect the final iput() on that inode to add it to 
  7.  * the front of the inode_unused list.  So look for it there and if the 
  8.  * inode is still freeable, proceed.  The right inode is found 99.9% of the 
  9.  * time in testing on a 4-way. 
  10.  * 
  11.  * If the inode has metadata buffers attached to mapping->private_list then 
  12.  * try to remove them. 
  13.  */  
  14. static void prune_icache(int nr_to_scan)  
  15. {  
  16.     LIST_HEAD(freeable);/*初始化freeable,在下面需要用到,作为临时存放可被释放的inode*/  
  17.     int nr_pruned = 0;  
  18.     int nr_scanned;  
  19.     unsigned long reap = 0;  
  20.   
  21.     down_read(&iprune_sem);  
  22.     spin_lock(&inode_lock);  
  23.     for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {  
  24.         struct inode *inode;  
  25.   
  26.         if (list_empty(&inode_unused))  
  27.             break;  
  28.   
  29.         inode = list_entry(inode_unused.prev, struct inode, i_list);  
  30.   
  31.         if (inode->i_state || atomic_read(&inode->i_count)) {  
  32.             /*将ionde从inode_unused链表中删除,加入inode_unused链表头*/  
  33.             list_move(&inode->i_list, &inode_unused);  
  34.             continue;  
  35.         }  
  36.         if (inode_has_buffers(inode) || inode->i_data.nrpages) {  
  37.             __iget(inode);/*移动到使用链表*/  
  38.             spin_unlock(&inode_lock);  
  39.             if (remove_inode_buffers(inode))/*从buffer链表中删除所有buffer*/  
  40.                 reap += invalidate_mapping_pages(&inode->i_data,  
  41.                                 0, -1);  
  42.             iput(inode);  
  43.             spin_lock(&inode_lock);  
  44.   
  45.             if (inode != list_entry(inode_unused.next,  
  46.                         struct inode, i_list))  
  47.                 continue;   /* wrong inode or list_empty */  
  48.             if (!can_unuse(inode))  
  49.                 continue;  
  50.         }  
  51.         /*移动到freeable链表*/  
  52.         list_move(&inode->i_list, &freeable);  
  53.         WARN_ON(inode->i_state & I_NEW);  
  54.         inode->i_state |= I_FREEING;  
  55.         nr_pruned++;/*统计移动到freeable链表的元素个数*/  
  56.     }  
  57.     inodes_stat.nr_unused -= nr_pruned;/*更新统计数据*/  
  58.     if (current_is_kswapd())  
  59.         __count_vm_events(KSWAPD_INODESTEAL, reap);  
  60.     else  
  61.         __count_vm_events(PGINODESTEAL, reap);  
  62.     spin_unlock(&inode_lock);  
  63.   
  64.     dispose_list(&freeable);/*将freeable链表中的数据处理掉*/  
  65.     up_read(&iprune_sem);  
  66. }  
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款