Why is the close function is called release in `struct file_operations` in the Linux kernel?

后端 未结 3 1387
礼貌的吻别
礼貌的吻别 2021-02-13 12:48

I\'m trying to make a linux kernel module, which supports open, close, read and write operations. So I want to register these functions via struct file_operations, however I can

相关标签:
3条回答
  • 2021-02-13 13:22

    Note that release isn’t invoked every time a process calls close. Whenever a file structure is shared (for example, after a fork or a dup),release won’t be invoked until all copies are closed. If you need to flush pending data when any copy is closed, you should implement the flush method.

    taken from LDD3 (chapter 3).

    0 讨论(0)
  • 2021-02-13 13:34

    Because the file may be opened multiple times, when you close a descriptor, only on the last close call for the last reference to the file invokes release. So there is a difference between close and release.

    release: called at the last close(2) of this file, i.e. when file->f_count reaches 0. Although defined as returning int, the return value is ignored by VFS (see fs/file_table.c:__fput()). more

    0 讨论(0)
  • 2021-02-13 13:44

    I had a similar confusion. Perreal is correct in that release is not called when close is called. Here is an extract from the book Linux Device Drivers 3rd edition:

    int (*flush) (struct file *);
    

    The flush operation is invoked when a process closes its copy of a file descriptor for a device; it should execute (and wait for) any outstanding operations on the device. This must not be confused with the fsync operation requested by user programs. Currently, flush is used only in the network file system (NFS) code. If flush is NULL, it is simply not invoked.

    int (*release) (struct inode *, struct file *);
    

    This operation is invoked when the file structure is being released. Like open, release can be missing.

    Note that release isn't invoked every time a process calls close. Whenever a file structure is shared (for example, after a fork or a dup), release won't be invoked until all copies are closed. If you need to flush pending data when any copy is closed, you should implement the flush method.

    0 讨论(0)
提交回复
热议问题