在文件成功打开之后,进程将使用内核提供的read和write系统调用,来读取或修改文件的数据。内核中文件读写操作的系统调用实现基本都一样,下面我们看看文件的读取。
-
"code" class="cpp">
-
SYSCALL_DEFINE3(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);
-
if (file) {
-
-
loff_t pos = file_pos_read(file);
"code"
class="cpp">"white-space:pre">
-
ret = vfs_read(file, buf, count, &pos);
-
-
file_pos_write(file, pos);
-
-
fput_light(file, fput_needed);
-
}
-
-
return ret;
-
}
-
-
"font-size:18px">读取实现,返回读取字节数
-
"code"
class="cpp">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;
-
}
-
阅读(1739) | 评论(0) | 转发(0) |