sys_open如何工作? [英] how sys_open works?

查看:121
本文介绍了sys_open如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个简单的char设备驱动程序(mydev),其中包含打开"文件操作.

I have write a simple char device driver (mydev) with "open" file operation in it.

在用户空间应用程序中,我打开此驱动程序节点.使用open("/dev/mydev",O_RDONLY); open()系统调用在内部调用sys_open().

In user space application I open this driver node. using open("/dev/mydev", O_RDONLY); The open() system call internally calls the sys_open().

我只想了解sys_open()函数如何调用驱动程序的打开文件操作的内容. VFS如何处理此问题,它在内部调用哪个函数.

I just want to know the follow of how sys_open() function call my driver's open file operation. How VFS handles this, which function it internally calls.

推荐答案

我在了解Linux内核"书的12.5.1节中找到了答案

I found the answer in Understanding Linux Kernel book, at section 12.5.1

步骤是

  1. 调用getname()从进程地址空间读取文件路径名.

  1. Invokes getname( ) to read the file pathname from the process address space.

调用get_unused_fd()在current-> files-> fd中找到一个空插槽.这 相应的索引(新文件描述符)存储在fd局部变量中.

Invokes get_unused_fd( ) to find an empty slot in current->files->fd. The corresponding index (the new file descriptor) is stored in the fd local variable.

调用filp_open()函数,将路径名,访问权限作为参数传递 模式标志和权限位掩码.该函数依次执行以下操作 步骤:

Invokes the filp_open( ) function, passing as parameters the pathname, the access mode flags, and the permission bit mask. This function, in turn, executes the following steps:

a.调用get_empty_filp()以获取一个新的文件对象.

a. Invokes get_empty_filp( ) to get a new file object.

b.根据以下参数设置文件对象的f_flags和f_mode字段: 标志和模式参数.

b. Sets the f_flags and f_mode fields of the file object according to the values of the flags and modes parameters.

c.调用open_namei(),它将执行以下操作:

c. Invokes open_namei( ), which executes the following operations:

   i. Invokes lookup_dentry( ) to interpret the file pathname and gets the
      dentry object associated with the requested file.

   ii. Performs a series of checks to verify whether the process is permitted
      to open the file as specified by the values of the flags parameter. If so,
      returns the address of the dentry object; otherwise, returns an error code.

d.如果访问用于写入,请检查 索引节点对象.负值表示该文件已被内存映射, 指定必须拒绝写访问(请参阅第15.2节 第15章).在这种情况下,返回错误代码.任何其他值指定 实际写入文件的进程数.在后一种情况下, 递增计数器.

d. If the access is for writing, checks the value of the i_writecount field of the inode object. A negative value means that the file has been memory-mapped, specifying that write accesses must be denied (see the section Section 15.2 in Chapter 15). In this case, returns an error code. Any other value specifies the number of processes that are actually writing into the file. In the latter case, increments the counter.

e.初始化文件对象的字段;特别是,将f_op字段设置为inode对象的i_op-> default_file_ops字段的内容.这套 设置所有正确的功能以用于将来的文件操作.

e. Initializes the fields of the file object; in particular, sets the f_op field to the contents of the i_op->default_file_ops field of the inode object. This sets up all the right functions for future file operations.

f.如果定义了(默认)文件操作的open方法,请调用它.

f. If the open method of the (default) file operations is defined, invokes it.

g清除f_flags中的O_CREAT,O_EXCL,O_NOCTTY和O_TRUNC标志.

g. Clears the O_CREAT, O_EXCL, O_NOCTTY, and O_TRUNC flags in f_flags.

h.返回文件对象的地址.

h. Returns the address of the file object.

这篇关于sys_open如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆