﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-Jiwu Bu-随笔分类-Linux内核VFS</title><link>http://www.cppblog.com/bujiwu/category/14178.html</link><description /><language>zh-cn</language><lastBuildDate>Sun, 04 Jul 2010 09:13:27 GMT</lastBuildDate><pubDate>Sun, 04 Jul 2010 09:13:27 GMT</pubDate><ttl>60</ttl><item><title>Linux内核VFS---rootfs文件系统加载</title><link>http://www.cppblog.com/bujiwu/archive/2010/07/04/119301.html</link><dc:creator>bujiwu</dc:creator><author>bujiwu</author><pubDate>Sun, 04 Jul 2010 08:58:00 GMT</pubDate><guid>http://www.cppblog.com/bujiwu/archive/2010/07/04/119301.html</guid><wfw:comment>http://www.cppblog.com/bujiwu/comments/119301.html</wfw:comment><comments>http://www.cppblog.com/bujiwu/archive/2010/07/04/119301.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/bujiwu/comments/commentRss/119301.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/bujiwu/services/trackbacks/119301.html</trackback:ping><description><![CDATA[对linux-2.6.34源码rootfs文件系统加载学习笔记<br><br>1.内核启动调用start_kernel函数进行初始化。
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;">asmlinkage&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;__init&nbsp;start_kernel(</span><span style="color: #0000ff;">void</span><span style="color: #000000;">)&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">init\Main.c:528</span><span style="color: #008000;"><br></span><span style="color: #000000;">{<br>&nbsp;&nbsp;&nbsp;&nbsp;buffer_init();<br>&nbsp;&nbsp;&nbsp;&nbsp;key_init();<br>&nbsp;&nbsp;&nbsp;&nbsp;security_init();<br>&nbsp;&nbsp;&nbsp;&nbsp;vfs_caches_init(totalram_pages);</span><span style="color: #008000;">//</span><span style="color: #008000;">678行，vfs初始化</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;signals_init();<br>}</span></div>
2.start_kernel调用vfs_caches_init对vfs进行初始化。<br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008000;">//</span><span style="color: #008000;">fs\Dcach.c:2354行</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;__init&nbsp;vfs_caches_init(unsigned&nbsp;</span><span style="color: #0000ff;">long</span><span style="color: #000000;">&nbsp;mempages)<br>{<br>&nbsp;&nbsp;&nbsp; </span><span style="color: #000000;">.......<br>&nbsp;&nbsp;&nbsp;&nbsp;dcache_init();<br>&nbsp;&nbsp;&nbsp;&nbsp;inode_init();<br>&nbsp;&nbsp;&nbsp;&nbsp;files_init(mempages);<br>&nbsp;&nbsp;&nbsp;&nbsp;mnt_init();&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">mnt初始化</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;bdev_cache_init();<br>&nbsp;&nbsp;&nbsp;&nbsp;chrdev_init();<br>}<br></span></div>
<br>3.在vfs_caches_init调用fs\namespace.c 2321行的mnt_init函数<br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;__init&nbsp;mnt_init(</span><span style="color: #0000ff;">void</span><span style="color: #000000;">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<img src="http://www.cppblog.com/Images/dot.gif"><img src="http://www.cppblog.com/Images/dot.gif"><br>&nbsp;&nbsp;&nbsp;&nbsp;init_rootfs();&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">初始化rootfs文件系统</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;init_mount_tree();&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">初始化加载树</span><span style="color: #008000;"><br></span><span style="color: #000000;">}</span></div>
4. fs\ramfs第308行的init_rootfs函数初始化rootfs文件系统<br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;__init&nbsp;init_rootfs(</span><span style="color: #0000ff;">void</span><span style="color: #000000;">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;err;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;err&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;bdi_init(</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">ramfs_backing_dev_info);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(err)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;err;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;err&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;register_filesystem(</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">rootfs_fs_type);</span><span style="color: #008000;">//</span><span style="color: #008000;">注册rootfs文件系统</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(err)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bdi_destroy(</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">ramfs_backing_dev_info);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;err;<br>}</span></div>
rootfs文件系统信息fs\ramfs第289行
<br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">struct</span><span style="color: #000000;">&nbsp;file_system_type&nbsp;rootfs_fs_type&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;.name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">rootfs</span><span style="color: #000000;">"</span><span style="color: #000000;">,<br>&nbsp;&nbsp;&nbsp;&nbsp;.get_sb&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;rootfs_get_sb,<br>&nbsp;&nbsp;&nbsp;&nbsp;.kill_sb&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;kill_litter_super,<br>};</span></div>
<br>5.调用register_filesystem函数将rootfs注册到<span style="font-weight: bold; color: #ff0000; font-family: 黑体;">file_systems</span>文件系统中。<br>
<div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000ff;">//fs\Filesystem.c第69行。<br>int</span><span style="color: #000000;">&nbsp;register_filesystem(</span><span style="color: #0000ff;">struct</span><span style="color: #000000;">&nbsp;file_system_type&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;fs)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;res&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">struct</span><span style="color: #000000;">&nbsp;file_system_type&nbsp;</span><span style="color: #000000;">**</span><span style="color: #000000;">&nbsp;p;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;BUG_ON(strchr(fs</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">name,&nbsp;</span><span style="color: #000000;">'</span><span style="color: #000000;">.</span><span style="color: #000000;">'</span><span style="color: #000000;">));<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(fs</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">next)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">EBUSY;<br>&nbsp;&nbsp;&nbsp;&nbsp;INIT_LIST_HEAD(</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">fs</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">fs_supers);<br>&nbsp;&nbsp;&nbsp;&nbsp;write_lock(</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">file_systems_lock);<br>&nbsp;&nbsp;&nbsp;&nbsp;p&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;find_filesystem(fs</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">name,&nbsp;strlen(fs</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">name));//查找到文件系统加载位置<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(</span><span style="color: #000000;">*</span><span style="color: #000000;">p)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;res&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">EBUSY;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">p&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;fs;<br>&nbsp;&nbsp;&nbsp;&nbsp;write_unlock(</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">file_systems_lock);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;res;<br>}</span></div>
file_systems变量是所有文件系统链表的头指针。通过以上过程rootfs_fs_type就加入到了file_systems链表头的file_system_type类型的链表中。可以通过file_systems指针遍历这个链表，得到特定的文件系统file_system_type
指针。<br>  <img src ="http://www.cppblog.com/bujiwu/aggbug/119301.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/bujiwu/" target="_blank">bujiwu</a> 2010-07-04 16:58 <a href="http://www.cppblog.com/bujiwu/archive/2010/07/04/119301.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>