linux文件系统读写测试技术可行性,Linux VFS文件系统之读写(read/write)文件

从软件流程图中可以看出来, VFS处理文件的读写流程基本都是一样的.

首先按照这个流程看看read 具体是如何实现的:

asmlinkage ssize_t sys_read(unsigned int fd, char __user * buf, size_t count)

{

struct file *file;

ssize_t ret = -EBADF;

int fput_needed;

file = fget_light(fd, &fput_needed);  //获取file

if (file) {

loff_t pos = file_pos_read(file); //读取文件读写位置

ret = vfs_read(file, buf, count, &pos);  //VFS 读文件

file_pos_write(file, pos);  //回写文件读写位置

fput_light(file, fput_needed);

}

return ret;

}

因为在我们在open文件时就把文件相关的信息放在了文件描述表的file结构中(见上文) ,所以在读一个文件前,我们需要在当前进程的文件描述表中找到这个file结构 ,那么这个file结构是如何找到的呢?

那么就来看看 fget_light()代码是如何实现的:

struct file *fget_light(unsigned int fd, int *fput_needed)

{

struct file *file;

struct files_struct *files = current->files;  //取得当前进程的files_struct ,我们需要找到文件描述表

*fput_needed = 0;

if (likely((atomic_read(&files->count) == 1))) {

file = fcheck_files(files, fd);   // 由fd值, 取得 file

} else {

rcu_read_lock();

file = fcheck_files(files, fd);

if (file) {

if (atomic_inc_not_zero(&file->f_count))

*fput_needed = 1;

else

/* Didn't get the reference, someone's freed */

file = NULL;

}

rcu_read_unlock();

}

return file;

}

其中关键的一个函数为fcheck_files()  ,看看代码:

static inline struct file * fcheck_files(struct files_struct *files, unsigned int fd)

{

struct file * file = NULL;

struct fdtable *fdt = files_fdtable(files);

if (fd < fdt->max_fds)

file = rcu_dereference(fdt->fd[fd]);

return file;

}

函数files_fdtable(),就是去读取当前进程的文件描述表 ,rcu_dereference()会根据文件描述符fd取得文件描述表中的file.

接下来获取文件读写位置file->f_pos ,这个值可以通过系统调用llseek修改.

既然我们已经在当前进程的文件描述表中找到了file , 下面就需要调用保存在file中的文件操作方法(open时初始化) .

看看下面的代码就很清楚了:

ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)

{

ssize_t ret;

if (!(file->f_mode & FMODE_READ))  //判断文件是否可读

return -EBADF;

if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read))  //是否定义文件读方法

return -EINVAL;

if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))

return -EFAULT;

ret = rw_verify_area(READ, file, pos, count);  //读校验 ,

if (ret >= 0) {

count = ret;

if (file->f_op->read)

ret = file->f_op->read(file, buf, count, pos);  //调用文件读操作方法

else

ret = do_sync_read(file, buf, count, pos);  //通用文件模型读方法

if (ret > 0) {

fsnotify_access(file->f_path.dentry);

add_rchar(current, ret);

}

inc_syscr(current);

}

return ret;

}

上面的代码实现很简单,在做了一些条件判断以后 ,如果该文件索引节点inode定义了文件的读实现方法的话,就调用此方法. Linux下特殊文件读往往是用此方法, 一些伪文件系统如:proc,sysfs等,读写文件也是用此方法 . 而如果没有定义此方法就会调用通用文件模型的读写方法.它最终就是读内存,或者需要从存储介质中去读数据.

三: VFS write的实现流程

其实VFS写文件的流程和读文件是一样的,只是调用文件操作方法不同而已 .

我们只需要看看vfs_write()的代码就可以了:

ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)

{

ssize_t ret;

if (!(file->f_mode & FMODE_WRITE))

return -EBADF;

if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))

return -EINVAL;

if (unlikely(!access_ok(VERIFY_READ, buf, count)))

return -EFAULT;

ret = rw_verify_area(WRITE, file, pos, count);

if (ret >= 0) {

count = ret;

if (file->f_op->write)

ret = file->f_op->write(file, buf, count, pos);

else

ret = do_sync_write(file, buf, count, pos);

if (ret > 0) {

fsnotify_modify(file->f_path.dentry);

add_wchar(current, ret);

}

inc_syscw(current);

}

return ret;

}

可以看出这个函数和vfs_read()都是差不多的,只是调用的文件操作方法不同而已(file->f_op->write) ,如果没有定义file->f_op->write ,同样也需要do_sync_write()调用同样文件写操作, 首先把数据写到内存中,然后在适当的时候把数据同步到具体的存储介质中去.

四: 小结

其实到这里我们已经可以深深领会到linux VFS的重要性了, 它就是为文件系统提供一个通用的接口模型,是linux文件系统的奠基石.VFS也是一个比较大的系统,要想清楚的知道其中的来龙去脉,还的要去潜心研究代码了..

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值