Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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't find 'close' entry in the struct. I think I should use 'release' instead of 'close', but I wonder why the name is 'release' and not 'close'?

like image 862
Takayuki Sato Avatar asked Jul 09 '12 10:07

Takayuki Sato


People also ask

What is the File_operations struct?

The file_operations structure is defined in linux/fs. h, and holds pointers to functions defined by the driver that perform various operations on the device. Each field of the structure corresponds to the address of some function defined by the driver to handle a requested operation.

What does struct Cdev Cdev represent in kernel space?

The struct cdev is the kernel's internal structure that represents char devices. So cdev* i_cdev field of struct inode is a pointer to cdev structure while the inode refers to the char device file. Therefore if the kernel has to invoke the character device, it has to register a structure of cdev type.

How do I release a device in Linux?

Check for device-specific errors (such as device-not-ready or similar hardware problems) Initialize the device, if it is being opened for the first time. Identify the minor number and update the f_op pointer, if necessary. Allocate and fill any data structure to be put in filp->private_data.

What is Dev_t in Linux?

Within the kernel, the dev_t type (defined in <linux/types. h>) is used to hold device numbers—both the major and minor parts. As of Version 2.6. 0 of the kernel, dev_t is a 32-bit quantity with 12 bits set aside for the major number and 20 for the minor number.


2 Answers

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

like image 197
perreal Avatar answered Oct 12 '22 11:10

perreal


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.

like image 42
Safayet Ahmed Avatar answered Oct 12 '22 12:10

Safayet Ahmed