Linux File structure

Source: Internet
Author: User
In Linux, each file has a file structure and inode structure. The inode structure is used to manage the kernel, while the file structure is usually used to read, write, or enable the file, disable. Of course, from the user's point of view, nothing can be seen. In Linux, the archive concept is widely used, and you only need to provide a set of file operations to write a driver. Let's take a look at the content of the file structure.
  
Struct file {
Struct file * f_next, ** f_pprev;
Struct dentry * f_dentry;
Struct file_operations * f_op;
Mode_t f_mode;
Loff_t f_pos;
Unsigned int f_count, f_flags;
Unsigned long f_reada, f_ramax, f_raend, f_ralen, f_rawin;
Struct fown_struct f_owner;
Unsigned long f_version;
Void * private_data;
};
  
Compared with the super_block and inode structures, the file structure is much smaller, and the file structure is also managed in serial mode. f_next will refer to the next file structure, f_pprev refers to the address of the previous file structure address. However, the usage of this field is not the same as that of the previous file structure. I will discuss it with you later, f_dentry records the inode's dentry address. f_mode indicates the type of file access. f_pos indicates the offset of the current file. Each read/write operation starts from the position of the Offset record, f_count is the reference cout of this file structure, f_flags is the mode for enabling this file, f_reada, f_ramax, f_raend, f_ralen, f_rawin is the parameter for controlling read ahead, f_owner records the ID or group ID of the trip to receive sigio and sigurg. private_data is the field used by the TTY driver. Finally, let's take a look at the f_op field. This field records a set of functions specifically used to use files.
  
· Llseek (file, offset, where)
  
We write a program to call the lseek () system to call the setting to start reading and writing from the location of the file. You do not need to provide this function, because the system already has a written, but the llseek () provided by the system () there is no way for you to set the where as seek_end, because the system does not know the length of your file, so you cannot provide such a service. If you do not provide llseek (), the system will directly use its existing llseek (). Llseek () must change the value of file-> offset.
  
· Read (file, Buf, buflen, poffset)
  
When we read an archive, we eventually call the read () function to read the archive content. These parameters VFS will be prepared for us. As for poffset, It is the pointer of offset. This is to tell read () to start reading from there. After reading, the content of poffset must be updated. Note that Buf is an address and a user space address.
  
· Write (file, Buf, buflen, poffset)
  
The write () action is the opposite of read (), and the parameters are the same. Buf is still the address of user space.
  
· Readdir (file, dirent, filldir)
  
This is used to read the next direntry of the Directory, where file is the file structure address, and dirent is a readdir_callback structure, which contains the User call readdir () the dirent structure address passed during the system call, And filldir is a function pointer. This function is provided in VFS, which actually increases the kernel's flexibility in reading dirent. When the readdir () of the file system is called, call filldir () after it extracts the next dirent (), let it write the required data into the dirent structure of the user space, and it may do more. If you are interested, you can refer to the filldir () function.
  
· Poll (file, poll_table)
  
The previous kernel version originally had a select () function in the file_operations structure instead of a poll () function. However, this does not mean that Linux does not provide select () system calls. On the contrary, Linux still provides select () system calls, but the select () system calls implement by using poll () function.
  
· IOCTL (inode, file, CMD, ARG)
  
IOCTL () is actually a very useful function, especially it can be used as a channel for the user space program to communicate with the kernel. When is IOCTL () called? Do you still remember that IOCTL () system calls are occasionally used to directly control files or devices when writing programs? Yes, the ioctl () system call finally delivers the command to the f_op-> IOCTL () file for execution. F_op-> IOCTL () is easy to do. You only need to make appropriate actions based on the CMD value and return the value. However, the ioctl () system calls are implemented in several steps. First, the system has several built-in commands that can be handled by itself. In this case, it does not call f_op-> IOCTL () for processing. If the command specified by the user is one of the following, VFS will handle it by itself.
  
O fionclex
  
Clear the close-on-exec bits of the file.
  
O fioclex
  
Set the close-on-exec bit of the file.
  
O fionbio
  
If the value transmitted by Arg is 0, the o_nonblock attribute of the file is removed. If the value is not 0, the o_nonblock attribute is set.
  
O fioasync
  
If the value passed by Arg is 0, the o_sync attribute of the file will be removed, but if it is not equal to 0, the o_sync attribute will be set. It is only in kernel 2.2.1 that the function of this attribute has not been completed.
  
If the CMD value is different from the above several types, and the file does not represent a common file, such as a special file such as device, VFS will directly call f_op-> IOCTL () for processing. However, if file represents a normal file, VFS will call file_ioctl () for additional processing. What is another solution? File_ioctl () will repeat the CMD value again. If there are the following types, it will first perform some processing and then call f_op-> IOCTL (). In any case, file_ioctl () in the end, f_op-> IOCTL () will be called for processing.
  
O fibmap
  
Obtain the file block number specified by Arg and call f_op-> bmap () to calculate the block number on its disk, finally, put the calculated block number in the ARG parameter.
  
O figetbsz
  
Obtain the block size of the file system and put it in the ARG parameter.
  
O fionread
  
Write the remaining length of the file to Arg. For example, if the file size is 1000, and the value of f_op-> offset is 300, 700 bytes are still not read. Therefore, 700 is written to the ARG parameter.
  
· MMAP (file, vmarea)
  
This function is used to map part of the file content to the memory. file refers to the file to be mapped, and vmarea is used to describe the image to the memory.
  
· Open (inode, file)
  
When we call an open () System Call to open an archive, open () will do everything well and call f_op-> open () to check whether the file system has something to do, VFS has already done a good job, so many systems actually do not provide this function. Of course, you can also provide it. For example, in this function, you can calculate the number of times the archive of this file system has been used.
  
· Flush (file)
  
This function is also newly added. This function is called when we call the close () System Call to close the file. As long as you call the close () System Call, close () will call flush (), regardless of whether the f_count value is 0 at that time. I'm not sure what I'm doing with this function. In fact, this function is not provided in ext2. Maybe before closing the file, VFS allows the file system to perform backup operations first.
  
· Release (inode, file)
  
This function is also used in the close () system call. Of course, it is not used in close (), and is also used elsewhere. Basically, this function is similar to open (). However, when we call close () for an archive, only when the f_count value is 0, VFS calls this function for processing. Generally, If you configure something in open (), you should release the configuration in release. The f_count value does not need to be controlled in open () and release (). VFS has increased or decreased f_count in fget () and fput.

  
· Fsync (file, dentry)
  
The fsync () function is mainly used by the buffer cache. It is used to write data from the file to the disk. In fact, there are two system calls in Linux, fsync () and fdatasync (), both of which call f_op-> fsync (). They are almost identical. The difference is that before fsync () calls f_op-> fsync (), semaphore is used to set f_op-> fsync () to a critical section, while fdatasync () you can directly call f_op-> fsync () without semaphore.
  
· Fasync (FD, file, on)
  
When we call the fcntl () System Call and use the f_setfl command to set the file parameters, VFS calls the fasync () function. When the file read/write operation is complete, the itinerary will receive a sigio message.
  
· Check_media_change (Dev)
  
This function is only valid for Block devices that can use removable disks, such as Mo, CDROM, and Floopy disks. Why do we need such a function to extract disks at any time? Actually, we can probably know from the literal that this is used to check whether the disk has been replaced. Taking CDROM as an example, each optical disk represents a file system. If we replace the optical disk today, it indicates that this file system does not exist. If the user reads the information of this file system at this time, what will happen? It is very likely that the system has an accident. Therefore, every time a device is mounted, we must check whether the disk in the device has been replaced. How can we check it? Of course, only the file system itself knows, so the file system must provide this function.
  
· Revalidate (Dev)
  
This function has a considerable relationship with check_media_change. When the user executes the mount command to mount a file system, the mount will first call check_disk_change (). If the device to which the file system belongs provides this function, the check_disk_change () you will first call f_op-> check_media_change () to check whether the disk has been replaced. If yes, call invalidate_inodes () and invalidate_buffers () set the buffer or inode related to the original disk as invalid. If the device to which the file system belongs still provides revalidate (), call revalidate () record the device information.
  
· Lock (file, CMD, file_lock)
  
This function is also newly added. In Linux, we can

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.