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

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

[日期:2012-02-09] 来源:Linux社区  作者:bullbat [字体:大 中 小]
[cpp]
  1. /* 
  2.  * Shrink the dentry LRU on a given superblock. 
  3.  * @sb   : superblock to shrink dentry LRU. 
  4.  * @count: If count is NULL, we prune all dentries on superblock. 
  5.  * @flags: If flags is non-zero, we need to do special processing based on 
  6.  * which flags are set. This means we don't need to maintain multiple 
  7.  * similar copies of this loop. 
  8.  */  
  9. static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags)  
  10. {  
  11.     LIST_HEAD(referenced);  
  12.     LIST_HEAD(tmp);  
  13.     struct dentry *dentry;  
  14.     int cnt = 0;  
  15.   
  16.     BUG_ON(!sb);  
  17.     BUG_ON((flags & DCACHE_REFERENCED) && count == NULL);  
  18.     spin_lock(&dcache_lock);  
  19.     if (count != NULL)  
  20.         /* called from prune_dcache() and shrink_dcache_parent() */  
  21.         cnt = *count;/*在下面用到*/  
  22. restart:  
  23.     if (count == NULL)  
  24.         list_splice_init(&sb->s_dentry_lru, &tmp);  
  25.     else {  
  26.         while (!list_empty(&sb->s_dentry_lru)) {  
  27.             dentry = list_entry(sb->s_dentry_lru.prev,  
  28.                     struct dentry, d_lru);  
  29.             BUG_ON(dentry->d_sb != sb);  
  30.   
  31.             spin_lock(&dentry->d_lock);  
  32.             /* 
  33.              * If we are honouring the DCACHE_REFERENCED flag and 
  34.              * the dentry has this flag set, don't free it. Clear 
  35.              * the flag and put it back on the LRU. 
  36.              */  
  37.              /*清flag对应位,将链表元素放LRU尾部*/  
  38.             if ((flags & DCACHE_REFERENCED)  
  39.                 && (dentry->d_flags & DCACHE_REFERENCED)) {  
  40.                 dentry->d_flags &= ~DCACHE_REFERENCED;  
  41.                 list_move(&dentry->d_lru, &referenced);  
  42.                 spin_unlock(&dentry->d_lock);  
  43.             } else {  
  44.                 /*从d_lru链表中删除,加到tmp链表中*/  
  45.                 list_move_tail(&dentry->d_lru, &tmp);  
  46.                 spin_unlock(&dentry->d_lock);  
  47.                 cnt--;/*数量减一*/  
  48.                 if (!cnt)/*减到0跳出循环*/  
  49.                     break;  
  50.             }  
  51.             cond_resched_lock(&dcache_lock);  
  52.         }  
  53.     }  
  54.     /*对tmp中的每个元素,其中tmp中的元素为上面移过来的*/  
  55.     while (!list_empty(&tmp)) {  
  56.         dentry = list_entry(tmp.prev, struct dentry, d_lru);  
  57.         /*从tmp中删除相关链表并做重新初始化和数据统计*/  
  58.         dentry_lru_del_init(dentry);  
  59.         spin_lock(&dentry->d_lock);  
  60.         /* 
  61.          * We found an inuse dentry which was not removed from 
  62.          * the LRU because of laziness during lookup.  Do not free 
  63.          * it - just keep it off the LRU list. 
  64.          */  
  65.         if (atomic_read(&dentry->d_count)) {  
  66.             spin_unlock(&dentry->d_lock);  
  67.             continue;  
  68.         }/*释放dentry和其父dentry*/  
  69.         prune_one_dentry(dentry);  
  70.         /* dentry->d_lock was dropped in prune_one_dentry() */  
  71.         cond_resched_lock(&dcache_lock);  
  72.     }  
  73.     if (count == NULL && !list_empty(&sb->s_dentry_lru))  
  74.         goto restart;  
  75.     if (count != NULL)  
  76.         *count = cnt;  
  77.     if (!list_empty(&referenced))  
  78.         list_splice(&referenced, &sb->s_dentry_lru);  
  79.     spin_unlock(&dcache_lock);  
  80. }  
[cpp]
  1. static void dentry_lru_del_init(struct dentry *dentry)  
  2. {  
  3.     if (likely(!list_empty(&dentry->d_lru))) {  
  4.         list_del_init(&dentry->d_lru);/*从链表中删除并初始化dentry->d_lru*/  
  5.         dentry->d_sb->s_nr_dentry_unused--;/*未用数减一*/  
  6.         dentry_stat.nr_unused--;/*更新统计数据*/  
  7.     }  
  8. }  
[html]
  1. /*  
  2.  * Throw away a dentry - free the inode, dput the parent.  This requires that  
  3.  * the LRU list has already been removed.  
  4.  *  
  5.  * Try to prune ancestors as well.  This is necessary to prevent  
  6.  * quadratic behavior of shrink_dcache_parent(), but is also expected  
  7.  * to be beneficial in reducing dentry cache fragmentation.  
  8.  */  
  9. static void prune_one_dentry(struct dentry * dentry)  
  10.     __releases(dentry->d_lock)  
  11.     __releases(dcache_lock)  
  12.     __acquires(dcache_lock)  
  13. {  
  14.     __d_drop(dentry);  
  15.     dentry = d_kill(dentry);/*释放dentry*/  
  16.   
  17.     /*  
  18.      * Prune ancestors.  Locking is simpler than in dput(),  
  19.      * because dcache_lock needs to be taken anyway.  
  20.      */  
  21.     spin_lock(&dcache_lock);  
  22.     while (dentry) {  
  23.         if (!atomic_dec_and_lock(&dentry->d_count, &dentry->d_lock))  
  24.             return;  
  25.   
  26.         if (dentry->d_op && dentry->d_op->d_delete)  
  27.             dentry->d_op->d_delete(dentry);  
  28.         dentry_lru_del_init(dentry);  
  29.         __d_drop(dentry);  
  30.         dentry = d_kill(dentry);  
  31.         spin_lock(&dcache_lock);  
  32.     }  
  33. }  
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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