📄 036_fs_dcache_c.html
字号:
<html lang="zh-CN" xmlns:gdoc=""> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <style type="text/css">/* default css */table { font-size: 1em; line-height: inherit;}div, address, ol, ul, li, option, select { margin-top: 0px; margin-bottom: 0px;}p { margin: 0px;}body { margin: 0px; padding: 0px; font-family: Verdana, sans-serif; font-size: 10pt; background-color: #ffffff;}h6 { font-size: 10pt }h5 { font-size: 11pt }h4 { font-size: 12pt }h3 { font-size: 13pt }h2 { font-size: 14pt }h1 { font-size: 16pt }blockquote {padding: 10px; border: 1px #DDD dashed }a img {border: 0}div.google_header, div.google_footer { position: relative; margin-top: 1em; margin-bottom: 1em;}/* end default css */ /* default print css */ @media print { body { padding: 0; margin: 0; } div.google_header, div.google_footer { display: block; min-height: 0; border: none; } div.google_header { flow: static(header); } /* used to insert page numbers */ div.google_header::before, div.google_footer::before { position: absolute; top: 0; } div.google_footer { flow: static(footer); } /* always consider this element at the start of the doc */ div#google_footer { flow: static(footer, start); } span.google_pagenumber { content: counter(page); } span.google_pagecount { content: counter(pages); } } @page { @top { content: flow(header); } @bottom { content: flow(footer); } } /* end default print css */ /* custom css *//* end custom css */ /* ui edited css */ body { font-family: Verdana; font-size: 10.0pt; line-height: normal; background-color: #ffffff; } .documentBG { background-color: #ffffff; } /* end ui edited css */</style> </head> <body revision="dcbsxfpf_18c72n4z:276"> <TABLE align=center cellPadding=0 cellSpacing=0 height=5716 width=802>
<TBODY>
<TR>
<TD height=5716 vAlign=top width=802>
<PRE><B><FONT color=#ff00ff><FONT size=4>dcache.c</FONT> <BR></FONT> </B>2007-<SPAN style=FONT-FAMILY:Verdana>11-26<BR><BR></SPAN></PRE>
感觉文件系统里尽是cache, buffer cache, dcahce, inode, super 呵呵. 真是叫人目不暇给(这成语啥意思??).
好在dcache 并不想buffer cache那样千丝万缕不易理出个头绪.<BR>
<BR>
<FONT size=4><B>分配dentry和dentry tree</B></FONT><BR>
<BR>
<BR>
文件系统的比较核心的一棵树, 也就是dentry和mnt组成的一整棵大树,
这里给出一个dentry树的以部分结构,我们先集中精力到这棵树的机构上来,这是一个数据结构的问题:<BR>
struct dentry {<BR>
.....<BR>
struct inode * d_inode; /*
Where the name belongs to - NULL is negative */<BR>
struct dentry * <B>d_parent</B>; /*
parent directory */<BR>
struct list_head d_vfsmnt;<BR>
struct list_head d_hash; /* lookup
hash list */<BR>
struct list_head d_lru;
/* d_count = 0 LRU list */<BR>
struct list_head <B>d_child</B>; /*
child of parent list */<BR>
struct list_head<B> d_subdirs</B>; /*
our children */<BR>
struct list_head d_alias; /* inode
alias list */<BR>
..... <BR>
struct super_block * d_sb; /* The
root of the dentry tree */<BR>
......<BR>
};<BR>
<BR>
dentry本身是一个树形结构,下面两个函数 build这颗树:<BR>
static kmem_cache_t *<FONT color=#3333ff><B>dentry_cache</B></FONT>;
/*dentry 的free list,一个mem cache*/<BR>
struct dentry * <FONT color=#006600><B>d_alloc</B></FONT>(struct dentry *
parent, const struct qstr *name)<BR>
struct dentry *<FONT color=#006600> <B>d_alloc_root</B></FONT>(struct
inode * root_inode)<BR>
<DIV id=mq9f style="PADDING-RIGHT:0pt; PADDING-LEFT:0pt; PADDING-BOTTOM:1em; PADDING-TOP:1em; TEXT-ALIGN:left">
<IMG src=036_fs_dcache_c_images/dcbsxfpf_19f6zntnfd.gif>
</DIV>
<BR>
<FONT size=4><B>dentry的回收</B></FONT><BR>
<BR>
<BR>
static struct list_head
*<FONT color=#990000><B>dentry_hashtable</B></FONT>;
hash,通过dentry->d_hash接入.<BR>
<FONT size=3>一个dentry可以存在于hash中,可以有一个inode与之对应或者没有(negative,已删除).还有一个unused队列:</FONT><BR>
static LIST_HEAD(<FONT color=#990000><B>dentry_unused</B></FONT>);
这是一个已经删除了但是还没有unhash的,等待回收的denty list, 通过dentry->d_lru接入;<BR>
<BR>
<FONT size=2>dentry cache 的复杂就在释放和回收上了. 简单的ref count是不够的.
先看看</FONT><FONT size=2><B>纯粹的内存释</B>放函数:</FONT><BR>
static inline void <FONT color=#006600><B>d_free</B></FONT>(struct dentry
*dentry)<BR>
{<BR>
if (dentry->d_op &&
dentry->d_op->d_release)<BR>
dentry->d_op->d_release(dentry);<BR>
if (dname_external(dentry))<BR>
kfree(dentry->d_name.name);<BR>
kmem_cache_free(dentry_cache, dentry);<BR>
dentry_stat.nr_dentry--;<BR>
}<BR>
然后是<B>hash摘除函数</B>:<BR>
<FONT color=#3333ff>/**</FONT><BR>
<FONT color=#3333ff> * d_drop - drop a dentry</FONT><BR>
<FONT color=#3333ff> * @dentry: dentry to drop</FONT><BR>
<FONT color=#3333ff> *</FONT><BR>
<FONT color=#3333ff> * d_drop() <B>unhashes</B> <B>the entry</B> from
the parent</FONT><BR>
<FONT color=#3333ff> * dentry hashes, so that it won't be found
through</FONT><BR>
<FONT color=#3333ff> * a VFS lookup any more. Note that this is
different</FONT><BR>
<FONT color=#3333ff> * from deleting the dentry - <B>d_delete</B>
will try to</FONT><BR>
<FONT color=#3333ff> * mark the dentry negative if possible, giving
a</FONT><BR>
<FONT color=#3333ff> * successful _negative_ lookup, while d_drop
will</FONT><BR>
<FONT color=#3333ff> * just make the cache lookup fail.</FONT><BR>
<FONT color=#3333ff> *</FONT><BR>
<FONT color=#3333ff> * d_drop() is used mainly for stuff that
wants</FONT><BR>
<FONT color=#3333ff> * to invalidate a dentry for some reason
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -