Linux-26 file opening process involves data structure analysis: http: blogcsdnnetordeder
Linux-2.6 file opening process involves data structure analysis:
Link: http://blog.csdn.net/ordeder/article/details/24420637
User level
The file is opened in the following structure:
Fd = open (path, flag, mode)
Kernel layer
After the open function is trapped in the kernel space, perform the following operations:
The system traverses the path based on the path. In linux, everything is a file, including directories, which are also considered as files. Each file has an entry structure to indicate dentry {}. if dentry records a directory file, dentry-> d_inode describes the storage information of subdirectories in the current directory. otherwise, if dentry records files, dentry-> d_inode records file-related storage information. In linux, there is a path_walk, which searches for and traverses directories based on the fields in the path parameter and dentry-> d_name. path-> dentry-> d_inode searches for subdirectory information and traverses the directories cyclically, until the dentry of the target file is found. The inode of the target file is the node pointed to by dentry-> d_inode.
3. construct the description structure file {} of the open file. after finding the I node of the target file, initialize the new file structure, as shown in the blue arrow,
File-> f_path = * path;
File-> f_inode = path-> dentry-> d_inode;
File-> f_mapping = path-> dentry-> d_inode-> I _mapping;
File-> f_mode = mode;
File-> f_op = fop;
...
4. build the relationship between fd and file and record the file address in task_struct-> files-> fd_array [fd] = & file
After the summary process opens a file, the kernel constructs a file {} structure for it to describe the file read/write mode, flag, the current read/write location f_pos, and the file operation interface f_op. The process uses task_struct-> files-> fd_array [] to record all opened files. each opened file is uniquely identified by the subscript of the array, this subscript is the return value of our open function fd (int type)