⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 file_systems.html

📁 ADI 公司blackfin系列的用户使用文挡。
💻 HTML
📖 第 1 页 / 共 2 页
字号:
</p><p> The key code components are: </p><ul><li class="level1"><div class="li"> register a file system</div></li><li class="level1"><div class="li"> define how to read a superblock</div></li></ul><p> Once you read a superblock you then define the file operations allowed by that file systemo The basic building block of a file system is the inode. An inode defines an entity in the file system. Typically such an entity can be  </p><ul><li class="level1"><div class="li"> a regular file</div></li><li class="level1"><div class="li"> a directory</div></li><li class="level1"><div class="li"> a link to another entry</div></li></ul><p> For each different type of inode the file system registers different methods for handling reads and writes to that particular inode. There are inode operations and file operations.</p><p>Directory inode operations include </p><ul><li class="level1"><div class="li"> create  - create a file in the directory</div></li><li class="level1"><div class="li"> mkdir - make a sub directory </div></li><li class="level1"><div class="li"> mknod - make a special device file</div></li><li class="level1"><div class="li"> symlink - make a link to another file</div></li><li class="level1"><div class="li"> rename - change the name of an entry</div></li><li class="level1"><div class="li"> unlink - delete a file ( when no longer used )</div></li></ul><p>Refer to the code for more details</p></div><!-- SECTION [3417-4573] --><h2><a name="file_system_exampledebugfs" id="file_system_exampledebugfs">File System Example: Debugfs</a></h2><div class="level2"><p>Created by Greg Kroah-Hartman debugfs is an in-kernel filesystem which gas been designed to help kernel developers easily access debug data from userspace.</p><p>It has been designed to be easy to use and also it is optional. If you dont mount the debugfs the filesystem , and its kernel memory overhead, is not there.</p><p> Debugfs is available as a patch which also contains a few use examples.</p><p>NOTE that all the code below is in <strong>kernel space</strong> </p><p> You can create a debug directory as follows:</p><pre class="code c">&nbsp;<span class="kw4">struct</span> dentry *debugfs_create_dir<span class="br0">&#40;</span><span class="kw4">const</span> <span class="kw4">char</span> *name, <span class="kw4">struct</span> dentry *parent<span class="br0">&#41;</span>;&nbsp;<span class="kw4">struct</span> dentry *mydebdir;mydebdir = debugfs_create_dir<span class="br0">&#40;</span><span class="st0">"mydebug"</span>, <span class="kw2">NULL</span><span class="br0">&#41;</span>;&nbsp;</pre><p>Then, for example, to export a single value <strong>mydebugch</strong> to userspace:</p><pre class="code c"><span class="kw4">struct</span> dentry *debugfs_create_u8<span class="br0">&#40;</span><span class="kw4">const</span> <span class="kw4">char</span> *name,                                 mode_t mode,                                 <span class="kw4">struct</span> dentry *parent, u8*value<span class="br0">&#41;</span>;&nbsp;&nbsp;&nbsp;<span class="co1">//Example</span>&nbsp;u8 mydebugch; <span class="br0">&#40;</span>also u16 , u32 <span class="br0">&#41;</span>debugfs_create_u8<span class="br0">&#40;</span><span class="st0">"mydebugch"</span>, S_IWUGO, mydebdir, &amp;mydebugchar<span class="br0">&#41;</span>;</pre><p> Two lines of code provide the following entry in the file system</p><pre class="code">    /debug/mydebug/mydebugchar</pre><p> For your own parser you will need to define a file operations table with suitable  open , seek, read and write functions.</p><pre class="code c">&nbsp;<span class="kw4">static</span> <span class="kw4">int</span> my_open<span class="br0">&#40;</span><span class="kw4">struct</span> inode *inode, <span class="kw4">struct</span> file *file<span class="br0">&#41;</span><span class="br0">&#123;</span>        <span class="kw1">return</span> <span class="nu0">0</span>;<span class="br0">&#125;</span>&nbsp;<span class="kw4">static</span> loff_t my_lseek<span class="br0">&#40;</span><span class="kw4">struct</span> file *file, loff_t offset, <span class="kw4">int</span> orig<span class="br0">&#41;</span><span class="br0">&#123;</span>        loff_t retval = -EINVAL;&nbsp;        down<span class="br0">&#40;</span>&amp;file-&gt;f_dentry-&gt;d_inode-&gt;i_sem<span class="br0">&#41;</span>;        <span class="kw1">switch</span><span class="br0">&#40;</span>orig<span class="br0">&#41;</span> <span class="br0">&#123;</span>        <span class="kw1">case</span> <span class="nu0">0</span>:                <span class="kw1">if</span> <span class="br0">&#40;</span>offset &gt; <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>                        file-&gt;f_pos = offset;                        retval = file-&gt;f_pos;                <span class="br0">&#125;</span>                <span class="kw2">break</span>;        <span class="kw1">case</span> <span class="nu0">1</span>:                <span class="kw1">if</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>offset + file-&gt;f_pos<span class="br0">&#41;</span> &gt; <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>                        file-&gt;f_pos += offset;                        retval = file-&gt;f_pos;                <span class="br0">&#125;</span>                <span class="kw2">break</span>;        <span class="kw1">default</span>:                <span class="kw2">break</span>;        <span class="br0">&#125;</span>        up<span class="br0">&#40;</span>&amp;file-&gt;f_dentry-&gt;d_inode-&gt;i_sem<span class="br0">&#41;</span>;        <span class="kw1">return</span> retval;<span class="br0">&#125;</span>&nbsp;<span class="kw4">static</span> ssize_t my_read<span class="br0">&#40;</span><span class="kw4">struct</span> file *file, <span class="kw4">char</span> __user *user_buf,                                  size_t count, loff_t *ppos<span class="br0">&#41;</span><span class="br0">&#123;</span>        <span class="kw4">char</span> buf<span class="br0">&#91;</span><span class="nu0">32</span><span class="br0">&#93;</span>;        u32 *val = file-&gt;private_data;        <span class="kw4">int</span> len;        len = sprintf<span class="br0">&#40;</span>buf,<span class="st0">"my val is %d <span class="es0">\n</span>"</span>,val<span class="br0">&#41;</span>;&nbsp;        <span class="kw1">return</span> simple_read_from_buffer<span class="br0">&#40;</span>user_buf, count, ppos, buf, len<span class="br0">&#41;</span>;<span class="br0">&#125;</span>&nbsp;<span class="kw4">static</span> ssize_t my_write<span class="br0">&#40;</span><span class="kw4">struct</span> file *file, <span class="kw4">const</span> <span class="kw4">char</span> __user *user_buf,                              size_t count, loff_t *ppos<span class="br0">&#41;</span><span class="br0">&#123;</span>        <span class="kw4">char</span> buf<span class="br0">&#91;</span><span class="nu0">32</span><span class="br0">&#93;</span>;        <span class="kw4">int</span> buf_size;        <span class="kw4">int</span> scanc;        u32 *val = file-&gt;private_data;        buf_size = min<span class="br0">&#40;</span>count, <span class="br0">&#40;</span><span class="kw4">sizeof</span><span class="br0">&#40;</span>buf<span class="br0">&#41;</span><span class="nu0">-1</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;        <span class="kw1">if</span> <span class="br0">&#40;</span>copy_from_user<span class="br0">&#40;</span>buf, user_buf, buf_size<span class="br0">&#41;</span><span class="br0">&#41;</span>                <span class="kw1">return</span> -EFAULT;&nbsp;        sscanf<span class="br0">&#40;</span>buf,<span class="st0">"Set val to %d<span class="es0">\n</span>"</span>,val<span class="br0">&#41;</span>;&nbsp;        <span class="kw1">return</span> count;<span class="br0">&#125;</span></pre><p>Create a file using debugfs the call is</p><pre class="code c"><span class="kw4">static</span> <span class="kw4">struct</span> file_operations my_fops = <span class="br0">&#123;</span>        .<span class="me1">read</span> =         my_read,        .<span class="me1">write</span> =        my_write,        .<span class="me1">open</span> =         my_open,        .<span class="me1">llseek</span> =       my_lseek,<span class="br0">&#125;</span>;&nbsp;</pre><p>Then use the <strong>create_file</strong> system function to make the entry in debugfs:</p><pre class="code c"><span class="kw4">struct</span> dentry *debugfs_create_file<span class="br0">&#40;</span><span class="kw4">const</span> <span class="kw4">char</span> *name,                                   mode_t mode,                                   <span class="kw4">struct</span> dentry *parent, <span class="kw4">void</span> *data,                                   <span class="kw4">struct</span> file_operations *fops<span class="br0">&#41;</span>;&nbsp;<span class="kw4">static</span> <span class="kw4">int</span> mysval;&nbsp;debugfs_create_file<span class="br0">&#40;</span><span class="st0">"mystruct"</span>,                        <span class="nu0">0444</span>,                        mydebdir,                        &amp;mysval,                        my_fops<span class="br0">&#41;</span>;&nbsp;&nbsp;</pre></div><!-- SECTION [4574-] --></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -