how sys_open works?

孤街醉人 提交于 2020-05-08 17:55:19

问题


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

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

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.


回答1:


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

Steps are,

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

  2. 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.

  3. 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. Invokes get_empty_filp( ) to get a new file object.

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

    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. 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. 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. If the open method of the (default) file operations is defined, invokes it.

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

    h. Returns the address of the file object.

  4. Sets current->files->fd[fd] to the address of the file object.
  5. Returns fd .


来源:https://stackoverflow.com/questions/11578504/how-sys-open-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!