📄 17.html
字号:
<a name='L73'> <b>break</b>;
<a name='L74'><i><font color='green'>// origin 设置出错,返回出错码退出。</font></i>
<a name='L75'> <b>default</b>:
<a name='L76'> <b>return</b> -<a href='../S/28.html#L51' title='Defined at 51 in include/errno.h.'>EINVAL</a>;
<a name='L77'> <font color='red'>}</font>
<a name='L78'> <b>return</b> file->f_pos; <i><font color='green'>// 返回重定位后的文件读写指针值。</font></i>
<a name='L79'><font color='red'>}</font>
<a name='L80'>
<a name='L81'><i><font color='green'>//// 读文件系统调用函数。</font></i>
<a name='L82'><i><font color='green'>// 参数fd 是文件句柄,buf 是缓冲区,count 是欲读字节数。</font></i>
<a name='L83'><b>int</b>
<a name='L84'><a href='../R/669.html' title='Multiple refered from 2 places.'>sys_read</a> (<b>unsigned</b> <b>int</b> fd, <b>char</b> *buf, <b>int</b> count)
<a name='L85'><font color='red'>{</font>
<a name='L86'> <b>struct</b> file *file;
<a name='L87'> <b>struct</b> m_inode *inode;
<a name='L88'>
<a name='L89'><i><font color='green'>// 如果文件句柄值大于程序最多打开文件数NR_OPEN,或者需要读取的字节计数值小于0,或者该句柄</font></i>
<a name='L90'><i><font color='green'>// 的文件结构指针为空,则返回出错码并退出。</font></i>
<a name='L91'> <b>if</b> (fd >= <a href='../S/31.html#L59' title='Defined at 59 in include/linux/fs.h.'>NR_OPEN</a> || <a href='../D/738.html' title='Multiple defined in 17 places.'>count</a> < 0 || !(file = current->filp[fd]))
<a name='L92'> <b>return</b> -<a href='../S/28.html#L51' title='Defined at 51 in include/errno.h.'>EINVAL</a>;
<a name='L93'><i><font color='green'>// 若需读取的字节数count 等于0,则返回0,退出</font></i>
<a name='L94'> <b>if</b> (!<a href='../D/738.html' title='Multiple defined in 17 places.'>count</a>)
<a name='L95'> <b>return</b> 0;
<a name='L96'><i><font color='green'>// 验证存放数据的缓冲区内存限制。</font></i>
<a name='L97'> <a href='../S/68.html#L34' title='Defined at 34 in kernel/fork.c.'>verify_area</a> (<a href='../D/716.html' title='Multiple defined in 16 places.'>buf</a>, <a href='../D/738.html' title='Multiple defined in 17 places.'>count</a>);
<a name='L98'><i><font color='green'>// 取文件对应的i 节点。若是管道文件,并且是读管道文件模式,则进行读管道操作,若成功则返回</font></i>
<a name='L99'><i><font color='green'>// 读取的字节数,否则返回出错码,退出。</font></i>
<a name='L100'> inode = file->f_inode;
<a name='L101'> <b>if</b> (inode->i_pipe)
<a name='L102'> <b>return</b> (file->f_mode & 1) ? <a href='../S/16.html#L18' title='Defined at 18 in fs/pipe.c.'>read_pipe</a> (inode, <a href='../D/716.html' title='Multiple defined in 16 places.'>buf</a>, <a href='../D/738.html' title='Multiple defined in 17 places.'>count</a>) : -<a href='../D/92.html' title='Multiple defined in 5 places.'>EIO</a>;
<a name='L103'><i><font color='green'>// 如果是字符型文件,则进行读字符设备操作,返回读取的字符数。</font></i>
<a name='L104'> <b>if</b> (<a href='../S/43.html#L36' title='Defined at 36 in include/sys/stat.h.'>S_ISCHR</a> (inode->i_mode))
<a name='L105'> <b>return</b> rw_char (<a href='../S/31.html#L42' title='Defined at 42 in include/linux/fs.h.'>READ</a>, inode->i_zone[0], <a href='../D/716.html' title='Multiple defined in 16 places.'>buf</a>, <a href='../D/738.html' title='Multiple defined in 17 places.'>count</a>, &file->f_pos);
<a name='L106'><i><font color='green'>// 如果是块设备文件,则执行块设备读操作,并返回读取的字节数。</font></i>
<a name='L107'> <b>if</b> (<a href='../S/43.html#L37' title='Defined at 37 in include/sys/stat.h.'>S_ISBLK</a> (inode->i_mode))
<a name='L108'> <b>return</b> <a href='../S/5.html#L70' title='Defined at 70 in fs/block_dev.c.'>block_read</a> (inode->i_zone[0], &file->f_pos, <a href='../D/716.html' title='Multiple defined in 16 places.'>buf</a>, <a href='../D/738.html' title='Multiple defined in 17 places.'>count</a>);
<a name='L109'><i><font color='green'>// 如果是目录文件或者是常规文件,则首先验证读取数count 的有效性并进行调整(若读取字节数加上</font></i>
<a name='L110'><i><font color='green'>// 文件当前读写指针值大于文件大小,则重新设置读取字节数为文件长度-当前读写指针值,若读取数</font></i>
<a name='L111'><i><font color='green'>// 等于0,则返回0 退出),然后执行文件读操作,返回读取的字节数并退出。</font></i>
<a name='L112'> <b>if</b> (<a href='../S/43.html#L35' title='Defined at 35 in include/sys/stat.h.'>S_ISDIR</a> (inode->i_mode) || <a href='../S/43.html#L34' title='Defined at 34 in include/sys/stat.h.'>S_ISREG</a> (inode->i_mode))
<a name='L113'> <font color='red'>{</font>
<a name='L114'> <b>if</b> (<a href='../D/738.html' title='Multiple defined in 17 places.'>count</a> + file->f_pos > inode->i_size)
<a name='L115'> <a href='../D/738.html' title='Multiple defined in 17 places.'>count</a> = inode->i_size - file->f_pos;
<a name='L116'> <b>if</b> (<a href='../D/738.html' title='Multiple defined in 17 places.'>count</a> <= 0)
<a name='L117'> <b>return</b> 0;
<a name='L118'> <b>return</b> <a href='../S/10.html#L22' title='Defined at 22 in fs/file_dev.c.'>file_read</a> (inode, file, <a href='../D/716.html' title='Multiple defined in 16 places.'>buf</a>, <a href='../D/738.html' title='Multiple defined in 17 places.'>count</a>);
<a name='L119'> <font color='red'>}</font>
<a name='L120'><i><font color='green'>// 否则打印节点文件属性,并返回出错码退出。</font></i>
<a name='L121'> <a href='../S/73.html#L30' title='Defined at 30 in kernel/printk.c.'>printk</a> ("(Read)inode->i_mode=%06o\n\r", inode->i_mode);
<a name='L122'> <b>return</b> -<a href='../S/28.html#L51' title='Defined at 51 in include/errno.h.'>EINVAL</a>;
<a name='L123'><font color='red'>}</font>
<a name='L124'>
<a name='L125'><b>int</b>
<a name='L126'><a href='../R/697.html' title='Multiple refered from 2 places.'>sys_write</a> (<b>unsigned</b> <b>int</b> fd, <b>char</b> *buf, <b>int</b> count)
<a name='L127'><font color='red'>{</font>
<a name='L128'> <b>struct</b> file *file;
<a name='L129'> <b>struct</b> m_inode *inode;
<a name='L130'>
<a name='L131'><i><font color='green'>// 如果文件句柄值大于程序最多打开文件数NR_OPEN,或者需要写入的字节计数小于0,或者该句柄</font></i>
<a name='L132'><i><font color='green'>// 的文件结构指针为空,则返回出错码并退出。</font></i>
<a name='L133'> <b>if</b> (fd >= <a href='../S/31.html#L59' title='Defined at 59 in include/linux/fs.h.'>NR_OPEN</a> || <a href='../D/738.html' title='Multiple defined in 17 places.'>count</a> < 0 || !(file = current->filp[fd]))
<a name='L134'> <b>return</b> -<a href='../S/28.html#L51' title='Defined at 51 in include/errno.h.'>EINVAL</a>;
<a name='L135'><i><font color='green'>// 若需读取的字节数count 等于0,则返回0,退出</font></i>
<a name='L136'> <b>if</b> (!<a href='../D/738.html' title='Multiple defined in 17 places.'>count</a>)
<a name='L137'> <b>return</b> 0;
<a name='L138'><i><font color='green'>// 取文件对应的i 节点。若是管道文件,并且是写管道文件模式,则进行写管道操作,若成功则返回</font></i>
<a name='L139'><i><font color='green'>// 写入的字节数,否则返回出错码,退出。</font></i>
<a name='L140'> inode = file->f_inode;
<a name='L141'> <b>if</b> (inode->i_pipe)
<a name='L142'> <b>return</b> (file->f_mode & 2) ? <a href='../S/16.html#L60' title='Defined at 60 in fs/pipe.c.'>write_pipe</a> (inode, <a href='../D/716.html' title='Multiple defined in 16 places.'>buf</a>, <a href='../D/738.html' title='Multiple defined in 17 places.'>count</a>) : -<a href='../D/92.html' title='Multiple defined in 5 places.'>EIO</a>;
<a name='L143'><i><font color='green'>// 如果是字符型文件,则进行写字符设备操作,返回写入的字符数,退出。</font></i>
<a name='L144'> <b>if</b> (<a href='../S/43.html#L36' title='Defined at 36 in include/sys/stat.h.'>S_ISCHR</a> (inode->i_mode))
<a name='L145'> <b>return</b> rw_char (<a href='../S/31.html#L43' title='Defined at 43 in include/linux/fs.h.'>WRITE</a>, inode->i_zone[0], <a href='../D/716.html' title='Multiple defined in 16 places.'>buf</a>, <a href='../D/738.html' title='Multiple defined in 17 places.'>count</a>, &file->f_pos);
<a name='L146'><i><font color='green'>// 如果是块设备文件,则进行块设备写操作,并返回写入的字节数,退出。</font></i>
<a name='L147'> <b>if</b> (<a href='../S/43.html#L37' title='Defined at 37 in include/sys/stat.h.'>S_ISBLK</a> (inode->i_mode))
<a name='L148'> <b>return</b> <a href='../S/5.html#L23' title='Defined at 23 in fs/block_dev.c.'>block_write</a> (inode->i_zone[0], &file->f_pos, <a href='../D/716.html' title='Multiple defined in 16 places.'>buf</a>, <a href='../D/738.html' title='Multiple defined in 17 places.'>count</a>);
<a name='L149'><i><font color='green'>// 若是常规文件,则执行文件写操作,并返回写入的字节数,退出。</font></i>
<a name='L150'> <b>if</b> (<a href='../S/43.html#L34' title='Defined at 34 in include/sys/stat.h.'>S_ISREG</a> (inode->i_mode))
<a name='L151'> <b>return</b> <a href='../S/10.html#L75' title='Defined at 75 in fs/file_dev.c.'>file_write</a> (inode, file, <a href='../D/716.html' title='Multiple defined in 16 places.'>buf</a>, <a href='../D/738.html' title='Multiple defined in 17 places.'>count</a>);
<a name='L152'><i><font color='green'>// 否则,显示对应节点的文件模式,返回出错码,退出。</font></i>
<a name='L153'> <a href='../S/73.html#L30' title='Defined at 30 in kernel/printk.c.'>printk</a> ("(Write)inode->i_mode=%06o\n\r", inode->i_mode);
<a name='L154'> <b>return</b> -<a href='../S/28.html#L51' title='Defined at 51 in include/errno.h.'>EINVAL</a>;
<a name='L155'><font color='red'>}</font>
</pre>
<hr>
<a name='BOTTOM'>
<i><font color='green'>/* [<][>]<a href='#L37'>[^]</a><a href='#L126'>[v]</a><a href='#TOP'>[top]</a>[bottom]<a href='../mains.html'>[index]</a><a href='../help.html'>[help]</a> */</font></i>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -